src/mime.c
changeset 90 42b5eb9d5af2
parent 89 aef5a4bc78f3
child 91 00c67be2d56d
     1.1 --- a/src/mime.c	Sat Mar 07 20:29:13 2015 +0100
     1.2 +++ b/src/mime.c	Sun Mar 08 00:35:18 2015 +0100
     1.3 @@ -33,7 +33,7 @@
     1.4  
     1.5      r = unlink(template);
     1.6      assert(r == 0);
     1.7 -    if (r == -1)
     1.8 +    if (r)
     1.9          goto err_file;
    1.10  
    1.11      free(template);
    1.12 @@ -230,12 +230,88 @@
    1.13      return status;
    1.14  }
    1.15  
    1.16 +static struct mailimf_mailbox_list * mbl_from_identity(const pEp_identity *ident)
    1.17 +{
    1.18 +    struct mailimf_mailbox_list *mbl = NULL;
    1.19 +    struct mailimf_mailbox *mb = NULL;
    1.20 +    clist *list = NULL;
    1.21 +    int r;
    1.22 +
    1.23 +    assert(ident);
    1.24 +
    1.25 +    list = clist_new();
    1.26 +    if (list == NULL)
    1.27 +        goto enomem;
    1.28 +
    1.29 +    mb = mailbox_from_string(ident->username, ident->address);
    1.30 +    if (mb == NULL)
    1.31 +        goto enomem;
    1.32 +
    1.33 +    r = clist_append(list, mb);
    1.34 +    if (r)
    1.35 +        goto enomem;
    1.36 +
    1.37 +    mbl = mailimf_mailbox_list_new(list);
    1.38 +    if (mbl == NULL)
    1.39 +        goto enomem;
    1.40 +
    1.41 +    return mbl;
    1.42 +
    1.43 +enomem:
    1.44 +    if (mb)
    1.45 +        mailimf_mailbox_free(mb);
    1.46 +
    1.47 +    if (list)
    1.48 +        clist_free(list);
    1.49 +
    1.50 +    return NULL;
    1.51 +}
    1.52 +
    1.53 +static struct mailimf_mailbox_list * mbl_from_identity_list(identity_list *il)
    1.54 +{
    1.55 +    struct mailimf_mailbox_list *mbl = NULL;
    1.56 +    struct mailimf_mailbox *mb = NULL;
    1.57 +    clist *list = NULL;
    1.58 +    int r;
    1.59 +
    1.60 +    assert(il);
    1.61 +
    1.62 +    list = clist_new();
    1.63 +    if (list == NULL)
    1.64 +        goto enomem;
    1.65 +
    1.66 +    identity_list *_il;
    1.67 +    for (_il = il; _il; _il = _il->next) {
    1.68 +        mb = mailbox_from_string(_il->ident->username, _il->ident->address);
    1.69 +        if (mb == NULL)
    1.70 +            goto enomem;
    1.71 +
    1.72 +        r = clist_append(list, mb);
    1.73 +        if (r)
    1.74 +            goto enomem;
    1.75 +    }
    1.76 +
    1.77 +    mbl = mailimf_mailbox_list_new(list);
    1.78 +    if (mbl == NULL)
    1.79 +        goto enomem;
    1.80 +
    1.81 +    return mbl;
    1.82 +
    1.83 +enomem:
    1.84 +    if (mb)
    1.85 +        mailimf_mailbox_free(mb);
    1.86 +
    1.87 +    if (list)
    1.88 +        clist_free(list);
    1.89 +
    1.90 +    return NULL;
    1.91 +}
    1.92 +
    1.93  static PEP_STATUS build_fields(const message *msg, struct mailimf_fields **result)
    1.94  {
    1.95      PEP_STATUS status = PEP_STATUS_OK;
    1.96      struct mailimf_fields * fields = NULL;
    1.97      int r;
    1.98 -    clist * list;
    1.99      clist * fields_list = NULL;
   1.100      char *subject = msg->shortmsg ? msg->shortmsg : "pEp";
   1.101  
   1.102 @@ -251,15 +327,71 @@
   1.103      if (fields_list == NULL)
   1.104          goto enomem;
   1.105  
   1.106 -    r = _append_field(fields_list, MAILIMF_FIELD_MESSAGE_ID,
   1.107 -            (_new_func_t) mailimf_message_id_new, msg->id);
   1.108 -    if (r == -1)
   1.109 -        goto enomem;
   1.110 +    if (msg->id) {
   1.111 +        char *_msgid = strdup(msg->id);
   1.112 +        if (_msgid == NULL)
   1.113 +            goto enomem;
   1.114  
   1.115 -    r = _append_field(fields_list, MAILIMF_FIELD_SUBJECT,
   1.116 -            (_new_func_t) mailimf_subject_new, subject);
   1.117 -    if (r == -1)
   1.118 -        goto enomem;
   1.119 +        r = _append_field(fields_list, MAILIMF_FIELD_MESSAGE_ID,
   1.120 +                (_new_func_t) mailimf_message_id_new, _msgid);
   1.121 +        if (r) {
   1.122 +            free(_msgid);
   1.123 +            goto enomem;
   1.124 +        }
   1.125 +    }
   1.126 +
   1.127 +    /* if (subject) */ {
   1.128 +        char *_subject = strdup(subject);
   1.129 +        if (_subject == NULL)
   1.130 +            goto enomem;
   1.131 +
   1.132 +        r = _append_field(fields_list, MAILIMF_FIELD_SUBJECT,
   1.133 +                (_new_func_t) mailimf_subject_new, _subject);
   1.134 +        if (r) {
   1.135 +            free(_subject);
   1.136 +            goto enomem;
   1.137 +        }
   1.138 +    }
   1.139 +
   1.140 +    if (msg->sent) {
   1.141 +        struct mailimf_date_time * dt = timestamp_to_etpantime(msg->sent);
   1.142 +        if (dt == NULL)
   1.143 +            goto enomem;
   1.144 +
   1.145 +        r = _append_field(fields_list, MAILIMF_FIELD_ORIG_DATE,
   1.146 +                (_new_func_t) mailimf_orig_date_new, dt);
   1.147 +        if (r) {
   1.148 +            mailimf_date_time_free(dt);
   1.149 +            goto enomem;
   1.150 +        }
   1.151 +        dt = NULL;
   1.152 +    }
   1.153 +
   1.154 +    if (msg->from) {
   1.155 +        struct mailimf_mailbox_list *from = mbl_from_identity(msg->from);
   1.156 +        if (from == NULL)
   1.157 +            goto enomem;
   1.158 +
   1.159 +        r = _append_field(fields_list, MAILIMF_FIELD_FROM,
   1.160 +                (_new_func_t) mailimf_from_new, from);
   1.161 +        if (r) {
   1.162 +            mailimf_mailbox_list_free(from);
   1.163 +            goto enomem;
   1.164 +        }
   1.165 +    }
   1.166 +
   1.167 +    if (msg->to) {
   1.168 +        struct mailimf_mailbox_list *to = mbl_from_identity_list(msg->to);
   1.169 +        if (to == NULL)
   1.170 +            goto enomem;
   1.171 +
   1.172 +        r = _append_field(fields_list, MAILIMF_FIELD_FROM,
   1.173 +                (_new_func_t) mailimf_to_new, to);
   1.174 +        if (r) {
   1.175 +            mailimf_mailbox_list_free(to);
   1.176 +            goto enomem;
   1.177 +        }
   1.178 +    }
   1.179  
   1.180      fields = mailimf_fields_new(fields_list);
   1.181      assert(fields);
   1.182 @@ -291,6 +423,7 @@
   1.183      struct mailmime * msg_mime = NULL;
   1.184      struct mailmime * mime = NULL;
   1.185      struct mailmime * submime = NULL;
   1.186 +    struct mailimf_fields * fields = NULL;
   1.187      char *buf = NULL;
   1.188      int r;
   1.189      PEP_STATUS status;
   1.190 @@ -313,7 +446,7 @@
   1.191              goto pep_error;
   1.192      }
   1.193      else {
   1.194 -        mime = get_text_part("text/plain", msg->longmsg, strlen(msg->longmsg),
   1.195 +        mime = get_text_part("text/plain", plaintext, strlen(plaintext),
   1.196                  MAILMIME_MECHANISM_QUOTED_PRINTABLE);
   1.197          assert(mime);
   1.198          if (mime == NULL)
   1.199 @@ -361,14 +494,29 @@
   1.200      }
   1.201  
   1.202      msg_mime = mailmime_new_message_data(NULL);
   1.203 -    mailmime_add_part(msg_mime, mime);
   1.204 +    assert(msg_mime);
   1.205 +    if (msg_mime == NULL)
   1.206 +        goto enomem;
   1.207  
   1.208 -    status = render_mime(mime, &buf);
   1.209 +    r = mailmime_add_part(msg_mime, mime);
   1.210 +    if (r) {
   1.211 +        mailmime_free(mime);
   1.212 +        goto enomem;
   1.213 +    }
   1.214 +
   1.215 +    status = build_fields(msg, &fields);
   1.216 +    if (status != PEP_STATUS_OK)
   1.217 +        goto pep_error;
   1.218 +
   1.219 +    mailmime_set_imf_fields(msg_mime, fields);
   1.220 +
   1.221 +    status = render_mime(msg_mime, &buf);
   1.222      if (status != PEP_STATUS_OK)
   1.223          goto pep_error;
   1.224  
   1.225      mailmime_free(msg_mime);
   1.226      *mimetext = buf;
   1.227 +
   1.228      return PEP_STATUS_OK;
   1.229  
   1.230  enomem:
   1.231 @@ -376,7 +524,7 @@
   1.232  
   1.233  pep_error:
   1.234      if (msg_mime)
   1.235 -        mailmime_free(mime);
   1.236 +        mailmime_free(msg_mime);
   1.237      else
   1.238          if (mime)
   1.239              mailmime_free(mime);
   1.240 @@ -405,7 +553,7 @@
   1.241      r = mailmime_parse(mimetext, strlen(mimetext), &index, &mime);
   1.242      assert(r == 0);
   1.243      assert(mime);
   1.244 -    if (r != 0) {
   1.245 +    if (r) {
   1.246          if (r == MAILIMF_ERROR_MEMORY)
   1.247              goto enomem;
   1.248          else