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