src/etpan_mime.c
author Krista 'DarthMama' Bennett <krista@pep.foundation>
Wed, 22 Apr 2020 10:33:06 +0200
branchpostrelease
changeset 4581 780d3e087575
parent 4578 4878f4a80aba
child 4582 4f450d477709
permissions -rw-r--r--
More tests.
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     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@3439
     9
#include "pEp_internal.h"
vb@130
    10
#include "platform.h"
vb@3439
    11
#include "mime.h"
vb@3439
    12
#include "wrappers.h"
vb@3439
    13
#include "resource_id.h"
vb@130
    14
vb@130
    15
#include <string.h>
vb@130
    16
#include <stdlib.h>
vb@130
    17
#include <assert.h>
vb@157
    18
#include <errno.h>
vb@89
    19
roker@438
    20
#define MAX_MESSAGE_ID 128
vb@48
    21
vb@116
    22
static char * generate_boundary(void)
vb@48
    23
{
vb@48
    24
    char id[MAX_MESSAGE_ID];
vb@90
    25
roker@438
    26
    // no cryptographically strong random needed here
roker@438
    27
    const long value1 = random();
roker@438
    28
    const long value2 = random();
roker@438
    29
    const long value3 = random();
roker@438
    30
    const long value4 = random();
vb@116
    31
vb@116
    32
    snprintf(id, MAX_MESSAGE_ID, "%.4lx%.4lx%.4lx%.4lx", value1, value2,
vb@116
    33
            value3, value4);
vb@48
    34
    
vb@48
    35
    return strdup(id);
vb@48
    36
}
vb@48
    37
vb@48
    38
struct mailmime * part_new_empty(
vb@48
    39
        struct mailmime_content * content,
vb@48
    40
        struct mailmime_fields * mime_fields,
krista@3710
    41
        stringpair_list_t* param_keyvals,
vb@48
    42
        int force_single
vb@48
    43
    )
vb@48
    44
{
krista@2688
    45
    struct mailmime * build_info;
krista@2688
    46
    clist * list = NULL;
krista@2688
    47
    int r;
krista@2688
    48
    int mime_type;
vb@115
    49
    char * attr_name = NULL;
vb@115
    50
    char * attr_value = NULL;
vb@115
    51
    struct mailmime_parameter * param = NULL;
vb@115
    52
    clist * parameters = NULL;
vb@116
    53
    char *boundary = NULL;
vb@48
    54
krista@2688
    55
    list = NULL;
vb@48
    56
krista@2688
    57
    if (force_single) {
krista@2688
    58
        mime_type = MAILMIME_SINGLE;
krista@2688
    59
    }
krista@2688
    60
    else {
krista@2688
    61
        switch (content->ct_type->tp_type) {
krista@2688
    62
            case MAILMIME_TYPE_DISCRETE_TYPE:
vb@115
    63
                mime_type = MAILMIME_SINGLE;
vb@115
    64
                break;
vb@48
    65
krista@2688
    66
            case MAILMIME_TYPE_COMPOSITE_TYPE:
vb@115
    67
                switch (content->ct_type->tp_data.tp_composite_type->ct_type) {
vb@115
    68
                    case MAILMIME_COMPOSITE_TYPE_MULTIPART:
vb@115
    69
                        mime_type = MAILMIME_MULTIPLE;
vb@115
    70
                        break;
vb@48
    71
vb@115
    72
                    case MAILMIME_COMPOSITE_TYPE_MESSAGE:
vb@115
    73
                        if (strcasecmp(content->ct_subtype, "rfc822") == 0)
vb@115
    74
                            mime_type = MAILMIME_MESSAGE;
vb@115
    75
                        else
vb@115
    76
                            mime_type = MAILMIME_SINGLE;
vb@115
    77
                        break;
vb@48
    78
vb@115
    79
                    default:
vb@115
    80
                        goto enomem;
vb@115
    81
                }
vb@115
    82
                break;
vb@48
    83
krista@2688
    84
            default:
vb@115
    85
                goto enomem;
krista@2688
    86
        }
krista@2688
    87
    }
vb@48
    88
krista@2688
    89
    if (mime_type == MAILMIME_MULTIPLE) {
krista@2688
    90
        list = clist_new();
vb@115
    91
        assert(list);
krista@2688
    92
        if (list == NULL)
krista@2688
    93
            goto enomem;
vb@48
    94
krista@2688
    95
        attr_name = strdup("boundary");
vb@115
    96
        assert(attr_name);
vb@115
    97
        if (attr_name == NULL)
vb@115
    98
            goto enomem;
vb@115
    99
krista@2688
   100
        boundary = generate_boundary();
vb@115
   101
        assert(boundary);
krista@2688
   102
        attr_value = boundary;
krista@2688
   103
        if (attr_value == NULL)
krista@2688
   104
            goto enomem;
vb@48
   105
krista@2688
   106
        param = mailmime_parameter_new(attr_name, attr_value);
vb@115
   107
        assert(param);
krista@2688
   108
        if (param == NULL)
krista@2688
   109
            goto enomem;
vb@115
   110
        attr_name = NULL;
vb@115
   111
        attr_value = NULL;
vb@48
   112
krista@2688
   113
        if (content->ct_parameters == NULL) {
krista@2688
   114
            parameters = clist_new();
vb@115
   115
            assert(parameters);
krista@2688
   116
            if (parameters == NULL)
krista@2688
   117
                goto enomem;
krista@2688
   118
        }
krista@2688
   119
        else {
krista@2688
   120
            parameters = content->ct_parameters;
vb@115
   121
        }
vb@48
   122
krista@2688
   123
        r = clist_append(parameters, param);
krista@2688
   124
        if (r)
krista@2688
   125
            goto enomem;
vb@115
   126
        param = NULL;
vb@48
   127
krista@2688
   128
        if (content->ct_parameters == NULL)
krista@2688
   129
            content->ct_parameters = parameters;
krista@2688
   130
    }
krista@3710
   131
    
krista@3710
   132
    if (param_keyvals) {
krista@3710
   133
        stringpair_list_t* cur;
krista@3710
   134
        for (cur = param_keyvals; cur; cur = cur->next) {
krista@3710
   135
            attr_name = strdup(cur->value->key);
krista@3710
   136
            attr_value = strdup(cur->value->value);
krista@3710
   137
            
krista@3710
   138
            param = mailmime_parameter_new(attr_name, attr_value);
krista@3710
   139
            assert(param);
krista@3710
   140
            if (param == NULL)
krista@3710
   141
                goto enomem;
krista@3710
   142
                
krista@3710
   143
            attr_name = NULL;
krista@3710
   144
            attr_value = NULL;
krista@3710
   145
krista@3710
   146
            if (content->ct_parameters == NULL) {
krista@3710
   147
                parameters = clist_new();
krista@3710
   148
                assert(parameters);
krista@3710
   149
                if (parameters == NULL)
krista@3710
   150
                    goto enomem;
krista@3710
   151
            }
krista@3710
   152
            else {
krista@3710
   153
                parameters = content->ct_parameters;
krista@3710
   154
            }
krista@3710
   155
krista@3710
   156
            r = clist_append(parameters, param);
krista@3710
   157
            if (r)
krista@3710
   158
                goto enomem;
krista@3710
   159
            param = NULL;
krista@3710
   160
krista@3710
   161
            if (content->ct_parameters == NULL)
krista@3710
   162
                content->ct_parameters = parameters;            
krista@3710
   163
        }
krista@3710
   164
    }
vb@48
   165
vb@115
   166
    build_info = mailmime_new(mime_type, NULL, 0, mime_fields, content, NULL,
vb@115
   167
            NULL, NULL, list, NULL, NULL);
krista@2688
   168
    if (build_info == NULL)
krista@2688
   169
        goto enomem;
vb@48
   170
krista@2688
   171
    return build_info;
vb@48
   172
vb@115
   173
enomem:
vb@115
   174
    if (list)
vb@115
   175
        clist_free(list);
vb@115
   176
    free(attr_name);
vb@115
   177
    free(attr_value);
vb@115
   178
    if (content->ct_parameters == NULL)
vb@115
   179
        if (parameters)
vb@115
   180
            clist_free(parameters);
krista@819
   181
    if (param)
krista@819
   182
        mailmime_parameter_free(param);
krista@2688
   183
    return NULL;
vb@48
   184
}
vb@48
   185
vb@114
   186
struct mailmime * get_pgp_encrypted_part(void)
vb@114
   187
{
krista@2688
   188
    struct mailmime * mime = NULL;
krista@2688
   189
    struct mailmime_fields * mime_fields = NULL;
krista@2688
   190
    struct mailmime_content * content = NULL;
vb@115
   191
    int r;
vb@114
   192
krista@2688
   193
    content = mailmime_content_new_with_str("application/pgp-encrypted");
vb@115
   194
    if (content == NULL)
vb@115
   195
        goto enomem;
vb@115
   196
vb@114
   197
    mime_fields = mailmime_fields_new_empty();
vb@115
   198
    if (mime_fields == NULL)
vb@115
   199
        goto enomem;
vb@115
   200
krista@3710
   201
    mime = part_new_empty(content, mime_fields, NULL, 1);
vb@115
   202
    if (mime == NULL)
vb@115
   203
        goto enomem;
vb@115
   204
    mime_fields = NULL;
vb@115
   205
    content = NULL;
vb@115
   206
vb@115
   207
    r = mailmime_set_body_text(mime, "Version: 1\n", 10);
vb@115
   208
    if (r != 0)
vb@115
   209
        goto enomem;
vb@114
   210
krista@2688
   211
    return mime;
vb@115
   212
vb@115
   213
enomem:
vb@115
   214
    if (content)
vb@115
   215
        mailmime_content_free(content);
vb@115
   216
    if (mime_fields)
vb@115
   217
        mailmime_fields_free(mime_fields);
vb@115
   218
    if (mime)
vb@115
   219
        mailmime_free(mime);
vb@115
   220
vb@115
   221
    return NULL;
vb@114
   222
}
vb@114
   223
vb@48
   224
struct mailmime * get_text_part(
krista@1878
   225
        pEp_rid_list_t* resource,
vb@48
   226
        const char * mime_type,
vb@48
   227
        const char * text,
vb@48
   228
        size_t length,
vb@48
   229
        int encoding_type
vb@48
   230
    )
vb@48
   231
{
vb@114
   232
    char * disposition_name = NULL;
krista@2688
   233
    struct mailmime_fields * mime_fields = NULL;
krista@2688
   234
    struct mailmime * mime = NULL;
krista@2688
   235
    struct mailmime_content * content = NULL;
krista@2688
   236
    struct mailmime_parameter * param = NULL;
krista@2688
   237
    struct mailmime_disposition * disposition = NULL;
krista@2688
   238
    struct mailmime_mechanism * encoding = NULL;
krista@1878
   239
    char* content_id = NULL;
vb@115
   240
    int r;
krista@1878
   241
                
krista@1878
   242
    if (resource != NULL && resource->rid != NULL) {
krista@1878
   243
        switch (resource->rid_type) {
krista@1878
   244
            case PEP_RID_CID:
krista@1878
   245
                content_id = strdup(resource->rid);
krista@1878
   246
                break;
krista@1878
   247
            case PEP_RID_FILENAME:
krista@1878
   248
            default:
krista@1878
   249
                disposition_name = strdup(resource->rid);
krista@1878
   250
                if (disposition_name == NULL)
krista@1878
   251
                    goto enomem;
krista@1878
   252
                    
krista@1878
   253
                disposition =
krista@1878
   254
                        mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
krista@1878
   255
                                disposition_name, NULL, NULL, NULL, (size_t) -1);
krista@1878
   256
krista@1878
   257
                if (disposition == NULL)
krista@1878
   258
                    goto enomem;
krista@1878
   259
krista@1878
   260
                disposition_name = NULL;                
krista@1878
   261
                break;
krista@1878
   262
        }    
krista@1878
   263
    }
vb@48
   264
    
vb@115
   265
    if (encoding_type) {
vb@114
   266
        encoding = mailmime_mechanism_new(encoding_type, NULL);
vb@115
   267
        if (encoding == NULL)
vb@115
   268
            goto enomem;
vb@115
   269
    }
vb@114
   270
krista@1878
   271
    mime_fields = mailmime_fields_new_with_data(encoding, content_id, NULL,
vb@115
   272
            disposition, NULL);
vb@115
   273
    if (mime_fields == NULL)
vb@115
   274
        goto enomem;
vb@115
   275
    encoding = NULL;
vb@115
   276
    disposition = NULL;
krista@1878
   277
    content_id = NULL;
vb@48
   278
krista@2688
   279
    content = mailmime_content_new_with_str(mime_type);
vb@115
   280
    if (content == NULL)
vb@115
   281
        goto enomem;
vb@114
   282
    
vb@114
   283
    if (encoding_type != MAILMIME_MECHANISM_7BIT) {
vb@114
   284
        param = mailmime_param_new_with_data("charset", "utf-8");
vb@115
   285
        r = clist_append(content->ct_parameters, param);
vb@115
   286
        if (r != 0)
vb@115
   287
            goto enomem;
vb@114
   288
    }
vb@114
   289
krista@3710
   290
    mime = part_new_empty(content, mime_fields, NULL, 1);
vb@115
   291
    if (mime == NULL)
vb@115
   292
        goto enomem;
vb@115
   293
    content = NULL;
vb@115
   294
    mime_fields = NULL;
vb@114
   295
vb@115
   296
    if (text) {
vb@115
   297
        r = mailmime_set_body_text(mime, (char *) text, length);
vb@115
   298
        if (r != 0)
vb@115
   299
            goto enomem;
vb@115
   300
    }
krista@2688
   301
    
krista@2688
   302
    return mime;
vb@115
   303
vb@115
   304
enomem:
vb@115
   305
    free(disposition_name);
vb@115
   306
    if (mime_fields)
vb@115
   307
        mailmime_fields_free(mime_fields);
vb@115
   308
    if (mime)
vb@115
   309
        mailmime_free(mime);
vb@115
   310
    if (content)
vb@115
   311
        mailmime_content_free(content);
vb@115
   312
    if (param)
vb@115
   313
        mailmime_parameter_free(param);
vb@115
   314
    if (disposition)
vb@115
   315
        mailmime_disposition_free(disposition);
vb@115
   316
    if (encoding)
vb@115
   317
        mailmime_mechanism_free(encoding);
vb@115
   318
vb@115
   319
    return NULL;
vb@48
   320
}
vb@48
   321
vb@59
   322
struct mailmime * get_file_part(
krista@1878
   323
        pEp_rid_list_t* resource,
vb@59
   324
        const char * mime_type,
vb@59
   325
        char * data,
krista@2200
   326
        size_t length,
krista@3710
   327
        bool transport_encode,
krista@3710
   328
        bool set_attachment_forward_comment
vb@59
   329
    )
vb@59
   330
{
vb@115
   331
    char * disposition_name = NULL;
vb@59
   332
    int encoding_type;
vb@115
   333
    struct mailmime_disposition * disposition = NULL;
vb@115
   334
    struct mailmime_mechanism * encoding = NULL;
vb@115
   335
    struct mailmime_content * content = NULL;
vb@115
   336
    struct mailmime * mime = NULL;
vb@115
   337
    struct mailmime_fields * mime_fields = NULL;
krista@1878
   338
    char* content_id = NULL;
vb@115
   339
    int r;
krista@1878
   340
                
krista@1878
   341
    if (resource != NULL && resource->rid != NULL) {
krista@1878
   342
        switch (resource->rid_type) {
krista@1878
   343
            case PEP_RID_CID:
krista@1878
   344
                content_id = strdup(resource->rid);
krista@2011
   345
                disposition =
krista@2011
   346
                    mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
krista@2011
   347
                                                       NULL, NULL, NULL, NULL, (size_t) -1);
krista@2011
   348
                    if (disposition == NULL)
krista@2011
   349
                        goto enomem;
krista@1878
   350
                break;
krista@1878
   351
            case PEP_RID_FILENAME:
krista@1878
   352
            default:
krista@1878
   353
                disposition_name = strdup(resource->rid);
krista@1878
   354
                if (disposition_name == NULL)
krista@1878
   355
                    goto enomem;
krista@1878
   356
                    
krista@1878
   357
                disposition =
krista@1878
   358
                        mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
krista@1878
   359
                                disposition_name, NULL, NULL, NULL, (size_t) -1);
krista@1878
   360
                                
krista@1878
   361
                if (disposition == NULL)
krista@1878
   362
                    goto enomem;
krista@1878
   363
                disposition_name = NULL;
krista@1878
   364
                
krista@1878
   365
                break;
krista@1878
   366
        }    
vb@59
   367
    }
krista@1878
   368
    
vb@115
   369
vb@59
   370
    content = mailmime_content_new_with_str(mime_type);
vb@115
   371
    if (content == NULL)
vb@115
   372
        goto enomem;
vb@59
   373
krista@2200
   374
    encoding = NULL;
krista@2200
   375
krista@2200
   376
    if (transport_encode) {
krista@2200
   377
        encoding_type = MAILMIME_MECHANISM_BASE64;
krista@2200
   378
        encoding = mailmime_mechanism_new(encoding_type, NULL);
krista@2200
   379
        if (encoding == NULL)
krista@2200
   380
            goto enomem;
krista@2200
   381
    }
vb@115
   382
krista@1878
   383
    mime_fields = mailmime_fields_new_with_data(encoding, content_id, NULL,
vb@115
   384
            disposition, NULL);
vb@115
   385
    if (mime_fields == NULL)
vb@115
   386
        goto enomem;
vb@115
   387
    encoding = NULL;
vb@115
   388
    disposition = NULL;
vb@115
   389
krista@3710
   390
    stringpair_list_t* extra_params = NULL;
krista@3710
   391
    
krista@3710
   392
    if (set_attachment_forward_comment)
krista@3729
   393
        extra_params = new_stringpair_list(new_stringpair("forwarded", "no"));
krista@3710
   394
    
krista@3710
   395
    mime = part_new_empty(content, mime_fields, extra_params, 1);
krista@3710
   396
    free_stringpair_list(extra_params);
vb@115
   397
    if (mime == NULL)
vb@115
   398
        goto enomem;
vb@115
   399
    content = NULL;
vb@115
   400
    mime_fields = NULL;
vb@115
   401
Edouard@747
   402
    if(length > 0)
Edouard@747
   403
    {
Edouard@747
   404
        r = mailmime_set_body_text(mime, data, length);
Edouard@747
   405
        if (r != 0)
Edouard@747
   406
            goto enomem;
Edouard@747
   407
    }
vb@59
   408
vb@59
   409
    return mime;
vb@115
   410
vb@115
   411
enomem:
vb@115
   412
    free(disposition_name);
vb@115
   413
    if (disposition)
vb@115
   414
        mailmime_disposition_free(disposition);
vb@115
   415
    if (encoding)
vb@115
   416
        mailmime_mechanism_free(encoding);
vb@115
   417
    if (content)
vb@115
   418
        mailmime_content_free(content);
vb@115
   419
    if (mime_fields)
vb@115
   420
        mailmime_fields_free(mime_fields);
vb@115
   421
    if (mime)
vb@115
   422
        mailmime_free(mime);
krista@1878
   423
    
vb@115
   424
    return NULL;
vb@59
   425
}
vb@59
   426
vb@116
   427
struct mailmime * part_multiple_new(const char *type)
vb@48
   428
{
vb@115
   429
    struct mailmime_fields *mime_fields = NULL;
vb@115
   430
    struct mailmime_content *content = NULL;
vb@115
   431
    struct mailmime *mp = NULL;
vb@48
   432
    
vb@48
   433
    mime_fields = mailmime_fields_new_empty();
vb@48
   434
    if (mime_fields == NULL)
vb@115
   435
        goto enomem;
vb@48
   436
    
vb@48
   437
    content = mailmime_content_new_with_str(type);
vb@48
   438
    if (content == NULL)
vb@115
   439
        goto enomem;
vb@48
   440
    
krista@3710
   441
    mp = part_new_empty(content, mime_fields, NULL, 0);
vb@48
   442
    if (mp == NULL)
vb@115
   443
        goto enomem;
vb@48
   444
    
vb@48
   445
    return mp;
vb@48
   446
    
vb@115
   447
enomem:
vb@115
   448
    if (content)
vb@115
   449
        mailmime_content_free(content);
vb@115
   450
    if (mime_fields)
vb@115
   451
        mailmime_fields_free(mime_fields);
vb@115
   452
vb@48
   453
    return NULL;
vb@48
   454
}
vb@48
   455
vb@89
   456
struct mailimf_field * _new_field(
vb@89
   457
        int type,
vb@89
   458
        _new_func_t new_func,
vb@89
   459
        void *value
vb@89
   460
    )
vb@89
   461
{
vb@89
   462
    void *data = new_func(value);
vb@89
   463
    assert(data);
vb@89
   464
    if (data == NULL)
vb@89
   465
        return NULL;
vb@89
   466
vb@89
   467
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@89
   468
    assert(result);
vb@89
   469
    if (result == NULL) {
vb@89
   470
        free(data);
vb@89
   471
        return NULL;
vb@89
   472
    }
vb@89
   473
vb@89
   474
    result->fld_type = type;
vb@89
   475
    result->fld_data.fld_return_path = data;
vb@89
   476
vb@89
   477
    return result;
vb@89
   478
}
vb@89
   479
vb@89
   480
void _free_field(struct mailimf_field *field)
vb@89
   481
{
vb@89
   482
    if (field)
vb@89
   483
        free(field->fld_data.fld_return_path);
vb@89
   484
    free(field);
vb@89
   485
}
vb@89
   486
vb@89
   487
int _append_field(
vb@89
   488
        clist *list,
vb@89
   489
        int type,
vb@89
   490
        _new_func_t new_func,
vb@89
   491
        void *value
vb@89
   492
    )
vb@89
   493
{
vb@89
   494
    int r;
vb@89
   495
    struct mailimf_field * field;
vb@89
   496
vb@89
   497
    assert(list);
vb@89
   498
    assert(new_func);
vb@89
   499
    assert(value);
vb@89
   500
vb@89
   501
    field = _new_field(type, new_func, value);
vb@89
   502
    if (field == NULL)
vb@89
   503
        return -1;
vb@89
   504
vb@89
   505
    r = clist_append(list, field);
vb@90
   506
    if (r)
vb@89
   507
        _free_field(field);
vb@89
   508
vb@89
   509
    return r;
vb@89
   510
}
vb@89
   511
vb@92
   512
// http://media2.giga.de/2014/02/Image-28.jpg
vb@92
   513
vb@4550
   514
struct mailimf_date_time * timestamp_to_etpantime(const timestamp *ts)
vb@89
   515
{
vb@89
   516
    struct mailimf_date_time * result = calloc(1,
vb@89
   517
            sizeof(struct mailimf_date_time));
vb@89
   518
    assert(result);
vb@89
   519
    if (result == NULL)
vb@89
   520
        return NULL;
vb@89
   521
vb@89
   522
    assert(ts);
vb@89
   523
vb@89
   524
    result->dt_sec = ts->tm_sec;
vb@89
   525
    result->dt_min = ts->tm_min;
vb@89
   526
    result->dt_hour = ts->tm_hour;
vb@89
   527
    result->dt_day = ts->tm_mday;
vb@89
   528
    result->dt_month = ts->tm_mon + 1;
vb@89
   529
    result->dt_year = ts->tm_year + 1900;
vb@89
   530
    result->dt_zone = (int) (ts->tm_gmtoff / 36L);
vb@89
   531
    return result;
vb@89
   532
}
vb@89
   533
vb@4550
   534
timestamp * etpantime_to_timestamp(const struct mailimf_date_time *et)
vb@89
   535
{
vb@4550
   536
    timestamp * result = calloc(1, sizeof(timestamp));
vb@89
   537
    assert(result);
vb@89
   538
    if (result == NULL)
vb@89
   539
        return NULL;
vb@89
   540
vb@89
   541
    assert(et);
vb@89
   542
vb@89
   543
    result->tm_sec = et->dt_sec;
vb@89
   544
    result->tm_min = et->dt_min;
vb@89
   545
    result->tm_hour = et->dt_hour;
vb@89
   546
    result->tm_mday = et->dt_day;
vb@89
   547
    result->tm_mon = et->dt_month - 1;
vb@89
   548
    result->tm_year = et->dt_year - 1900;
vb@89
   549
    result->tm_gmtoff = 36L * (long) et->dt_zone;
vb@89
   550
    return result;
vb@89
   551
}
vb@89
   552
vb@90
   553
struct mailimf_mailbox * mailbox_from_string(
vb@90
   554
        const char *name,
vb@90
   555
        const char *address
vb@90
   556
    )
vb@90
   557
{
vb@90
   558
    struct mailimf_mailbox *mb = NULL;
vb@90
   559
    char *_name = NULL;
vb@90
   560
    char *_address = NULL;
vb@90
   561
vb@90
   562
    assert(address);
vb@90
   563
vb@90
   564
    _name = name ? strdup(name) : strdup("");
vb@90
   565
    if (_name == NULL)
vb@90
   566
        goto enomem;
vb@90
   567
vb@90
   568
    _address = strdup(address);
vb@90
   569
    if (_address == NULL)
vb@90
   570
        goto enomem;
vb@90
   571
vb@90
   572
    mb = mailimf_mailbox_new(_name, _address);
vb@90
   573
    assert(mb);
vb@90
   574
    if (mb == NULL)
vb@90
   575
        goto enomem;
vb@90
   576
vb@90
   577
    return mb;
vb@90
   578
vb@90
   579
enomem:
vb@90
   580
    free(_name);
vb@90
   581
    free(_address);
vb@90
   582
vb@90
   583
    return NULL;
vb@90
   584
}
vb@90
   585
krista@2562
   586
vb@94
   587
struct mailimf_field * create_optional_field(
vb@94
   588
        const char *field,
vb@94
   589
        const char *value
vb@94
   590
    )
vb@94
   591
{
vb@94
   592
    char *_field = NULL;
vb@94
   593
    char *_value = NULL;
vb@94
   594
    struct mailimf_optional_field *optional_field = NULL;
vb@94
   595
vb@94
   596
    _field = strdup(field);
vb@94
   597
    if (_field == NULL)
vb@94
   598
        goto enomem;
vb@94
   599
krista@2562
   600
    if (!must_field_value_be_encoded(value))
krista@2562
   601
        _value = strdup(value);
krista@2562
   602
    else    
krista@2562
   603
        _value = mailmime_encode_subject_header("utf-8", value, 0);
vb@94
   604
    if (_value == NULL)
vb@94
   605
        goto enomem;
vb@94
   606
vb@94
   607
    optional_field = mailimf_optional_field_new(_field, _value);
vb@94
   608
    if (optional_field == NULL)
vb@94
   609
        goto enomem;
vb@94
   610
vb@94
   611
    struct mailimf_field * result = calloc(1, sizeof(struct mailimf_field));
vb@94
   612
    assert(result);
vb@94
   613
    if (result == NULL)
vb@94
   614
        goto enomem;
vb@94
   615
vb@94
   616
    result->fld_type = MAILIMF_FIELD_OPTIONAL_FIELD;
vb@94
   617
    result->fld_data.fld_optional_field = optional_field;
vb@94
   618
vb@94
   619
    return result;
vb@94
   620
vb@94
   621
enomem:
vb@94
   622
    if (optional_field) {
vb@94
   623
        mailimf_optional_field_free(optional_field);
vb@94
   624
    }
vb@94
   625
    else {
vb@94
   626
        free(_field);
vb@94
   627
        free(_value);
vb@94
   628
    }
vb@94
   629
vb@94
   630
    return NULL;
vb@94
   631
}
vb@94
   632
vb@94
   633
int _append_optional_field(
vb@94
   634
        clist *list,
vb@94
   635
        const char *field,
vb@94
   636
        const char *value
vb@94
   637
    )
vb@94
   638
{
vb@94
   639
    int r;
vb@94
   640
    struct mailimf_field * optional_field =
vb@94
   641
            create_optional_field(field, value);
vb@94
   642
vb@94
   643
    if (optional_field == NULL)
vb@94
   644
        return -1;
vb@94
   645
vb@94
   646
    r = clist_append(list, optional_field);
vb@94
   647
    if (r)
vb@94
   648
        mailimf_field_free(optional_field);
vb@94
   649
vb@94
   650
    return r;
vb@94
   651
}
vb@94
   652
vb@147
   653
clist * _get_fields(struct mailmime * mime)
vb@147
   654
{
vb@147
   655
    clist * _fieldlist = NULL;
vb@147
   656
vb@147
   657
    assert(mime);
vb@147
   658
vb@147
   659
    if (mime->mm_data.mm_message.mm_fields &&
vb@147
   660
            mime->mm_data.mm_message.mm_fields->fld_list) {
vb@147
   661
        _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
vb@147
   662
    }
vb@147
   663
vb@147
   664
    return _fieldlist;
vb@147
   665
}
vb@147
   666
vb@147
   667
struct mailmime_content * _get_content(struct mailmime * mime)
vb@147
   668
{
vb@147
   669
    struct mailmime_content * content = NULL;
vb@147
   670
vb@147
   671
    assert(mime);
vb@147
   672
vb@147
   673
    if (mime->mm_data.mm_message.mm_msg_mime)
vb@147
   674
        content = mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
vb@147
   675
vb@147
   676
    return content;
vb@147
   677
}
vb@147
   678
krista@1872
   679
krista@1873
   680
/* Return a list of identifier_type and resource id (filename, cid, etc) */
krista@1873
   681
pEp_rid_list_t* _get_resource_id_list(struct mailmime *mime)
krista@1873
   682
{
krista@1873
   683
    clist * _fieldlist = NULL;
krista@1872
   684
krista@1873
   685
    assert(mime);
krista@1873
   686
krista@1873
   687
    if (mime->mm_mime_fields && mime->mm_mime_fields->fld_list)
krista@1873
   688
        _fieldlist = mime->mm_mime_fields->fld_list;
krista@1873
   689
    else
krista@1873
   690
        return NULL;
krista@1873
   691
krista@1873
   692
    clistiter *cur;
krista@1873
   693
krista@1873
   694
    pEp_rid_list_t* rid_list = NULL; 
krista@1873
   695
    pEp_rid_list_t** rid_list_curr_p = &rid_list; 
krista@1873
   696
        
krista@1873
   697
    for (cur = clist_begin(_fieldlist); cur; cur = clist_next(cur)) {
krista@1873
   698
        struct mailmime_field * _field = clist_content(cur);
krista@1873
   699
        /* content_id */
krista@1873
   700
        if (_field && _field->fld_type == MAILMIME_FIELD_ID) {
krista@1900
   701
            pEp_rid_list_t* new_rid = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
krista@1873
   702
            new_rid->rid_type = PEP_RID_CID;
krista@1900
   703
            new_rid->rid = strdup(_field->fld_data.fld_id);
krista@1873
   704
            *rid_list_curr_p = new_rid;
krista@1873
   705
            rid_list_curr_p = &new_rid->next;
krista@1873
   706
        }
krista@1873
   707
        else if (_field && _field->fld_type == MAILMIME_FIELD_DISPOSITION) {
krista@1873
   708
            /* filename */
krista@1873
   709
            if (_field->fld_data.fld_disposition &&
krista@1873
   710
                    _field->fld_data.fld_disposition->dsp_parms) {
krista@1873
   711
                clist * _parmlist =
krista@1873
   712
                        _field->fld_data.fld_disposition->dsp_parms;
krista@1873
   713
                clistiter *cur2;
krista@1873
   714
                for (cur2 = clist_begin(_parmlist); cur2; cur2 =
krista@1873
   715
                        clist_next(cur2)) {
krista@1873
   716
                    struct mailmime_disposition_parm * param =
krista@1873
   717
                            clist_content(cur2);
krista@1873
   718
                    if (param->pa_type == MAILMIME_DISPOSITION_PARM_FILENAME) {
krista@1900
   719
                        pEp_rid_list_t* new_rid = (pEp_rid_list_t*)calloc(1, sizeof(pEp_rid_list_t));
krista@1873
   720
                        new_rid->rid_type = PEP_RID_FILENAME;
krista@1900
   721
                        new_rid->rid = strdup(param->pa_data.pa_filename);
krista@1873
   722
                        *rid_list_curr_p = new_rid;
krista@1873
   723
                        rid_list_curr_p = &new_rid->next;
krista@1873
   724
                    }                
krista@1873
   725
                }
krista@1873
   726
            }
krista@1873
   727
        }
krista@1873
   728
    }
krista@1873
   729
    /* Will almost certainly usually be a singleton, but we need to be able to decide */
krista@1873
   730
    return rid_list;
krista@1873
   731
}
krista@1873
   732
krista@1873
   733
krista@1873
   734
/* FIXME: about to be obsoleted? */
krista@1872
   735
char * _get_filename_or_cid(struct mailmime *mime)
vb@154
   736
{
vb@154
   737
    clist * _fieldlist = NULL;
vb@154
   738
vb@154
   739
    assert(mime);
vb@154
   740
vb@154
   741
    if (mime->mm_mime_fields && mime->mm_mime_fields->fld_list)
vb@154
   742
        _fieldlist = mime->mm_mime_fields->fld_list;
vb@154
   743
    else
vb@154
   744
        return NULL;
vb@154
   745
vb@154
   746
    clistiter *cur;
krista@1873
   747
    
krista@1873
   748
    char* _temp_filename_ptr = NULL;
krista@1873
   749
    
vb@154
   750
    for (cur = clist_begin(_fieldlist); cur; cur = clist_next(cur)) {
vb@154
   751
        struct mailmime_field * _field = clist_content(cur);
krista@1873
   752
        if (_field && _field->fld_type == MAILMIME_FIELD_ID) {
krista@1873
   753
            /* We prefer CIDs to filenames when both are present */
krista@1873
   754
            free(_temp_filename_ptr); /* can be null, it's ok */
krista@1881
   755
            return build_uri("cid", _field->fld_data.fld_id); 
krista@1873
   756
        }
krista@1873
   757
        else if (_field && _field->fld_type == MAILMIME_FIELD_DISPOSITION) {
vb@154
   758
            if (_field->fld_data.fld_disposition &&
krista@1873
   759
                    _field->fld_data.fld_disposition->dsp_parms &&
krista@1873
   760
                    !_temp_filename_ptr) {
vb@154
   761
                clist * _parmlist =
vb@154
   762
                        _field->fld_data.fld_disposition->dsp_parms;
vb@154
   763
                clistiter *cur2;
vb@154
   764
                for (cur2 = clist_begin(_parmlist); cur2; cur2 =
vb@154
   765
                        clist_next(cur2)) {
vb@156
   766
                    struct mailmime_disposition_parm * param =
vb@156
   767
                            clist_content(cur2);
krista@1873
   768
                    if (param->pa_type == MAILMIME_DISPOSITION_PARM_FILENAME) {
krista@1881
   769
                        _temp_filename_ptr = build_uri("file", param->pa_data.pa_filename);
krista@1873
   770
                        break;
krista@1873
   771
                    }                
vb@154
   772
                }
vb@154
   773
            }
vb@154
   774
        }
vb@154
   775
    }
krista@1873
   776
    /* Ok, it wasn't a CID */
krista@1873
   777
    return _temp_filename_ptr;
vb@154
   778
}
vb@154
   779
vb@157
   780
static bool parameter_has_value(
vb@152
   781
        struct mailmime_content *content,       
vb@152
   782
        const char *name,
vb@152
   783
        const char *value
vb@152
   784
    )
vb@152
   785
{
vb@152
   786
    clistiter *cur;
vb@152
   787
vb@152
   788
    assert(name);
vb@152
   789
    assert(value);
vb@152
   790
vb@152
   791
    clist * list = content->ct_parameters;
vb@152
   792
    if (list == NULL)
vb@152
   793
        return false;
vb@152
   794
vb@152
   795
    for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@152
   796
        struct mailmime_parameter * param = clist_content(cur);
vb@152
   797
        if (param &&
vb@165
   798
                param->pa_name && strcasecmp(name, param->pa_name) == 0 &&
vb@165
   799
                param->pa_value && strcasecmp(value, param->pa_value) == 0)
vb@152
   800
            return true;
vb@152
   801
    }
vb@152
   802
vb@152
   803
    return false;
vb@152
   804
}
vb@152
   805
vb@151
   806
bool _is_multipart(struct mailmime_content *content, const char *subtype)
vb@147
   807
{
vb@147
   808
    assert(content);
vb@147
   809
vb@147
   810
    if (content->ct_type && content->ct_type->tp_type ==
vb@147
   811
            MAILMIME_TYPE_COMPOSITE_TYPE &&
vb@147
   812
            content->ct_type->tp_data.tp_composite_type &&
vb@147
   813
            content->ct_type->tp_data.tp_composite_type->ct_type ==
vb@151
   814
            MAILMIME_COMPOSITE_TYPE_MULTIPART) {
vb@151
   815
        if (subtype)
vb@151
   816
            return content->ct_subtype &&
vb@165
   817
                    strcasecmp(content->ct_subtype, subtype) == 0;
vb@151
   818
        else
vb@151
   819
            return true;
vb@151
   820
    }
vb@147
   821
vb@151
   822
    return false;
vb@147
   823
}
vb@147
   824
vb@147
   825
bool _is_PGP_MIME(struct mailmime_content *content)
vb@147
   826
{
vb@147
   827
    assert(content);
vb@147
   828
vb@151
   829
    if (_is_multipart(content, "encrypted") &&
vb@152
   830
            parameter_has_value(content, "protocol",
vb@147
   831
                    "application/pgp-encrypted"))
vb@151
   832
        return true;
vb@147
   833
vb@151
   834
    return false;
vb@147
   835
}
vb@147
   836
vb@152
   837
bool _is_text_part(struct mailmime_content *content, const char *subtype)
vb@152
   838
{
vb@152
   839
    assert(content);
vb@152
   840
vb@153
   841
    if (content->ct_type && content->ct_type->tp_type ==
vb@153
   842
            MAILMIME_TYPE_DISCRETE_TYPE &&
vb@153
   843
            content->ct_type->tp_data.tp_discrete_type &&
vb@152
   844
            content->ct_type->tp_data.tp_discrete_type->dt_type ==
vb@152
   845
            MAILMIME_DISCRETE_TYPE_TEXT) {
vb@152
   846
        if (subtype)
vb@152
   847
            return content->ct_subtype &&
vb@165
   848
                    strcasecmp(content->ct_subtype, subtype) == 0;
vb@152
   849
        else
vb@152
   850
            return true;
vb@152
   851
    }
vb@152
   852
vb@152
   853
    return false;
vb@152
   854
}
vb@152
   855
krista@3710
   856
bool _is_message_part(struct mailmime_content *content, const char* subtype) {
krista@3710
   857
    assert(content);
krista@3710
   858
    if (content->ct_type && content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE &&
krista@3710
   859
            content->ct_type->tp_data.tp_composite_type &&
krista@3710
   860
            content->ct_type->tp_data.tp_composite_type->ct_type ==
krista@3710
   861
            MAILMIME_COMPOSITE_TYPE_MESSAGE) {
krista@3710
   862
        if (subtype)
krista@3710
   863
            return content->ct_subtype &&
krista@3710
   864
                    strcasecmp(content->ct_subtype, subtype) == 0;
krista@3710
   865
        else
krista@3710
   866
            return true;                
krista@3710
   867
    }
krista@3710
   868
    
krista@3710
   869
    return false;
krista@3710
   870
}
krista@3710
   871
vb@159
   872
int _get_content_type(
vb@159
   873
        const struct mailmime_content *content,
vb@159
   874
        char **type,
vb@159
   875
        char **charset
vb@159
   876
    )
vb@154
   877
{
vb@159
   878
    char *_type = NULL;
vb@159
   879
    char *_charset = NULL;
vb@154
   880
vb@154
   881
    assert(content);
vb@159
   882
    assert(type);
vb@159
   883
    assert(charset);
vb@154
   884
vb@159
   885
    *type = NULL;
vb@159
   886
    *charset = NULL;
vb@159
   887
vb@159
   888
    if (content->ct_subtype == NULL)
vb@159
   889
        return EINVAL;
vb@154
   890
vb@154
   891
    if (content->ct_type && content->ct_type->tp_data.tp_discrete_type) {
vb@154
   892
        size_t len;
vb@159
   893
        const char *_main_type;
vb@154
   894
vb@154
   895
        switch  (content->ct_type->tp_data.tp_discrete_type->dt_type) {
vb@154
   896
            case MAILMIME_DISCRETE_TYPE_TEXT:
krista@4540
   897
                _main_type = (content->ct_subtype && 
krista@4540
   898
                              strcasecmp(content->ct_subtype, "rfc822") == 0 ?
krista@4540
   899
                              "message" : "text");
vb@154
   900
                break;
vb@154
   901
            case MAILMIME_DISCRETE_TYPE_IMAGE:
vb@159
   902
                _main_type = "image";
vb@154
   903
                break;
vb@154
   904
            case MAILMIME_DISCRETE_TYPE_AUDIO:
vb@159
   905
                _main_type = "audio";
vb@154
   906
                break;
vb@154
   907
            case MAILMIME_DISCRETE_TYPE_VIDEO:
vb@159
   908
                _main_type = "video";
vb@154
   909
                break;
vb@154
   910
            case MAILMIME_DISCRETE_TYPE_APPLICATION:
vb@159
   911
                _main_type = "application";
vb@154
   912
                break;
vb@154
   913
            case MAILMIME_DISCRETE_TYPE_EXTENSION:
vb@159
   914
                _main_type = "extension";
vb@154
   915
                break;
vb@154
   916
            default:
vb@159
   917
                return EINVAL;
vb@157
   918
        }
vb@154
   919
vb@159
   920
        len = strlen(_main_type) + 1 + strlen(content->ct_subtype) + 1;
vb@159
   921
        _type = calloc(1, len);
vb@159
   922
        assert(_type);
vb@159
   923
        if (_type == NULL)
vb@159
   924
            return ENOMEM;
vb@159
   925
krista@903
   926
        strncpy(_type, _main_type, len);
krista@903
   927
        len -= strlen(_main_type);
krista@903
   928
        strncat(_type, "/", len--);
krista@903
   929
        strncat(_type, content->ct_subtype, len);
vb@154
   930
vb@159
   931
        if (content->ct_parameters) {
vb@159
   932
            clistiter *cur;
vb@159
   933
            for (cur = clist_begin(content->ct_parameters); cur; cur =
vb@159
   934
                    clist_next(cur)) {
vb@159
   935
                struct mailmime_parameter * param = clist_content(cur);
vb@165
   936
                if (param && param->pa_name && strcasecmp(param->pa_name,
vb@159
   937
                            "charset") == 0) {
vb@159
   938
                    _charset = param->pa_value;
vb@159
   939
                    break;
vb@159
   940
                }
vb@159
   941
            }
vb@159
   942
            if (_charset)
vb@159
   943
                *charset = strdup(_charset);
vb@159
   944
        }
vb@159
   945
vb@159
   946
        *type = _type;
vb@159
   947
        return 0;
vb@154
   948
    }
vb@154
   949
vb@159
   950
    return EINVAL;
vb@154
   951
}
krista@2562
   952
krista@2562
   953
// Only for null-terminated field strings.
krista@2562
   954
// can this field be transported as is without modification?)
krista@2562
   955
// (See rfc2822, section 2.2.3 - libetpan's handling isn't quite what
krista@2562
   956
// we need here.)
krista@2562
   957
bool must_field_value_be_encoded(const char* field_value) {
krista@2579
   958
    if (!field_value)
krista@2579
   959
        return false;
krista@2579
   960
        
krista@2581
   961
    return must_chunk_be_encoded((const void*)field_value, strlen(field_value), false);    
krista@2579
   962
}
krista@2562
   963
krista@2581
   964
bool must_chunk_be_encoded(const void* value, size_t size, bool ignore_fws) {
krista@2579
   965
krista@2579
   966
    const char* begin_ptr = (const char*)value;    
krista@2579
   967
krista@2579
   968
    const char* end_ptr = begin_ptr + size;
krista@2579
   969
krista@2579
   970
    const char* cur_char_ptr = begin_ptr;
krista@2562
   971
    while (cur_char_ptr < end_ptr) {
krista@2562
   972
        char cur_char = *cur_char_ptr;
krista@2563
   973
        if (cur_char > 127 || cur_char < 0)
krista@2562
   974
            return true;
krista@2562
   975
        // FIXME - do we need to deal with CRCRLF here?
krista@2562
   976
        //         I guess in the worst case, it gets encoded, which
krista@2562
   977
        //         is *supposed* to be harmless...
krista@2581
   978
        if (!ignore_fws) {
krista@2581
   979
            if (cur_char == '\r') {
krista@2581
   980
                const char* next = cur_char_ptr + 1;
krista@2581
   981
                const char* nextnext = next + 1;
krista@2581
   982
                if (next >= end_ptr || nextnext >= end_ptr
krista@2581
   983
                    || *next != '\n'
krista@2581
   984
                    || (*nextnext != ' ' && *nextnext != '\t')) {
krista@2581
   985
                    return true;
krista@2581
   986
                }            
krista@2581
   987
            }
krista@2581
   988
            else if (cur_char == '\n') {
krista@2581
   989
                const char* prev = cur_char_ptr - 1;
krista@2581
   990
                if (prev == begin_ptr || *prev != '\r')
krista@2581
   991
                    return true;
krista@2581
   992
            }
krista@2562
   993
        }
krista@2562
   994
        cur_char_ptr++;
krista@2562
   995
    }    
krista@2562
   996
    return false;
krista@2562
   997
}
vb@3439
   998
vb@3439
   999
#define TMP_TEMPLATE "pEp.XXXXXXXXXXXXXXXXXXXX"
vb@3439
  1000
#ifdef _WIN32
vb@3439
  1001
#define PATH_SEP '\\'
vb@3439
  1002
#else
vb@3439
  1003
#define PATH_SEP '/'
vb@3439
  1004
#endif
vb@3439
  1005
vb@3439
  1006
static PEP_STATUS interpret_MIME(struct mailmime *mime,
krista@3710
  1007
                                 message *msg,
krista@3710
  1008
                                 bool* raise_msg_attachment);
vb@3439
  1009
vb@3439
  1010
// This function was rewritten to use in-memory buffers instead of
vb@3439
  1011
// temporary files when the pgp/mime support was implemented for
vb@3439
  1012
// outlook, as the existing code did not work well on windows.
vb@3439
  1013
vb@3439
  1014
static PEP_STATUS render_mime(struct mailmime *mime, char **mimetext)
vb@3439
  1015
{
vb@3439
  1016
    PEP_STATUS status = PEP_STATUS_OK;
vb@3439
  1017
    int col;
vb@3439
  1018
    int r;
vb@3439
  1019
	size_t len;
vb@3439
  1020
	char* buf = NULL;
vb@3439
  1021
vb@3439
  1022
	MMAPString* buffer;
vb@3439
  1023
vb@3439
  1024
	buffer = mmap_string_new(NULL);
vb@3439
  1025
	if (buffer == NULL)
vb@3439
  1026
		goto enomem;
vb@3439
  1027
	
vb@3439
  1028
	col = 0;
vb@3439
  1029
	r = mailmime_write_mem(buffer, &col, mime);
vb@3439
  1030
	assert(r == MAILIMF_NO_ERROR);
vb@3439
  1031
	if (r == MAILIMF_ERROR_MEMORY)
vb@3439
  1032
		goto enomem;
vb@3439
  1033
	else if (r != MAILIMF_NO_ERROR)
vb@3439
  1034
		goto err_file;
vb@3439
  1035
vb@3439
  1036
	// we overallocate by 1 byte, so we have a terminating 0.
vb@3439
  1037
	len = buffer->len;
vb@3439
  1038
	buf = calloc(len + 1, 1);
vb@3439
  1039
	if (buf == NULL)
vb@3439
  1040
		goto enomem;
vb@3439
  1041
vb@3439
  1042
	memcpy(buf, buffer->str, len);
vb@3439
  1043
	mmap_string_free(buffer);
vb@3439
  1044
vb@3439
  1045
    *mimetext = buf;
vb@3439
  1046
    return PEP_STATUS_OK;
vb@3439
  1047
vb@3439
  1048
err_file:
vb@3439
  1049
    status = PEP_CANNOT_CREATE_TEMP_FILE;
vb@3439
  1050
    goto pEp_error;
vb@3439
  1051
vb@3439
  1052
enomem:
vb@3439
  1053
    status = PEP_OUT_OF_MEMORY;
vb@3439
  1054
vb@3439
  1055
pEp_error:
vb@3439
  1056
	if (buffer)
vb@3439
  1057
		mmap_string_free(buffer);
vb@3439
  1058
	if (buf)
vb@3439
  1059
		free(buf);
vb@3439
  1060
    return status;
vb@3439
  1061
}
vb@3439
  1062
vb@3439
  1063
static PEP_STATUS mime_attachment(
vb@3439
  1064
        bloblist_t *blob,
vb@3439
  1065
        struct mailmime **result,
krista@3710
  1066
        bool transport_encode,
krista@3710
  1067
        bool set_attachment_forward_comment
vb@3439
  1068
    )
vb@3439
  1069
{
vb@3439
  1070
    PEP_STATUS status = PEP_STATUS_OK;
vb@3439
  1071
    struct mailmime * mime = NULL;
vb@3439
  1072
    char * mime_type;
vb@3439
  1073
    assert(blob);
vb@3439
  1074
    assert(result);
vb@3439
  1075
vb@3439
  1076
    *result = NULL;
vb@3439
  1077
vb@3439
  1078
// TODO: It seems the pEp COM server adapter sends an empty string here,
vb@3439
  1079
// which leads to a crash later. Thus, we workaround here by treating an
vb@3439
  1080
// empty string as NULL. We need to check whether the bug really is here,
vb@3439
  1081
// or the pEp COM server adapter needs to be changed.
vb@3439
  1082
    if (blob->mime_type == NULL || blob->mime_type[0] == '\0')
vb@3439
  1083
        mime_type = "application/octet-stream";
vb@3439
  1084
    else
vb@3439
  1085
        mime_type = blob->mime_type;
vb@3439
  1086
vb@3439
  1087
    pEp_rid_list_t* resource = parse_uri(blob->filename);
vb@3439
  1088
vb@3439
  1089
    bool already_ascii = !(must_chunk_be_encoded(blob->value, blob->size, true));
vb@3439
  1090
vb@3439
  1091
    mime = get_file_part(resource, mime_type, blob->value, blob->size, 
krista@3710
  1092
                          (already_ascii ? false : transport_encode),
krista@3710
  1093
                          set_attachment_forward_comment);
vb@3439
  1094
    free_rid_list(resource);
vb@3439
  1095
    
vb@3439
  1096
    assert(mime);
vb@3439
  1097
    if (mime == NULL)
vb@3439
  1098
        goto enomem;
vb@3439
  1099
vb@3439
  1100
    *result = mime;
vb@3439
  1101
    return PEP_STATUS_OK;
vb@3439
  1102
vb@3439
  1103
enomem:
vb@3439
  1104
    status = PEP_OUT_OF_MEMORY;
vb@3439
  1105
vb@3439
  1106
    if (mime)
vb@3439
  1107
        mailmime_free(mime);
vb@3439
  1108
vb@3439
  1109
    return status;
vb@3439
  1110
}
vb@3439
  1111
vb@3439
  1112
static PEP_STATUS mime_html_text(
vb@3439
  1113
        const char *plaintext,
vb@3439
  1114
        const char *htmltext,
vb@3439
  1115
        bloblist_t *attachments,
vb@3439
  1116
        struct mailmime **result,
vb@3439
  1117
        bool transport_encode
vb@3439
  1118
    )
vb@3439
  1119
{
vb@3439
  1120
    PEP_STATUS status = PEP_STATUS_OK;
vb@3439
  1121
    struct mailmime * top_level_html_mime = NULL;
vb@3439
  1122
    struct mailmime * mime = NULL;
vb@3439
  1123
    struct mailmime * submime = NULL;
vb@3439
  1124
    int r;
vb@3439
  1125
vb@3439
  1126
    assert(plaintext);
vb@3439
  1127
    assert(htmltext);
vb@3439
  1128
    assert(result);
vb@3439
  1129
vb@3439
  1130
    *result = NULL;
vb@3439
  1131
krista@4578
  1132
    int encoding_type = (transport_encode ? MAILMIME_MECHANISM_QUOTED_PRINTABLE : 0);
vb@4502
  1133
    pEp_rid_list_t* resource = NULL;
krista@4578
  1134
        
krista@4578
  1135
    if (*plaintext != '\0') {
krista@4578
  1136
        mime = part_multiple_new("multipart/alternative");
krista@4578
  1137
        assert(mime);
krista@4578
  1138
        if (mime == NULL)
krista@4578
  1139
            goto enomem;
krista@4578
  1140
        
krista@4578
  1141
        submime = get_text_part(NULL, "text/plain", plaintext, strlen(plaintext),
krista@4578
  1142
                encoding_type);
krista@4578
  1143
        free_rid_list(resource);
krista@4578
  1144
        resource = NULL;
krista@4578
  1145
        
krista@4578
  1146
        assert(submime);
krista@4578
  1147
        if (submime == NULL)
krista@4578
  1148
            goto enomem;
krista@4578
  1149
krista@4578
  1150
        r = mailmime_smart_add_part(mime, submime);
krista@4578
  1151
        assert(r == MAILIMF_NO_ERROR);
krista@4578
  1152
        if (r == MAILIMF_ERROR_MEMORY) {
krista@4578
  1153
            goto enomem;
krista@4578
  1154
        }
krista@4578
  1155
        else {
krista@4578
  1156
            // mailmime_smart_add_part() takes ownership of submime
krista@4578
  1157
            submime = NULL;
krista@4578
  1158
        }
krista@4578
  1159
    }
vb@3439
  1160
    
vb@3439
  1161
    bool inlined_attachments = false;
vb@3439
  1162
    
vb@3439
  1163
    bloblist_t* traversal_ptr = attachments;
vb@3439
  1164
    
vb@3439
  1165
    while (traversal_ptr) {
vb@3439
  1166
        if (traversal_ptr->disposition == PEP_CONTENT_DISP_INLINE) {
vb@3439
  1167
            inlined_attachments = true;
vb@3439
  1168
            break;
vb@3439
  1169
        }
vb@3439
  1170
        traversal_ptr = traversal_ptr->next;
vb@3439
  1171
    }
vb@3439
  1172
vb@3439
  1173
    if (inlined_attachments) {
vb@3439
  1174
        /* Noooooo... dirk, why do you do this to me? */
vb@3439
  1175
        submime = part_multiple_new("multipart/related");
vb@3439
  1176
        assert(submime);
vb@3439
  1177
        if (submime == NULL)
vb@3439
  1178
            goto enomem;
vb@3439
  1179
krista@4578
  1180
        // This is where all of the html MIME stuff will go
vb@3439
  1181
        top_level_html_mime = submime;
vb@3439
  1182
        
krista@4578
  1183
        if (!mime)
krista@4578
  1184
            mime = top_level_html_mime;
krista@4578
  1185
        else {    
krista@4578
  1186
            r = mailmime_smart_add_part(mime, top_level_html_mime);
krista@4578
  1187
            assert(r == MAILIMF_NO_ERROR);
krista@4578
  1188
            if (r == MAILIMF_ERROR_MEMORY) {
krista@4578
  1189
                goto enomem;
krista@4578
  1190
            }
krista@4578
  1191
            else {
krista@4578
  1192
                // mailmime_smart_add_part() takes ownership of submime
krista@4578
  1193
                submime = NULL;
krista@4578
  1194
            }
krista@4578
  1195
        }    
vb@3439
  1196
    }
vb@3439
  1197
    else {
krista@4578
  1198
        // Otherwise, html MIME stuff gets added to the top node 
krista@4578
  1199
        // - may be NULL if there's no multipart!
vb@3439
  1200
        top_level_html_mime = mime;
vb@3439
  1201
    }
vb@3439
  1202
vb@3439
  1203
//    resource = new_rid_node(PEP_RID_FILENAME, "msg.html");
vb@3439
  1204
    submime = get_text_part(NULL, "text/html", htmltext, strlen(htmltext),
vb@3439
  1205
            encoding_type);
vb@3439
  1206
    free_rid_list(resource);
vb@3439
  1207
    resource = NULL;
vb@3439
  1208
    
vb@3439
  1209
    assert(submime);
vb@3439
  1210
    if (submime == NULL)
vb@3439
  1211
        goto enomem;
krista@4578
  1212
        
krista@4578
  1213
    // IF there are no inlined attachments AND mime is NULL, then 
krista@4578
  1214
    // we just have an HTML body here and won't need to 
krista@4578
  1215
    // process inlined attachments - submime will actually be 
krista@4578
  1216
    // the mime root of from this function, at least.    
krista@4578
  1217
krista@4578
  1218
    if (!top_level_html_mime) {
krista@4578
  1219
        mime = submime;
vb@3439
  1220
        submime = NULL;
vb@3439
  1221
    }
krista@4578
  1222
    else {    
vb@3439
  1223
        r = mailmime_smart_add_part(top_level_html_mime, submime);
vb@3439
  1224
        assert(r == MAILIMF_NO_ERROR);
krista@4578
  1225
        if (r == MAILIMF_ERROR_MEMORY)
vb@3439
  1226
            goto enomem;
vb@3439
  1227
        else {
vb@3439
  1228
            // mailmime_smart_add_part() takes ownership of submime
vb@3439
  1229
            submime = NULL;
vb@3439
  1230
        }
krista@4578
  1231
krista@4578
  1232
        bloblist_t *_a;
krista@4578
  1233
        for (_a = attachments; _a != NULL; _a = _a->next) {
krista@4578
  1234
            if (_a->disposition != PEP_CONTENT_DISP_INLINE)
krista@4578
  1235
                continue;
krista@4578
  1236
            status = mime_attachment(_a, &submime, transport_encode, false);
krista@4578
  1237
            if (status != PEP_STATUS_OK)
krista@4578
  1238
                return PEP_UNKNOWN_ERROR; // FIXME
krista@4578
  1239
krista@4578
  1240
            r = mailmime_smart_add_part(top_level_html_mime, submime);
krista@4578
  1241
            assert(r == MAILIMF_NO_ERROR);
krista@4578
  1242
            if (r == MAILIMF_ERROR_MEMORY) {
krista@4578
  1243
                goto enomem;
krista@4578
  1244
            }
krista@4578
  1245
            else {
krista@4578
  1246
                // mailmime_smart_add_part() takes ownership of submime
krista@4578
  1247
                submime = NULL;
krista@4578
  1248
            }
krista@4578
  1249
        }
krista@4578
  1250
    }    
vb@3439
  1251
    *result = mime;
vb@3439
  1252
    return PEP_STATUS_OK;
vb@3439
  1253
vb@3439
  1254
enomem:
vb@3439
  1255
    status = PEP_OUT_OF_MEMORY;
vb@3439
  1256
vb@3439
  1257
    if (mime)
vb@3439
  1258
        mailmime_free(mime);
vb@3439
  1259
vb@3439
  1260
    if (submime)
vb@3439
  1261
        mailmime_free(submime);
vb@3439
  1262
vb@3439
  1263
    return status;
vb@3439
  1264
}
vb@3439
  1265
vb@3439
  1266
vb@3439
  1267
// FIXME: maybe need to add transport_encode field here
vb@3439
  1268
static struct mailimf_mailbox * identity_to_mailbox(const pEp_identity *ident)
vb@3439
  1269
{
vb@3439
  1270
    char *_username = NULL;
vb@3439
  1271
    struct mailimf_mailbox *mb;
vb@3439
  1272
vb@3439
  1273
    if (!ident->username)
vb@3439
  1274
        _username = strdup("");
vb@3439
  1275
    else
vb@3439
  1276
        _username = must_field_value_be_encoded(ident->username) ?
vb@3439
  1277
                    mailmime_encode_subject_header("utf-8", ident->username, 0) : 
vb@3439
  1278
                    strdup(ident->username);
vb@3439
  1279
                  
vb@3439
  1280
    assert(_username);
vb@3439
  1281
    if (_username == NULL)
vb@3439
  1282
        goto enomem;
vb@3439
  1283
vb@3439
  1284
    mb = mailbox_from_string(_username, ident->address);
vb@3439
  1285
    if (mb == NULL)
vb@3439
  1286
        goto enomem;
vb@3439
  1287
vb@3439
  1288
    free(_username);
vb@3439
  1289
    _username = NULL;
vb@3439
  1290
vb@3439
  1291
    return mb;
vb@3439
  1292
vb@3439
  1293
enomem:
vb@3439
  1294
    free(_username);
vb@3439
  1295
    return NULL;
vb@3439
  1296
}
vb@3439
  1297
vb@3439
  1298
static struct mailimf_mailbox_list * identity_to_mbl(
vb@3439
  1299
        const pEp_identity *ident)
vb@3439
  1300
{
vb@3439
  1301
    struct mailimf_mailbox_list *mbl = NULL;
vb@3439
  1302
    struct mailimf_mailbox *mb = NULL;
vb@3439
  1303
    clist *list = NULL;
vb@3439
  1304
    int r;
vb@3439
  1305
vb@3439
  1306
    assert(ident);
vb@3439
  1307
vb@3439
  1308
    list = clist_new();
vb@3439
  1309
    if (list == NULL)
vb@3439
  1310
        goto enomem;
vb@3439
  1311
vb@3439
  1312
    mb = identity_to_mailbox(ident);
vb@3439
  1313
    if (mb == NULL)
vb@3439
  1314
        goto enomem;
vb@3439
  1315
vb@3439
  1316
    r = clist_append(list, mb);
vb@3439
  1317
    if (r)
vb@3439
  1318
        goto enomem;
vb@3439
  1319
vb@3439
  1320
    mbl = mailimf_mailbox_list_new(list);
vb@3439
  1321
    if (mbl == NULL)
vb@3439
  1322
        goto enomem;
vb@3439
  1323
vb@3439
  1324
    return mbl;
vb@3439
  1325
vb@3439
  1326
enomem:
vb@3439
  1327
    if (mb)
vb@3439
  1328
        mailimf_mailbox_free(mb);
vb@3439
  1329
vb@3439
  1330
    if (list)
vb@3439
  1331
        clist_free(list);
vb@3439
  1332
vb@3439
  1333
    return NULL;
vb@3439
  1334
}
vb@3439
  1335
vb@3439
  1336
static struct mailimf_address_list * identity_list_to_mal(identity_list *il)
vb@3439
  1337
{
vb@3439
  1338
    struct mailimf_address_list *mal = NULL;
vb@3439
  1339
    struct mailimf_mailbox *mb = NULL;
vb@3439
  1340
    struct mailimf_address * addr = NULL;
vb@3439
  1341
    clist *list = NULL;
vb@3439
  1342
    int r;
vb@3439
  1343
vb@3439
  1344
    assert(il);
vb@3439
  1345
vb@3439
  1346
    list = clist_new();
vb@3439
  1347
    if (list == NULL)
vb@3439
  1348
        goto enomem;
vb@3439
  1349
vb@3439
  1350
    identity_list *_il;
vb@3439
  1351
    for (_il = il; _il && _il->ident; _il = _il->next) {
vb@3439
  1352
        mb = identity_to_mailbox(_il->ident);
vb@3439
  1353
        if (mb == NULL)
vb@3439
  1354
            goto enomem;
vb@3439
  1355
vb@3439
  1356
        addr = mailimf_address_new(MAILIMF_ADDRESS_MAILBOX, mb, NULL);
vb@3439
  1357
        if (addr == NULL)
vb@3439
  1358
            goto enomem;
vb@3439
  1359
        mb = NULL;
vb@3439
  1360
vb@3439
  1361
        r = clist_append(list, addr);
vb@3439
  1362
        if (r)
vb@3439
  1363
            goto enomem;
vb@3439
  1364
        addr = NULL;
vb@3439
  1365
    }
vb@3439
  1366
    mal = mailimf_address_list_new(list);
vb@3439
  1367
    if (mal == NULL)
vb@3439
  1368
        goto enomem;
vb@3439
  1369
vb@3439
  1370
    return mal;
vb@3439
  1371
vb@3439
  1372
enomem:
vb@3439
  1373
    if (mb)
vb@3439
  1374
        mailimf_mailbox_free(mb);
vb@3439
  1375
vb@3439
  1376
    if (addr)
vb@3439
  1377
        mailimf_address_free(addr);
vb@3439
  1378
vb@3439
  1379
    if (list)
vb@3439
  1380
        clist_free(list);
vb@3439
  1381
vb@3439
  1382
    return NULL;
vb@3439
  1383
}
vb@3439
  1384
vb@3439
  1385
static clist * stringlist_to_clist(stringlist_t *sl, bool transport_encode)
vb@3439
  1386
{
vb@3439
  1387
    clist * cl = clist_new();
vb@3439
  1388
    assert(cl);
vb@3439
  1389
    if (cl == NULL)
vb@3439
  1390
        return NULL;
vb@3439
  1391
vb@3439
  1392
    if (!sl || ((!sl->value || sl->value[0] == '\0') && sl->next == NULL))
vb@3439
  1393
        return cl;
vb@3439
  1394
        
vb@3439
  1395
    stringlist_t *_sl;
vb@3439
  1396
    for (_sl = sl; _sl; _sl = _sl->next) {
vb@3439
  1397
        int r;
vb@3439
  1398
        char * value = ((transport_encode && must_field_value_be_encoded(_sl->value)) ?
vb@3439
  1399
                        mailmime_encode_subject_header("utf-8", _sl->value, 0) :
vb@3439
  1400
                        strdup(_sl->value));
vb@3439
  1401
        assert(value);
vb@3439
  1402
        if (value == NULL) {
vb@3439
  1403
            clist_free(cl);
vb@3439
  1404
            return NULL;
vb@3439
  1405
        }
vb@3439
  1406
        r = clist_append(cl, value);
vb@3439
  1407
        assert(r == 0);
vb@3439
  1408
        if (r) {
vb@3439
  1409
            free(value);
vb@3439
  1410
            clist_free(cl);
vb@3439
  1411
            return NULL;
vb@3439
  1412
        }
vb@3439
  1413
    }
vb@3439
  1414
vb@3439
  1415
    return cl;
vb@3439
  1416
}
vb@3439
  1417
vb@3439
  1418
static PEP_STATUS build_fields(const message *msg, struct mailimf_fields **result)
vb@3439
  1419
{
vb@3439
  1420
    PEP_STATUS status = PEP_STATUS_OK;
vb@3439
  1421
    struct mailimf_fields * fields = NULL;
vb@3439
  1422
    int r;
vb@3439
  1423
    clist * fields_list = NULL;
vb@3439
  1424
    unsigned char pEpstr[] = PEP_SUBJ_STRING; // unsigned due to UTF-8 byte fun
vb@3439
  1425
#ifdef WIN32
vb@3439
  1426
    char* altstr = "pEp";
vb@3439
  1427
#else
vb@3439
  1428
    char* altstr = (char*)pEpstr;
vb@3439
  1429
#endif        
vb@4534
  1430
    char *subject = msg->shortmsg && msg->shortmsg[0] ? msg->shortmsg : altstr;
vb@3439
  1431
vb@3439
  1432
    assert(msg);
vb@3439
  1433
    assert(result);
vb@3439
  1434
vb@3439
  1435
    *result = NULL;
vb@3439
  1436
vb@3439
  1437
    fields_list = clist_new();
vb@3439
  1438
    assert(fields_list);
vb@3439
  1439
    if (fields_list == NULL)
vb@3439
  1440
        goto enomem;
vb@3439
  1441
vb@4534
  1442
    if (msg->id && msg->id[0]) {
vb@3439
  1443
        char *_msgid = strdup(msg->id);
vb@3439
  1444
        assert(_msgid);
vb@3439
  1445
        if (_msgid == NULL)
vb@3439
  1446
            goto enomem;
vb@3439
  1447
vb@3439
  1448
        r = _append_field(fields_list, MAILIMF_FIELD_MESSAGE_ID,
vb@3439
  1449
                (_new_func_t) mailimf_message_id_new, _msgid);
vb@3439
  1450
        if (r) {
vb@3439
  1451
            free(_msgid);
vb@3439
  1452
            goto enomem;
vb@3439
  1453
        }
vb@3439
  1454
    }
vb@3439
  1455
vb@3439
  1456
    if (msg->sent) {
vb@3439
  1457
        struct mailimf_date_time * dt = timestamp_to_etpantime(msg->sent);
vb@3439
  1458
        if (dt == NULL)
vb@3439
  1459
            goto enomem;
vb@3439
  1460
vb@3439
  1461
        r = _append_field(fields_list, MAILIMF_FIELD_ORIG_DATE,
vb@3439
  1462
                (_new_func_t) mailimf_orig_date_new, dt);
vb@3439
  1463
        if (r) {
vb@3439
  1464
            mailimf_date_time_free(dt);
vb@3439
  1465
            goto enomem;
vb@3439
  1466
        }
vb@3439
  1467
        dt = NULL;
vb@3439
  1468
    }
vb@3439
  1469
vb@3439
  1470
     if (msg->from) {
vb@3439
  1471
        struct mailimf_mailbox_list *from = identity_to_mbl(msg->from);
vb@3439
  1472
        if (from == NULL)
vb@3439
  1473
            goto enomem;
vb@3439
  1474
vb@3439
  1475
        r = _append_field(fields_list, MAILIMF_FIELD_FROM,
vb@3439
  1476
                (_new_func_t) mailimf_from_new, from);
vb@3439
  1477
        if (r) {
vb@3439
  1478
            mailimf_mailbox_list_free(from);
vb@3439
  1479
            goto enomem;
vb@3439
  1480
        }
vb@3439
  1481
    }
vb@3439
  1482
vb@4534
  1483
    if (msg->to && msg->to->ident) {
vb@3439
  1484
        struct mailimf_address_list *to = identity_list_to_mal(msg->to);
vb@3439
  1485
        if (to == NULL)
vb@3439
  1486
            goto enomem;
vb@3439
  1487
vb@3439
  1488
        r = _append_field(fields_list, MAILIMF_FIELD_TO,
vb@3439
  1489
                (_new_func_t) mailimf_to_new, to);
vb@3439
  1490
        if (r) {
vb@3439
  1491
            mailimf_address_list_free(to);
vb@3439
  1492
            goto enomem;
vb@3439
  1493
        }
vb@3439
  1494
    }
vb@3439
  1495
vb@3439
  1496
    char* _subject = NULL;
vb@3439
  1497
    if (!must_field_value_be_encoded(subject)) {
vb@3439
  1498
        _subject = strdup(subject);
vb@3439
  1499
        assert(_subject);
vb@3439
  1500
    }
vb@3439
  1501
    else {
vb@3439
  1502
        _subject = mailmime_encode_subject_header("utf-8", subject, 1);
vb@3439
  1503
    }
vb@3439
  1504
    if (_subject == NULL)
vb@3439
  1505
        goto enomem;
vb@3439
  1506
vb@3439
  1507
    r = _append_field(fields_list, MAILIMF_FIELD_SUBJECT,
vb@3439
  1508
            (_new_func_t) mailimf_subject_new, _subject);
vb@3439
  1509
    if (r) {
vb@3439
  1510
        free(_subject);
vb@3439
  1511
        goto enomem;
vb@3439
  1512
    }
vb@3439
  1513
vb@4534
  1514
    if (msg->cc && msg->cc->ident) {
vb@3439
  1515
        struct mailimf_address_list *cc = identity_list_to_mal(msg->cc);
vb@3439
  1516
        if (cc == NULL)
vb@3439
  1517
            goto enomem;
vb@3439
  1518
vb@3439
  1519
        r = _append_field(fields_list, MAILIMF_FIELD_CC,
vb@3439
  1520
                (_new_func_t) mailimf_cc_new, cc);
vb@3439
  1521
        if (r) {
vb@3439
  1522
            mailimf_address_list_free(cc);
vb@3439
  1523
            goto enomem;
vb@3439
  1524
        }
vb@3439
  1525
    }
vb@3439
  1526
    
vb@4534
  1527
    if (msg->bcc && msg->bcc->ident) {
vb@3439
  1528
        struct mailimf_address_list *bcc = identity_list_to_mal(msg->bcc);
vb@3439
  1529
        if (bcc == NULL)
vb@3439
  1530
            goto enomem;
vb@3439
  1531
vb@3439
  1532
        r = _append_field(fields_list, MAILIMF_FIELD_BCC,
vb@3439
  1533
                (_new_func_t) mailimf_bcc_new, bcc);
vb@3439
  1534
        if (r) {
vb@3439
  1535
            mailimf_address_list_free(bcc);
vb@3439
  1536
            goto enomem;
vb@3439
  1537
        }
vb@3439
  1538
    }
vb@3439
  1539
    
vb@4534
  1540
    if (msg->reply_to && msg->reply_to->ident) {
vb@3439
  1541
        struct mailimf_address_list *reply_to = identity_list_to_mal(msg->reply_to);
vb@3439
  1542
        if (reply_to == NULL)
vb@3439
  1543
            goto enomem;
vb@3439
  1544
vb@3439
  1545
        r = _append_field(fields_list, MAILIMF_FIELD_REPLY_TO,
vb@3439
  1546
                (_new_func_t) mailimf_reply_to_new, reply_to);
vb@3439
  1547
        if (r) {
vb@3439
  1548
            mailimf_address_list_free(reply_to);
vb@3439
  1549
            goto enomem;
vb@3439
  1550
        }
vb@3439
  1551
    }
vb@3439
  1552
vb@4534
  1553
    if (msg->in_reply_to && msg->in_reply_to->value) {
vb@3439
  1554
        clist *in_reply_to = stringlist_to_clist(msg->in_reply_to, true);
vb@3439
  1555
        if (in_reply_to == NULL)
vb@3439
  1556
            goto enomem;
vb@3439
  1557
vb@3439
  1558
        r = _append_field(fields_list, MAILIMF_FIELD_IN_REPLY_TO,
vb@3439
  1559
                (_new_func_t) mailimf_in_reply_to_new, in_reply_to);
vb@3439
  1560
        if (r) {
vb@3439
  1561
            clist_free(in_reply_to);
vb@3439
  1562
            goto enomem;
vb@3439
  1563
        }
vb@3439
  1564
    }
vb@3439
  1565
vb@4534
  1566
    if (msg->references && msg->references->value) {
vb@3439
  1567
        clist *references = stringlist_to_clist(msg->references, true);
vb@3439
  1568
        if (references == NULL)
vb@3439
  1569
            goto enomem;
vb@3439
  1570
vb@3439
  1571
        r = _append_field(fields_list, MAILIMF_FIELD_REFERENCES,
vb@3439
  1572
                (_new_func_t) mailimf_references_new, references);
vb@3439
  1573
        if (r) {
vb@3439
  1574
            clist_free(references);
vb@3439
  1575
            goto enomem;
vb@3439
  1576
        }
vb@3439
  1577
    }
vb@3439
  1578
vb@4534
  1579
    if (msg->keywords && msg->keywords->value) {
vb@3439
  1580
        clist *keywords = stringlist_to_clist(msg->keywords, true);
vb@3439
  1581
        if (keywords == NULL)
vb@3439
  1582
            goto enomem;
vb@3439
  1583
vb@3439
  1584
        r = _append_field(fields_list, MAILIMF_FIELD_KEYWORDS,
vb@3439
  1585
                (_new_func_t) mailimf_keywords_new, keywords);
vb@3439
  1586
        if (r) {
vb@3439
  1587
            clist_free(keywords);
vb@3439
  1588
            goto enomem;
vb@3439
  1589
        }
vb@3439
  1590
    }
vb@3439
  1591
vb@4534
  1592
    if (msg->comments && msg->comments[0]) {
vb@3439
  1593
        char *comments = NULL;
vb@3439
  1594
        if (!must_field_value_be_encoded(msg->comments)) {
vb@3439
  1595
            comments = strdup(msg->comments);
vb@3439
  1596
            assert(comments);
vb@3439
  1597
        }
vb@3439
  1598
        else  {
vb@3439
  1599
            comments = mailmime_encode_subject_header("utf-8", msg->comments, 0);
vb@3439
  1600
        }
vb@3439
  1601
        if (comments == NULL)
vb@3439
  1602
            goto enomem;
vb@3439
  1603
vb@3439
  1604
        r = _append_field(fields_list, MAILIMF_FIELD_COMMENTS,
vb@3439
  1605
                (_new_func_t) mailimf_comments_new, comments);
vb@3439
  1606
        if (r) {
vb@3439
  1607
            free(comments);
vb@3439
  1608
            goto enomem;
vb@3439
  1609
        }
vb@3439
  1610
    }
vb@3439
  1611
vb@4534
  1612
    if (msg->opt_fields && msg->opt_fields->value) {
vb@3439
  1613
        stringpair_list_t *_l;
vb@3439
  1614
        for (_l = msg->opt_fields; _l && _l->value; _l = _l->next) {
vb@3439
  1615
            char *key = _l->value->key;
vb@3439
  1616
            char *value = _l->value->value;
vb@3439
  1617
            if (key && value) {
vb@3439
  1618
                r = _append_optional_field(fields_list, key, value);
vb@3439
  1619
vb@3439
  1620
                if (r)
vb@3439
  1621
                    goto enomem;
vb@3439
  1622
            }
vb@3439
  1623
        }
vb@3439
  1624
    }
vb@3439
  1625
vb@3439
  1626
    fields = mailimf_fields_new(fields_list);
vb@3439
  1627
    assert(fields);
vb@3439
  1628
    if (fields == NULL)
vb@3439
  1629
        goto enomem;
vb@3439
  1630
vb@3439
  1631
    *result = fields;
vb@3439
  1632
vb@3439
  1633
    return PEP_STATUS_OK;
vb@3439
  1634
vb@3439
  1635
enomem:
vb@3439
  1636
    status = PEP_OUT_OF_MEMORY;
vb@3439
  1637
vb@3439
  1638
    if (fields_list)
vb@3439
  1639
        clist_free(fields_list);
vb@3439
  1640
vb@3439
  1641
    if (fields)
vb@3439
  1642
        mailimf_fields_free(fields);
vb@3439
  1643
vb@3439
  1644
    return status;
vb@3439
  1645
}
vb@3439
  1646
vb@3439
  1647
static bool has_exceptional_extension(char* filename) {
vb@3439
  1648
    if (!filename)
vb@3439
  1649
        return false;
vb@3439
  1650
    int len = strlen(filename);
vb@3439
  1651
    if (len < 4)
vb@3439
  1652
        return false;
vb@3439
  1653
    char* ext_start = filename + (len - 4);
vb@3439
  1654
    if (strcmp(ext_start, ".pgp") == 0 || strcmp(ext_start, ".gpg") == 0 ||
vb@3439
  1655
        strcmp(ext_start, ".asc") == 0 || strcmp(ext_start, ".pEp") == 0)
vb@3439
  1656
        return true;
vb@3439
  1657
    return false;
vb@3439
  1658
}
vb@3439
  1659
vb@3439
  1660
static pEp_rid_list_t* choose_resource_id(pEp_rid_list_t* rid_list) {
vb@3439
  1661
    pEp_rid_list_t* retval = rid_list;
vb@3439
  1662
    
vb@3439
  1663
    /* multiple elements - least common case */
vb@3439
  1664
    if (rid_list && rid_list->next) {
vb@3439
  1665
        pEp_rid_list_t* rid_list_curr = rid_list;
vb@3439
  1666
        retval = rid_list; 
vb@3439
  1667
        
vb@3439
  1668
        while (rid_list_curr) {
vb@3439
  1669
            pEp_resource_id_type rid_type = rid_list_curr->rid_type;
vb@3439
  1670
            if (rid_type == PEP_RID_CID)
vb@3439
  1671
                retval = rid_list_curr;
vb@3439
  1672
            else if (rid_type == PEP_RID_FILENAME && has_exceptional_extension(rid_list_curr->rid))
vb@3439
  1673
                return rid_list_curr;
vb@3439
  1674
            rid_list_curr = rid_list_curr->next;
vb@3439
  1675
        }
vb@3439
  1676
    } 
vb@3439
  1677
    return retval;
vb@3439
  1678
}
vb@3439
  1679
vb@3439
  1680
// static void split_inlined_and_attached(bloblist_t** inlined, bloblist_t** attached) {
vb@3439
  1681
//     bloblist_t** curr_pp = attached;
vb@3439
  1682
//     bloblist_t* curr = *curr_pp;
vb@3439
  1683
//     
vb@3439
  1684
//     bloblist_t* inline_ret = NULL;
vb@3439
  1685
//     bloblist_t** inline_curr_pp = &inline_ret;
vb@3439
  1686
//     
vb@3439
  1687
//     bloblist_t* att_ret = NULL;
vb@3439
  1688
//     bloblist_t** att_curr_pp = &att_ret;
vb@3439
  1689
//     
vb@3439
  1690
//     while (curr) {
vb@3439
  1691
//         if (curr->disposition == PEP_CONTENT_DISP_INLINE) {
vb@3439
  1692
//             *inline_curr_pp = curr;
vb@3439
  1693
//             inline_curr_pp = &(curr->next);
vb@3439
  1694
//         }
vb@3439
  1695
//         else {
vb@3439
  1696
//             *att_curr_pp = curr;
vb@3439
  1697
//             att_curr_pp = &(curr->next);            
vb@3439
  1698
//         }
vb@3439
  1699
//         *curr_pp = curr->next;
vb@3439
  1700
//         curr->next = NULL;
vb@3439
  1701
//         curr = *curr_pp;
vb@3439
  1702
//     }
vb@3439
  1703
//     
vb@3439
  1704
//     *inlined = inline_ret;
vb@3439
  1705
//     *attached = att_ret;
vb@3439
  1706
// }
vb@3439
  1707
vb@3439
  1708
vb@3439
  1709
static PEP_STATUS mime_encode_message_plain(
vb@3439
  1710
        const message *msg,
vb@3439
  1711
        bool omit_fields,
vb@3439
  1712
        struct mailmime **result,
krista@3710
  1713
        bool transport_encode,
krista@3710
  1714
        bool set_attachment_forward_comment
vb@3439
  1715
    )
vb@3439
  1716
{
vb@3439
  1717
    struct mailmime * mime = NULL;
vb@3439
  1718
    struct mailmime * submime = NULL;
vb@3439
  1719
    int r;
vb@3439
  1720
    PEP_STATUS status;
vb@3439
  1721
    //char *subject;
krista@4578
  1722
    const char *plaintext;
vb@3439
  1723
    char *htmltext;
vb@3439
  1724
vb@3439
  1725
    assert(msg);
vb@3439
  1726
    assert(result);
vb@3439
  1727
    
vb@3439
  1728
    plaintext = (msg->longmsg) ? msg->longmsg : "";
vb@3439
  1729
    htmltext = msg->longmsg_formatted;
vb@3439
  1730
vb@3439
  1731
    if (htmltext && (htmltext[0] != '\0')) {
vb@3439
  1732
        /* first, we need to strip out the inlined attachments to ensure this
vb@3439
  1733
           gets set up correctly */
vb@3439
  1734
           
vb@3439
  1735
        status = mime_html_text(plaintext, htmltext, msg->attachments, &mime,
vb@3439
  1736
                                transport_encode);
vb@3439
  1737
                
vb@3439
  1738
        if (status != PEP_STATUS_OK)
vb@3439
  1739
            goto pEp_error;
vb@3439
  1740
    }
vb@3439
  1741
    else {
vb@3439
  1742
        pEp_rid_list_t* resource = NULL;
vb@3439
  1743
        if (is_PGP_message_text(plaintext)) {
vb@4502
  1744
            resource = NULL;
vb@3439
  1745
            int encoding_type = (transport_encode ? MAILMIME_MECHANISM_7BIT : 0);
vb@3439
  1746
            mime = get_text_part(resource, "application/octet-stream", plaintext,
vb@3439
  1747
                    strlen(plaintext), encoding_type);
vb@3439
  1748
        }
vb@3439
  1749
        else {
vb@4502
  1750
            resource = NULL;
vb@3439
  1751
            int encoding_type = (transport_encode ? MAILMIME_MECHANISM_QUOTED_PRINTABLE : 0);
vb@3439
  1752
            mime = get_text_part(resource, "text/plain", plaintext, strlen(plaintext),
vb@3439
  1753
                    encoding_type);
vb@3439
  1754
        }
vb@3439
  1755
        free_rid_list(resource);
vb@3439
  1756
        
vb@3439
  1757
        assert(mime);
vb@3439
  1758
        if (mime == NULL)
vb@3439
  1759
            goto enomem;
vb@3439
  1760
    }
vb@3439
  1761
vb@3439
  1762
    bool normal_attachments = false;
vb@3439
  1763
    
vb@3439
  1764
    bloblist_t* traversal_ptr = msg->attachments;
vb@3439
  1765
    
krista@4581
  1766
    // If there were any inline attachments, they should have 
krista@4581
  1767
    // been stripped out in mime_html_text and dealt with. 
krista@4581
  1768
    // I'm not entirely sure what the alternative case 
krista@4581
  1769
    // is here. But basically, if there are any non-inlined 
krista@4581
  1770
    // attachments to deal with, this is designed to 
krista@4581
  1771
    // make sure we process them. So flag it for 
krista@4581
  1772
    // "hey, Bob, you got some regular attachments here"
krista@4581
  1773
    // so Bob (obviously, the MIME engine is called Bob)
krista@4581
  1774
    // can do the right thing in the next block.
vb@3439
  1775
    while (traversal_ptr) {
vb@3439
  1776
        if (traversal_ptr->disposition != PEP_CONTENT_DISP_INLINE) {
vb@3439
  1777
            normal_attachments = true;
vb@3439
  1778
            break;
vb@3439
  1779
        }
vb@3439
  1780
        traversal_ptr = traversal_ptr->next;
vb@3439
  1781
    }
vb@3439
  1782
vb@3439
  1783
    if (normal_attachments) {
vb@3439
  1784
        submime = mime;
vb@3439
  1785
        mime = part_multiple_new("multipart/mixed");
vb@3439
  1786
        assert(mime);
vb@3439
  1787
        if (mime == NULL)
vb@3439
  1788
            goto enomem;
vb@3439
  1789
vb@3439
  1790
        r = mailmime_smart_add_part(mime, submime);
vb@3439
  1791
        assert(r == MAILIMF_NO_ERROR);
vb@3439
  1792
        if (r == MAILIMF_ERROR_MEMORY) {
vb@3439
  1793
            goto enomem;
vb@3439
  1794
        }
vb@3439
  1795
        else {
vb@3439
  1796
            // mailmime_smart_add_part() takes ownership of submime
vb@3439
  1797
            submime = NULL;
vb@3439
  1798
        }
vb@3439
  1799
vb@3439
  1800
        bloblist_t *_a;
krista@3710
  1801
        bool first_one = true;
krista@3710
  1802
        
krista@4581
  1803
        // Go through the non-inline attachments and add em.
vb@3439
  1804
        for (_a = msg->attachments; _a != NULL; _a = _a->next) {
vb@3439
  1805
vb@3439
  1806
            if (_a->disposition == PEP_CONTENT_DISP_INLINE)
vb@3439
  1807
                continue;
vb@3439
  1808
krista@3710
  1809
            status = mime_attachment(_a, &submime, transport_encode,
krista@3710
  1810
                                     (first_one && set_attachment_forward_comment));                         
vb@3439
  1811
            if (status != PEP_STATUS_OK)
vb@3439
  1812
                goto pEp_error;
krista@3710
  1813
            
krista@3710
  1814
            first_one = false;    
vb@3439
  1815
vb@3439
  1816
            r = mailmime_smart_add_part(mime, submime);
vb@3439
  1817
            assert(r == MAILIMF_NO_ERROR);
vb@3439
  1818
            if (r == MAILIMF_ERROR_MEMORY) {
vb@3439
  1819
                goto enomem;
vb@3439
  1820
            }
vb@3439
  1821
            else {
vb@3439
  1822
                // mailmime_smart_add_part() takes ownership of submime
vb@3439
  1823
                submime = NULL;
vb@3439
  1824
            }
vb@3439
  1825
        }
vb@3439
  1826
    }
vb@3439
  1827
vb@3439
  1828
    *result = mime;
vb@3439
  1829
    return PEP_STATUS_OK;
vb@3439
  1830
vb@3439
  1831
enomem:
vb@3439
  1832
    status = PEP_OUT_OF_MEMORY;
vb@3439
  1833
vb@3439
  1834
pEp_error:
vb@3439
  1835
    if (mime)
vb@3439
  1836
        mailmime_free(mime);
vb@3439
  1837
vb@3439
  1838
    if (submime)
vb@3439
  1839
        mailmime_free(submime);
vb@3439
  1840
vb@3439
  1841
    return status;
vb@3439
  1842
}
vb@3439
  1843
vb@3439
  1844
static PEP_STATUS mime_encode_message_PGP_MIME(
vb@3439
  1845
        const message * msg,
vb@3439
  1846
        bool omit_fields,
vb@3439
  1847
        struct mailmime **result
vb@3439
  1848
    )
vb@3439
  1849
{
vb@3439
  1850
    struct mailmime * mime = NULL;
vb@3439
  1851
    struct mailmime * submime = NULL;
vb@3439
  1852
	struct mailmime_parameter * param;
vb@3439
  1853
    int r;
vb@3439
  1854
    PEP_STATUS status;
vb@3439
  1855
    char *plaintext;
vb@3439
  1856
    size_t plaintext_size;
vb@3439
  1857
vb@3439
  1858
    assert(msg->attachments && msg->attachments->next &&
vb@3439
  1859
            msg->attachments->next->value);
vb@3439
  1860
vb@3439
  1861
    plaintext = msg->attachments->next->value;
vb@3439
  1862
    plaintext_size = msg->attachments->next->size;
vb@3439
  1863
vb@3439
  1864
    mime = part_multiple_new("multipart/encrypted");
vb@3439
  1865
    assert(mime);
vb@3439
  1866
    if (mime == NULL)
vb@3439
  1867
        goto enomem;
vb@3439
  1868
vb@3439
  1869
    param = mailmime_param_new_with_data("protocol", "application/pgp-encrypted");
vb@3439
  1870
    clist_append(mime->mm_content_type->ct_parameters, param);
vb@3439
  1871
vb@3439
  1872
    submime = get_pgp_encrypted_part();
vb@3439
  1873
    assert(submime);
vb@3439
  1874
    if (submime == NULL)
vb@3439
  1875
        goto enomem;
vb@3439
  1876
vb@3439
  1877
    r = mailmime_smart_add_part(mime, submime);
vb@3439
  1878
    assert(r == MAILIMF_NO_ERROR);
vb@3439
  1879
    if (r == MAILIMF_ERROR_MEMORY) {
vb@3439
  1880
        goto enomem;
vb@3439
  1881
    }
vb@3439
  1882
    else {
vb@3439
  1883
        // mailmime_smart_add_part() takes ownership of submime
vb@3439
  1884
        submime = NULL;
vb@3439
  1885
    }
vb@3439
  1886
vb@3439
  1887
    pEp_rid_list_t* resource = new_rid_node(PEP_RID_FILENAME, "msg.asc");
vb@3439
  1888
    submime = get_text_part(resource, "application/octet-stream", plaintext,
vb@3439
  1889
            plaintext_size, MAILMIME_MECHANISM_7BIT);
vb@3439
  1890
            
vb@3439
  1891
    free_rid_list(resource);
vb@3439
  1892
    
vb@3439
  1893
    assert(submime);
vb@3439
  1894
    if (submime == NULL)
vb@3439
  1895
        goto enomem;
vb@3439
  1896
vb@3439
  1897
    r = mailmime_smart_add_part(mime, submime);
vb@3439
  1898
    assert(r == MAILIMF_NO_ERROR);
vb@3439
  1899
    if (r == MAILIMF_ERROR_MEMORY) {
vb@3439
  1900
        goto enomem;
vb@3439
  1901
    }
vb@3439
  1902
    else {
vb@3439
  1903
        // mailmime_smart_add_part() takes ownership of submime
vb@3439
  1904
        submime = NULL;
vb@3439
  1905
    }
vb@3439
  1906
vb@3439
  1907
    *result = mime;
vb@3439
  1908
    return PEP_STATUS_OK;
vb@3439
  1909
vb@3439
  1910
enomem:
vb@3439
  1911
    status = PEP_OUT_OF_MEMORY;
vb@3439
  1912
vb@3439
  1913
    if (mime)
vb@3439
  1914
        mailmime_free(mime);
vb@3439
  1915
vb@3439
  1916
    if (submime)
vb@3439
  1917
        mailmime_free(submime);
vb@3439
  1918
vb@3439
  1919
    return status;
vb@3439
  1920
}
vb@3439
  1921
vb@3439
  1922
PEP_STATUS _mime_encode_message_internal(
vb@3439
  1923
        const message * msg,
vb@3439
  1924
        bool omit_fields,
vb@3439
  1925
        char **mimetext,
krista@3710
  1926
        bool transport_encode,
krista@3710
  1927
        bool set_attachment_forward_comment
vb@3439
  1928
    )
vb@3439
  1929
{
vb@3439
  1930
    PEP_STATUS status = PEP_STATUS_OK;
vb@3439
  1931
    struct mailmime * msg_mime = NULL;
vb@3439
  1932
    struct mailmime * mime = NULL;
vb@3439
  1933
    struct mailimf_fields * fields = NULL;
vb@3439
  1934
    char *buf = NULL;
vb@3439
  1935
    int r;
vb@3439
  1936
vb@3439
  1937
    assert(msg);
vb@3439
  1938
    assert(mimetext);
vb@3439
  1939
vb@3439
  1940
    if (!(msg && mimetext))
vb@3439
  1941
        return PEP_ILLEGAL_VALUE;
vb@3439
  1942
vb@3439
  1943
    *mimetext = NULL;
vb@3439
  1944
vb@3439
  1945
    switch (msg->enc_format) {
vb@3439
  1946
        case PEP_enc_none:
krista@3710
  1947
            status = mime_encode_message_plain(msg, omit_fields, &mime, transport_encode, set_attachment_forward_comment);
vb@3439
  1948
            break;
vb@3439
  1949
krista@3710
  1950
        // I'm presuming we should hardcore ignoring set_attachment_forward_comment here...
vb@3678
  1951
        case PEP_enc_inline:
krista@3710
  1952
            status = mime_encode_message_plain(msg, omit_fields, &mime, transport_encode, false);
vb@3439
  1953
            break;
vb@3439
  1954
vb@3439
  1955
        case PEP_enc_S_MIME:
vb@3439
  1956
            NOT_IMPLEMENTED
vb@3439
  1957
                
vb@3439
  1958
        case PEP_enc_PGP_MIME:
vb@3439
  1959
            status = mime_encode_message_PGP_MIME(msg, omit_fields, &mime);
vb@3439
  1960
            break;
vb@3439
  1961
vb@3439
  1962
        case PEP_enc_PEP:
vb@4489
  1963
            // today's pEp message format is PGP/MIME from the outside
vb@4489
  1964
            status = mime_encode_message_PGP_MIME(msg, omit_fields, &mime);
vb@4489
  1965
            break;
vb@3439
  1966
vb@3439
  1967
        default:
vb@3439
  1968
            NOT_IMPLEMENTED
vb@3439
  1969
    }
vb@3439
  1970
vb@3439
  1971
    if (status != PEP_STATUS_OK)
vb@3439
  1972
        goto pEp_error;
vb@3439
  1973
vb@3439
  1974
    msg_mime = mailmime_new_message_data(NULL);
vb@3439
  1975
    assert(msg_mime);
vb@3439
  1976
    if (msg_mime == NULL)
vb@3439
  1977
        goto enomem;
vb@3439
  1978
vb@3439
  1979
    r = mailmime_add_part(msg_mime, mime);
vb@3439
  1980
    if (r) {
vb@3439
  1981
        mailmime_free(mime);
vb@3439
  1982
        goto enomem;
vb@3439
  1983
    }
vb@3439
  1984
    mime = NULL;
vb@3439
  1985
vb@3439
  1986
    if (!omit_fields) {
vb@3439
  1987
        status = build_fields(msg, &fields);
vb@3439
  1988
        if (status != PEP_STATUS_OK)
vb@3439
  1989
            goto pEp_error;
vb@3439
  1990
vb@3439
  1991
        mailmime_set_imf_fields(msg_mime, fields);
vb@3439
  1992
    }
vb@3439
  1993
vb@3439
  1994
    status = render_mime(msg_mime, &buf);
vb@3439
  1995
    if (status != PEP_STATUS_OK)
vb@3439
  1996
        goto pEp_error;
vb@3439
  1997
vb@3439
  1998
    mailmime_free(msg_mime);
vb@3439
  1999
    *mimetext = buf;
vb@3439
  2000
vb@3439
  2001
    return PEP_STATUS_OK;
vb@3439
  2002
vb@3439
  2003
enomem:
vb@3439
  2004
    status = PEP_OUT_OF_MEMORY;
vb@3439
  2005
vb@3439
  2006
pEp_error:
vb@3439
  2007
    if (msg_mime)
vb@3439
  2008
        mailmime_free(msg_mime);
vb@3439
  2009
    else
vb@3439
  2010
        if (mime)
vb@3439
  2011
            mailmime_free(mime);
vb@3439
  2012
vb@3439
  2013
    return status;
vb@3439
  2014
}
vb@3439
  2015
vb@3439
  2016
static pEp_identity *mailbox_to_identity(const struct mailimf_mailbox * mb)
vb@3439
  2017
{
vb@3439
  2018
    char *username = NULL;
vb@3439
  2019
vb@3439
  2020
    assert(mb);
vb@3439
  2021
    assert(mb->mb_addr_spec);
vb@3439
  2022
vb@3439
  2023
    if (mb->mb_addr_spec == NULL)
vb@3439
  2024
        return NULL;
vb@3439
  2025
vb@3439
  2026
    if (mb->mb_display_name) {
vb@3439
  2027
        size_t index = 0;
vb@3439
  2028
        const int r = mailmime_encoded_phrase_parse("utf-8", mb->mb_display_name,
vb@3439
  2029
                strlen(mb->mb_display_name), &index, "utf-8", &username);
vb@3439
  2030
        if (r)
vb@3439
  2031
            goto enomem;
vb@3439
  2032
    }
vb@3439
  2033
vb@3439
  2034
    pEp_identity *ident = new_identity(mb->mb_addr_spec, NULL, NULL, username);
vb@3439
  2035
    if (ident == NULL)
vb@3439
  2036
        goto enomem;
vb@3439
  2037
    free(username);
vb@3439
  2038
vb@3439
  2039
    return ident;
vb@3439
  2040
vb@3439
  2041
enomem:
vb@3439
  2042
    free(username);
vb@3439
  2043
    return NULL;
vb@3439
  2044
}
vb@3439
  2045
vb@3439
  2046
static pEp_identity * mbl_to_identity(const struct mailimf_mailbox_list * mbl)
vb@3439
  2047
{
vb@3439
  2048
    struct mailimf_mailbox * mb = clist_content(clist_begin(mbl->mb_list));
vb@3439
  2049
    return mailbox_to_identity(mb);
vb@3439
  2050
}
vb@3439
  2051
vb@3439
  2052
static identity_list * mal_to_identity_list(
vb@3439
  2053
        const struct mailimf_address_list *mal
vb@3439
  2054
    )
vb@3439
  2055
{
vb@3439
  2056
    identity_list *il = new_identity_list(NULL);
vb@3439
  2057
    if (il == NULL)
vb@3439
  2058
        goto enomem;
vb@3439
  2059
vb@4537
  2060
    // if we have nothing to translate then return an empty list
vb@4537
  2061
    if (!mal)
vb@4537
  2062
        return il;
vb@4537
  2063
vb@4537
  2064
    clist *list = mal->ad_list;
vb@4537
  2065
vb@3439
  2066
    identity_list *_il = il;
vb@3439
  2067
    for (clistiter *cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@3439
  2068
        pEp_identity *ident;
vb@3439
  2069
vb@3439
  2070
        struct mailimf_address *addr = clist_content(cur);
vb@3439
  2071
        switch(addr->ad_type) {
vb@3439
  2072
            case MAILIMF_ADDRESS_MAILBOX:
vb@3439
  2073
                ident = mailbox_to_identity(addr->ad_data.ad_mailbox);
vb@3439
  2074
                if (ident == NULL)
vb@3439
  2075
                    goto enomem;
vb@3439
  2076
                _il = identity_list_add(_il, ident);
vb@3439
  2077
                if (_il == NULL)
vb@3439
  2078
                    goto enomem;
vb@3439
  2079
                break;
vb@3439
  2080
vb@3439
  2081
            case MAILIMF_ADDRESS_GROUP:
vb@3439
  2082
                {
vb@3439
  2083
                    struct mailimf_mailbox_list * mbl =
vb@3439
  2084
                            addr->ad_data.ad_group->grp_mb_list;
vb@3439
  2085
                    for (clistiter *cur2 = clist_begin(mbl->mb_list); cur2 != NULL;
vb@3439
  2086
                            cur2 = clist_next(cur2)) {
vb@3439
  2087
                        ident = mailbox_to_identity(clist_content(cur));
vb@3439
  2088
                        if (ident == NULL)
vb@3439
  2089
                            goto enomem;
vb@3439
  2090
                        _il = identity_list_add(_il, ident);
vb@3439
  2091
                        if (_il == NULL)
vb@3439
  2092
                            goto enomem;
vb@3439
  2093
                    }
vb@3439
  2094
                }
vb@3439
  2095
                break;
vb@3439
  2096
vb@3439
  2097
            default:
vb@3439
  2098
                assert(0);
vb@3439
  2099
                goto enomem;
vb@3439
  2100
        }
vb@3439
  2101
    }
vb@3439
  2102
vb@3439
  2103
    return il;
vb@3439
  2104
vb@3439
  2105
enomem:
vb@3439
  2106
    free_identity_list(il);
vb@3439
  2107
    return NULL;
vb@3439
  2108
}
vb@3439
  2109
vb@3439
  2110
static stringlist_t * clist_to_stringlist(const clist *list)
vb@3439
  2111
{
vb@3439
  2112
    char *text = NULL;;
vb@3439
  2113
    stringlist_t * sl = new_stringlist(NULL);
vb@3439
  2114
    if (sl == NULL)
vb@3439
  2115
        return NULL;
vb@3439
  2116
vb@3439
  2117
    stringlist_t *_sl = sl;
vb@3439
  2118
    for (clistiter *cur = clist_begin(list); cur != NULL; cur = clist_next(cur)) {
vb@3439
  2119
        char *phrase = clist_content(cur);
vb@3439
  2120
        size_t index = 0;
vb@3439
  2121
        
vb@3439
  2122
        const int r = mailmime_encoded_phrase_parse("utf-8", phrase, strlen(phrase),
vb@3439
  2123
                &index, "utf-8", &text);
vb@3439
  2124
        if (r)
vb@3439
  2125
            goto enomem;
vb@3439
  2126
vb@3439
  2127
        _sl = stringlist_add(_sl, text);
vb@3439
  2128
        if (_sl == NULL)
vb@3439
  2129
            goto enomem;
vb@3439
  2130
vb@3439
  2131
        free(text);
vb@3439
  2132
        text = NULL;
vb@3439
  2133
    }
vb@3439
  2134
vb@3439
  2135
    return sl;
vb@3439
  2136
vb@3439
  2137
enomem:
vb@3439
  2138
    free_stringlist(sl);
vb@3439
  2139
    free(text);
vb@3439
  2140
vb@3439
  2141
    return NULL;
vb@3439
  2142
}
vb@3439
  2143
vb@3439
  2144
static PEP_STATUS read_fields(message *msg, clist *fieldlist)
vb@3439
  2145
{
vb@3439
  2146
    PEP_STATUS status = PEP_STATUS_OK;
vb@3439
  2147
    struct mailimf_field * _field;
vb@3439
  2148
    clistiter *cur;
vb@3439
  2149
    size_t index;
vb@3439
  2150
    int r;
vb@3439
  2151
    
vb@3439
  2152
    stringpair_list_t *opt = msg->opt_fields;
vb@3439
  2153
vb@3439
  2154
    if (!fieldlist)
vb@3439
  2155
        return PEP_STATUS_OK;
vb@3439
  2156
        
vb@3439
  2157
    for (cur = clist_begin(fieldlist); cur != NULL; cur = clist_next(cur)) {
vb@3439
  2158
        _field = clist_content(cur);
vb@3439
  2159
vb@3439
  2160
        switch (_field->fld_type) {
vb@3439
  2161
            case MAILIMF_FIELD_MESSAGE_ID:
vb@3439
  2162
                {
vb@3439
  2163
                    char * text = _field->fld_data.fld_message_id->mid_value;
vb@3439
  2164
vb@3439
  2165
                    free(msg->id);
vb@3439
  2166
                    index = 0;
vb@3439
  2167
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@3439
  2168
                            strlen(text), &index, "utf-8", &msg->id);
vb@3439
  2169
                    if (r)
vb@3439
  2170
                        goto enomem;
vb@3439
  2171
                }
vb@3439
  2172
                break;
vb@3439
  2173
vb@3439
  2174
            case MAILIMF_FIELD_SUBJECT:
vb@3439
  2175
                {
vb@3439
  2176
                    char * text = _field->fld_data.fld_subject->sbj_value;
vb@3439
  2177
vb@3439
  2178
                    free(msg->shortmsg);
vb@3439
  2179
                    index = 0;
vb@3439
  2180
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@3439
  2181
                            strlen(text), &index, "utf-8", &msg->shortmsg);
vb@3439
  2182
                    if (r)
vb@3439
  2183
                        goto enomem;
vb@3439
  2184
                }
vb@3439
  2185
                break;
vb@3439
  2186
vb@3439
  2187
            case MAILIMF_FIELD_ORIG_DATE:
vb@3439
  2188
                {
vb@3439
  2189
                    struct mailimf_date_time *date =
vb@3439
  2190
                        _field->fld_data.fld_orig_date->dt_date_time;
vb@3439
  2191
vb@3439
  2192
                    free_timestamp(msg->sent);
vb@3439
  2193
                    msg->sent = etpantime_to_timestamp(date);
vb@3439
  2194
                    if (msg->sent == NULL)
vb@3439
  2195
                        goto enomem;
vb@3439
  2196
                }
vb@3439
  2197
                break;
vb@3439
  2198
vb@3439
  2199
            case MAILIMF_FIELD_FROM:
vb@3439
  2200
                {
vb@3439
  2201
                    struct mailimf_mailbox_list *mbl =
vb@3439
  2202
                            _field->fld_data.fld_from->frm_mb_list;
vb@3439
  2203
                    pEp_identity *ident;
vb@3439
  2204
vb@3439
  2205
                    ident = mbl_to_identity(mbl);
vb@3439
  2206
                    if (ident == NULL)
vb@3439
  2207
                        goto pEp_error;
vb@3439
  2208
vb@3439
  2209
                    free_identity(msg->from);
vb@3439
  2210
                    msg->from = ident;
vb@3439
  2211
                }
vb@3439
  2212
                break;
vb@3439
  2213
vb@3439
  2214
            case MAILIMF_FIELD_TO:
vb@3439
  2215
                {
vb@3439
  2216
                    struct mailimf_address_list *mal =
vb@3439
  2217
                            _field->fld_data.fld_to->to_addr_list;
vb@3439
  2218
                    identity_list *il = mal_to_identity_list(mal);
vb@3439
  2219
                    if (il == NULL)
vb@3439
  2220
                        goto enomem;
vb@3439
  2221
vb@3439
  2222
                    free_identity_list(msg->to);
vb@3439
  2223
                    msg->to = il;
vb@3439
  2224
                }
vb@3439
  2225
                break;
vb@3439
  2226
vb@3439
  2227
            case MAILIMF_FIELD_CC:
vb@3439
  2228
                {
vb@3439
  2229
                    struct mailimf_address_list *mal =
vb@3439
  2230
                            _field->fld_data.fld_cc->cc_addr_list;
vb@3439
  2231
                    identity_list *il = mal_to_identity_list(mal);
vb@3439
  2232
                    if (il == NULL)
vb@3439
  2233
                        goto enomem;
vb@3439
  2234
vb@3439
  2235
                    free_identity_list(msg->cc);
vb@3439
  2236
                    msg->cc = il;
vb@3439
  2237
                }
vb@3439
  2238
                break;
vb@3439
  2239
vb@3439
  2240
            case MAILIMF_FIELD_BCC:
vb@3439
  2241
                {
vb@3439
  2242
                    struct mailimf_address_list *mal =
vb@3439
  2243
                            _field->fld_data.fld_bcc->bcc_addr_list;
vb@3439
  2244
                    identity_list *il = mal_to_identity_list(mal);
vb@3439
  2245
                    if (il == NULL)
vb@3439
  2246
                        goto enomem;
vb@3439
  2247
vb@3439
  2248
                    free_identity_list(msg->bcc);
vb@3439
  2249
                    msg->bcc = il;
vb@3439
  2250
                }
vb@3439
  2251
                break;
vb@3439
  2252
vb@3439
  2253
            case MAILIMF_FIELD_REPLY_TO:
vb@3439
  2254
                {
vb@3439
  2255
                    struct mailimf_address_list *mal =
vb@3439
  2256
                            _field->fld_data.fld_reply_to->rt_addr_list;
vb@3439
  2257
                    identity_list *il = mal_to_identity_list(mal);
vb@3439
  2258
                    if (il == NULL)
vb@3439
  2259
                        goto enomem;
vb@3439
  2260
vb@3439
  2261
                    free_identity_list(msg->reply_to);
vb@3439
  2262
                    msg->reply_to = il;
vb@3439
  2263
                }
vb@3439
  2264
                break;
vb@3439
  2265
vb@3439
  2266
            case MAILIMF_FIELD_IN_REPLY_TO:
vb@3439
  2267
                {
vb@3439
  2268
                    clist *list = _field->fld_data.fld_in_reply_to->mid_list;
vb@3439
  2269
                    stringlist_t *sl = clist_to_stringlist(list);
vb@3439
  2270
                    if (sl == NULL)
vb@3439
  2271
                        goto enomem;
vb@3439
  2272
vb@3439
  2273
                    free_stringlist(msg->in_reply_to);
vb@3439
  2274
                    msg->in_reply_to = sl;
vb@3439
  2275
                }
vb@3439
  2276
                break;
vb@3439
  2277
vb@3439
  2278
            case MAILIMF_FIELD_REFERENCES:
vb@3439
  2279
                {
vb@3439
  2280
                    clist *list = _field->fld_data.fld_references->mid_list;
vb@3439
  2281
                    stringlist_t *sl = clist_to_stringlist(list);
vb@3439
  2282
                    if (sl == NULL)
vb@3439
  2283
                        goto enomem;
vb@3439
  2284
vb@3439
  2285
                    free_stringlist(msg->references);
vb@3439
  2286
                    msg->references = sl;
vb@3439
  2287
                }
vb@3439
  2288
                break;
vb@3439
  2289
vb@3439
  2290
            case MAILIMF_FIELD_KEYWORDS:
vb@3439
  2291
                {
vb@3439
  2292
                    clist *list = _field->fld_data.fld_keywords->kw_list;
vb@3439
  2293
                    stringlist_t *sl = clist_to_stringlist(list);
vb@3439
  2294
                    if (sl == NULL)
vb@3439
  2295
                        goto enomem;
vb@3439
  2296
vb@3439
  2297
                    free_stringlist(msg->keywords);
vb@3439
  2298
                    msg->keywords = sl;
vb@3439
  2299
                }
vb@3439
  2300
                break;
vb@3439
  2301
vb@3439
  2302
            case MAILIMF_FIELD_COMMENTS:
vb@3439
  2303
                {
vb@3439
  2304
                    char * text = _field->fld_data.fld_comments->cm_value;
vb@3439
  2305
vb@3439
  2306
                    free(msg->comments);
vb@3439
  2307
                    index = 0;
vb@3439
  2308
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@3439
  2309
                            strlen(text), &index, "utf-8", &msg->comments);
vb@3439
  2310
                    if (r)
vb@3439
  2311
                        goto enomem;
vb@3439
  2312
                }
vb@3439
  2313
                break;
vb@3439
  2314
vb@3439
  2315
            case MAILIMF_FIELD_OPTIONAL_FIELD:
vb@3439
  2316
                {
vb@3439
  2317
                    char * name =
vb@3439
  2318
                            _field->fld_data.fld_optional_field->fld_name;
vb@3439
  2319
                    char * value =
vb@3439
  2320
                            _field->fld_data.fld_optional_field->fld_value;
vb@3439
  2321
                    char *_value;
vb@3439
  2322
vb@3439
  2323
                    index = 0;
vb@3439
  2324
                    r = mailmime_encoded_phrase_parse("utf-8", value,
vb@3439
  2325
                            strlen(value), &index, "utf-8", &_value);
vb@3439
  2326
                    if (r)
vb@3439
  2327
                        goto enomem;
vb@3439
  2328
vb@3439
  2329
                    stringpair_t *pair = new_stringpair(name, _value);
vb@3439
  2330
                    if (pair == NULL)
vb@3439
  2331
                        goto enomem;
vb@3439
  2332
vb@3439
  2333
                    opt = stringpair_list_add(opt, pair);
vb@3439
  2334
                    free(_value);
vb@3439
  2335
                    if (opt == NULL)
vb@3439
  2336
                        goto enomem;
vb@3439
  2337
vb@3439
  2338
                    if (msg->opt_fields == NULL)
vb@3439
  2339
                        msg->opt_fields = opt;
vb@3439
  2340
                }
vb@3439
  2341
                break;
vb@3439
  2342
        }
vb@3439
  2343
    }
vb@3439
  2344
    
vb@3439
  2345
    return PEP_STATUS_OK;
vb@3439
  2346
vb@3439
  2347
enomem:
vb@3439
  2348
    status = PEP_OUT_OF_MEMORY;
vb@3439
  2349
vb@3439
  2350
pEp_error:
vb@3439
  2351
    return status;
vb@3439
  2352
}
vb@3439
  2353
vb@3439
  2354
static PEP_STATUS interpret_body(struct mailmime *part, char **longmsg, size_t *size)
vb@3439
  2355
{
vb@3439
  2356
    const char *text;
vb@3439
  2357
    char *_longmsg;
vb@3439
  2358
    size_t length;
vb@3439
  2359
    size_t _size;
vb@3439
  2360
    size_t index;
vb@3439
  2361
    char *type = NULL;
vb@3439
  2362
    char *charset = NULL;
vb@3439
  2363
vb@3439
  2364
    assert(part);
vb@3439
  2365
    assert(longmsg);
vb@3439
  2366
vb@3439
  2367
    *longmsg = NULL;
vb@3439
  2368
    if (size)
vb@3439
  2369
        *size = 0;
vb@3439
  2370
vb@3439
  2371
    if (part->mm_body == NULL)
vb@3439
  2372
        return PEP_ILLEGAL_VALUE;
vb@3439
  2373
vb@3439
  2374
    text = part->mm_body-> dt_data.dt_text.dt_data;
vb@3439
  2375
    if (text == NULL)
vb@3439
  2376
        return PEP_ILLEGAL_VALUE;
vb@3439
  2377
vb@3439
  2378
    length = part->mm_body->dt_data.dt_text.dt_length;
vb@3439
  2379
vb@3439
  2380
    if (part->mm_body->dt_encoded) {
vb@3439
  2381
        int code = part->mm_body->dt_encoding;
vb@3439
  2382
        index = 0;
vb@3439
  2383
        int r = mailmime_part_parse(text, length, &index, code, &_longmsg, &_size);
vb@3439
  2384
        switch (r) {
vb@3439
  2385
            case MAILIMF_NO_ERROR:
vb@3439
  2386
                break;
vb@3439
  2387
            case MAILIMF_ERROR_MEMORY:
vb@3439
  2388
                return PEP_OUT_OF_MEMORY;
vb@3439
  2389
            default:
vb@3439
  2390
                return PEP_ILLEGAL_VALUE;
vb@3439
  2391
        }
vb@3439
  2392
    }
vb@3439
  2393
    else {
vb@3439
  2394
        _size = length + 1;
vb@3439
  2395
        _longmsg = strndup(text, length);
vb@3439
  2396
        if (_longmsg == NULL)
vb@3439
  2397
            return PEP_OUT_OF_MEMORY;
vb@3439
  2398
    }
vb@3439
  2399
vb@3439
  2400
    if (part->mm_content_type) {
vb@3439
  2401
        if (_get_content_type(part->mm_content_type, &type, &charset) == 0) {
vb@3439
  2402
            if (charset && strncasecmp(charset, "utf-8", 5) != 0) {
vb@3439
  2403
                char * _text;
vb@3439
  2404
                int r = charconv("utf-8", charset, _longmsg, _size, &_text);
vb@3439
  2405
                switch (r) {
vb@3439
  2406
                    case MAILIMF_NO_ERROR:
vb@3439
  2407
                        break;
vb@3439
  2408
                    case MAILIMF_ERROR_MEMORY:
vb@3439
  2409
                        return PEP_OUT_OF_MEMORY;
vb@3439
  2410
                    default:
vb@3439
  2411
                        return PEP_ILLEGAL_VALUE;
vb@3439
  2412
                }
vb@3439
  2413
                free(_longmsg);
vb@3439
  2414
                _longmsg = _text;
vb@3439
  2415
            }
vb@3439
  2416
        }
vb@3439
  2417
    }
vb@3439
  2418
    // FIXME: KG - we now have the text we want.
vb@3439
  2419
    // Now we need to strip sigs and process them if they are there..
vb@3439
  2420
    
vb@3439
  2421
vb@3439
  2422
    *longmsg = _longmsg;
vb@3439
  2423
    if (size)
vb@3439
  2424
        *size = _size;
vb@3439
  2425
vb@3439
  2426
    return PEP_STATUS_OK;
vb@3439
  2427
}
vb@3439
  2428
vb@3439
  2429
// THIS IS A BEST-EFFORT ONLY FUNCTION, AND WE ARE NOT DOING MORE THAN THE
vb@3439
  2430
// SUBJECT FOR NOW.
vb@3439
  2431
static PEP_STATUS interpret_protected_headers(
vb@3439
  2432
        struct mailmime* mime, 
vb@3439
  2433
        message* msg
vb@3439
  2434
    )
vb@3439
  2435
{
vb@3439
  2436
    // N.B. this is *very much* enigmail output specific, and right now,
vb@3439
  2437
    // we only care about subject replacement.
vb@3439
  2438
    const char* header_string = "Content-Type: text/rfc822-headers; protected-headers=\"v1\"\nContent-Disposition: inline\n\n";
vb@3439
  2439
    size_t content_length = mime->mm_length;
vb@3439
  2440
    size_t header_strlen = strlen(header_string);
vb@3439
  2441
    if (header_strlen < content_length) {
vb@3439
  2442
        const char* headerblock = mime->mm_mime_start;
vb@3439
  2443
        size_t subject_len = 0;
vb@3439
  2444
        headerblock = strstr(headerblock, header_string);
vb@3439
  2445
        if (headerblock) {
vb@3439
  2446
            const char* subj_start = "Subject: ";
vb@3439
  2447
            headerblock = strstr(headerblock, subj_start);
vb@3439
  2448
            if (headerblock) {
vb@3439
  2449
                size_t subj_len = strlen(subj_start);
vb@3439
  2450
                headerblock += subj_len;
vb@3439
  2451
                char* end_pt = strstr(headerblock, "\n");
vb@3439
  2452
                if (end_pt) {
vb@3439
  2453
                    if (end_pt != mime->mm_mime_start && *(end_pt - 1) == '\r')
vb@3439
  2454
                        end_pt--;
vb@3439
  2455
                    subject_len = end_pt - headerblock;
vb@3439
  2456
                    char* new_subj = (char*)calloc(subject_len + 1, 1);
vb@3439
  2457
                    if (new_subj) {
vb@3439
  2458
                        strlcpy(new_subj, headerblock, subject_len + 1);
vb@3439
  2459
                        free(msg->shortmsg);
vb@3439
  2460
                        msg->shortmsg = new_subj;
vb@3439
  2461
                    }    
vb@3439
  2462
                } // if there's no endpoint, there's something wrong here so we ignore all
vb@3439
  2463
                  // This is best effort.
vb@3439
  2464
            }
vb@3439
  2465
        }
vb@3439
  2466
    }
vb@3439
  2467
    return PEP_STATUS_OK;
vb@3439
  2468
}
vb@3439
  2469
vb@3439
  2470
// ONLY for main part!!!
vb@3439
  2471
static PEP_STATUS process_multipart_related(struct mailmime *mime,
vb@3439
  2472
                                            message *msg) {
vb@3439
  2473
    PEP_STATUS status = PEP_STATUS_OK;
vb@3439
  2474
vb@3439
  2475
    assert(mime);
vb@3439
  2476
    assert(msg);
vb@3439
  2477
vb@3439
  2478
    clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;                                                
vb@3439
  2479
vb@3439
  2480
    if (partlist == NULL)
vb@3439
  2481
        return PEP_ILLEGAL_VALUE;
vb@3439
  2482
vb@3439
  2483
    clistiter *cur = clist_begin(partlist);
vb@3439
  2484
    struct mailmime *part = clist_content(cur);
vb@3439
  2485
    
vb@3439
  2486
    if (part == NULL)
vb@3439
  2487
        return PEP_ILLEGAL_VALUE;
vb@3439
  2488
vb@3439
  2489
    struct mailmime_content *content = part->mm_content_type;    
vb@3439
  2490
    assert(content);
vb@3439
  2491
    
vb@3439
  2492
    if (content == NULL)
vb@3439
  2493
        return PEP_ILLEGAL_VALUE;
vb@3439
  2494
vb@3439
  2495
    if (_is_text_part(content, "html")) {
vb@3439
  2496
        status = interpret_body(part, &msg->longmsg_formatted,
vb@3439
  2497
                NULL);
vb@3439
  2498
        if (status)
vb@3439
  2499
            return status;
vb@3439
  2500
    }
vb@3439
  2501
    else {
vb@3439
  2502
        // ???
vb@3439
  2503
        // This is what we would have done before, so... no
vb@3439
  2504
        // worse than the status quo. But FIXME!
krista@3710
  2505
        status = interpret_MIME(part, msg, NULL);
vb@3439
  2506
        if (status)
vb@3439
  2507
            return status;
vb@3439
  2508
    }
vb@3439
  2509
    
vb@3439
  2510
    for (cur = clist_next(cur); cur; cur = clist_next(cur)) {
vb@3439
  2511
        part = clist_content(cur);
vb@3439
  2512
        if (part == NULL)
vb@3439
  2513
            return PEP_ILLEGAL_VALUE;
vb@3439
  2514
vb@3439
  2515
        content = part->mm_content_type;
vb@3439
  2516
        assert(content);
vb@3439
  2517
        if (content == NULL)
vb@3439
  2518
            return PEP_ILLEGAL_VALUE;
vb@3439
  2519
krista@3710
  2520
        status = interpret_MIME(part, msg, NULL);
vb@3439
  2521
        if (status)
vb@3439
  2522
            return status;
vb@3439
  2523
    }
vb@3439
  2524
    return status;
vb@3439
  2525
}
vb@3439
  2526
vb@3439
  2527
static PEP_STATUS interpret_MIME(
vb@3439
  2528
        struct mailmime *mime,
krista@3710
  2529
        message *msg,
krista@3710
  2530
        bool* raise_msg_attachment
vb@3439
  2531
    )
vb@3439
  2532
{
vb@3439
  2533
    PEP_STATUS status = PEP_STATUS_OK;
vb@3439
  2534
vb@3439
  2535
    assert(mime);
vb@3439
  2536
    assert(msg);
vb@3439
  2537
vb@3439
  2538
    struct mailmime_content *content = mime->mm_content_type;
vb@3439
  2539
    if (content) {
vb@3439
  2540
        if (_is_multipart(content, "alternative")) {
vb@3439
  2541
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@3439
  2542
            if (partlist == NULL)
vb@3439
  2543
                return PEP_ILLEGAL_VALUE;
vb@3439
  2544
vb@3439
  2545
            clistiter *cur;
vb@3439
  2546
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@3439
  2547
                struct mailmime *part = clist_content(cur);
vb@3439
  2548
                if (part == NULL)
vb@3439
  2549
                    return PEP_ILLEGAL_VALUE;
vb@3439
  2550
vb@3439
  2551
                content = part->mm_content_type;
vb@3439
  2552
                assert(content);
vb@3439
  2553
                if (content == NULL)
vb@3439
  2554
                    return PEP_ILLEGAL_VALUE;
vb@3439
  2555
vb@3439
  2556
                if (_is_text_part(content, "plain") && msg->longmsg == NULL) {
vb@3439
  2557
                    status = interpret_body(part, &msg->longmsg, NULL);
vb@3439
  2558
                    if (status)
vb@3439
  2559
                        return status;
vb@3439
  2560
                }
vb@3439
  2561
                else if (_is_text_part(content, "rfc822-headers")) {
vb@3439
  2562
                    status = interpret_protected_headers(part, msg);
vb@3439
  2563
                    if (status)
vb@3439
  2564
                        return status;
vb@3439
  2565
                }
vb@3439
  2566
                else if (_is_text_part(content, "html") &&
vb@3439
  2567
                        msg->longmsg_formatted == NULL) {
vb@3439
  2568
                    status = interpret_body(part, &msg->longmsg_formatted,
vb@3439
  2569
                            NULL);
vb@3439
  2570
                    if (status)
vb@3439
  2571
                        return status;
vb@3439
  2572
                }
vb@3439
  2573
                else if (_is_multipart(content, "related") && 
vb@3439
  2574
                    msg->longmsg_formatted == NULL) {
vb@3439
  2575
                    status = process_multipart_related(part, msg);
vb@3439
  2576
                    if (status)
vb@3439
  2577
                        return status;
vb@3439
  2578
                }
vb@3439
  2579
                else /* add as attachment */ {
krista@3710
  2580
                    status = interpret_MIME(part, msg, NULL);
vb@3439
  2581
                    if (status)
vb@3439
  2582
                        return status;
vb@3439
  2583
                }
vb@3439
  2584
            }
vb@3439
  2585
        }
vb@3439
  2586
        else if (_is_multipart(content, "encrypted")) {
vb@3439
  2587
            if (msg->longmsg == NULL)
vb@3439
  2588
                msg->longmsg = strdup("");
vb@3439
  2589
            assert(msg->longmsg);
vb@3439
  2590
            if (!msg->longmsg)
vb@3439
  2591
                return PEP_OUT_OF_MEMORY;
vb@3439
  2592
vb@3439
  2593
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@3439
  2594
            if (partlist == NULL)
vb@3439
  2595
                return PEP_ILLEGAL_VALUE;
vb@3439
  2596
vb@3439
  2597
            clistiter *cur;
vb@3439
  2598
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@3439
  2599
                struct mailmime *part= clist_content(cur);
vb@3439
  2600
                if (part == NULL)
vb@3439
  2601
                    return PEP_ILLEGAL_VALUE;
vb@3439
  2602
krista@3710
  2603
                status = interpret_MIME(part, msg, NULL);
vb@3439
  2604
                if (status != PEP_STATUS_OK)
vb@3439
  2605
                    return status;
vb@3439
  2606
            }
vb@3439
  2607
        }
vb@3439
  2608
        else if (_is_multipart(content, NULL)) {
vb@3439
  2609
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@3439
  2610
            if (partlist == NULL)
vb@3439
  2611
                return PEP_ILLEGAL_VALUE;
vb@3439
  2612
vb@3439
  2613
            clistiter *cur;
krista@3710
  2614
            // only add raise_msg_attachment on 2nd part!
krista@3710
  2615
            int _att_count = 0;
krista@3710
  2616
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur), _att_count++) {
vb@3439
  2617
                struct mailmime *part= clist_content(cur);
vb@3439
  2618
                if (part == NULL)
vb@3439
  2619
                    return PEP_ILLEGAL_VALUE;
krista@3710
  2620
                status = interpret_MIME(part, msg, _att_count == 1 ? raise_msg_attachment : NULL);
vb@3439
  2621
                if (status != PEP_STATUS_OK)
vb@3439
  2622
                    return status;
vb@3439
  2623
            }
vb@3439
  2624
        }
vb@3439
  2625
        else {
vb@3439
  2626
            if (_is_text_part(content, "html") &&
vb@3439
  2627
                msg->longmsg_formatted == NULL &&
vb@3439
  2628
                msg->longmsg == NULL) {
vb@3439
  2629
                status = interpret_body(mime, &msg->longmsg_formatted,
vb@3439
  2630
                                        NULL);
vb@3439
  2631
                if (status)
vb@3439
  2632
                    return status;
vb@3439
  2633
            }
vb@3439
  2634
            else if (_is_text_part(content, "rfc822-headers")) {
vb@3439
  2635
                status = interpret_protected_headers(mime, msg);
vb@3439
  2636
                if (status)
vb@3439
  2637
                    return status;
vb@3439
  2638
            }
vb@3439
  2639
            else if (_is_text_part(content, "plain") && 
vb@3439
  2640
                     msg->longmsg == NULL && msg->longmsg_formatted == NULL) {
vb@3439
  2641
                status = interpret_body(mime, &msg->longmsg, NULL);
vb@3439
  2642
                if (status)
vb@3439
  2643
                    return status;
vb@3439
  2644
            }            
vb@3439
  2645
            else if (_is_text_part(content, NULL) &&