src/mime.c
changeset 117 d2ff99ccdf30
parent 116 e4d286e7b300
child 118 217e8ebe524a
     1.1 --- a/src/mime.c	Sun Mar 15 19:39:07 2015 +0100
     1.2 +++ b/src/mime.c	Sun Mar 15 21:07:12 2015 +0100
     1.3 @@ -11,6 +11,15 @@
     1.4  
     1.5  #define NOT_IMPLEMENTED assert(0);
     1.6  
     1.7 +DYNAMIC_API bool is_PGP_message_text(const char *text)
     1.8 +{
     1.9 +    assert(text);
    1.10 +    if (text == NULL)
    1.11 +        return false;
    1.12 +
    1.13 +    return strncmp(text, "-----BEGIN PGP MESSAGE-----", 27) == 0;
    1.14 +}
    1.15 +
    1.16  static PEP_STATUS render_mime(struct mailmime *mime, char **mimetext)
    1.17  {
    1.18      PEP_STATUS status = PEP_STATUS_OK;
    1.19 @@ -585,15 +594,6 @@
    1.20      return status;
    1.21  }
    1.22  
    1.23 -bool is_PGP_message(const char *text)
    1.24 -{
    1.25 -    assert(text);
    1.26 -    if (text == NULL)
    1.27 -        return false;
    1.28 -
    1.29 -    return strncmp(text, "-----BEGIN PGP MESSAGE-----", 27) == 0;
    1.30 -}
    1.31 -
    1.32  static PEP_STATUS mime_encode_message_plain(
    1.33          const message *msg,
    1.34          bool omit_fields,
    1.35 @@ -621,7 +621,7 @@
    1.36              goto pep_error;
    1.37      }
    1.38      else {
    1.39 -        if (is_PGP_message(plaintext))
    1.40 +        if (is_PGP_message_text(plaintext))
    1.41              mime = get_text_part("msg.asc", "application/octet-stream", plaintext,
    1.42                      strlen(plaintext), MAILMIME_MECHANISM_7BIT);
    1.43          else
    1.44 @@ -1185,30 +1185,45 @@
    1.45      return status;
    1.46  }
    1.47  
    1.48 -static PEP_STATUS interpret_PGP_MIME(struct mailmime *mime, message **msg)
    1.49 +static PEP_STATUS interpret_PGP_MIME(struct mailmime *mime, message *msg)
    1.50  {
    1.51 -    PEP_STATUS status = PEP_STATUS_OK;
    1.52 -    message *_msg = NULL;
    1.53 -
    1.54      assert(mime);
    1.55      assert(msg);
    1.56  
    1.57 -    *msg = NULL;
    1.58 +    clist *partlist = mime->mm_data.mm_multipart.mm_mp_list;
    1.59 +    if (partlist == NULL)
    1.60 +        return PEP_ILLEGAL_VALUE;
    1.61  
    1.62 -    _msg = calloc(1, sizeof(message));
    1.63 -    assert(_msg);
    1.64 -    if (_msg == NULL)
    1.65 -        goto enomem;
    1.66 +    clistiter *cur = clist_begin(partlist);
    1.67 +    if (cur == NULL)
    1.68 +        return PEP_ILLEGAL_VALUE;
    1.69  
    1.70 -    *msg = _msg;
    1.71 +    cur = clist_next(cur);
    1.72 +    if (cur == NULL)
    1.73 +        return PEP_ILLEGAL_VALUE;
    1.74 +
    1.75 +    struct mailmime *second = clist_content(cur);
    1.76 +    if (second == NULL)
    1.77 +        return PEP_ILLEGAL_VALUE;
    1.78 +
    1.79 +    if (second->mm_body == NULL)
    1.80 +        return PEP_ILLEGAL_VALUE;
    1.81 +
    1.82 +    const char *text = second->mm_body->dt_data.dt_text.dt_data;
    1.83 +    if (text == NULL)
    1.84 +        return PEP_ILLEGAL_VALUE;
    1.85 +
    1.86 +    char *_text = strdup(text);
    1.87 +    if (_text == NULL)
    1.88 +        return PEP_OUT_OF_MEMORY;
    1.89 +
    1.90 +    msg->mime = PEP_MIME_fields_omitted;
    1.91 +    msg->enc_format = PEP_enc_PGP_MIME;
    1.92 +
    1.93 +    free(msg->longmsg);
    1.94 +    msg->longmsg = _text;
    1.95 +
    1.96      return PEP_STATUS_OK;
    1.97 -
    1.98 -enomem:
    1.99 -    status = PEP_OUT_OF_MEMORY;
   1.100 -
   1.101 -pep_error:
   1.102 -    free_message(msg);
   1.103 -    return status;
   1.104  }
   1.105  
   1.106  static bool parameter_has_value(
   1.107 @@ -1219,10 +1234,15 @@
   1.108  {
   1.109      clistiter *cur;
   1.110  
   1.111 +    assert(list);
   1.112 +    assert(name);
   1.113 +    assert(value);
   1.114 +
   1.115      for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
   1.116          struct mailmime_parameter * param = clist_content(cur);
   1.117 -        if (strcmp(name, param->pa_name) == 0 &&
   1.118 -                strcmp(value, param->pa_value) == 0)
   1.119 +        if (param &&
   1.120 +                param->pa_name && strcmp(name, param->pa_name) == 0 &&
   1.121 +                param->pa_value && strcmp(value, param->pa_value) == 0)
   1.122              return true;
   1.123      }
   1.124  
   1.125 @@ -1256,31 +1276,43 @@
   1.126              goto err_mime;
   1.127      }
   1.128  
   1.129 -    struct mailmime_content *content =
   1.130 -            mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
   1.131 +    _msg = calloc(1, sizeof(message));
   1.132 +    assert(_msg);
   1.133 +    if (_msg == NULL)
   1.134 +        goto enomem;
   1.135  
   1.136 -    if (content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
   1.137 -            && content->ct_type->tp_data.tp_composite_type->ct_type ==
   1.138 -                    MAILMIME_COMPOSITE_TYPE_MULTIPART
   1.139 -            && strcmp(content->ct_subtype, "encrypted") == 0
   1.140 -            && parameter_has_value(content->ct_parameters, "protocol",
   1.141 -                    "application/pgp-encrypted")) {
   1.142 -
   1.143 -        status = interpret_PGP_MIME(mime, &_msg);
   1.144 +    if (mime->mm_data.mm_message.mm_fields
   1.145 +            && mime->mm_data.mm_message.mm_fields->fld_list) {
   1.146 +        clist * _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
   1.147 +        status = read_fields(_msg, _fieldlist);
   1.148          if (status != PEP_STATUS_OK)
   1.149              goto pep_error;
   1.150      }
   1.151 -    else {
   1.152  
   1.153 +    if (mime->mm_data.mm_message.mm_msg_mime) {
   1.154 +        struct mailmime_content *content =
   1.155 +                mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
   1.156 +
   1.157 +        if (content->ct_type &&
   1.158 +                content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
   1.159 +                && content->ct_type->tp_data.tp_composite_type
   1.160 +                && content->ct_type->tp_data.tp_composite_type->ct_type ==
   1.161 +                        MAILMIME_COMPOSITE_TYPE_MULTIPART
   1.162 +                && content->ct_subtype
   1.163 +                && strcmp(content->ct_subtype, "encrypted") == 0
   1.164 +                && content->ct_parameters
   1.165 +                && parameter_has_value(content->ct_parameters, "protocol",
   1.166 +                        "application/pgp-encrypted")) {
   1.167 +
   1.168 +            status = interpret_PGP_MIME(mime->mm_data.mm_message.mm_msg_mime,
   1.169 +                    _msg);
   1.170 +            if (status != PEP_STATUS_OK)
   1.171 +                goto pep_error;
   1.172 +        }
   1.173 +        else {
   1.174 +        }
   1.175      }
   1.176  
   1.177 -    assert(_msg);
   1.178 -
   1.179 -    clist * _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
   1.180 -    status = read_fields(_msg, _fieldlist);
   1.181 -    if (status != PEP_STATUS_OK)
   1.182 -        goto pep_error;
   1.183 -
   1.184      mailmime_free(mime);
   1.185      *msg = _msg;
   1.186