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