...
authorvb
Sat, 03 Jan 2015 16:37:53 +0100
changeset 444013b57a6a69
parent 43 816f4ccf84da
child 45 37a80387790d
...
src/message_api.c
src/message_api.h
src/pEpEngine.h
src/transport.h
     1.1 --- a/src/message_api.c	Wed Dec 31 00:19:21 2014 +0100
     1.2 +++ b/src/message_api.c	Sat Jan 03 16:37:53 2015 +0100
     1.3 @@ -1,13 +1,157 @@
     1.4  #include "message_api.h"
     1.5  #include "keymanagement.h"
     1.6  
     1.7 -#include <libetpan/libetpan.h>
     1.8 +#include <libetpan/mailmime.h>
     1.9 +#ifndef mailmime_param_new_with_data
    1.10 +#include <libetpan/mailprivacy_tools.h>
    1.11 +#endif
    1.12  #include <assert.h>
    1.13  #include <string.h>
    1.14  #include <stdlib.h>
    1.15  
    1.16  #define NOT_IMPLEMENTED assert(0);
    1.17  
    1.18 +PEP_STATUS mime_encode_parts(const message *src, message **dst)
    1.19 +{
    1.20 +    struct mailmime * mime_body;
    1.21 +
    1.22 +    assert(src);
    1.23 +    assert(src->enc_format == PEP_enc_none);
    1.24 +    assert(dst);
    1.25 +
    1.26 +    if (src->enc_format != PEP_enc_none)
    1.27 +        return PEP_ILLEGAL_VALUE;
    1.28 +
    1.29 +    *dst = NULL;
    1.30 +
    1.31 +    if (src->longmsg && src->longmsg_formatted) {
    1.32 +        struct mailmime * mime_text;
    1.33 +        struct mailmime * mime_html;
    1.34 +        NOT_IMPLEMENTED
    1.35 +    }
    1.36 +    else if (src->longmsg) {
    1.37 +        struct mailmime_fields * mime_fields
    1.38 +                = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT);
    1.39 +        assert(mime_fields);
    1.40 +        if (mime_fields == NULL)
    1.41 +            return PEP_OUT_OF_MEMORY;
    1.42 +
    1.43 +        struct mailmime_content * content
    1.44 +                = mailmime_content_new_with_str("text/plain");
    1.45 +        assert(content);
    1.46 +        if (content == NULL) {
    1.47 +            mailmime_fields_free(mime_fields);
    1.48 +            return PEP_OUT_OF_MEMORY;
    1.49 +        }
    1.50 +
    1.51 +        struct mailmime_parameter * param
    1.52 +                = mailmime_param_new_with_data("charset", "utf-8");
    1.53 +        assert(param);
    1.54 +        if (param == NULL) {
    1.55 +            mailmime_fields_free(mime_fields);
    1.56 +            mailmime_content_free(content);
    1.57 +            return PEP_OUT_OF_MEMORY;
    1.58 +        }
    1.59 +
    1.60 +        int r = clist_append(content->ct_parameters, param);
    1.61 +        if (r < 0) {
    1.62 +            mailmime_fields_free(mime_fields);
    1.63 +            mailmime_content_free(content);
    1.64 +            mailmime_parameter_free(param);
    1.65 +            return PEP_OUT_OF_MEMORY;
    1.66 +        }
    1.67 +
    1.68 +        mime_body = mailmime_new_empty(content, mime_fields);
    1.69 +        if (mime_body == NULL) {
    1.70 +            mailmime_fields_free(mime_fields);
    1.71 +            mailmime_content_free(content);
    1.72 +            return PEP_OUT_OF_MEMORY;
    1.73 +        }
    1.74 +
    1.75 +        r = mailmime_set_body_text(mime_body, src->longmsg, strlen(src->longmsg));
    1.76 +        if (r != MAILIMF_NO_ERROR) {
    1.77 +            mailmime_free(mime_body);
    1.78 +            return PEP_OUT_OF_MEMORY;
    1.79 +        }
    1.80 +    }
    1.81 +    else if (src->longmsg_formatted) {
    1.82 +        NOT_IMPLEMENTED
    1.83 +    }
    1.84 + 
    1.85 +    char *fn = strdup("/tmp/pEp.XXXXXXXXXX");
    1.86 +    assert(fn);
    1.87 +    if (fn == NULL) {
    1.88 +        mailmime_free(mime_body);
    1.89 +        return PEP_OUT_OF_MEMORY;
    1.90 +    }
    1.91 +
    1.92 +    int f = mkstemp(fn);
    1.93 +    assert(f != -1);
    1.94 +    free(fn);
    1.95 +    if (f == -1) {
    1.96 +        mailmime_free(mime_body);
    1.97 +        return PEP_CANNOT_CREATE_TEMP_FILE;
    1.98 +    }
    1.99 +
   1.100 +    FILE *fp = fdopen(f, "w+");
   1.101 +    assert(fp);
   1.102 +    if (fp == NULL) {
   1.103 +        return PEP_CANNOT_CREATE_TEMP_FILE;
   1.104 +    }
   1.105 +
   1.106 +    int col = 0;
   1.107 +    int r = mailmime_write_file(fp, &col, mime_body);
   1.108 +    assert(r == MAILIMF_NO_ERROR);
   1.109 +    mailmime_free(mime_body);
   1.110 +    if (r != MAILIMF_NO_ERROR) {
   1.111 +        fclose(fp);
   1.112 +        return PEP_CANNOT_CREATE_TEMP_FILE;
   1.113 +    }
   1.114 +
   1.115 +    rewind(fp);
   1.116 +
   1.117 +    char *buf = (char *) calloc(1, col + 1);
   1.118 +    assert(buf);
   1.119 +    if (buf == NULL) {
   1.120 +        fclose(fp);
   1.121 +        return PEP_OUT_OF_MEMORY;
   1.122 +    }
   1.123 +
   1.124 +    size_t size = fread(buf, col, 1, fp);
   1.125 +    assert(size);
   1.126 +    fclose(fp);
   1.127 +    if (size == 0L) {
   1.128 +        free(buf);
   1.129 +        return PEP_CANNOT_CREATE_TEMP_FILE;
   1.130 +    }
   1.131 +
   1.132 +    message *msg = new_message(src->dir, src->from, src->to, src->shortmsg);
   1.133 +    if (msg == NULL) {
   1.134 +        free(buf);
   1.135 +        return PEP_OUT_OF_MEMORY;
   1.136 +    }
   1.137 +    msg->longmsg = buf;
   1.138 +
   1.139 +    *dst = msg;
   1.140 +    return PEP_STATUS_OK;
   1.141 +}
   1.142 +
   1.143 +PEP_STATUS mime_decode_parts(const message *src, message **dst)
   1.144 +{
   1.145 +    PEP_STATUS status = PEP_STATUS_OK;
   1.146 +
   1.147 +    assert(src);
   1.148 +    assert(src->enc_format == PEP_enc_none_MIME);
   1.149 +    assert(dst);
   1.150 +
   1.151 +    if (src->enc_format != PEP_enc_none_MIME)
   1.152 +        return PEP_ILLEGAL_VALUE;
   1.153 +
   1.154 +    *dst = NULL;
   1.155 +
   1.156 +    return status;
   1.157 +}
   1.158 +
   1.159  PEP_STATUS encrypt_message(
   1.160          PEP_SESSION session,
   1.161          const message *src,
   1.162 @@ -91,9 +235,10 @@
   1.163          size_t csize = 0;
   1.164  
   1.165          switch (format) {
   1.166 -        case PEP_enc_MIME_multipart:
   1.167 -            NOT_IMPLEMENTED
   1.168 +        case PEP_enc_MIME_multipart: {
   1.169 +            *dst = msg;
   1.170              break;
   1.171 +        }
   1.172  
   1.173          case PEP_enc_pieces:
   1.174              if (src->shortmsg && src->longmsg) {
     2.1 --- a/src/message_api.h	Wed Dec 31 00:19:21 2014 +0100
     2.2 +++ b/src/message_api.h	Sat Jan 03 16:37:53 2015 +0100
     2.3 @@ -7,6 +7,34 @@
     2.4  #include "transport.h"
     2.5  
     2.6  
     2.7 +// mime_encode_parts() - encode message with MIME
     2.8 +//  parameters:
     2.9 +//      src                 message to encode
    2.10 +//      dst                 encoded message or NULL on error
    2.11 +//
    2.12 +//  return value:
    2.13 +//      error status or PEP_STATUS_OK on success
    2.14 +//
    2.15 +//  caveat:
    2.16 +//      message must be unencrypted
    2.17 +
    2.18 +DYNAMIC_API PEP_STATUS mime_encode_parts(const message *src, message **dst);
    2.19 +
    2.20 +
    2.21 +// mime_decode_parts() - decode MIME message
    2.22 +//  parameters:
    2.23 +//      src                 message to decode
    2.24 +//      dst                 decoded message or NULL on error
    2.25 +//
    2.26 +//  return value:
    2.27 +//      error status or PEP_STATUS_OK on success
    2.28 +//
    2.29 +//  caveat:
    2.30 +//      message must be unencrypted
    2.31 +
    2.32 +DYNAMIC_API PEP_STATUS mime_decode_parts(const message *src, message **dst);
    2.33 +
    2.34 +
    2.35  // encrypt_message() - encrypt message in memory
    2.36  //
    2.37  //  parameters:
    2.38 @@ -21,7 +49,7 @@
    2.39  //      or more keys couldn't be found, but the message could be encrypted
    2.40  //      with other keys
    2.41  
    2.42 -PEP_STATUS encrypt_message(
    2.43 +DYNAMIC_API PEP_STATUS encrypt_message(
    2.44          PEP_SESSION session,
    2.45          const message *src,
    2.46          stringlist_t *extra,
    2.47 @@ -40,7 +68,7 @@
    2.48  //  return value:
    2.49  //      error status or PEP_STATUS_OK on success
    2.50  
    2.51 -PEP_STATUS decrypt_message(
    2.52 +DYNAMIC_API PEP_STATUS decrypt_message(
    2.53          PEP_SESSION session,
    2.54          const message *src,
    2.55          message **dst
     3.1 --- a/src/pEpEngine.h	Wed Dec 31 00:19:21 2014 +0100
     3.2 +++ b/src/pEpEngine.h	Sat Jan 03 16:37:53 2015 +0100
     3.3 @@ -65,6 +65,7 @@
     3.4  
     3.5  	PEP_COMMIT_FAILED								= 0xff01,
     3.6  
     3.7 +    PEP_CANNOT_CREATE_TEMP_FILE                     = -5,
     3.8      PEP_ILLEGAL_VALUE                               = -4,
     3.9      PEP_BUFFER_TOO_SMALL                            = -3,
    3.10  	PEP_OUT_OF_MEMORY								= -2,
     4.1 --- a/src/transport.h	Wed Dec 31 00:19:21 2014 +0100
     4.2 +++ b/src/transport.h	Sat Jan 03 16:37:53 2015 +0100
     4.3 @@ -144,9 +144,14 @@
     4.4  
     4.5  
     4.6  typedef enum _PEP_enc_format {
     4.7 -    PEP_enc_none = 0,
     4.8 -    PEP_enc_MIME_multipart,
     4.9 -    PEP_enc_pieces
    4.10 +    PEP_enc_none = 0,                       // message is in pieces and nor
    4.11 +                                            // encoded nor encrypted
    4.12 +    PEP_enc_none_MIME,                      // message is MIME encoded but not
    4.13 +                                            // encrypted
    4.14 +    PEP_enc_pieces,                         // message is encrypted but not
    4.15 +                                            // MIME encoded
    4.16 +    PEP_enc_MIME_multipart                  // message is encrypted and MIME
    4.17 +                                            // encoded
    4.18  } PEP_enc_format;
    4.19  
    4.20  struct _message_ref_list;