src/etpan_mime.c
author vb
Sun, 15 Mar 2015 19:39:07 +0100
changeset 116 e4d286e7b300
parent 115 cab016770f6f
child 125 5119178815d8
permissions -rw-r--r--
...
vb@48
     1
#include <string.h>
vb@48
     2
#include <stdlib.h>
vb@48
     3
#include <unistd.h>
vb@89
     4
#include <assert.h>
vb@48
     5
vb@48
     6
#include "etpan_mime.h"
vb@62
     7
#ifndef mailmime_param_new_with_data
vb@62
     8
#include <libetpan/mailprivacy_tools.h>
vb@62
     9
#endif
vb@48
    10
vb@89
    11
time_t mail_mkgmtime(struct tm * tmp);
vb@89
    12
vb@48
    13
#define MAX_MESSAGE_ID 512
vb@48
    14
vb@116
    15
static char * generate_boundary(void)
vb@48
    16
{
vb@48
    17
    char id[MAX_MESSAGE_ID];
vb@116
    18
    long value1;
vb@116
    19
    long value2;
vb@116
    20
    long value3;
vb@116
    21
    long value4;
vb@115
    22
    int r;
vb@54
    23
 
vb@116
    24
    // no random needed here
vb@90
    25
vb@116
    26
    value1 = random();
vb@116
    27
    value2 = random();
vb@116
    28
    value3 = random();
vb@116
    29
    value4 = random();
vb@116
    30
vb@116
    31
    snprintf(id, MAX_MESSAGE_ID, "%.4lx%.4lx%.4lx%.4lx", value1, value2,
vb@116
    32
            value3, value4);
vb@48
    33
    
vb@48
    34
    return strdup(id);
vb@48
    35
}
vb@48
    36
vb@48
    37
struct mailmime * part_new_empty(
vb@48
    38
        struct mailmime_content * content,
vb@48
    39
        struct mailmime_fields * mime_fields,
vb@48
    40
        int force_single
vb@48
    41
    )
vb@48
    42
{
vb@48
    43
	struct mailmime * build_info;
vb@115
    44
	clist * list = NULL;
vb@48
    45
	int r;
vb@48
    46
	int mime_type;
vb@115
    47
    char * attr_name = NULL;
vb@115
    48
    char * attr_value = NULL;
vb@115
    49
    struct mailmime_parameter * param = NULL;
vb@115
    50
    clist * parameters = NULL;
vb@116
    51
    char *boundary = NULL;
vb@48
    52
vb@48
    53
	list = NULL;
vb@48
    54
vb@48
    55
	if (force_single) {
vb@48
    56
		mime_type = MAILMIME_SINGLE;
vb@48
    57
	}
vb@48
    58
	else {
vb@48
    59
		switch (content->ct_type->tp_type) {
vb@48
    60
			case MAILMIME_TYPE_DISCRETE_TYPE:
vb@115
    61
                mime_type = MAILMIME_SINGLE;
vb@115
    62
                break;
vb@48
    63
vb@48
    64
			case MAILMIME_TYPE_COMPOSITE_TYPE:
vb@115
    65
                switch (content->ct_type->tp_data.tp_composite_type->ct_type) {
vb@115
    66
                    case MAILMIME_COMPOSITE_TYPE_MULTIPART:
vb@115
    67
                        mime_type = MAILMIME_MULTIPLE;
vb@115
    68
                        break;
vb@48
    69
vb@115
    70
                    case MAILMIME_COMPOSITE_TYPE_MESSAGE:
vb@115
    71
                        if (strcasecmp(content->ct_subtype, "rfc822") == 0)
vb@115
    72
                            mime_type = MAILMIME_MESSAGE;
vb@115
    73
                        else
vb@115
    74
                            mime_type = MAILMIME_SINGLE;
vb@115
    75
                        break;
vb@48
    76
vb@115
    77
                    default:
vb@115
    78
                        goto enomem;
vb@115
    79
                }
vb@115
    80
                break;
vb@48
    81
vb@48
    82
			default:
vb@115
    83
                goto enomem;
vb@48
    84
		}
vb@48
    85
	}
vb@48
    86
vb@48
    87
	if (mime_type == MAILMIME_MULTIPLE) {
vb@48
    88
		list = clist_new();
vb@115
    89
        assert(list);
vb@48
    90
		if (list == NULL)
vb@115
    91
			goto enomem;
vb@48
    92
vb@48
    93
		attr_name = strdup("boundary");
vb@115
    94
        assert(attr_name);
vb@115
    95
        if (attr_name == NULL)
vb@115
    96
            goto enomem;
vb@115
    97
vb@116
    98
		boundary = generate_boundary();
vb@115
    99
        assert(boundary);
vb@48
   100
		attr_value = boundary;
vb@115
   101
		if (attr_value == NULL)
vb@115
   102
			goto enomem;
vb@48
   103
vb@48
   104
		param = mailmime_parameter_new(attr_name, attr_value);
vb@115
   105
        assert(param);
vb@115
   106
		if (param == NULL)
vb@115
   107
			goto enomem;
vb@115
   108
        attr_name = NULL;
vb@115
   109
        attr_value = NULL;
vb@48
   110
vb@48
   111
		if (content->ct_parameters == NULL) {
vb@48
   112
			parameters = clist_new();
vb@115
   113
            assert(parameters);
vb@115
   114
			if (parameters == NULL)
vb@115
   115
				goto enomem;
vb@48
   116
		}
vb@115
   117
		else {
vb@48
   118
			parameters = content->ct_parameters;
vb@115
   119
        }
vb@48
   120
vb@48
   121
		r = clist_append(parameters, param);
vb@115
   122
		if (r)
vb@115
   123
			goto enomem;
vb@115
   124
        param = NULL;
vb@48
   125
vb@48
   126
		if (content->ct_parameters == NULL)
vb@48
   127
			content->ct_parameters = parameters;
vb@48
   128
	}
vb@48
   129
vb@115
   130
    build_info = mailmime_new(mime_type, NULL, 0, mime_fields, content, NULL,
vb@115
   131
            NULL, NULL, list, NULL, NULL);
vb@115
   132
	if (build_info == NULL)
vb@115
   133
		goto enomem;
vb@48
   134
vb@48
   135
	return build_info;
vb@48
   136
vb@115
   137
enomem:
vb@115
   138
    if (list)
vb@115
   139
        clist_free(list);
vb@115
   140
    free(attr_name);
vb@115
   141
    free(attr_value);
vb@115
   142
    if (content->ct_parameters == NULL)
vb@115
   143
        if (parameters)
vb@115
   144
            clist_free(parameters);
vb@115
   145
vb@48
   146
	return NULL;
vb@48
   147
}
vb@48
   148
vb@114
   149
struct mailmime * get_pgp_encrypted_part(void)
vb@114
   150
{
vb@115
   151
	struct mailmime * mime = NULL;
vb@115
   152
	struct mailmime_fields * mime_fields = NULL;
vb@115
   153
	struct mailmime_content * content = NULL;
vb@115
   154
    int r;
vb@114
   155
vb@114
   156
	content = mailmime_content_new_with_str("application/pgp-encrypted");
vb@115
   157
    if (content == NULL)
vb@115
   158
        goto enomem;
vb@115
   159
vb@114
   160
    mime_fields = mailmime_fields_new_empty();
vb@115
   161
    if (mime_fields == NULL)
vb@115
   162
        goto enomem;
vb@115
   163
vb@116
   164
	mime = part_new_empty(content, mime_fields, 1);
vb@115
   165
    if (mime == NULL)
vb@115
   166
        goto enomem;
vb@115
   167
    mime_fields = NULL;
vb@115
   168
    content = NULL;
vb@115
   169
vb@115
   170
    r = mailmime_set_body_text(mime, "Version: 1\n", 10);
vb@115
   171
    if (r != 0)
vb@115
   172
        goto enomem;
vb@114
   173
vb@114
   174
	return mime;
vb@115
   175
vb@115
   176
enomem:
vb@115
   177
    if (content)
vb@115
   178
        mailmime_content_free(content);
vb@115
   179
    if (mime_fields)
vb@115
   180
        mailmime_fields_free(mime_fields);
vb@115
   181
    if (mime)
vb@115
   182
        mailmime_free(mime);
vb@115
   183
vb@115
   184
    return NULL;
vb@114
   185
}
vb@114
   186
vb@48
   187
struct mailmime * get_text_part(
vb@114
   188
        const char * filename,
vb@48
   189
        const char * mime_type,
vb@48
   190
        const char * text,
vb@48
   191
        size_t length,
vb@48
   192
        int encoding_type
vb@48
   193
    )
vb@48
   194
{
vb@114
   195
    char * disposition_name = NULL;
vb@115
   196
	struct mailmime_fields * mime_fields = NULL;
vb@115
   197
	struct mailmime * mime = NULL;
vb@115
   198
	struct mailmime_content * content = NULL;
vb@115
   199
	struct mailmime_parameter * param = NULL;
vb@115
   200
	struct mailmime_disposition * disposition = NULL;
vb@114
   201
	struct mailmime_mechanism * encoding = NULL;
vb@115
   202
    int r;
vb@48
   203
    
vb@115
   204
    if (filename != NULL) {
vb@114
   205
        disposition_name = strdup(filename);
vb@115
   206
        if (disposition_name == NULL)
vb@115
   207
            goto enomem;
vb@115
   208
    }
vb@114
   209
vb@115
   210
    if (encoding_type) {
vb@114
   211
        encoding = mailmime_mechanism_new(encoding_type, NULL);
vb@115
   212
        if (encoding == NULL)
vb@115
   213
            goto enomem;
vb@115
   214
    }
vb@114
   215
vb@115
   216
    disposition =
vb@115
   217
            mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
vb@115
   218
                    disposition_name, NULL, NULL, NULL, (size_t) -1);
vb@115
   219
    if (disposition == NULL)
vb@115
   220
        goto enomem;
vb@115
   221
    disposition_name = NULL;
vb@114
   222
vb@115
   223
    mime_fields = mailmime_fields_new_with_data(encoding, NULL, NULL,
vb@115
   224
            disposition, NULL);
vb@115
   225
    if (mime_fields == NULL)
vb@115
   226
        goto enomem;
vb@115
   227
    encoding = NULL;
vb@115
   228
    disposition = NULL;
vb@48
   229
vb@48
   230
	content = mailmime_content_new_with_str(mime_type);
vb@115
   231
    if (content == NULL)
vb@115
   232
        goto enomem;
vb@114
   233
    
vb@114
   234
    if (encoding_type != MAILMIME_MECHANISM_7BIT) {
vb@114
   235
        param = mailmime_param_new_with_data("charset", "utf-8");
vb@115
   236
        r = clist_append(content->ct_parameters, param);
vb@115
   237
        if (r != 0)
vb@115
   238
            goto enomem;
vb@114
   239
    }
vb@114
   240
vb@116
   241
	mime = part_new_empty(content, mime_fields, 1);
vb@115
   242
    if (mime == NULL)
vb@115
   243
        goto enomem;
vb@115
   244
    content = NULL;
vb@115
   245
    mime_fields = NULL;
vb@114
   246
vb@115
   247
    if (text) {
vb@115
   248
        r = mailmime_set_body_text(mime, (char *) text, length);
vb@115
   249
        if (r != 0)
vb@115
   250
            goto enomem;
vb@115
   251
    }
vb@48
   252
	
vb@48
   253
	return mime;
vb@115
   254
vb@115
   255
enomem:
vb@115
   256
    free(disposition_name);
vb@115
   257
    if (mime_fields)
vb@115
   258
        mailmime_fields_free(mime_fields);
vb@115
   259
    if (mime)
vb@115
   260
        mailmime_free(mime);
vb@115
   261
    if (content)
vb@115
   262
        mailmime_content_free(content);
vb@115
   263
    if (param)
vb@115
   264
        mailmime_parameter_free(param);
vb@115
   265
    if (disposition)
vb@115
   266
        mailmime_disposition_free(disposition);
vb@115
   267
    if (encoding)
vb@115
   268
        mailmime_mechanism_free(encoding);
vb@115
   269
vb@115
   270
    return NULL;
vb@48
   271
}
vb@48
   272
vb@59
   273
struct mailmime * get_file_part(
vb@59
   274
        const char * filename,
vb@59
   275
        const char * mime_type,
vb@59
   276
        char * data,
vb@59
   277
        size_t length
vb@59
   278
    )
vb@59
   279
{
vb@115
   280
    char * disposition_name = NULL;
vb@59
   281
    int encoding_type;
vb@115
   282
    struct mailmime_disposition * disposition = NULL;
vb@115
   283
    struct mailmime_mechanism * encoding = NULL;
vb@115
   284
    struct mailmime_content * content = NULL;
vb@115
   285
    struct mailmime * mime = NULL;
vb@115
   286
    struct mailmime_fields * mime_fields = NULL;
vb@115
   287
    int r;
vb@59
   288
vb@59
   289
    if (filename != NULL) {
vb@59
   290
        disposition_name = strdup(filename);
vb@115
   291
        if (disposition_name == NULL)
vb@115
   292
            goto enomem;
vb@59
   293
    }
vb@115
   294
vb@59
   295
    disposition =
vb@115
   296
            mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
vb@115
   297
                    disposition_name, NULL, NULL, NULL, (size_t) -1);
vb@115
   298
    if (disposition == NULL)
vb@115
   299
        goto enomem;
vb@115
   300
    disposition_name = NULL;
vb@115
   301
vb@59
   302
    content = mailmime_content_new_with_str(mime_type);
vb@115
   303
    if (content == NULL)
vb@115
   304
        goto enomem;
vb@59
   305
vb@59
   306
    encoding_type = MAILMIME_MECHANISM_BASE64;
vb@59
   307
    encoding = mailmime_mechanism_new(encoding_type, NULL);
vb@115
   308
    if (encoding == NULL)
vb@115
   309
        goto enomem;
vb@115
   310
vb@115
   311
    mime_fields = mailmime_fields_new_with_data(encoding, NULL, NULL,
vb@115
   312
            disposition, NULL);
vb@115
   313
    if (mime_fields == NULL)
vb@115
   314
        goto enomem;
vb@115
   315
    encoding = NULL;
vb@115
   316
    disposition = NULL;
vb@115
   317
vb@116
   318
    mime = part_new_empty(content, mime_fields, 1);
vb@115
   319
    if (mime == NULL)
vb@115
   320
        goto enomem;
vb@115
   321
    content = NULL;
vb@115
   322
    mime_fields = NULL;
vb@115
   323
vb@115
   324
    r = mailmime_set_body_text(mime, data, length);
vb@115
   325
    if (r != 0)
vb@115
   326
        goto enomem;
vb@59
   327
vb@59
   328
    return mime;
vb@115
   329
vb@115
   330
enomem:
vb@115
   331
    free(disposition_name);
vb@115
   332
    if (disposition)
vb@115
   333
        mailmime_disposition_free(disposition);
vb@115
   334
    if (encoding)
vb@115
   335
        mailmime_mechanism_free(encoding);
vb@115
   336
    if (content)
vb@115
   337
        mailmime_content_free(content);
vb@115
   338
    if (mime_fields)
vb@115
   339
        mailmime_fields_free(mime_fields);
vb@115
   340
    if (mime)
vb@115
   341
        mailmime_free(mime);
vb@115
   342
vb@115
   343
    return NULL;
vb@59
   344
}
vb@59
   345
vb@116
   346
struct mailmime * part_multiple_new(const char *type)
vb@48
   347
{
vb@115
   348
    struct mailmime_fields *mime_fields = NULL;
vb@115
   349
    struct mailmime_content *content = NULL;
vb@115
   350
    struct mailmime *mp = NULL;
vb@48
   351
    
vb@48
   352
    mime_fields = mailmime_fields_new_empty();
vb@48
   353
    if (mime_fields == NULL)
vb@115
   354
        goto enomem;
vb@48
   355
    
vb@48
   356
    content = mailmime_content_new_with_str(type);
vb@48
   357
    if (content == NULL)
vb@115
   358
        goto enomem;
vb@48
   359
    
vb@116
   360
    mp = part_new_empty(content, mime_fields, 0);
vb@48
   361
    if (mp == NULL)
vb@115
   362
        goto enomem;
vb@48
   363
    
vb@48
   364
    return mp;
vb@48
   365
    
vb@115
   366
enomem:
vb@115
   367
    if (content)
vb@115
   368
        mailmime_content_free(content);
vb@115
   369
    if (mime_fields)
vb@115
   370
        mailmime_fields_free(mime_fields);
vb@115
   371
vb@48
   372
    return NULL;
vb@48
   373
}
vb@48
   374
vb@89
   375
struct mailimf_field * _new_field(
vb@89
   376
        int type,
vb@89
   377
        _new_func_t new_func,
vb@89
   378
        void *value
vb@89
   379
    )
vb@89
   380
{
vb@89
   381
    void *data = new_func(value);
vb@89
   382
    assert(data);
vb@89
   383
    if (data == NULL)
vb@89
   384
        return NULL;
vb@89
   385
vb@89
   386
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@89
   387
    assert(result);
vb@89
   388
    if (result == NULL) {
vb@89
   389
        free(data);
vb@89
   390
        return NULL;
vb@89
   391
    }
vb@89
   392
vb@89
   393
    result->fld_type = type;
vb@89
   394
    result->fld_data.fld_return_path = data;
vb@89
   395
vb@89
   396
    return result;
vb@89
   397
}
vb@89
   398
vb@89
   399
void _free_field(struct mailimf_field *field)
vb@89
   400
{
vb@89
   401
    if (field)
vb@89
   402
        free(field->fld_data.fld_return_path);
vb@89
   403
    free(field);
vb@89
   404
}
vb@89
   405
vb@89
   406
int _append_field(
vb@89
   407
        clist *list,
vb@89
   408
        int type,
vb@89
   409
        _new_func_t new_func,
vb@89
   410
        void *value
vb@89
   411
    )
vb@89
   412
{
vb@89
   413
    int r;
vb@89
   414
    struct mailimf_field * field;
vb@89
   415
vb@89
   416
    assert(list);
vb@89
   417
    assert(new_func);
vb@89
   418
    assert(value);
vb@89
   419
vb@89
   420
    field = _new_field(type, new_func, value);
vb@89
   421
    if (field == NULL)
vb@89
   422
        return -1;
vb@89
   423
vb@89
   424
    r = clist_append(list, field);
vb@90
   425
    if (r)
vb@89
   426
        _free_field(field);
vb@89
   427
vb@89
   428
    return r;
vb@89
   429
}
vb@89
   430
vb@92
   431
// http://media2.giga.de/2014/02/Image-28.jpg
vb@92
   432
vb@89
   433
struct mailimf_date_time * timestamp_to_etpantime(const struct tm *ts)
vb@89
   434
{
vb@89
   435
    struct mailimf_date_time * result = calloc(1,
vb@89
   436
            sizeof(struct mailimf_date_time));
vb@89
   437
    assert(result);
vb@89
   438
    if (result == NULL)
vb@89
   439
        return NULL;
vb@89
   440
vb@89
   441
    assert(ts);
vb@89
   442
vb@89
   443
    result->dt_sec = ts->tm_sec;
vb@89
   444
    result->dt_min = ts->tm_min;
vb@89
   445
    result->dt_hour = ts->tm_hour;
vb@89
   446
    result->dt_day = ts->tm_mday;
vb@89
   447
    result->dt_month = ts->tm_mon + 1;
vb@89
   448
    result->dt_year = ts->tm_year + 1900;
vb@89
   449
    result->dt_zone = (int) (ts->tm_gmtoff / 36L);
vb@89
   450
vb@89
   451
    return result;
vb@89
   452
}
vb@89
   453
vb@89
   454
struct tm * etpantime_to_timestamp(const struct mailimf_date_time *et)
vb@89
   455
{
vb@89
   456
    struct tm * result = calloc(1, sizeof(struct tm));
vb@89
   457
    assert(result);
vb@89
   458
    if (result == NULL)
vb@89
   459
        return NULL;
vb@89
   460
vb@89
   461
    assert(et);
vb@89
   462
vb@89
   463
    result->tm_sec = et->dt_sec;
vb@89
   464
    result->tm_min = et->dt_min;
vb@89
   465
    result->tm_hour = et->dt_hour;
vb@89
   466
    result->tm_mday = et->dt_day;
vb@89
   467
    result->tm_mon = et->dt_month - 1;
vb@89
   468
    result->tm_year = et->dt_year - 1900;
vb@89
   469
    result->tm_gmtoff = 36L * (long) et->dt_zone;
vb@89
   470
vb@89
   471
    return result;
vb@89
   472
}
vb@89
   473
vb@90
   474
struct mailimf_mailbox * mailbox_from_string(
vb@90
   475
        const char *name,
vb@90
   476
        const char *address
vb@90
   477
    )
vb@90
   478
{
vb@90
   479
    struct mailimf_mailbox *mb = NULL;
vb@90
   480
    char *_name = NULL;
vb@90
   481
    char *_address = NULL;
vb@90
   482
vb@90
   483
    assert(address);
vb@90
   484
vb@90
   485
    _name = name ? strdup(name) : strdup("");
vb@90
   486
    if (_name == NULL)
vb@90
   487
        goto enomem;
vb@90
   488
vb@90
   489
    _address = strdup(address);
vb@90
   490
    if (_address == NULL)
vb@90
   491
        goto enomem;
vb@90
   492
vb@90
   493
    mb = mailimf_mailbox_new(_name, _address);
vb@90
   494
    assert(mb);
vb@90
   495
    if (mb == NULL)
vb@90
   496
        goto enomem;
vb@90
   497
vb@90
   498
    return mb;
vb@90
   499
vb@90
   500
enomem:
vb@90
   501
    free(_name);
vb@90
   502
    free(_address);
vb@90
   503
vb@90
   504
    return NULL;
vb@90
   505
}
vb@90
   506
vb@94
   507
struct mailimf_field * create_optional_field(
vb@94
   508
        const char *field,
vb@94
   509
        const char *value
vb@94
   510
    )
vb@94
   511
{
vb@94
   512
    char *_field = NULL;
vb@94
   513
    char *_value = NULL;
vb@94
   514
    struct mailimf_optional_field *optional_field = NULL;
vb@94
   515
vb@94
   516
    _field = strdup(field);
vb@94
   517
    if (_field == NULL)
vb@94
   518
        goto enomem;
vb@94
   519
vb@94
   520
    _value = mailmime_encode_subject_header("utf-8", value, 0);
vb@94
   521
    if (_value == NULL)
vb@94
   522
        goto enomem;
vb@94
   523
vb@94
   524
    optional_field = mailimf_optional_field_new(_field, _value);
vb@94
   525
    if (optional_field == NULL)
vb@94
   526
        goto enomem;
vb@94
   527
vb@94
   528
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@94
   529
    assert(result);
vb@94
   530
    if (result == NULL)
vb@94
   531
        goto enomem;
vb@94
   532
vb@94
   533
    result->fld_type = MAILIMF_FIELD_OPTIONAL_FIELD;
vb@94
   534
    result->fld_data.fld_optional_field = optional_field;
vb@94
   535
vb@94
   536
    return result;
vb@94
   537
vb@94
   538
enomem:
vb@94
   539
    if (optional_field) {
vb@94
   540
        mailimf_optional_field_free(optional_field);
vb@94
   541
    }
vb@94
   542
    else {
vb@94
   543
        free(_field);
vb@94
   544
        free(_value);
vb@94
   545
    }
vb@94
   546
vb@94
   547
    return NULL;
vb@94
   548
}
vb@94
   549
vb@94
   550
int _append_optional_field(
vb@94
   551
        clist *list,
vb@94
   552
        const char *field,
vb@94
   553
        const char *value
vb@94
   554
    )
vb@94
   555
{
vb@94
   556
    int r;
vb@94
   557
    struct mailimf_field * optional_field =
vb@94
   558
            create_optional_field(field, value);
vb@94
   559
vb@94
   560
    if (optional_field == NULL)
vb@94
   561
        return -1;
vb@94
   562
vb@94
   563
    r = clist_append(list, optional_field);
vb@94
   564
    if (r)
vb@94
   565
        mailimf_field_free(optional_field);
vb@94
   566
vb@94
   567
    return r;
vb@94
   568
}
vb@94
   569