src/mime.c
changeset 114 7a006f74365d
parent 113 4bee48270834
child 116 e4d286e7b300
     1.1 --- a/src/mime.c	Sun Mar 15 10:14:43 2015 +0100
     1.2 +++ b/src/mime.c	Sun Mar 15 13:06:38 2015 +0100
     1.3 @@ -147,7 +147,7 @@
     1.4      if (mime == NULL)
     1.5          goto enomem;
     1.6  
     1.7 -    submime = get_text_part("text/plain", plaintext, strlen(plaintext),
     1.8 +    submime = get_text_part("msg.txt", "text/plain", plaintext, strlen(plaintext),
     1.9              MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    1.10      assert(submime);
    1.11      if (submime == NULL)
    1.12 @@ -163,7 +163,7 @@
    1.13          submime = NULL;
    1.14      }
    1.15  
    1.16 -    submime = get_text_part("text/html", htmltext, strlen(htmltext),
    1.17 +    submime = get_text_part("msg.html", "text/html", htmltext, strlen(htmltext),
    1.18              MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    1.19      assert(submime);
    1.20      if (submime == NULL)
    1.21 @@ -585,17 +585,23 @@
    1.22      return status;
    1.23  }
    1.24  
    1.25 -DYNAMIC_API PEP_STATUS mime_encode_message(
    1.26 +bool is_PGP_message(const char *text)
    1.27 +{
    1.28 +    assert(text);
    1.29 +    if (text == NULL)
    1.30 +        return false;
    1.31 +
    1.32 +    return strncmp(text, "-----BEGIN PGP MESSAGE-----", 27) == 0;
    1.33 +}
    1.34 +
    1.35 +static PEP_STATUS mime_encode_message_plain(
    1.36          const message *msg,
    1.37          bool omit_fields,
    1.38 -        char **mimetext
    1.39 +        struct mailmime **result
    1.40      )
    1.41  {
    1.42 -    struct mailmime * msg_mime = NULL;
    1.43      struct mailmime * mime = NULL;
    1.44      struct mailmime * submime = NULL;
    1.45 -    struct mailimf_fields * fields = NULL;
    1.46 -    char *buf = NULL;
    1.47      int r;
    1.48      PEP_STATUS status;
    1.49      char *subject;
    1.50 @@ -603,18 +609,7 @@
    1.51      char *htmltext;
    1.52  
    1.53      assert(msg);
    1.54 -    assert(mimetext);
    1.55 -
    1.56 -    *mimetext = NULL;
    1.57 -
    1.58 -    if (msg->mime == PEP_MIME) {
    1.59 -        assert(0); // why encoding again what is already encoded?
    1.60 -        buf = strdup(msg->longmsg);
    1.61 -        if (buf == NULL)
    1.62 -            return PEP_OUT_OF_MEMORY;
    1.63 -        *mimetext = buf;
    1.64 -        return PEP_STATUS_OK;
    1.65 -    }
    1.66 +    assert(result);
    1.67  
    1.68      subject = (msg->shortmsg) ? msg->shortmsg : "pEp";
    1.69      plaintext = (msg->longmsg) ? msg->longmsg : "";
    1.70 @@ -626,8 +621,12 @@
    1.71              goto pep_error;
    1.72      }
    1.73      else {
    1.74 -        mime = get_text_part("text/plain", plaintext, strlen(plaintext),
    1.75 -                MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    1.76 +        if (is_PGP_message(plaintext))
    1.77 +            mime = get_text_part("msg.asc", "application/octet-stream", plaintext,
    1.78 +                    strlen(plaintext), MAILMIME_MECHANISM_7BIT);
    1.79 +        else
    1.80 +            mime = get_text_part("msg.txt", "text/plain", plaintext, strlen(plaintext),
    1.81 +                    MAILMIME_MECHANISM_QUOTED_PRINTABLE);
    1.82          assert(mime);
    1.83          if (mime == NULL)
    1.84              goto enomem;
    1.85 @@ -673,6 +672,138 @@
    1.86          }
    1.87      }
    1.88  
    1.89 +    *result = mime;
    1.90 +    return PEP_STATUS_OK;
    1.91 +
    1.92 +enomem:
    1.93 +    status = PEP_OUT_OF_MEMORY;
    1.94 +
    1.95 +pep_error:
    1.96 +    if (mime)
    1.97 +        mailmime_free(mime);
    1.98 +
    1.99 +    if (submime)
   1.100 +        mailmime_free(submime);
   1.101 +
   1.102 +    return status;
   1.103 +}
   1.104 +
   1.105 +static PEP_STATUS mime_encode_message_PGP_MIME(
   1.106 +        const message * msg,
   1.107 +        bool omit_fields,
   1.108 +        struct mailmime **result
   1.109 +    )
   1.110 +{
   1.111 +    struct mailmime * mime = NULL;
   1.112 +    struct mailmime * submime = NULL;
   1.113 +	struct mailmime_parameter * param;
   1.114 +    int r;
   1.115 +    PEP_STATUS status;
   1.116 +    char *subject;
   1.117 +    char *plaintext;
   1.118 +
   1.119 +    assert(msg->longmsg);
   1.120 +
   1.121 +    subject = (msg->shortmsg) ? msg->shortmsg : "pEp";
   1.122 +    plaintext = msg->longmsg;
   1.123 +
   1.124 +    mime = part_multiple_new("multipart/encrypted", NULL);
   1.125 +    assert(mime);
   1.126 +    if (mime == NULL)
   1.127 +        goto enomem;
   1.128 +
   1.129 +    param = mailmime_param_new_with_data("protocol", "application/pgp-encrypted");
   1.130 +    clist_append(mime->mm_content_type->ct_parameters, param);
   1.131 +
   1.132 +    submime = get_pgp_encrypted_part();
   1.133 +    assert(submime);
   1.134 +    if (submime == NULL)
   1.135 +        goto enomem;
   1.136 +
   1.137 +    r = mailmime_smart_add_part(mime, submime);
   1.138 +    assert(r == MAILIMF_NO_ERROR);
   1.139 +    if (r == MAILIMF_ERROR_MEMORY) {
   1.140 +        goto enomem;
   1.141 +    }
   1.142 +    else {
   1.143 +        // mailmime_smart_add_part() takes ownership of submime
   1.144 +        submime = NULL;
   1.145 +    }
   1.146 +
   1.147 +    submime = get_text_part("msg.asc", "application/octet-stream", plaintext,
   1.148 +            strlen(plaintext), MAILMIME_MECHANISM_7BIT);
   1.149 +    assert(submime);
   1.150 +    if (submime == NULL)
   1.151 +        goto enomem;
   1.152 +
   1.153 +    r = mailmime_smart_add_part(mime, submime);
   1.154 +    assert(r == MAILIMF_NO_ERROR);
   1.155 +    if (r == MAILIMF_ERROR_MEMORY) {
   1.156 +        goto enomem;
   1.157 +    }
   1.158 +    else {
   1.159 +        // mailmime_smart_add_part() takes ownership of submime
   1.160 +        submime = NULL;
   1.161 +    }
   1.162 +
   1.163 +    *result = mime;
   1.164 +    return PEP_STATUS_OK;
   1.165 +
   1.166 +enomem:
   1.167 +    status = PEP_OUT_OF_MEMORY;
   1.168 +
   1.169 +pep_error:
   1.170 +    if (mime)
   1.171 +        mailmime_free(mime);
   1.172 +
   1.173 +    if (submime)
   1.174 +        mailmime_free(submime);
   1.175 +
   1.176 +    return status;
   1.177 +}
   1.178 +
   1.179 +DYNAMIC_API PEP_STATUS mime_encode_message(
   1.180 +        const message * msg,
   1.181 +        bool omit_fields,
   1.182 +        char **mimetext
   1.183 +    )
   1.184 +{
   1.185 +    PEP_STATUS status = PEP_STATUS_OK;
   1.186 +    struct mailmime * msg_mime = NULL;
   1.187 +    struct mailmime * mime = NULL;
   1.188 +    struct mailimf_fields * fields = NULL;
   1.189 +    char *buf = NULL;
   1.190 +    int r;
   1.191 +
   1.192 +    assert(msg);
   1.193 +    assert(msg->mime == PEP_MIME_none);
   1.194 +    assert(mimetext);
   1.195 +
   1.196 +    *mimetext = NULL;
   1.197 +
   1.198 +    switch (msg->enc_format) {
   1.199 +        case PEP_enc_none:
   1.200 +            status = mime_encode_message_plain(msg, omit_fields, &mime);
   1.201 +            break;
   1.202 +
   1.203 +        case PEP_enc_pieces:
   1.204 +            status = mime_encode_message_plain(msg, omit_fields, &mime);
   1.205 +            break;
   1.206 +
   1.207 +        case PEP_enc_S_MIME:
   1.208 +            NOT_IMPLEMENTED
   1.209 +                
   1.210 +        case PEP_enc_PGP_MIME:
   1.211 +            status = mime_encode_message_PGP_MIME(msg, omit_fields, &mime);
   1.212 +            break;
   1.213 +
   1.214 +        case PEP_enc_PEP:
   1.215 +            NOT_IMPLEMENTED
   1.216 +    }
   1.217 +
   1.218 +    if (status != PEP_STATUS_OK)
   1.219 +        goto pep_error;
   1.220 +
   1.221      msg_mime = mailmime_new_message_data(NULL);
   1.222      assert(msg_mime);
   1.223      if (msg_mime == NULL)
   1.224 @@ -683,6 +814,7 @@
   1.225          mailmime_free(mime);
   1.226          goto enomem;
   1.227      }
   1.228 +    mime = NULL;
   1.229  
   1.230      if (!omit_fields) {
   1.231          status = build_fields(msg, &fields);
   1.232 @@ -711,9 +843,6 @@
   1.233          if (mime)
   1.234              mailmime_free(mime);
   1.235  
   1.236 -    if (submime)
   1.237 -        mailmime_free(submime);
   1.238 -
   1.239      return status;
   1.240  }
   1.241