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