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