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