src/mime.c
changeset 59 88429085f8da
parent 57 03f99659418e
child 62 ad5e484720e1
     1.1 --- a/src/mime.c	Sun Feb 08 15:56:04 2015 -0500
     1.2 +++ b/src/mime.c	Mon Feb 09 09:49:26 2015 -0500
     1.3 @@ -12,11 +12,10 @@
     1.4  DYNAMIC_API PEP_STATUS mime_encode_text(
     1.5          const char *plaintext,
     1.6          const char *htmltext,
     1.7 +        bloblist_t *attachments,
     1.8          char **resulttext
     1.9      )
    1.10  {
    1.11 -    struct mailmime * msg_mime = NULL;
    1.12 -	struct mailimf_fields * fields = NULL;
    1.13      struct mailmime * mime = NULL;
    1.14      struct mailmime * submime = NULL;
    1.15      int col;
    1.16 @@ -32,18 +31,6 @@
    1.17  
    1.18      *resulttext = NULL;
    1.19  
    1.20 -    msg_mime = mailmime_new_message_data(NULL);
    1.21 -    assert(msg_mime);
    1.22 -    if (msg_mime == NULL)
    1.23 -        goto enomem;
    1.24 -
    1.25 -    fields = mailimf_fields_new_empty();
    1.26 -    assert(fields);
    1.27 -    if (fields == NULL)
    1.28 -        goto enomem;
    1.29 -
    1.30 -    mailmime_set_imf_fields(msg_mime, fields);
    1.31 -
    1.32      if (htmltext) {
    1.33          mime = part_multiple_new("multipart/alternative", NULL);
    1.34          assert(mime);
    1.35 @@ -53,15 +40,12 @@
    1.36          submime = get_text_part("text/plain", plaintext, strlen(plaintext),
    1.37                  MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    1.38          assert(submime);
    1.39 -        if (submime == NULL) {
    1.40 -            mailmime_free(msg_mime);
    1.41 +        if (submime == NULL)
    1.42              goto enomem;
    1.43 -        }
    1.44  
    1.45          r = mailmime_smart_add_part(mime, submime);
    1.46          assert(r == MAILIMF_NO_ERROR);
    1.47          if (r == MAILIMF_ERROR_MEMORY) {
    1.48 -            mailmime_free(msg_mime);
    1.49              goto enomem;
    1.50          }
    1.51          else {
    1.52 @@ -72,17 +56,13 @@
    1.53          submime = get_text_part("text/html", htmltext, strlen(htmltext),
    1.54                  MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    1.55          assert(submime);
    1.56 -        if (submime == NULL) {
    1.57 -            mailmime_free(msg_mime);
    1.58 +        if (submime == NULL)
    1.59              goto enomem;
    1.60 -        }
    1.61  
    1.62          r = mailmime_smart_add_part(mime, submime);
    1.63          assert(r == MAILIMF_NO_ERROR);
    1.64 -        if (r == MAILIMF_ERROR_MEMORY) {
    1.65 -            mailmime_free(msg_mime);
    1.66 +        if (r == MAILIMF_ERROR_MEMORY)
    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 @@ -92,19 +72,56 @@
    1.73          mime = get_text_part("text/plain", plaintext, strlen(plaintext),
    1.74                  MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    1.75          assert(mime);
    1.76 -        if (mime == NULL) {
    1.77 -            mailmime_free(msg_mime);
    1.78 +        if (mime == NULL)
    1.79 +            goto enomem;
    1.80 +    }
    1.81 +
    1.82 +    if (attachments) {
    1.83 +        submime = mime;
    1.84 +        mime = part_multiple_new("multipart/mixed", NULL);
    1.85 +        assert(mime);
    1.86 +        if (mime == NULL)
    1.87 +            goto enomem;
    1.88 +
    1.89 +        r = mailmime_smart_add_part(mime, submime);
    1.90 +        assert(r == MAILIMF_NO_ERROR);
    1.91 +        if (r == MAILIMF_ERROR_MEMORY) {
    1.92              goto enomem;
    1.93          }
    1.94 +        else {
    1.95 +            // mailmime_smart_add_part() takes ownership of submime
    1.96 +            submime = NULL;
    1.97 +        }
    1.98 +
    1.99 +        bloblist_t *_a;
   1.100 +        for (_a = attachments; _a != NULL; _a = _a->next) {
   1.101 +            char * mime_type;
   1.102 +
   1.103 +            assert(_a->data);
   1.104 +            assert(_a->size);
   1.105 +
   1.106 +            if (_a->mime_type == NULL)
   1.107 +                mime_type = "application/octet-stream";
   1.108 +            else
   1.109 +                mime_type = _a->mime_type;
   1.110 +
   1.111 +            submime = get_file_part(_a->file_name, mime_type, _a->data, _a->size);
   1.112 +            assert(submime);
   1.113 +            if (submime == NULL)
   1.114 +                goto enomem;
   1.115 +
   1.116 +            r = mailmime_smart_add_part(mime, submime);
   1.117 +            assert(r == MAILIMF_NO_ERROR);
   1.118 +            if (r == MAILIMF_ERROR_MEMORY) {
   1.119 +                goto enomem;
   1.120 +            }
   1.121 +            else {
   1.122 +                // mailmime_smart_add_part() takes ownership of submime
   1.123 +                submime = NULL;
   1.124 +            }
   1.125 +        }
   1.126      }
   1.127  
   1.128 -    r = mailmime_add_part(msg_mime, mime);
   1.129 -    assert(r == MAILIMF_NO_ERROR);
   1.130 -    if (r == MAILIMF_ERROR_MEMORY) {
   1.131 -        goto enomem;
   1.132 -    }
   1.133 -    // mailmime_add_part() takes ownership of mime
   1.134 -
   1.135      char *template = strdup("/tmp/pEp.XXXXXXXXXXXXXXXXXXXX");
   1.136      assert(template);
   1.137      if (template == NULL)
   1.138 @@ -182,7 +199,7 @@
   1.139      }
   1.140  
   1.141      fclose(file);
   1.142 -    mailmime_free(msg_mime);
   1.143 +    mailmime_free(mime);
   1.144      *resulttext = buf;
   1.145      return PEP_STATUS_OK;
   1.146  
   1.147 @@ -206,10 +223,8 @@
   1.148      else if (fd != -1)
   1.149          close(fd);
   1.150  
   1.151 -    if (msg_mime)
   1.152 -        mailmime_free(msg_mime);
   1.153 -    if (fields)
   1.154 -        mailimf_fields_free(fields);
   1.155 +    if (mime)
   1.156 +        mailmime_free(mime);
   1.157      if (submime)
   1.158          mailmime_free(submime);
   1.159