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