src/etpan_mime.c
author Krista Grothoff <krista@pep-project.org>
Mon, 13 Feb 2017 22:13:48 +0100
branchenigmail-color-tests
changeset 1582 7dd855780eef
parent 1513 e7f7e42385b5
child 1829 909cb53fc069
permissions -rw-r--r--
closing 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(
vb@114
   186
        const char * filename,
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;
vb@115
   200
    int r;
vb@48
   201
    
vb@115
   202
    if (filename != NULL) {
vb@114
   203
        disposition_name = strdup(filename);
vb@115
   204
        if (disposition_name == NULL)
vb@115
   205
            goto enomem;
vb@115
   206
    }
vb@114
   207
vb@115
   208
    if (encoding_type) {
vb@114
   209
        encoding = mailmime_mechanism_new(encoding_type, NULL);
vb@115
   210
        if (encoding == NULL)
vb@115
   211
            goto enomem;
vb@115
   212
    }
vb@114
   213
vb@115
   214
    disposition =
vb@115
   215
            mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
vb@115
   216
                    disposition_name, NULL, NULL, NULL, (size_t) -1);
vb@115
   217
    if (disposition == NULL)
vb@115
   218
        goto enomem;
vb@115
   219
    disposition_name = NULL;
vb@114
   220
vb@115
   221
    mime_fields = mailmime_fields_new_with_data(encoding, NULL, NULL,
vb@115
   222
            disposition, NULL);
vb@115
   223
    if (mime_fields == NULL)
vb@115
   224
        goto enomem;
vb@115
   225
    encoding = NULL;
vb@115
   226
    disposition = NULL;
vb@48
   227
vb@48
   228
	content = mailmime_content_new_with_str(mime_type);
vb@115
   229
    if (content == NULL)
vb@115
   230
        goto enomem;
vb@114
   231
    
vb@114
   232
    if (encoding_type != MAILMIME_MECHANISM_7BIT) {
vb@114
   233
        param = mailmime_param_new_with_data("charset", "utf-8");
vb@115
   234
        r = clist_append(content->ct_parameters, param);
vb@115
   235
        if (r != 0)
vb@115
   236
            goto enomem;
vb@114
   237
    }
vb@114
   238
vb@116
   239
	mime = part_new_empty(content, mime_fields, 1);
vb@115
   240
    if (mime == NULL)
vb@115
   241
        goto enomem;
vb@115
   242
    content = NULL;
vb@115
   243
    mime_fields = NULL;
vb@114
   244
vb@115
   245
    if (text) {
vb@115
   246
        r = mailmime_set_body_text(mime, (char *) text, length);
vb@115
   247
        if (r != 0)
vb@115
   248
            goto enomem;
vb@115
   249
    }
vb@48
   250
	
vb@48
   251
	return mime;
vb@115
   252
vb@115
   253
enomem:
vb@115
   254
    free(disposition_name);
vb@115
   255
    if (mime_fields)
vb@115
   256
        mailmime_fields_free(mime_fields);
vb@115
   257
    if (mime)
vb@115
   258
        mailmime_free(mime);
vb@115
   259
    if (content)
vb@115
   260
        mailmime_content_free(content);
vb@115
   261
    if (param)
vb@115
   262
        mailmime_parameter_free(param);
vb@115
   263
    if (disposition)
vb@115
   264
        mailmime_disposition_free(disposition);
vb@115
   265
    if (encoding)
vb@115
   266
        mailmime_mechanism_free(encoding);
vb@115
   267
vb@115
   268
    return NULL;
vb@48
   269
}
vb@48
   270
vb@59
   271
struct mailmime * get_file_part(
vb@59
   272
        const char * filename,
vb@59
   273
        const char * mime_type,
vb@59
   274
        char * data,
vb@59
   275
        size_t length
vb@59
   276
    )
vb@59
   277
{
vb@115
   278
    char * disposition_name = NULL;
vb@59
   279
    int encoding_type;
vb@115
   280
    struct mailmime_disposition * disposition = NULL;
vb@115
   281
    struct mailmime_mechanism * encoding = NULL;
vb@115
   282
    struct mailmime_content * content = NULL;
vb@115
   283
    struct mailmime * mime = NULL;
vb@115
   284
    struct mailmime_fields * mime_fields = NULL;
vb@115
   285
    int r;
vb@59
   286
vb@59
   287
    if (filename != NULL) {
vb@59
   288
        disposition_name = strdup(filename);
vb@115
   289
        if (disposition_name == NULL)
vb@115
   290
            goto enomem;
vb@59
   291
    }
vb@115
   292
vb@59
   293
    disposition =
vb@115
   294
            mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
vb@115
   295
                    disposition_name, NULL, NULL, NULL, (size_t) -1);
vb@115
   296
    if (disposition == NULL)
vb@115
   297
        goto enomem;
vb@115
   298
    disposition_name = NULL;
vb@115
   299
vb@59
   300
    content = mailmime_content_new_with_str(mime_type);
vb@115
   301
    if (content == NULL)
vb@115
   302
        goto enomem;
vb@59
   303
vb@59
   304
    encoding_type = MAILMIME_MECHANISM_BASE64;
vb@59
   305
    encoding = mailmime_mechanism_new(encoding_type, NULL);
vb@115
   306
    if (encoding == NULL)
vb@115
   307
        goto enomem;
vb@115
   308
vb@115
   309
    mime_fields = mailmime_fields_new_with_data(encoding, NULL, NULL,
vb@115
   310
            disposition, NULL);
vb@115
   311
    if (mime_fields == NULL)
vb@115
   312
        goto enomem;
vb@115
   313
    encoding = NULL;
vb@115
   314
    disposition = NULL;
vb@115
   315
vb@116
   316
    mime = part_new_empty(content, mime_fields, 1);
vb@115
   317
    if (mime == NULL)
vb@115
   318
        goto enomem;
vb@115
   319
    content = NULL;
vb@115
   320
    mime_fields = NULL;
vb@115
   321
Edouard@747
   322
    if(length > 0)
Edouard@747
   323
    {
Edouard@747
   324
        r = mailmime_set_body_text(mime, data, length);
Edouard@747
   325
        if (r != 0)
Edouard@747
   326
            goto enomem;
Edouard@747
   327
    }
vb@59
   328
vb@59
   329
    return mime;
vb@115
   330
vb@115
   331
enomem:
vb@115
   332
    free(disposition_name);
vb@115
   333
    if (disposition)
vb@115
   334
        mailmime_disposition_free(disposition);
vb@115
   335
    if (encoding)
vb@115
   336
        mailmime_mechanism_free(encoding);
vb@115
   337
    if (content)
vb@115
   338
        mailmime_content_free(content);
vb@115
   339
    if (mime_fields)
vb@115
   340
        mailmime_fields_free(mime_fields);
vb@115
   341
    if (mime)
vb@115
   342
        mailmime_free(mime);
vb@115
   343
vb@115
   344
    return NULL;
vb@59
   345
}
vb@59
   346
vb@116
   347
struct mailmime * part_multiple_new(const char *type)
vb@48
   348
{
vb@115
   349
    struct mailmime_fields *mime_fields = NULL;
vb@115
   350
    struct mailmime_content *content = NULL;
vb@115
   351
    struct mailmime *mp = NULL;
vb@48
   352
    
vb@48
   353
    mime_fields = mailmime_fields_new_empty();
vb@48
   354
    if (mime_fields == NULL)
vb@115
   355
        goto enomem;
vb@48
   356
    
vb@48
   357
    content = mailmime_content_new_with_str(type);
vb@48
   358
    if (content == NULL)
vb@115
   359
        goto enomem;
vb@48
   360
    
vb@116
   361
    mp = part_new_empty(content, mime_fields, 0);
vb@48
   362
    if (mp == NULL)
vb@115
   363
        goto enomem;
vb@48
   364
    
vb@48
   365
    return mp;
vb@48
   366
    
vb@115
   367
enomem:
vb@115
   368
    if (content)
vb@115
   369
        mailmime_content_free(content);
vb@115
   370
    if (mime_fields)
vb@115
   371
        mailmime_fields_free(mime_fields);
vb@115
   372
vb@48
   373
    return NULL;
vb@48
   374
}
vb@48
   375
vb@89
   376
struct mailimf_field * _new_field(
vb@89
   377
        int type,
vb@89
   378
        _new_func_t new_func,
vb@89
   379
        void *value
vb@89
   380
    )
vb@89
   381
{
vb@89
   382
    void *data = new_func(value);
vb@89
   383
    assert(data);
vb@89
   384
    if (data == NULL)
vb@89
   385
        return NULL;
vb@89
   386
vb@89
   387
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@89
   388
    assert(result);
vb@89
   389
    if (result == NULL) {
vb@89
   390
        free(data);
vb@89
   391
        return NULL;
vb@89
   392
    }
vb@89
   393
vb@89
   394
    result->fld_type = type;
vb@89
   395
    result->fld_data.fld_return_path = data;
vb@89
   396
vb@89
   397
    return result;
vb@89
   398
}
vb@89
   399
vb@89
   400
void _free_field(struct mailimf_field *field)
vb@89
   401
{
vb@89
   402
    if (field)
vb@89
   403
        free(field->fld_data.fld_return_path);
vb@89
   404
    free(field);
vb@89
   405
}
vb@89
   406
vb@89
   407
int _append_field(
vb@89
   408
        clist *list,
vb@89
   409
        int type,
vb@89
   410
        _new_func_t new_func,
vb@89
   411
        void *value
vb@89
   412
    )
vb@89
   413
{
vb@89
   414
    int r;
vb@89
   415
    struct mailimf_field * field;
vb@89
   416
vb@89
   417
    assert(list);
vb@89
   418
    assert(new_func);
vb@89
   419
    assert(value);
vb@89
   420
vb@89
   421
    field = _new_field(type, new_func, value);
vb@89
   422
    if (field == NULL)
vb@89
   423
        return -1;
vb@89
   424
vb@89
   425
    r = clist_append(list, field);
vb@90
   426
    if (r)
vb@89
   427
        _free_field(field);
vb@89
   428
vb@89
   429
    return r;
vb@89
   430
}
vb@89
   431
vb@92
   432
// http://media2.giga.de/2014/02/Image-28.jpg
vb@92
   433
vb@89
   434
struct mailimf_date_time * timestamp_to_etpantime(const struct tm *ts)
vb@89
   435
{
vb@89
   436
    struct mailimf_date_time * result = calloc(1,
vb@89
   437
            sizeof(struct mailimf_date_time));
vb@89
   438
    assert(result);
vb@89
   439
    if (result == NULL)
vb@89
   440
        return NULL;
vb@89
   441
vb@89
   442
    assert(ts);
vb@89
   443
vb@89
   444
    result->dt_sec = ts->tm_sec;
vb@89
   445
    result->dt_min = ts->tm_min;
vb@89
   446
    result->dt_hour = ts->tm_hour;
vb@89
   447
    result->dt_day = ts->tm_mday;
vb@89
   448
    result->dt_month = ts->tm_mon + 1;
vb@89
   449
    result->dt_year = ts->tm_year + 1900;
vb@130
   450
#ifndef WIN32
vb@89
   451
    result->dt_zone = (int) (ts->tm_gmtoff / 36L);
vb@130
   452
#endif
vb@89
   453
    return result;
vb@89
   454
}
vb@89
   455
vb@89
   456
struct tm * etpantime_to_timestamp(const struct mailimf_date_time *et)
vb@89
   457
{
vb@89
   458
    struct tm * result = calloc(1, sizeof(struct tm));
vb@89
   459
    assert(result);
vb@89
   460
    if (result == NULL)
vb@89
   461
        return NULL;
vb@89
   462
vb@89
   463
    assert(et);
vb@89
   464
vb@89
   465
    result->tm_sec = et->dt_sec;
vb@89
   466
    result->tm_min = et->dt_min;
vb@89
   467
    result->tm_hour = et->dt_hour;
vb@89
   468
    result->tm_mday = et->dt_day;
vb@89
   469
    result->tm_mon = et->dt_month - 1;
vb@89
   470
    result->tm_year = et->dt_year - 1900;
vb@130
   471
#ifndef WIN32
vb@89
   472
    result->tm_gmtoff = 36L * (long) et->dt_zone;
vb@130
   473
#endif
vb@89
   474
    return result;
vb@89
   475
}
vb@89
   476
vb@90
   477
struct mailimf_mailbox * mailbox_from_string(
vb@90
   478
        const char *name,
vb@90
   479
        const char *address
vb@90
   480
    )
vb@90
   481
{
vb@90
   482
    struct mailimf_mailbox *mb = NULL;
vb@90
   483
    char *_name = NULL;
vb@90
   484
    char *_address = NULL;
vb@90
   485
vb@90
   486
    assert(address);
vb@90
   487
vb@90
   488
    _name = name ? strdup(name) : strdup("");
vb@90
   489
    if (_name == NULL)
vb@90
   490
        goto enomem;
vb@90
   491
vb@90
   492
    _address = strdup(address);
vb@90
   493
    if (_address == NULL)
vb@90
   494
        goto enomem;
vb@90
   495
vb@90
   496
    mb = mailimf_mailbox_new(_name, _address);
vb@90
   497
    assert(mb);
vb@90
   498
    if (mb == NULL)
vb@90
   499
        goto enomem;
vb@90
   500
vb@90
   501
    return mb;
vb@90
   502
vb@90
   503
enomem:
vb@90
   504
    free(_name);
vb@90
   505
    free(_address);
vb@90
   506
vb@90
   507
    return NULL;
vb@90
   508
}
vb@90
   509
vb@94
   510
struct mailimf_field * create_optional_field(
vb@94
   511
        const char *field,
vb@94
   512
        const char *value
vb@94
   513
    )
vb@94
   514
{
vb@94
   515
    char *_field = NULL;
vb@94
   516
    char *_value = NULL;
vb@94
   517
    struct mailimf_optional_field *optional_field = NULL;
vb@94
   518
vb@94
   519
    _field = strdup(field);
vb@94
   520
    if (_field == NULL)
vb@94
   521
        goto enomem;
vb@94
   522
vb@94
   523
    _value = mailmime_encode_subject_header("utf-8", value, 0);
vb@94
   524
    if (_value == NULL)
vb@94
   525
        goto enomem;
vb@94
   526
vb@94
   527
    optional_field = mailimf_optional_field_new(_field, _value);
vb@94
   528
    if (optional_field == NULL)
vb@94
   529
        goto enomem;
vb@94
   530
vb@94
   531
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@94
   532
    assert(result);
vb@94
   533
    if (result == NULL)
vb@94
   534
        goto enomem;
vb@94
   535
vb@94
   536
    result->fld_type = MAILIMF_FIELD_OPTIONAL_FIELD;
vb@94
   537
    result->fld_data.fld_optional_field = optional_field;
vb@94
   538
vb@94
   539
    return result;
vb@94
   540
vb@94
   541
enomem:
vb@94
   542
    if (optional_field) {
vb@94
   543
        mailimf_optional_field_free(optional_field);
vb@94
   544
    }
vb@94
   545
    else {
vb@94
   546
        free(_field);
vb@94
   547
        free(_value);
vb@94
   548
    }
vb@94
   549
vb@94
   550
    return NULL;
vb@94
   551
}
vb@94
   552
vb@94
   553
int _append_optional_field(
vb@94
   554
        clist *list,
vb@94
   555
        const char *field,
vb@94
   556
        const char *value
vb@94
   557
    )
vb@94
   558
{
vb@94
   559
    int r;
vb@94
   560
    struct mailimf_field * optional_field =
vb@94
   561
            create_optional_field(field, value);
vb@94
   562
vb@94
   563
    if (optional_field == NULL)
vb@94
   564
        return -1;
vb@94
   565
vb@94
   566
    r = clist_append(list, optional_field);
vb@94
   567
    if (r)
vb@94
   568
        mailimf_field_free(optional_field);
vb@94
   569
vb@94
   570
    return r;
vb@94
   571
}
vb@94
   572
vb@147
   573
clist * _get_fields(struct mailmime * mime)
vb@147
   574
{
vb@147
   575
    clist * _fieldlist = NULL;
vb@147
   576
vb@147
   577
    assert(mime);
vb@147
   578
vb@147
   579
    if (mime->mm_data.mm_message.mm_fields &&
vb@147
   580
            mime->mm_data.mm_message.mm_fields->fld_list) {
vb@147
   581
        _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
vb@147
   582
    }
vb@147
   583
vb@147
   584
    return _fieldlist;
vb@147
   585
}
vb@147
   586
vb@147
   587
struct mailmime_content * _get_content(struct mailmime * mime)
vb@147
   588
{
vb@147
   589
    struct mailmime_content * content = NULL;
vb@147
   590
vb@147
   591
    assert(mime);
vb@147
   592
vb@147
   593
    if (mime->mm_data.mm_message.mm_msg_mime)
vb@147
   594
        content = mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
vb@147
   595
vb@147
   596
    return content;
vb@147
   597
}
vb@147
   598
vb@154
   599
char * _get_filename(struct mailmime *mime)
vb@154
   600
{
vb@154
   601
    clist * _fieldlist = NULL;
vb@154
   602
vb@154
   603
    assert(mime);
vb@154
   604
vb@154
   605
    if (mime->mm_mime_fields && mime->mm_mime_fields->fld_list)
vb@154
   606
        _fieldlist = mime->mm_mime_fields->fld_list;
vb@154
   607
    else
vb@154
   608
        return NULL;
vb@154
   609
vb@154
   610
    clistiter *cur;
vb@154
   611
    for (cur = clist_begin(_fieldlist); cur; cur = clist_next(cur)) {
vb@154
   612
        struct mailmime_field * _field = clist_content(cur);
vb@154
   613
        if (_field && _field->fld_type == MAILMIME_FIELD_DISPOSITION) {
vb@154
   614
            if (_field->fld_data.fld_disposition &&
vb@154
   615
                    _field->fld_data.fld_disposition->dsp_parms) {
vb@154
   616
                clist * _parmlist =
vb@154
   617
                        _field->fld_data.fld_disposition->dsp_parms;
vb@154
   618
                clistiter *cur2;
vb@154
   619
                for (cur2 = clist_begin(_parmlist); cur2; cur2 =
vb@154
   620
                        clist_next(cur2)) {
vb@156
   621
                    struct mailmime_disposition_parm * param =
vb@156
   622
                            clist_content(cur2);
vb@156
   623
                    if (param->pa_type == MAILMIME_DISPOSITION_PARM_FILENAME)
vb@156
   624
                        return param->pa_data.pa_filename;
vb@154
   625
                }
vb@154
   626
            }
vb@154
   627
        }
vb@154
   628
    }
vb@154
   629
vb@154
   630
    return NULL;
vb@154
   631
}
vb@154
   632
vb@157
   633
static bool parameter_has_value(
vb@152
   634
        struct mailmime_content *content,       
vb@152
   635
        const char *name,
vb@152
   636
        const char *value
vb@152
   637
    )
vb@152
   638
{
vb@152
   639
    clistiter *cur;
vb@152
   640
vb@152
   641
    assert(name);
vb@152
   642
    assert(value);
vb@152
   643
vb@152
   644
    clist * list = content->ct_parameters;
vb@152
   645
    if (list == NULL)
vb@152
   646
        return false;
vb@152
   647
vb@152
   648
    for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@152
   649
        struct mailmime_parameter * param = clist_content(cur);
vb@152
   650
        if (param &&
vb@165
   651
                param->pa_name && strcasecmp(name, param->pa_name) == 0 &&
vb@165
   652
                param->pa_value && strcasecmp(value, param->pa_value) == 0)
vb@152
   653
            return true;
vb@152
   654
    }
vb@152
   655
vb@152
   656
    return false;
vb@152
   657
}
vb@152
   658
vb@151
   659
bool _is_multipart(struct mailmime_content *content, const char *subtype)
vb@147
   660
{
vb@147
   661
    assert(content);
vb@147
   662
vb@147
   663
    if (content->ct_type && content->ct_type->tp_type ==
vb@147
   664
            MAILMIME_TYPE_COMPOSITE_TYPE &&
vb@147
   665
            content->ct_type->tp_data.tp_composite_type &&
vb@147
   666
            content->ct_type->tp_data.tp_composite_type->ct_type ==
vb@151
   667
            MAILMIME_COMPOSITE_TYPE_MULTIPART) {
vb@151
   668
        if (subtype)
vb@151
   669
            return content->ct_subtype &&
vb@165
   670
                    strcasecmp(content->ct_subtype, subtype) == 0;
vb@151
   671
        else
vb@151
   672
            return true;
vb@151
   673
    }
vb@147
   674
vb@151
   675
    return false;
vb@147
   676
}
vb@147
   677
vb@147
   678
bool _is_PGP_MIME(struct mailmime_content *content)
vb@147
   679
{
vb@147
   680
    assert(content);
vb@147
   681
vb@151
   682
    if (_is_multipart(content, "encrypted") &&
vb@152
   683
            parameter_has_value(content, "protocol",
vb@147
   684
                    "application/pgp-encrypted"))
vb@151
   685
        return true;
vb@147
   686
vb@151
   687
    return false;
vb@147
   688
}
vb@147
   689
vb@152
   690
bool _is_text_part(struct mailmime_content *content, const char *subtype)
vb@152
   691
{
vb@152
   692
    assert(content);
vb@152
   693
vb@153
   694
    if (content->ct_type && content->ct_type->tp_type ==
vb@153
   695
            MAILMIME_TYPE_DISCRETE_TYPE &&
vb@153
   696
            content->ct_type->tp_data.tp_discrete_type &&
vb@152
   697
            content->ct_type->tp_data.tp_discrete_type->dt_type ==
vb@152
   698
            MAILMIME_DISCRETE_TYPE_TEXT) {
vb@152
   699
        if (subtype)
vb@152
   700
            return content->ct_subtype &&
vb@165
   701
                    strcasecmp(content->ct_subtype, subtype) == 0;
vb@152
   702
        else
vb@152
   703
            return true;
vb@152
   704
    }
vb@152
   705
vb@152
   706
    return false;
vb@152
   707
}
vb@152
   708
vb@159
   709
int _get_content_type(
vb@159
   710
        const struct mailmime_content *content,
vb@159
   711
        char **type,
vb@159
   712
        char **charset
vb@159
   713
    )
vb@154
   714
{
vb@159
   715
    char *_type = NULL;
vb@159
   716
    char *_charset = NULL;
vb@154
   717
vb@154
   718
    assert(content);
vb@159
   719
    assert(type);
vb@159
   720
    assert(charset);
vb@154
   721
vb@159
   722
    *type = NULL;
vb@159
   723
    *charset = NULL;
vb@159
   724
vb@159
   725
    if (content->ct_subtype == NULL)
vb@159
   726
        return EINVAL;
vb@154
   727
vb@154
   728
    if (content->ct_type && content->ct_type->tp_data.tp_discrete_type) {
vb@154
   729
        size_t len;
vb@159
   730
        const char *_main_type;
vb@154
   731
vb@154
   732
        switch  (content->ct_type->tp_data.tp_discrete_type->dt_type) {
vb@154
   733
            case MAILMIME_DISCRETE_TYPE_TEXT:
vb@159
   734
                _main_type = "text";
vb@154
   735
                break;
vb@154
   736
            case MAILMIME_DISCRETE_TYPE_IMAGE:
vb@159
   737
                _main_type = "image";
vb@154
   738
                break;
vb@154
   739
            case MAILMIME_DISCRETE_TYPE_AUDIO:
vb@159
   740
                _main_type = "audio";
vb@154
   741
                break;
vb@154
   742
            case MAILMIME_DISCRETE_TYPE_VIDEO:
vb@159
   743
                _main_type = "video";
vb@154
   744
                break;
vb@154
   745
            case MAILMIME_DISCRETE_TYPE_APPLICATION:
vb@159
   746
                _main_type = "application";
vb@154
   747
                break;
vb@154
   748
            case MAILMIME_DISCRETE_TYPE_EXTENSION:
vb@159
   749
                _main_type = "extension";
vb@154
   750
                break;
vb@154
   751
            default:
vb@159
   752
                return EINVAL;
vb@154
   753
        }
vb@154
   754
vb@159
   755
        len = strlen(_main_type) + 1 + strlen(content->ct_subtype) + 1;
vb@159
   756
        _type = calloc(1, len);
vb@159
   757
        assert(_type);
vb@159
   758
        if (_type == NULL)
vb@159
   759
            return ENOMEM;
vb@159
   760
krista@903
   761
        strncpy(_type, _main_type, len);
krista@903
   762
        len -= strlen(_main_type);
krista@903
   763
        strncat(_type, "/", len--);
krista@903
   764
        strncat(_type, content->ct_subtype, len);
vb@159
   765
vb@159
   766
        if (content->ct_parameters) {
vb@159
   767
            clistiter *cur;
vb@159
   768
            for (cur = clist_begin(content->ct_parameters); cur; cur =
vb@159
   769
                    clist_next(cur)) {
vb@159
   770
                struct mailmime_parameter * param = clist_content(cur);
vb@165
   771
                if (param && param->pa_name && strcasecmp(param->pa_name,
vb@159
   772
                            "charset") == 0) {
vb@159
   773
                    _charset = param->pa_value;
vb@159
   774
                    break;
vb@159
   775
                }
vb@159
   776
            }
vb@159
   777
            if (_charset)
vb@159
   778
                *charset = strdup(_charset);
vb@157
   779
        }
vb@154
   780
vb@159
   781
        *type = _type;
vb@159
   782
        return 0;
vb@154
   783
    }
vb@154
   784
vb@159
   785
    return EINVAL;
vb@154
   786
}
vb@154
   787