src/mime.c
author vb
Mon, 23 Mar 2015 21:42:45 +0100
changeset 120 c282594b523b
parent 118 217e8ebe524a
child 125 5119178815d8
permissions -rw-r--r--
case conversion including tailoring based on PyICU
vb@48
     1
#include "mime.h"
vb@48
     2
vb@48
     3
#include <string.h>
vb@48
     4
#include <stdlib.h>
vb@48
     5
#include <assert.h>
vb@48
     6
#include <errno.h>
vb@48
     7
#include <unistd.h>
vb@48
     8
vb@48
     9
#include "etpan_mime.h"
vb@63
    10
#include "wrappers.h"
vb@48
    11
vb@94
    12
#define NOT_IMPLEMENTED assert(0);
vb@94
    13
vb@117
    14
DYNAMIC_API bool is_PGP_message_text(const char *text)
vb@117
    15
{
vb@117
    16
    assert(text);
vb@117
    17
    if (text == NULL)
vb@117
    18
        return false;
vb@117
    19
vb@117
    20
    return strncmp(text, "-----BEGIN PGP MESSAGE-----", 27) == 0;
vb@117
    21
}
vb@117
    22
vb@89
    23
static PEP_STATUS render_mime(struct mailmime *mime, char **mimetext)
vb@48
    24
{
vb@89
    25
    PEP_STATUS status = PEP_STATUS_OK;
vb@48
    26
    int fd;
vb@48
    27
    FILE *file = NULL;
vb@48
    28
    size_t size;
vb@48
    29
    char *buf = NULL;
vb@89
    30
    int col;
vb@89
    31
    int r;
vb@51
    32
    char *template = strdup("/tmp/pEp.XXXXXXXXXXXXXXXXXXXX");
vb@48
    33
    assert(template);
vb@48
    34
    if (template == NULL)
vb@48
    35
        goto enomem;
vb@48
    36
vb@89
    37
    *mimetext = NULL;
vb@89
    38
vb@63
    39
    fd = Mkstemp(template);
vb@48
    40
    assert(fd != -1);
vb@48
    41
    if (fd == -1)
vb@48
    42
        goto err_file;
vb@48
    43
vb@48
    44
    r = unlink(template);
vb@48
    45
    assert(r == 0);
vb@90
    46
    if (r)
vb@48
    47
        goto err_file;
vb@48
    48
vb@48
    49
    free(template);
vb@48
    50
    template = NULL;
vb@48
    51
vb@63
    52
    file = Fdopen(fd, "w+");
vb@48
    53
    assert(file);
vb@48
    54
    if (file == NULL) {
vb@48
    55
        switch (errno) {
vb@48
    56
            case ENOMEM:
vb@48
    57
                goto enomem;
vb@48
    58
            default:
vb@48
    59
                goto err_file;
vb@48
    60
        }
vb@48
    61
    }
vb@48
    62
vb@48
    63
    fd = -1;
vb@48
    64
vb@48
    65
    col = 0;
vb@48
    66
    r = mailmime_write_file(file, &col, mime);
vb@48
    67
    assert(r == MAILIMF_NO_ERROR);
vb@48
    68
    if (r == MAILIMF_ERROR_MEMORY)
vb@48
    69
        goto enomem;
vb@48
    70
    else if (r != MAILIMF_NO_ERROR)
vb@48
    71
        goto err_file;
vb@48
    72
vb@48
    73
    off_t len = ftello(file);
vb@48
    74
    assert(len != -1);
vb@48
    75
    if (len == -1 && errno == EOVERFLOW)
vb@48
    76
        goto err_file;
vb@48
    77
Edouard@70
    78
    if (len + 1 > SIZE_MAX)
vb@48
    79
        goto err_buffer;
vb@48
    80
vb@48
    81
    size = (size_t) len;
vb@48
    82
vb@51
    83
    errno = 0;
vb@48
    84
    rewind(file);
vb@51
    85
    assert(errno == 0);
vb@62
    86
    switch (errno) {
vb@63
    87
        case 0:
vb@63
    88
            break;
vb@62
    89
        case ENOMEM:
vb@62
    90
            goto enomem;
vb@62
    91
        default:
vb@62
    92
            goto err_file;
vb@62
    93
    }
vb@48
    94
vb@48
    95
    buf = calloc(1, size + 1);
vb@48
    96
    assert(buf);
vb@48
    97
    if (buf == NULL)
vb@48
    98
        goto enomem;
vb@63
    99
 
vb@63
   100
    size_t _read;
vb@89
   101
    _read = Fread(buf, size, 1, file);
vb@63
   102
    assert(_read == size);
vb@62
   103
vb@63
   104
    r = Fclose(file);
vb@62
   105
    assert(r == 0);
vb@62
   106
vb@87
   107
    *mimetext = buf;
vb@48
   108
    return PEP_STATUS_OK;
vb@48
   109
vb@48
   110
err_buffer:
vb@88
   111
    status = PEP_BUFFER_TOO_SMALL;
vb@88
   112
    goto pep_error;
vb@48
   113
vb@48
   114
err_file:
vb@88
   115
    status = PEP_CANNOT_CREATE_TEMP_FILE;
vb@88
   116
    goto pep_error;
vb@48
   117
vb@48
   118
enomem:
vb@88
   119
    status = PEP_OUT_OF_MEMORY;
vb@52
   120
vb@88
   121
pep_error:
vb@48
   122
    free(buf);
vb@48
   123
    free(template);
vb@48
   124
vb@62
   125
    if (file) {
vb@63
   126
        r = Fclose(file);
vb@62
   127
        assert(r == 0);
vb@62
   128
    }
vb@62
   129
    else if (fd != -1) {
vb@63
   130
        r = Close(fd);
vb@62
   131
        assert(r == 0);
vb@62
   132
    }
vb@48
   133
vb@89
   134
    return status;
vb@89
   135
}
vb@89
   136
vb@89
   137
static PEP_STATUS mime_html_text(
vb@89
   138
        const char *plaintext,
vb@89
   139
        const char *htmltext,
vb@89
   140
        struct mailmime **result
vb@89
   141
    )
vb@89
   142
{
vb@89
   143
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
   144
    struct mailmime * mime = NULL;
vb@89
   145
    struct mailmime * submime = NULL;
vb@89
   146
    int r;
vb@89
   147
vb@89
   148
    assert(plaintext);
vb@89
   149
    assert(htmltext);
vb@89
   150
    assert(result);
vb@89
   151
vb@89
   152
    *result = NULL;
vb@89
   153
vb@116
   154
    mime = part_multiple_new("multipart/alternative");
vb@89
   155
    assert(mime);
vb@89
   156
    if (mime == NULL)
vb@89
   157
        goto enomem;
vb@89
   158
vb@114
   159
    submime = get_text_part("msg.txt", "text/plain", plaintext, strlen(plaintext),
vb@89
   160
            MAILMIME_MECHANISM_QUOTED_PRINTABLE);
vb@89
   161
    assert(submime);
vb@89
   162
    if (submime == NULL)
vb@89
   163
        goto enomem;
vb@89
   164
vb@89
   165
    r = mailmime_smart_add_part(mime, submime);
vb@89
   166
    assert(r == MAILIMF_NO_ERROR);
vb@89
   167
    if (r == MAILIMF_ERROR_MEMORY) {
vb@89
   168
        goto enomem;
vb@89
   169
    }
vb@89
   170
    else {
vb@89
   171
        // mailmime_smart_add_part() takes ownership of submime
vb@89
   172
        submime = NULL;
vb@89
   173
    }
vb@89
   174
vb@114
   175
    submime = get_text_part("msg.html", "text/html", htmltext, strlen(htmltext),
vb@89
   176
            MAILMIME_MECHANISM_QUOTED_PRINTABLE);
vb@89
   177
    assert(submime);
vb@89
   178
    if (submime == NULL)
vb@89
   179
        goto enomem;
vb@89
   180
vb@89
   181
    r = mailmime_smart_add_part(mime, submime);
vb@89
   182
    assert(r == MAILIMF_NO_ERROR);
vb@89
   183
    if (r == MAILIMF_ERROR_MEMORY)
vb@89
   184
        goto enomem;
vb@89
   185
    else {
vb@89
   186
        // mailmime_smart_add_part() takes ownership of submime
vb@89
   187
        submime = NULL;
vb@89
   188
    }
vb@89
   189
vb@89
   190
    *result = mime;
vb@89
   191
    return PEP_STATUS_OK;
vb@89
   192
vb@89
   193
enomem:
vb@89
   194
    status = PEP_OUT_OF_MEMORY;
vb@89
   195
vb@89
   196
pep_error:
vb@59
   197
    if (mime)
vb@59
   198
        mailmime_free(mime);
vb@89
   199
vb@48
   200
    if (submime)
vb@48
   201
        mailmime_free(submime);
vb@48
   202
vb@88
   203
    return status;
vb@48
   204
}
vb@88
   205
vb@89
   206
static PEP_STATUS mime_attachment(
vb@89
   207
        bloblist_t *blob,
vb@89
   208
        struct mailmime **result
vb@88
   209
    )
vb@88
   210
{
vb@88
   211
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
   212
    struct mailmime * mime = NULL;
vb@89
   213
    char * mime_type;
vb@88
   214
vb@89
   215
    assert(blob);
vb@89
   216
    assert(result);
vb@88
   217
vb@89
   218
    *result = NULL;
vb@88
   219
vb@89
   220
    if (blob->mime_type == NULL)
vb@89
   221
        mime_type = "application/octet-stream";
vb@89
   222
    else
vb@89
   223
        mime_type = blob->mime_type;
vb@89
   224
vb@113
   225
    mime = get_file_part(blob->filename, mime_type, blob->data, blob->size);
vb@89
   226
    assert(mime);
vb@89
   227
    if (mime == NULL)
vb@89
   228
        goto enomem;
vb@89
   229
vb@89
   230
    *result = mime;
vb@89
   231
    return PEP_STATUS_OK;
vb@88
   232
vb@88
   233
enomem:
vb@88
   234
    status = PEP_OUT_OF_MEMORY;
vb@88
   235
vb@88
   236
pep_error:
vb@89
   237
    if (mime)
vb@89
   238
        mailmime_free(mime);
vb@89
   239
vb@88
   240
    return status;
vb@88
   241
}
vb@88
   242
vb@94
   243
static struct mailimf_mailbox * identity_to_mailbox(const pEp_identity *ident)
vb@94
   244
{
vb@94
   245
    char *_username = NULL;
vb@94
   246
    struct mailimf_mailbox *mb;
vb@94
   247
vb@94
   248
    _username = mailmime_encode_subject_header("utf-8", ident->username, 0);
vb@94
   249
    if (_username == NULL)
vb@94
   250
        goto enomem;
vb@94
   251
vb@94
   252
    mb = mailbox_from_string(_username, ident->address);
vb@94
   253
    if (mb == NULL)
vb@94
   254
        goto enomem;
vb@94
   255
vb@94
   256
    free(_username);
vb@94
   257
    _username = NULL;
vb@94
   258
vb@94
   259
    return mb;
vb@94
   260
vb@94
   261
enomem:
vb@94
   262
    free(_username);
vb@94
   263
    return NULL;
vb@94
   264
}
vb@94
   265
vb@94
   266
static struct mailimf_mailbox_list * identity_to_mbl(
vb@94
   267
        const pEp_identity *ident)
vb@90
   268
{
vb@90
   269
    struct mailimf_mailbox_list *mbl = NULL;
vb@90
   270
    struct mailimf_mailbox *mb = NULL;
vb@90
   271
    clist *list = NULL;
vb@90
   272
    int r;
vb@90
   273
vb@90
   274
    assert(ident);
vb@90
   275
vb@90
   276
    list = clist_new();
vb@90
   277
    if (list == NULL)
vb@90
   278
        goto enomem;
vb@90
   279
vb@94
   280
    mb = identity_to_mailbox(ident);
vb@90
   281
    if (mb == NULL)
vb@90
   282
        goto enomem;
vb@90
   283
vb@90
   284
    r = clist_append(list, mb);
vb@90
   285
    if (r)
vb@90
   286
        goto enomem;
vb@90
   287
vb@90
   288
    mbl = mailimf_mailbox_list_new(list);
vb@90
   289
    if (mbl == NULL)
vb@90
   290
        goto enomem;
vb@90
   291
vb@90
   292
    return mbl;
vb@90
   293
vb@90
   294
enomem:
vb@90
   295
    if (mb)
vb@90
   296
        mailimf_mailbox_free(mb);
vb@90
   297
vb@90
   298
    if (list)
vb@90
   299
        clist_free(list);
vb@90
   300
vb@90
   301
    return NULL;
vb@90
   302
}
vb@90
   303
vb@94
   304
static struct mailimf_address_list * identity_list_to_mal(identity_list *il)
vb@90
   305
{
vb@93
   306
    struct mailimf_address_list *mal = NULL;
vb@90
   307
    struct mailimf_mailbox *mb = NULL;
vb@93
   308
    struct mailimf_address * addr = NULL;
vb@90
   309
    clist *list = NULL;
vb@90
   310
    int r;
vb@90
   311
vb@90
   312
    assert(il);
vb@90
   313
vb@90
   314
    list = clist_new();
vb@90
   315
    if (list == NULL)
vb@90
   316
        goto enomem;
vb@90
   317
vb@90
   318
    identity_list *_il;
vb@90
   319
    for (_il = il; _il; _il = _il->next) {
vb@94
   320
        mb = identity_to_mailbox(_il->ident);
vb@90
   321
        if (mb == NULL)
vb@90
   322
            goto enomem;
vb@90
   323
vb@93
   324
        addr = mailimf_address_new(MAILIMF_ADDRESS_MAILBOX, mb, NULL);
vb@93
   325
        if (addr == NULL)
vb@93
   326
            goto enomem;
vb@93
   327
        mb = NULL;
vb@93
   328
vb@93
   329
        r = clist_append(list, addr);
vb@90
   330
        if (r)
vb@90
   331
            goto enomem;
vb@93
   332
        addr = NULL;
vb@90
   333
    }
vb@93
   334
    mal = mailimf_address_list_new(list);
vb@93
   335
    if (mal == NULL)
vb@90
   336
        goto enomem;
vb@90
   337
vb@93
   338
    return mal;
vb@90
   339
vb@90
   340
enomem:
vb@90
   341
    if (mb)
vb@90
   342
        mailimf_mailbox_free(mb);
vb@90
   343
vb@93
   344
    if (addr)
vb@93
   345
        mailimf_address_free(addr);
vb@93
   346
vb@90
   347
    if (list)
vb@90
   348
        clist_free(list);
vb@90
   349
vb@90
   350
    return NULL;
vb@90
   351
}
vb@90
   352
vb@94
   353
static clist * stringlist_to_clist(stringlist_t *sl)
vb@91
   354
{
vb@91
   355
    clist * cl = clist_new();
vb@91
   356
    assert(cl);
vb@91
   357
    if (cl == NULL)
vb@91
   358
        return NULL;
vb@91
   359
vb@91
   360
    stringlist_t *_sl;
vb@91
   361
    for (_sl = sl; _sl; _sl = _sl->next) {
vb@91
   362
        int r;
vb@94
   363
        char * value = mailmime_encode_subject_header("utf-8", _sl->value, 0);
vb@91
   364
        assert(value);
vb@91
   365
        if (value == NULL) {
vb@91
   366
            clist_free(cl);
vb@91
   367
            return NULL;
vb@91
   368
        }
vb@91
   369
        r = clist_append(cl, value);
vb@91
   370
        assert(r == 0);
vb@91
   371
        if (r) {
vb@91
   372
            free(value);
vb@91
   373
            clist_free(cl);
vb@91
   374
            return NULL;
vb@91
   375
        }
vb@91
   376
    }
vb@91
   377
vb@91
   378
    return cl;
vb@91
   379
}
vb@91
   380
vb@89
   381
static PEP_STATUS build_fields(const message *msg, struct mailimf_fields **result)
vb@89
   382
{
vb@89
   383
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
   384
    struct mailimf_fields * fields = NULL;
vb@89
   385
    int r;
vb@89
   386
    clist * fields_list = NULL;
vb@89
   387
    char *subject = msg->shortmsg ? msg->shortmsg : "pEp";
vb@89
   388
vb@89
   389
    assert(msg);
vb@89
   390
    assert(msg->from);
vb@89
   391
    assert(msg->from->address);
vb@89
   392
    assert(result);
vb@89
   393
vb@89
   394
    *result = NULL;
vb@89
   395
vb@89
   396
    fields_list = clist_new();
vb@89
   397
    assert(fields_list);
vb@89
   398
    if (fields_list == NULL)
vb@89
   399
        goto enomem;
vb@89
   400
vb@90
   401
    if (msg->id) {
vb@90
   402
        char *_msgid = strdup(msg->id);
vb@90
   403
        if (_msgid == NULL)
vb@90
   404
            goto enomem;
vb@89
   405
vb@90
   406
        r = _append_field(fields_list, MAILIMF_FIELD_MESSAGE_ID,
vb@90
   407
                (_new_func_t) mailimf_message_id_new, _msgid);
vb@90
   408
        if (r) {
vb@90
   409
            free(_msgid);
vb@90
   410
            goto enomem;
vb@90
   411
        }
vb@90
   412
    }
vb@90
   413
vb@90
   414
    if (msg->sent) {
vb@90
   415
        struct mailimf_date_time * dt = timestamp_to_etpantime(msg->sent);
vb@90
   416
        if (dt == NULL)
vb@90
   417
            goto enomem;
vb@90
   418
vb@90
   419
        r = _append_field(fields_list, MAILIMF_FIELD_ORIG_DATE,
vb@90
   420
                (_new_func_t) mailimf_orig_date_new, dt);
vb@90
   421
        if (r) {
vb@90
   422
            mailimf_date_time_free(dt);
vb@90
   423
            goto enomem;
vb@90
   424
        }
vb@90
   425
        dt = NULL;
vb@90
   426
    }
vb@90
   427
vb@91
   428
    /* if (msg->from) */ {
vb@94
   429
        struct mailimf_mailbox_list *from = identity_to_mbl(msg->from);
vb@90
   430
        if (from == NULL)
vb@90
   431
            goto enomem;
vb@90
   432
vb@90
   433
        r = _append_field(fields_list, MAILIMF_FIELD_FROM,
vb@90
   434
                (_new_func_t) mailimf_from_new, from);
vb@90
   435
        if (r) {
vb@90
   436
            mailimf_mailbox_list_free(from);
vb@90
   437
            goto enomem;
vb@90
   438
        }
vb@90
   439
    }
vb@90
   440
vb@90
   441
    if (msg->to) {
vb@94
   442
        struct mailimf_address_list *to = identity_list_to_mal(msg->to);
vb@90
   443
        if (to == NULL)
vb@90
   444
            goto enomem;
vb@90
   445
vb@91
   446
        r = _append_field(fields_list, MAILIMF_FIELD_TO,
vb@90
   447
                (_new_func_t) mailimf_to_new, to);
vb@90
   448
        if (r) {
vb@93
   449
            mailimf_address_list_free(to);
vb@90
   450
            goto enomem;
vb@90
   451
        }
vb@90
   452
    }
vb@89
   453
vb@112
   454
    /* if (subject) */ {
vb@112
   455
        char *_subject = mailmime_encode_subject_header("utf-8", subject, 1);
vb@112
   456
        if (_subject == NULL)
vb@112
   457
            goto enomem;
vb@112
   458
vb@112
   459
        r = _append_field(fields_list, MAILIMF_FIELD_SUBJECT,
vb@112
   460
                (_new_func_t) mailimf_subject_new, _subject);
vb@112
   461
        if (r) {
vb@112
   462
            free(_subject);
vb@112
   463
            goto enomem;
vb@112
   464
        }
vb@112
   465
    }
vb@112
   466
vb@91
   467
    if (msg->cc) {
vb@94
   468
        struct mailimf_address_list *cc = identity_list_to_mal(msg->cc);
vb@91
   469
        if (cc == NULL)
vb@91
   470
            goto enomem;
vb@91
   471
vb@91
   472
        r = _append_field(fields_list, MAILIMF_FIELD_CC,
vb@91
   473
                (_new_func_t) mailimf_cc_new, cc);
vb@91
   474
        if (r) {
vb@93
   475
            mailimf_address_list_free(cc);
vb@91
   476
            goto enomem;
vb@91
   477
        }
vb@91
   478
    }
vb@91
   479
    
vb@91
   480
    if (msg->bcc) {
vb@94
   481
        struct mailimf_address_list *bcc = identity_list_to_mal(msg->bcc);
vb@91
   482
        if (bcc == NULL)
vb@91
   483
            goto enomem;
vb@91
   484
vb@91
   485
        r = _append_field(fields_list, MAILIMF_FIELD_BCC,
vb@91
   486
                (_new_func_t) mailimf_bcc_new, bcc);
vb@91
   487
        if (r) {
vb@93
   488
            mailimf_address_list_free(bcc);
vb@91
   489
            goto enomem;
vb@91
   490
        }
vb@91
   491
    }
vb@91
   492
    
vb@91
   493
    if (msg->reply_to) {
vb@94
   494
        struct mailimf_address_list *reply_to = identity_list_to_mal(msg->reply_to);
vb@91
   495
        if (reply_to == NULL)
vb@91
   496
            goto enomem;
vb@91
   497
vb@91
   498
        r = _append_field(fields_list, MAILIMF_FIELD_REPLY_TO,
vb@91
   499
                (_new_func_t) mailimf_reply_to_new, reply_to);
vb@91
   500
        if (r) {
vb@94
   501
            mailimf_address_list_free(reply_to);
vb@91
   502
            goto enomem;
vb@91
   503
        }
vb@91
   504
    }
vb@91
   505
vb@91
   506
    if (msg->in_reply_to) {
vb@94
   507
        clist *in_reply_to = stringlist_to_clist(msg->in_reply_to);
vb@91
   508
        if (in_reply_to == NULL)
vb@91
   509
            goto enomem;
vb@91
   510
vb@91
   511
        r = _append_field(fields_list, MAILIMF_FIELD_IN_REPLY_TO,
vb@91
   512
                (_new_func_t) mailimf_in_reply_to_new, in_reply_to);
vb@91
   513
        if (r) {
vb@94
   514
            clist_free(in_reply_to);
vb@91
   515
            goto enomem;
vb@91
   516
        }
vb@91
   517
    }
vb@91
   518
vb@91
   519
    if (msg->references) {
vb@94
   520
        clist *references = stringlist_to_clist(msg->references);
vb@91
   521
        if (references == NULL)
vb@91
   522
            goto enomem;
vb@91
   523
vb@91
   524
        r = _append_field(fields_list, MAILIMF_FIELD_REFERENCES,
vb@91
   525
                (_new_func_t) mailimf_references_new, references);
vb@91
   526
        if (r) {
vb@91
   527
            clist_free(references);
vb@91
   528
            goto enomem;
vb@91
   529
        }
vb@91
   530
    }
vb@91
   531
vb@91
   532
    if (msg->keywords) {
vb@94
   533
        clist *keywords = stringlist_to_clist(msg->keywords);
vb@91
   534
        if (keywords == NULL)
vb@91
   535
            goto enomem;
vb@91
   536
vb@91
   537
        r = _append_field(fields_list, MAILIMF_FIELD_KEYWORDS,
vb@91
   538
                (_new_func_t) mailimf_keywords_new, keywords);
vb@91
   539
        if (r) {
vb@91
   540
            clist_free(keywords);
vb@91
   541
            goto enomem;
vb@91
   542
        }
vb@91
   543
    }
vb@91
   544
vb@91
   545
    if (msg->comments) {
vb@94
   546
        char *comments = mailmime_encode_subject_header("utf-8", msg->comments,
vb@94
   547
                0);
vb@91
   548
        if (comments == NULL)
vb@91
   549
            goto enomem;
vb@91
   550
vb@91
   551
        r = _append_field(fields_list, MAILIMF_FIELD_COMMENTS,
vb@91
   552
                (_new_func_t) mailimf_comments_new, comments);
vb@91
   553
        if (r) {
vb@91
   554
            free(comments);
vb@91
   555
            goto enomem;
vb@91
   556
        }
vb@91
   557
    }
vb@91
   558
vb@98
   559
    if (msg->opt_fields) {
vb@110
   560
        stringpair_list_t *_l;
vb@110
   561
        for (_l = msg->opt_fields; _l; _l = _l->next) {
vb@110
   562
            char *key = _l->value->key;
vb@110
   563
            char *value = _l->value->value;
vb@110
   564
            char *_value = mailmime_encode_subject_header("utf-8", value, 0);
vb@110
   565
            if (_value == NULL)
vb@110
   566
                goto enomem;
vb@110
   567
vb@110
   568
            r = _append_optional_field(fields_list, key, _value);
vb@110
   569
            free(_value);
vb@110
   570
            if (r)
vb@110
   571
                goto enomem;
vb@110
   572
        }
vb@98
   573
    }
vb@94
   574
vb@89
   575
    fields = mailimf_fields_new(fields_list);
vb@89
   576
    assert(fields);
vb@89
   577
    if (fields == NULL)
vb@89
   578
        goto enomem;
vb@89
   579
vb@89
   580
    *result = fields;
vb@89
   581
vb@89
   582
    return PEP_STATUS_OK;
vb@89
   583
vb@89
   584
enomem:
vb@89
   585
    status = PEP_OUT_OF_MEMORY;
vb@89
   586
vb@89
   587
pep_error:
vb@89
   588
    if (fields_list)
vb@89
   589
        clist_free(fields_list);
vb@89
   590
vb@89
   591
    if (fields)
vb@89
   592
        mailimf_fields_free(fields);
vb@89
   593
vb@89
   594
    return status;
vb@89
   595
}
vb@89
   596
vb@114
   597
static PEP_STATUS mime_encode_message_plain(
vb@89
   598
        const message *msg,
vb@113
   599
        bool omit_fields,
vb@114
   600
        struct mailmime **result
vb@89
   601
    )
vb@89
   602
{
vb@89
   603
    struct mailmime * mime = NULL;
vb@89
   604
    struct mailmime * submime = NULL;
vb@89
   605
    int r;
vb@89
   606
    PEP_STATUS status;
vb@89
   607
    char *subject;
vb@89
   608
    char *plaintext;
vb@89
   609
    char *htmltext;
vb@89
   610
vb@89
   611
    assert(msg);
vb@114
   612
    assert(result);
vb@112
   613
vb@89
   614
    subject = (msg->shortmsg) ? msg->shortmsg : "pEp";
vb@89
   615
    plaintext = (msg->longmsg) ? msg->longmsg : "";
vb@89
   616
    htmltext = msg->longmsg_formatted;
vb@89
   617
vb@89
   618
    if (htmltext) {
vb@89
   619
        status = mime_html_text(plaintext, htmltext, &mime);
vb@89
   620
        if (status != PEP_STATUS_OK)
vb@89
   621
            goto pep_error;
vb@89
   622
    }
vb@89
   623
    else {
vb@117
   624
        if (is_PGP_message_text(plaintext))
vb@114
   625
            mime = get_text_part("msg.asc", "application/octet-stream", plaintext,
vb@114
   626
                    strlen(plaintext), MAILMIME_MECHANISM_7BIT);
vb@114
   627
        else
vb@114
   628
            mime = get_text_part("msg.txt", "text/plain", plaintext, strlen(plaintext),
vb@114
   629
                    MAILMIME_MECHANISM_QUOTED_PRINTABLE);
vb@89
   630
        assert(mime);
vb@89
   631
        if (mime == NULL)
vb@89
   632
            goto enomem;
vb@89
   633
    }
vb@89
   634
vb@89
   635
    if (msg->attachments) {
vb@89
   636
        submime = mime;
vb@116
   637
        mime = part_multiple_new("multipart/mixed");
vb@89
   638
        assert(mime);
vb@89
   639
        if (mime == NULL)
vb@89
   640
            goto enomem;
vb@89
   641
vb@89
   642
        r = mailmime_smart_add_part(mime, submime);
vb@89
   643
        assert(r == MAILIMF_NO_ERROR);
vb@89
   644
        if (r == MAILIMF_ERROR_MEMORY) {
vb@89
   645
            goto enomem;
vb@89
   646
        }
vb@89
   647
        else {
vb@89
   648
            // mailmime_smart_add_part() takes ownership of submime
vb@89
   649
            submime = NULL;
vb@89
   650
        }
vb@89
   651
vb@89
   652
        bloblist_t *_a;
vb@89
   653
        for (_a = msg->attachments; _a != NULL; _a = _a->next) {
vb@89
   654
            char * mime_type;
vb@89
   655
vb@89
   656
            assert(_a->data);
vb@89
   657
            assert(_a->size);
vb@89
   658
vb@89
   659
            status = mime_attachment(_a, &submime);
vb@89
   660
            if (status != PEP_STATUS_OK)
vb@89
   661
                goto pep_error;
vb@89
   662
vb@89
   663
            r = mailmime_smart_add_part(mime, submime);
vb@89
   664
            assert(r == MAILIMF_NO_ERROR);
vb@89
   665
            if (r == MAILIMF_ERROR_MEMORY) {
vb@89
   666
                goto enomem;
vb@89
   667
            }
vb@89
   668
            else {
vb@89
   669
                // mailmime_smart_add_part() takes ownership of submime
vb@89
   670
                submime = NULL;
vb@89
   671
            }
vb@89
   672
        }
vb@89
   673
    }
vb@89
   674
vb@114
   675
    *result = mime;
vb@114
   676
    return PEP_STATUS_OK;
vb@114
   677
vb@114
   678
enomem:
vb@114
   679
    status = PEP_OUT_OF_MEMORY;
vb@114
   680
vb@114
   681
pep_error:
vb@114
   682
    if (mime)
vb@114
   683
        mailmime_free(mime);
vb@114
   684
vb@114
   685
    if (submime)
vb@114
   686
        mailmime_free(submime);
vb@114
   687
vb@114
   688
    return status;
vb@114
   689
}
vb@114
   690
vb@114
   691
static PEP_STATUS mime_encode_message_PGP_MIME(
vb@114
   692
        const message * msg,
vb@114
   693
        bool omit_fields,
vb@114
   694
        struct mailmime **result
vb@114
   695
    )
vb@114
   696
{
vb@114
   697
    struct mailmime * mime = NULL;
vb@114
   698
    struct mailmime * submime = NULL;
vb@114
   699
	struct mailmime_parameter * param;
vb@114
   700
    int r;
vb@114
   701
    PEP_STATUS status;
vb@114
   702
    char *subject;
vb@114
   703
    char *plaintext;
vb@114
   704
vb@114
   705
    assert(msg->longmsg);
vb@114
   706
vb@114
   707
    subject = (msg->shortmsg) ? msg->shortmsg : "pEp";
vb@114
   708
    plaintext = msg->longmsg;
vb@114
   709
vb@116
   710
    mime = part_multiple_new("multipart/encrypted");
vb@114
   711
    assert(mime);
vb@114
   712
    if (mime == NULL)
vb@114
   713
        goto enomem;
vb@114
   714
vb@114
   715
    param = mailmime_param_new_with_data("protocol", "application/pgp-encrypted");
vb@114
   716
    clist_append(mime->mm_content_type->ct_parameters, param);
vb@114
   717
vb@114
   718
    submime = get_pgp_encrypted_part();
vb@114
   719
    assert(submime);
vb@114
   720
    if (submime == NULL)
vb@114
   721
        goto enomem;
vb@114
   722
vb@114
   723
    r = mailmime_smart_add_part(mime, submime);
vb@114
   724
    assert(r == MAILIMF_NO_ERROR);
vb@114
   725
    if (r == MAILIMF_ERROR_MEMORY) {
vb@114
   726
        goto enomem;
vb@114
   727
    }
vb@114
   728
    else {
vb@114
   729
        // mailmime_smart_add_part() takes ownership of submime
vb@114
   730
        submime = NULL;
vb@114
   731
    }
vb@114
   732
vb@114
   733
    submime = get_text_part("msg.asc", "application/octet-stream", plaintext,
vb@114
   734
            strlen(plaintext), MAILMIME_MECHANISM_7BIT);
vb@114
   735
    assert(submime);
vb@114
   736
    if (submime == NULL)
vb@114
   737
        goto enomem;
vb@114
   738
vb@114
   739
    r = mailmime_smart_add_part(mime, submime);
vb@114
   740
    assert(r == MAILIMF_NO_ERROR);
vb@114
   741
    if (r == MAILIMF_ERROR_MEMORY) {
vb@114
   742
        goto enomem;
vb@114
   743
    }
vb@114
   744
    else {
vb@114
   745
        // mailmime_smart_add_part() takes ownership of submime
vb@114
   746
        submime = NULL;
vb@114
   747
    }
vb@114
   748
vb@114
   749
    *result = mime;
vb@114
   750
    return PEP_STATUS_OK;
vb@114
   751
vb@114
   752
enomem:
vb@114
   753
    status = PEP_OUT_OF_MEMORY;
vb@114
   754
vb@114
   755
pep_error:
vb@114
   756
    if (mime)
vb@114
   757
        mailmime_free(mime);
vb@114
   758
vb@114
   759
    if (submime)
vb@114
   760
        mailmime_free(submime);
vb@114
   761
vb@114
   762
    return status;
vb@114
   763
}
vb@114
   764
vb@114
   765
DYNAMIC_API PEP_STATUS mime_encode_message(
vb@114
   766
        const message * msg,
vb@114
   767
        bool omit_fields,
vb@114
   768
        char **mimetext
vb@114
   769
    )
vb@114
   770
{
vb@114
   771
    PEP_STATUS status = PEP_STATUS_OK;
vb@114
   772
    struct mailmime * msg_mime = NULL;
vb@114
   773
    struct mailmime * mime = NULL;
vb@114
   774
    struct mailimf_fields * fields = NULL;
vb@114
   775
    char *buf = NULL;
vb@114
   776
    int r;
vb@114
   777
vb@114
   778
    assert(msg);
vb@114
   779
    assert(msg->mime == PEP_MIME_none);
vb@114
   780
    assert(mimetext);
vb@114
   781
vb@114
   782
    *mimetext = NULL;
vb@114
   783
vb@114
   784
    switch (msg->enc_format) {
vb@114
   785
        case PEP_enc_none:
vb@114
   786
            status = mime_encode_message_plain(msg, omit_fields, &mime);
vb@114
   787
            break;
vb@114
   788
vb@114
   789
        case PEP_enc_pieces:
vb@114
   790
            status = mime_encode_message_plain(msg, omit_fields, &mime);
vb@114
   791
            break;
vb@114
   792
vb@114
   793
        case PEP_enc_S_MIME:
vb@114
   794
            NOT_IMPLEMENTED
vb@114
   795
                
vb@114
   796
        case PEP_enc_PGP_MIME:
vb@114
   797
            status = mime_encode_message_PGP_MIME(msg, omit_fields, &mime);
vb@114
   798
            break;
vb@114
   799
vb@114
   800
        case PEP_enc_PEP:
vb@114
   801
            NOT_IMPLEMENTED
vb@114
   802
    }
vb@114
   803
vb@114
   804
    if (status != PEP_STATUS_OK)
vb@114
   805
        goto pep_error;
vb@114
   806
vb@89
   807
    msg_mime = mailmime_new_message_data(NULL);
vb@90
   808
    assert(msg_mime);
vb@90
   809
    if (msg_mime == NULL)
vb@90
   810
        goto enomem;
vb@89
   811
vb@90
   812
    r = mailmime_add_part(msg_mime, mime);
vb@90
   813
    if (r) {
vb@90
   814
        mailmime_free(mime);
vb@90
   815
        goto enomem;
vb@90
   816
    }
vb@114
   817
    mime = NULL;
vb@90
   818
vb@113
   819
    if (!omit_fields) {
vb@113
   820
        status = build_fields(msg, &fields);
vb@113
   821
        if (status != PEP_STATUS_OK)
vb@113
   822
            goto pep_error;
vb@90
   823
vb@113
   824
        mailmime_set_imf_fields(msg_mime, fields);
vb@113
   825
    }
vb@90
   826
vb@90
   827
    status = render_mime(msg_mime, &buf);
vb@89
   828
    if (status != PEP_STATUS_OK)
vb@89
   829
        goto pep_error;
vb@89
   830
vb@89
   831
    mailmime_free(msg_mime);
vb@89
   832
    *mimetext = buf;
vb@90
   833
vb@89
   834
    return PEP_STATUS_OK;
vb@89
   835
vb@89
   836
enomem:
vb@89
   837
    status = PEP_OUT_OF_MEMORY;
vb@89
   838
vb@89
   839
pep_error:
vb@89
   840
    if (msg_mime)
vb@90
   841
        mailmime_free(msg_mime);
vb@89
   842
    else
vb@89
   843
        if (mime)
vb@89
   844
            mailmime_free(mime);
vb@89
   845
vb@89
   846
    return status;
vb@89
   847
}
vb@89
   848
vb@94
   849
static pEp_identity *mailbox_to_identity(const struct mailimf_mailbox * mb)
vb@94
   850
{
vb@94
   851
    pEp_identity *ident;
vb@94
   852
    char *username = NULL;
vb@94
   853
    size_t index;
vb@94
   854
    int r;
vb@94
   855
vb@94
   856
    index = 0;
vb@94
   857
    r = mailmime_encoded_phrase_parse("utf-8", mb->mb_display_name,
vb@94
   858
            strlen(mb->mb_display_name), &index, "utf-8", &username);
vb@94
   859
    if (r)
vb@94
   860
        goto enomem;
vb@94
   861
vb@94
   862
    ident = new_identity(mb->mb_addr_spec, NULL, NULL, username);
vb@94
   863
    if (ident == NULL)
vb@94
   864
        goto enomem;
vb@94
   865
    free(username);
vb@94
   866
vb@94
   867
    return ident;
vb@94
   868
vb@94
   869
enomem:
vb@94
   870
    free(username);
vb@94
   871
vb@94
   872
    return NULL;
vb@94
   873
}
vb@94
   874
vb@94
   875
static pEp_identity * mbl_to_identity(const struct mailimf_mailbox_list * mbl)
vb@94
   876
{
vb@94
   877
    struct mailimf_mailbox * mb = clist_content(clist_begin(mbl->mb_list));
vb@94
   878
    return mailbox_to_identity(mb);
vb@94
   879
}
vb@94
   880
vb@94
   881
static identity_list * mal_to_identity_list(
vb@94
   882
        const struct mailimf_address_list *mal
vb@94
   883
    )
vb@94
   884
{
vb@94
   885
    PEP_STATUS status = PEP_STATUS_OK;
vb@94
   886
    identity_list *il = NULL;
vb@94
   887
    clist *list = mal->ad_list;
vb@94
   888
    struct mailimf_address * addr = NULL;
vb@94
   889
    struct mailimf_mailbox *mb = NULL;
vb@94
   890
    clistiter *cur;
vb@94
   891
    int r;
vb@94
   892
vb@94
   893
    assert(mal);
vb@94
   894
vb@94
   895
    il = new_identity_list(NULL);
vb@94
   896
    if (il == NULL)
vb@94
   897
        goto enomem;
vb@94
   898
vb@94
   899
    identity_list *_il = il;
vb@94
   900
    for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@94
   901
        pEp_identity *ident;
vb@94
   902
vb@94
   903
        addr = clist_content(cur);
vb@94
   904
        switch(addr->ad_type) {
vb@94
   905
            case MAILIMF_ADDRESS_MAILBOX:
vb@94
   906
                ident = mailbox_to_identity(addr->ad_data.ad_mailbox);
vb@94
   907
                if (ident == NULL)
vb@94
   908
                    goto enomem;
vb@94
   909
                _il = identity_list_add(_il, ident);
vb@94
   910
                if (_il == NULL)
vb@94
   911
                    goto enomem;
vb@94
   912
                break;
vb@94
   913
vb@94
   914
            case MAILIMF_ADDRESS_GROUP:
vb@94
   915
                {
vb@94
   916
                    clistiter *cur2;
vb@94
   917
                    struct mailimf_mailbox_list * mbl =
vb@94
   918
                            addr->ad_data.ad_group->grp_mb_list;
vb@94
   919
                    for (cur2 = clist_begin(mbl->mb_list); cur2 != NULL;
vb@94
   920
                            cur2 = clist_next(cur2)) {
vb@94
   921
                        ident = mailbox_to_identity(clist_content(cur));
vb@94
   922
                        if (ident == NULL)
vb@94
   923
                            goto enomem;
vb@94
   924
                        _il = identity_list_add(_il, ident);
vb@94
   925
                        if (_il == NULL)
vb@94
   926
                            goto enomem;
vb@94
   927
                    }
vb@94
   928
                }
vb@94
   929
                break;
vb@94
   930
vb@94
   931
            default:
vb@94
   932
                assert(0);
vb@94
   933
                goto enomem;
vb@94
   934
        }
vb@94
   935
    }
vb@94
   936
vb@94
   937
    return il;
vb@94
   938
vb@94
   939
enomem:
vb@94
   940
    free_identity_list(il);
vb@94
   941
vb@94
   942
    return NULL;
vb@94
   943
}
vb@94
   944
vb@94
   945
static stringlist_t * clist_to_stringlist(const clist *list)
vb@94
   946
{
vb@94
   947
    char *text = NULL;;
vb@94
   948
    stringlist_t * sl = new_stringlist(NULL);
vb@94
   949
    if (sl == NULL)
vb@94
   950
        return NULL;
vb@94
   951
vb@94
   952
    clistiter *cur;
vb@94
   953
    stringlist_t *_sl = sl;
vb@94
   954
    for (cur = clist_begin(list); cur != NULL; cur = clist_next(cur)) {
vb@94
   955
        char *phrase = clist_content(cur);
vb@94
   956
        size_t index;
vb@94
   957
        int r;
vb@94
   958
vb@94
   959
        index = 0;
vb@94
   960
        r = mailmime_encoded_phrase_parse("utf-8", phrase, strlen(phrase),
vb@94
   961
                &index, "utf-8", &text);
vb@94
   962
        if (r)
vb@94
   963
            goto enomem;
vb@94
   964
vb@94
   965
        _sl = stringlist_add(_sl, text);
vb@94
   966
        if (_sl == NULL)
vb@94
   967
            goto enomem;
vb@94
   968
vb@94
   969
        free(text);
vb@94
   970
        text = NULL;
vb@94
   971
    }
vb@94
   972
vb@94
   973
    return _sl;
vb@94
   974
vb@94
   975
enomem:
vb@94
   976
    free_stringlist(sl);
vb@94
   977
    free(text);
vb@94
   978
vb@94
   979
    return NULL;
vb@94
   980
}
vb@94
   981
vb@94
   982
static PEP_STATUS read_fields(message *msg, clist *fieldlist)
vb@94
   983
{
vb@94
   984
    PEP_STATUS status = PEP_STATUS_OK;
vb@94
   985
    struct mailimf_field * _field;
vb@94
   986
    clistiter *cur;
vb@94
   987
    size_t index;
vb@94
   988
    int r;
vb@110
   989
    stringpair_list_t *opt = msg->opt_fields;
vb@94
   990
vb@94
   991
    for (cur = clist_begin(fieldlist); cur != NULL; cur = clist_next(cur)) {
vb@94
   992
        _field = clist_content(cur);
vb@94
   993
vb@94
   994
        switch (_field->fld_type) {
vb@94
   995
            case MAILIMF_FIELD_MESSAGE_ID:
vb@94
   996
                {
vb@94
   997
                    char * text = _field->fld_data.fld_message_id->mid_value;
vb@116
   998
vb@116
   999
                    free(msg->id);
vb@94
  1000
                    index = 0;
vb@94
  1001
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1002
                            strlen(text), &index, "utf-8", &msg->id);
vb@94
  1003
                    if (r)
vb@94
  1004
                        goto enomem;
vb@94
  1005
                }
vb@94
  1006
                break;
vb@94
  1007
vb@94
  1008
            case MAILIMF_FIELD_SUBJECT:
vb@94
  1009
                {
vb@94
  1010
                    char * text = _field->fld_data.fld_subject->sbj_value;
vb@116
  1011
vb@116
  1012
                    free(msg->shortmsg);
vb@94
  1013
                    index = 0;
vb@94
  1014
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1015
                            strlen(text), &index, "utf-8", &msg->shortmsg);
vb@94
  1016
                    if (r)
vb@94
  1017
                        goto enomem;
vb@94
  1018
                }
vb@94
  1019
                break;
vb@94
  1020
vb@94
  1021
            case MAILIMF_FIELD_ORIG_DATE:
vb@94
  1022
                {
vb@94
  1023
                    struct mailimf_date_time *date =
vb@94
  1024
                        _field->fld_data.fld_orig_date->dt_date_time;
vb@116
  1025
vb@116
  1026
                    free_timestamp(msg->sent);
vb@94
  1027
                    msg->sent = etpantime_to_timestamp(date);
vb@94
  1028
                    if (msg->sent == NULL)
vb@94
  1029
                        goto enomem;
vb@94
  1030
                }
vb@94
  1031
                break;
vb@94
  1032
vb@94
  1033
            case MAILIMF_FIELD_FROM:
vb@94
  1034
                {
vb@94
  1035
                    struct mailimf_mailbox_list *mbl =
vb@94
  1036
                            _field->fld_data.fld_from->frm_mb_list;
vb@94
  1037
                    pEp_identity *ident;
vb@94
  1038
vb@94
  1039
                    ident = mbl_to_identity(mbl);
vb@94
  1040
                    if (ident == NULL)
vb@94
  1041
                        goto pep_error;
vb@94
  1042
vb@116
  1043
                    free_identity(msg->from);
vb@94
  1044
                    msg->from = ident;
vb@94
  1045
                }
vb@94
  1046
                break;
vb@94
  1047
vb@94
  1048
            case MAILIMF_FIELD_TO:
vb@94
  1049
                {
vb@94
  1050
                    struct mailimf_address_list *mal =
vb@94
  1051
                            _field->fld_data.fld_to->to_addr_list;
vb@94
  1052
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1053
                    if (il == NULL)
vb@94
  1054
                        goto enomem;
vb@116
  1055
vb@116
  1056
                    free_identity_list(msg->to);
vb@94
  1057
                    msg->to = il;
vb@94
  1058
                }
vb@94
  1059
                break;
vb@94
  1060
vb@94
  1061
            case MAILIMF_FIELD_CC:
vb@94
  1062
                {
vb@94
  1063
                    struct mailimf_address_list *mal =
vb@94
  1064
                            _field->fld_data.fld_cc->cc_addr_list;
vb@94
  1065
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1066
                    if (il == NULL)
vb@94
  1067
                        goto enomem;
vb@116
  1068
vb@116
  1069
                    free_identity_list(msg->cc);
vb@94
  1070
                    msg->cc = il;
vb@94
  1071
                }
vb@94
  1072
                break;
vb@94
  1073
vb@94
  1074
            case MAILIMF_FIELD_BCC:
vb@94
  1075
                {
vb@94
  1076
                    struct mailimf_address_list *mal =
vb@94
  1077
                            _field->fld_data.fld_bcc->bcc_addr_list;
vb@94
  1078
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1079
                    if (il == NULL)
vb@94
  1080
                        goto enomem;
vb@116
  1081
vb@116
  1082
                    free_identity_list(msg->bcc);
vb@94
  1083
                    msg->bcc = il;
vb@94
  1084
                }
vb@94
  1085
                break;
vb@94
  1086
vb@94
  1087
            case MAILIMF_FIELD_REPLY_TO:
vb@94
  1088
                {
vb@94
  1089
                    struct mailimf_address_list *mal =
vb@94
  1090
                            _field->fld_data.fld_reply_to->rt_addr_list;
vb@94
  1091
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1092
                    if (il == NULL)
vb@94
  1093
                        goto enomem;
vb@116
  1094
vb@116
  1095
                    free_identity_list(msg->reply_to);
vb@94
  1096
                    msg->reply_to = il;
vb@94
  1097
                }
vb@94
  1098
                break;
vb@94
  1099
vb@94
  1100
            case MAILIMF_FIELD_IN_REPLY_TO:
vb@94
  1101
                {
vb@94
  1102
                    clist *list = _field->fld_data.fld_in_reply_to->mid_list;
vb@94
  1103
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1104
                    if (sl == NULL)
vb@94
  1105
                        goto enomem;
vb@116
  1106
vb@116
  1107
                    free_stringlist(msg->in_reply_to);
vb@94
  1108
                    msg->in_reply_to = sl;
vb@94
  1109
                }
vb@94
  1110
                break;
vb@94
  1111
vb@94
  1112
            case MAILIMF_FIELD_REFERENCES:
vb@94
  1113
                {
vb@94
  1114
                    clist *list = _field->fld_data.fld_references->mid_list;
vb@94
  1115
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1116
                    if (sl == NULL)
vb@94
  1117
                        goto enomem;
vb@116
  1118
vb@116
  1119
                    free_stringlist(msg->references);
vb@94
  1120
                    msg->references = sl;
vb@94
  1121
                }
vb@94
  1122
                break;
vb@94
  1123
vb@94
  1124
            case MAILIMF_FIELD_KEYWORDS:
vb@94
  1125
                {
vb@94
  1126
                    clist *list = _field->fld_data.fld_keywords->kw_list;
vb@94
  1127
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1128
                    if (sl == NULL)
vb@94
  1129
                        goto enomem;
vb@116
  1130
vb@116
  1131
                    free_stringlist(msg->keywords);
vb@94
  1132
                    msg->keywords = sl;
vb@94
  1133
                }
vb@94
  1134
                break;
vb@94
  1135
vb@94
  1136
            case MAILIMF_FIELD_COMMENTS:
vb@94
  1137
                {
vb@94
  1138
                    char * text = _field->fld_data.fld_comments->cm_value;
vb@116
  1139
vb@116
  1140
                    free(msg->comments);
vb@94
  1141
                    index = 0;
vb@94
  1142
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1143
                            strlen(text), &index, "utf-8", &msg->comments);
vb@94
  1144
                    if (r)
vb@94
  1145
                        goto enomem;
vb@94
  1146
                }
vb@94
  1147
                break;
vb@110
  1148
vb@110
  1149
            case MAILIMF_FIELD_OPTIONAL_FIELD:
vb@110
  1150
                {
vb@110
  1151
                    char * name =
vb@110
  1152
                            _field->fld_data.fld_optional_field->fld_name;
vb@110
  1153
                    char * value =
vb@110
  1154
                            _field->fld_data.fld_optional_field->fld_value;
vb@110
  1155
                    char *_value;
vb@110
  1156
vb@110
  1157
                    index = 0;
vb@110
  1158
                    r = mailmime_encoded_phrase_parse("utf-8", value,
vb@110
  1159
                            strlen(value), &index, "utf-8", &_value);
vb@110
  1160
                    if (r)
vb@110
  1161
                        goto enomem;
vb@110
  1162
vb@110
  1163
                    stringpair_t pair;
vb@110
  1164
                    pair.key = name;
vb@110
  1165
                    pair.value = _value;
vb@110
  1166
vb@110
  1167
                    opt = stringpair_list_add(opt, &pair);
vb@110
  1168
                    free(_value);
vb@110
  1169
                    if (opt == NULL)
vb@110
  1170
                        goto enomem;
vb@110
  1171
vb@110
  1172
                    if (msg->opt_fields == NULL)
vb@110
  1173
                        msg->opt_fields = opt;
vb@110
  1174
                }
vb@110
  1175
                break;
vb@94
  1176
        }
vb@94
  1177
    }
vb@94
  1178
vb@94
  1179
    return PEP_STATUS_OK;
vb@94
  1180
vb@94
  1181
enomem:
vb@94
  1182
    status = PEP_OUT_OF_MEMORY;
vb@94
  1183
vb@94
  1184
pep_error:
vb@94
  1185
    return status;
vb@94
  1186
}
vb@94
  1187
vb@118
  1188
static PEP_STATUS interpret_MIME(
vb@118
  1189
        struct mailmime *mime,
vb@118
  1190
        message *msg
vb@118
  1191
    )
vb@118
  1192
{
vb@118
  1193
    PEP_STATUS status = PEP_STATUS_OK;
vb@118
  1194
vb@118
  1195
    assert(mime);
vb@118
  1196
    assert(msg);
vb@118
  1197
vb@118
  1198
    struct mailmime_content *content = mime->mm_content_type;
vb@118
  1199
    if (content) {
vb@118
  1200
        if (msg->longmsg == NULL && content->ct_type &&
vb@118
  1201
                content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
vb@118
  1202
                && content->ct_type->tp_data.tp_composite_type
vb@118
  1203
                && content->ct_type->tp_data.tp_composite_type->ct_type ==
vb@118
  1204
                        MAILMIME_COMPOSITE_TYPE_MULTIPART
vb@118
  1205
                && content->ct_subtype
vb@118
  1206
                && strcmp(content->ct_subtype, "alternative") == 0) {
vb@118
  1207
vb@118
  1208
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1209
            if (partlist == NULL)
vb@118
  1210
                return PEP_ILLEGAL_VALUE;
vb@118
  1211
vb@118
  1212
            clistiter *cur;
vb@118
  1213
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@120
  1214
                size_t index;
vb@120
  1215
                int r;
vb@120
  1216
                struct mailmime *part = clist_content(cur);
vb@118
  1217
                if (part == NULL)
vb@118
  1218
                    return PEP_ILLEGAL_VALUE;
vb@118
  1219
vb@120
  1220
                content = part->mm_content_type;
vb@120
  1221
                assert(content);
vb@120
  1222
                if (content == NULL)
vb@120
  1223
                    return PEP_ILLEGAL_VALUE;
vb@118
  1224
vb@120
  1225
                if (content->ct_type == NULL)
vb@120
  1226
                    return PEP_ILLEGAL_VALUE;
vb@120
  1227
vb@120
  1228
                switch (content->ct_type->tp_type) {
vb@120
  1229
                    case MAILMIME_TYPE_DISCRETE_TYPE:
vb@120
  1230
                        if (content->ct_type->tp_data.tp_discrete_type == NULL)
vb@120
  1231
                            return PEP_ILLEGAL_VALUE;
vb@120
  1232
vb@120
  1233
                        switch (content->ct_type->tp_data.tp_discrete_type->
vb@120
  1234
                                dt_type) {
vb@120
  1235
                            case MAILMIME_DISCRETE_TYPE_TEXT:
vb@120
  1236
                                if (strcmp(content->ct_subtype, "plain") ==
vb@120
  1237
                                        0) {
vb@120
  1238
                                    const char *text;
vb@120
  1239
                                    size_t length;
vb@120
  1240
vb@120
  1241
                                    if (part->mm_body == NULL)
vb@120
  1242
                                        return PEP_ILLEGAL_VALUE;
vb@120
  1243
vb@120
  1244
                                    text = part->mm_body->
vb@120
  1245
                                            dt_data.dt_text.dt_data;
vb@120
  1246
                                    length =
vb@120
  1247
                                        part->mm_body->dt_data.dt_text.dt_length;
vb@120
  1248
                                    index = 0;
vb@120
  1249
                                    r = mailmime_encoded_phrase_parse(
vb@120
  1250
                                            "utf-8", text, length, &index,
vb@120
  1251
                                            "utf-8", &msg->longmsg);
vb@120
  1252
                                    if (r)
vb@120
  1253
                                        return PEP_ILLEGAL_VALUE;
vb@120
  1254
                                }
vb@120
  1255
                                else if (strcmp(content->ct_subtype, "html") ==
vb@120
  1256
                                        0) {
vb@120
  1257
                                    const char *html;
vb@120
  1258
                                    size_t length;
vb@120
  1259
vb@120
  1260
                                    if (part->mm_body == NULL)
vb@120
  1261
                                        return PEP_ILLEGAL_VALUE;
vb@120
  1262
vb@120
  1263
                                    html = part->mm_body->
vb@120
  1264
                                            dt_data.dt_text.dt_data;
vb@120
  1265
                                    length =
vb@120
  1266
                                        part->mm_body->dt_data.dt_text.dt_length;
vb@120
  1267
                                    index = 0;
vb@120
  1268
                                    r = mailmime_encoded_phrase_parse(
vb@120
  1269
                                            "utf-8", html, length, &index,
vb@120
  1270
                                            "utf-8", &msg->longmsg_formatted);
vb@120
  1271
                                    if (r)
vb@120
  1272
                                        return PEP_ILLEGAL_VALUE;
vb@120
  1273
                                }
vb@120
  1274
                                else {
vb@120
  1275
                                    return interpret_MIME(part, msg);
vb@120
  1276
                                }
vb@120
  1277
vb@120
  1278
                                break;
vb@120
  1279
                                
vb@120
  1280
                            case MAILMIME_DISCRETE_TYPE_IMAGE:
vb@120
  1281
                            case MAILMIME_DISCRETE_TYPE_AUDIO:
vb@120
  1282
                            case MAILMIME_DISCRETE_TYPE_VIDEO:
vb@120
  1283
                            case MAILMIME_DISCRETE_TYPE_APPLICATION:
vb@120
  1284
                            case MAILMIME_DISCRETE_TYPE_EXTENSION:
vb@120
  1285
                                    return interpret_MIME(part, msg);
vb@120
  1286
vb@120
  1287
                                break;
vb@120
  1288
                                
vb@120
  1289
                            default:
vb@120
  1290
                                return PEP_ILLEGAL_VALUE;
vb@120
  1291
                        }
vb@120
  1292
                        break;
vb@120
  1293
vb@120
  1294
                    case MAILMIME_TYPE_COMPOSITE_TYPE:
vb@120
  1295
vb@120
  1296
                        break;
vb@120
  1297
                    default:
vb@120
  1298
                        return PEP_ILLEGAL_VALUE;
vb@120
  1299
                }
vb@118
  1300
            }
vb@118
  1301
        }
vb@118
  1302
        if (content->ct_type &&
vb@118
  1303
                content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
vb@118
  1304
                && content->ct_type->tp_data.tp_composite_type
vb@118
  1305
                && content->ct_type->tp_data.tp_composite_type->ct_type ==
vb@118
  1306
                        MAILMIME_COMPOSITE_TYPE_MULTIPART) {
vb@118
  1307
vb@118
  1308
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1309
            if (partlist == NULL)
vb@118
  1310
                return PEP_ILLEGAL_VALUE;
vb@118
  1311
vb@118
  1312
            clistiter *cur;
vb@118
  1313
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@118
  1314
                struct mailmime *part= clist_content(cur);
vb@118
  1315
                if (part == NULL)
vb@118
  1316
                    return PEP_ILLEGAL_VALUE;
vb@118
  1317
vb@118
  1318
                status = interpret_MIME(part, msg);
vb@118
  1319
                if (status != PEP_STATUS_OK)
vb@118
  1320
                    return status;
vb@118
  1321
            }
vb@118
  1322
        }
vb@118
  1323
        else {
vb@118
  1324
vb@118
  1325
        }
vb@118
  1326
    }
vb@118
  1327
vb@118
  1328
    return PEP_STATUS_OK;
vb@118
  1329
}
vb@118
  1330
vb@117
  1331
static PEP_STATUS interpret_PGP_MIME(struct mailmime *mime, message *msg)
vb@116
  1332
{
vb@116
  1333
    assert(mime);
vb@116
  1334
    assert(msg);
vb@116
  1335
vb@117
  1336
    clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@117
  1337
    if (partlist == NULL)
vb@117
  1338
        return PEP_ILLEGAL_VALUE;
vb@116
  1339
vb@117
  1340
    clistiter *cur = clist_begin(partlist);
vb@117
  1341
    if (cur == NULL)
vb@117
  1342
        return PEP_ILLEGAL_VALUE;
vb@116
  1343
vb@117
  1344
    cur = clist_next(cur);
vb@117
  1345
    if (cur == NULL)
vb@117
  1346
        return PEP_ILLEGAL_VALUE;
vb@117
  1347
vb@117
  1348
    struct mailmime *second = clist_content(cur);
vb@117
  1349
    if (second == NULL)
vb@117
  1350
        return PEP_ILLEGAL_VALUE;
vb@117
  1351
vb@117
  1352
    if (second->mm_body == NULL)
vb@117
  1353
        return PEP_ILLEGAL_VALUE;
vb@117
  1354
vb@117
  1355
    const char *text = second->mm_body->dt_data.dt_text.dt_data;
vb@117
  1356
    if (text == NULL)
vb@117
  1357
        return PEP_ILLEGAL_VALUE;
vb@117
  1358
vb@117
  1359
    char *_text = strdup(text);
vb@117
  1360
    if (_text == NULL)
vb@117
  1361
        return PEP_OUT_OF_MEMORY;
vb@117
  1362
vb@117
  1363
    msg->mime = PEP_MIME_fields_omitted;
vb@117
  1364
    msg->enc_format = PEP_enc_PGP_MIME;
vb@117
  1365
vb@117
  1366
    free(msg->longmsg);
vb@117
  1367
    msg->longmsg = _text;
vb@117
  1368
vb@116
  1369
    return PEP_STATUS_OK;
vb@116
  1370
}
vb@116
  1371
vb@116
  1372
static bool parameter_has_value(
vb@116
  1373
        clist *list,
vb@116
  1374
        const char *name,
vb@116
  1375
        const char *value
vb@116
  1376
    )
vb@116
  1377
{
vb@116
  1378
    clistiter *cur;
vb@116
  1379
vb@117
  1380
    assert(list);
vb@117
  1381
    assert(name);
vb@117
  1382
    assert(value);
vb@117
  1383
vb@116
  1384
    for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@116
  1385
        struct mailmime_parameter * param = clist_content(cur);
vb@117
  1386
        if (param &&
vb@117
  1387
                param->pa_name && strcmp(name, param->pa_name) == 0 &&
vb@117
  1388
                param->pa_value && strcmp(value, param->pa_value) == 0)
vb@116
  1389
            return true;
vb@116
  1390
    }
vb@116
  1391
vb@116
  1392
    return false;
vb@116
  1393
}
vb@116
  1394
vb@89
  1395
DYNAMIC_API PEP_STATUS mime_decode_message(
vb@89
  1396
        const char *mimetext,
vb@89
  1397
        message **msg
vb@89
  1398
    )
vb@89
  1399
{
vb@89
  1400
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
  1401
    struct mailmime * mime = NULL;
vb@89
  1402
    int r;
vb@94
  1403
    message *_msg = NULL;
vb@94
  1404
    size_t index;
vb@89
  1405
vb@89
  1406
    assert(mimetext);
vb@89
  1407
    assert(msg);
vb@89
  1408
vb@89
  1409
    *msg = NULL;
vb@113
  1410
vb@94
  1411
    index = 0;
vb@89
  1412
    r = mailmime_parse(mimetext, strlen(mimetext), &index, &mime);
vb@89
  1413
    assert(r == 0);
vb@89
  1414
    assert(mime);
vb@90
  1415
    if (r) {
vb@89
  1416
        if (r == MAILIMF_ERROR_MEMORY)
vb@89
  1417
            goto enomem;
vb@89
  1418
        else
vb@89
  1419
            goto err_mime;
vb@89
  1420
    }
vb@89
  1421
vb@117
  1422
    _msg = calloc(1, sizeof(message));
vb@117
  1423
    assert(_msg);
vb@117
  1424
    if (_msg == NULL)
vb@117
  1425
        goto enomem;
vb@116
  1426
vb@117
  1427
    if (mime->mm_data.mm_message.mm_fields
vb@117
  1428
            && mime->mm_data.mm_message.mm_fields->fld_list) {
vb@117
  1429
        clist * _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
vb@117
  1430
        status = read_fields(_msg, _fieldlist);
vb@116
  1431
        if (status != PEP_STATUS_OK)
vb@116
  1432
            goto pep_error;
vb@116
  1433
    }
vb@116
  1434
vb@117
  1435
    if (mime->mm_data.mm_message.mm_msg_mime) {
vb@117
  1436
        struct mailmime_content *content =
vb@117
  1437
                mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
vb@117
  1438
vb@117
  1439
        if (content->ct_type &&
vb@117
  1440
                content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
vb@117
  1441
                && content->ct_type->tp_data.tp_composite_type
vb@117
  1442
                && content->ct_type->tp_data.tp_composite_type->ct_type ==
vb@117
  1443
                        MAILMIME_COMPOSITE_TYPE_MULTIPART
vb@117
  1444
                && content->ct_subtype
vb@117
  1445
                && strcmp(content->ct_subtype, "encrypted") == 0
vb@117
  1446
                && content->ct_parameters
vb@117
  1447
                && parameter_has_value(content->ct_parameters, "protocol",
vb@117
  1448
                        "application/pgp-encrypted")) {
vb@117
  1449
vb@117
  1450
            status = interpret_PGP_MIME(mime->mm_data.mm_message.mm_msg_mime,
vb@117
  1451
                    _msg);
vb@117
  1452
            if (status != PEP_STATUS_OK)
vb@117
  1453
                goto pep_error;
vb@117
  1454
        }
vb@117
  1455
        else {
vb@118
  1456
            status = interpret_MIME(mime->mm_data.mm_message.mm_msg_mime,
vb@118
  1457
                    _msg);
vb@118
  1458
            if (status != PEP_STATUS_OK)
vb@118
  1459
                goto pep_error;
vb@117
  1460
        }
vb@116
  1461
    }
vb@116
  1462
vb@89
  1463
    mailmime_free(mime);
vb@94
  1464
    *msg = _msg;
vb@89
  1465
vb@89
  1466
    return status;
vb@89
  1467
vb@89
  1468
err_mime:
vb@89
  1469
    status = PEP_ILLEGAL_VALUE;
vb@89
  1470
    goto pep_error;
vb@89
  1471
vb@89
  1472
enomem:
vb@89
  1473
    status = PEP_OUT_OF_MEMORY;
vb@89
  1474
vb@89
  1475
pep_error:
vb@94
  1476
    free_message(_msg);
vb@94
  1477
vb@89
  1478
    if (mime)
vb@89
  1479
        mailmime_free(mime);
vb@89
  1480
vb@89
  1481
    return status;
vb@89
  1482
}
vb@89
  1483