...
authorvb
Mon, 09 Feb 2015 09:49:26 -0500
changeset 5988429085f8da
parent 58 92d4cc3df7f4
child 60 8b1f6d763e3d
...
src/etpan_mime.c
src/etpan_mime.h
src/mime.c
src/mime.h
src/platform_windows.cpp
     1.1 --- a/src/etpan_mime.c	Sun Feb 08 15:56:04 2015 -0500
     1.2 +++ b/src/etpan_mime.c	Mon Feb 09 09:49:26 2015 -0500
     1.3 @@ -174,6 +174,40 @@
     1.4  	return mime;
     1.5  }
     1.6  
     1.7 +struct mailmime * get_file_part(
     1.8 +        const char * filename,
     1.9 +        const char * mime_type,
    1.10 +        char * data,
    1.11 +        size_t length
    1.12 +    )
    1.13 +{
    1.14 +    char * disposition_name;
    1.15 +    int encoding_type;
    1.16 +    struct mailmime_disposition * disposition;
    1.17 +    struct mailmime_mechanism * encoding;
    1.18 +    struct mailmime_content * content;
    1.19 +    struct mailmime * mime;
    1.20 +    struct mailmime_fields * mime_fields;
    1.21 +
    1.22 +    disposition_name = NULL;
    1.23 +    if (filename != NULL) {
    1.24 +        disposition_name = strdup(filename);
    1.25 +    }
    1.26 +    disposition =
    1.27 +        mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_ATTACHMENT,
    1.28 +                disposition_name, NULL, NULL, NULL, (size_t) -1);
    1.29 +    content = mailmime_content_new_with_str(mime_type);
    1.30 +
    1.31 +    encoding_type = MAILMIME_MECHANISM_BASE64;
    1.32 +    encoding = mailmime_mechanism_new(encoding_type, NULL);
    1.33 +    mime_fields = mailmime_fields_new_with_data(encoding,
    1.34 +        NULL, NULL, disposition, NULL);
    1.35 +    mime = part_new_empty(content, mime_fields, NULL, 1);
    1.36 +    mailmime_set_body_text(mime, data, length);
    1.37 +
    1.38 +    return mime;
    1.39 +}
    1.40 +
    1.41  struct mailmime * part_multiple_new(
    1.42          const char * type,
    1.43          const char * boundary_prefix
     2.1 --- a/src/etpan_mime.h	Sun Feb 08 15:56:04 2015 -0500
     2.2 +++ b/src/etpan_mime.h	Mon Feb 09 09:49:26 2015 -0500
     2.3 @@ -17,6 +17,13 @@
     2.4          int encoding_type
     2.5      );
     2.6  
     2.7 +struct mailmime * get_file_part(
     2.8 +        const char * filename,
     2.9 +        const char * mime_type,
    2.10 +        char * data,
    2.11 +        size_t length
    2.12 +    );
    2.13 +
    2.14  struct mailmime * part_multiple_new(
    2.15          const char * type,
    2.16          const char * boundary_prefix
     3.1 --- a/src/mime.c	Sun Feb 08 15:56:04 2015 -0500
     3.2 +++ b/src/mime.c	Mon Feb 09 09:49:26 2015 -0500
     3.3 @@ -12,11 +12,10 @@
     3.4  DYNAMIC_API PEP_STATUS mime_encode_text(
     3.5          const char *plaintext,
     3.6          const char *htmltext,
     3.7 +        bloblist_t *attachments,
     3.8          char **resulttext
     3.9      )
    3.10  {
    3.11 -    struct mailmime * msg_mime = NULL;
    3.12 -	struct mailimf_fields * fields = NULL;
    3.13      struct mailmime * mime = NULL;
    3.14      struct mailmime * submime = NULL;
    3.15      int col;
    3.16 @@ -32,18 +31,6 @@
    3.17  
    3.18      *resulttext = NULL;
    3.19  
    3.20 -    msg_mime = mailmime_new_message_data(NULL);
    3.21 -    assert(msg_mime);
    3.22 -    if (msg_mime == NULL)
    3.23 -        goto enomem;
    3.24 -
    3.25 -    fields = mailimf_fields_new_empty();
    3.26 -    assert(fields);
    3.27 -    if (fields == NULL)
    3.28 -        goto enomem;
    3.29 -
    3.30 -    mailmime_set_imf_fields(msg_mime, fields);
    3.31 -
    3.32      if (htmltext) {
    3.33          mime = part_multiple_new("multipart/alternative", NULL);
    3.34          assert(mime);
    3.35 @@ -53,15 +40,12 @@
    3.36          submime = get_text_part("text/plain", plaintext, strlen(plaintext),
    3.37                  MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    3.38          assert(submime);
    3.39 -        if (submime == NULL) {
    3.40 -            mailmime_free(msg_mime);
    3.41 +        if (submime == NULL)
    3.42              goto enomem;
    3.43 -        }
    3.44  
    3.45          r = mailmime_smart_add_part(mime, submime);
    3.46          assert(r == MAILIMF_NO_ERROR);
    3.47          if (r == MAILIMF_ERROR_MEMORY) {
    3.48 -            mailmime_free(msg_mime);
    3.49              goto enomem;
    3.50          }
    3.51          else {
    3.52 @@ -72,17 +56,13 @@
    3.53          submime = get_text_part("text/html", htmltext, strlen(htmltext),
    3.54                  MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    3.55          assert(submime);
    3.56 -        if (submime == NULL) {
    3.57 -            mailmime_free(msg_mime);
    3.58 +        if (submime == NULL)
    3.59              goto enomem;
    3.60 -        }
    3.61  
    3.62          r = mailmime_smart_add_part(mime, submime);
    3.63          assert(r == MAILIMF_NO_ERROR);
    3.64 -        if (r == MAILIMF_ERROR_MEMORY) {
    3.65 -            mailmime_free(msg_mime);
    3.66 +        if (r == MAILIMF_ERROR_MEMORY)
    3.67              goto enomem;
    3.68 -        }
    3.69          else {
    3.70              // mailmime_smart_add_part() takes ownership of submime
    3.71              submime = NULL;
    3.72 @@ -92,19 +72,56 @@
    3.73          mime = get_text_part("text/plain", plaintext, strlen(plaintext),
    3.74                  MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    3.75          assert(mime);
    3.76 -        if (mime == NULL) {
    3.77 -            mailmime_free(msg_mime);
    3.78 +        if (mime == NULL)
    3.79 +            goto enomem;
    3.80 +    }
    3.81 +
    3.82 +    if (attachments) {
    3.83 +        submime = mime;
    3.84 +        mime = part_multiple_new("multipart/mixed", NULL);
    3.85 +        assert(mime);
    3.86 +        if (mime == NULL)
    3.87 +            goto enomem;
    3.88 +
    3.89 +        r = mailmime_smart_add_part(mime, submime);
    3.90 +        assert(r == MAILIMF_NO_ERROR);
    3.91 +        if (r == MAILIMF_ERROR_MEMORY) {
    3.92              goto enomem;
    3.93          }
    3.94 +        else {
    3.95 +            // mailmime_smart_add_part() takes ownership of submime
    3.96 +            submime = NULL;
    3.97 +        }
    3.98 +
    3.99 +        bloblist_t *_a;
   3.100 +        for (_a = attachments; _a != NULL; _a = _a->next) {
   3.101 +            char * mime_type;
   3.102 +
   3.103 +            assert(_a->data);
   3.104 +            assert(_a->size);
   3.105 +
   3.106 +            if (_a->mime_type == NULL)
   3.107 +                mime_type = "application/octet-stream";
   3.108 +            else
   3.109 +                mime_type = _a->mime_type;
   3.110 +
   3.111 +            submime = get_file_part(_a->file_name, mime_type, _a->data, _a->size);
   3.112 +            assert(submime);
   3.113 +            if (submime == NULL)
   3.114 +                goto enomem;
   3.115 +
   3.116 +            r = mailmime_smart_add_part(mime, submime);
   3.117 +            assert(r == MAILIMF_NO_ERROR);
   3.118 +            if (r == MAILIMF_ERROR_MEMORY) {
   3.119 +                goto enomem;
   3.120 +            }
   3.121 +            else {
   3.122 +                // mailmime_smart_add_part() takes ownership of submime
   3.123 +                submime = NULL;
   3.124 +            }
   3.125 +        }
   3.126      }
   3.127  
   3.128 -    r = mailmime_add_part(msg_mime, mime);
   3.129 -    assert(r == MAILIMF_NO_ERROR);
   3.130 -    if (r == MAILIMF_ERROR_MEMORY) {
   3.131 -        goto enomem;
   3.132 -    }
   3.133 -    // mailmime_add_part() takes ownership of mime
   3.134 -
   3.135      char *template = strdup("/tmp/pEp.XXXXXXXXXXXXXXXXXXXX");
   3.136      assert(template);
   3.137      if (template == NULL)
   3.138 @@ -182,7 +199,7 @@
   3.139      }
   3.140  
   3.141      fclose(file);
   3.142 -    mailmime_free(msg_mime);
   3.143 +    mailmime_free(mime);
   3.144      *resulttext = buf;
   3.145      return PEP_STATUS_OK;
   3.146  
   3.147 @@ -206,10 +223,8 @@
   3.148      else if (fd != -1)
   3.149          close(fd);
   3.150  
   3.151 -    if (msg_mime)
   3.152 -        mailmime_free(msg_mime);
   3.153 -    if (fields)
   3.154 -        mailimf_fields_free(fields);
   3.155 +    if (mime)
   3.156 +        mailmime_free(mime);
   3.157      if (submime)
   3.158          mailmime_free(submime);
   3.159  
     4.1 --- a/src/mime.h	Sun Feb 08 15:56:04 2015 -0500
     4.2 +++ b/src/mime.h	Mon Feb 09 09:49:26 2015 -0500
     4.3 @@ -1,6 +1,6 @@
     4.4  #pragma once
     4.5  
     4.6 -#include "pEpEngine.h"
     4.7 +#include "transport.h"
     4.8  
     4.9  #ifdef __cplusplus
    4.10  extern "C" {
    4.11 @@ -13,6 +13,7 @@
    4.12  //      plaintext (in)          plaintext of message as UTF-8 string
    4.13  //      htmltext (in)           optional HTML version of message as UTF-8
    4.14  //                              string or NULL if it does not apply
    4.15 +//      attachments (in)        attatchments or NULL if there are none
    4.16  //      resulttext (out)        the resulting encoded text or NULL on any error
    4.17  //
    4.18  //  return value:
    4.19 @@ -25,12 +26,13 @@
    4.20  //      PEP_OUT_OF_MEMORY       if not enough memory could be allocated
    4.21  //
    4.22  //  caveat:
    4.23 -//      the resulttext will go to the ownership of the caller; plaintext
    4.24 -//      and htmltext will remain in the ownership of the caller
    4.25 +//      the resulttext will go to the ownership of the caller; plaintext,
    4.26 +//      htmltext and attachments will remain in the ownership of the caller
    4.27  
    4.28  DYNAMIC_API PEP_STATUS mime_encode_text(
    4.29          const char *plaintext,
    4.30          const char *htmltext,
    4.31 +        bloblist_t *attachments,
    4.32          char **resulttext
    4.33      );
    4.34  
     5.1 --- a/src/platform_windows.cpp	Sun Feb 08 15:56:04 2015 -0500
     5.2 +++ b/src/platform_windows.cpp	Mon Feb 09 09:49:26 2015 -0500
     5.3 @@ -203,3 +203,4 @@
     5.4  }
     5.5  
     5.6  } // "C"
     5.7 +