src/mime.c
author Edouard Tisserant <edouard@pep-project.org>
Fri, 09 Jun 2017 15:11:11 +0200
changeset 1844 415b5ea8bfd1
parent 1557 72aa4570d2c9
child 1684 20643edc4c53
child 1829 909cb53fc069
permissions -rw-r--r--
Tests : use $HOME to sandbox test`s management DB and keyrings
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
vb@114
   562
static PEP_STATUS mime_encode_message_plain(
vb@89
   563
        const message *msg,
vb@113
   564
        bool omit_fields,
vb@114
   565
        struct mailmime **result
vb@89
   566
    )
vb@89
   567
{
vb@89
   568
    struct mailmime * mime = NULL;
vb@89
   569
    struct mailmime * submime = NULL;
vb@89
   570
    int r;
vb@89
   571
    PEP_STATUS status;
roker@723
   572
    //char *subject;
vb@89
   573
    char *plaintext;
vb@89
   574
    char *htmltext;
vb@89
   575
vb@89
   576
    assert(msg);
vb@114
   577
    assert(result);
vb@112
   578
roker@723
   579
    //subject = (msg->shortmsg) ? msg->shortmsg : "pEp";  // not used, yet.
vb@89
   580
    plaintext = (msg->longmsg) ? msg->longmsg : "";
vb@89
   581
    htmltext = msg->longmsg_formatted;
vb@89
   582
krista@1197
   583
    if (htmltext && (htmltext[0] != '\0')) {
vb@89
   584
        status = mime_html_text(plaintext, htmltext, &mime);
vb@89
   585
        if (status != PEP_STATUS_OK)
vb@89
   586
            goto pep_error;
vb@89
   587
    }
vb@89
   588
    else {
vb@117
   589
        if (is_PGP_message_text(plaintext))
Edouard@426
   590
            mime = get_text_part("msg.asc", "application/octet-stream", plaintext,
vb@114
   591
                    strlen(plaintext), MAILMIME_MECHANISM_7BIT);
vb@114
   592
        else
vb@114
   593
            mime = get_text_part("msg.txt", "text/plain", plaintext, strlen(plaintext),
vb@114
   594
                    MAILMIME_MECHANISM_QUOTED_PRINTABLE);
vb@89
   595
        assert(mime);
vb@89
   596
        if (mime == NULL)
vb@89
   597
            goto enomem;
vb@89
   598
    }
vb@89
   599
vb@89
   600
    if (msg->attachments) {
vb@89
   601
        submime = mime;
vb@116
   602
        mime = part_multiple_new("multipart/mixed");
vb@89
   603
        assert(mime);
vb@89
   604
        if (mime == NULL)
vb@89
   605
            goto enomem;
vb@89
   606
vb@89
   607
        r = mailmime_smart_add_part(mime, submime);
vb@89
   608
        assert(r == MAILIMF_NO_ERROR);
vb@89
   609
        if (r == MAILIMF_ERROR_MEMORY) {
vb@89
   610
            goto enomem;
vb@89
   611
        }
vb@89
   612
        else {
vb@89
   613
            // mailmime_smart_add_part() takes ownership of submime
vb@89
   614
            submime = NULL;
vb@89
   615
        }
vb@89
   616
vb@89
   617
        bloblist_t *_a;
vb@89
   618
        for (_a = msg->attachments; _a != NULL; _a = _a->next) {
vb@89
   619
vb@89
   620
            status = mime_attachment(_a, &submime);
vb@89
   621
            if (status != PEP_STATUS_OK)
vb@89
   622
                goto pep_error;
vb@89
   623
vb@89
   624
            r = mailmime_smart_add_part(mime, submime);
vb@89
   625
            assert(r == MAILIMF_NO_ERROR);
vb@89
   626
            if (r == MAILIMF_ERROR_MEMORY) {
vb@89
   627
                goto enomem;
vb@89
   628
            }
vb@89
   629
            else {
vb@89
   630
                // mailmime_smart_add_part() takes ownership of submime
vb@89
   631
                submime = NULL;
vb@89
   632
            }
vb@89
   633
        }
vb@89
   634
    }
vb@89
   635
vb@114
   636
    *result = mime;
vb@114
   637
    return PEP_STATUS_OK;
vb@114
   638
vb@114
   639
enomem:
vb@114
   640
    status = PEP_OUT_OF_MEMORY;
vb@114
   641
vb@114
   642
pep_error:
vb@114
   643
    if (mime)
vb@114
   644
        mailmime_free(mime);
vb@114
   645
vb@114
   646
    if (submime)
vb@114
   647
        mailmime_free(submime);
vb@114
   648
vb@114
   649
    return status;
vb@114
   650
}
vb@114
   651
vb@114
   652
static PEP_STATUS mime_encode_message_PGP_MIME(
vb@114
   653
        const message * msg,
vb@114
   654
        bool omit_fields,
vb@114
   655
        struct mailmime **result
vb@114
   656
    )
vb@114
   657
{
vb@114
   658
    struct mailmime * mime = NULL;
vb@114
   659
    struct mailmime * submime = NULL;
vb@114
   660
	struct mailmime_parameter * param;
vb@114
   661
    int r;
vb@114
   662
    PEP_STATUS status;
roker@723
   663
    //char *subject;
vb@114
   664
    char *plaintext;
edouard@1274
   665
    size_t plaintext_size;
vb@114
   666
vb@260
   667
    assert(msg->attachments && msg->attachments->next &&
vb@301
   668
            msg->attachments->next->value);
vb@114
   669
roker@723
   670
    //subject = (msg->shortmsg) ? msg->shortmsg : "pEp"; // not used, yet.
vb@301
   671
    plaintext = msg->attachments->next->value;
edouard@1274
   672
    plaintext_size = msg->attachments->next->size;
vb@114
   673
vb@116
   674
    mime = part_multiple_new("multipart/encrypted");
vb@114
   675
    assert(mime);
vb@114
   676
    if (mime == NULL)
vb@114
   677
        goto enomem;
vb@114
   678
vb@114
   679
    param = mailmime_param_new_with_data("protocol", "application/pgp-encrypted");
vb@114
   680
    clist_append(mime->mm_content_type->ct_parameters, param);
vb@114
   681
vb@114
   682
    submime = get_pgp_encrypted_part();
vb@114
   683
    assert(submime);
vb@114
   684
    if (submime == NULL)
vb@114
   685
        goto enomem;
vb@114
   686
vb@114
   687
    r = mailmime_smart_add_part(mime, submime);
vb@114
   688
    assert(r == MAILIMF_NO_ERROR);
vb@114
   689
    if (r == MAILIMF_ERROR_MEMORY) {
vb@114
   690
        goto enomem;
vb@114
   691
    }
vb@114
   692
    else {
vb@114
   693
        // mailmime_smart_add_part() takes ownership of submime
vb@114
   694
        submime = NULL;
vb@114
   695
    }
vb@114
   696
Edouard@427
   697
    submime = get_text_part("msg.asc", "application/octet-stream", plaintext,
edouard@1274
   698
            plaintext_size, MAILMIME_MECHANISM_7BIT);
vb@114
   699
    assert(submime);
vb@114
   700
    if (submime == NULL)
vb@114
   701
        goto enomem;
vb@114
   702
vb@114
   703
    r = mailmime_smart_add_part(mime, submime);
vb@114
   704
    assert(r == MAILIMF_NO_ERROR);
vb@114
   705
    if (r == MAILIMF_ERROR_MEMORY) {
vb@114
   706
        goto enomem;
vb@114
   707
    }
vb@114
   708
    else {
vb@114
   709
        // mailmime_smart_add_part() takes ownership of submime
vb@114
   710
        submime = NULL;
vb@114
   711
    }
vb@114
   712
vb@114
   713
    *result = mime;
vb@114
   714
    return PEP_STATUS_OK;
vb@114
   715
vb@114
   716
enomem:
vb@114
   717
    status = PEP_OUT_OF_MEMORY;
vb@114
   718
vb@114
   719
    if (mime)
vb@114
   720
        mailmime_free(mime);
vb@114
   721
vb@114
   722
    if (submime)
vb@114
   723
        mailmime_free(submime);
vb@114
   724
vb@114
   725
    return status;
vb@114
   726
}
vb@114
   727
vb@114
   728
DYNAMIC_API PEP_STATUS mime_encode_message(
vb@114
   729
        const message * msg,
vb@114
   730
        bool omit_fields,
vb@114
   731
        char **mimetext
vb@114
   732
    )
vb@114
   733
{
vb@114
   734
    PEP_STATUS status = PEP_STATUS_OK;
vb@114
   735
    struct mailmime * msg_mime = NULL;
vb@114
   736
    struct mailmime * mime = NULL;
vb@114
   737
    struct mailimf_fields * fields = NULL;
vb@114
   738
    char *buf = NULL;
vb@114
   739
    int r;
vb@114
   740
vb@114
   741
    assert(msg);
vb@114
   742
    assert(mimetext);
vb@114
   743
vb@260
   744
    if (!(msg && mimetext))
vb@191
   745
        return PEP_ILLEGAL_VALUE;
vb@191
   746
vb@114
   747
    *mimetext = NULL;
vb@114
   748
vb@114
   749
    switch (msg->enc_format) {
vb@114
   750
        case PEP_enc_none:
vb@114
   751
            status = mime_encode_message_plain(msg, omit_fields, &mime);
vb@114
   752
            break;
vb@114
   753
vb@114
   754
        case PEP_enc_pieces:
vb@114
   755
            status = mime_encode_message_plain(msg, omit_fields, &mime);
vb@114
   756
            break;
vb@114
   757
vb@114
   758
        case PEP_enc_S_MIME:
vb@114
   759
            NOT_IMPLEMENTED
vb@114
   760
                
vb@114
   761
        case PEP_enc_PGP_MIME:
vb@114
   762
            status = mime_encode_message_PGP_MIME(msg, omit_fields, &mime);
vb@114
   763
            break;
vb@114
   764
vb@114
   765
        case PEP_enc_PEP:
vb@114
   766
            NOT_IMPLEMENTED
edouard@1557
   767
edouard@1557
   768
        default:
edouard@1557
   769
            NOT_IMPLEMENTED
vb@114
   770
    }
vb@114
   771
vb@114
   772
    if (status != PEP_STATUS_OK)
vb@114
   773
        goto pep_error;
vb@114
   774
vb@89
   775
    msg_mime = mailmime_new_message_data(NULL);
vb@90
   776
    assert(msg_mime);
vb@90
   777
    if (msg_mime == NULL)
vb@90
   778
        goto enomem;
vb@89
   779
vb@90
   780
    r = mailmime_add_part(msg_mime, mime);
vb@90
   781
    if (r) {
vb@90
   782
        mailmime_free(mime);
vb@90
   783
        goto enomem;
vb@90
   784
    }
vb@114
   785
    mime = NULL;
vb@90
   786
vb@113
   787
    if (!omit_fields) {
vb@113
   788
        status = build_fields(msg, &fields);
vb@113
   789
        if (status != PEP_STATUS_OK)
vb@113
   790
            goto pep_error;
vb@90
   791
vb@113
   792
        mailmime_set_imf_fields(msg_mime, fields);
vb@113
   793
    }
vb@90
   794
vb@90
   795
    status = render_mime(msg_mime, &buf);
vb@89
   796
    if (status != PEP_STATUS_OK)
vb@89
   797
        goto pep_error;
vb@89
   798
vb@89
   799
    mailmime_free(msg_mime);
vb@89
   800
    *mimetext = buf;
vb@90
   801
vb@89
   802
    return PEP_STATUS_OK;
vb@89
   803
vb@89
   804
enomem:
vb@89
   805
    status = PEP_OUT_OF_MEMORY;
vb@89
   806
vb@89
   807
pep_error:
vb@89
   808
    if (msg_mime)
vb@90
   809
        mailmime_free(msg_mime);
vb@89
   810
    else
vb@89
   811
        if (mime)
vb@89
   812
            mailmime_free(mime);
vb@89
   813
vb@89
   814
    return status;
vb@89
   815
}
vb@89
   816
vb@94
   817
static pEp_identity *mailbox_to_identity(const struct mailimf_mailbox * mb)
vb@94
   818
{
vb@94
   819
    char *username = NULL;
vb@94
   820
vb@159
   821
    assert(mb);
vb@159
   822
    assert(mb->mb_addr_spec);
vb@159
   823
vb@159
   824
    if (mb->mb_addr_spec == NULL)
vb@159
   825
        return NULL;
vb@159
   826
vb@159
   827
    if (mb->mb_display_name) {
roker@871
   828
        size_t index = 0;
roker@871
   829
        const int r = mailmime_encoded_phrase_parse("utf-8", mb->mb_display_name,
vb@159
   830
                strlen(mb->mb_display_name), &index, "utf-8", &username);
vb@159
   831
        if (r)
vb@159
   832
            goto enomem;
vb@159
   833
    }
vb@94
   834
roker@871
   835
    pEp_identity *ident = new_identity(mb->mb_addr_spec, NULL, NULL, username);
vb@94
   836
    if (ident == NULL)
vb@94
   837
        goto enomem;
vb@94
   838
    free(username);
vb@94
   839
vb@94
   840
    return ident;
vb@94
   841
vb@94
   842
enomem:
vb@94
   843
    free(username);
vb@94
   844
    return NULL;
vb@94
   845
}
vb@94
   846
vb@94
   847
static pEp_identity * mbl_to_identity(const struct mailimf_mailbox_list * mbl)
vb@94
   848
{
vb@94
   849
    struct mailimf_mailbox * mb = clist_content(clist_begin(mbl->mb_list));
vb@94
   850
    return mailbox_to_identity(mb);
vb@94
   851
}
vb@94
   852
vb@94
   853
static identity_list * mal_to_identity_list(
vb@94
   854
        const struct mailimf_address_list *mal
vb@94
   855
    )
vb@94
   856
{
roker@871
   857
    assert(mal);
vb@94
   858
    clist *list = mal->ad_list;
vb@94
   859
roker@871
   860
    identity_list *il = new_identity_list(NULL);
vb@94
   861
    if (il == NULL)
vb@94
   862
        goto enomem;
vb@94
   863
vb@94
   864
    identity_list *_il = il;
roker@871
   865
    for (clistiter *cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@94
   866
        pEp_identity *ident;
vb@94
   867
roker@871
   868
        struct mailimf_address *addr = clist_content(cur);
vb@94
   869
        switch(addr->ad_type) {
vb@94
   870
            case MAILIMF_ADDRESS_MAILBOX:
vb@94
   871
                ident = mailbox_to_identity(addr->ad_data.ad_mailbox);
vb@94
   872
                if (ident == NULL)
vb@94
   873
                    goto enomem;
vb@94
   874
                _il = identity_list_add(_il, ident);
vb@94
   875
                if (_il == NULL)
vb@94
   876
                    goto enomem;
vb@94
   877
                break;
vb@94
   878
vb@94
   879
            case MAILIMF_ADDRESS_GROUP:
vb@94
   880
                {
vb@94
   881
                    struct mailimf_mailbox_list * mbl =
vb@94
   882
                            addr->ad_data.ad_group->grp_mb_list;
roker@871
   883
                    for (clistiter *cur2 = clist_begin(mbl->mb_list); cur2 != NULL;
vb@94
   884
                            cur2 = clist_next(cur2)) {
vb@94
   885
                        ident = mailbox_to_identity(clist_content(cur));
vb@94
   886
                        if (ident == NULL)
vb@94
   887
                            goto enomem;
vb@94
   888
                        _il = identity_list_add(_il, ident);
vb@94
   889
                        if (_il == NULL)
vb@94
   890
                            goto enomem;
vb@94
   891
                    }
vb@94
   892
                }
vb@94
   893
                break;
vb@94
   894
vb@94
   895
            default:
vb@94
   896
                assert(0);
vb@94
   897
                goto enomem;
vb@94
   898
        }
vb@94
   899
    }
vb@94
   900
vb@94
   901
    return il;
vb@94
   902
vb@94
   903
enomem:
vb@94
   904
    free_identity_list(il);
vb@94
   905
    return NULL;
vb@94
   906
}
vb@94
   907
vb@94
   908
static stringlist_t * clist_to_stringlist(const clist *list)
vb@94
   909
{
vb@94
   910
    char *text = NULL;;
vb@94
   911
    stringlist_t * sl = new_stringlist(NULL);
vb@94
   912
    if (sl == NULL)
vb@94
   913
        return NULL;
vb@94
   914
vb@94
   915
    stringlist_t *_sl = sl;
roker@871
   916
    for (clistiter *cur = clist_begin(list); cur != NULL; cur = clist_next(cur)) {
vb@94
   917
        char *phrase = clist_content(cur);
roker@871
   918
        size_t index = 0;
roker@871
   919
        
roker@871
   920
        const int r = mailmime_encoded_phrase_parse("utf-8", phrase, strlen(phrase),
vb@94
   921
                &index, "utf-8", &text);
vb@94
   922
        if (r)
vb@94
   923
            goto enomem;
vb@94
   924
vb@94
   925
        _sl = stringlist_add(_sl, text);
vb@94
   926
        if (_sl == NULL)
vb@94
   927
            goto enomem;
vb@94
   928
vb@94
   929
        free(text);
vb@94
   930
        text = NULL;
vb@94
   931
    }
vb@94
   932
vb@94
   933
    return _sl;
vb@94
   934
vb@94
   935
enomem:
vb@94
   936
    free_stringlist(sl);
vb@94
   937
    free(text);
vb@94
   938
vb@94
   939
    return NULL;
vb@94
   940
}
vb@94
   941
vb@94
   942
static PEP_STATUS read_fields(message *msg, clist *fieldlist)
vb@94
   943
{
vb@94
   944
    PEP_STATUS status = PEP_STATUS_OK;
vb@94
   945
    struct mailimf_field * _field;
vb@94
   946
    clistiter *cur;
vb@94
   947
    size_t index;
vb@94
   948
    int r;
vb@110
   949
    stringpair_list_t *opt = msg->opt_fields;
vb@94
   950
vb@94
   951
    for (cur = clist_begin(fieldlist); cur != NULL; cur = clist_next(cur)) {
vb@94
   952
        _field = clist_content(cur);
vb@94
   953
vb@94
   954
        switch (_field->fld_type) {
vb@94
   955
            case MAILIMF_FIELD_MESSAGE_ID:
vb@94
   956
                {
vb@94
   957
                    char * text = _field->fld_data.fld_message_id->mid_value;
vb@116
   958
vb@116
   959
                    free(msg->id);
vb@94
   960
                    index = 0;
vb@94
   961
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
   962
                            strlen(text), &index, "utf-8", &msg->id);
vb@94
   963
                    if (r)
vb@94
   964
                        goto enomem;
vb@94
   965
                }
vb@94
   966
                break;
vb@94
   967
vb@94
   968
            case MAILIMF_FIELD_SUBJECT:
vb@94
   969
                {
vb@94
   970
                    char * text = _field->fld_data.fld_subject->sbj_value;
vb@116
   971
vb@116
   972
                    free(msg->shortmsg);
vb@94
   973
                    index = 0;
vb@94
   974
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
   975
                            strlen(text), &index, "utf-8", &msg->shortmsg);
vb@94
   976
                    if (r)
vb@94
   977
                        goto enomem;
vb@94
   978
                }
vb@94
   979
                break;
vb@94
   980
vb@94
   981
            case MAILIMF_FIELD_ORIG_DATE:
vb@94
   982
                {
vb@94
   983
                    struct mailimf_date_time *date =
vb@94
   984
                        _field->fld_data.fld_orig_date->dt_date_time;
vb@116
   985
vb@116
   986
                    free_timestamp(msg->sent);
vb@94
   987
                    msg->sent = etpantime_to_timestamp(date);
vb@94
   988
                    if (msg->sent == NULL)
vb@94
   989
                        goto enomem;
vb@94
   990
                }
vb@94
   991
                break;
vb@94
   992
vb@94
   993
            case MAILIMF_FIELD_FROM:
vb@94
   994
                {
vb@94
   995
                    struct mailimf_mailbox_list *mbl =
vb@94
   996
                            _field->fld_data.fld_from->frm_mb_list;
vb@94
   997
                    pEp_identity *ident;
vb@94
   998
vb@94
   999
                    ident = mbl_to_identity(mbl);
vb@94
  1000
                    if (ident == NULL)
vb@94
  1001
                        goto pep_error;
vb@94
  1002
vb@116
  1003
                    free_identity(msg->from);
vb@94
  1004
                    msg->from = ident;
vb@94
  1005
                }
vb@94
  1006
                break;
vb@94
  1007
vb@94
  1008
            case MAILIMF_FIELD_TO:
vb@94
  1009
                {
vb@94
  1010
                    struct mailimf_address_list *mal =
vb@94
  1011
                            _field->fld_data.fld_to->to_addr_list;
vb@94
  1012
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1013
                    if (il == NULL)
vb@94
  1014
                        goto enomem;
vb@116
  1015
vb@116
  1016
                    free_identity_list(msg->to);
vb@94
  1017
                    msg->to = il;
vb@94
  1018
                }
vb@94
  1019
                break;
vb@94
  1020
vb@94
  1021
            case MAILIMF_FIELD_CC:
vb@94
  1022
                {
vb@94
  1023
                    struct mailimf_address_list *mal =
vb@94
  1024
                            _field->fld_data.fld_cc->cc_addr_list;
vb@94
  1025
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1026
                    if (il == NULL)
vb@94
  1027
                        goto enomem;
vb@116
  1028
vb@116
  1029
                    free_identity_list(msg->cc);
vb@94
  1030
                    msg->cc = il;
vb@94
  1031
                }
vb@94
  1032
                break;
vb@94
  1033
vb@94
  1034
            case MAILIMF_FIELD_BCC:
vb@94
  1035
                {
vb@94
  1036
                    struct mailimf_address_list *mal =
vb@94
  1037
                            _field->fld_data.fld_bcc->bcc_addr_list;
vb@94
  1038
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1039
                    if (il == NULL)
vb@94
  1040
                        goto enomem;
vb@116
  1041
vb@116
  1042
                    free_identity_list(msg->bcc);
vb@94
  1043
                    msg->bcc = il;
vb@94
  1044
                }
vb@94
  1045
                break;
vb@94
  1046
vb@94
  1047
            case MAILIMF_FIELD_REPLY_TO:
vb@94
  1048
                {
vb@94
  1049
                    struct mailimf_address_list *mal =
vb@94
  1050
                            _field->fld_data.fld_reply_to->rt_addr_list;
vb@94
  1051
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1052
                    if (il == NULL)
vb@94
  1053
                        goto enomem;
vb@116
  1054
vb@116
  1055
                    free_identity_list(msg->reply_to);
vb@94
  1056
                    msg->reply_to = il;
vb@94
  1057
                }
vb@94
  1058
                break;
vb@94
  1059
vb@94
  1060
            case MAILIMF_FIELD_IN_REPLY_TO:
vb@94
  1061
                {
vb@94
  1062
                    clist *list = _field->fld_data.fld_in_reply_to->mid_list;
vb@94
  1063
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1064
                    if (sl == NULL)
vb@94
  1065
                        goto enomem;
vb@116
  1066
vb@116
  1067
                    free_stringlist(msg->in_reply_to);
vb@94
  1068
                    msg->in_reply_to = sl;
vb@94
  1069
                }
vb@94
  1070
                break;
vb@94
  1071
vb@94
  1072
            case MAILIMF_FIELD_REFERENCES:
vb@94
  1073
                {
vb@94
  1074
                    clist *list = _field->fld_data.fld_references->mid_list;
vb@94
  1075
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1076
                    if (sl == NULL)
vb@94
  1077
                        goto enomem;
vb@116
  1078
vb@116
  1079
                    free_stringlist(msg->references);
vb@94
  1080
                    msg->references = sl;
vb@94
  1081
                }
vb@94
  1082
                break;
vb@94
  1083
vb@94
  1084
            case MAILIMF_FIELD_KEYWORDS:
vb@94
  1085
                {
vb@94
  1086
                    clist *list = _field->fld_data.fld_keywords->kw_list;
vb@94
  1087
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1088
                    if (sl == NULL)
vb@94
  1089
                        goto enomem;
vb@116
  1090
vb@116
  1091
                    free_stringlist(msg->keywords);
vb@94
  1092
                    msg->keywords = sl;
vb@94
  1093
                }
vb@94
  1094
                break;
vb@94
  1095
vb@94
  1096
            case MAILIMF_FIELD_COMMENTS:
vb@94
  1097
                {
vb@94
  1098
                    char * text = _field->fld_data.fld_comments->cm_value;
vb@116
  1099
vb@116
  1100
                    free(msg->comments);
vb@94
  1101
                    index = 0;
vb@94
  1102
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1103
                            strlen(text), &index, "utf-8", &msg->comments);
vb@94
  1104
                    if (r)
vb@94
  1105
                        goto enomem;
vb@94
  1106
                }
vb@94
  1107
                break;
vb@110
  1108
vb@110
  1109
            case MAILIMF_FIELD_OPTIONAL_FIELD:
vb@110
  1110
                {
vb@110
  1111
                    char * name =
vb@110
  1112
                            _field->fld_data.fld_optional_field->fld_name;
vb@110
  1113
                    char * value =
vb@110
  1114
                            _field->fld_data.fld_optional_field->fld_value;
vb@110
  1115
                    char *_value;
vb@110
  1116
vb@110
  1117
                    index = 0;
vb@110
  1118
                    r = mailmime_encoded_phrase_parse("utf-8", value,
vb@110
  1119
                            strlen(value), &index, "utf-8", &_value);
vb@110
  1120
                    if (r)
vb@110
  1121
                        goto enomem;
vb@110
  1122
vb@281
  1123
                    stringpair_t *pair = new_stringpair(name, _value);
vb@281
  1124
                    if (pair == NULL)
vb@281
  1125
                        goto enomem;
vb@110
  1126
vb@281
  1127
                    opt = stringpair_list_add(opt, pair);
vb@110
  1128
                    free(_value);
vb@110
  1129
                    if (opt == NULL)
vb@110
  1130
                        goto enomem;
vb@110
  1131
vb@110
  1132
                    if (msg->opt_fields == NULL)
vb@110
  1133
                        msg->opt_fields = opt;
vb@110
  1134
                }
vb@110
  1135
                break;
vb@94
  1136
        }
vb@94
  1137
    }
vb@94
  1138
vb@94
  1139
    return PEP_STATUS_OK;
vb@94
  1140
vb@94
  1141
enomem:
vb@94
  1142
    status = PEP_OUT_OF_MEMORY;
vb@94
  1143
vb@94
  1144
pep_error:
vb@94
  1145
    return status;
vb@94
  1146
}
vb@94
  1147
vb@147
  1148
static PEP_STATUS interpret_body(struct mailmime *part, char **longmsg, size_t *size)
vb@147
  1149
{
vb@147
  1150
    const char *text;
vb@159
  1151
    char *_longmsg;
vb@147
  1152
    size_t length;
vb@159
  1153
    size_t _size;
vb@147
  1154
    int code;
vb@147
  1155
    int r;
vb@147
  1156
    size_t index;
vb@159
  1157
    char *type = NULL;
vb@159
  1158
    char *charset = NULL;
vb@147
  1159
vb@147
  1160
    assert(part);
vb@147
  1161
    assert(longmsg);
vb@147
  1162
vb@147
  1163
    *longmsg = NULL;
vb@147
  1164
    if (size)
vb@147
  1165
        *size = 0;
vb@147
  1166
vb@147
  1167
    if (part->mm_body == NULL)
vb@147
  1168
        return PEP_ILLEGAL_VALUE;
vb@147
  1169
vb@147
  1170
    text = part->mm_body-> dt_data.dt_text.dt_data;
vb@147
  1171
    if (text == NULL)
vb@147
  1172
        return PEP_ILLEGAL_VALUE;
vb@147
  1173
vb@147
  1174
    length = part->mm_body->dt_data.dt_text.dt_length;
vb@147
  1175
vb@147
  1176
    if (part->mm_body->dt_encoded) {
vb@147
  1177
        code = part->mm_body->dt_encoding;
vb@147
  1178
        index = 0;
vb@159
  1179
        r = mailmime_part_parse(text, length, &index, code, &_longmsg, &_size);
vb@147
  1180
        switch (r) {
vb@147
  1181
            case MAILIMF_NO_ERROR:
vb@147
  1182
                break;
vb@147
  1183
            case MAILIMF_ERROR_MEMORY:
vb@147
  1184
                return PEP_OUT_OF_MEMORY;
vb@147
  1185
            default:
vb@147
  1186
                return PEP_ILLEGAL_VALUE;
vb@147
  1187
        }
vb@147
  1188
    }
vb@147
  1189
    else {
vb@289
  1190
        _size = length + 1;
vb@289
  1191
        _longmsg = strndup(text, length);
vb@159
  1192
        if (_longmsg == NULL)
vb@147
  1193
            return PEP_OUT_OF_MEMORY;
vb@147
  1194
    }
vb@147
  1195
vb@159
  1196
    if (part->mm_content_type) {
vb@159
  1197
        if (_get_content_type(part->mm_content_type, &type, &charset) == 0) {
vb@160
  1198
            if (charset && strncasecmp(charset, "utf-8", 5) != 0) {
vb@159
  1199
                char * _text;
vb@159
  1200
                int r = charconv("utf-8", charset, _longmsg, _size, &_text);
vb@159
  1201
                switch (r) {
vb@159
  1202
                    case MAILIMF_NO_ERROR:
vb@159
  1203
                        break;
vb@159
  1204
                    case MAILIMF_ERROR_MEMORY:
vb@159
  1205
                        return PEP_OUT_OF_MEMORY;
vb@159
  1206
                    default:
vb@159
  1207
                        return PEP_ILLEGAL_VALUE;
vb@159
  1208
                }
vb@159
  1209
                free(_longmsg);
vb@159
  1210
                _longmsg = _text;
vb@159
  1211
            }
vb@159
  1212
        }
vb@159
  1213
    }
krista@1536
  1214
    // FIXME: KG - we now have the text we want.
krista@1536
  1215
    // Now we need to strip sigs and process them if they are there..
krista@1536
  1216
    
vb@159
  1217
vb@159
  1218
    *longmsg = _longmsg;
vb@159
  1219
    if (size)
vb@159
  1220
        *size = _size;
vb@159
  1221
vb@147
  1222
    return PEP_STATUS_OK;
vb@147
  1223
}
vb@147
  1224
vb@118
  1225
static PEP_STATUS interpret_MIME(
vb@118
  1226
        struct mailmime *mime,
vb@118
  1227
        message *msg
vb@118
  1228
    )
vb@118
  1229
{
vb@118
  1230
    PEP_STATUS status = PEP_STATUS_OK;
vb@118
  1231
vb@118
  1232
    assert(mime);
vb@118
  1233
    assert(msg);
vb@118
  1234
vb@118
  1235
    struct mailmime_content *content = mime->mm_content_type;
vb@118
  1236
    if (content) {
vb@152
  1237
        if (_is_multipart(content, "alternative")) {
vb@118
  1238
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1239
            if (partlist == NULL)
vb@118
  1240
                return PEP_ILLEGAL_VALUE;
vb@118
  1241
vb@118
  1242
            clistiter *cur;
vb@118
  1243
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@120
  1244
                struct mailmime *part = clist_content(cur);
vb@118
  1245
                if (part == NULL)
vb@118
  1246
                    return PEP_ILLEGAL_VALUE;
vb@118
  1247
vb@120
  1248
                content = part->mm_content_type;
vb@120
  1249
                assert(content);
vb@120
  1250
                if (content == NULL)
vb@120
  1251
                    return PEP_ILLEGAL_VALUE;
vb@118
  1252
vb@152
  1253
                if (_is_text_part(content, "plain") && msg->longmsg == NULL) {
vb@152
  1254
                    status = interpret_body(part, &msg->longmsg, NULL);
vb@152
  1255
                    if (status)
vb@152
  1256
                        return status;
vb@152
  1257
                }
vb@152
  1258
                else if (_is_text_part(content, "html") &&
vb@152
  1259
                        msg->longmsg_formatted == NULL) {
vb@152
  1260
                    status = interpret_body(part, &msg->longmsg_formatted,
vb@152
  1261
                            NULL);
vb@152
  1262
                    if (status)
vb@152
  1263
                        return status;
vb@152
  1264
                }
vb@152
  1265
                else /* add as attachment */ {
vb@152
  1266
                    status = interpret_MIME(part, msg);
vb@152
  1267
                    if (status)
vb@152
  1268
                        return status;
vb@120
  1269
                }
vb@118
  1270
            }
vb@118
  1271
        }
vb@260
  1272
        else if (_is_multipart(content, "encrypted")) {
vb@260
  1273
            if (msg->longmsg == NULL)
vb@260
  1274
                msg->longmsg = strdup("");
vb@260
  1275
vb@260
  1276
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@260
  1277
            if (partlist == NULL)
vb@260
  1278
                return PEP_ILLEGAL_VALUE;
vb@260
  1279
vb@260
  1280
            clistiter *cur;
vb@260
  1281
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@260
  1282
                struct mailmime *part= clist_content(cur);
vb@260
  1283
                if (part == NULL)
vb@260
  1284
                    return PEP_ILLEGAL_VALUE;
vb@260
  1285
vb@260
  1286
                status = interpret_MIME(part, msg);
vb@260
  1287
                if (status != PEP_STATUS_OK)
vb@260
  1288
                    return status;
vb@260
  1289
            }
vb@260
  1290
        }
vb@152
  1291
        else if (_is_multipart(content, NULL)) {
vb@118
  1292
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1293
            if (partlist == NULL)
vb@118
  1294
                return PEP_ILLEGAL_VALUE;
vb@118
  1295
vb@118
  1296
            clistiter *cur;
vb@118
  1297
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@118
  1298
                struct mailmime *part= clist_content(cur);
vb@118
  1299
                if (part == NULL)
vb@118
  1300
                    return PEP_ILLEGAL_VALUE;
vb@118
  1301
vb@118
  1302
                status = interpret_MIME(part, msg);
vb@118
  1303
                if (status != PEP_STATUS_OK)
vb@118
  1304
                    return status;
vb@118
  1305
            }
vb@118
  1306
        }
vb@118
  1307
        else {
krista@1229
  1308
            if (_is_text_part(content, "html") &&
krista@1229
  1309
                msg->longmsg_formatted == NULL) {
krista@1229
  1310
                status = interpret_body(mime, &msg->longmsg_formatted,
krista@1229
  1311
                                        NULL);
krista@1229
  1312
                if (status)
krista@1229
  1313
                    return status;
krista@1229
  1314
            }
krista@1229
  1315
            else if (_is_text_part(content, NULL) && msg->longmsg == NULL) {
vb@152
  1316
                status = interpret_body(mime, &msg->longmsg, NULL);
vb@152
  1317
                if (status)
vb@152
  1318
                    return status;
vb@152
  1319
            }
vb@152
  1320
            else {
vb@152
  1321
                char *data = NULL;
vb@157
  1322
                size_t size = 0;
vb@157
  1323
                char * mime_type;
vb@159
  1324
                char * charset;
vb@157
  1325
                char * filename;
vb@159
  1326
                int r;
vb@118
  1327
vb@159
  1328
                r = _get_content_type(content, &mime_type, &charset);
vb@159
  1329
                switch (r) {
vb@159
  1330
                    case 0:
vb@159
  1331
                        break;
vb@159
  1332
                    case EINVAL:
vb@159
  1333
                        return PEP_ILLEGAL_VALUE;
vb@159
  1334
                    case ENOMEM:
vb@159
  1335
                        return PEP_OUT_OF_MEMORY;
vb@159
  1336
                    default:
vb@159
  1337
                        return PEP_UNKNOWN_ERROR;
vb@157
  1338
                }
vb@154
  1339
vb@159
  1340
                assert(mime_type);
vb@159
  1341
vb@152
  1342
                status = interpret_body(mime, &data, &size);
vb@152
  1343
                if (status)
vb@152
  1344
                    return status;
vb@152
  1345
vb@154
  1346
                filename = _get_filename(mime);
vb@481
  1347
                char *_filename = NULL;
vb@481
  1348
                if (filename) {
vb@481
  1349
                    size_t index = 0;
vb@481
  1350
                    r = mailmime_encoded_phrase_parse("utf-8", filename,
vb@481
  1351
                            strlen(filename), &index, "utf-8", &_filename);
vb@481
  1352
                    if (r)
vb@481
  1353
                        goto enomem;
vb@481
  1354
                }
vb@154
  1355
vb@260
  1356
                bloblist_t *_a = bloblist_add(msg->attachments, data, size,
vb@481
  1357
                        mime_type, _filename);
vb@481
  1358
                free(_filename);
vb@260
  1359
                if (_a == NULL)
vb@260
  1360
                    return PEP_OUT_OF_MEMORY;
vb@152
  1361
                if (msg->attachments == NULL)
vb@260
  1362
                    msg->attachments = _a;
vb@152
  1363
            }
vb@118
  1364
        }
vb@118
  1365
    }
vb@118
  1366
vb@118
  1367
    return PEP_STATUS_OK;
vb@481
  1368
vb@481
  1369
enomem:
vb@481
  1370
    return PEP_OUT_OF_MEMORY;
vb@118
  1371
}
vb@118
  1372
vb@89
  1373
DYNAMIC_API PEP_STATUS mime_decode_message(
vb@89
  1374
        const char *mimetext,
vb@269
  1375
        size_t size,
vb@89
  1376
        message **msg
vb@89
  1377
    )
vb@89
  1378
{
vb@89
  1379
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
  1380
    struct mailmime * mime = NULL;
vb@89
  1381
    int r;
vb@94
  1382
    message *_msg = NULL;
vb@94
  1383
    size_t index;
vb@89
  1384
vb@89
  1385
    assert(mimetext);
vb@89
  1386
    assert(msg);
vb@89
  1387
vb@191
  1388
    if (!(mimetext && msg))
vb@191
  1389
        return PEP_ILLEGAL_VALUE;
vb@191
  1390
vb@89
  1391
    *msg = NULL;
vb@113
  1392
vb@94
  1393
    index = 0;
vb@269
  1394
    r = mailmime_parse(mimetext, size, &index, &mime);
vb@89
  1395
    assert(r == 0);
vb@89
  1396
    assert(mime);
vb@90
  1397
    if (r) {
vb@89
  1398
        if (r == MAILIMF_ERROR_MEMORY)
vb@89
  1399
            goto enomem;
vb@89
  1400
        else
vb@89
  1401
            goto err_mime;
vb@89
  1402
    }
vb@89
  1403
vb@117
  1404
    _msg = calloc(1, sizeof(message));
vb@117
  1405
    assert(_msg);
vb@117
  1406
    if (_msg == NULL)
vb@117
  1407
        goto enomem;
vb@116
  1408
vb@147
  1409
    clist * _fieldlist = _get_fields(mime);
vb@147
  1410
    if (_fieldlist) {
vb@117
  1411
        status = read_fields(_msg, _fieldlist);
vb@116
  1412
        if (status != PEP_STATUS_OK)
vb@116
  1413
            goto pep_error;
vb@116
  1414
    }
vb@116
  1415
vb@147
  1416
    struct mailmime_content *content = _get_content(mime);
vb@117
  1417
vb@147
  1418
    if (content) {
vb@260
  1419
        status = interpret_MIME(mime->mm_data.mm_message.mm_msg_mime,
vb@260
  1420
                _msg);
vb@260
  1421
        if (status != PEP_STATUS_OK)
vb@260
  1422
            goto pep_error;
vb@116
  1423
    }
vb@116
  1424
vb@89
  1425
    mailmime_free(mime);
vb@94
  1426
    *msg = _msg;
vb@89
  1427
vb@89
  1428
    return status;
vb@89
  1429
vb@89
  1430
err_mime:
vb@89
  1431
    status = PEP_ILLEGAL_VALUE;
vb@89
  1432
    goto pep_error;
vb@89
  1433
vb@89
  1434
enomem:
vb@89
  1435
    status = PEP_OUT_OF_MEMORY;
vb@89
  1436
vb@89
  1437
pep_error:
vb@94
  1438
    free_message(_msg);
vb@94
  1439
vb@89
  1440
    if (mime)
vb@89
  1441
        mailmime_free(mime);
vb@89
  1442
vb@89
  1443
    return status;
vb@89
  1444
}