src/mime.c
author Krista Bennett <krista@pep-project.org>
Wed, 25 Oct 2017 10:29:16 +0200
branchENGINE-298
changeset 2200 644d22d740f0
parent 2196 e680ca5b914b
child 2275 de17b80d7394
permissions -rw-r--r--
ENGINE-298: transport encoding should only take place when the message is at the last stage before outbound return. Added a transport_encode flag to several of the MIME-tree building functions, as libetpan does not distinguish between building the MIME tree and encoding its parts.
vb@1513
     1
// This file is under GNU General Public License 3.0
vb@1513
     2
// see LICENSE.txt
vb@1513
     3
vb@126
     4
#include "pEp_internal.h"
vb@48
     5
#include "mime.h"
vb@48
     6
vb@48
     7
#include <string.h>
vb@48
     8
#include <stdlib.h>
vb@48
     9
#include <assert.h>
vb@48
    10
#include <errno.h>
vb@130
    11
krista@1878
    12
#include "resource_id.h"
vb@48
    13
#include "etpan_mime.h"
vb@63
    14
#include "wrappers.h"
vb@48
    15
vb@309
    16
static bool is_whitespace(char c)
vb@308
    17
{
vb@308
    18
    switch (c) {
vb@308
    19
        case ' ':
vb@308
    20
        case '\t':
vb@308
    21
        case '\r':
vb@308
    22
        case '\n':
vb@308
    23
            return true;
vb@308
    24
vb@308
    25
        default:
vb@308
    26
            return false;
vb@308
    27
    }
vb@308
    28
}
vb@308
    29
vb@117
    30
DYNAMIC_API bool is_PGP_message_text(const char *text)
vb@117
    31
{
vb@117
    32
    if (text == NULL)
vb@117
    33
        return false;
vb@117
    34
vb@309
    35
    for (; *text && is_whitespace(*text); text++);
vb@308
    36
vb@117
    37
    return strncmp(text, "-----BEGIN PGP MESSAGE-----", 27) == 0;
vb@117
    38
}
vb@117
    39
Edouard@373
    40
#define TMP_TEMPLATE "pEp.XXXXXXXXXXXXXXXXXXXX"
Edouard@373
    41
#ifdef _WIN32
Edouard@373
    42
#define PATH_SEP '\\'
Edouard@373
    43
#else
Edouard@373
    44
#define PATH_SEP '/'
Edouard@373
    45
#endif
Edouard@373
    46
markus@1042
    47
// This function was rewritten to use in-memory buffers instead of
markus@1042
    48
// temporary files when the pgp/mime support was implemented for
markus@1042
    49
// outlook, as the existing code did not work well on windows.
markus@1042
    50
vb@89
    51
static PEP_STATUS render_mime(struct mailmime *mime, char **mimetext)
vb@48
    52
{
vb@89
    53
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
    54
    int col;
vb@89
    55
    int r;
markus@1042
    56
	size_t len;
markus@1042
    57
	char* buf = NULL;
Edouard@373
    58
markus@1042
    59
	MMAPString* buffer;
Edouard@373
    60
markus@1042
    61
	buffer = mmap_string_new(NULL);
markus@1042
    62
	if (buffer == NULL)
markus@1042
    63
		goto enomem;
markus@1042
    64
	
markus@1042
    65
	col = 0;
markus@1042
    66
	r = mailmime_write_mem(buffer, &col, mime);
markus@1042
    67
	assert(r == MAILIMF_NO_ERROR);
markus@1042
    68
	if (r == MAILIMF_ERROR_MEMORY)
markus@1042
    69
		goto enomem;
markus@1042
    70
	else if (r != MAILIMF_NO_ERROR)
markus@1042
    71
		goto err_file;
Edouard@373
    72
markus@1042
    73
	// we overallocate by 1 byte, so we have a terminating 0.
markus@1042
    74
	len = buffer->len;
markus@1042
    75
	buf = calloc(len + 1, 1);
markus@1042
    76
	if (buf == NULL)
markus@1042
    77
		goto enomem;
vb@48
    78
markus@1042
    79
	memcpy(buf, buffer->str, len);
markus@1042
    80
	mmap_string_free(buffer);
vb@62
    81
vb@87
    82
    *mimetext = buf;
vb@48
    83
    return PEP_STATUS_OK;
vb@48
    84
vb@48
    85
err_file:
vb@88
    86
    status = PEP_CANNOT_CREATE_TEMP_FILE;
vb@88
    87
    goto pep_error;
vb@48
    88
vb@48
    89
enomem:
vb@88
    90
    status = PEP_OUT_OF_MEMORY;
vb@52
    91
vb@88
    92
pep_error:
markus@1042
    93
	if (buffer)
markus@1042
    94
		mmap_string_free(buffer);
markus@1042
    95
	if (buf)
markus@1042
    96
		free(buf);
vb@89
    97
    return status;
vb@89
    98
}
vb@89
    99
vb@88
   100
vb@89
   101
static PEP_STATUS mime_attachment(
vb@89
   102
        bloblist_t *blob,
krista@2200
   103
        struct mailmime **result,
krista@2200
   104
        bool transport_encode
vb@88
   105
    )
vb@88
   106
{
vb@88
   107
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
   108
    struct mailmime * mime = NULL;
vb@89
   109
    char * mime_type;
vb@89
   110
    assert(blob);
vb@89
   111
    assert(result);
vb@88
   112
vb@89
   113
    *result = NULL;
vb@88
   114
markus@1042
   115
// TODO: It seems the pep com server adapter sends an empty string here,
markus@1042
   116
// which leads to a crash later. Thus, we workaround here by treating an
markus@1042
   117
// empty string as NULL. We need to check whether the bug really is here,
markus@1042
   118
// or the pep com server adapter needs to be changed.
markus@1042
   119
    if (blob->mime_type == NULL || blob->mime_type[0] == '\0')
vb@89
   120
        mime_type = "application/octet-stream";
vb@89
   121
    else
vb@89
   122
        mime_type = blob->mime_type;
vb@89
   123
krista@1878
   124
    pEp_rid_list_t* resource = parse_uri(blob->filename);
krista@2200
   125
    mime = get_file_part(resource, mime_type, blob->value, blob->size, transport_encode);
krista@1878
   126
    free_rid_list(resource);
krista@1878
   127
    
vb@89
   128
    assert(mime);
vb@89
   129
    if (mime == NULL)
vb@89
   130
        goto enomem;
vb@89
   131
vb@89
   132
    *result = mime;
vb@89
   133
    return PEP_STATUS_OK;
vb@88
   134
vb@88
   135
enomem:
vb@88
   136
    status = PEP_OUT_OF_MEMORY;
vb@88
   137
vb@89
   138
    if (mime)
vb@89
   139
        mailmime_free(mime);
vb@89
   140
vb@88
   141
    return status;
vb@88
   142
}
vb@88
   143
krista@2011
   144
static PEP_STATUS mime_html_text(
krista@2011
   145
        const char *plaintext,
krista@2011
   146
        const char *htmltext,
krista@2036
   147
        bloblist_t *attachments,
krista@2200
   148
        struct mailmime **result,
krista@2200
   149
        bool transport_encode
krista@2011
   150
    )
krista@2011
   151
{
krista@2011
   152
    PEP_STATUS status = PEP_STATUS_OK;
krista@2011
   153
    struct mailmime * top_level_html_mime = NULL;
krista@2011
   154
    struct mailmime * mime = NULL;
krista@2011
   155
    struct mailmime * submime = NULL;
krista@2011
   156
    int r;
krista@2011
   157
krista@2011
   158
    assert(plaintext);
krista@2011
   159
    assert(htmltext);
krista@2011
   160
    assert(result);
krista@2011
   161
krista@2011
   162
    *result = NULL;
krista@2011
   163
krista@2011
   164
    mime = part_multiple_new("multipart/alternative");
krista@2011
   165
    assert(mime);
krista@2011
   166
    if (mime == NULL)
krista@2011
   167
        goto enomem;
krista@2011
   168
krista@2011
   169
    pEp_rid_list_t* resource = new_rid_node(PEP_RID_FILENAME, "msg.txt");
krista@2011
   170
    
krista@2200
   171
    int encoding_type = (transport_encode ? MAILMIME_MECHANISM_QUOTED_PRINTABLE : 0);
krista@2012
   172
    submime = get_text_part(NULL, "text/plain", plaintext, strlen(plaintext),
krista@2200
   173
            encoding_type);
krista@2011
   174
    free_rid_list(resource);
krista@2011
   175
    resource = NULL;
krista@2011
   176
    
krista@2011
   177
    assert(submime);
krista@2011
   178
    if (submime == NULL)
krista@2011
   179
        goto enomem;
krista@2011
   180
krista@2011
   181
    r = mailmime_smart_add_part(mime, submime);
krista@2011
   182
    assert(r == MAILIMF_NO_ERROR);
krista@2011
   183
    if (r == MAILIMF_ERROR_MEMORY) {
krista@2011
   184
        goto enomem;
krista@2011
   185
    }
krista@2011
   186
    else {
krista@2011
   187
        // mailmime_smart_add_part() takes ownership of submime
krista@2011
   188
        submime = NULL;
krista@2011
   189
    }
krista@2011
   190
krista@2036
   191
    bool inlined_attachments = false;
krista@2036
   192
    
krista@2036
   193
    bloblist_t* traversal_ptr = attachments;
krista@2036
   194
    
krista@2036
   195
    while (traversal_ptr) {
krista@2036
   196
        if (traversal_ptr->disposition == PEP_CONTENT_DISP_INLINE) {
krista@2036
   197
            inlined_attachments = true;
krista@2036
   198
            break;
krista@2036
   199
        }
krista@2036
   200
        traversal_ptr = traversal_ptr->next;
krista@2036
   201
    }
krista@2011
   202
krista@2011
   203
    if (inlined_attachments) {
krista@2011
   204
        /* Noooooo... dirk, why do you do this to me? */
krista@2011
   205
        submime = part_multiple_new("multipart/related");
krista@2011
   206
        assert(submime);
krista@2011
   207
        if (submime == NULL)
krista@2011
   208
            goto enomem;
krista@2011
   209
krista@2011
   210
        top_level_html_mime = submime;
krista@2011
   211
        
krista@2011
   212
        r = mailmime_smart_add_part(mime, top_level_html_mime);
krista@2011
   213
        assert(r == MAILIMF_NO_ERROR);
krista@2011
   214
        if (r == MAILIMF_ERROR_MEMORY) {
krista@2011
   215
            goto enomem;
krista@2011
   216
        }
krista@2011
   217
        else {
krista@2011
   218
            // mailmime_smart_add_part() takes ownership of submime
krista@2011
   219
            submime = NULL;
krista@2011
   220
        }
krista@2011
   221
    }
krista@2011
   222
    else {
krista@2011
   223
        top_level_html_mime = mime;
krista@2011
   224
    }
krista@2011
   225
krista@2012
   226
//    resource = new_rid_node(PEP_RID_FILENAME, "msg.html");
krista@2012
   227
    submime = get_text_part(NULL, "text/html", htmltext, strlen(htmltext),
krista@2200
   228
            encoding_type);
krista@2011
   229
    free_rid_list(resource);
krista@2011
   230
    resource = NULL;
krista@2011
   231
    
krista@2011
   232
    assert(submime);
krista@2011
   233
    if (submime == NULL)
krista@2011
   234
        goto enomem;
krista@2011
   235
krista@2011
   236
    r = mailmime_smart_add_part(top_level_html_mime, submime);
krista@2011
   237
    assert(r == MAILIMF_NO_ERROR);
krista@2011
   238
    if (r == MAILIMF_ERROR_MEMORY)
krista@2011
   239
        goto enomem;
krista@2011
   240
    else {
krista@2011
   241
        // mailmime_smart_add_part() takes ownership of submime
krista@2011
   242
        submime = NULL;
krista@2011
   243
    }
krista@2011
   244
krista@2011
   245
    bloblist_t *_a;
krista@2036
   246
    for (_a = attachments; _a != NULL; _a = _a->next) {
krista@2036
   247
        if (_a->disposition != PEP_CONTENT_DISP_INLINE)
krista@2036
   248
            continue;
krista@2200
   249
        status = mime_attachment(_a, &submime, transport_encode);
krista@2011
   250
        if (status != PEP_STATUS_OK)
krista@2011
   251
            return PEP_UNKNOWN_ERROR; // FIXME
krista@2011
   252
krista@2011
   253
        r = mailmime_smart_add_part(top_level_html_mime, submime);
krista@2011
   254
        assert(r == MAILIMF_NO_ERROR);
krista@2011
   255
        if (r == MAILIMF_ERROR_MEMORY) {
krista@2011
   256
            goto enomem;
krista@2011
   257
        }
krista@2011
   258
        else {
krista@2011
   259
            // mailmime_smart_add_part() takes ownership of submime
krista@2011
   260
            submime = NULL;
krista@2011
   261
        }
krista@2011
   262
    }
krista@2011
   263
krista@2011
   264
    *result = mime;
krista@2011
   265
    return PEP_STATUS_OK;
krista@2011
   266
krista@2011
   267
enomem:
krista@2011
   268
    status = PEP_OUT_OF_MEMORY;
krista@2011
   269
krista@2011
   270
    if (mime)
krista@2011
   271
        mailmime_free(mime);
krista@2011
   272
krista@2011
   273
    if (submime)
krista@2011
   274
        mailmime_free(submime);
krista@2011
   275
krista@2011
   276
    return status;
krista@2011
   277
}
krista@2011
   278
krista@2200
   279
krista@2200
   280
// FIXME: maybe need to add transport_encode field here
vb@94
   281
static struct mailimf_mailbox * identity_to_mailbox(const pEp_identity *ident)
vb@94
   282
{
vb@94
   283
    char *_username = NULL;
vb@94
   284
    struct mailimf_mailbox *mb;
vb@94
   285
vb@977
   286
    _username = ident->username ? mailmime_encode_subject_header("utf-8",
vb@977
   287
            ident->username, 0) : strdup("");
vb@94
   288
    if (_username == NULL)
vb@94
   289
        goto enomem;
vb@94
   290
vb@94
   291
    mb = mailbox_from_string(_username, ident->address);
vb@94
   292
    if (mb == NULL)
vb@94
   293
        goto enomem;
vb@94
   294
vb@94
   295
    free(_username);
vb@94
   296
    _username = NULL;
vb@94
   297
vb@94
   298
    return mb;
vb@94
   299
vb@94
   300
enomem:
vb@94
   301
    free(_username);
vb@94
   302
    return NULL;
vb@94
   303
}
vb@94
   304
vb@94
   305
static struct mailimf_mailbox_list * identity_to_mbl(
vb@94
   306
        const pEp_identity *ident)
vb@90
   307
{
vb@90
   308
    struct mailimf_mailbox_list *mbl = NULL;
vb@90
   309
    struct mailimf_mailbox *mb = NULL;
vb@90
   310
    clist *list = NULL;
vb@90
   311
    int r;
vb@90
   312
vb@90
   313
    assert(ident);
vb@90
   314
vb@90
   315
    list = clist_new();
vb@90
   316
    if (list == NULL)
vb@90
   317
        goto enomem;
vb@90
   318
vb@94
   319
    mb = identity_to_mailbox(ident);
vb@90
   320
    if (mb == NULL)
vb@90
   321
        goto enomem;
vb@90
   322
vb@90
   323
    r = clist_append(list, mb);
vb@90
   324
    if (r)
vb@90
   325
        goto enomem;
vb@90
   326
vb@90
   327
    mbl = mailimf_mailbox_list_new(list);
vb@90
   328
    if (mbl == NULL)
vb@90
   329
        goto enomem;
vb@90
   330
vb@90
   331
    return mbl;
vb@90
   332
vb@90
   333
enomem:
vb@90
   334
    if (mb)
vb@90
   335
        mailimf_mailbox_free(mb);
vb@90
   336
vb@90
   337
    if (list)
vb@90
   338
        clist_free(list);
vb@90
   339
vb@90
   340
    return NULL;
vb@90
   341
}
vb@90
   342
vb@94
   343
static struct mailimf_address_list * identity_list_to_mal(identity_list *il)
vb@90
   344
{
vb@93
   345
    struct mailimf_address_list *mal = NULL;
vb@90
   346
    struct mailimf_mailbox *mb = NULL;
vb@93
   347
    struct mailimf_address * addr = NULL;
vb@90
   348
    clist *list = NULL;
vb@90
   349
    int r;
vb@90
   350
vb@90
   351
    assert(il);
vb@90
   352
vb@90
   353
    list = clist_new();
vb@90
   354
    if (list == NULL)
vb@90
   355
        goto enomem;
vb@90
   356
vb@90
   357
    identity_list *_il;
vb@1016
   358
    for (_il = il; _il && _il->ident; _il = _il->next) {
vb@94
   359
        mb = identity_to_mailbox(_il->ident);
vb@90
   360
        if (mb == NULL)
vb@90
   361
            goto enomem;
vb@90
   362
vb@93
   363
        addr = mailimf_address_new(MAILIMF_ADDRESS_MAILBOX, mb, NULL);
vb@93
   364
        if (addr == NULL)
vb@93
   365
            goto enomem;
vb@93
   366
        mb = NULL;
vb@93
   367
vb@93
   368
        r = clist_append(list, addr);
vb@90
   369
        if (r)
vb@90
   370
            goto enomem;
vb@93
   371
        addr = NULL;
vb@90
   372
    }
vb@93
   373
    mal = mailimf_address_list_new(list);
vb@93
   374
    if (mal == NULL)
vb@90
   375
        goto enomem;
vb@90
   376
vb@93
   377
    return mal;
vb@90
   378
vb@90
   379
enomem:
vb@90
   380
    if (mb)
vb@90
   381
        mailimf_mailbox_free(mb);
vb@90
   382
vb@93
   383
    if (addr)
vb@93
   384
        mailimf_address_free(addr);
vb@93
   385
vb@90
   386
    if (list)
vb@90
   387
        clist_free(list);
vb@90
   388
vb@90
   389
    return NULL;
vb@90
   390
}
vb@90
   391
krista@2200
   392
static clist * stringlist_to_clist(stringlist_t *sl, bool transport_encode)
vb@91
   393
{
vb@91
   394
    clist * cl = clist_new();
vb@91
   395
    assert(cl);
vb@91
   396
    if (cl == NULL)
vb@91
   397
        return NULL;
vb@91
   398
krista@2196
   399
    if (!sl || ((!sl->value || sl->value[0] == '\0') && sl->next == NULL))
krista@2196
   400
        return cl;
krista@2196
   401
        
vb@91
   402
    stringlist_t *_sl;
vb@91
   403
    for (_sl = sl; _sl; _sl = _sl->next) {
vb@91
   404
        int r;
krista@2200
   405
        char * value = (transport_encode ?
krista@2200
   406
                        mailmime_encode_subject_header("utf-8", _sl->value, 0) :
krista@2200
   407
                        strdup(_sl->value));
vb@91
   408
        assert(value);
vb@91
   409
        if (value == NULL) {
vb@91
   410
            clist_free(cl);
vb@91
   411
            return NULL;
vb@91
   412
        }
vb@91
   413
        r = clist_append(cl, value);
vb@91
   414
        assert(r == 0);
vb@91
   415
        if (r) {
vb@91
   416
            free(value);
vb@91
   417
            clist_free(cl);
vb@91
   418
            return NULL;
vb@91
   419
        }
vb@91
   420
    }
vb@91
   421
vb@91
   422
    return cl;
vb@91
   423
}
vb@91
   424
krista@2200
   425
static PEP_STATUS build_fields(const message *msg, struct mailimf_fields **result,
krista@2200
   426
                               bool transport_encode)
vb@89
   427
{
vb@89
   428
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
   429
    struct mailimf_fields * fields = NULL;
vb@89
   430
    int r;
vb@89
   431
    clist * fields_list = NULL;
krista@2045
   432
    unsigned char pepstr[] = PEP_SUBJ_STRING; // unsigned due to UTF-8 byte fun
krista@2045
   433
    char *subject = msg->shortmsg ? msg->shortmsg : (char*)pepstr;
vb@89
   434
vb@89
   435
    assert(msg);
vb@89
   436
    assert(msg->from);
vb@89
   437
    assert(msg->from->address);
vb@89
   438
    assert(result);
vb@89
   439
vb@89
   440
    *result = NULL;
vb@89
   441
vb@89
   442
    fields_list = clist_new();
vb@89
   443
    assert(fields_list);
vb@89
   444
    if (fields_list == NULL)
vb@89
   445
        goto enomem;
vb@89
   446
vb@90
   447
    if (msg->id) {
vb@90
   448
        char *_msgid = strdup(msg->id);
vb@90
   449
        if (_msgid == NULL)
vb@90
   450
            goto enomem;
vb@89
   451
vb@90
   452
        r = _append_field(fields_list, MAILIMF_FIELD_MESSAGE_ID,
vb@90
   453
                (_new_func_t) mailimf_message_id_new, _msgid);
vb@90
   454
        if (r) {
vb@90
   455
            free(_msgid);
vb@90
   456
            goto enomem;
vb@90
   457
        }
vb@90
   458
    }
vb@90
   459
vb@90
   460
    if (msg->sent) {
vb@90
   461
        struct mailimf_date_time * dt = timestamp_to_etpantime(msg->sent);
vb@90
   462
        if (dt == NULL)
vb@90
   463
            goto enomem;
vb@90
   464
vb@90
   465
        r = _append_field(fields_list, MAILIMF_FIELD_ORIG_DATE,
vb@90
   466
                (_new_func_t) mailimf_orig_date_new, dt);
vb@90
   467
        if (r) {
vb@90
   468
            mailimf_date_time_free(dt);
vb@90
   469
            goto enomem;
vb@90
   470
        }
vb@90
   471
        dt = NULL;
vb@90
   472
    }
vb@90
   473
vb@91
   474
    /* if (msg->from) */ {
vb@94
   475
        struct mailimf_mailbox_list *from = identity_to_mbl(msg->from);
vb@90
   476
        if (from == NULL)
vb@90
   477
            goto enomem;
vb@90
   478
vb@90
   479
        r = _append_field(fields_list, MAILIMF_FIELD_FROM,
vb@90
   480
                (_new_func_t) mailimf_from_new, from);
vb@90
   481
        if (r) {
vb@90
   482
            mailimf_mailbox_list_free(from);
vb@90
   483
            goto enomem;
vb@90
   484
        }
vb@90
   485
    }
vb@90
   486
vb@90
   487
    if (msg->to) {
vb@94
   488
        struct mailimf_address_list *to = identity_list_to_mal(msg->to);
vb@90
   489
        if (to == NULL)
vb@90
   490
            goto enomem;
vb@90
   491
vb@91
   492
        r = _append_field(fields_list, MAILIMF_FIELD_TO,
vb@90
   493
                (_new_func_t) mailimf_to_new, to);
vb@90
   494
        if (r) {
vb@93
   495
            mailimf_address_list_free(to);
vb@90
   496
            goto enomem;
vb@90
   497
        }
vb@90
   498
    }
vb@89
   499
vb@112
   500
    /* if (subject) */ {
krista@2200
   501
        char *_subject = (transport_encode ? 
krista@2200
   502
                          mailmime_encode_subject_header("utf-8", subject, 1) :
krista@2200
   503
                          strdup(subject));
vb@112
   504
        if (_subject == NULL)
vb@112
   505
            goto enomem;
vb@112
   506
vb@112
   507
        r = _append_field(fields_list, MAILIMF_FIELD_SUBJECT,
vb@112
   508
                (_new_func_t) mailimf_subject_new, _subject);
vb@112
   509
        if (r) {
vb@112
   510
            free(_subject);
vb@112
   511
            goto enomem;
vb@112
   512
        }
vb@112
   513
    }
vb@112
   514
vb@91
   515
    if (msg->cc) {
vb@94
   516
        struct mailimf_address_list *cc = identity_list_to_mal(msg->cc);
vb@91
   517
        if (cc == NULL)
vb@91
   518
            goto enomem;
vb@91
   519
vb@91
   520
        r = _append_field(fields_list, MAILIMF_FIELD_CC,
vb@91
   521
                (_new_func_t) mailimf_cc_new, cc);
vb@91
   522
        if (r) {
vb@93
   523
            mailimf_address_list_free(cc);
vb@91
   524
            goto enomem;
vb@91
   525
        }
vb@91
   526
    }
vb@91
   527
    
vb@91
   528
    if (msg->bcc) {
vb@94
   529
        struct mailimf_address_list *bcc = identity_list_to_mal(msg->bcc);
vb@91
   530
        if (bcc == NULL)
vb@91
   531
            goto enomem;
vb@91
   532
vb@91
   533
        r = _append_field(fields_list, MAILIMF_FIELD_BCC,
vb@91
   534
                (_new_func_t) mailimf_bcc_new, bcc);
vb@91
   535
        if (r) {
vb@93
   536
            mailimf_address_list_free(bcc);
vb@91
   537
            goto enomem;
vb@91
   538
        }
vb@91
   539
    }
vb@91
   540
    
vb@91
   541
    if (msg->reply_to) {
vb@94
   542
        struct mailimf_address_list *reply_to = identity_list_to_mal(msg->reply_to);
vb@91
   543
        if (reply_to == NULL)
vb@91
   544
            goto enomem;
vb@91
   545
vb@91
   546
        r = _append_field(fields_list, MAILIMF_FIELD_REPLY_TO,
vb@91
   547
                (_new_func_t) mailimf_reply_to_new, reply_to);
vb@91
   548
        if (r) {
vb@94
   549
            mailimf_address_list_free(reply_to);
vb@91
   550
            goto enomem;
vb@91
   551
        }
vb@91
   552
    }
vb@91
   553
vb@91
   554
    if (msg->in_reply_to) {
krista@2200
   555
        clist *in_reply_to = stringlist_to_clist(msg->in_reply_to, transport_encode);
vb@91
   556
        if (in_reply_to == NULL)
vb@91
   557
            goto enomem;
vb@91
   558
vb@91
   559
        r = _append_field(fields_list, MAILIMF_FIELD_IN_REPLY_TO,
vb@91
   560
                (_new_func_t) mailimf_in_reply_to_new, in_reply_to);
vb@91
   561
        if (r) {
vb@94
   562
            clist_free(in_reply_to);
vb@91
   563
            goto enomem;
vb@91
   564
        }
vb@91
   565
    }
vb@91
   566
vb@91
   567
    if (msg->references) {
krista@2200
   568
        clist *references = stringlist_to_clist(msg->references, transport_encode);
vb@91
   569
        if (references == NULL)
vb@91
   570
            goto enomem;
vb@91
   571
vb@91
   572
        r = _append_field(fields_list, MAILIMF_FIELD_REFERENCES,
vb@91
   573
                (_new_func_t) mailimf_references_new, references);
vb@91
   574
        if (r) {
vb@91
   575
            clist_free(references);
vb@91
   576
            goto enomem;
vb@91
   577
        }
vb@91
   578
    }
vb@91
   579
vb@91
   580
    if (msg->keywords) {
krista@2200
   581
        clist *keywords = stringlist_to_clist(msg->keywords, transport_encode);
vb@91
   582
        if (keywords == NULL)
vb@91
   583
            goto enomem;
vb@91
   584
vb@91
   585
        r = _append_field(fields_list, MAILIMF_FIELD_KEYWORDS,
vb@91
   586
                (_new_func_t) mailimf_keywords_new, keywords);
vb@91
   587
        if (r) {
vb@91
   588
            clist_free(keywords);
vb@91
   589
            goto enomem;
vb@91
   590
        }
vb@91
   591
    }
vb@91
   592
vb@91
   593
    if (msg->comments) {
krista@2200
   594
        char *comments = (transport_encode ?
krista@2200
   595
                          mailmime_encode_subject_header("utf-8", msg->comments, 0) :
krista@2200
   596
                          strdup(msg->comments));
vb@91
   597
        if (comments == NULL)
vb@91
   598
            goto enomem;
vb@91
   599
vb@91
   600
        r = _append_field(fields_list, MAILIMF_FIELD_COMMENTS,
vb@91
   601
                (_new_func_t) mailimf_comments_new, comments);
vb@91
   602
        if (r) {
vb@91
   603
            free(comments);
vb@91
   604
            goto enomem;
vb@91
   605
        }
vb@91
   606
    }
vb@91
   607
vb@98
   608
    if (msg->opt_fields) {
vb@110
   609
        stringpair_list_t *_l;
vb@281
   610
        for (_l = msg->opt_fields; _l && _l->value; _l = _l->next) {
vb@110
   611
            char *key = _l->value->key;
vb@110
   612
            char *value = _l->value->value;
vb@281
   613
            if (key && value) {
krista@2200
   614
                char *_value = (transport_encode ?
krista@2200
   615
                                mailmime_encode_subject_header("utf-8", value, 0) :
krista@2200
   616
                                strdup(value));
vb@281
   617
                if (_value == NULL)
vb@281
   618
                    goto enomem;
vb@110
   619
vb@281
   620
                r = _append_optional_field(fields_list, key, _value);
vb@281
   621
                free(_value);
vb@281
   622
                if (r)
vb@281
   623
                    goto enomem;
vb@281
   624
            }
vb@110
   625
        }
vb@98
   626
    }
vb@94
   627
vb@89
   628
    fields = mailimf_fields_new(fields_list);
vb@89
   629
    assert(fields);
vb@89
   630
    if (fields == NULL)
vb@89
   631
        goto enomem;
vb@89
   632
vb@89
   633
    *result = fields;
vb@89
   634
vb@89
   635
    return PEP_STATUS_OK;
vb@89
   636
vb@89
   637
enomem:
vb@89
   638
    status = PEP_OUT_OF_MEMORY;
vb@89
   639
vb@89
   640
    if (fields_list)
vb@89
   641
        clist_free(fields_list);
vb@89
   642
vb@89
   643
    if (fields)
vb@89
   644
        mailimf_fields_free(fields);
vb@89
   645
vb@89
   646
    return status;
vb@89
   647
}
vb@89
   648
krista@1873
   649
static bool has_exceptional_extension(char* filename) {
krista@1873
   650
    if (!filename)
krista@1873
   651
        return false;
krista@1873
   652
    int len = strlen(filename);
krista@1873
   653
    if (len < 4)
krista@1873
   654
        return false;
krista@1873
   655
    char* ext_start = filename + (len - 4);
krista@1873
   656
    if (strcmp(ext_start, ".pgp") == 0 || strcmp(ext_start, ".gpg") == 0 ||
krista@1873
   657
        strcmp(ext_start, ".asc") == 0 || strcmp(ext_start, ".pEp") == 0)
krista@1873
   658
        return true;
krista@1873
   659
    return false;
krista@1873
   660
}
krista@1873
   661
krista@1873
   662
static pEp_rid_list_t* choose_resource_id(pEp_rid_list_t* rid_list) {
krista@1882
   663
    pEp_rid_list_t* retval = rid_list;
krista@1873
   664
    
krista@1873
   665
    /* multiple elements - least common case */
krista@1873
   666
    if (rid_list && rid_list->next) {
krista@1873
   667
        pEp_rid_list_t* rid_list_curr = rid_list;
krista@1873
   668
        retval = rid_list; 
krista@1873
   669
        
krista@1873
   670
        while (rid_list_curr) {
krista@1873
   671
            pEp_resource_id_type rid_type = rid_list_curr->rid_type;
krista@1873
   672
            if (rid_type == PEP_RID_CID)
krista@1873
   673
                retval = rid_list_curr;
krista@1873
   674
            else if (rid_type == PEP_RID_FILENAME && has_exceptional_extension(rid_list_curr->rid))
krista@1873
   675
                return rid_list_curr;
krista@1873
   676
            rid_list_curr = rid_list_curr->next;
krista@1873
   677
        }
krista@1873
   678
    } 
krista@1873
   679
    return retval;
krista@1873
   680
}
krista@1873
   681
krista@2036
   682
// static void split_inlined_and_attached(bloblist_t** inlined, bloblist_t** attached) {
krista@2036
   683
//     bloblist_t** curr_pp = attached;
krista@2036
   684
//     bloblist_t* curr = *curr_pp;
krista@2036
   685
//     
krista@2036
   686
//     bloblist_t* inline_ret = NULL;
krista@2036
   687
//     bloblist_t** inline_curr_pp = &inline_ret;
krista@2036
   688
//     
krista@2036
   689
//     bloblist_t* att_ret = NULL;
krista@2036
   690
//     bloblist_t** att_curr_pp = &att_ret;
krista@2036
   691
//     
krista@2036
   692
//     while (curr) {
krista@2036
   693
//         if (curr->disposition == PEP_CONTENT_DISP_INLINE) {
krista@2036
   694
//             *inline_curr_pp = curr;
krista@2036
   695
//             inline_curr_pp = &(curr->next);
krista@2036
   696
//         }
krista@2036
   697
//         else {
krista@2036
   698
//             *att_curr_pp = curr;
krista@2036
   699
//             att_curr_pp = &(curr->next);            
krista@2036
   700
//         }
krista@2036
   701
//         *curr_pp = curr->next;
krista@2036
   702
//         curr->next = NULL;
krista@2036
   703
//         curr = *curr_pp;
krista@2036
   704
//     }
krista@2036
   705
//     
krista@2036
   706
//     *inlined = inline_ret;
krista@2036
   707
//     *attached = att_ret;
krista@2036
   708
// }
krista@2011
   709
krista@2011
   710
vb@114
   711
static PEP_STATUS mime_encode_message_plain(
vb@89
   712
        const message *msg,
vb@113
   713
        bool omit_fields,
krista@2200
   714
        struct mailmime **result,
krista@2200
   715
        bool transport_encode
vb@89
   716
    )
vb@89
   717
{
vb@89
   718
    struct mailmime * mime = NULL;
vb@89
   719
    struct mailmime * submime = NULL;
vb@89
   720
    int r;
vb@89
   721
    PEP_STATUS status;
roker@723
   722
    //char *subject;
vb@89
   723
    char *plaintext;
vb@89
   724
    char *htmltext;
vb@89
   725
vb@89
   726
    assert(msg);
vb@114
   727
    assert(result);
krista@2035
   728
    
roker@723
   729
    //subject = (msg->shortmsg) ? msg->shortmsg : "pEp";  // not used, yet.
vb@89
   730
    plaintext = (msg->longmsg) ? msg->longmsg : "";
vb@89
   731
    htmltext = msg->longmsg_formatted;
vb@89
   732
krista@1197
   733
    if (htmltext && (htmltext[0] != '\0')) {
krista@2011
   734
        /* first, we need to strip out the inlined attachments to ensure this
krista@2011
   735
           gets set up correctly */
krista@2036
   736
           
krista@2200
   737
        status = mime_html_text(plaintext, htmltext, msg->attachments, &mime,
krista@2200
   738
                                transport_encode);
krista@2027
   739
                
vb@89
   740
        if (status != PEP_STATUS_OK)
vb@89
   741
            goto pep_error;
vb@89
   742
    }
vb@89
   743
    else {
krista@1878
   744
        pEp_rid_list_t* resource = NULL;
krista@1878
   745
        if (is_PGP_message_text(plaintext)) {
krista@1878
   746
            resource = new_rid_node(PEP_RID_FILENAME, "msg.asc");
krista@2200
   747
            int encoding_type = (transport_encode ? MAILMIME_MECHANISM_7BIT : 0);
krista@1878
   748
            mime = get_text_part(resource, "application/octet-stream", plaintext,
krista@2200
   749
                    strlen(plaintext), encoding_type);
krista@1878
   750
        }
krista@1878
   751
        else {
krista@1878
   752
            resource = new_rid_node(PEP_RID_FILENAME, "msg.txt");
krista@2200
   753
            int encoding_type = (transport_encode ? MAILMIME_MECHANISM_QUOTED_PRINTABLE : 0);
krista@1878
   754
            mime = get_text_part(resource, "text/plain", plaintext, strlen(plaintext),
krista@2200
   755
                    encoding_type);
krista@1878
   756
        }
krista@1878
   757
        free_rid_list(resource);
krista@1878
   758
        
vb@89
   759
        assert(mime);
vb@89
   760
        if (mime == NULL)
vb@89
   761
            goto enomem;
vb@89
   762
    }
vb@89
   763
krista@2036
   764
    bool normal_attachments = false;
krista@2036
   765
    
krista@2036
   766
    bloblist_t* traversal_ptr = msg->attachments;
krista@2036
   767
    
krista@2036
   768
    while (traversal_ptr) {
krista@2036
   769
        if (traversal_ptr->disposition != PEP_CONTENT_DISP_INLINE) {
krista@2036
   770
            normal_attachments = true;
krista@2036
   771
            break;
krista@2036
   772
        }
krista@2036
   773
        traversal_ptr = traversal_ptr->next;
krista@2036
   774
    }
krista@2036
   775
krista@2035
   776
    if (normal_attachments) {
vb@89
   777
        submime = mime;
vb@116
   778
        mime = part_multiple_new("multipart/mixed");
vb@89
   779
        assert(mime);
vb@89
   780
        if (mime == NULL)
vb@89
   781
            goto enomem;
vb@89
   782
vb@89
   783
        r = mailmime_smart_add_part(mime, submime);
vb@89
   784
        assert(r == MAILIMF_NO_ERROR);
vb@89
   785
        if (r == MAILIMF_ERROR_MEMORY) {
vb@89
   786
            goto enomem;
vb@89
   787
        }
vb@89
   788
        else {
vb@89
   789
            // mailmime_smart_add_part() takes ownership of submime
vb@89
   790
            submime = NULL;
vb@89
   791
        }
vb@89
   792
vb@89
   793
        bloblist_t *_a;
krista@2036
   794
        for (_a = msg->attachments; _a != NULL; _a = _a->next) {
vb@89
   795
krista@2036
   796
            if (_a->disposition == PEP_CONTENT_DISP_INLINE)
krista@2036
   797
                continue;
krista@2036
   798
krista@2200
   799
            status = mime_attachment(_a, &submime, transport_encode);
vb@89
   800
            if (status != PEP_STATUS_OK)
vb@89
   801
                goto pep_error;
vb@89
   802
vb@89
   803
            r = mailmime_smart_add_part(mime, submime);
vb@89
   804
            assert(r == MAILIMF_NO_ERROR);
vb@89
   805
            if (r == MAILIMF_ERROR_MEMORY) {
vb@89
   806
                goto enomem;
vb@89
   807
            }
vb@89
   808
            else {
vb@89
   809
                // mailmime_smart_add_part() takes ownership of submime
vb@89
   810
                submime = NULL;
vb@89
   811
            }
vb@89
   812
        }
vb@89
   813
    }
vb@89
   814
vb@114
   815
    *result = mime;
vb@114
   816
    return PEP_STATUS_OK;
vb@114
   817
vb@114
   818
enomem:
vb@114
   819
    status = PEP_OUT_OF_MEMORY;
vb@114
   820
vb@114
   821
pep_error:
vb@114
   822
    if (mime)
vb@114
   823
        mailmime_free(mime);
vb@114
   824
vb@114
   825
    if (submime)
vb@114
   826
        mailmime_free(submime);
vb@114
   827
vb@114
   828
    return status;
vb@114
   829
}
vb@114
   830
vb@114
   831
static PEP_STATUS mime_encode_message_PGP_MIME(
vb@114
   832
        const message * msg,
vb@114
   833
        bool omit_fields,
vb@114
   834
        struct mailmime **result
vb@114
   835
    )
vb@114
   836
{
vb@114
   837
    struct mailmime * mime = NULL;
vb@114
   838
    struct mailmime * submime = NULL;
vb@114
   839
	struct mailmime_parameter * param;
vb@114
   840
    int r;
vb@114
   841
    PEP_STATUS status;
vb@114
   842
    char *plaintext;
edouard@1274
   843
    size_t plaintext_size;
vb@114
   844
vb@260
   845
    assert(msg->attachments && msg->attachments->next &&
vb@301
   846
            msg->attachments->next->value);
vb@114
   847
vb@301
   848
    plaintext = msg->attachments->next->value;
edouard@1274
   849
    plaintext_size = msg->attachments->next->size;
vb@114
   850
vb@116
   851
    mime = part_multiple_new("multipart/encrypted");
vb@114
   852
    assert(mime);
vb@114
   853
    if (mime == NULL)
vb@114
   854
        goto enomem;
vb@114
   855
vb@114
   856
    param = mailmime_param_new_with_data("protocol", "application/pgp-encrypted");
vb@114
   857
    clist_append(mime->mm_content_type->ct_parameters, param);
vb@114
   858
vb@114
   859
    submime = get_pgp_encrypted_part();
vb@114
   860
    assert(submime);
vb@114
   861
    if (submime == NULL)
vb@114
   862
        goto enomem;
vb@114
   863
vb@114
   864
    r = mailmime_smart_add_part(mime, submime);
vb@114
   865
    assert(r == MAILIMF_NO_ERROR);
vb@114
   866
    if (r == MAILIMF_ERROR_MEMORY) {
vb@114
   867
        goto enomem;
vb@114
   868
    }
vb@114
   869
    else {
vb@114
   870
        // mailmime_smart_add_part() takes ownership of submime
vb@114
   871
        submime = NULL;
vb@114
   872
    }
vb@114
   873
krista@1878
   874
    pEp_rid_list_t* resource = new_rid_node(PEP_RID_FILENAME, "msg.asc");
krista@1878
   875
    submime = get_text_part(resource, "application/octet-stream", plaintext,
edouard@1274
   876
            plaintext_size, MAILMIME_MECHANISM_7BIT);
krista@1878
   877
            
krista@1878
   878
    free_rid_list(resource);
krista@1878
   879
    
vb@114
   880
    assert(submime);
vb@114
   881
    if (submime == NULL)
vb@114
   882
        goto enomem;
vb@114
   883
vb@114
   884
    r = mailmime_smart_add_part(mime, submime);
vb@114
   885
    assert(r == MAILIMF_NO_ERROR);
vb@114
   886
    if (r == MAILIMF_ERROR_MEMORY) {
vb@114
   887
        goto enomem;
vb@114
   888
    }
vb@114
   889
    else {
vb@114
   890
        // mailmime_smart_add_part() takes ownership of submime
vb@114
   891
        submime = NULL;
vb@114
   892
    }
vb@114
   893
vb@114
   894
    *result = mime;
vb@114
   895
    return PEP_STATUS_OK;
vb@114
   896
vb@114
   897
enomem:
vb@114
   898
    status = PEP_OUT_OF_MEMORY;
vb@114
   899
vb@114
   900
    if (mime)
vb@114
   901
        mailmime_free(mime);
vb@114
   902
vb@114
   903
    if (submime)
vb@114
   904
        mailmime_free(submime);
vb@114
   905
vb@114
   906
    return status;
vb@114
   907
}
vb@114
   908
vb@114
   909
DYNAMIC_API PEP_STATUS mime_encode_message(
vb@114
   910
        const message * msg,
vb@114
   911
        bool omit_fields,
vb@114
   912
        char **mimetext
vb@114
   913
    )
vb@114
   914
{
krista@2200
   915
    return _mime_encode_message_internal(msg, omit_fields, mimetext, true);
krista@2200
   916
}
krista@2200
   917
krista@2200
   918
PEP_STATUS _mime_encode_message_internal(
krista@2200
   919
        const message * msg,
krista@2200
   920
        bool omit_fields,
krista@2200
   921
        char **mimetext,
krista@2200
   922
        bool transport_encode
krista@2200
   923
    )
krista@2200
   924
{
vb@114
   925
    PEP_STATUS status = PEP_STATUS_OK;
vb@114
   926
    struct mailmime * msg_mime = NULL;
vb@114
   927
    struct mailmime * mime = NULL;
vb@114
   928
    struct mailimf_fields * fields = NULL;
vb@114
   929
    char *buf = NULL;
vb@114
   930
    int r;
vb@114
   931
vb@114
   932
    assert(msg);
vb@114
   933
    assert(mimetext);
vb@114
   934
vb@260
   935
    if (!(msg && mimetext))
vb@191
   936
        return PEP_ILLEGAL_VALUE;
vb@191
   937
vb@114
   938
    *mimetext = NULL;
vb@114
   939
vb@114
   940
    switch (msg->enc_format) {
vb@114
   941
        case PEP_enc_none:
krista@2200
   942
            status = mime_encode_message_plain(msg, omit_fields, &mime, transport_encode);
vb@114
   943
            break;
vb@114
   944
vb@114
   945
        case PEP_enc_pieces:
krista@2200
   946
            status = mime_encode_message_plain(msg, omit_fields, &mime, transport_encode);
vb@114
   947
            break;
vb@114
   948
vb@114
   949
        case PEP_enc_S_MIME:
vb@114
   950
            NOT_IMPLEMENTED
vb@114
   951
                
vb@114
   952
        case PEP_enc_PGP_MIME:
vb@114
   953
            status = mime_encode_message_PGP_MIME(msg, omit_fields, &mime);
vb@114
   954
            break;
vb@114
   955
vb@114
   956
        case PEP_enc_PEP:
vb@114
   957
            NOT_IMPLEMENTED
edouard@1557
   958
edouard@1557
   959
        default:
edouard@1557
   960
            NOT_IMPLEMENTED
vb@114
   961
    }
vb@114
   962
vb@114
   963
    if (status != PEP_STATUS_OK)
vb@114
   964
        goto pep_error;
vb@114
   965
vb@89
   966
    msg_mime = mailmime_new_message_data(NULL);
vb@90
   967
    assert(msg_mime);
vb@90
   968
    if (msg_mime == NULL)
vb@90
   969
        goto enomem;
vb@89
   970
vb@90
   971
    r = mailmime_add_part(msg_mime, mime);
vb@90
   972
    if (r) {
vb@90
   973
        mailmime_free(mime);
vb@90
   974
        goto enomem;
vb@90
   975
    }
vb@114
   976
    mime = NULL;
vb@90
   977
vb@113
   978
    if (!omit_fields) {
krista@2200
   979
        status = build_fields(msg, &fields, transport_encode);
vb@113
   980
        if (status != PEP_STATUS_OK)
vb@113
   981
            goto pep_error;
vb@90
   982
vb@113
   983
        mailmime_set_imf_fields(msg_mime, fields);
vb@113
   984
    }
vb@90
   985
vb@90
   986
    status = render_mime(msg_mime, &buf);
vb@89
   987
    if (status != PEP_STATUS_OK)
vb@89
   988
        goto pep_error;
vb@89
   989
vb@89
   990
    mailmime_free(msg_mime);
vb@89
   991
    *mimetext = buf;
vb@90
   992
vb@89
   993
    return PEP_STATUS_OK;
vb@89
   994
vb@89
   995
enomem:
vb@89
   996
    status = PEP_OUT_OF_MEMORY;
vb@89
   997
vb@89
   998
pep_error:
vb@89
   999
    if (msg_mime)
vb@90
  1000
        mailmime_free(msg_mime);
vb@89
  1001
    else
vb@89
  1002
        if (mime)
vb@89
  1003
            mailmime_free(mime);
vb@89
  1004
vb@89
  1005
    return status;
vb@89
  1006
}
vb@89
  1007
vb@94
  1008
static pEp_identity *mailbox_to_identity(const struct mailimf_mailbox * mb)
vb@94
  1009
{
vb@94
  1010
    char *username = NULL;
vb@94
  1011
vb@159
  1012
    assert(mb);
vb@159
  1013
    assert(mb->mb_addr_spec);
vb@159
  1014
vb@159
  1015
    if (mb->mb_addr_spec == NULL)
vb@159
  1016
        return NULL;
vb@159
  1017
vb@159
  1018
    if (mb->mb_display_name) {
roker@871
  1019
        size_t index = 0;
roker@871
  1020
        const int r = mailmime_encoded_phrase_parse("utf-8", mb->mb_display_name,
vb@159
  1021
                strlen(mb->mb_display_name), &index, "utf-8", &username);
vb@159
  1022
        if (r)
vb@159
  1023
            goto enomem;
vb@159
  1024
    }
vb@94
  1025
roker@871
  1026
    pEp_identity *ident = new_identity(mb->mb_addr_spec, NULL, NULL, username);
vb@94
  1027
    if (ident == NULL)
vb@94
  1028
        goto enomem;
vb@94
  1029
    free(username);
vb@94
  1030
vb@94
  1031
    return ident;
vb@94
  1032
vb@94
  1033
enomem:
vb@94
  1034
    free(username);
vb@94
  1035
    return NULL;
vb@94
  1036
}
vb@94
  1037
vb@94
  1038
static pEp_identity * mbl_to_identity(const struct mailimf_mailbox_list * mbl)
vb@94
  1039
{
vb@94
  1040
    struct mailimf_mailbox * mb = clist_content(clist_begin(mbl->mb_list));
vb@94
  1041
    return mailbox_to_identity(mb);
vb@94
  1042
}
vb@94
  1043
vb@94
  1044
static identity_list * mal_to_identity_list(
vb@94
  1045
        const struct mailimf_address_list *mal
vb@94
  1046
    )
vb@94
  1047
{
roker@871
  1048
    assert(mal);
vb@94
  1049
    clist *list = mal->ad_list;
vb@94
  1050
roker@871
  1051
    identity_list *il = new_identity_list(NULL);
vb@94
  1052
    if (il == NULL)
vb@94
  1053
        goto enomem;
vb@94
  1054
vb@94
  1055
    identity_list *_il = il;
roker@871
  1056
    for (clistiter *cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@94
  1057
        pEp_identity *ident;
vb@94
  1058
roker@871
  1059
        struct mailimf_address *addr = clist_content(cur);
vb@94
  1060
        switch(addr->ad_type) {
vb@94
  1061
            case MAILIMF_ADDRESS_MAILBOX:
vb@94
  1062
                ident = mailbox_to_identity(addr->ad_data.ad_mailbox);
vb@94
  1063
                if (ident == NULL)
vb@94
  1064
                    goto enomem;
vb@94
  1065
                _il = identity_list_add(_il, ident);
vb@94
  1066
                if (_il == NULL)
vb@94
  1067
                    goto enomem;
vb@94
  1068
                break;
vb@94
  1069
vb@94
  1070
            case MAILIMF_ADDRESS_GROUP:
vb@94
  1071
                {
vb@94
  1072
                    struct mailimf_mailbox_list * mbl =
vb@94
  1073
                            addr->ad_data.ad_group->grp_mb_list;
roker@871
  1074
                    for (clistiter *cur2 = clist_begin(mbl->mb_list); cur2 != NULL;
vb@94
  1075
                            cur2 = clist_next(cur2)) {
vb@94
  1076
                        ident = mailbox_to_identity(clist_content(cur));
vb@94
  1077
                        if (ident == NULL)
vb@94
  1078
                            goto enomem;
vb@94
  1079
                        _il = identity_list_add(_il, ident);
vb@94
  1080
                        if (_il == NULL)
vb@94
  1081
                            goto enomem;
vb@94
  1082
                    }
vb@94
  1083
                }
vb@94
  1084
                break;
vb@94
  1085
vb@94
  1086
            default:
vb@94
  1087
                assert(0);
vb@94
  1088
                goto enomem;
vb@94
  1089
        }
vb@94
  1090
    }
vb@94
  1091
vb@94
  1092
    return il;
vb@94
  1093
vb@94
  1094
enomem:
vb@94
  1095
    free_identity_list(il);
vb@94
  1096
    return NULL;
vb@94
  1097
}
vb@94
  1098
vb@94
  1099
static stringlist_t * clist_to_stringlist(const clist *list)
vb@94
  1100
{
vb@94
  1101
    char *text = NULL;;
vb@94
  1102
    stringlist_t * sl = new_stringlist(NULL);
vb@94
  1103
    if (sl == NULL)
vb@94
  1104
        return NULL;
vb@94
  1105
vb@94
  1106
    stringlist_t *_sl = sl;
roker@871
  1107
    for (clistiter *cur = clist_begin(list); cur != NULL; cur = clist_next(cur)) {
vb@94
  1108
        char *phrase = clist_content(cur);
roker@871
  1109
        size_t index = 0;
roker@871
  1110
        
roker@871
  1111
        const int r = mailmime_encoded_phrase_parse("utf-8", phrase, strlen(phrase),
vb@94
  1112
                &index, "utf-8", &text);
vb@94
  1113
        if (r)
vb@94
  1114
            goto enomem;
vb@94
  1115
vb@94
  1116
        _sl = stringlist_add(_sl, text);
vb@94
  1117
        if (_sl == NULL)
vb@94
  1118
            goto enomem;
vb@94
  1119
vb@94
  1120
        free(text);
vb@94
  1121
        text = NULL;
vb@94
  1122
    }
vb@94
  1123
krista@2182
  1124
    return sl;
vb@94
  1125
vb@94
  1126
enomem:
vb@94
  1127
    free_stringlist(sl);
vb@94
  1128
    free(text);
vb@94
  1129
vb@94
  1130
    return NULL;
vb@94
  1131
}
vb@94
  1132
vb@94
  1133
static PEP_STATUS read_fields(message *msg, clist *fieldlist)
vb@94
  1134
{
vb@94
  1135
    PEP_STATUS status = PEP_STATUS_OK;
vb@94
  1136
    struct mailimf_field * _field;
vb@94
  1137
    clistiter *cur;
vb@94
  1138
    size_t index;
vb@94
  1139
    int r;
vb@110
  1140
    stringpair_list_t *opt = msg->opt_fields;
vb@94
  1141
vb@94
  1142
    for (cur = clist_begin(fieldlist); cur != NULL; cur = clist_next(cur)) {
vb@94
  1143
        _field = clist_content(cur);
vb@94
  1144
vb@94
  1145
        switch (_field->fld_type) {
vb@94
  1146
            case MAILIMF_FIELD_MESSAGE_ID:
vb@94
  1147
                {
vb@94
  1148
                    char * text = _field->fld_data.fld_message_id->mid_value;
vb@116
  1149
vb@116
  1150
                    free(msg->id);
vb@94
  1151
                    index = 0;
vb@94
  1152
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1153
                            strlen(text), &index, "utf-8", &msg->id);
vb@94
  1154
                    if (r)
vb@94
  1155
                        goto enomem;
vb@94
  1156
                }
vb@94
  1157
                break;
vb@94
  1158
vb@94
  1159
            case MAILIMF_FIELD_SUBJECT:
vb@94
  1160
                {
vb@94
  1161
                    char * text = _field->fld_data.fld_subject->sbj_value;
vb@116
  1162
vb@116
  1163
                    free(msg->shortmsg);
vb@94
  1164
                    index = 0;
vb@94
  1165
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1166
                            strlen(text), &index, "utf-8", &msg->shortmsg);
vb@94
  1167
                    if (r)
vb@94
  1168
                        goto enomem;
vb@94
  1169
                }
vb@94
  1170
                break;
vb@94
  1171
vb@94
  1172
            case MAILIMF_FIELD_ORIG_DATE:
vb@94
  1173
                {
vb@94
  1174
                    struct mailimf_date_time *date =
vb@94
  1175
                        _field->fld_data.fld_orig_date->dt_date_time;
vb@116
  1176
vb@116
  1177
                    free_timestamp(msg->sent);
vb@94
  1178
                    msg->sent = etpantime_to_timestamp(date);
vb@94
  1179
                    if (msg->sent == NULL)
vb@94
  1180
                        goto enomem;
vb@94
  1181
                }
vb@94
  1182
                break;
vb@94
  1183
vb@94
  1184
            case MAILIMF_FIELD_FROM:
vb@94
  1185
                {
vb@94
  1186
                    struct mailimf_mailbox_list *mbl =
vb@94
  1187
                            _field->fld_data.fld_from->frm_mb_list;
vb@94
  1188
                    pEp_identity *ident;
vb@94
  1189
vb@94
  1190
                    ident = mbl_to_identity(mbl);
vb@94
  1191
                    if (ident == NULL)
vb@94
  1192
                        goto pep_error;
vb@94
  1193
vb@116
  1194
                    free_identity(msg->from);
vb@94
  1195
                    msg->from = ident;
vb@94
  1196
                }
vb@94
  1197
                break;
vb@94
  1198
vb@94
  1199
            case MAILIMF_FIELD_TO:
vb@94
  1200
                {
vb@94
  1201
                    struct mailimf_address_list *mal =
vb@94
  1202
                            _field->fld_data.fld_to->to_addr_list;
vb@94
  1203
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1204
                    if (il == NULL)
vb@94
  1205
                        goto enomem;
vb@116
  1206
vb@116
  1207
                    free_identity_list(msg->to);
vb@94
  1208
                    msg->to = il;
vb@94
  1209
                }
vb@94
  1210
                break;
vb@94
  1211
vb@94
  1212
            case MAILIMF_FIELD_CC:
vb@94
  1213
                {
vb@94
  1214
                    struct mailimf_address_list *mal =
vb@94
  1215
                            _field->fld_data.fld_cc->cc_addr_list;
vb@94
  1216
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1217
                    if (il == NULL)
vb@94
  1218
                        goto enomem;
vb@116
  1219
vb@116
  1220
                    free_identity_list(msg->cc);
vb@94
  1221
                    msg->cc = il;
vb@94
  1222
                }
vb@94
  1223
                break;
vb@94
  1224
vb@94
  1225
            case MAILIMF_FIELD_BCC:
vb@94
  1226
                {
vb@94
  1227
                    struct mailimf_address_list *mal =
vb@94
  1228
                            _field->fld_data.fld_bcc->bcc_addr_list;
vb@94
  1229
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1230
                    if (il == NULL)
vb@94
  1231
                        goto enomem;
vb@116
  1232
vb@116
  1233
                    free_identity_list(msg->bcc);
vb@94
  1234
                    msg->bcc = il;
vb@94
  1235
                }
vb@94
  1236
                break;
vb@94
  1237
vb@94
  1238
            case MAILIMF_FIELD_REPLY_TO:
vb@94
  1239
                {
vb@94
  1240
                    struct mailimf_address_list *mal =
vb@94
  1241
                            _field->fld_data.fld_reply_to->rt_addr_list;
vb@94
  1242
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1243
                    if (il == NULL)
vb@94
  1244
                        goto enomem;
vb@116
  1245
vb@116
  1246
                    free_identity_list(msg->reply_to);
vb@94
  1247
                    msg->reply_to = il;
vb@94
  1248
                }
vb@94
  1249
                break;
vb@94
  1250
vb@94
  1251
            case MAILIMF_FIELD_IN_REPLY_TO:
vb@94
  1252
                {
vb@94
  1253
                    clist *list = _field->fld_data.fld_in_reply_to->mid_list;
vb@94
  1254
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1255
                    if (sl == NULL)
vb@94
  1256
                        goto enomem;
vb@116
  1257
vb@116
  1258
                    free_stringlist(msg->in_reply_to);
vb@94
  1259
                    msg->in_reply_to = sl;
vb@94
  1260
                }
vb@94
  1261
                break;
vb@94
  1262
vb@94
  1263
            case MAILIMF_FIELD_REFERENCES:
vb@94
  1264
                {
vb@94
  1265
                    clist *list = _field->fld_data.fld_references->mid_list;
vb@94
  1266
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1267
                    if (sl == NULL)
vb@94
  1268
                        goto enomem;
vb@116
  1269
vb@116
  1270
                    free_stringlist(msg->references);
vb@94
  1271
                    msg->references = sl;
vb@94
  1272
                }
vb@94
  1273
                break;
vb@94
  1274
vb@94
  1275
            case MAILIMF_FIELD_KEYWORDS:
vb@94
  1276
                {
vb@94
  1277
                    clist *list = _field->fld_data.fld_keywords->kw_list;
vb@94
  1278
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1279
                    if (sl == NULL)
vb@94
  1280
                        goto enomem;
vb@116
  1281
vb@116
  1282
                    free_stringlist(msg->keywords);
vb@94
  1283
                    msg->keywords = sl;
vb@94
  1284
                }
vb@94
  1285
                break;
vb@94
  1286
vb@94
  1287
            case MAILIMF_FIELD_COMMENTS:
vb@94
  1288
                {
vb@94
  1289
                    char * text = _field->fld_data.fld_comments->cm_value;
vb@116
  1290
vb@116
  1291
                    free(msg->comments);
vb@94
  1292
                    index = 0;
vb@94
  1293
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1294
                            strlen(text), &index, "utf-8", &msg->comments);
vb@94
  1295
                    if (r)
vb@94
  1296
                        goto enomem;
vb@94
  1297
                }
vb@94
  1298
                break;
vb@110
  1299
vb@110
  1300
            case MAILIMF_FIELD_OPTIONAL_FIELD:
vb@110
  1301
                {
vb@110
  1302
                    char * name =
vb@110
  1303
                            _field->fld_data.fld_optional_field->fld_name;
vb@110
  1304
                    char * value =
vb@110
  1305
                            _field->fld_data.fld_optional_field->fld_value;
vb@110
  1306
                    char *_value;
vb@110
  1307
vb@110
  1308
                    index = 0;
vb@110
  1309
                    r = mailmime_encoded_phrase_parse("utf-8", value,
vb@110
  1310
                            strlen(value), &index, "utf-8", &_value);
vb@110
  1311
                    if (r)
vb@110
  1312
                        goto enomem;
vb@110
  1313
vb@281
  1314
                    stringpair_t *pair = new_stringpair(name, _value);
vb@281
  1315
                    if (pair == NULL)
vb@281
  1316
                        goto enomem;
vb@110
  1317
vb@281
  1318
                    opt = stringpair_list_add(opt, pair);
vb@110
  1319
                    free(_value);
vb@110
  1320
                    if (opt == NULL)
vb@110
  1321
                        goto enomem;
vb@110
  1322
vb@110
  1323
                    if (msg->opt_fields == NULL)
vb@110
  1324
                        msg->opt_fields = opt;
vb@110
  1325
                }
vb@110
  1326
                break;
vb@94
  1327
        }
vb@94
  1328
    }
vb@94
  1329
vb@94
  1330
    return PEP_STATUS_OK;
vb@94
  1331
vb@94
  1332
enomem:
vb@94
  1333
    status = PEP_OUT_OF_MEMORY;
vb@94
  1334
vb@94
  1335
pep_error:
vb@94
  1336
    return status;
vb@94
  1337
}
vb@94
  1338
vb@147
  1339
static PEP_STATUS interpret_body(struct mailmime *part, char **longmsg, size_t *size)
vb@147
  1340
{
vb@147
  1341
    const char *text;
vb@159
  1342
    char *_longmsg;
vb@147
  1343
    size_t length;
vb@159
  1344
    size_t _size;
vb@147
  1345
    int code;
vb@147
  1346
    int r;
vb@147
  1347
    size_t index;
vb@159
  1348
    char *type = NULL;
vb@159
  1349
    char *charset = NULL;
vb@147
  1350
vb@147
  1351
    assert(part);
vb@147
  1352
    assert(longmsg);
vb@147
  1353
vb@147
  1354
    *longmsg = NULL;
vb@147
  1355
    if (size)
vb@147
  1356
        *size = 0;
vb@147
  1357
vb@147
  1358
    if (part->mm_body == NULL)
vb@147
  1359
        return PEP_ILLEGAL_VALUE;
vb@147
  1360
vb@147
  1361
    text = part->mm_body-> dt_data.dt_text.dt_data;
vb@147
  1362
    if (text == NULL)
vb@147
  1363
        return PEP_ILLEGAL_VALUE;
vb@147
  1364
vb@147
  1365
    length = part->mm_body->dt_data.dt_text.dt_length;
vb@147
  1366
vb@147
  1367
    if (part->mm_body->dt_encoded) {
vb@147
  1368
        code = part->mm_body->dt_encoding;
vb@147
  1369
        index = 0;
vb@159
  1370
        r = mailmime_part_parse(text, length, &index, code, &_longmsg, &_size);
vb@147
  1371
        switch (r) {
vb@147
  1372
            case MAILIMF_NO_ERROR:
vb@147
  1373
                break;
vb@147
  1374
            case MAILIMF_ERROR_MEMORY:
vb@147
  1375
                return PEP_OUT_OF_MEMORY;
vb@147
  1376
            default:
vb@147
  1377
                return PEP_ILLEGAL_VALUE;
vb@147
  1378
        }
vb@147
  1379
    }
vb@147
  1380
    else {
vb@289
  1381
        _size = length + 1;
vb@289
  1382
        _longmsg = strndup(text, length);
vb@159
  1383
        if (_longmsg == NULL)
vb@147
  1384
            return PEP_OUT_OF_MEMORY;
vb@147
  1385
    }
vb@147
  1386
vb@159
  1387
    if (part->mm_content_type) {
vb@159
  1388
        if (_get_content_type(part->mm_content_type, &type, &charset) == 0) {
vb@160
  1389
            if (charset && strncasecmp(charset, "utf-8", 5) != 0) {
vb@159
  1390
                char * _text;
vb@159
  1391
                int r = charconv("utf-8", charset, _longmsg, _size, &_text);
vb@159
  1392
                switch (r) {
vb@159
  1393
                    case MAILIMF_NO_ERROR:
vb@159
  1394
                        break;
vb@159
  1395
                    case MAILIMF_ERROR_MEMORY:
vb@159
  1396
                        return PEP_OUT_OF_MEMORY;
vb@159
  1397
                    default:
vb@159
  1398
                        return PEP_ILLEGAL_VALUE;
vb@159
  1399
                }
vb@159
  1400
                free(_longmsg);
vb@159
  1401
                _longmsg = _text;
vb@159
  1402
            }
vb@159
  1403
        }
vb@159
  1404
    }
krista@1536
  1405
    // FIXME: KG - we now have the text we want.
krista@1536
  1406
    // Now we need to strip sigs and process them if they are there..
krista@1536
  1407
    
vb@159
  1408
vb@159
  1409
    *longmsg = _longmsg;
vb@159
  1410
    if (size)
vb@159
  1411
        *size = _size;
vb@159
  1412
vb@147
  1413
    return PEP_STATUS_OK;
vb@147
  1414
}
vb@147
  1415
vb@118
  1416
static PEP_STATUS interpret_MIME(
vb@118
  1417
        struct mailmime *mime,
vb@118
  1418
        message *msg
vb@118
  1419
    )
vb@118
  1420
{
vb@118
  1421
    PEP_STATUS status = PEP_STATUS_OK;
vb@118
  1422
vb@118
  1423
    assert(mime);
vb@118
  1424
    assert(msg);
vb@118
  1425
vb@118
  1426
    struct mailmime_content *content = mime->mm_content_type;
vb@118
  1427
    if (content) {
vb@152
  1428
        if (_is_multipart(content, "alternative")) {
vb@118
  1429
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1430
            if (partlist == NULL)
vb@118
  1431
                return PEP_ILLEGAL_VALUE;
vb@118
  1432
vb@118
  1433
            clistiter *cur;
vb@118
  1434
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@120
  1435
                struct mailmime *part = clist_content(cur);
vb@118
  1436
                if (part == NULL)
vb@118
  1437
                    return PEP_ILLEGAL_VALUE;
vb@118
  1438
vb@120
  1439
                content = part->mm_content_type;
vb@120
  1440
                assert(content);
vb@120
  1441
                if (content == NULL)
vb@120
  1442
                    return PEP_ILLEGAL_VALUE;
vb@118
  1443
vb@152
  1444
                if (_is_text_part(content, "plain") && msg->longmsg == NULL) {
vb@152
  1445
                    status = interpret_body(part, &msg->longmsg, NULL);
vb@152
  1446
                    if (status)
vb@152
  1447
                        return status;
vb@152
  1448
                }
vb@152
  1449
                else if (_is_text_part(content, "html") &&
vb@152
  1450
                        msg->longmsg_formatted == NULL) {
vb@152
  1451
                    status = interpret_body(part, &msg->longmsg_formatted,
vb@152
  1452
                            NULL);
vb@152
  1453
                    if (status)
vb@152
  1454
                        return status;
vb@152
  1455
                }
vb@152
  1456
                else /* add as attachment */ {
vb@152
  1457
                    status = interpret_MIME(part, msg);
vb@152
  1458
                    if (status)
vb@152
  1459
                        return status;
vb@120
  1460
                }
vb@118
  1461
            }
vb@118
  1462
        }
vb@260
  1463
        else if (_is_multipart(content, "encrypted")) {
vb@260
  1464
            if (msg->longmsg == NULL)
vb@260
  1465
                msg->longmsg = strdup("");
vb@260
  1466
vb@260
  1467
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@260
  1468
            if (partlist == NULL)
vb@260
  1469
                return PEP_ILLEGAL_VALUE;
vb@260
  1470
vb@260
  1471
            clistiter *cur;
vb@260
  1472
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@260
  1473
                struct mailmime *part= clist_content(cur);
vb@260
  1474
                if (part == NULL)
vb@260
  1475
                    return PEP_ILLEGAL_VALUE;
vb@260
  1476
vb@260
  1477
                status = interpret_MIME(part, msg);
vb@260
  1478
                if (status != PEP_STATUS_OK)
vb@260
  1479
                    return status;
vb@260
  1480
            }
vb@260
  1481
        }
vb@152
  1482
        else if (_is_multipart(content, NULL)) {
vb@118
  1483
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1484
            if (partlist == NULL)
vb@118
  1485
                return PEP_ILLEGAL_VALUE;
vb@118
  1486
vb@118
  1487
            clistiter *cur;
vb@118
  1488
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@118
  1489
                struct mailmime *part= clist_content(cur);
vb@118
  1490
                if (part == NULL)
vb@118
  1491
                    return PEP_ILLEGAL_VALUE;
vb@118
  1492
vb@118
  1493
                status = interpret_MIME(part, msg);
vb@118
  1494
                if (status != PEP_STATUS_OK)
vb@118
  1495
                    return status;
vb@118
  1496
            }
vb@118
  1497
        }
vb@118
  1498
        else {
krista@1229
  1499
            if (_is_text_part(content, "html") &&
krista@1229
  1500
                msg->longmsg_formatted == NULL) {
krista@1229
  1501
                status = interpret_body(mime, &msg->longmsg_formatted,
krista@1229
  1502
                                        NULL);
krista@1229
  1503
                if (status)
krista@1229
  1504
                    return status;
krista@1229
  1505
            }
krista@1229
  1506
            else if (_is_text_part(content, NULL) && msg->longmsg == NULL) {
vb@152
  1507
                status = interpret_body(mime, &msg->longmsg, NULL);
vb@152
  1508
                if (status)
vb@152
  1509
                    return status;
vb@152
  1510
            }
vb@152
  1511
            else {
vb@152
  1512
                char *data = NULL;
vb@157
  1513
                size_t size = 0;
vb@157
  1514
                char * mime_type;
vb@159
  1515
                char * charset;
vb@157
  1516
                char * filename;
vb@159
  1517
                int r;
vb@118
  1518
vb@159
  1519
                r = _get_content_type(content, &mime_type, &charset);
vb@159
  1520
                switch (r) {
vb@159
  1521
                    case 0:
vb@159
  1522
                        break;
vb@159
  1523
                    case EINVAL:
vb@159
  1524
                        return PEP_ILLEGAL_VALUE;
vb@159
  1525
                    case ENOMEM:
vb@159
  1526
                        return PEP_OUT_OF_MEMORY;
vb@159
  1527
                    default:
vb@159
  1528
                        return PEP_UNKNOWN_ERROR;
vb@157
  1529
                }
vb@154
  1530
vb@159
  1531
                assert(mime_type);
vb@159
  1532
vb@152
  1533
                status = interpret_body(mime, &data, &size);
vb@152
  1534
                if (status)
vb@152
  1535
                    return status;
vb@152
  1536
krista@1873
  1537
                pEp_rid_list_t* resource_id_list = _get_resource_id_list(mime);
krista@1873
  1538
                pEp_rid_list_t* chosen_resource_id = choose_resource_id(resource_id_list);
krista@1873
  1539
                
krista@1873
  1540
                //filename = _get_filename_or_cid(mime);
vb@481
  1541
                char *_filename = NULL;
krista@1873
  1542
                
krista@1873
  1543
                if (chosen_resource_id) {
krista@1873
  1544
                    filename = chosen_resource_id->rid;
vb@481
  1545
                    size_t index = 0;
krista@1873
  1546
                    /* NOTA BENE */
krista@1873
  1547
                    /* The prefix we just added shouldn't be a problem - this is about decoding %XX (RFC 2392) */
krista@1873
  1548
                    /* If it becomes one, we have some MESSY fixing to do. :(                                  */
vb@481
  1549
                    r = mailmime_encoded_phrase_parse("utf-8", filename,
vb@481
  1550
                            strlen(filename), &index, "utf-8", &_filename);
krista@1872
  1551
                    if (r) {
vb@481
  1552
                        goto enomem;
krista@1872
  1553
                    }
krista@1873
  1554
                    char* file_prefix = NULL;
krista@1873
  1555
                    
krista@1873
  1556
                    /* in case there are others later */
krista@1873
  1557
                    switch (chosen_resource_id->rid_type) {
krista@1873
  1558
                        case PEP_RID_CID:
krista@1873
  1559
                            file_prefix = "cid";
krista@1873
  1560
                            break;
krista@1873
  1561
                        case PEP_RID_FILENAME:
krista@1873
  1562
                            file_prefix = "file";
krista@1873
  1563
                            break;
krista@1873
  1564
                        default:
krista@1873
  1565
                            break;
krista@1873
  1566
                    }
krista@1873
  1567
krista@1873
  1568
                    
krista@1873
  1569
                    if (file_prefix) {
krista@1881
  1570
                        filename = build_uri(file_prefix, _filename);
krista@1873
  1571
                        free(_filename);
krista@1873
  1572
                        _filename = filename;
krista@1873
  1573
                    }
vb@481
  1574
                }
vb@154
  1575
vb@260
  1576
                bloblist_t *_a = bloblist_add(msg->attachments, data, size,
krista@1871
  1577
                        mime_type, _filename);
vb@481
  1578
                free(_filename);
krista@1902
  1579
                free_rid_list(resource_id_list);
krista@1902
  1580
                resource_id_list = NULL;
vb@260
  1581
                if (_a == NULL)
vb@260
  1582
                    return PEP_OUT_OF_MEMORY;
vb@152
  1583
                if (msg->attachments == NULL)
vb@260
  1584
                    msg->attachments = _a;
vb@152
  1585
            }
vb@118
  1586
        }
vb@118
  1587
    }
vb@118
  1588
vb@118
  1589
    return PEP_STATUS_OK;
vb@481
  1590
vb@481
  1591
enomem:
vb@481
  1592
    return PEP_OUT_OF_MEMORY;
vb@118
  1593
}
vb@118
  1594
vb@89
  1595
DYNAMIC_API PEP_STATUS mime_decode_message(
vb@89
  1596
        const char *mimetext,
vb@269
  1597
        size_t size,
vb@89
  1598
        message **msg
vb@89
  1599
    )
vb@89
  1600
{
vb@89
  1601
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
  1602
    struct mailmime * mime = NULL;
vb@89
  1603
    int r;
vb@94
  1604
    message *_msg = NULL;
vb@94
  1605
    size_t index;
vb@89
  1606
vb@89
  1607
    assert(mimetext);
vb@89
  1608
    assert(msg);
vb@89
  1609
vb@191
  1610
    if (!(mimetext && msg))
vb@191
  1611
        return PEP_ILLEGAL_VALUE;
vb@191
  1612
vb@89
  1613
    *msg = NULL;
vb@113
  1614
vb@94
  1615
    index = 0;
vb@269
  1616
    r = mailmime_parse(mimetext, size, &index, &mime);
vb@89
  1617
    assert(r == 0);
vb@89
  1618
    assert(mime);
vb@90
  1619
    if (r) {
vb@89
  1620
        if (r == MAILIMF_ERROR_MEMORY)
vb@89
  1621
            goto enomem;
vb@89
  1622
        else
vb@89
  1623
            goto err_mime;
vb@89
  1624
    }
vb@89
  1625
vb@117
  1626
    _msg = calloc(1, sizeof(message));
vb@117
  1627
    assert(_msg);
vb@117
  1628
    if (_msg == NULL)
vb@117
  1629
        goto enomem;
vb@116
  1630
vb@147
  1631
    clist * _fieldlist = _get_fields(mime);
vb@147
  1632
    if (_fieldlist) {
vb@117
  1633
        status = read_fields(_msg, _fieldlist);
vb@116
  1634
        if (status != PEP_STATUS_OK)
vb@116
  1635
            goto pep_error;
vb@116
  1636
    }
vb@116
  1637
vb@147
  1638
    struct mailmime_content *content = _get_content(mime);
vb@117
  1639
vb@147
  1640
    if (content) {
vb@260
  1641
        status = interpret_MIME(mime->mm_data.mm_message.mm_msg_mime,
vb@260
  1642
                _msg);
vb@260
  1643
        if (status != PEP_STATUS_OK)
vb@260
  1644
            goto pep_error;
vb@116
  1645
    }
vb@116
  1646
vb@89
  1647
    mailmime_free(mime);
vb@94
  1648
    *msg = _msg;
vb@89
  1649
vb@89
  1650
    return status;
vb@89
  1651
vb@89
  1652
err_mime:
vb@89
  1653
    status = PEP_ILLEGAL_VALUE;
vb@89
  1654
    goto pep_error;
vb@89
  1655
vb@89
  1656
enomem:
vb@89
  1657
    status = PEP_OUT_OF_MEMORY;
vb@89
  1658
vb@89
  1659
pep_error:
vb@94
  1660
    free_message(_msg);
vb@94
  1661
vb@89
  1662
    if (mime)
vb@89
  1663
        mailmime_free(mime);
vb@89
  1664
vb@89
  1665
    return status;
vb@89
  1666
}