src/etpan_mime.c
author Krista Bennett <krista@pep-project.org>
Fri, 30 Jun 2017 05:51:53 +0200
branchENGINE-9
changeset 1900 0aa3e9e7c850
parent 1881 facb0b5b0bce
child 2011 86bbefb0e806
permissions -rw-r--r--
ENGINE=9: it helps to malloc the space for the data tructure, not just a pointer. DOH.
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@48
     4
#include "etpan_mime.h"
vb@62
     5
#ifndef mailmime_param_new_with_data
vb@62
     6
#include <libetpan/mailprivacy_tools.h>
vb@62
     7
#endif
vb@48
     8
vb@130
     9
#include "platform.h"
vb@130
    10
vb@130
    11
#include <string.h>
vb@130
    12
#include <stdlib.h>
vb@130
    13
#include <assert.h>
vb@157
    14
#include <errno.h>
vb@89
    15
roker@438
    16
#define MAX_MESSAGE_ID 128
vb@48
    17
vb@116
    18
static char * generate_boundary(void)
vb@48
    19
{
vb@48
    20
    char id[MAX_MESSAGE_ID];
vb@54
    21
roker@438
    22
    // no cryptographically strong random needed here
roker@438
    23
    const long value1 = random();
roker@438
    24
    const long value2 = random();
roker@438
    25
    const long value3 = random();
roker@438
    26
    const long value4 = random();
vb@90
    27
vb@116
    28
    snprintf(id, MAX_MESSAGE_ID, "%.4lx%.4lx%.4lx%.4lx", value1, value2,
vb@116
    29
            value3, value4);
vb@48
    30
    
vb@48
    31
    return strdup(id);
vb@48
    32
}
vb@48
    33
vb@48
    34
struct mailmime * part_new_empty(
vb@48
    35
        struct mailmime_content * content,
vb@48
    36
        struct mailmime_fields * mime_fields,
vb@48
    37
        int force_single
vb@48
    38
    )
vb@48
    39
{
vb@48
    40
	struct mailmime * build_info;
vb@115
    41
	clist * list = NULL;
vb@48
    42
	int r;
vb@48
    43
	int mime_type;
vb@115
    44
    char * attr_name = NULL;
vb@115
    45
    char * attr_value = NULL;
vb@115
    46
    struct mailmime_parameter * param = NULL;
vb@115
    47
    clist * parameters = NULL;
vb@116
    48
    char *boundary = NULL;
vb@48
    49
vb@48
    50
	list = NULL;
vb@48
    51
vb@48
    52
	if (force_single) {
vb@48
    53
		mime_type = MAILMIME_SINGLE;
vb@48
    54
	}
vb@48
    55
	else {
vb@48
    56
		switch (content->ct_type->tp_type) {
vb@48
    57
			case MAILMIME_TYPE_DISCRETE_TYPE:
vb@115
    58
                mime_type = MAILMIME_SINGLE;
vb@115
    59
                break;
vb@48
    60
vb@48
    61
			case MAILMIME_TYPE_COMPOSITE_TYPE:
vb@115
    62
                switch (content->ct_type->tp_data.tp_composite_type->ct_type) {
vb@115
    63
                    case MAILMIME_COMPOSITE_TYPE_MULTIPART:
vb@115
    64
                        mime_type = MAILMIME_MULTIPLE;
vb@115
    65
                        break;
vb@48
    66
vb@115
    67
                    case MAILMIME_COMPOSITE_TYPE_MESSAGE:
vb@115
    68
                        if (strcasecmp(content->ct_subtype, "rfc822") == 0)
vb@115
    69
                            mime_type = MAILMIME_MESSAGE;
vb@115
    70
                        else
vb@115
    71
                            mime_type = MAILMIME_SINGLE;
vb@115
    72
                        break;
vb@48
    73
vb@115
    74
                    default:
vb@115
    75
                        goto enomem;
vb@115
    76
                }
vb@115
    77
                break;
vb@48
    78
vb@48
    79
			default:
vb@115
    80
                goto enomem;
vb@48
    81
		}
vb@48
    82
	}
vb@48
    83
vb@48
    84
	if (mime_type == MAILMIME_MULTIPLE) {
vb@48
    85
		list = clist_new();
vb@115
    86
        assert(list);
vb@48
    87
		if (list == NULL)
vb@115
    88
			goto enomem;
vb@48
    89
vb@48
    90
		attr_name = strdup("boundary");
vb@115
    91
        assert(attr_name);
vb@115
    92
        if (attr_name == NULL)
vb@115
    93
            goto enomem;
vb@115
    94
vb@116
    95
		boundary = generate_boundary();
vb@115
    96
        assert(boundary);
vb@48
    97
		attr_value = boundary;
vb@115
    98
		if (attr_value == NULL)
vb@115
    99
			goto enomem;
vb@48
   100
vb@48
   101
		param = mailmime_parameter_new(attr_name, attr_value);
vb@115
   102
        assert(param);
vb@115
   103
		if (param == NULL)
vb@115
   104
			goto enomem;
vb@115
   105
        attr_name = NULL;
vb@115
   106
        attr_value = NULL;
vb@48
   107
vb@48
   108
		if (content->ct_parameters == NULL) {
vb@48
   109
			parameters = clist_new();
vb@115
   110
            assert(parameters);
vb@115
   111
			if (parameters == NULL)
vb@115
   112
				goto enomem;
vb@48
   113
		}
vb@115
   114
		else {
vb@48
   115
			parameters = content->ct_parameters;
vb@115
   116
        }
vb@48
   117
vb@48
   118
		r = clist_append(parameters, param);
vb@115
   119
		if (r)
vb@115
   120
			goto enomem;
vb@115
   121
        param = NULL;
vb@48
   122
vb@48
   123
		if (content->ct_parameters == NULL)
vb@48
   124
			content->ct_parameters = parameters;
vb@48
   125
	}
vb@48
   126
vb@115
   127
    build_info = mailmime_new(mime_type, NULL, 0, mime_fields, content, NULL,
vb@115
   128
            NULL, NULL, list, NULL, NULL);
vb@115
   129
	if (build_info == NULL)
vb@115
   130
		goto enomem;
vb@48
   131
vb@48
   132
	return build_info;
vb@48
   133
vb@115
   134
enomem:
vb@115
   135
    if (list)
vb@115
   136
        clist_free(list);
vb@115
   137
    free(attr_name);
vb@115
   138
    free(attr_value);
vb@115
   139
    if (content->ct_parameters == NULL)
vb@115
   140
        if (parameters)
vb@115
   141
            clist_free(parameters);
krista@819
   142
    if (param)
krista@819
   143
        mailmime_parameter_free(param);
vb@48
   144
	return NULL;
vb@48
   145
}
vb@48
   146
vb@114
   147
struct mailmime * get_pgp_encrypted_part(void)
vb@114
   148
{
vb@115
   149
	struct mailmime * mime = NULL;
vb@115
   150
	struct mailmime_fields * mime_fields = NULL;
vb@115
   151
	struct mailmime_content * content = NULL;
vb@115
   152
    int r;
vb@114
   153
vb@114
   154
	content = mailmime_content_new_with_str("application/pgp-encrypted");
vb@115
   155
    if (content == NULL)
vb@115
   156
        goto enomem;
vb@115
   157
vb@114
   158
    mime_fields = mailmime_fields_new_empty();
vb@115
   159
    if (mime_fields == NULL)
vb@115
   160
        goto enomem;
vb@115
   161
vb@116
   162
	mime = part_new_empty(content, mime_fields, 1);
vb@115
   163
    if (mime == NULL)
vb@115
   164
        goto enomem;
vb@115
   165
    mime_fields = NULL;
vb@115
   166
    content = NULL;
vb@115
   167
vb@115
   168
    r = mailmime_set_body_text(mime, "Version: 1\n", 10);
vb@115
   169
    if (r != 0)
vb@115
   170
        goto enomem;
vb@114
   171
vb@114
   172
	return mime;
vb@115
   173
vb@115
   174
enomem:
vb@115
   175
    if (content)
vb@115
   176
        mailmime_content_free(content);
vb@115
   177
    if (mime_fields)
vb@115
   178
        mailmime_fields_free(mime_fields);
vb@115
   179
    if (mime)
vb@115
   180
        mailmime_free(mime);
vb@115
   181
vb@115
   182
    return NULL;
vb@114
   183
}
vb@114
   184
vb@48
   185
struct mailmime * get_text_part(
krista@1878
   186
        pEp_rid_list_t* resource,
vb@48
   187
        const char * mime_type,
vb@48
   188
        const char * text,
vb@48
   189
        size_t length,
vb@48
   190
        int encoding_type
vb@48
   191
    )
vb@48
   192
{
vb@114
   193
    char * disposition_name = NULL;
vb@115
   194
	struct mailmime_fields * mime_fields = NULL;
vb@115
   195
	struct mailmime * mime = NULL;
vb@115
   196
	struct mailmime_content * content = NULL;
vb@115
   197
	struct mailmime_parameter * param = NULL;
vb@115
   198
	struct mailmime_disposition * disposition = NULL;
vb@114
   199
	struct mailmime_mechanism * encoding = NULL;
krista@1878
   200
    char* content_id = NULL;
vb@115
   201
    int r;
krista@1878
   202
                
krista@1878
   203
    if (resource != NULL && resource->rid != NULL) {
krista@1878
   204
        switch (resource->rid_type) {
krista@1878
   205
            case PEP_RID_CID:
krista@1878
   206
                content_id = strdup(resource->rid);
krista@1878
   207
                break;
krista@1878
   208
            case PEP_RID_FILENAME:
krista@1878
   209
            default:
krista@1878
   210
                disposition_name = strdup(resource->rid);
krista@1878
   211
                if (disposition_name == NULL)
krista@1878
   212
                    goto enomem;
krista@1878
   213
                    
krista@1878
   214
                disposition =
krista@1878
   215
                        mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
krista@1878
   216
                                disposition_name, NULL, NULL, NULL, (size_t) -1);
krista@1878
   217
krista@1878
   218
                if (disposition == NULL)
krista@1878
   219
                    goto enomem;
krista@1878
   220
krista@1878
   221
                disposition_name = NULL;                
krista@1878
   222
                break;
krista@1878
   223
        }    
krista@1878
   224
    }
vb@48
   225
    
vb@115
   226
    if (encoding_type) {
vb@114
   227
        encoding = mailmime_mechanism_new(encoding_type, NULL);
vb@115
   228
        if (encoding == NULL)
vb@115
   229
            goto enomem;
vb@115
   230
    }
vb@114
   231
krista@1878
   232
    mime_fields = mailmime_fields_new_with_data(encoding, content_id, NULL,
vb@115
   233
            disposition, NULL);
vb@115
   234
    if (mime_fields == NULL)
vb@115
   235
        goto enomem;
vb@115
   236
    encoding = NULL;
vb@115
   237
    disposition = NULL;
krista@1878
   238
    content_id = NULL;
vb@48
   239
vb@48
   240
	content = mailmime_content_new_with_str(mime_type);
vb@115
   241
    if (content == NULL)
vb@115
   242
        goto enomem;
vb@114
   243
    
vb@114
   244
    if (encoding_type != MAILMIME_MECHANISM_7BIT) {
vb@114
   245
        param = mailmime_param_new_with_data("charset", "utf-8");
vb@115
   246
        r = clist_append(content->ct_parameters, param);
vb@115
   247
        if (r != 0)
vb@115
   248
            goto enomem;
vb@114
   249
    }
vb@114
   250
vb@116
   251
	mime = part_new_empty(content, mime_fields, 1);
vb@115
   252
    if (mime == NULL)
vb@115
   253
        goto enomem;
vb@115
   254
    content = NULL;
vb@115
   255
    mime_fields = NULL;
vb@114
   256
vb@115
   257
    if (text) {
vb@115
   258
        r = mailmime_set_body_text(mime, (char *) text, length);
vb@115
   259
        if (r != 0)
vb@115
   260
            goto enomem;
vb@115
   261
    }
vb@48
   262
	
vb@48
   263
	return mime;
vb@115
   264
vb@115
   265
enomem:
vb@115
   266
    free(disposition_name);
vb@115
   267
    if (mime_fields)
vb@115
   268
        mailmime_fields_free(mime_fields);
vb@115
   269
    if (mime)
vb@115
   270
        mailmime_free(mime);
vb@115
   271
    if (content)
vb@115
   272
        mailmime_content_free(content);
vb@115
   273
    if (param)
vb@115
   274
        mailmime_parameter_free(param);
vb@115
   275
    if (disposition)
vb@115
   276
        mailmime_disposition_free(disposition);
vb@115
   277
    if (encoding)
vb@115
   278
        mailmime_mechanism_free(encoding);
vb@115
   279
vb@115
   280
    return NULL;
vb@48
   281
}
vb@48
   282
vb@59
   283
struct mailmime * get_file_part(
krista@1878
   284
        pEp_rid_list_t* resource,
vb@59
   285
        const char * mime_type,
vb@59
   286
        char * data,
vb@59
   287
        size_t length
vb@59
   288
    )
vb@59
   289
{
vb@115
   290
    char * disposition_name = NULL;
vb@59
   291
    int encoding_type;
vb@115
   292
    struct mailmime_disposition * disposition = NULL;
vb@115
   293
    struct mailmime_mechanism * encoding = NULL;
vb@115
   294
    struct mailmime_content * content = NULL;
vb@115
   295
    struct mailmime * mime = NULL;
vb@115
   296
    struct mailmime_fields * mime_fields = NULL;
krista@1878
   297
    char* content_id = NULL;
vb@115
   298
    int r;
krista@1878
   299
                
krista@1878
   300
    if (resource != NULL && resource->rid != NULL) {
krista@1878
   301
        switch (resource->rid_type) {
krista@1878
   302
            case PEP_RID_CID:
krista@1878
   303
                content_id = strdup(resource->rid);
krista@1878
   304
                break;
krista@1878
   305
            case PEP_RID_FILENAME:
krista@1878
   306
            default:
krista@1878
   307
                disposition_name = strdup(resource->rid);
krista@1878
   308
                if (disposition_name == NULL)
krista@1878
   309
                    goto enomem;
krista@1878
   310
                    
krista@1878
   311
                disposition =
krista@1878
   312
                        mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
krista@1878
   313
                                disposition_name, NULL, NULL, NULL, (size_t) -1);
krista@1878
   314
                                
krista@1878
   315
                if (disposition == NULL)
krista@1878
   316
                    goto enomem;
krista@1878
   317
                disposition_name = NULL;
krista@1878
   318
                
krista@1878
   319
                break;
krista@1878
   320
        }    
vb@59
   321
    }
krista@1878
   322
    
vb@115
   323
vb@59
   324
    content = mailmime_content_new_with_str(mime_type);
vb@115
   325
    if (content == NULL)
vb@115
   326
        goto enomem;
vb@59
   327
vb@59
   328
    encoding_type = MAILMIME_MECHANISM_BASE64;
vb@59
   329
    encoding = mailmime_mechanism_new(encoding_type, NULL);
vb@115
   330
    if (encoding == NULL)
vb@115
   331
        goto enomem;
vb@115
   332
krista@1878
   333
    mime_fields = mailmime_fields_new_with_data(encoding, content_id, NULL,
vb@115
   334
            disposition, NULL);
vb@115
   335
    if (mime_fields == NULL)
vb@115
   336
        goto enomem;
vb@115
   337
    encoding = NULL;
vb@115
   338
    disposition = NULL;
vb@115
   339
vb@116
   340
    mime = part_new_empty(content, mime_fields, 1);
vb@115
   341
    if (mime == NULL)
vb@115
   342
        goto enomem;
vb@115
   343
    content = NULL;
vb@115
   344
    mime_fields = NULL;
vb@115
   345
Edouard@747
   346
    if(length > 0)
Edouard@747
   347
    {
Edouard@747
   348
        r = mailmime_set_body_text(mime, data, length);
Edouard@747
   349
        if (r != 0)
Edouard@747
   350
            goto enomem;
Edouard@747
   351
    }
vb@59
   352
vb@59
   353
    return mime;
vb@115
   354
vb@115
   355
enomem:
vb@115
   356
    free(disposition_name);
vb@115
   357
    if (disposition)
vb@115
   358
        mailmime_disposition_free(disposition);
vb@115
   359
    if (encoding)
vb@115
   360
        mailmime_mechanism_free(encoding);
vb@115
   361
    if (content)
vb@115
   362
        mailmime_content_free(content);
vb@115
   363
    if (mime_fields)
vb@115
   364
        mailmime_fields_free(mime_fields);
vb@115
   365
    if (mime)
vb@115
   366
        mailmime_free(mime);
krista@1878
   367
    
vb@115
   368
    return NULL;
vb@59
   369
}
vb@59
   370
vb@116
   371
struct mailmime * part_multiple_new(const char *type)
vb@48
   372
{
vb@115
   373
    struct mailmime_fields *mime_fields = NULL;
vb@115
   374
    struct mailmime_content *content = NULL;
vb@115
   375
    struct mailmime *mp = NULL;
vb@48
   376
    
vb@48
   377
    mime_fields = mailmime_fields_new_empty();
vb@48
   378
    if (mime_fields == NULL)
vb@115
   379
        goto enomem;
vb@48
   380
    
vb@48
   381
    content = mailmime_content_new_with_str(type);
vb@48
   382
    if (content == NULL)
vb@115
   383
        goto enomem;
vb@48
   384
    
vb@116
   385
    mp = part_new_empty(content, mime_fields, 0);
vb@48
   386
    if (mp == NULL)
vb@115
   387
        goto enomem;
vb@48
   388
    
vb@48
   389
    return mp;
vb@48
   390
    
vb@115
   391
enomem:
vb@115
   392
    if (content)
vb@115
   393
        mailmime_content_free(content);
vb@115
   394
    if (mime_fields)
vb@115
   395
        mailmime_fields_free(mime_fields);
vb@115
   396
vb@48
   397
    return NULL;
vb@48
   398
}
vb@48
   399
vb@89
   400
struct mailimf_field * _new_field(
vb@89
   401
        int type,
vb@89
   402
        _new_func_t new_func,
vb@89
   403
        void *value
vb@89
   404
    )
vb@89
   405
{
vb@89
   406
    void *data = new_func(value);
vb@89
   407
    assert(data);
vb@89
   408
    if (data == NULL)
vb@89
   409
        return NULL;
vb@89
   410
vb@89
   411
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@89
   412
    assert(result);
vb@89
   413
    if (result == NULL) {
vb@89
   414
        free(data);
vb@89
   415
        return NULL;
vb@89
   416
    }
vb@89
   417
vb@89
   418
    result->fld_type = type;
vb@89
   419
    result->fld_data.fld_return_path = data;
vb@89
   420
vb@89
   421
    return result;
vb@89
   422
}
vb@89
   423
vb@89
   424
void _free_field(struct mailimf_field *field)
vb@89
   425
{
vb@89
   426
    if (field)
vb@89
   427
        free(field->fld_data.fld_return_path);
vb@89
   428
    free(field);
vb@89
   429
}
vb@89
   430
vb@89
   431
int _append_field(
vb@89
   432
        clist *list,
vb@89
   433
        int type,
vb@89
   434
        _new_func_t new_func,
vb@89
   435
        void *value
vb@89
   436
    )
vb@89
   437
{
vb@89
   438
    int r;
vb@89
   439
    struct mailimf_field * field;
vb@89
   440
vb@89
   441
    assert(list);
vb@89
   442
    assert(new_func);
vb@89
   443
    assert(value);
vb@89
   444
vb@89
   445
    field = _new_field(type, new_func, value);
vb@89
   446
    if (field == NULL)
vb@89
   447
        return -1;
vb@89
   448
vb@89
   449
    r = clist_append(list, field);
vb@90
   450
    if (r)
vb@89
   451
        _free_field(field);
vb@89
   452
vb@89
   453
    return r;
vb@89
   454
}
vb@89
   455
vb@92
   456
// http://media2.giga.de/2014/02/Image-28.jpg
vb@92
   457
vb@89
   458
struct mailimf_date_time * timestamp_to_etpantime(const struct tm *ts)
vb@89
   459
{
vb@89
   460
    struct mailimf_date_time * result = calloc(1,
vb@89
   461
            sizeof(struct mailimf_date_time));
vb@89
   462
    assert(result);
vb@89
   463
    if (result == NULL)
vb@89
   464
        return NULL;
vb@89
   465
vb@89
   466
    assert(ts);
vb@89
   467
vb@89
   468
    result->dt_sec = ts->tm_sec;
vb@89
   469
    result->dt_min = ts->tm_min;
vb@89
   470
    result->dt_hour = ts->tm_hour;
vb@89
   471
    result->dt_day = ts->tm_mday;
vb@89
   472
    result->dt_month = ts->tm_mon + 1;
vb@89
   473
    result->dt_year = ts->tm_year + 1900;
vb@130
   474
#ifndef WIN32
vb@89
   475
    result->dt_zone = (int) (ts->tm_gmtoff / 36L);
vb@130
   476
#endif
vb@89
   477
    return result;
vb@89
   478
}
vb@89
   479
vb@89
   480
struct tm * etpantime_to_timestamp(const struct mailimf_date_time *et)
vb@89
   481
{
vb@89
   482
    struct tm * result = calloc(1, sizeof(struct tm));
vb@89
   483
    assert(result);
vb@89
   484
    if (result == NULL)
vb@89
   485
        return NULL;
vb@89
   486
vb@89
   487
    assert(et);
vb@89
   488
vb@89
   489
    result->tm_sec = et->dt_sec;
vb@89
   490
    result->tm_min = et->dt_min;
vb@89
   491
    result->tm_hour = et->dt_hour;
vb@89
   492
    result->tm_mday = et->dt_day;
vb@89
   493
    result->tm_mon = et->dt_month - 1;
vb@89
   494
    result->tm_year = et->dt_year - 1900;
vb@130
   495
#ifndef WIN32
vb@89
   496
    result->tm_gmtoff = 36L * (long) et->dt_zone;
vb@130
   497
#endif
vb@89
   498
    return result;
vb@89
   499
}
vb@89
   500
vb@90
   501
struct mailimf_mailbox * mailbox_from_string(
vb@90
   502
        const char *name,
vb@90
   503
        const char *address
vb@90
   504
    )
vb@90
   505
{
vb@90
   506
    struct mailimf_mailbox *mb = NULL;
vb@90
   507
    char *_name = NULL;
vb@90
   508
    char *_address = NULL;
vb@90
   509
vb@90
   510
    assert(address);
vb@90
   511
vb@90
   512
    _name = name ? strdup(name) : strdup("");
vb@90
   513
    if (_name == NULL)
vb@90
   514
        goto enomem;
vb@90
   515
vb@90
   516
    _address = strdup(address);
vb@90
   517
    if (_address == NULL)
vb@90
   518
        goto enomem;
vb@90
   519
vb@90
   520
    mb = mailimf_mailbox_new(_name, _address);
vb@90
   521
    assert(mb);
vb@90
   522
    if (mb == NULL)
vb@90
   523
        goto enomem;
vb@90
   524
vb@90
   525
    return mb;
vb@90
   526
vb@90
   527
enomem:
vb@90
   528
    free(_name);
vb@90
   529
    free(_address);
vb@90
   530
vb@90
   531
    return NULL;
vb@90
   532
}
vb@90
   533
vb@94
   534
struct mailimf_field * create_optional_field(
vb@94
   535
        const char *field,
vb@94
   536
        const char *value
vb@94
   537
    )
vb@94
   538
{
vb@94
   539
    char *_field = NULL;
vb@94
   540
    char *_value = NULL;
vb@94
   541
    struct mailimf_optional_field *optional_field = NULL;
vb@94
   542
vb@94
   543
    _field = strdup(field);
vb@94
   544
    if (_field == NULL)
vb@94
   545
        goto enomem;
vb@94
   546
vb@94
   547
    _value = mailmime_encode_subject_header("utf-8", value, 0);
vb@94
   548
    if (_value == NULL)
vb@94
   549
        goto enomem;
vb@94
   550
vb@94
   551
    optional_field = mailimf_optional_field_new(_field, _value);
vb@94
   552
    if (optional_field == NULL)
vb@94
   553
        goto enomem;
vb@94
   554
vb@94
   555
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@94
   556
    assert(result);
vb@94
   557
    if (result == NULL)
vb@94
   558
        goto enomem;
vb@94
   559
vb@94
   560
    result->fld_type = MAILIMF_FIELD_OPTIONAL_FIELD;
vb@94
   561
    result->fld_data.fld_optional_field = optional_field;
vb@94
   562
vb@94
   563
    return result;
vb@94
   564
vb@94
   565
enomem:
vb@94
   566
    if (optional_field) {
vb@94
   567
        mailimf_optional_field_free(optional_field);
vb@94
   568
    }
vb@94
   569
    else {
vb@94
   570
        free(_field);
vb@94
   571
        free(_value);
vb@94
   572
    }
vb@94
   573
vb@94
   574
    return NULL;
vb@94
   575
}
vb@94
   576
vb@94
   577
int _append_optional_field(
vb@94
   578
        clist *list,
vb@94
   579
        const char *field,
vb@94
   580
        const char *value
vb@94
   581
    )
vb@94
   582
{
vb@94
   583
    int r;
vb@94
   584
    struct mailimf_field * optional_field =
vb@94
   585
            create_optional_field(field, value);
vb@94
   586
vb@94
   587
    if (optional_field == NULL)
vb@94
   588
        return -1;
vb@94
   589
vb@94
   590
    r = clist_append(list, optional_field);
vb@94
   591
    if (r)
vb@94
   592
        mailimf_field_free(optional_field);
vb@94
   593
vb@94
   594
    return r;
vb@94
   595
}
vb@94
   596
vb@147
   597
clist * _get_fields(struct mailmime * mime)
vb@147
   598
{
vb@147
   599
    clist * _fieldlist = NULL;
vb@147
   600
vb@147
   601
    assert(mime);
vb@147
   602
vb@147
   603
    if (mime->mm_data.mm_message.mm_fields &&
vb@147
   604
            mime->mm_data.mm_message.mm_fields->fld_list) {
vb@147
   605
        _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
vb@147
   606
    }
vb@147
   607
vb@147
   608
    return _fieldlist;
vb@147
   609
}
vb@147
   610
vb@147
   611
struct mailmime_content * _get_content(struct mailmime * mime)
vb@147
   612
{
vb@147
   613
    struct mailmime_content * content = NULL;
vb@147
   614
vb@147
   615
    assert(mime);
vb@147
   616
vb@147
   617
    if (mime->mm_data.mm_message.mm_msg_mime)
vb@147
   618
        content = mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
vb@147
   619
vb@147
   620
    return content;
vb@147
   621
}
vb@147
   622
krista@1872
   623
krista@1873
   624
/* Return a list of identifier_type and resource id (filename, cid, etc) */
krista@1873
   625
pEp_rid_list_t* _get_resource_id_list(struct mailmime *mime)
krista@1873
   626
{
krista@1873
   627
    clist * _fieldlist = NULL;
krista@1872
   628
krista@1873
   629
    assert(mime);
krista@1873
   630
krista@1873
   631
    if (mime->mm_mime_fields && mime->mm_mime_fields->fld_list)
krista@1873
   632
        _fieldlist = mime->mm_mime_fields->fld_list;
krista@1873
   633
    else
krista@1873
   634
        return NULL;
krista@1873
   635
krista@1873
   636
    clistiter *cur;
krista@1873
   637
krista@1873
   638
    pEp_rid_list_t* rid_list = NULL; 
krista@1873
   639
    pEp_rid_list_t** rid_list_curr_p = &rid_list; 
krista@1873
   640
        
krista@1873
   641
    for (cur = clist_begin(_fieldlist); cur; cur = clist_next(cur)) {
krista@1873
   642
        struct mailmime_field * _field = clist_content(cur);
krista@1873
   643
        /* content_id */
krista@1873
   644
        if (_field && _field->fld_type == MAILMIME_FIELD_ID) {
krista@1900
   645
            pEp_rid_list_t* new_rid = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
krista@1873
   646
            new_rid->rid_type = PEP_RID_CID;
krista@1900
   647
            new_rid->rid = strdup(_field->fld_data.fld_id);
krista@1873
   648
            *rid_list_curr_p = new_rid;
krista@1873
   649
            rid_list_curr_p = &new_rid->next;
krista@1873
   650
        }
krista@1873
   651
        else if (_field && _field->fld_type == MAILMIME_FIELD_DISPOSITION) {
krista@1873
   652
            /* filename */
krista@1873
   653
            if (_field->fld_data.fld_disposition &&
krista@1873
   654
                    _field->fld_data.fld_disposition->dsp_parms) {
krista@1873
   655
                clist * _parmlist =
krista@1873
   656
                        _field->fld_data.fld_disposition->dsp_parms;
krista@1873
   657
                clistiter *cur2;
krista@1873
   658
                for (cur2 = clist_begin(_parmlist); cur2; cur2 =
krista@1873
   659
                        clist_next(cur2)) {
krista@1873
   660
                    struct mailmime_disposition_parm * param =
krista@1873
   661
                            clist_content(cur2);
krista@1873
   662
                    if (param->pa_type == MAILMIME_DISPOSITION_PARM_FILENAME) {
krista@1900
   663
                        pEp_rid_list_t* new_rid = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
krista@1873
   664
                        new_rid->rid_type = PEP_RID_FILENAME;
krista@1900
   665
                        new_rid->rid = strdup(param->pa_data.pa_filename);
krista@1873
   666
                        *rid_list_curr_p = new_rid;
krista@1873
   667
                        rid_list_curr_p = &new_rid->next;
krista@1873
   668
                    }                
krista@1873
   669
                }
krista@1873
   670
            }
krista@1873
   671
        }
krista@1873
   672
    }
krista@1873
   673
    /* Will almost certainly usually be a singleton, but we need to be able to decide */
krista@1873
   674
    return rid_list;
krista@1873
   675
}
krista@1873
   676
krista@1873
   677
krista@1873
   678
/* FIXME: about to be obsoleted? */
krista@1872
   679
char * _get_filename_or_cid(struct mailmime *mime)
vb@154
   680
{
vb@154
   681
    clist * _fieldlist = NULL;
vb@154
   682
vb@154
   683
    assert(mime);
vb@154
   684
vb@154
   685
    if (mime->mm_mime_fields && mime->mm_mime_fields->fld_list)
vb@154
   686
        _fieldlist = mime->mm_mime_fields->fld_list;
vb@154
   687
    else
vb@154
   688
        return NULL;
vb@154
   689
vb@154
   690
    clistiter *cur;
krista@1873
   691
    
krista@1873
   692
    char* _temp_filename_ptr = NULL;
krista@1873
   693
    
vb@154
   694
    for (cur = clist_begin(_fieldlist); cur; cur = clist_next(cur)) {
vb@154
   695
        struct mailmime_field * _field = clist_content(cur);
krista@1873
   696
        if (_field && _field->fld_type == MAILMIME_FIELD_ID) {
krista@1873
   697
            /* We prefer CIDs to filenames when both are present */
krista@1873
   698
            free(_temp_filename_ptr); /* can be null, it's ok */
krista@1881
   699
            return build_uri("cid", _field->fld_data.fld_id); 
krista@1873
   700
        }
krista@1873
   701
        else if (_field && _field->fld_type == MAILMIME_FIELD_DISPOSITION) {
vb@154
   702
            if (_field->fld_data.fld_disposition &&
krista@1873
   703
                    _field->fld_data.fld_disposition->dsp_parms &&
krista@1873
   704
                    !_temp_filename_ptr) {
vb@154
   705
                clist * _parmlist =
vb@154
   706
                        _field->fld_data.fld_disposition->dsp_parms;
vb@154
   707
                clistiter *cur2;
vb@154
   708
                for (cur2 = clist_begin(_parmlist); cur2; cur2 =
vb@154
   709
                        clist_next(cur2)) {
vb@156
   710
                    struct mailmime_disposition_parm * param =
vb@156
   711
                            clist_content(cur2);
krista@1873
   712
                    if (param->pa_type == MAILMIME_DISPOSITION_PARM_FILENAME) {
krista@1881
   713
                        _temp_filename_ptr = build_uri("file", param->pa_data.pa_filename);
krista@1873
   714
                        break;
krista@1873
   715
                    }                
vb@154
   716
                }
vb@154
   717
            }
vb@154
   718
        }
vb@154
   719
    }
krista@1873
   720
    /* Ok, it wasn't a CID */
krista@1873
   721
    return _temp_filename_ptr;
vb@154
   722
}
vb@154
   723
vb@157
   724
static bool parameter_has_value(
vb@152
   725
        struct mailmime_content *content,       
vb@152
   726
        const char *name,
vb@152
   727
        const char *value
vb@152
   728
    )
vb@152
   729
{
vb@152
   730
    clistiter *cur;
vb@152
   731
vb@152
   732
    assert(name);
vb@152
   733
    assert(value);
vb@152
   734
vb@152
   735
    clist * list = content->ct_parameters;
vb@152
   736
    if (list == NULL)
vb@152
   737
        return false;
vb@152
   738
vb@152
   739
    for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@152
   740
        struct mailmime_parameter * param = clist_content(cur);
vb@152
   741
        if (param &&
vb@165
   742
                param->pa_name && strcasecmp(name, param->pa_name) == 0 &&
vb@165
   743
                param->pa_value && strcasecmp(value, param->pa_value) == 0)
vb@152
   744
            return true;
vb@152
   745
    }
vb@152
   746
vb@152
   747
    return false;
vb@152
   748
}
vb@152
   749
vb@151
   750
bool _is_multipart(struct mailmime_content *content, const char *subtype)
vb@147
   751
{
vb@147
   752
    assert(content);
vb@147
   753
vb@147
   754
    if (content->ct_type && content->ct_type->tp_type ==
vb@147
   755
            MAILMIME_TYPE_COMPOSITE_TYPE &&
vb@147
   756
            content->ct_type->tp_data.tp_composite_type &&
vb@147
   757
            content->ct_type->tp_data.tp_composite_type->ct_type ==
vb@151
   758
            MAILMIME_COMPOSITE_TYPE_MULTIPART) {
vb@151
   759
        if (subtype)
vb@151
   760
            return content->ct_subtype &&
vb@165
   761
                    strcasecmp(content->ct_subtype, subtype) == 0;
vb@151
   762
        else
vb@151
   763
            return true;
vb@151
   764
    }
vb@147
   765
vb@151
   766
    return false;
vb@147
   767
}
vb@147
   768
vb@147
   769
bool _is_PGP_MIME(struct mailmime_content *content)
vb@147
   770
{
vb@147
   771
    assert(content);
vb@147
   772
vb@151
   773
    if (_is_multipart(content, "encrypted") &&
vb@152
   774
            parameter_has_value(content, "protocol",
vb@147
   775
                    "application/pgp-encrypted"))
vb@151
   776
        return true;
vb@147
   777
vb@151
   778
    return false;
vb@147
   779
}
vb@147
   780
vb@152
   781
bool _is_text_part(struct mailmime_content *content, const char *subtype)
vb@152
   782
{
vb@152
   783
    assert(content);
vb@152
   784
vb@153
   785
    if (content->ct_type && content->ct_type->tp_type ==
vb@153
   786
            MAILMIME_TYPE_DISCRETE_TYPE &&
vb@153
   787
            content->ct_type->tp_data.tp_discrete_type &&
vb@152
   788
            content->ct_type->tp_data.tp_discrete_type->dt_type ==
vb@152
   789
            MAILMIME_DISCRETE_TYPE_TEXT) {
vb@152
   790
        if (subtype)
vb@152
   791
            return content->ct_subtype &&
vb@165
   792
                    strcasecmp(content->ct_subtype, subtype) == 0;
vb@152
   793
        else
vb@152
   794
            return true;
vb@152
   795
    }
vb@152
   796
vb@152
   797
    return false;
vb@152
   798
}
vb@152
   799
vb@159
   800
int _get_content_type(
vb@159
   801
        const struct mailmime_content *content,
vb@159
   802
        char **type,
vb@159
   803
        char **charset
vb@159
   804
    )
vb@154
   805
{
vb@159
   806
    char *_type = NULL;
vb@159
   807
    char *_charset = NULL;
vb@154
   808
vb@154
   809
    assert(content);
vb@159
   810
    assert(type);
vb@159
   811
    assert(charset);
vb@154
   812
vb@159
   813
    *type = NULL;
vb@159
   814
    *charset = NULL;
vb@159
   815
vb@159
   816
    if (content->ct_subtype == NULL)
vb@159
   817
        return EINVAL;
vb@154
   818
vb@154
   819
    if (content->ct_type && content->ct_type->tp_data.tp_discrete_type) {
vb@154
   820
        size_t len;
vb@159
   821
        const char *_main_type;
vb@154
   822
vb@154
   823
        switch  (content->ct_type->tp_data.tp_discrete_type->dt_type) {
vb@154
   824
            case MAILMIME_DISCRETE_TYPE_TEXT:
vb@159
   825
                _main_type = "text";
vb@154
   826
                break;
vb@154
   827
            case MAILMIME_DISCRETE_TYPE_IMAGE:
vb@159
   828
                _main_type = "image";
vb@154
   829
                break;
vb@154
   830
            case MAILMIME_DISCRETE_TYPE_AUDIO:
vb@159
   831
                _main_type = "audio";
vb@154
   832
                break;
vb@154
   833
            case MAILMIME_DISCRETE_TYPE_VIDEO:
vb@159
   834
                _main_type = "video";
vb@154
   835
                break;
vb@154
   836
            case MAILMIME_DISCRETE_TYPE_APPLICATION:
vb@159
   837
                _main_type = "application";
vb@154
   838
                break;
vb@154
   839
            case MAILMIME_DISCRETE_TYPE_EXTENSION:
vb@159
   840
                _main_type = "extension";
vb@154
   841
                break;
vb@154
   842
            default:
vb@159
   843
                return EINVAL;
vb@154
   844
        }
vb@154
   845
vb@159
   846
        len = strlen(_main_type) + 1 + strlen(content->ct_subtype) + 1;
vb@159
   847
        _type = calloc(1, len);
vb@159
   848
        assert(_type);
vb@159
   849
        if (_type == NULL)
vb@159
   850
            return ENOMEM;
vb@159
   851
krista@903
   852
        strncpy(_type, _main_type, len);
krista@903
   853
        len -= strlen(_main_type);
krista@903
   854
        strncat(_type, "/", len--);
krista@903
   855
        strncat(_type, content->ct_subtype, len);
vb@159
   856
vb@159
   857
        if (content->ct_parameters) {
vb@159
   858
            clistiter *cur;
vb@159
   859
            for (cur = clist_begin(content->ct_parameters); cur; cur =
vb@159
   860
                    clist_next(cur)) {
vb@159
   861
                struct mailmime_parameter * param = clist_content(cur);
vb@165
   862
                if (param && param->pa_name && strcasecmp(param->pa_name,
vb@159
   863
                            "charset") == 0) {
vb@159
   864
                    _charset = param->pa_value;
vb@159
   865
                    break;
vb@159
   866
                }
vb@159
   867
            }
vb@159
   868
            if (_charset)
vb@159
   869
                *charset = strdup(_charset);
vb@157
   870
        }
vb@154
   871
vb@159
   872
        *type = _type;
vb@159
   873
        return 0;
vb@154
   874
    }
vb@154
   875
vb@159
   876
    return EINVAL;
vb@154
   877
}