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