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