src/mime.c
changeset 48 9ff5d2dfa75a
child 51 761bc0b46b98
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/mime.c	Sun Feb 08 11:33:04 2015 -0500
     1.3 @@ -0,0 +1,180 @@
     1.4 +#include "mime.h"
     1.5 +
     1.6 +#include <libetpan/libetpan.h>
     1.7 +#include <string.h>
     1.8 +#include <stdlib.h>
     1.9 +#include <assert.h>
    1.10 +#include <errno.h>
    1.11 +#include <unistd.h>
    1.12 +
    1.13 +#include "etpan_mime.h"
    1.14 +
    1.15 +DYNAMIC_API PEP_STATUS mime_encode_text(
    1.16 +        const char *plaintext,
    1.17 +        const char *htmltext,
    1.18 +        char **resulttext
    1.19 +    )
    1.20 +{
    1.21 +    struct mailmime * msg_mime = NULL;
    1.22 +	struct mailimf_fields * fields = NULL;
    1.23 +    struct mailmime * mime = NULL;
    1.24 +    struct mailmime * submime = NULL;
    1.25 +    int col;
    1.26 +    int r;
    1.27 +    int fd;
    1.28 +    FILE *file = NULL;
    1.29 +    size_t size;
    1.30 +    char *buf = NULL;
    1.31 +    PEP_STATUS error = PEP_OUT_OF_MEMORY;
    1.32 +
    1.33 +    assert(plaintext);
    1.34 +    assert(resulttext);
    1.35 +
    1.36 +    *resulttext = NULL;
    1.37 +
    1.38 +    msg_mime = mailmime_new_message_data(NULL);
    1.39 +    assert(msg_mime);
    1.40 +    if (msg_mime == NULL)
    1.41 +        goto enomem;
    1.42 +
    1.43 +    fields = mailimf_fields_new_empty();
    1.44 +    assert(fields);
    1.45 +    if (fields == NULL)
    1.46 +        goto enomem;
    1.47 +
    1.48 +    mailmime_set_imf_fields(msg_mime, fields);
    1.49 +
    1.50 +    mime = part_multiple_new("multipart/mixed", NULL);
    1.51 +    assert(mime);
    1.52 +    if (mime == NULL)
    1.53 +        goto enomem;
    1.54 +
    1.55 +    submime = get_text_part("text/plain", plaintext, strlen(plaintext),
    1.56 +            MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    1.57 +    assert(submime);
    1.58 +    if (submime == NULL) {
    1.59 +        mailmime_free(msg_mime);
    1.60 +        goto enomem;
    1.61 +    }
    1.62 +
    1.63 +    r = mailmime_smart_add_part(mime, submime);
    1.64 +    assert(r == MAILIMF_NO_ERROR);
    1.65 +    if (r == MAILIMF_ERROR_MEMORY) {
    1.66 +        mailmime_free(msg_mime);
    1.67 +        goto enomem;
    1.68 +    }
    1.69 +    else {
    1.70 +        // mailmime_smart_add_part() takes ownership of submime
    1.71 +        submime = NULL;
    1.72 +    }
    1.73 +
    1.74 +    r = mailmime_add_part(msg_mime, mime);
    1.75 +    assert(r == MAILIMF_NO_ERROR);
    1.76 +    if (r == MAILIMF_ERROR_MEMORY) {
    1.77 +        goto enomem;
    1.78 +    }
    1.79 +    // mailmime_add_part() takes ownership of mime
    1.80 +
    1.81 +    char *template = strdup("/tmp/temp.XXXXXXXXXXXXXXXXXXXX");
    1.82 +    assert(template);
    1.83 +    if (template == NULL)
    1.84 +        goto enomem;
    1.85 +
    1.86 +    do {
    1.87 +        fd = mkstemp(template);
    1.88 +    } while (fd == -1 && errno == EINTR);
    1.89 +
    1.90 +    assert(fd != -1);
    1.91 +    if (fd == -1)
    1.92 +        goto err_file;
    1.93 +
    1.94 +    r = unlink(template);
    1.95 +    assert(r == 0);
    1.96 +    if (r == -1)
    1.97 +        goto err_file;
    1.98 +
    1.99 +    free(template);
   1.100 +    template = NULL;
   1.101 +
   1.102 +    do {
   1.103 +        file = fdopen(fd, "w+");
   1.104 +    } while (file == NULL && errno == EINTR);
   1.105 +
   1.106 +    assert(file);
   1.107 +    if (file == NULL) {
   1.108 +        switch (errno) {
   1.109 +            case ENOMEM:
   1.110 +                goto enomem;
   1.111 +            default:
   1.112 +                goto err_file;
   1.113 +        }
   1.114 +    }
   1.115 +
   1.116 +    fd = -1;
   1.117 +
   1.118 +    col = 0;
   1.119 +    r = mailmime_write_file(file, &col, mime);
   1.120 +    assert(r == MAILIMF_NO_ERROR);
   1.121 +    if (r == MAILIMF_ERROR_MEMORY)
   1.122 +        goto enomem;
   1.123 +    else if (r != MAILIMF_NO_ERROR)
   1.124 +        goto err_file;
   1.125 +
   1.126 +    off_t len = ftello(file);
   1.127 +    assert(len != -1);
   1.128 +    if (len == -1 && errno == EOVERFLOW)
   1.129 +        goto err_file;
   1.130 +
   1.131 +    if (len + 1 > SIZE_T_MAX)
   1.132 +        goto err_buffer;
   1.133 +
   1.134 +    size = (size_t) len;
   1.135 +
   1.136 +    rewind(file);
   1.137 +
   1.138 +    buf = calloc(1, size + 1);
   1.139 +    assert(buf);
   1.140 +    if (buf == NULL)
   1.141 +        goto enomem;
   1.142 +    
   1.143 +    char *_buf = buf;
   1.144 +    size_t rest = size;
   1.145 +    for (size_t bytes_read = 0; rest > 0; rest -= bytes_read, _buf += rest) {
   1.146 +        assert(feof(file) == 0);
   1.147 +        if (feof(file))
   1.148 +            goto err_file;
   1.149 +        bytes_read = fread(_buf, rest, 1, file);
   1.150 +        if (ferror(file))
   1.151 +            goto err_file;
   1.152 +    }
   1.153 +
   1.154 +    fclose(file);
   1.155 +    mailmime_free(msg_mime);
   1.156 +    *resulttext = buf;
   1.157 +    return PEP_STATUS_OK;
   1.158 +
   1.159 +err_buffer:
   1.160 +    error = PEP_BUFFER_TOO_SMALL;
   1.161 +    goto enomem;
   1.162 +
   1.163 +err_file:
   1.164 +    error = PEP_CANNOT_CREATE_TEMP_FILE;
   1.165 +
   1.166 +enomem:
   1.167 +    free(buf);
   1.168 +    free(template);
   1.169 +
   1.170 +    if (file)
   1.171 +        fclose(file);
   1.172 +    else if (fd != -1)
   1.173 +        close(fd);
   1.174 +
   1.175 +    if (msg_mime)
   1.176 +        mailmime_free(msg_mime);
   1.177 +    if (fields)
   1.178 +        mailimf_fields_free(fields);
   1.179 +    if (submime)
   1.180 +        mailmime_free(submime);
   1.181 +
   1.182 +    return error;
   1.183 +}