src/mime.c
author Krista Grothoff <krista@pep-project.org>
Fri, 13 Jan 2017 15:44:08 +0100
changeset 1536 8efaa23410e0
parent 1513 e7f7e42385b5
child 1557 72aa4570d2c9
permissions -rw-r--r--
ENGINE-1: first try at cleartext sigs, plus posteo now decrypts (ENGINE-86)
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
vb@114
   767
    }
vb@114
   768
vb@114
   769
    if (status != PEP_STATUS_OK)
vb@114
   770
        goto pep_error;
vb@114
   771
vb@89
   772
    msg_mime = mailmime_new_message_data(NULL);
vb@90
   773
    assert(msg_mime);
vb@90
   774
    if (msg_mime == NULL)
vb@90
   775
        goto enomem;
vb@89
   776
vb@90
   777
    r = mailmime_add_part(msg_mime, mime);
vb@90
   778
    if (r) {
vb@90
   779
        mailmime_free(mime);
vb@90
   780
        goto enomem;
vb@90
   781
    }
vb@114
   782
    mime = NULL;
vb@90
   783
vb@113
   784
    if (!omit_fields) {
vb@113
   785
        status = build_fields(msg, &fields);
vb@113
   786
        if (status != PEP_STATUS_OK)
vb@113
   787
            goto pep_error;
vb@90
   788
vb@113
   789
        mailmime_set_imf_fields(msg_mime, fields);
vb@113
   790
    }
vb@90
   791
vb@90
   792
    status = render_mime(msg_mime, &buf);
vb@89
   793
    if (status != PEP_STATUS_OK)
vb@89
   794
        goto pep_error;
vb@89
   795
vb@89
   796
    mailmime_free(msg_mime);
vb@89
   797
    *mimetext = buf;
vb@90
   798
vb@89
   799
    return PEP_STATUS_OK;
vb@89
   800
vb@89
   801
enomem:
vb@89
   802
    status = PEP_OUT_OF_MEMORY;
vb@89
   803
vb@89
   804
pep_error:
vb@89
   805
    if (msg_mime)
vb@90
   806
        mailmime_free(msg_mime);
vb@89
   807
    else
vb@89
   808
        if (mime)
vb@89
   809
            mailmime_free(mime);
vb@89
   810
vb@89
   811
    return status;
vb@89
   812
}
vb@89
   813
vb@94
   814
static pEp_identity *mailbox_to_identity(const struct mailimf_mailbox * mb)
vb@94
   815
{
vb@94
   816
    char *username = NULL;
vb@94
   817
vb@159
   818
    assert(mb);
vb@159
   819
    assert(mb->mb_addr_spec);
vb@159
   820
vb@159
   821
    if (mb->mb_addr_spec == NULL)
vb@159
   822
        return NULL;
vb@159
   823
vb@159
   824
    if (mb->mb_display_name) {
roker@871
   825
        size_t index = 0;
roker@871
   826
        const int r = mailmime_encoded_phrase_parse("utf-8", mb->mb_display_name,
vb@159
   827
                strlen(mb->mb_display_name), &index, "utf-8", &username);
vb@159
   828
        if (r)
vb@159
   829
            goto enomem;
vb@159
   830
    }
vb@94
   831
roker@871
   832
    pEp_identity *ident = new_identity(mb->mb_addr_spec, NULL, NULL, username);
vb@94
   833
    if (ident == NULL)
vb@94
   834
        goto enomem;
vb@94
   835
    free(username);
vb@94
   836
vb@94
   837
    return ident;
vb@94
   838
vb@94
   839
enomem:
vb@94
   840
    free(username);
vb@94
   841
    return NULL;
vb@94
   842
}
vb@94
   843
vb@94
   844
static pEp_identity * mbl_to_identity(const struct mailimf_mailbox_list * mbl)
vb@94
   845
{
vb@94
   846
    struct mailimf_mailbox * mb = clist_content(clist_begin(mbl->mb_list));
vb@94
   847
    return mailbox_to_identity(mb);
vb@94
   848
}
vb@94
   849
vb@94
   850
static identity_list * mal_to_identity_list(
vb@94
   851
        const struct mailimf_address_list *mal
vb@94
   852
    )
vb@94
   853
{
roker@871
   854
    assert(mal);
vb@94
   855
    clist *list = mal->ad_list;
vb@94
   856
roker@871
   857
    identity_list *il = new_identity_list(NULL);
vb@94
   858
    if (il == NULL)
vb@94
   859
        goto enomem;
vb@94
   860
vb@94
   861
    identity_list *_il = il;
roker@871
   862
    for (clistiter *cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@94
   863
        pEp_identity *ident;
vb@94
   864
roker@871
   865
        struct mailimf_address *addr = clist_content(cur);
vb@94
   866
        switch(addr->ad_type) {
vb@94
   867
            case MAILIMF_ADDRESS_MAILBOX:
vb@94
   868
                ident = mailbox_to_identity(addr->ad_data.ad_mailbox);
vb@94
   869
                if (ident == NULL)
vb@94
   870
                    goto enomem;
vb@94
   871
                _il = identity_list_add(_il, ident);
vb@94
   872
                if (_il == NULL)
vb@94
   873
                    goto enomem;
vb@94
   874
                break;
vb@94
   875
vb@94
   876
            case MAILIMF_ADDRESS_GROUP:
vb@94
   877
                {
vb@94
   878
                    struct mailimf_mailbox_list * mbl =
vb@94
   879
                            addr->ad_data.ad_group->grp_mb_list;
roker@871
   880
                    for (clistiter *cur2 = clist_begin(mbl->mb_list); cur2 != NULL;
vb@94
   881
                            cur2 = clist_next(cur2)) {
vb@94
   882
                        ident = mailbox_to_identity(clist_content(cur));
vb@94
   883
                        if (ident == NULL)
vb@94
   884
                            goto enomem;
vb@94
   885
                        _il = identity_list_add(_il, ident);
vb@94
   886
                        if (_il == NULL)
vb@94
   887
                            goto enomem;
vb@94
   888
                    }
vb@94
   889
                }
vb@94
   890
                break;
vb@94
   891
vb@94
   892
            default:
vb@94
   893
                assert(0);
vb@94
   894
                goto enomem;
vb@94
   895
        }
vb@94
   896
    }
vb@94
   897
vb@94
   898
    return il;
vb@94
   899
vb@94
   900
enomem:
vb@94
   901
    free_identity_list(il);
vb@94
   902
    return NULL;
vb@94
   903
}
vb@94
   904
vb@94
   905
static stringlist_t * clist_to_stringlist(const clist *list)
vb@94
   906
{
vb@94
   907
    char *text = NULL;;
vb@94
   908
    stringlist_t * sl = new_stringlist(NULL);
vb@94
   909
    if (sl == NULL)
vb@94
   910
        return NULL;
vb@94
   911
vb@94
   912
    stringlist_t *_sl = sl;
roker@871
   913
    for (clistiter *cur = clist_begin(list); cur != NULL; cur = clist_next(cur)) {
vb@94
   914
        char *phrase = clist_content(cur);
roker@871
   915
        size_t index = 0;
roker@871
   916
        
roker@871
   917
        const int r = mailmime_encoded_phrase_parse("utf-8", phrase, strlen(phrase),
vb@94
   918
                &index, "utf-8", &text);
vb@94
   919
        if (r)
vb@94
   920
            goto enomem;
vb@94
   921
vb@94
   922
        _sl = stringlist_add(_sl, text);
vb@94
   923
        if (_sl == NULL)
vb@94
   924
            goto enomem;
vb@94
   925
vb@94
   926
        free(text);
vb@94
   927
        text = NULL;
vb@94
   928
    }
vb@94
   929
vb@94
   930
    return _sl;
vb@94
   931
vb@94
   932
enomem:
vb@94
   933
    free_stringlist(sl);
vb@94
   934
    free(text);
vb@94
   935
vb@94
   936
    return NULL;
vb@94
   937
}
vb@94
   938
vb@94
   939
static PEP_STATUS read_fields(message *msg, clist *fieldlist)
vb@94
   940
{
vb@94
   941
    PEP_STATUS status = PEP_STATUS_OK;
vb@94
   942
    struct mailimf_field * _field;
vb@94
   943
    clistiter *cur;
vb@94
   944
    size_t index;
vb@94
   945
    int r;
vb@110
   946
    stringpair_list_t *opt = msg->opt_fields;
vb@94
   947
vb@94
   948
    for (cur = clist_begin(fieldlist); cur != NULL; cur = clist_next(cur)) {
vb@94
   949
        _field = clist_content(cur);
vb@94
   950
vb@94
   951
        switch (_field->fld_type) {
vb@94
   952
            case MAILIMF_FIELD_MESSAGE_ID:
vb@94
   953
                {
vb@94
   954
                    char * text = _field->fld_data.fld_message_id->mid_value;
vb@116
   955
vb@116
   956
                    free(msg->id);
vb@94
   957
                    index = 0;
vb@94
   958
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
   959
                            strlen(text), &index, "utf-8", &msg->id);
vb@94
   960
                    if (r)
vb@94
   961
                        goto enomem;
vb@94
   962
                }
vb@94
   963
                break;
vb@94
   964
vb@94
   965
            case MAILIMF_FIELD_SUBJECT:
vb@94
   966
                {
vb@94
   967
                    char * text = _field->fld_data.fld_subject->sbj_value;
vb@116
   968
vb@116
   969
                    free(msg->shortmsg);
vb@94
   970
                    index = 0;
vb@94
   971
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
   972
                            strlen(text), &index, "utf-8", &msg->shortmsg);
vb@94
   973
                    if (r)
vb@94
   974
                        goto enomem;
vb@94
   975
                }
vb@94
   976
                break;
vb@94
   977
vb@94
   978
            case MAILIMF_FIELD_ORIG_DATE:
vb@94
   979
                {
vb@94
   980
                    struct mailimf_date_time *date =
vb@94
   981
                        _field->fld_data.fld_orig_date->dt_date_time;
vb@116
   982
vb@116
   983
                    free_timestamp(msg->sent);
vb@94
   984
                    msg->sent = etpantime_to_timestamp(date);
vb@94
   985
                    if (msg->sent == NULL)
vb@94
   986
                        goto enomem;
vb@94
   987
                }
vb@94
   988
                break;
vb@94
   989
vb@94
   990
            case MAILIMF_FIELD_FROM:
vb@94
   991
                {
vb@94
   992
                    struct mailimf_mailbox_list *mbl =
vb@94
   993
                            _field->fld_data.fld_from->frm_mb_list;
vb@94
   994
                    pEp_identity *ident;
vb@94
   995
vb@94
   996
                    ident = mbl_to_identity(mbl);
vb@94
   997
                    if (ident == NULL)
vb@94
   998
                        goto pep_error;
vb@94
   999
vb@116
  1000
                    free_identity(msg->from);
vb@94
  1001
                    msg->from = ident;
vb@94
  1002
                }
vb@94
  1003
                break;
vb@94
  1004
vb@94
  1005
            case MAILIMF_FIELD_TO:
vb@94
  1006
                {
vb@94
  1007
                    struct mailimf_address_list *mal =
vb@94
  1008
                            _field->fld_data.fld_to->to_addr_list;
vb@94
  1009
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1010
                    if (il == NULL)
vb@94
  1011
                        goto enomem;
vb@116
  1012
vb@116
  1013
                    free_identity_list(msg->to);
vb@94
  1014
                    msg->to = il;
vb@94
  1015
                }
vb@94
  1016
                break;
vb@94
  1017
vb@94
  1018
            case MAILIMF_FIELD_CC:
vb@94
  1019
                {
vb@94
  1020
                    struct mailimf_address_list *mal =
vb@94
  1021
                            _field->fld_data.fld_cc->cc_addr_list;
vb@94
  1022
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1023
                    if (il == NULL)
vb@94
  1024
                        goto enomem;
vb@116
  1025
vb@116
  1026
                    free_identity_list(msg->cc);
vb@94
  1027
                    msg->cc = il;
vb@94
  1028
                }
vb@94
  1029
                break;
vb@94
  1030
vb@94
  1031
            case MAILIMF_FIELD_BCC:
vb@94
  1032
                {
vb@94
  1033
                    struct mailimf_address_list *mal =
vb@94
  1034
                            _field->fld_data.fld_bcc->bcc_addr_list;
vb@94
  1035
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1036
                    if (il == NULL)
vb@94
  1037
                        goto enomem;
vb@116
  1038
vb@116
  1039
                    free_identity_list(msg->bcc);
vb@94
  1040
                    msg->bcc = il;
vb@94
  1041
                }
vb@94
  1042
                break;
vb@94
  1043
vb@94
  1044
            case MAILIMF_FIELD_REPLY_TO:
vb@94
  1045
                {
vb@94
  1046
                    struct mailimf_address_list *mal =
vb@94
  1047
                            _field->fld_data.fld_reply_to->rt_addr_list;
vb@94
  1048
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1049
                    if (il == NULL)
vb@94
  1050
                        goto enomem;
vb@116
  1051
vb@116
  1052
                    free_identity_list(msg->reply_to);
vb@94
  1053
                    msg->reply_to = il;
vb@94
  1054
                }
vb@94
  1055
                break;
vb@94
  1056
vb@94
  1057
            case MAILIMF_FIELD_IN_REPLY_TO:
vb@94
  1058
                {
vb@94
  1059
                    clist *list = _field->fld_data.fld_in_reply_to->mid_list;
vb@94
  1060
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1061
                    if (sl == NULL)
vb@94
  1062
                        goto enomem;
vb@116
  1063
vb@116
  1064
                    free_stringlist(msg->in_reply_to);
vb@94
  1065
                    msg->in_reply_to = sl;
vb@94
  1066
                }
vb@94
  1067
                break;
vb@94
  1068
vb@94
  1069
            case MAILIMF_FIELD_REFERENCES:
vb@94
  1070
                {
vb@94
  1071
                    clist *list = _field->fld_data.fld_references->mid_list;
vb@94
  1072
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1073
                    if (sl == NULL)
vb@94
  1074
                        goto enomem;
vb@116
  1075
vb@116
  1076
                    free_stringlist(msg->references);
vb@94
  1077
                    msg->references = sl;
vb@94
  1078
                }
vb@94
  1079
                break;
vb@94
  1080
vb@94
  1081
            case MAILIMF_FIELD_KEYWORDS:
vb@94
  1082
                {
vb@94
  1083
                    clist *list = _field->fld_data.fld_keywords->kw_list;
vb@94
  1084
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1085
                    if (sl == NULL)
vb@94
  1086
                        goto enomem;
vb@116
  1087
vb@116
  1088
                    free_stringlist(msg->keywords);
vb@94
  1089
                    msg->keywords = sl;
vb@94
  1090
                }
vb@94
  1091
                break;
vb@94
  1092
vb@94
  1093
            case MAILIMF_FIELD_COMMENTS:
vb@94
  1094
                {
vb@94
  1095
                    char * text = _field->fld_data.fld_comments->cm_value;
vb@116
  1096
vb@116
  1097
                    free(msg->comments);
vb@94
  1098
                    index = 0;
vb@94
  1099
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1100
                            strlen(text), &index, "utf-8", &msg->comments);
vb@94
  1101
                    if (r)
vb@94
  1102
                        goto enomem;
vb@94
  1103
                }
vb@94
  1104
                break;
vb@110
  1105
vb@110
  1106
            case MAILIMF_FIELD_OPTIONAL_FIELD:
vb@110
  1107
                {
vb@110
  1108
                    char * name =
vb@110
  1109
                            _field->fld_data.fld_optional_field->fld_name;
vb@110
  1110
                    char * value =
vb@110
  1111
                            _field->fld_data.fld_optional_field->fld_value;
vb@110
  1112
                    char *_value;
vb@110
  1113
vb@110
  1114
                    index = 0;
vb@110
  1115
                    r = mailmime_encoded_phrase_parse("utf-8", value,
vb@110
  1116
                            strlen(value), &index, "utf-8", &_value);
vb@110
  1117
                    if (r)
vb@110
  1118
                        goto enomem;
vb@110
  1119
vb@281
  1120
                    stringpair_t *pair = new_stringpair(name, _value);
vb@281
  1121
                    if (pair == NULL)
vb@281
  1122
                        goto enomem;
vb@110
  1123
vb@281
  1124
                    opt = stringpair_list_add(opt, pair);
vb@110
  1125
                    free(_value);
vb@110
  1126
                    if (opt == NULL)
vb@110
  1127
                        goto enomem;
vb@110
  1128
vb@110
  1129
                    if (msg->opt_fields == NULL)
vb@110
  1130
                        msg->opt_fields = opt;
vb@110
  1131
                }
vb@110
  1132
                break;
vb@94
  1133
        }
vb@94
  1134
    }
vb@94
  1135
vb@94
  1136
    return PEP_STATUS_OK;
vb@94
  1137
vb@94
  1138
enomem:
vb@94
  1139
    status = PEP_OUT_OF_MEMORY;
vb@94
  1140
vb@94
  1141
pep_error:
vb@94
  1142
    return status;
vb@94
  1143
}
vb@94
  1144
vb@147
  1145
static PEP_STATUS interpret_body(struct mailmime *part, char **longmsg, size_t *size)
vb@147
  1146
{
vb@147
  1147
    const char *text;
vb@159
  1148
    char *_longmsg;
vb@147
  1149
    size_t length;
vb@159
  1150
    size_t _size;
vb@147
  1151
    int code;
vb@147
  1152
    int r;
vb@147
  1153
    size_t index;
vb@159
  1154
    char *type = NULL;
vb@159
  1155
    char *charset = NULL;
vb@147
  1156
vb@147
  1157
    assert(part);
vb@147
  1158
    assert(longmsg);
vb@147
  1159
vb@147
  1160
    *longmsg = NULL;
vb@147
  1161
    if (size)
vb@147
  1162
        *size = 0;
vb@147
  1163
vb@147
  1164
    if (part->mm_body == NULL)
vb@147
  1165
        return PEP_ILLEGAL_VALUE;
vb@147
  1166
vb@147
  1167
    text = part->mm_body-> dt_data.dt_text.dt_data;
vb@147
  1168
    if (text == NULL)
vb@147
  1169
        return PEP_ILLEGAL_VALUE;
vb@147
  1170
vb@147
  1171
    length = part->mm_body->dt_data.dt_text.dt_length;
vb@147
  1172
vb@147
  1173
    if (part->mm_body->dt_encoded) {
vb@147
  1174
        code = part->mm_body->dt_encoding;
vb@147
  1175
        index = 0;
vb@159
  1176
        r = mailmime_part_parse(text, length, &index, code, &_longmsg, &_size);
vb@147
  1177
        switch (r) {
vb@147
  1178
            case MAILIMF_NO_ERROR:
vb@147
  1179
                break;
vb@147
  1180
            case MAILIMF_ERROR_MEMORY:
vb@147
  1181
                return PEP_OUT_OF_MEMORY;
vb@147
  1182
            default:
vb@147
  1183
                return PEP_ILLEGAL_VALUE;
vb@147
  1184
        }
vb@147
  1185
    }
vb@147
  1186
    else {
vb@289
  1187
        _size = length + 1;
vb@289
  1188
        _longmsg = strndup(text, length);
vb@159
  1189
        if (_longmsg == NULL)
vb@147
  1190
            return PEP_OUT_OF_MEMORY;
vb@147
  1191
    }
vb@147
  1192
vb@159
  1193
    if (part->mm_content_type) {
vb@159
  1194
        if (_get_content_type(part->mm_content_type, &type, &charset) == 0) {
vb@160
  1195
            if (charset && strncasecmp(charset, "utf-8", 5) != 0) {
vb@159
  1196
                char * _text;
vb@159
  1197
                int r = charconv("utf-8", charset, _longmsg, _size, &_text);
vb@159
  1198
                switch (r) {
vb@159
  1199
                    case MAILIMF_NO_ERROR:
vb@159
  1200
                        break;
vb@159
  1201
                    case MAILIMF_ERROR_MEMORY:
vb@159
  1202
                        return PEP_OUT_OF_MEMORY;
vb@159
  1203
                    default:
vb@159
  1204
                        return PEP_ILLEGAL_VALUE;
vb@159
  1205
                }
vb@159
  1206
                free(_longmsg);
vb@159
  1207
                _longmsg = _text;
vb@159
  1208
            }
vb@159
  1209
        }
vb@159
  1210
    }
krista@1536
  1211
    // FIXME: KG - we now have the text we want.
krista@1536
  1212
    // Now we need to strip sigs and process them if they are there..
krista@1536
  1213
    
vb@159
  1214
vb@159
  1215
    *longmsg = _longmsg;
vb@159
  1216
    if (size)
vb@159
  1217
        *size = _size;
vb@159
  1218
vb@147
  1219
    return PEP_STATUS_OK;
vb@147
  1220
}
vb@147
  1221
vb@118
  1222
static PEP_STATUS interpret_MIME(
vb@118
  1223
        struct mailmime *mime,
vb@118
  1224
        message *msg
vb@118
  1225
    )
vb@118
  1226
{
vb@118
  1227
    PEP_STATUS status = PEP_STATUS_OK;
vb@118
  1228
vb@118
  1229
    assert(mime);
vb@118
  1230
    assert(msg);
vb@118
  1231
vb@118
  1232
    struct mailmime_content *content = mime->mm_content_type;
vb@118
  1233
    if (content) {
vb@152
  1234
        if (_is_multipart(content, "alternative")) {
vb@118
  1235
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1236
            if (partlist == NULL)
vb@118
  1237
                return PEP_ILLEGAL_VALUE;
vb@118
  1238
vb@118
  1239
            clistiter *cur;
vb@118
  1240
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@120
  1241
                struct mailmime *part = clist_content(cur);
vb@118
  1242
                if (part == NULL)
vb@118
  1243
                    return PEP_ILLEGAL_VALUE;
vb@118
  1244
vb@120
  1245
                content = part->mm_content_type;
vb@120
  1246
                assert(content);
vb@120
  1247
                if (content == NULL)
vb@120
  1248
                    return PEP_ILLEGAL_VALUE;
vb@118
  1249
vb@152
  1250
                if (_is_text_part(content, "plain") && msg->longmsg == NULL) {
vb@152
  1251
                    status = interpret_body(part, &msg->longmsg, NULL);
vb@152
  1252
                    if (status)
vb@152
  1253
                        return status;
vb@152
  1254
                }
vb@152
  1255
                else if (_is_text_part(content, "html") &&
vb@152
  1256
                        msg->longmsg_formatted == NULL) {
vb@152
  1257
                    status = interpret_body(part, &msg->longmsg_formatted,
vb@152
  1258
                            NULL);
vb@152
  1259
                    if (status)
vb@152
  1260
                        return status;
vb@152
  1261
                }
vb@152
  1262
                else /* add as attachment */ {
vb@152
  1263
                    status = interpret_MIME(part, msg);
vb@152
  1264
                    if (status)
vb@152
  1265
                        return status;
vb@120
  1266
                }
vb@118
  1267
            }
vb@118
  1268
        }
vb@260
  1269
        else if (_is_multipart(content, "encrypted")) {
vb@260
  1270
            if (msg->longmsg == NULL)
vb@260
  1271
                msg->longmsg = strdup("");
vb@260
  1272
vb@260
  1273
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@260
  1274
            if (partlist == NULL)
vb@260
  1275
                return PEP_ILLEGAL_VALUE;
vb@260
  1276
vb@260
  1277
            clistiter *cur;
vb@260
  1278
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@260
  1279
                struct mailmime *part= clist_content(cur);
vb@260
  1280
                if (part == NULL)
vb@260
  1281
                    return PEP_ILLEGAL_VALUE;
vb@260
  1282
vb@260
  1283
                status = interpret_MIME(part, msg);
vb@260
  1284
                if (status != PEP_STATUS_OK)
vb@260
  1285
                    return status;
vb@260
  1286
            }
vb@260
  1287
        }
vb@152
  1288
        else if (_is_multipart(content, NULL)) {
vb@118
  1289
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1290
            if (partlist == NULL)
vb@118
  1291
                return PEP_ILLEGAL_VALUE;
vb@118
  1292
vb@118
  1293
            clistiter *cur;
vb@118
  1294
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@118
  1295
                struct mailmime *part= clist_content(cur);
vb@118
  1296
                if (part == NULL)
vb@118
  1297
                    return PEP_ILLEGAL_VALUE;
vb@118
  1298
vb@118
  1299
                status = interpret_MIME(part, msg);
vb@118
  1300
                if (status != PEP_STATUS_OK)
vb@118
  1301
                    return status;
vb@118
  1302
            }
vb@118
  1303
        }
vb@118
  1304
        else {
krista@1229
  1305
            if (_is_text_part(content, "html") &&
krista@1229
  1306
                msg->longmsg_formatted == NULL) {
krista@1229
  1307
                status = interpret_body(mime, &msg->longmsg_formatted,
krista@1229
  1308
                                        NULL);
krista@1229
  1309
                if (status)
krista@1229
  1310
                    return status;
krista@1229
  1311
            }
krista@1229
  1312
            else if (_is_text_part(content, NULL) && msg->longmsg == NULL) {
vb@152
  1313
                status = interpret_body(mime, &msg->longmsg, NULL);
vb@152
  1314
                if (status)
vb@152
  1315
                    return status;
vb@152
  1316
            }
vb@152
  1317
            else {
vb@152
  1318
                char *data = NULL;
vb@157
  1319
                size_t size = 0;
vb@157
  1320
                char * mime_type;
vb@159
  1321
                char * charset;
vb@157
  1322
                char * filename;
vb@159
  1323
                int r;
vb@118
  1324
vb@159
  1325
                r = _get_content_type(content, &mime_type, &charset);
vb@159
  1326
                switch (r) {
vb@159
  1327
                    case 0:
vb@159
  1328
                        break;
vb@159
  1329
                    case EINVAL:
vb@159
  1330
                        return PEP_ILLEGAL_VALUE;
vb@159
  1331
                    case ENOMEM:
vb@159
  1332
                        return PEP_OUT_OF_MEMORY;
vb@159
  1333
                    default:
vb@159
  1334
                        return PEP_UNKNOWN_ERROR;
vb@157
  1335
                }
vb@154
  1336
vb@159
  1337
                assert(mime_type);
vb@159
  1338
vb@152
  1339
                status = interpret_body(mime, &data, &size);
vb@152
  1340
                if (status)
vb@152
  1341
                    return status;
vb@152
  1342
vb@154
  1343
                filename = _get_filename(mime);
vb@481
  1344
                char *_filename = NULL;
vb@481
  1345
                if (filename) {
vb@481
  1346
                    size_t index = 0;
vb@481
  1347
                    r = mailmime_encoded_phrase_parse("utf-8", filename,
vb@481
  1348
                            strlen(filename), &index, "utf-8", &_filename);
vb@481
  1349
                    if (r)
vb@481
  1350
                        goto enomem;
vb@481
  1351
                }
vb@154
  1352
vb@260
  1353
                bloblist_t *_a = bloblist_add(msg->attachments, data, size,
vb@481
  1354
                        mime_type, _filename);
vb@481
  1355
                free(_filename);
vb@260
  1356
                if (_a == NULL)
vb@260
  1357
                    return PEP_OUT_OF_MEMORY;
vb@152
  1358
                if (msg->attachments == NULL)
vb@260
  1359
                    msg->attachments = _a;
vb@152
  1360
            }
vb@118
  1361
        }
vb@118
  1362
    }
vb@118
  1363
vb@118
  1364
    return PEP_STATUS_OK;
vb@481
  1365
vb@481
  1366
enomem:
vb@481
  1367
    return PEP_OUT_OF_MEMORY;
vb@118
  1368
}
vb@118
  1369
vb@89
  1370
DYNAMIC_API PEP_STATUS mime_decode_message(
vb@89
  1371
        const char *mimetext,
vb@269
  1372
        size_t size,
vb@89
  1373
        message **msg
vb@89
  1374
    )
vb@89
  1375
{
vb@89
  1376
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
  1377
    struct mailmime * mime = NULL;
vb@89
  1378
    int r;
vb@94
  1379
    message *_msg = NULL;
vb@94
  1380
    size_t index;
vb@89
  1381
vb@89
  1382
    assert(mimetext);
vb@89
  1383
    assert(msg);
vb@89
  1384
vb@191
  1385
    if (!(mimetext && msg))
vb@191
  1386
        return PEP_ILLEGAL_VALUE;
vb@191
  1387
vb@89
  1388
    *msg = NULL;
vb@113
  1389
vb@94
  1390
    index = 0;
vb@269
  1391
    r = mailmime_parse(mimetext, size, &index, &mime);
vb@89
  1392
    assert(r == 0);
vb@89
  1393
    assert(mime);
vb@90
  1394
    if (r) {
vb@89
  1395
        if (r == MAILIMF_ERROR_MEMORY)
vb@89
  1396
            goto enomem;
vb@89
  1397
        else
vb@89
  1398
            goto err_mime;
vb@89
  1399
    }
vb@89
  1400
vb@117
  1401
    _msg = calloc(1, sizeof(message));
vb@117
  1402
    assert(_msg);
vb@117
  1403
    if (_msg == NULL)
vb@117
  1404
        goto enomem;
vb@116
  1405
vb@147
  1406
    clist * _fieldlist = _get_fields(mime);
vb@147
  1407
    if (_fieldlist) {
vb@117
  1408
        status = read_fields(_msg, _fieldlist);
vb@116
  1409
        if (status != PEP_STATUS_OK)
vb@116
  1410
            goto pep_error;
vb@116
  1411
    }
vb@116
  1412
vb@147
  1413
    struct mailmime_content *content = _get_content(mime);
vb@117
  1414
vb@147
  1415
    if (content) {
vb@260
  1416
        status = interpret_MIME(mime->mm_data.mm_message.mm_msg_mime,
vb@260
  1417
                _msg);
vb@260
  1418
        if (status != PEP_STATUS_OK)
vb@260
  1419
            goto pep_error;
vb@116
  1420
    }
vb@116
  1421
vb@89
  1422
    mailmime_free(mime);
vb@94
  1423
    *msg = _msg;
vb@89
  1424
vb@89
  1425
    return status;
vb@89
  1426
vb@89
  1427
err_mime:
vb@89
  1428
    status = PEP_ILLEGAL_VALUE;
vb@89
  1429
    goto pep_error;
vb@89
  1430
vb@89
  1431
enomem:
vb@89
  1432
    status = PEP_OUT_OF_MEMORY;
vb@89
  1433
vb@89
  1434
pep_error:
vb@94
  1435
    free_message(_msg);
vb@94
  1436
vb@89
  1437
    if (mime)
vb@89
  1438
        mailmime_free(mime);
vb@89
  1439
vb@89
  1440
    return status;
vb@89
  1441
}