src/mime.c
author Roker <roker@pep-project.org>
Thu, 14 Jul 2016 10:13:08 +0200
changeset 871 f0c1582416e3
parent 754 2aab9c3e29ca
child 974 c9a5b9b58f2c
child 977 82d54dbc4f2a
permissions -rw-r--r--
declare variables when you need them and when you can initialize them
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;
roker@723
   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
roker@723
   656
    //subject = (msg->shortmsg) ? msg->shortmsg : "pEp";  // not used, yet.
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@426
   667
            mime = get_text_part("msg.asc", "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@89
   696
vb@89
   697
            status = mime_attachment(_a, &submime);
vb@89
   698
            if (status != PEP_STATUS_OK)
vb@89
   699
                goto pep_error;
vb@89
   700
vb@89
   701
            r = mailmime_smart_add_part(mime, submime);
vb@89
   702
            assert(r == MAILIMF_NO_ERROR);
vb@89
   703
            if (r == MAILIMF_ERROR_MEMORY) {
vb@89
   704
                goto enomem;
vb@89
   705
            }
vb@89
   706
            else {
vb@89
   707
                // mailmime_smart_add_part() takes ownership of submime
vb@89
   708
                submime = NULL;
vb@89
   709
            }
vb@89
   710
        }
vb@89
   711
    }
vb@89
   712
vb@114
   713
    *result = mime;
vb@114
   714
    return PEP_STATUS_OK;
vb@114
   715
vb@114
   716
enomem:
vb@114
   717
    status = PEP_OUT_OF_MEMORY;
vb@114
   718
vb@114
   719
pep_error:
vb@114
   720
    if (mime)
vb@114
   721
        mailmime_free(mime);
vb@114
   722
vb@114
   723
    if (submime)
vb@114
   724
        mailmime_free(submime);
vb@114
   725
vb@114
   726
    return status;
vb@114
   727
}
vb@114
   728
vb@114
   729
static PEP_STATUS mime_encode_message_PGP_MIME(
vb@114
   730
        const message * msg,
vb@114
   731
        bool omit_fields,
vb@114
   732
        struct mailmime **result
vb@114
   733
    )
vb@114
   734
{
vb@114
   735
    struct mailmime * mime = NULL;
vb@114
   736
    struct mailmime * submime = NULL;
vb@114
   737
	struct mailmime_parameter * param;
vb@114
   738
    int r;
vb@114
   739
    PEP_STATUS status;
roker@723
   740
    //char *subject;
vb@114
   741
    char *plaintext;
vb@114
   742
vb@260
   743
    assert(msg->attachments && msg->attachments->next &&
vb@301
   744
            msg->attachments->next->value);
vb@114
   745
roker@723
   746
    //subject = (msg->shortmsg) ? msg->shortmsg : "pEp"; // not used, yet.
vb@301
   747
    plaintext = msg->attachments->next->value;
vb@114
   748
vb@116
   749
    mime = part_multiple_new("multipart/encrypted");
vb@114
   750
    assert(mime);
vb@114
   751
    if (mime == NULL)
vb@114
   752
        goto enomem;
vb@114
   753
vb@114
   754
    param = mailmime_param_new_with_data("protocol", "application/pgp-encrypted");
vb@114
   755
    clist_append(mime->mm_content_type->ct_parameters, param);
vb@114
   756
vb@114
   757
    submime = get_pgp_encrypted_part();
vb@114
   758
    assert(submime);
vb@114
   759
    if (submime == NULL)
vb@114
   760
        goto enomem;
vb@114
   761
vb@114
   762
    r = mailmime_smart_add_part(mime, submime);
vb@114
   763
    assert(r == MAILIMF_NO_ERROR);
vb@114
   764
    if (r == MAILIMF_ERROR_MEMORY) {
vb@114
   765
        goto enomem;
vb@114
   766
    }
vb@114
   767
    else {
vb@114
   768
        // mailmime_smart_add_part() takes ownership of submime
vb@114
   769
        submime = NULL;
vb@114
   770
    }
vb@114
   771
Edouard@427
   772
    submime = get_text_part("msg.asc", "application/octet-stream", plaintext,
vb@114
   773
            strlen(plaintext), MAILMIME_MECHANISM_7BIT);
vb@114
   774
    assert(submime);
vb@114
   775
    if (submime == NULL)
vb@114
   776
        goto enomem;
vb@114
   777
vb@114
   778
    r = mailmime_smart_add_part(mime, submime);
vb@114
   779
    assert(r == MAILIMF_NO_ERROR);
vb@114
   780
    if (r == MAILIMF_ERROR_MEMORY) {
vb@114
   781
        goto enomem;
vb@114
   782
    }
vb@114
   783
    else {
vb@114
   784
        // mailmime_smart_add_part() takes ownership of submime
vb@114
   785
        submime = NULL;
vb@114
   786
    }
vb@114
   787
vb@114
   788
    *result = mime;
vb@114
   789
    return PEP_STATUS_OK;
vb@114
   790
vb@114
   791
enomem:
vb@114
   792
    status = PEP_OUT_OF_MEMORY;
vb@114
   793
vb@114
   794
    if (mime)
vb@114
   795
        mailmime_free(mime);
vb@114
   796
vb@114
   797
    if (submime)
vb@114
   798
        mailmime_free(submime);
vb@114
   799
vb@114
   800
    return status;
vb@114
   801
}
vb@114
   802
vb@114
   803
DYNAMIC_API PEP_STATUS mime_encode_message(
vb@114
   804
        const message * msg,
vb@114
   805
        bool omit_fields,
vb@114
   806
        char **mimetext
vb@114
   807
    )
vb@114
   808
{
vb@114
   809
    PEP_STATUS status = PEP_STATUS_OK;
vb@114
   810
    struct mailmime * msg_mime = NULL;
vb@114
   811
    struct mailmime * mime = NULL;
vb@114
   812
    struct mailimf_fields * fields = NULL;
vb@114
   813
    char *buf = NULL;
vb@114
   814
    int r;
vb@114
   815
vb@114
   816
    assert(msg);
vb@114
   817
    assert(mimetext);
vb@114
   818
vb@260
   819
    if (!(msg && mimetext))
vb@191
   820
        return PEP_ILLEGAL_VALUE;
vb@191
   821
vb@114
   822
    *mimetext = NULL;
vb@114
   823
vb@114
   824
    switch (msg->enc_format) {
vb@114
   825
        case PEP_enc_none:
vb@114
   826
            status = mime_encode_message_plain(msg, omit_fields, &mime);
vb@114
   827
            break;
vb@114
   828
vb@114
   829
        case PEP_enc_pieces:
vb@114
   830
            status = mime_encode_message_plain(msg, omit_fields, &mime);
vb@114
   831
            break;
vb@114
   832
vb@114
   833
        case PEP_enc_S_MIME:
vb@114
   834
            NOT_IMPLEMENTED
vb@114
   835
                
vb@114
   836
        case PEP_enc_PGP_MIME:
vb@114
   837
            status = mime_encode_message_PGP_MIME(msg, omit_fields, &mime);
vb@114
   838
            break;
vb@114
   839
vb@114
   840
        case PEP_enc_PEP:
vb@114
   841
            NOT_IMPLEMENTED
vb@114
   842
    }
vb@114
   843
vb@114
   844
    if (status != PEP_STATUS_OK)
vb@114
   845
        goto pep_error;
vb@114
   846
vb@89
   847
    msg_mime = mailmime_new_message_data(NULL);
vb@90
   848
    assert(msg_mime);
vb@90
   849
    if (msg_mime == NULL)
vb@90
   850
        goto enomem;
vb@89
   851
vb@90
   852
    r = mailmime_add_part(msg_mime, mime);
vb@90
   853
    if (r) {
vb@90
   854
        mailmime_free(mime);
vb@90
   855
        goto enomem;
vb@90
   856
    }
vb@114
   857
    mime = NULL;
vb@90
   858
vb@113
   859
    if (!omit_fields) {
vb@113
   860
        status = build_fields(msg, &fields);
vb@113
   861
        if (status != PEP_STATUS_OK)
vb@113
   862
            goto pep_error;
vb@90
   863
vb@113
   864
        mailmime_set_imf_fields(msg_mime, fields);
vb@113
   865
    }
vb@90
   866
vb@90
   867
    status = render_mime(msg_mime, &buf);
vb@89
   868
    if (status != PEP_STATUS_OK)
vb@89
   869
        goto pep_error;
vb@89
   870
vb@89
   871
    mailmime_free(msg_mime);
vb@89
   872
    *mimetext = buf;
vb@90
   873
vb@89
   874
    return PEP_STATUS_OK;
vb@89
   875
vb@89
   876
enomem:
vb@89
   877
    status = PEP_OUT_OF_MEMORY;
vb@89
   878
vb@89
   879
pep_error:
vb@89
   880
    if (msg_mime)
vb@90
   881
        mailmime_free(msg_mime);
vb@89
   882
    else
vb@89
   883
        if (mime)
vb@89
   884
            mailmime_free(mime);
vb@89
   885
vb@89
   886
    return status;
vb@89
   887
}
vb@89
   888
vb@94
   889
static pEp_identity *mailbox_to_identity(const struct mailimf_mailbox * mb)
vb@94
   890
{
vb@94
   891
    char *username = NULL;
vb@94
   892
vb@159
   893
    assert(mb);
vb@159
   894
    assert(mb->mb_addr_spec);
vb@159
   895
vb@159
   896
    if (mb->mb_addr_spec == NULL)
vb@159
   897
        return NULL;
vb@159
   898
vb@159
   899
    if (mb->mb_display_name) {
roker@871
   900
        size_t index = 0;
roker@871
   901
        const int r = mailmime_encoded_phrase_parse("utf-8", mb->mb_display_name,
vb@159
   902
                strlen(mb->mb_display_name), &index, "utf-8", &username);
vb@159
   903
        if (r)
vb@159
   904
            goto enomem;
vb@159
   905
    }
vb@94
   906
roker@871
   907
    pEp_identity *ident = new_identity(mb->mb_addr_spec, NULL, NULL, username);
vb@94
   908
    if (ident == NULL)
vb@94
   909
        goto enomem;
vb@94
   910
    free(username);
vb@94
   911
vb@94
   912
    return ident;
vb@94
   913
vb@94
   914
enomem:
vb@94
   915
    free(username);
vb@94
   916
    return NULL;
vb@94
   917
}
vb@94
   918
vb@94
   919
static pEp_identity * mbl_to_identity(const struct mailimf_mailbox_list * mbl)
vb@94
   920
{
vb@94
   921
    struct mailimf_mailbox * mb = clist_content(clist_begin(mbl->mb_list));
vb@94
   922
    return mailbox_to_identity(mb);
vb@94
   923
}
vb@94
   924
vb@94
   925
static identity_list * mal_to_identity_list(
vb@94
   926
        const struct mailimf_address_list *mal
vb@94
   927
    )
vb@94
   928
{
roker@871
   929
    assert(mal);
vb@94
   930
    clist *list = mal->ad_list;
vb@94
   931
roker@871
   932
    identity_list *il = new_identity_list(NULL);
vb@94
   933
    if (il == NULL)
vb@94
   934
        goto enomem;
vb@94
   935
vb@94
   936
    identity_list *_il = il;
roker@871
   937
    for (clistiter *cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
vb@94
   938
        pEp_identity *ident;
vb@94
   939
roker@871
   940
        struct mailimf_address *addr = clist_content(cur);
vb@94
   941
        switch(addr->ad_type) {
vb@94
   942
            case MAILIMF_ADDRESS_MAILBOX:
vb@94
   943
                ident = mailbox_to_identity(addr->ad_data.ad_mailbox);
vb@94
   944
                if (ident == NULL)
vb@94
   945
                    goto enomem;
vb@94
   946
                _il = identity_list_add(_il, ident);
vb@94
   947
                if (_il == NULL)
vb@94
   948
                    goto enomem;
vb@94
   949
                break;
vb@94
   950
vb@94
   951
            case MAILIMF_ADDRESS_GROUP:
vb@94
   952
                {
vb@94
   953
                    struct mailimf_mailbox_list * mbl =
vb@94
   954
                            addr->ad_data.ad_group->grp_mb_list;
roker@871
   955
                    for (clistiter *cur2 = clist_begin(mbl->mb_list); cur2 != NULL;
vb@94
   956
                            cur2 = clist_next(cur2)) {
vb@94
   957
                        ident = mailbox_to_identity(clist_content(cur));
vb@94
   958
                        if (ident == NULL)
vb@94
   959
                            goto enomem;
vb@94
   960
                        _il = identity_list_add(_il, ident);
vb@94
   961
                        if (_il == NULL)
vb@94
   962
                            goto enomem;
vb@94
   963
                    }
vb@94
   964
                }
vb@94
   965
                break;
vb@94
   966
vb@94
   967
            default:
vb@94
   968
                assert(0);
vb@94
   969
                goto enomem;
vb@94
   970
        }
vb@94
   971
    }
vb@94
   972
vb@94
   973
    return il;
vb@94
   974
vb@94
   975
enomem:
vb@94
   976
    free_identity_list(il);
vb@94
   977
    return NULL;
vb@94
   978
}
vb@94
   979
vb@94
   980
static stringlist_t * clist_to_stringlist(const clist *list)
vb@94
   981
{
vb@94
   982
    char *text = NULL;;
vb@94
   983
    stringlist_t * sl = new_stringlist(NULL);
vb@94
   984
    if (sl == NULL)
vb@94
   985
        return NULL;
vb@94
   986
vb@94
   987
    stringlist_t *_sl = sl;
roker@871
   988
    for (clistiter *cur = clist_begin(list); cur != NULL; cur = clist_next(cur)) {
vb@94
   989
        char *phrase = clist_content(cur);
roker@871
   990
        size_t index = 0;
roker@871
   991
        
roker@871
   992
        const int r = mailmime_encoded_phrase_parse("utf-8", phrase, strlen(phrase),
vb@94
   993
                &index, "utf-8", &text);
vb@94
   994
        if (r)
vb@94
   995
            goto enomem;
vb@94
   996
vb@94
   997
        _sl = stringlist_add(_sl, text);
vb@94
   998
        if (_sl == NULL)
vb@94
   999
            goto enomem;
vb@94
  1000
vb@94
  1001
        free(text);
vb@94
  1002
        text = NULL;
vb@94
  1003
    }
vb@94
  1004
vb@94
  1005
    return _sl;
vb@94
  1006
vb@94
  1007
enomem:
vb@94
  1008
    free_stringlist(sl);
vb@94
  1009
    free(text);
vb@94
  1010
vb@94
  1011
    return NULL;
vb@94
  1012
}
vb@94
  1013
vb@94
  1014
static PEP_STATUS read_fields(message *msg, clist *fieldlist)
vb@94
  1015
{
vb@94
  1016
    PEP_STATUS status = PEP_STATUS_OK;
vb@94
  1017
    struct mailimf_field * _field;
vb@94
  1018
    clistiter *cur;
vb@94
  1019
    size_t index;
vb@94
  1020
    int r;
vb@110
  1021
    stringpair_list_t *opt = msg->opt_fields;
vb@94
  1022
vb@94
  1023
    for (cur = clist_begin(fieldlist); cur != NULL; cur = clist_next(cur)) {
vb@94
  1024
        _field = clist_content(cur);
vb@94
  1025
vb@94
  1026
        switch (_field->fld_type) {
vb@94
  1027
            case MAILIMF_FIELD_MESSAGE_ID:
vb@94
  1028
                {
vb@94
  1029
                    char * text = _field->fld_data.fld_message_id->mid_value;
vb@116
  1030
vb@116
  1031
                    free(msg->id);
vb@94
  1032
                    index = 0;
vb@94
  1033
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1034
                            strlen(text), &index, "utf-8", &msg->id);
vb@94
  1035
                    if (r)
vb@94
  1036
                        goto enomem;
vb@94
  1037
                }
vb@94
  1038
                break;
vb@94
  1039
vb@94
  1040
            case MAILIMF_FIELD_SUBJECT:
vb@94
  1041
                {
vb@94
  1042
                    char * text = _field->fld_data.fld_subject->sbj_value;
vb@116
  1043
vb@116
  1044
                    free(msg->shortmsg);
vb@94
  1045
                    index = 0;
vb@94
  1046
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1047
                            strlen(text), &index, "utf-8", &msg->shortmsg);
vb@94
  1048
                    if (r)
vb@94
  1049
                        goto enomem;
vb@94
  1050
                }
vb@94
  1051
                break;
vb@94
  1052
vb@94
  1053
            case MAILIMF_FIELD_ORIG_DATE:
vb@94
  1054
                {
vb@94
  1055
                    struct mailimf_date_time *date =
vb@94
  1056
                        _field->fld_data.fld_orig_date->dt_date_time;
vb@116
  1057
vb@116
  1058
                    free_timestamp(msg->sent);
vb@94
  1059
                    msg->sent = etpantime_to_timestamp(date);
vb@94
  1060
                    if (msg->sent == NULL)
vb@94
  1061
                        goto enomem;
vb@94
  1062
                }
vb@94
  1063
                break;
vb@94
  1064
vb@94
  1065
            case MAILIMF_FIELD_FROM:
vb@94
  1066
                {
vb@94
  1067
                    struct mailimf_mailbox_list *mbl =
vb@94
  1068
                            _field->fld_data.fld_from->frm_mb_list;
vb@94
  1069
                    pEp_identity *ident;
vb@94
  1070
vb@94
  1071
                    ident = mbl_to_identity(mbl);
vb@94
  1072
                    if (ident == NULL)
vb@94
  1073
                        goto pep_error;
vb@94
  1074
vb@116
  1075
                    free_identity(msg->from);
vb@94
  1076
                    msg->from = ident;
vb@94
  1077
                }
vb@94
  1078
                break;
vb@94
  1079
vb@94
  1080
            case MAILIMF_FIELD_TO:
vb@94
  1081
                {
vb@94
  1082
                    struct mailimf_address_list *mal =
vb@94
  1083
                            _field->fld_data.fld_to->to_addr_list;
vb@94
  1084
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1085
                    if (il == NULL)
vb@94
  1086
                        goto enomem;
vb@116
  1087
vb@116
  1088
                    free_identity_list(msg->to);
vb@94
  1089
                    msg->to = il;
vb@94
  1090
                }
vb@94
  1091
                break;
vb@94
  1092
vb@94
  1093
            case MAILIMF_FIELD_CC:
vb@94
  1094
                {
vb@94
  1095
                    struct mailimf_address_list *mal =
vb@94
  1096
                            _field->fld_data.fld_cc->cc_addr_list;
vb@94
  1097
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1098
                    if (il == NULL)
vb@94
  1099
                        goto enomem;
vb@116
  1100
vb@116
  1101
                    free_identity_list(msg->cc);
vb@94
  1102
                    msg->cc = il;
vb@94
  1103
                }
vb@94
  1104
                break;
vb@94
  1105
vb@94
  1106
            case MAILIMF_FIELD_BCC:
vb@94
  1107
                {
vb@94
  1108
                    struct mailimf_address_list *mal =
vb@94
  1109
                            _field->fld_data.fld_bcc->bcc_addr_list;
vb@94
  1110
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1111
                    if (il == NULL)
vb@94
  1112
                        goto enomem;
vb@116
  1113
vb@116
  1114
                    free_identity_list(msg->bcc);
vb@94
  1115
                    msg->bcc = il;
vb@94
  1116
                }
vb@94
  1117
                break;
vb@94
  1118
vb@94
  1119
            case MAILIMF_FIELD_REPLY_TO:
vb@94
  1120
                {
vb@94
  1121
                    struct mailimf_address_list *mal =
vb@94
  1122
                            _field->fld_data.fld_reply_to->rt_addr_list;
vb@94
  1123
                    identity_list *il = mal_to_identity_list(mal);
vb@94
  1124
                    if (il == NULL)
vb@94
  1125
                        goto enomem;
vb@116
  1126
vb@116
  1127
                    free_identity_list(msg->reply_to);
vb@94
  1128
                    msg->reply_to = il;
vb@94
  1129
                }
vb@94
  1130
                break;
vb@94
  1131
vb@94
  1132
            case MAILIMF_FIELD_IN_REPLY_TO:
vb@94
  1133
                {
vb@94
  1134
                    clist *list = _field->fld_data.fld_in_reply_to->mid_list;
vb@94
  1135
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1136
                    if (sl == NULL)
vb@94
  1137
                        goto enomem;
vb@116
  1138
vb@116
  1139
                    free_stringlist(msg->in_reply_to);
vb@94
  1140
                    msg->in_reply_to = sl;
vb@94
  1141
                }
vb@94
  1142
                break;
vb@94
  1143
vb@94
  1144
            case MAILIMF_FIELD_REFERENCES:
vb@94
  1145
                {
vb@94
  1146
                    clist *list = _field->fld_data.fld_references->mid_list;
vb@94
  1147
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1148
                    if (sl == NULL)
vb@94
  1149
                        goto enomem;
vb@116
  1150
vb@116
  1151
                    free_stringlist(msg->references);
vb@94
  1152
                    msg->references = sl;
vb@94
  1153
                }
vb@94
  1154
                break;
vb@94
  1155
vb@94
  1156
            case MAILIMF_FIELD_KEYWORDS:
vb@94
  1157
                {
vb@94
  1158
                    clist *list = _field->fld_data.fld_keywords->kw_list;
vb@94
  1159
                    stringlist_t *sl = clist_to_stringlist(list);
vb@94
  1160
                    if (sl == NULL)
vb@94
  1161
                        goto enomem;
vb@116
  1162
vb@116
  1163
                    free_stringlist(msg->keywords);
vb@94
  1164
                    msg->keywords = sl;
vb@94
  1165
                }
vb@94
  1166
                break;
vb@94
  1167
vb@94
  1168
            case MAILIMF_FIELD_COMMENTS:
vb@94
  1169
                {
vb@94
  1170
                    char * text = _field->fld_data.fld_comments->cm_value;
vb@116
  1171
vb@116
  1172
                    free(msg->comments);
vb@94
  1173
                    index = 0;
vb@94
  1174
                    r = mailmime_encoded_phrase_parse("utf-8", text,
vb@94
  1175
                            strlen(text), &index, "utf-8", &msg->comments);
vb@94
  1176
                    if (r)
vb@94
  1177
                        goto enomem;
vb@94
  1178
                }
vb@94
  1179
                break;
vb@110
  1180
vb@110
  1181
            case MAILIMF_FIELD_OPTIONAL_FIELD:
vb@110
  1182
                {
vb@110
  1183
                    char * name =
vb@110
  1184
                            _field->fld_data.fld_optional_field->fld_name;
vb@110
  1185
                    char * value =
vb@110
  1186
                            _field->fld_data.fld_optional_field->fld_value;
vb@110
  1187
                    char *_value;
vb@110
  1188
vb@110
  1189
                    index = 0;
vb@110
  1190
                    r = mailmime_encoded_phrase_parse("utf-8", value,
vb@110
  1191
                            strlen(value), &index, "utf-8", &_value);
vb@110
  1192
                    if (r)
vb@110
  1193
                        goto enomem;
vb@110
  1194
vb@281
  1195
                    stringpair_t *pair = new_stringpair(name, _value);
vb@281
  1196
                    if (pair == NULL)
vb@281
  1197
                        goto enomem;
vb@110
  1198
vb@281
  1199
                    opt = stringpair_list_add(opt, pair);
vb@110
  1200
                    free(_value);
vb@110
  1201
                    if (opt == NULL)
vb@110
  1202
                        goto enomem;
vb@110
  1203
vb@110
  1204
                    if (msg->opt_fields == NULL)
vb@110
  1205
                        msg->opt_fields = opt;
vb@110
  1206
                }
vb@110
  1207
                break;
vb@94
  1208
        }
vb@94
  1209
    }
vb@94
  1210
vb@94
  1211
    return PEP_STATUS_OK;
vb@94
  1212
vb@94
  1213
enomem:
vb@94
  1214
    status = PEP_OUT_OF_MEMORY;
vb@94
  1215
vb@94
  1216
pep_error:
vb@94
  1217
    return status;
vb@94
  1218
}
vb@94
  1219
vb@147
  1220
static PEP_STATUS interpret_body(struct mailmime *part, char **longmsg, size_t *size)
vb@147
  1221
{
vb@147
  1222
    const char *text;
vb@159
  1223
    char *_longmsg;
vb@147
  1224
    size_t length;
vb@159
  1225
    size_t _size;
vb@147
  1226
    int code;
vb@147
  1227
    int r;
vb@147
  1228
    size_t index;
vb@159
  1229
    char *type = NULL;
vb@159
  1230
    char *charset = NULL;
vb@147
  1231
vb@147
  1232
    assert(part);
vb@147
  1233
    assert(longmsg);
vb@147
  1234
vb@147
  1235
    *longmsg = NULL;
vb@147
  1236
    if (size)
vb@147
  1237
        *size = 0;
vb@147
  1238
vb@147
  1239
    if (part->mm_body == NULL)
vb@147
  1240
        return PEP_ILLEGAL_VALUE;
vb@147
  1241
vb@147
  1242
    text = part->mm_body-> dt_data.dt_text.dt_data;
vb@147
  1243
    if (text == NULL)
vb@147
  1244
        return PEP_ILLEGAL_VALUE;
vb@147
  1245
vb@147
  1246
    length = part->mm_body->dt_data.dt_text.dt_length;
vb@147
  1247
vb@147
  1248
    if (part->mm_body->dt_encoded) {
vb@147
  1249
        code = part->mm_body->dt_encoding;
vb@147
  1250
        index = 0;
vb@159
  1251
        r = mailmime_part_parse(text, length, &index, code, &_longmsg, &_size);
vb@147
  1252
        switch (r) {
vb@147
  1253
            case MAILIMF_NO_ERROR:
vb@147
  1254
                break;
vb@147
  1255
            case MAILIMF_ERROR_MEMORY:
vb@147
  1256
                return PEP_OUT_OF_MEMORY;
vb@147
  1257
            default:
vb@147
  1258
                return PEP_ILLEGAL_VALUE;
vb@147
  1259
        }
vb@147
  1260
    }
vb@147
  1261
    else {
vb@289
  1262
        _size = length + 1;
vb@289
  1263
        _longmsg = strndup(text, length);
vb@159
  1264
        if (_longmsg == NULL)
vb@147
  1265
            return PEP_OUT_OF_MEMORY;
vb@147
  1266
    }
vb@147
  1267
vb@159
  1268
    if (part->mm_content_type) {
vb@159
  1269
        if (_get_content_type(part->mm_content_type, &type, &charset) == 0) {
vb@160
  1270
            if (charset && strncasecmp(charset, "utf-8", 5) != 0) {
vb@159
  1271
                char * _text;
vb@159
  1272
                int r = charconv("utf-8", charset, _longmsg, _size, &_text);
vb@159
  1273
                switch (r) {
vb@159
  1274
                    case MAILIMF_NO_ERROR:
vb@159
  1275
                        break;
vb@159
  1276
                    case MAILIMF_ERROR_MEMORY:
vb@159
  1277
                        return PEP_OUT_OF_MEMORY;
vb@159
  1278
                    default:
vb@159
  1279
                        return PEP_ILLEGAL_VALUE;
vb@159
  1280
                }
vb@159
  1281
                free(_longmsg);
vb@159
  1282
                _longmsg = _text;
vb@159
  1283
            }
vb@159
  1284
        }
vb@159
  1285
    }
vb@159
  1286
vb@159
  1287
    *longmsg = _longmsg;
vb@159
  1288
    if (size)
vb@159
  1289
        *size = _size;
vb@159
  1290
vb@147
  1291
    return PEP_STATUS_OK;
vb@147
  1292
}
vb@147
  1293
vb@118
  1294
static PEP_STATUS interpret_MIME(
vb@118
  1295
        struct mailmime *mime,
vb@118
  1296
        message *msg
vb@118
  1297
    )
vb@118
  1298
{
vb@118
  1299
    PEP_STATUS status = PEP_STATUS_OK;
vb@118
  1300
vb@118
  1301
    assert(mime);
vb@118
  1302
    assert(msg);
vb@118
  1303
vb@118
  1304
    struct mailmime_content *content = mime->mm_content_type;
vb@118
  1305
    if (content) {
vb@152
  1306
        if (_is_multipart(content, "alternative")) {
vb@118
  1307
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1308
            if (partlist == NULL)
vb@118
  1309
                return PEP_ILLEGAL_VALUE;
vb@118
  1310
vb@118
  1311
            clistiter *cur;
vb@118
  1312
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@120
  1313
                struct mailmime *part = clist_content(cur);
vb@118
  1314
                if (part == NULL)
vb@118
  1315
                    return PEP_ILLEGAL_VALUE;
vb@118
  1316
vb@120
  1317
                content = part->mm_content_type;
vb@120
  1318
                assert(content);
vb@120
  1319
                if (content == NULL)
vb@120
  1320
                    return PEP_ILLEGAL_VALUE;
vb@118
  1321
vb@152
  1322
                if (_is_text_part(content, "plain") && msg->longmsg == NULL) {
vb@152
  1323
                    status = interpret_body(part, &msg->longmsg, NULL);
vb@152
  1324
                    if (status)
vb@152
  1325
                        return status;
vb@152
  1326
                }
vb@152
  1327
                else if (_is_text_part(content, "html") &&
vb@152
  1328
                        msg->longmsg_formatted == NULL) {
vb@152
  1329
                    status = interpret_body(part, &msg->longmsg_formatted,
vb@152
  1330
                            NULL);
vb@152
  1331
                    if (status)
vb@152
  1332
                        return status;
vb@152
  1333
                }
vb@152
  1334
                else /* add as attachment */ {
vb@152
  1335
                    status = interpret_MIME(part, msg);
vb@152
  1336
                    if (status)
vb@152
  1337
                        return status;
vb@120
  1338
                }
vb@118
  1339
            }
vb@118
  1340
        }
vb@260
  1341
        else if (_is_multipart(content, "encrypted")) {
vb@260
  1342
            if (msg->longmsg == NULL)
vb@260
  1343
                msg->longmsg = strdup("");
vb@260
  1344
vb@260
  1345
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@260
  1346
            if (partlist == NULL)
vb@260
  1347
                return PEP_ILLEGAL_VALUE;
vb@260
  1348
vb@260
  1349
            clistiter *cur;
vb@260
  1350
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@260
  1351
                struct mailmime *part= clist_content(cur);
vb@260
  1352
                if (part == NULL)
vb@260
  1353
                    return PEP_ILLEGAL_VALUE;
vb@260
  1354
vb@260
  1355
                status = interpret_MIME(part, msg);
vb@260
  1356
                if (status != PEP_STATUS_OK)
vb@260
  1357
                    return status;
vb@260
  1358
            }
vb@260
  1359
        }
vb@152
  1360
        else if (_is_multipart(content, NULL)) {
vb@118
  1361
            clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
vb@118
  1362
            if (partlist == NULL)
vb@118
  1363
                return PEP_ILLEGAL_VALUE;
vb@118
  1364
vb@118
  1365
            clistiter *cur;
vb@118
  1366
            for (cur = clist_begin(partlist); cur; cur = clist_next(cur)) {
vb@118
  1367
                struct mailmime *part= clist_content(cur);
vb@118
  1368
                if (part == NULL)
vb@118
  1369
                    return PEP_ILLEGAL_VALUE;
vb@118
  1370
vb@118
  1371
                status = interpret_MIME(part, msg);
vb@118
  1372
                if (status != PEP_STATUS_OK)
vb@118
  1373
                    return status;
vb@118
  1374
            }
vb@118
  1375
        }
vb@118
  1376
        else {
vb@152
  1377
            if (_is_text_part(content, NULL) && msg->longmsg == NULL) {
vb@152
  1378
                status = interpret_body(mime, &msg->longmsg, NULL);
vb@152
  1379
                if (status)
vb@152
  1380
                    return status;
vb@152
  1381
            }
vb@152
  1382
            else {
vb@152
  1383
                char *data = NULL;
vb@157
  1384
                size_t size = 0;
vb@157
  1385
                char * mime_type;
vb@159
  1386
                char * charset;
vb@157
  1387
                char * filename;
vb@159
  1388
                int r;
vb@118
  1389
vb@159
  1390
                r = _get_content_type(content, &mime_type, &charset);
vb@159
  1391
                switch (r) {
vb@159
  1392
                    case 0:
vb@159
  1393
                        break;
vb@159
  1394
                    case EINVAL:
vb@159
  1395
                        return PEP_ILLEGAL_VALUE;
vb@159
  1396
                    case ENOMEM:
vb@159
  1397
                        return PEP_OUT_OF_MEMORY;
vb@159
  1398
                    default:
vb@159
  1399
                        return PEP_UNKNOWN_ERROR;
vb@157
  1400
                }
vb@154
  1401
vb@159
  1402
                assert(mime_type);
vb@159
  1403
vb@152
  1404
                status = interpret_body(mime, &data, &size);
vb@152
  1405
                if (status)
vb@152
  1406
                    return status;
vb@152
  1407
vb@154
  1408
                filename = _get_filename(mime);
vb@481
  1409
                char *_filename = NULL;
vb@481
  1410
                if (filename) {
vb@481
  1411
                    size_t index = 0;
vb@481
  1412
                    r = mailmime_encoded_phrase_parse("utf-8", filename,
vb@481
  1413
                            strlen(filename), &index, "utf-8", &_filename);
vb@481
  1414
                    if (r)
vb@481
  1415
                        goto enomem;
vb@481
  1416
                }
vb@154
  1417
vb@260
  1418
                bloblist_t *_a = bloblist_add(msg->attachments, data, size,
vb@481
  1419
                        mime_type, _filename);
vb@481
  1420
                free(_filename);
vb@260
  1421
                if (_a == NULL)
vb@260
  1422
                    return PEP_OUT_OF_MEMORY;
vb@152
  1423
                if (msg->attachments == NULL)
vb@260
  1424
                    msg->attachments = _a;
vb@152
  1425
            }
vb@118
  1426
        }
vb@118
  1427
    }
vb@118
  1428
vb@118
  1429
    return PEP_STATUS_OK;
vb@481
  1430
vb@481
  1431
enomem:
vb@481
  1432
    return PEP_OUT_OF_MEMORY;
vb@118
  1433
}
vb@118
  1434
vb@89
  1435
DYNAMIC_API PEP_STATUS mime_decode_message(
vb@89
  1436
        const char *mimetext,
vb@269
  1437
        size_t size,
vb@89
  1438
        message **msg
vb@89
  1439
    )
vb@89
  1440
{
vb@89
  1441
    PEP_STATUS status = PEP_STATUS_OK;
vb@89
  1442
    struct mailmime * mime = NULL;
vb@89
  1443
    int r;
vb@94
  1444
    message *_msg = NULL;
vb@94
  1445
    size_t index;
vb@89
  1446
vb@89
  1447
    assert(mimetext);
vb@89
  1448
    assert(msg);
vb@89
  1449
vb@191
  1450
    if (!(mimetext && msg))
vb@191
  1451
        return PEP_ILLEGAL_VALUE;
vb@191
  1452
vb@89
  1453
    *msg = NULL;
vb@113
  1454
vb@94
  1455
    index = 0;
vb@269
  1456
    r = mailmime_parse(mimetext, size, &index, &mime);
vb@89
  1457
    assert(r == 0);
vb@89
  1458
    assert(mime);
vb@90
  1459
    if (r) {
vb@89
  1460
        if (r == MAILIMF_ERROR_MEMORY)
vb@89
  1461
            goto enomem;
vb@89
  1462
        else
vb@89
  1463
            goto err_mime;
vb@89
  1464
    }
vb@89
  1465
vb@117
  1466
    _msg = calloc(1, sizeof(message));
vb@117
  1467
    assert(_msg);
vb@117
  1468
    if (_msg == NULL)
vb@117
  1469
        goto enomem;
vb@116
  1470
vb@147
  1471
    clist * _fieldlist = _get_fields(mime);
vb@147
  1472
    if (_fieldlist) {
vb@117
  1473
        status = read_fields(_msg, _fieldlist);
vb@116
  1474
        if (status != PEP_STATUS_OK)
vb@116
  1475
            goto pep_error;
vb@116
  1476
    }
vb@116
  1477
vb@147
  1478
    struct mailmime_content *content = _get_content(mime);
vb@117
  1479
vb@147
  1480
    if (content) {
vb@260
  1481
        status = interpret_MIME(mime->mm_data.mm_message.mm_msg_mime,
vb@260
  1482
                _msg);
vb@260
  1483
        if (status != PEP_STATUS_OK)
vb@260
  1484
            goto pep_error;
vb@116
  1485
    }
vb@116
  1486
vb@89
  1487
    mailmime_free(mime);
vb@94
  1488
    *msg = _msg;
vb@89
  1489
vb@89
  1490
    return status;
vb@89
  1491
vb@89
  1492
err_mime:
vb@89
  1493
    status = PEP_ILLEGAL_VALUE;
vb@89
  1494
    goto pep_error;
vb@89
  1495
vb@89
  1496
enomem:
vb@89
  1497
    status = PEP_OUT_OF_MEMORY;
vb@89
  1498
vb@89
  1499
pep_error:
vb@94
  1500
    free_message(_msg);
vb@94
  1501
vb@89
  1502
    if (mime)
vb@89
  1503
        mailmime_free(mime);
vb@89
  1504
vb@89
  1505
    return status;
vb@89
  1506
}
vb@89
  1507