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