src/mime.c
author Volker Birk <vb@pep-project.org>
Sun, 29 Mar 2015 21:03:41 +0200
changeset 159 ad6d0993d1ab
parent 157 63eb5372f2ef
child 160 ca9b52c362cb
permissions -rw-r--r--
char conversion to utf-8
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@159
   848
    assert(mb);
vb@159
   849
    assert(mb->mb_addr_spec);
vb@159
   850
vb@159
   851
    if (mb->mb_addr_spec == NULL)
vb@159
   852
        return NULL;
vb@159
   853
vb@159
   854
    if (mb->mb_display_name) {
vb@159
   855
        index = 0;
vb@159
   856
        r = mailmime_encoded_phrase_parse("utf-8", mb->mb_display_name,
vb@159
   857
                strlen(mb->mb_display_name), &index, "utf-8", &username);
vb@159
   858
        if (r)
vb@159
   859
            goto enomem;
vb@159
   860
    }
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
vb@94
   892
    assert(mal);
vb@94
   893
vb@94
   894
    il = new_identity_list(NULL);
vb@94
   895
    if (il == NULL)
vb@94
   896
        goto enomem;
vb@94
   897
vb@94
   898
    identity_list *_il = il;
vb@94
   899
    for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@94
   900
        pEp_identity *ident;
vb@94
   901
vb@94
   902
        addr = clist_content(cur);
vb@94
   903
        switch(addr->ad_type) {
vb@94
   904
            case MAILIMF_ADDRESS_MAILBOX:
vb@94
   905
                ident = mailbox_to_identity(addr->ad_data.ad_mailbox);
vb@94
   906
                if (ident == NULL)
vb@94
   907
                    goto enomem;
vb@94
   908
                _il = identity_list_add(_il, ident);
vb@94
   909
                if (_il == NULL)
vb@94
   910
                    goto enomem;
vb@94
   911
                break;
vb@94
   912
vb@94
   913
            case MAILIMF_ADDRESS_GROUP:
vb@94
   914
                {
vb@94
   915
                    clistiter *cur2;
vb@94
   916
                    struct mailimf_mailbox_list * mbl =
vb@94
   917
                            addr->ad_data.ad_group->grp_mb_list;
vb@94
   918
                    for (cur2 = clist_begin(mbl->mb_list); cur2 != NULL;
vb@94
   919
                            cur2 = clist_next(cur2)) {
vb@94
   920
                        ident = mailbox_to_identity(clist_content(cur));
vb@94
   921
                        if (ident == NULL)
vb@94
   922
                            goto enomem;
vb@94
   923
                        _il = identity_list_add(_il, ident);
vb@94
   924
                        if (_il == NULL)
vb@94
   925
                            goto enomem;
vb@94
   926
                    }
vb@94
   927
                }
vb@94
   928
                break;
vb@94
   929
vb@94
   930
            default:
vb@94
   931
                assert(0);
vb@94
   932
                goto enomem;
vb@94
   933
        }
vb@94
   934
    }
vb@94
   935
vb@94
   936
    return il;
vb@94
   937
vb@94
   938
enomem:
vb@94
   939
    free_identity_list(il);
vb@94
   940
vb@94
   941
    return NULL;
vb@94
   942
}
vb@94
   943
vb@94
   944
static stringlist_t * clist_to_stringlist(const clist *list)
vb@94
   945
{
vb@94
   946
    char *text = NULL;;
vb@94
   947
    stringlist_t * sl = new_stringlist(NULL);
vb@94
   948
    if (sl == NULL)
vb@94
   949
        return NULL;
vb@94
   950
vb@94
   951
    clistiter *cur;
vb@94
   952
    stringlist_t *_sl = sl;
vb@94
   953
    for (cur = clist_begin(list); cur != NULL; cur = clist_next(cur)) {
vb@94
   954
        char *phrase = clist_content(cur);
vb@94
   955
        size_t index;
vb@94
   956
        int r;
vb@94
   957
vb@94
   958
        index = 0;
vb@94
   959
        r = mailmime_encoded_phrase_parse("utf-8", phrase, strlen(phrase),
vb@94
   960
                &index, "utf-8", &text);
vb@94
   961
        if (r)
vb@94
   962
            goto enomem;
vb@94
   963
vb@94
   964
        _sl = stringlist_add(_sl, text);
vb@94
   965
        if (_sl == NULL)
vb@94
   966
            goto enomem;
vb@94
   967
vb@94
   968
        free(text);
vb@94
   969
        text = NULL;
vb@94
   970
    }
vb@94
   971
vb@94
   972
    return _sl;
vb@94
   973
vb@94
   974
enomem:
vb@94
   975
    free_stringlist(sl);
vb@94
   976
    free(text);
vb@94
   977
vb@94
   978
    return NULL;
vb@94
   979
}
vb@94
   980
vb@94
   981
static PEP_STATUS read_fields(message *msg, clist *fieldlist)
vb@94
   982
{
vb@94
   983
    PEP_STATUS status = PEP_STATUS_OK;
vb@94
   984
    struct mailimf_field * _field;
vb@94
   985
    clistiter *cur;
vb@94
   986
    size_t index;
vb@94
   987
    int r;
vb@110
   988
    stringpair_list_t *opt = msg->opt_fields;
vb@94
   989
vb@94
   990
    for (cur = clist_begin(fieldlist); cur != NULL; cur = clist_next(cur)) {
vb@94
   991
        _field = clist_content(cur);
vb@94
   992
vb@94
   993
        switch (_field->fld_type) {
vb@94
   994
            case MAILIMF_FIELD_MESSAGE_ID:
vb@94
   995
                {
vb@94
   996
                    char * text = _field->fld_data.fld_message_id->mid_value;
vb@116
   997
vb@116
   998
                    free(msg->id);
vb@94
   999
                    index = 0;
vb@94
  1000
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1001
                            strlen(text), &index, "utf-8", &msg->id);
vb@94
  1002
                    if (r)
vb@94
  1003
                        goto enomem;
vb@94
  1004
                }
vb@94
  1005
                break;
vb@94
  1006
vb@94
  1007
            case MAILIMF_FIELD_SUBJECT:
vb@94
  1008
                {
vb@94
  1009
                    char * text = _field->fld_data.fld_subject->sbj_value;
vb@116
  1010
vb@116
  1011
                    free(msg->shortmsg);
vb@94
  1012
                    index = 0;
vb@94
  1013
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1014
                            strlen(text), &index, "utf-8", &msg->shortmsg);
vb@94
  1015
                    if (r)
vb@94
  1016
                        goto enomem;
vb@94
  1017
                }
vb@94
  1018
                break;
vb@94
  1019
vb@94
  1020
            case MAILIMF_FIELD_ORIG_DATE:
vb@94
  1021
                {
vb@94
  1022
                    struct mailimf_date_time *date =
vb@94
  1023
                        _field->fld_data.fld_orig_date->dt_date_time;
vb@116
  1024
vb@116
  1025
                    free_timestamp(msg->sent);
vb@94
  1026
                    msg->sent = etpantime_to_timestamp(date);
vb@94
  1027
                    if (msg->sent == NULL)
vb@94
  1028
                        goto enomem;
vb@94
  1029
                }
vb@94
  1030
                break;
vb@94
  1031
vb@94
  1032
            case MAILIMF_FIELD_FROM:
vb@94
  1033
                {
vb@94
  1034
                    struct mailimf_mailbox_list *mbl =
vb@94
  1035
                            _field->fld_data.fld_from->frm_mb_list;
vb@94
  1036
                    pEp_identity *ident;
vb@94
  1037
vb@94
  1038
                    ident = mbl_to_identity(mbl);
vb@94
  1039
                    if (ident == NULL)
vb@94
  1040
                        goto pep_error;
vb@94
  1041
vb@116
  1042
                    free_identity(msg->from);
vb@94
  1043
                    msg->from = ident;
vb@94
  1044
                }
vb@94
  1045
                break;
vb@94
  1046
vb@94
  1047
            case MAILIMF_FIELD_TO:
vb@94
  1048
                {
vb@94
  1049
                    struct mailimf_address_list *mal =
vb@94
  1050
                            _field->fld_data.fld_to->to_addr_list;
vb@94
  1051
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1052
                    if (il == NULL)
vb@94
  1053
                        goto enomem;
vb@116
  1054
vb@116
  1055
                    free_identity_list(msg->to);
vb@94
  1056
                    msg->to = il;
vb@94
  1057
                }
vb@94
  1058
                break;
vb@94
  1059
vb@94
  1060
            case MAILIMF_FIELD_CC:
vb@94
  1061
                {
vb@94
  1062
                    struct mailimf_address_list *mal =
vb@94
  1063
                            _field->fld_data.fld_cc->cc_addr_list;
vb@94
  1064
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1065
                    if (il == NULL)
vb@94
  1066
                        goto enomem;
vb@116
  1067
vb@116
  1068
                    free_identity_list(msg->cc);
vb@94
  1069
                    msg->cc = il;
vb@94
  1070
                }
vb@94
  1071
                break;
vb@94
  1072
vb@94
  1073
            case MAILIMF_FIELD_BCC:
vb@94
  1074
                {
vb@94
  1075
                    struct mailimf_address_list *mal =
vb@94
  1076
                            _field->fld_data.fld_bcc->bcc_addr_list;
vb@94
  1077
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1078
                    if (il == NULL)
vb@94
  1079
                        goto enomem;
vb@116
  1080
vb@116
  1081
                    free_identity_list(msg->bcc);
vb@94
  1082
                    msg->bcc = il;
vb@94
  1083
                }
vb@94
  1084
                break;
vb@94
  1085
vb@94
  1086
            case MAILIMF_FIELD_REPLY_TO:
vb@94
  1087
                {
vb@94
  1088
                    struct mailimf_address_list *mal =
vb@94
  1089
                            _field->fld_data.fld_reply_to->rt_addr_list;
vb@94
  1090
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1091
                    if (il == NULL)
vb@94
  1092
                        goto enomem;
vb@116
  1093
vb@116
  1094
                    free_identity_list(msg->reply_to);
vb@94
  1095
                    msg->reply_to = il;
vb@94
  1096
                }
vb@94
  1097
                break;
vb@94
  1098
vb@94
  1099
            case MAILIMF_FIELD_IN_REPLY_TO:
vb@94
  1100
                {
vb@94
  1101
                    clist *list = _field->fld_data.fld_in_reply_to->mid_list;
vb@94
  1102
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1103
                    if (sl == NULL)
vb@94
  1104
                        goto enomem;
vb@116
  1105
vb@116
  1106
                    free_stringlist(msg->in_reply_to);
vb@94
  1107
                    msg->in_reply_to = sl;
vb@94
  1108
                }
vb@94
  1109
                break;
vb@94
  1110
vb@94
  1111
            case MAILIMF_FIELD_REFERENCES:
vb@94
  1112
                {
vb@94
  1113
                    clist *list = _field->fld_data.fld_references->mid_list;
vb@94
  1114
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1115
                    if (sl == NULL)
vb@94
  1116
                        goto enomem;
vb@116
  1117
vb@116
  1118
                    free_stringlist(msg->references);
vb@94
  1119
                    msg->references = sl;
vb@94
  1120
                }
vb@94
  1121
                break;
vb@94
  1122
vb@94
  1123
            case MAILIMF_FIELD_KEYWORDS:
vb@94
  1124
                {
vb@94
  1125
                    clist *list = _field->fld_data.fld_keywords->kw_list;
vb@94
  1126
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1127
                    if (sl == NULL)
vb@94
  1128
                        goto enomem;
vb@116
  1129
vb@116
  1130
                    free_stringlist(msg->keywords);
vb@94
  1131
                    msg->keywords = sl;
vb@94
  1132
                }
vb@94
  1133
                break;
vb@94
  1134
vb@94
  1135
            case MAILIMF_FIELD_COMMENTS:
vb@94
  1136
                {
vb@94
  1137
                    char * text = _field->fld_data.fld_comments->cm_value;
vb@116
  1138
vb@116
  1139
                    free(msg->comments);
vb@94
  1140
                    index = 0;
vb@94
  1141
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1142
                            strlen(text), &index, "utf-8", &msg->comments);
vb@94
  1143
                    if (r)
vb@94
  1144
                        goto enomem;
vb@94
  1145
                }
vb@94
  1146
                break;
vb@110
  1147
vb@110
  1148
            case MAILIMF_FIELD_OPTIONAL_FIELD:
vb@110
  1149
                {
vb@110
  1150
                    char * name =
vb@110
  1151
                            _field->fld_data.fld_optional_field->fld_name;
vb@110
  1152
                    char * value =
vb@110
  1153
                            _field->fld_data.fld_optional_field->fld_value;
vb@110
  1154
                    char *_value;
vb@110
  1155
vb@110
  1156
                    index = 0;
vb@110
  1157
                    r = mailmime_encoded_phrase_parse("utf-8", value,
vb@110
  1158
                            strlen(value), &index, "utf-8", &_value);
vb@110
  1159
                    if (r)
vb@110
  1160
                        goto enomem;
vb@110
  1161
vb@110
  1162
                    stringpair_t pair;
vb@110
  1163
                    pair.key = name;
vb@110
  1164
                    pair.value = _value;
vb@110
  1165
vb@110
  1166
                    opt = stringpair_list_add(opt, &pair);
vb@110
  1167
                    free(_value);
vb@110
  1168
                    if (opt == NULL)
vb@110
  1169
                        goto enomem;
vb@110
  1170
vb@110
  1171
                    if (msg->opt_fields == NULL)
vb@110
  1172
                        msg->opt_fields = opt;
vb@110
  1173
                }
vb@110
  1174
                break;
vb@94
  1175
        }
vb@94
  1176
    }
vb@94
  1177
vb@94
  1178
    return PEP_STATUS_OK;
vb@94
  1179
vb@94
  1180
enomem:
vb@94
  1181
    status = PEP_OUT_OF_MEMORY;
vb@94
  1182
vb@94
  1183
pep_error:
vb@94
  1184
    return status;
vb@94
  1185
}
vb@94
  1186
vb@147
  1187
static PEP_STATUS interpret_body(struct mailmime *part, char **longmsg, size_t *size)
vb@147
  1188
{
vb@147
  1189
    const char *text;
vb@159
  1190
    char *_longmsg;
vb@147
  1191
    size_t length;
vb@159
  1192
    size_t _size;
vb@147
  1193
    int code;
vb@147
  1194
    int r;
vb@147
  1195
    size_t index;
vb@159
  1196
    char *type = NULL;
vb@159
  1197
    char *charset = NULL;
vb@147
  1198
vb@147
  1199
    assert(part);
vb@147
  1200
    assert(longmsg);
vb@147
  1201
vb@147
  1202
    *longmsg = NULL;
vb@147
  1203
    if (size)
vb@147
  1204
        *size = 0;
vb@147
  1205
vb@147
  1206
    if (part->mm_body == NULL)
vb@147
  1207
        return PEP_ILLEGAL_VALUE;
vb@147
  1208
vb@147
  1209
    text = part->mm_body-> dt_data.dt_text.dt_data;
vb@147
  1210
    if (text == NULL)
vb@147
  1211
        return PEP_ILLEGAL_VALUE;
vb@147
  1212
vb@147
  1213
    length = part->mm_body->dt_data.dt_text.dt_length;
vb@147
  1214
vb@147
  1215
    if (part->mm_body->dt_encoded) {
vb@147
  1216
        code = part->mm_body->dt_encoding;
vb@147
  1217
        index = 0;
vb@159
  1218
        r = mailmime_part_parse(text, length, &index, code, &_longmsg, &_size);
vb@147
  1219
        switch (r) {
vb@147
  1220
            case MAILIMF_NO_ERROR:
vb@147
  1221
                break;
vb@147
  1222
            case MAILIMF_ERROR_MEMORY:
vb@147
  1223
                return PEP_OUT_OF_MEMORY;
vb@147
  1224
            default:
vb@147
  1225
                return PEP_ILLEGAL_VALUE;
vb@147
  1226
        }
vb@147
  1227
    }
vb@147
  1228
    else {
vb@159
  1229
        _size = length;
vb@159
  1230
        _longmsg = strndup(text, _size);
vb@159
  1231
        if (_longmsg == NULL)
vb@147
  1232
            return PEP_OUT_OF_MEMORY;
vb@147
  1233
    }
vb@147
  1234
vb@159
  1235
    if (part->mm_content_type) {
vb@159
  1236
        if (_get_content_type(part->mm_content_type, &type, &charset) == 0) {
vb@159
  1237
            if (charset && strcmp(charset, "utf-8") != 0) {
vb@159
  1238
                char * _text;
vb@159
  1239
                int r = charconv("utf-8", charset, _longmsg, _size, &_text);
vb@159
  1240
                switch (r) {
vb@159
  1241
                    case MAILIMF_NO_ERROR:
vb@159
  1242
                        break;
vb@159
  1243
                    case MAILIMF_ERROR_MEMORY:
vb@159
  1244
                        return PEP_OUT_OF_MEMORY;
vb@159
  1245
                    default:
vb@159
  1246
                        return PEP_ILLEGAL_VALUE;
vb@159
  1247
                }
vb@159
  1248
                free(_longmsg);
vb@159
  1249
                _longmsg = _text;
vb@159
  1250
            }
vb@159
  1251
        }
vb@159
  1252
    }
vb@159
  1253
vb@159
  1254
    *longmsg = _longmsg;
vb@159
  1255
    if (size)
vb@159
  1256
        *size = _size;
vb@159
  1257
vb@147
  1258
    return PEP_STATUS_OK;
vb@147
  1259
}
vb@147
  1260
vb@118
  1261
static PEP_STATUS interpret_MIME(
vb@118
  1262
        struct mailmime *mime,
vb@118
  1263
        message *msg
vb@118
  1264
    )
vb@118
  1265
{
vb@118
  1266
    PEP_STATUS status = PEP_STATUS_OK;
vb@118
  1267
vb@118
  1268
    assert(mime);
vb@118
  1269
    assert(msg);
vb@118
  1270
vb@118
  1271
    struct mailmime_content *content = mime->mm_content_type;
vb@118
  1272
    if (content) {
vb@152
  1273
        if (_is_multipart(content, "alternative")) {
vb@118
  1274
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1275
            if (partlist == NULL)
vb@118
  1276
                return PEP_ILLEGAL_VALUE;
vb@118
  1277
vb@118
  1278
            clistiter *cur;
vb@118
  1279
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@120
  1280
                size_t index;
vb@120
  1281
                int r;
vb@120
  1282
                struct mailmime *part = clist_content(cur);
vb@118
  1283
                if (part == NULL)
vb@118
  1284
                    return PEP_ILLEGAL_VALUE;
vb@118
  1285
vb@120
  1286
                content = part->mm_content_type;
vb@120
  1287
                assert(content);
vb@120
  1288
                if (content == NULL)
vb@120
  1289
                    return PEP_ILLEGAL_VALUE;
vb@118
  1290
vb@152
  1291
                if (_is_text_part(content, "plain") && msg->longmsg == NULL) {
vb@152
  1292
                    status = interpret_body(part, &msg->longmsg, NULL);
vb@152
  1293
                    if (status)
vb@152
  1294
                        return status;
vb@152
  1295
                }
vb@152
  1296
                else if (_is_text_part(content, "html") &&
vb@152
  1297
                        msg->longmsg_formatted == NULL) {
vb@152
  1298
                    status = interpret_body(part, &msg->longmsg_formatted,
vb@152
  1299
                            NULL);
vb@152
  1300
                    if (status)
vb@152
  1301
                        return status;
vb@152
  1302
                }
vb@152
  1303
                else /* add as attachment */ {
vb@152
  1304
                    status = interpret_MIME(part, msg);
vb@152
  1305
                    if (status)
vb@152
  1306
                        return status;
vb@120
  1307
                }
vb@118
  1308
            }
vb@118
  1309
        }
vb@152
  1310
        else if (_is_multipart(content, NULL)) {
vb@118
  1311
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1312
            if (partlist == NULL)
vb@118
  1313
                return PEP_ILLEGAL_VALUE;
vb@118
  1314
vb@118
  1315
            clistiter *cur;
vb@118
  1316
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@118
  1317
                struct mailmime *part= clist_content(cur);
vb@118
  1318
                if (part == NULL)
vb@118
  1319
                    return PEP_ILLEGAL_VALUE;
vb@118
  1320
vb@118
  1321
                status = interpret_MIME(part, msg);
vb@118
  1322
                if (status != PEP_STATUS_OK)
vb@118
  1323
                    return status;
vb@118
  1324
            }
vb@118
  1325
        }
vb@118
  1326
        else {
vb@152
  1327
            if (_is_text_part(content, NULL) && msg->longmsg == NULL) {
vb@152
  1328
                status = interpret_body(mime, &msg->longmsg, NULL);
vb@152
  1329
                if (status)
vb@152
  1330
                    return status;
vb@152
  1331
            }
vb@152
  1332
            else {
vb@152
  1333
                char *data = NULL;
vb@157
  1334
                size_t size = 0;
vb@157
  1335
                char * mime_type;
vb@159
  1336
                char * charset;
vb@157
  1337
                char * filename;
vb@159
  1338
                int r;
vb@118
  1339
vb@159
  1340
                r = _get_content_type(content, &mime_type, &charset);
vb@159
  1341
                switch (r) {
vb@159
  1342
                    case 0:
vb@159
  1343
                        break;
vb@159
  1344
                    case EINVAL:
vb@159
  1345
                        return PEP_ILLEGAL_VALUE;
vb@159
  1346
                    case ENOMEM:
vb@159
  1347
                        return PEP_OUT_OF_MEMORY;
vb@159
  1348
                    default:
vb@159
  1349
                        return PEP_UNKNOWN_ERROR;
vb@157
  1350
                }
vb@154
  1351
vb@159
  1352
                assert(mime_type);
vb@159
  1353
vb@152
  1354
                status = interpret_body(mime, &data, &size);
vb@152
  1355
                if (status)
vb@152
  1356
                    return status;
vb@152
  1357
vb@154
  1358
                filename = _get_filename(mime);
vb@154
  1359
vb@152
  1360
                msg->attachments = bloblist_add(msg->attachments, data, size,
vb@152
  1361
                        mime_type, filename);
vb@152
  1362
                if (msg->attachments == NULL)
vb@152
  1363
                    return PEP_OUT_OF_MEMORY;
vb@152
  1364
            }
vb@118
  1365
        }
vb@118
  1366
    }
vb@118
  1367
vb@118
  1368
    return PEP_STATUS_OK;
vb@118
  1369
}
vb@118
  1370
vb@117
  1371
static PEP_STATUS interpret_PGP_MIME(struct mailmime *mime, message *msg)
vb@116
  1372
{
vb@116
  1373
    assert(mime);
vb@116
  1374
    assert(msg);
vb@116
  1375
vb@117
  1376
    clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@117
  1377
    if (partlist == NULL)
vb@117
  1378
        return PEP_ILLEGAL_VALUE;
vb@116
  1379
vb@117
  1380
    clistiter *cur = clist_begin(partlist);
vb@117
  1381
    if (cur == NULL)
vb@117
  1382
        return PEP_ILLEGAL_VALUE;
vb@116
  1383
vb@117
  1384
    cur = clist_next(cur);
vb@117
  1385
    if (cur == NULL)
vb@117
  1386
        return PEP_ILLEGAL_VALUE;
vb@117
  1387
vb@117
  1388
    struct mailmime *second = clist_content(cur);
vb@117
  1389
    if (second == NULL)
vb@117
  1390
        return PEP_ILLEGAL_VALUE;
vb@117
  1391
vb@117
  1392
    if (second->mm_body == NULL)
vb@117
  1393
        return PEP_ILLEGAL_VALUE;
vb@117
  1394
vb@117
  1395
    const char *text = second->mm_body->dt_data.dt_text.dt_data;
vb@117
  1396
    if (text == NULL)
vb@117
  1397
        return PEP_ILLEGAL_VALUE;
vb@117
  1398
vb@117
  1399
    char *_text = strdup(text);
vb@117
  1400
    if (_text == NULL)
vb@117
  1401
        return PEP_OUT_OF_MEMORY;
vb@117
  1402
vb@117
  1403
    msg->mime = PEP_MIME_fields_omitted;
vb@117
  1404
    msg->enc_format = PEP_enc_PGP_MIME;
vb@117
  1405
vb@117
  1406
    free(msg->longmsg);
vb@117
  1407
    msg->longmsg = _text;
vb@117
  1408
vb@116
  1409
    return PEP_STATUS_OK;
vb@116
  1410
}
vb@116
  1411
vb@89
  1412
DYNAMIC_API PEP_STATUS mime_decode_message(
vb@89
  1413
        const char *mimetext,
vb@89
  1414
        message **msg
vb@89
  1415
    )
vb@89
  1416
{
vb@89
  1417
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
  1418
    struct mailmime * mime = NULL;
vb@89
  1419
    int r;
vb@94
  1420
    message *_msg = NULL;
vb@94
  1421
    size_t index;
vb@89
  1422
vb@89
  1423
    assert(mimetext);
vb@89
  1424
    assert(msg);
vb@89
  1425
vb@89
  1426
    *msg = NULL;
vb@113
  1427
vb@94
  1428
    index = 0;
vb@89
  1429
    r = mailmime_parse(mimetext, strlen(mimetext), &index, &mime);
vb@89
  1430
    assert(r == 0);
vb@89
  1431
    assert(mime);
vb@90
  1432
    if (r) {
vb@89
  1433
        if (r == MAILIMF_ERROR_MEMORY)
vb@89
  1434
            goto enomem;
vb@89
  1435
        else
vb@89
  1436
            goto err_mime;
vb@89
  1437
    }
vb@89
  1438
vb@117
  1439
    _msg = calloc(1, sizeof(message));
vb@117
  1440
    assert(_msg);
vb@117
  1441
    if (_msg == NULL)
vb@117
  1442
        goto enomem;
vb@116
  1443
vb@147
  1444
    clist * _fieldlist = _get_fields(mime);
vb@147
  1445
    if (_fieldlist) {
vb@117
  1446
        status = read_fields(_msg, _fieldlist);
vb@116
  1447
        if (status != PEP_STATUS_OK)
vb@116
  1448
            goto pep_error;
vb@116
  1449
    }
vb@116
  1450
vb@147
  1451
    struct mailmime_content *content = _get_content(mime);
vb@117
  1452
vb@147
  1453
    if (content) {
vb@147
  1454
        if (_is_PGP_MIME(content)) {
vb@117
  1455
            status = interpret_PGP_MIME(mime->mm_data.mm_message.mm_msg_mime,
vb@117
  1456
                    _msg);
vb@117
  1457
            if (status != PEP_STATUS_OK)
vb@117
  1458
                goto pep_error;
vb@117
  1459
        }
vb@117
  1460
        else {
vb@118
  1461
            status = interpret_MIME(mime->mm_data.mm_message.mm_msg_mime,
vb@118
  1462
                    _msg);
vb@118
  1463
            if (status != PEP_STATUS_OK)
vb@118
  1464
                goto pep_error;
vb@117
  1465
        }
vb@116
  1466
    }
vb@116
  1467
vb@89
  1468
    mailmime_free(mime);
vb@94
  1469
    *msg = _msg;
vb@89
  1470
vb@89
  1471
    return status;
vb@89
  1472
vb@89
  1473
err_mime:
vb@89
  1474
    status = PEP_ILLEGAL_VALUE;
vb@89
  1475
    goto pep_error;
vb@89
  1476
vb@89
  1477
enomem:
vb@89
  1478
    status = PEP_OUT_OF_MEMORY;
vb@89
  1479
vb@89
  1480
pep_error:
vb@94
  1481
    free_message(_msg);
vb@94
  1482
vb@89
  1483
    if (mime)
vb@89
  1484
        mailmime_free(mime);
vb@89
  1485
vb@89
  1486
    return status;
vb@89
  1487
}
vb@89
  1488