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