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