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