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