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