...
authorvb
Sun, 15 Mar 2015 13:06:38 +0100
changeset 1147a006f74365d
parent 113 4bee48270834
child 115 cab016770f6f
...
TODO.txt
src/etpan_mime.c
src/etpan_mime.h
src/mime.c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/TODO.txt	Sun Mar 15 13:06:38 2015 +0100
     1.3 @@ -0,0 +1,2 @@
     1.4 +- check if own key is good; if not: create a new keypair
     1.5 +
     2.1 --- a/src/etpan_mime.c	Sun Mar 15 10:14:43 2015 +0100
     2.2 +++ b/src/etpan_mime.c	Sun Mar 15 13:06:38 2015 +0100
     2.3 @@ -146,31 +146,59 @@
     2.4  	return NULL;
     2.5  }
     2.6  
     2.7 +struct mailmime * get_pgp_encrypted_part(void)
     2.8 +{
     2.9 +	struct mailmime * mime;
    2.10 +	struct mailmime_fields * mime_fields;
    2.11 +	struct mailmime_content * content;
    2.12 +
    2.13 +	content = mailmime_content_new_with_str("application/pgp-encrypted");
    2.14 +    mime_fields = mailmime_fields_new_empty();
    2.15 +	mime = part_new_empty(content, mime_fields, NULL, 1);
    2.16 +    mailmime_set_body_text(mime, "Version: 1\n", 10);
    2.17 +
    2.18 +	return mime;
    2.19 +}
    2.20 +
    2.21  struct mailmime * get_text_part(
    2.22 +        const char * filename,
    2.23          const char * mime_type,
    2.24          const char * text,
    2.25          size_t length,
    2.26          int encoding_type
    2.27      )
    2.28  {
    2.29 +    char * disposition_name = NULL;
    2.30  	struct mailmime_fields * mime_fields;
    2.31  	struct mailmime * mime;
    2.32  	struct mailmime_content * content;
    2.33  	struct mailmime_parameter * param;
    2.34  	struct mailmime_disposition * disposition;
    2.35 -	struct mailmime_mechanism * encoding;
    2.36 +	struct mailmime_mechanism * encoding = NULL;
    2.37      
    2.38 -	encoding = mailmime_mechanism_new(encoding_type, NULL);
    2.39 +    if (filename != NULL)
    2.40 +        disposition_name = strdup(filename);
    2.41 +
    2.42 +    if (encoding_type)
    2.43 +        encoding = mailmime_mechanism_new(encoding_type, NULL);
    2.44 +
    2.45  	disposition = mailmime_disposition_new_with_data(MAILMIME_DISPOSITION_TYPE_INLINE,
    2.46 -		NULL, NULL, NULL, NULL, (size_t) -1);
    2.47 +		disposition_name, NULL, NULL, NULL, (size_t) -1);
    2.48 +
    2.49  	mime_fields = mailmime_fields_new_with_data(encoding,
    2.50  		NULL, NULL, disposition, NULL);
    2.51  
    2.52  	content = mailmime_content_new_with_str(mime_type);
    2.53 -	param = mailmime_param_new_with_data("charset", "utf-8");
    2.54 -	clist_append(content->ct_parameters, param);
    2.55 +    
    2.56 +    if (encoding_type != MAILMIME_MECHANISM_7BIT) {
    2.57 +        param = mailmime_param_new_with_data("charset", "utf-8");
    2.58 +        clist_append(content->ct_parameters, param);
    2.59 +    }
    2.60 +
    2.61  	mime = part_new_empty(content, mime_fields, NULL, 1);
    2.62 -	mailmime_set_body_text(mime, (char *) text, length);
    2.63 +
    2.64 +    if (text)
    2.65 +        mailmime_set_body_text(mime, (char *) text, length);
    2.66  	
    2.67  	return mime;
    2.68  }
     3.1 --- a/src/etpan_mime.h	Sun Mar 15 10:14:43 2015 +0100
     3.2 +++ b/src/etpan_mime.h	Sun Mar 15 13:06:38 2015 +0100
     3.3 @@ -11,7 +11,10 @@
     3.4          int force_single
     3.5      );
     3.6  
     3.7 +struct mailmime * get_pgp_encrypted_part(void);
     3.8 +
     3.9  struct mailmime * get_text_part(
    3.10 +        const char * filename,
    3.11          const char * mime_type,
    3.12          const char * text,
    3.13          size_t length,
     4.1 --- a/src/mime.c	Sun Mar 15 10:14:43 2015 +0100
     4.2 +++ b/src/mime.c	Sun Mar 15 13:06:38 2015 +0100
     4.3 @@ -147,7 +147,7 @@
     4.4      if (mime == NULL)
     4.5          goto enomem;
     4.6  
     4.7 -    submime = get_text_part("text/plain", plaintext, strlen(plaintext),
     4.8 +    submime = get_text_part("msg.txt", "text/plain", plaintext, strlen(plaintext),
     4.9              MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    4.10      assert(submime);
    4.11      if (submime == NULL)
    4.12 @@ -163,7 +163,7 @@
    4.13          submime = NULL;
    4.14      }
    4.15  
    4.16 -    submime = get_text_part("text/html", htmltext, strlen(htmltext),
    4.17 +    submime = get_text_part("msg.html", "text/html", htmltext, strlen(htmltext),
    4.18              MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    4.19      assert(submime);
    4.20      if (submime == NULL)
    4.21 @@ -585,17 +585,23 @@
    4.22      return status;
    4.23  }
    4.24  
    4.25 -DYNAMIC_API PEP_STATUS mime_encode_message(
    4.26 +bool is_PGP_message(const char *text)
    4.27 +{
    4.28 +    assert(text);
    4.29 +    if (text == NULL)
    4.30 +        return false;
    4.31 +
    4.32 +    return strncmp(text, "-----BEGIN PGP MESSAGE-----", 27) == 0;
    4.33 +}
    4.34 +
    4.35 +static PEP_STATUS mime_encode_message_plain(
    4.36          const message *msg,
    4.37          bool omit_fields,
    4.38 -        char **mimetext
    4.39 +        struct mailmime **result
    4.40      )
    4.41  {
    4.42 -    struct mailmime * msg_mime = NULL;
    4.43      struct mailmime * mime = NULL;
    4.44      struct mailmime * submime = NULL;
    4.45 -    struct mailimf_fields * fields = NULL;
    4.46 -    char *buf = NULL;
    4.47      int r;
    4.48      PEP_STATUS status;
    4.49      char *subject;
    4.50 @@ -603,18 +609,7 @@
    4.51      char *htmltext;
    4.52  
    4.53      assert(msg);
    4.54 -    assert(mimetext);
    4.55 -
    4.56 -    *mimetext = NULL;
    4.57 -
    4.58 -    if (msg->mime == PEP_MIME) {
    4.59 -        assert(0); // why encoding again what is already encoded?
    4.60 -        buf = strdup(msg->longmsg);
    4.61 -        if (buf == NULL)
    4.62 -            return PEP_OUT_OF_MEMORY;
    4.63 -        *mimetext = buf;
    4.64 -        return PEP_STATUS_OK;
    4.65 -    }
    4.66 +    assert(result);
    4.67  
    4.68      subject = (msg->shortmsg) ? msg->shortmsg : "pEp";
    4.69      plaintext = (msg->longmsg) ? msg->longmsg : "";
    4.70 @@ -626,8 +621,12 @@
    4.71              goto pep_error;
    4.72      }
    4.73      else {
    4.74 -        mime = get_text_part("text/plain", plaintext, strlen(plaintext),
    4.75 -                MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    4.76 +        if (is_PGP_message(plaintext))
    4.77 +            mime = get_text_part("msg.asc", "application/octet-stream", plaintext,
    4.78 +                    strlen(plaintext), MAILMIME_MECHANISM_7BIT);
    4.79 +        else
    4.80 +            mime = get_text_part("msg.txt", "text/plain", plaintext, strlen(plaintext),
    4.81 +                    MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    4.82          assert(mime);
    4.83          if (mime == NULL)
    4.84              goto enomem;
    4.85 @@ -673,6 +672,138 @@
    4.86          }
    4.87      }
    4.88  
    4.89 +    *result = mime;
    4.90 +    return PEP_STATUS_OK;
    4.91 +
    4.92 +enomem:
    4.93 +    status = PEP_OUT_OF_MEMORY;
    4.94 +
    4.95 +pep_error:
    4.96 +    if (mime)
    4.97 +        mailmime_free(mime);
    4.98 +
    4.99 +    if (submime)
   4.100 +        mailmime_free(submime);
   4.101 +
   4.102 +    return status;
   4.103 +}
   4.104 +
   4.105 +static PEP_STATUS mime_encode_message_PGP_MIME(
   4.106 +        const message * msg,
   4.107 +        bool omit_fields,
   4.108 +        struct mailmime **result
   4.109 +    )
   4.110 +{
   4.111 +    struct mailmime * mime = NULL;
   4.112 +    struct mailmime * submime = NULL;
   4.113 +	struct mailmime_parameter * param;
   4.114 +    int r;
   4.115 +    PEP_STATUS status;
   4.116 +    char *subject;
   4.117 +    char *plaintext;
   4.118 +
   4.119 +    assert(msg->longmsg);
   4.120 +
   4.121 +    subject = (msg->shortmsg) ? msg->shortmsg : "pEp";
   4.122 +    plaintext = msg->longmsg;
   4.123 +
   4.124 +    mime = part_multiple_new("multipart/encrypted", NULL);
   4.125 +    assert(mime);
   4.126 +    if (mime == NULL)
   4.127 +        goto enomem;
   4.128 +
   4.129 +    param = mailmime_param_new_with_data("protocol", "application/pgp-encrypted");
   4.130 +    clist_append(mime->mm_content_type->ct_parameters, param);
   4.131 +
   4.132 +    submime = get_pgp_encrypted_part();
   4.133 +    assert(submime);
   4.134 +    if (submime == NULL)
   4.135 +        goto enomem;
   4.136 +
   4.137 +    r = mailmime_smart_add_part(mime, submime);
   4.138 +    assert(r == MAILIMF_NO_ERROR);
   4.139 +    if (r == MAILIMF_ERROR_MEMORY) {
   4.140 +        goto enomem;
   4.141 +    }
   4.142 +    else {
   4.143 +        // mailmime_smart_add_part() takes ownership of submime
   4.144 +        submime = NULL;
   4.145 +    }
   4.146 +
   4.147 +    submime = get_text_part("msg.asc", "application/octet-stream", plaintext,
   4.148 +            strlen(plaintext), MAILMIME_MECHANISM_7BIT);
   4.149 +    assert(submime);
   4.150 +    if (submime == NULL)
   4.151 +        goto enomem;
   4.152 +
   4.153 +    r = mailmime_smart_add_part(mime, submime);
   4.154 +    assert(r == MAILIMF_NO_ERROR);
   4.155 +    if (r == MAILIMF_ERROR_MEMORY) {
   4.156 +        goto enomem;
   4.157 +    }
   4.158 +    else {
   4.159 +        // mailmime_smart_add_part() takes ownership of submime
   4.160 +        submime = NULL;
   4.161 +    }
   4.162 +
   4.163 +    *result = mime;
   4.164 +    return PEP_STATUS_OK;
   4.165 +
   4.166 +enomem:
   4.167 +    status = PEP_OUT_OF_MEMORY;
   4.168 +
   4.169 +pep_error:
   4.170 +    if (mime)
   4.171 +        mailmime_free(mime);
   4.172 +
   4.173 +    if (submime)
   4.174 +        mailmime_free(submime);
   4.175 +
   4.176 +    return status;
   4.177 +}
   4.178 +
   4.179 +DYNAMIC_API PEP_STATUS mime_encode_message(
   4.180 +        const message * msg,
   4.181 +        bool omit_fields,
   4.182 +        char **mimetext
   4.183 +    )
   4.184 +{
   4.185 +    PEP_STATUS status = PEP_STATUS_OK;
   4.186 +    struct mailmime * msg_mime = NULL;
   4.187 +    struct mailmime * mime = NULL;
   4.188 +    struct mailimf_fields * fields = NULL;
   4.189 +    char *buf = NULL;
   4.190 +    int r;
   4.191 +
   4.192 +    assert(msg);
   4.193 +    assert(msg->mime == PEP_MIME_none);
   4.194 +    assert(mimetext);
   4.195 +
   4.196 +    *mimetext = NULL;
   4.197 +
   4.198 +    switch (msg->enc_format) {
   4.199 +        case PEP_enc_none:
   4.200 +            status = mime_encode_message_plain(msg, omit_fields, &mime);
   4.201 +            break;
   4.202 +
   4.203 +        case PEP_enc_pieces:
   4.204 +            status = mime_encode_message_plain(msg, omit_fields, &mime);
   4.205 +            break;
   4.206 +
   4.207 +        case PEP_enc_S_MIME:
   4.208 +            NOT_IMPLEMENTED
   4.209 +                
   4.210 +        case PEP_enc_PGP_MIME:
   4.211 +            status = mime_encode_message_PGP_MIME(msg, omit_fields, &mime);
   4.212 +            break;
   4.213 +
   4.214 +        case PEP_enc_PEP:
   4.215 +            NOT_IMPLEMENTED
   4.216 +    }
   4.217 +
   4.218 +    if (status != PEP_STATUS_OK)
   4.219 +        goto pep_error;
   4.220 +
   4.221      msg_mime = mailmime_new_message_data(NULL);
   4.222      assert(msg_mime);
   4.223      if (msg_mime == NULL)
   4.224 @@ -683,6 +814,7 @@
   4.225          mailmime_free(mime);
   4.226          goto enomem;
   4.227      }
   4.228 +    mime = NULL;
   4.229  
   4.230      if (!omit_fields) {
   4.231          status = build_fields(msg, &fields);
   4.232 @@ -711,9 +843,6 @@
   4.233          if (mime)
   4.234              mailmime_free(mime);
   4.235  
   4.236 -    if (submime)
   4.237 -        mailmime_free(submime);
   4.238 -
   4.239      return status;
   4.240  }
   4.241