src/etpan_mime.c
author Krista Bennett <krista@pep-project.org>
Fri, 01 Jun 2018 10:30:19 +0200
branchENGINE-254
changeset 2723 7194b9c8599d
parent 2011 86bbefb0e806
child 2200 644d22d740f0
permissions -rw-r--r--
close branch
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@2011
   304
                disposition =
krista@2011
   305
                    mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
krista@2011
   306
                                                       NULL, NULL, NULL, NULL, (size_t) -1);
krista@2011
   307
                    if (disposition == NULL)
krista@2011
   308
                        goto enomem;
krista@1878
   309
                break;
krista@1878
   310
            case PEP_RID_FILENAME:
krista@1878
   311
            default:
krista@1878
   312
                disposition_name = strdup(resource->rid);
krista@1878
   313
                if (disposition_name == NULL)
krista@1878
   314
                    goto enomem;
krista@1878
   315
                    
krista@1878
   316
                disposition =
krista@1878
   317
                        mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
krista@1878
   318
                                disposition_name, NULL, NULL, NULL, (size_t) -1);
krista@1878
   319
                                
krista@1878
   320
                if (disposition == NULL)
krista@1878
   321
                    goto enomem;
krista@1878
   322
                disposition_name = NULL;
krista@1878
   323
                
krista@1878
   324
                break;
krista@1878
   325
        }    
vb@59
   326
    }
krista@1878
   327
    
vb@115
   328
vb@59
   329
    content = mailmime_content_new_with_str(mime_type);
vb@115
   330
    if (content == NULL)
vb@115
   331
        goto enomem;
vb@59
   332
vb@59
   333
    encoding_type = MAILMIME_MECHANISM_BASE64;
vb@59
   334
    encoding = mailmime_mechanism_new(encoding_type, NULL);
vb@115
   335
    if (encoding == NULL)
vb@115
   336
        goto enomem;
vb@115
   337
krista@1878
   338
    mime_fields = mailmime_fields_new_with_data(encoding, content_id, NULL,
vb@115
   339
            disposition, NULL);
vb@115
   340
    if (mime_fields == NULL)
vb@115
   341
        goto enomem;
vb@115
   342
    encoding = NULL;
vb@115
   343
    disposition = NULL;
vb@115
   344
vb@116
   345
    mime = part_new_empty(content, mime_fields, 1);
vb@115
   346
    if (mime == NULL)
vb@115
   347
        goto enomem;
vb@115
   348
    content = NULL;
vb@115
   349
    mime_fields = NULL;
vb@115
   350
Edouard@747
   351
    if(length > 0)
Edouard@747
   352
    {
Edouard@747
   353
        r = mailmime_set_body_text(mime, data, length);
Edouard@747
   354
        if (r != 0)
Edouard@747
   355
            goto enomem;
Edouard@747
   356
    }
vb@59
   357
vb@59
   358
    return mime;
vb@115
   359
vb@115
   360
enomem:
vb@115
   361
    free(disposition_name);
vb@115
   362
    if (disposition)
vb@115
   363
        mailmime_disposition_free(disposition);
vb@115
   364
    if (encoding)
vb@115
   365
        mailmime_mechanism_free(encoding);
vb@115
   366
    if (content)
vb@115
   367
        mailmime_content_free(content);
vb@115
   368
    if (mime_fields)
vb@115
   369
        mailmime_fields_free(mime_fields);
vb@115
   370
    if (mime)
vb@115
   371
        mailmime_free(mime);
krista@1878
   372
    
vb@115
   373
    return NULL;
vb@59
   374
}
vb@59
   375
vb@116
   376
struct mailmime * part_multiple_new(const char *type)
vb@48
   377
{
vb@115
   378
    struct mailmime_fields *mime_fields = NULL;
vb@115
   379
    struct mailmime_content *content = NULL;
vb@115
   380
    struct mailmime *mp = NULL;
vb@48
   381
    
vb@48
   382
    mime_fields = mailmime_fields_new_empty();
vb@48
   383
    if (mime_fields == NULL)
vb@115
   384
        goto enomem;
vb@48
   385
    
vb@48
   386
    content = mailmime_content_new_with_str(type);
vb@48
   387
    if (content == NULL)
vb@115
   388
        goto enomem;
vb@48
   389
    
vb@116
   390
    mp = part_new_empty(content, mime_fields, 0);
vb@48
   391
    if (mp == NULL)
vb@115
   392
        goto enomem;
vb@48
   393
    
vb@48
   394
    return mp;
vb@48
   395
    
vb@115
   396
enomem:
vb@115
   397
    if (content)
vb@115
   398
        mailmime_content_free(content);
vb@115
   399
    if (mime_fields)
vb@115
   400
        mailmime_fields_free(mime_fields);
vb@115
   401
vb@48
   402
    return NULL;
vb@48
   403
}
vb@48
   404
vb@89
   405
struct mailimf_field * _new_field(
vb@89
   406
        int type,
vb@89
   407
        _new_func_t new_func,
vb@89
   408
        void *value
vb@89
   409
    )
vb@89
   410
{
vb@89
   411
    void *data = new_func(value);
vb@89
   412
    assert(data);
vb@89
   413
    if (data == NULL)
vb@89
   414
        return NULL;
vb@89
   415
vb@89
   416
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@89
   417
    assert(result);
vb@89
   418
    if (result == NULL) {
vb@89
   419
        free(data);
vb@89
   420
        return NULL;
vb@89
   421
    }
vb@89
   422
vb@89
   423
    result->fld_type = type;
vb@89
   424
    result->fld_data.fld_return_path = data;
vb@89
   425
vb@89
   426
    return result;
vb@89
   427
}
vb@89
   428
vb@89
   429
void _free_field(struct mailimf_field *field)
vb@89
   430
{
vb@89
   431
    if (field)
vb@89
   432
        free(field->fld_data.fld_return_path);
vb@89
   433
    free(field);
vb@89
   434
}
vb@89
   435
vb@89
   436
int _append_field(
vb@89
   437
        clist *list,
vb@89
   438
        int type,
vb@89
   439
        _new_func_t new_func,
vb@89
   440
        void *value
vb@89
   441
    )
vb@89
   442
{
vb@89
   443
    int r;
vb@89
   444
    struct mailimf_field * field;
vb@89
   445
vb@89
   446
    assert(list);
vb@89
   447
    assert(new_func);
vb@89
   448
    assert(value);
vb@89
   449
vb@89
   450
    field = _new_field(type, new_func, value);
vb@89
   451
    if (field == NULL)
vb@89
   452
        return -1;
vb@89
   453
vb@89
   454
    r = clist_append(list, field);
vb@90
   455
    if (r)
vb@89
   456
        _free_field(field);
vb@89
   457
vb@89
   458
    return r;
vb@89
   459
}
vb@89
   460
vb@92
   461
// http://media2.giga.de/2014/02/Image-28.jpg
vb@92
   462
vb@89
   463
struct mailimf_date_time * timestamp_to_etpantime(const struct tm *ts)
vb@89
   464
{
vb@89
   465
    struct mailimf_date_time * result = calloc(1,
vb@89
   466
            sizeof(struct mailimf_date_time));
vb@89
   467
    assert(result);
vb@89
   468
    if (result == NULL)
vb@89
   469
        return NULL;
vb@89
   470
vb@89
   471
    assert(ts);
vb@89
   472
vb@89
   473
    result->dt_sec = ts->tm_sec;
vb@89
   474
    result->dt_min = ts->tm_min;
vb@89
   475
    result->dt_hour = ts->tm_hour;
vb@89
   476
    result->dt_day = ts->tm_mday;
vb@89
   477
    result->dt_month = ts->tm_mon + 1;
vb@89
   478
    result->dt_year = ts->tm_year + 1900;
vb@130
   479
#ifndef WIN32
vb@89
   480
    result->dt_zone = (int) (ts->tm_gmtoff / 36L);
vb@130
   481
#endif
vb@89
   482
    return result;
vb@89
   483
}
vb@89
   484
vb@89
   485
struct tm * etpantime_to_timestamp(const struct mailimf_date_time *et)
vb@89
   486
{
vb@89
   487
    struct tm * result = calloc(1, sizeof(struct tm));
vb@89
   488
    assert(result);
vb@89
   489
    if (result == NULL)
vb@89
   490
        return NULL;
vb@89
   491
vb@89
   492
    assert(et);
vb@89
   493
vb@89
   494
    result->tm_sec = et->dt_sec;
vb@89
   495
    result->tm_min = et->dt_min;
vb@89
   496
    result->tm_hour = et->dt_hour;
vb@89
   497
    result->tm_mday = et->dt_day;
vb@89
   498
    result->tm_mon = et->dt_month - 1;
vb@89
   499
    result->tm_year = et->dt_year - 1900;
vb@130
   500
#ifndef WIN32
vb@89
   501
    result->tm_gmtoff = 36L * (long) et->dt_zone;
vb@130
   502
#endif
vb@89
   503
    return result;
vb@89
   504
}
vb@89
   505
vb@90
   506
struct mailimf_mailbox * mailbox_from_string(
vb@90
   507
        const char *name,
vb@90
   508
        const char *address
vb@90
   509
    )
vb@90
   510
{
vb@90
   511
    struct mailimf_mailbox *mb = NULL;
vb@90
   512
    char *_name = NULL;
vb@90
   513
    char *_address = NULL;
vb@90
   514
vb@90
   515
    assert(address);
vb@90
   516
vb@90
   517
    _name = name ? strdup(name) : strdup("");
vb@90
   518
    if (_name == NULL)
vb@90
   519
        goto enomem;
vb@90
   520
vb@90
   521
    _address = strdup(address);
vb@90
   522
    if (_address == NULL)
vb@90
   523
        goto enomem;
vb@90
   524
vb@90
   525
    mb = mailimf_mailbox_new(_name, _address);
vb@90
   526
    assert(mb);
vb@90
   527
    if (mb == NULL)
vb@90
   528
        goto enomem;
vb@90
   529
vb@90
   530
    return mb;
vb@90
   531
vb@90
   532
enomem:
vb@90
   533
    free(_name);
vb@90
   534
    free(_address);
vb@90
   535
vb@90
   536
    return NULL;
vb@90
   537
}
vb@90
   538
vb@94
   539
struct mailimf_field * create_optional_field(
vb@94
   540
        const char *field,
vb@94
   541
        const char *value
vb@94
   542
    )
vb@94
   543
{
vb@94
   544
    char *_field = NULL;
vb@94
   545
    char *_value = NULL;
vb@94
   546
    struct mailimf_optional_field *optional_field = NULL;
vb@94
   547
vb@94
   548
    _field = strdup(field);
vb@94
   549
    if (_field == NULL)
vb@94
   550
        goto enomem;
vb@94
   551
vb@94
   552
    _value = mailmime_encode_subject_header("utf-8", value, 0);
vb@94
   553
    if (_value == NULL)
vb@94
   554
        goto enomem;
vb@94
   555
vb@94
   556
    optional_field = mailimf_optional_field_new(_field, _value);
vb@94
   557
    if (optional_field == NULL)
vb@94
   558
        goto enomem;
vb@94
   559
vb@94
   560
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@94
   561
    assert(result);
vb@94
   562
    if (result == NULL)
vb@94
   563
        goto enomem;
vb@94
   564
vb@94
   565
    result->fld_type = MAILIMF_FIELD_OPTIONAL_FIELD;
vb@94
   566
    result->fld_data.fld_optional_field = optional_field;
vb@94
   567
vb@94
   568
    return result;
vb@94
   569
vb@94
   570
enomem:
vb@94
   571
    if (optional_field) {
vb@94
   572
        mailimf_optional_field_free(optional_field);
vb@94
   573
    }
vb@94
   574
    else {
vb@94
   575
        free(_field);
vb@94
   576
        free(_value);
vb@94
   577
    }
vb@94
   578
vb@94
   579
    return NULL;
vb@94
   580
}
vb@94
   581
vb@94
   582
int _append_optional_field(
vb@94
   583
        clist *list,
vb@94
   584
        const char *field,
vb@94
   585
        const char *value
vb@94
   586
    )
vb@94
   587
{
vb@94
   588
    int r;
vb@94
   589
    struct mailimf_field * optional_field =
vb@94
   590
            create_optional_field(field, value);
vb@94
   591
vb@94
   592
    if (optional_field == NULL)
vb@94
   593
        return -1;
vb@94
   594
vb@94
   595
    r = clist_append(list, optional_field);
vb@94
   596
    if (r)
vb@94
   597
        mailimf_field_free(optional_field);
vb@94
   598
vb@94
   599
    return r;
vb@94
   600
}
vb@94
   601
vb@147
   602
clist * _get_fields(struct mailmime * mime)
vb@147
   603
{
vb@147
   604
    clist * _fieldlist = NULL;
vb@147
   605
vb@147
   606
    assert(mime);
vb@147
   607
vb@147
   608
    if (mime->mm_data.mm_message.mm_fields &&
vb@147
   609
            mime->mm_data.mm_message.mm_fields->fld_list) {
vb@147
   610
        _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
vb@147
   611
    }
vb@147
   612
vb@147
   613
    return _fieldlist;
vb@147
   614
}
vb@147
   615
vb@147
   616
struct mailmime_content * _get_content(struct mailmime * mime)
vb@147
   617
{
vb@147
   618
    struct mailmime_content * content = NULL;
vb@147
   619
vb@147
   620
    assert(mime);
vb@147
   621
vb@147
   622
    if (mime->mm_data.mm_message.mm_msg_mime)
vb@147
   623
        content = mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
vb@147
   624
vb@147
   625
    return content;
vb@147
   626
}
vb@147
   627
krista@1872
   628
krista@1873
   629
/* Return a list of identifier_type and resource id (filename, cid, etc) */
krista@1873
   630
pEp_rid_list_t* _get_resource_id_list(struct mailmime *mime)
krista@1873
   631
{
krista@1873
   632
    clist * _fieldlist = NULL;
krista@1872
   633
krista@1873
   634
    assert(mime);
krista@1873
   635
krista@1873
   636
    if (mime->mm_mime_fields && mime->mm_mime_fields->fld_list)
krista@1873
   637
        _fieldlist = mime->mm_mime_fields->fld_list;
krista@1873
   638
    else
krista@1873
   639
        return NULL;
krista@1873
   640
krista@1873
   641
    clistiter *cur;
krista@1873
   642
krista@1873
   643
    pEp_rid_list_t* rid_list = NULL; 
krista@1873
   644
    pEp_rid_list_t** rid_list_curr_p = &rid_list; 
krista@1873
   645
        
krista@1873
   646
    for (cur = clist_begin(_fieldlist); cur; cur = clist_next(cur)) {
krista@1873
   647
        struct mailmime_field * _field = clist_content(cur);
krista@1873
   648
        /* content_id */
krista@1873
   649
        if (_field && _field->fld_type == MAILMIME_FIELD_ID) {
krista@1900
   650
            pEp_rid_list_t* new_rid = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
krista@1873
   651
            new_rid->rid_type = PEP_RID_CID;
krista@1900
   652
            new_rid->rid = strdup(_field->fld_data.fld_id);
krista@1873
   653
            *rid_list_curr_p = new_rid;
krista@1873
   654
            rid_list_curr_p = &new_rid->next;
krista@1873
   655
        }
krista@1873
   656
        else if (_field && _field->fld_type == MAILMIME_FIELD_DISPOSITION) {
krista@1873
   657
            /* filename */
krista@1873
   658
            if (_field->fld_data.fld_disposition &&
krista@1873
   659
                    _field->fld_data.fld_disposition->dsp_parms) {
krista@1873
   660
                clist * _parmlist =
krista@1873
   661
                        _field->fld_data.fld_disposition->dsp_parms;
krista@1873
   662
                clistiter *cur2;
krista@1873
   663
                for (cur2 = clist_begin(_parmlist); cur2; cur2 =
krista@1873
   664
                        clist_next(cur2)) {
krista@1873
   665
                    struct mailmime_disposition_parm * param =
krista@1873
   666
                            clist_content(cur2);
krista@1873
   667
                    if (param->pa_type == MAILMIME_DISPOSITION_PARM_FILENAME) {
krista@1900
   668
                        pEp_rid_list_t* new_rid = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
krista@1873
   669
                        new_rid->rid_type = PEP_RID_FILENAME;
krista@1900
   670
                        new_rid->rid = strdup(param->pa_data.pa_filename);
krista@1873
   671
                        *rid_list_curr_p = new_rid;
krista@1873
   672
                        rid_list_curr_p = &new_rid->next;
krista@1873
   673
                    }                
krista@1873
   674
                }
krista@1873
   675
            }
krista@1873
   676
        }
krista@1873
   677
    }
krista@1873
   678
    /* Will almost certainly usually be a singleton, but we need to be able to decide */
krista@1873
   679
    return rid_list;
krista@1873
   680
}
krista@1873
   681
krista@1873
   682
krista@1873
   683
/* FIXME: about to be obsoleted? */
krista@1872
   684
char * _get_filename_or_cid(struct mailmime *mime)
vb@154
   685
{
vb@154
   686
    clist * _fieldlist = NULL;
vb@154
   687
vb@154
   688
    assert(mime);
vb@154
   689
vb@154
   690
    if (mime->mm_mime_fields && mime->mm_mime_fields->fld_list)
vb@154
   691
        _fieldlist = mime->mm_mime_fields->fld_list;
vb@154
   692
    else
vb@154
   693
        return NULL;
vb@154
   694
vb@154
   695
    clistiter *cur;
krista@1873
   696
    
krista@1873
   697
    char* _temp_filename_ptr = NULL;
krista@1873
   698
    
vb@154
   699
    for (cur = clist_begin(_fieldlist); cur; cur = clist_next(cur)) {
vb@154
   700
        struct mailmime_field * _field = clist_content(cur);
krista@1873
   701
        if (_field && _field->fld_type == MAILMIME_FIELD_ID) {
krista@1873
   702
            /* We prefer CIDs to filenames when both are present */
krista@1873
   703
            free(_temp_filename_ptr); /* can be null, it's ok */
krista@1881
   704
            return build_uri("cid", _field->fld_data.fld_id); 
krista@1873
   705
        }
krista@1873
   706
        else if (_field && _field->fld_type == MAILMIME_FIELD_DISPOSITION) {
vb@154
   707
            if (_field->fld_data.fld_disposition &&
krista@1873
   708
                    _field->fld_data.fld_disposition->dsp_parms &&
krista@1873
   709
                    !_temp_filename_ptr) {
vb@154
   710
                clist * _parmlist =
vb@154
   711
                        _field->fld_data.fld_disposition->dsp_parms;
vb@154
   712
                clistiter *cur2;
vb@154
   713
                for (cur2 = clist_begin(_parmlist); cur2; cur2 =
vb@154
   714
                        clist_next(cur2)) {
vb@156
   715
                    struct mailmime_disposition_parm * param =
vb@156
   716
                            clist_content(cur2);
krista@1873
   717
                    if (param->pa_type == MAILMIME_DISPOSITION_PARM_FILENAME) {
krista@1881
   718
                        _temp_filename_ptr = build_uri("file", param->pa_data.pa_filename);
krista@1873
   719
                        break;
krista@1873
   720
                    }                
vb@154
   721
                }
vb@154
   722
            }
vb@154
   723
        }
vb@154
   724
    }
krista@1873
   725
    /* Ok, it wasn't a CID */
krista@1873
   726
    return _temp_filename_ptr;
vb@154
   727
}
vb@154
   728
vb@157
   729
static bool parameter_has_value(
vb@152
   730
        struct mailmime_content *content,       
vb@152
   731
        const char *name,
vb@152
   732
        const char *value
vb@152
   733
    )
vb@152
   734
{
vb@152
   735
    clistiter *cur;
vb@152
   736
vb@152
   737
    assert(name);
vb@152
   738
    assert(value);
vb@152
   739
vb@152
   740
    clist * list = content->ct_parameters;
vb@152
   741
    if (list == NULL)
vb@152
   742
        return false;
vb@152
   743
vb@152
   744
    for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@152
   745
        struct mailmime_parameter * param = clist_content(cur);
vb@152
   746
        if (param &&
vb@165
   747
                param->pa_name && strcasecmp(name, param->pa_name) == 0 &&
vb@165
   748
                param->pa_value && strcasecmp(value, param->pa_value) == 0)
vb@152
   749
            return true;
vb@152
   750
    }
vb@152
   751
vb@152
   752
    return false;
vb@152
   753
}
vb@152
   754
vb@151
   755
bool _is_multipart(struct mailmime_content *content, const char *subtype)
vb@147
   756
{
vb@147
   757
    assert(content);
vb@147
   758
vb@147
   759
    if (content->ct_type && content->ct_type->tp_type ==
vb@147
   760
            MAILMIME_TYPE_COMPOSITE_TYPE &&
vb@147
   761
            content->ct_type->tp_data.tp_composite_type &&
vb@147
   762
            content->ct_type->tp_data.tp_composite_type->ct_type ==
vb@151
   763
            MAILMIME_COMPOSITE_TYPE_MULTIPART) {
vb@151
   764
        if (subtype)
vb@151
   765
            return content->ct_subtype &&
vb@165
   766
                    strcasecmp(content->ct_subtype, subtype) == 0;
vb@151
   767
        else
vb@151
   768
            return true;
vb@151
   769
    }
vb@147
   770
vb@151
   771
    return false;
vb@147
   772
}
vb@147
   773
vb@147
   774
bool _is_PGP_MIME(struct mailmime_content *content)
vb@147
   775
{
vb@147
   776
    assert(content);
vb@147
   777
vb@151
   778
    if (_is_multipart(content, "encrypted") &&
vb@152
   779
            parameter_has_value(content, "protocol",
vb@147
   780
                    "application/pgp-encrypted"))
vb@151
   781
        return true;
vb@147
   782
vb@151
   783
    return false;
vb@147
   784
}
vb@147
   785
vb@152
   786
bool _is_text_part(struct mailmime_content *content, const char *subtype)
vb@152
   787
{
vb@152
   788
    assert(content);
vb@152
   789
vb@153
   790
    if (content->ct_type && content->ct_type->tp_type ==
vb@153
   791
            MAILMIME_TYPE_DISCRETE_TYPE &&
vb@153
   792
            content->ct_type->tp_data.tp_discrete_type &&
vb@152
   793
            content->ct_type->tp_data.tp_discrete_type->dt_type ==
vb@152
   794
            MAILMIME_DISCRETE_TYPE_TEXT) {
vb@152
   795
        if (subtype)
vb@152
   796
            return content->ct_subtype &&
vb@165
   797
                    strcasecmp(content->ct_subtype, subtype) == 0;
vb@152
   798
        else
vb@152
   799
            return true;
vb@152
   800
    }
vb@152
   801
vb@152
   802
    return false;
vb@152
   803
}
vb@152
   804
vb@159
   805
int _get_content_type(
vb@159
   806
        const struct mailmime_content *content,
vb@159
   807
        char **type,
vb@159
   808
        char **charset
vb@159
   809
    )
vb@154
   810
{
vb@159
   811
    char *_type = NULL;
vb@159
   812
    char *_charset = NULL;
vb@154
   813
vb@154
   814
    assert(content);
vb@159
   815
    assert(type);
vb@159
   816
    assert(charset);
vb@154
   817
vb@159
   818
    *type = NULL;
vb@159
   819
    *charset = NULL;
vb@159
   820
vb@159
   821
    if (content->ct_subtype == NULL)
vb@159
   822
        return EINVAL;
vb@154
   823
vb@154
   824
    if (content->ct_type && content->ct_type->tp_data.tp_discrete_type) {
vb@154
   825
        size_t len;
vb@159
   826
        const char *_main_type;
vb@154
   827
vb@154
   828
        switch  (content->ct_type->tp_data.tp_discrete_type->dt_type) {
vb@154
   829
            case MAILMIME_DISCRETE_TYPE_TEXT:
vb@159
   830
                _main_type = "text";
vb@154
   831
                break;
vb@154
   832
            case MAILMIME_DISCRETE_TYPE_IMAGE:
vb@159
   833
                _main_type = "image";
vb@154
   834
                break;
vb@154
   835
            case MAILMIME_DISCRETE_TYPE_AUDIO:
vb@159
   836
                _main_type = "audio";
vb@154
   837
                break;
vb@154
   838
            case MAILMIME_DISCRETE_TYPE_VIDEO:
vb@159
   839
                _main_type = "video";
vb@154
   840
                break;
vb@154
   841
            case MAILMIME_DISCRETE_TYPE_APPLICATION:
vb@159
   842
                _main_type = "application";
vb@154
   843
                break;
vb@154
   844
            case MAILMIME_DISCRETE_TYPE_EXTENSION:
vb@159
   845
                _main_type = "extension";
vb@154
   846
                break;
vb@154
   847
            default:
vb@159
   848
                return EINVAL;
vb@154
   849
        }
vb@154
   850
vb@159
   851
        len = strlen(_main_type) + 1 + strlen(content->ct_subtype) + 1;
vb@159
   852
        _type = calloc(1, len);
vb@159
   853
        assert(_type);
vb@159
   854
        if (_type == NULL)
vb@159
   855
            return ENOMEM;
vb@159
   856
krista@903
   857
        strncpy(_type, _main_type, len);
krista@903
   858
        len -= strlen(_main_type);
krista@903
   859
        strncat(_type, "/", len--);
krista@903
   860
        strncat(_type, content->ct_subtype, len);
vb@159
   861
vb@159
   862
        if (content->ct_parameters) {
vb@159
   863
            clistiter *cur;
vb@159
   864
            for (cur = clist_begin(content->ct_parameters); cur; cur =
vb@159
   865
                    clist_next(cur)) {
vb@159
   866
                struct mailmime_parameter * param = clist_content(cur);
vb@165
   867
                if (param && param->pa_name && strcasecmp(param->pa_name,
vb@159
   868
                            "charset") == 0) {
vb@159
   869
                    _charset = param->pa_value;
vb@159
   870
                    break;
vb@159
   871
                }
vb@159
   872
            }
vb@159
   873
            if (_charset)
vb@159
   874
                *charset = strdup(_charset);
vb@157
   875
        }
vb@154
   876
vb@159
   877
        *type = _type;
vb@159
   878
        return 0;
vb@154
   879
    }
vb@154
   880
vb@159
   881
    return EINVAL;
vb@154
   882
}