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 +    clistiter *cur = clist_begin(partlist);
    1.63 +    if (cur == NULL)
    1.64 +        return PEP_ILLEGAL_VALUE;
    1.65  
    1.66 -    _msg = calloc(1, sizeof(message));
    1.67 -    assert(_msg);
    1.68 -    if (_msg == NULL)
    1.69 -        goto enomem;
    1.70 +    cur = clist_next(cur);
    1.71 +    if (cur == NULL)
    1.72 +        return PEP_ILLEGAL_VALUE;
    1.73 +
    1.74 +    struct mailmime *second = clist_content(cur);
    1.75 +    if (second == NULL)
    1.76 +        return PEP_ILLEGAL_VALUE;
    1.77  
    1.78 -    *msg = _msg;
    1.79 -    return PEP_STATUS_OK;
    1.80 +    if (second->mm_body == NULL)
    1.81 +        return PEP_ILLEGAL_VALUE;
    1.82 +
    1.83 +    const char *text = second->mm_body->dt_data.dt_text.dt_data;
    1.84 +    if (text == NULL)
    1.85 +        return PEP_ILLEGAL_VALUE;
    1.86  
    1.87 -enomem:
    1.88 -    status = PEP_OUT_OF_MEMORY;
    1.89 +    char *_text = strdup(text);
    1.90 +    if (_text == NULL)
    1.91 +        return PEP_OUT_OF_MEMORY;
    1.92  
    1.93 -pep_error:
    1.94 -    free_message(msg);
    1.95 -    return status;
    1.96 +    msg->mime = PEP_MIME_fields_omitted;
    1.97 +    msg->enc_format = PEP_enc_PGP_MIME;
    1.98 +
    1.99 +    free(msg->longmsg);
   1.100 +    msg->longmsg = _text;
   1.101 +
   1.102 +    return PEP_STATUS_OK;
   1.103  }
   1.104  
   1.105  static bool parameter_has_value(
   1.106 @@ -1219,10 +1234,15 @@
   1.107  {
   1.108      clistiter *cur;
   1.109  
   1.110 +    assert(list);
   1.111 +    assert(name);
   1.112 +    assert(value);
   1.113 +
   1.114      for (cur = clist_begin(list); cur != NULL ; cur = clist_next(cur)) {
   1.115          struct mailmime_parameter * param = clist_content(cur);
   1.116 -        if (strcmp(name, param->pa_name) == 0 &&
   1.117 -                strcmp(value, param->pa_value) == 0)
   1.118 +        if (param &&
   1.119 +                param->pa_name && strcmp(name, param->pa_name) == 0 &&
   1.120 +                param->pa_value && strcmp(value, param->pa_value) == 0)
   1.121              return true;
   1.122      }
   1.123  
   1.124 @@ -1256,30 +1276,42 @@
   1.125              goto err_mime;
   1.126      }
   1.127  
   1.128 -    struct mailmime_content *content =
   1.129 -            mime->mm_data.mm_message.mm_msg_mime->mm_content_type;
   1.130 +    _msg = calloc(1, sizeof(message));
   1.131 +    assert(_msg);
   1.132 +    if (_msg == NULL)
   1.133 +        goto enomem;
   1.134  
   1.135 -    if (content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
   1.136 -            && content->ct_type->tp_data.tp_composite_type->ct_type ==
   1.137 -                    MAILMIME_COMPOSITE_TYPE_MULTIPART
   1.138 -            && strcmp(content->ct_subtype, "encrypted") == 0
   1.139 -            && parameter_has_value(content->ct_parameters, "protocol",
   1.140 -                    "application/pgp-encrypted")) {
   1.141 -
   1.142 -        status = interpret_PGP_MIME(mime, &_msg);
   1.143 +    if (mime->mm_data.mm_message.mm_fields
   1.144 +            && mime->mm_data.mm_message.mm_fields->fld_list) {
   1.145 +        clist * _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
   1.146 +        status = read_fields(_msg, _fieldlist);
   1.147          if (status != PEP_STATUS_OK)
   1.148              goto pep_error;
   1.149      }
   1.150 -    else {
   1.151  
   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 -    assert(_msg);
   1.158 +        if (content->ct_type &&
   1.159 +                content->ct_type->tp_type == MAILMIME_TYPE_COMPOSITE_TYPE
   1.160 +                && content->ct_type->tp_data.tp_composite_type
   1.161 +                && content->ct_type->tp_data.tp_composite_type->ct_type ==
   1.162 +                        MAILMIME_COMPOSITE_TYPE_MULTIPART
   1.163 +                && content->ct_subtype
   1.164 +                && strcmp(content->ct_subtype, "encrypted") == 0
   1.165 +                && content->ct_parameters
   1.166 +                && parameter_has_value(content->ct_parameters, "protocol",
   1.167 +                        "application/pgp-encrypted")) {
   1.168  
   1.169 -    clist * _fieldlist = mime->mm_data.mm_message.mm_fields->fld_list;
   1.170 -    status = read_fields(_msg, _fieldlist);
   1.171 -    if (status != PEP_STATUS_OK)
   1.172 -        goto pep_error;
   1.173 +            status = interpret_PGP_MIME(mime->mm_data.mm_message.mm_msg_mime,
   1.174 +                    _msg);
   1.175 +            if (status != PEP_STATUS_OK)
   1.176 +                goto pep_error;
   1.177 +        }
   1.178 +        else {
   1.179 +        }
   1.180 +    }
   1.181  
   1.182      mailmime_free(mime);
   1.183      *msg = _msg;