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