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