...
authorvb
Sun, 01 Mar 2015 15:32:16 +0100
changeset 81044ce1c7fc9c
parent 80 b12ce95403a1
child 82 d0ad5655688a
...
src/message_api.c
src/message_api.h
src/transport.c
src/transport.h
     1.1 --- a/src/message_api.c	Sun Mar 01 01:07:35 2015 +0100
     1.2 +++ b/src/message_api.c	Sun Mar 01 15:32:16 2015 +0100
     1.3 @@ -12,6 +12,7 @@
     1.4  {
     1.5      char * ptext;
     1.6      char * longmsg;
     1.7 +
     1.8      assert(src);
     1.9      assert(src->shortmsg && strcmp(src->shortmsg, "pEp") != 0);
    1.10  
    1.11 @@ -32,34 +33,65 @@
    1.12      return ptext;
    1.13  }
    1.14  
    1.15 -static message * clone_empty_message(const message * src)
    1.16 +static message * clone_to_empty_message(const message * src)
    1.17  {
    1.18      pEp_identity *from = NULL;
    1.19      identity_list *to = NULL;
    1.20 +
    1.21      message * msg = NULL;
    1.22  
    1.23 +    assert(src);
    1.24 +    assert(src->from);
    1.25 +    assert(src->to);
    1.26 +
    1.27 +    msg->dir = src->dir;
    1.28 +
    1.29      from = identity_dup(src->from);
    1.30 -    assert(from);
    1.31      if (from == NULL)
    1.32          goto enomem;
    1.33  
    1.34      from->me = true;
    1.35  
    1.36      to = identity_list_dup(src->to);
    1.37 -    assert(to);
    1.38      if (to == NULL)
    1.39          goto enomem;
    1.40  
    1.41      msg = new_message(src->dir, from, to, NULL);
    1.42 -    assert(msg);
    1.43      if (msg == NULL)
    1.44          goto enomem;
    1.45  
    1.46 +    if (src->cc) {
    1.47 +        msg->cc = identity_list_dup(src->cc);
    1.48 +        if (msg->cc == NULL)
    1.49 +            goto enomem;
    1.50 +    }
    1.51 +
    1.52 +    if (src->bcc) {
    1.53 +        msg->bcc = identity_list_dup(src->bcc);
    1.54 +        if (msg->bcc == NULL)
    1.55 +            goto enomem;
    1.56 +    }
    1.57 +
    1.58 +    if (src->reply_to) {
    1.59 +        msg->reply_to = identity_dup(src->reply_to);
    1.60 +        if (msg->reply_to == NULL)
    1.61 +            goto enomem;
    1.62 +    }
    1.63 +
    1.64 +    msg->sent = src->sent;
    1.65 +    msg->recv = src->recv;
    1.66 +
    1.67      return msg;
    1.68  
    1.69  enomem:
    1.70 -    free_identity(from);
    1.71 -    free_identity_list(to);
    1.72 +    if (msg) {
    1.73 +        free_message(msg);
    1.74 +    }
    1.75 +    else {
    1.76 +        free_identity(from);
    1.77 +        free_identity_list(to);
    1.78 +    }
    1.79 +
    1.80      return NULL;
    1.81  }
    1.82  
    1.83 @@ -68,7 +100,7 @@
    1.84          const message *src,
    1.85          stringlist_t * extra,
    1.86          message **dst,
    1.87 -        PEP_enc_format format
    1.88 +        PEP_enc_format enc_format
    1.89      )
    1.90  {
    1.91      PEP_STATUS status = PEP_STATUS_OK;
    1.92 @@ -78,15 +110,25 @@
    1.93      assert(session);
    1.94      assert(src);
    1.95      assert(dst);
    1.96 +    assert(enc_format >= PEP_enc_pieces);
    1.97 +
    1.98      *dst = NULL;
    1.99 -    assert(format != PEP_enc_none && format != PEP_enc_none_MIME);
   1.100  
   1.101 -    // TODO: we don't yet support re-encrypting already encrypted messages
   1.102 -    if ((int) src->format >= (int) PEP_enc_pieces) {
   1.103 -        NOT_IMPLEMENTED   
   1.104 +    if (src->enc_format >= PEP_enc_pieces) {
   1.105 +        if (src->enc_format == enc_format) {
   1.106 +            msg = message_dup(src);
   1.107 +            if (msg == NULL)
   1.108 +                goto enomem;
   1.109 +            *dst = msg;
   1.110 +            return PEP_STATUS_OK;
   1.111 +        }
   1.112 +        else {
   1.113 +            // TODO: we don't re-encrypt yet
   1.114 +            NOT_IMPLEMENTED
   1.115 +        }
   1.116      }
   1.117  
   1.118 -    msg = clone_empty_message(src);
   1.119 +    msg = clone_to_empty_message(src);
   1.120      if (msg == NULL)
   1.121          goto enomem;
   1.122  
   1.123 @@ -130,7 +172,7 @@
   1.124          char *ctext = NULL;
   1.125          size_t csize = 0;
   1.126  
   1.127 -        switch (format) {
   1.128 +        switch (enc_format) {
   1.129          case PEP_enc_MIME_multipart: {
   1.130              bool free_ptext = false;
   1.131  
   1.132 @@ -146,7 +188,7 @@
   1.133                  ptext = src->longmsg;
   1.134              }
   1.135  
   1.136 -            if (src->format == PEP_enc_none) {
   1.137 +            if (src->enc_format == PEP_enc_none) {
   1.138                  char *_ptext = ptext;
   1.139                  status = mime_encode_text(_ptext, src->longmsg_formatted,
   1.140                          src->attachments, &ptext);
   1.141 @@ -158,7 +200,7 @@
   1.142                      goto pep_error;
   1.143                  free_ptext = true;
   1.144              }
   1.145 -            else if (src->format == PEP_enc_none_MIME) {
   1.146 +            else if (src->enc_format == PEP_enc_none_MIME) {
   1.147                  assert(src->longmsg);
   1.148                  if (src->longmsg == NULL) {
   1.149                      status = PEP_ILLEGAL_VALUE;
   1.150 @@ -186,7 +228,7 @@
   1.151              msg->enc_format = PEP_enc_pieces;
   1.152  
   1.153              // TODO: decoding MIME
   1.154 -            if (src->format == PEP_enc_none_MIME) {
   1.155 +            if (src->enc_format == PEP_enc_none_MIME) {
   1.156                  NOT_IMPLEMENTED
   1.157              }
   1.158  
   1.159 @@ -264,6 +306,10 @@
   1.160              }
   1.161              break;
   1.162  
   1.163 +        case PEP_enc_PEP:
   1.164 +            // TODO: implement
   1.165 +            NOT_IMPLEMENTED
   1.166 +
   1.167          default:
   1.168              assert(0);
   1.169              status = PEP_ILLEGAL_VALUE;
   1.170 @@ -292,7 +338,8 @@
   1.171  DYNAMIC_API PEP_STATUS decrypt_message(
   1.172          PEP_SESSION session,
   1.173          const message *src,
   1.174 -        message **dst
   1.175 +        message **dst,
   1.176 +        PEP_enc_format enc_format
   1.177      )
   1.178  {
   1.179      PEP_STATUS status = PEP_STATUS_OK;
   1.180 @@ -300,12 +347,42 @@
   1.181  
   1.182      assert(session);
   1.183      assert(src);
   1.184 +    assert(src->dir == PEP_dir_incoming);
   1.185      assert(dst);
   1.186 +    assert(enc_format < PEP_enc_pieces);
   1.187  
   1.188      *dst = NULL;
   1.189 -    
   1.190 -    // msg = new_message(src->dir, from, to, NULL);
   1.191 -    NOT_IMPLEMENTED
   1.192 + 
   1.193 +    if (src->enc_format < PEP_enc_pieces) {
   1.194 +        if (enc_format == src->enc_format) {
   1.195 +            msg = message_dup(src);
   1.196 +            if (msg == NULL)
   1.197 +                goto enomem;
   1.198 +            *dst = msg;
   1.199 +            return PEP_STATUS_OK;
   1.200 +        }
   1.201 +        else {
   1.202 +            // TODO: we don't re-encode yet
   1.203 +            NOT_IMPLEMENTED
   1.204 +        }
   1.205 +    }
   1.206 +
   1.207 +    msg = clone_to_empty_message(src);
   1.208 +    if (msg == NULL)
   1.209 +        goto enomem;
   1.210 +
   1.211 +    switch (enc_format) {
   1.212 +        case PEP_enc_none:
   1.213 +            break;
   1.214 +
   1.215 +        case PEP_enc_none_MIME:
   1.216 +            break;
   1.217 +
   1.218 +        default:
   1.219 +            assert(0);
   1.220 +            status = PEP_ILLEGAL_VALUE;
   1.221 +            goto pep_error;
   1.222 +    }
   1.223  
   1.224      *dst = msg;
   1.225      return PEP_STATUS_OK;
     2.1 --- a/src/message_api.h	Sun Mar 01 01:07:35 2015 +0100
     2.2 +++ b/src/message_api.h	Sun Mar 01 15:32:16 2015 +0100
     2.3 @@ -14,7 +14,7 @@
     2.4  //      src (in)            message to encrypt
     2.5  //      extra (in)          extra keys for encryption
     2.6  //      dst (out)           pointer to encrypted message or NULL on failure
     2.7 -//      format (in)         encryption format
     2.8 +//      format (in)         unencrypted format
     2.9  //
    2.10  //  return value:
    2.11  //      PEP_STATUS_OK                   on success
    2.12 @@ -29,7 +29,7 @@
    2.13          const message *src,
    2.14          stringlist_t *extra,
    2.15          message **dst,
    2.16 -        PEP_enc_format format
    2.17 +        PEP_enc_format enc_format
    2.18      );
    2.19  
    2.20  
    2.21 @@ -46,7 +46,8 @@
    2.22  DYNAMIC_API PEP_STATUS decrypt_message(
    2.23          PEP_SESSION session,
    2.24          const message *src,
    2.25 -        message **dst
    2.26 +        message **dst,
    2.27 +        PEP_enc_format enc_format
    2.28      );
    2.29  
    2.30  #ifdef __cplusplus
     3.1 --- a/src/transport.c	Sun Mar 01 01:07:35 2015 +0100
     3.2 +++ b/src/transport.c	Sun Mar 01 15:32:16 2015 +0100
     3.3 @@ -94,8 +94,10 @@
     3.4          const char *file_name)
     3.5  {
     3.6      bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
     3.7 +    assert(bloblist);
     3.8      if (bloblist == NULL)
     3.9          return NULL;
    3.10 +
    3.11      if (mime_type) {
    3.12          bloblist->mime_type = strdup(mime_type);
    3.13          if (bloblist->mime_type == NULL) {
    3.14 @@ -103,6 +105,7 @@
    3.15              return NULL;
    3.16          }
    3.17      }
    3.18 +
    3.19      if (file_name) {
    3.20          bloblist->file_name = strdup(file_name);
    3.21          if (bloblist->file_name == NULL) {
    3.22 @@ -111,8 +114,10 @@
    3.23              return NULL;
    3.24          }
    3.25      }
    3.26 +
    3.27      bloblist->data = blob;
    3.28      bloblist->size = size;
    3.29 +
    3.30      return bloblist;
    3.31  }
    3.32  
    3.33 @@ -128,6 +133,29 @@
    3.34      }
    3.35  }
    3.36  
    3.37 +DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
    3.38 +{
    3.39 +    bloblist_t *bloblist = NULL;
    3.40 +
    3.41 +    assert(src);
    3.42 +
    3.43 +    bloblist = new_bloblist(src->data, src->size, src->mime_type, src->file_name);
    3.44 +    if (bloblist == NULL)
    3.45 +        goto enomem;
    3.46 +
    3.47 +    if (src->next) {
    3.48 +        bloblist->next = bloblist_dup(src->next);
    3.49 +        if (bloblist->next == NULL)
    3.50 +            goto enomem;
    3.51 +    }
    3.52 +
    3.53 +    return bloblist;
    3.54 +
    3.55 +enomem:
    3.56 +    free_bloblist(bloblist);
    3.57 +    return NULL;
    3.58 +}
    3.59 +
    3.60  DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
    3.61          const char *mime_type, const char *file_name)
    3.62  {
    3.63 @@ -206,12 +234,119 @@
    3.64          free_identity(msg->recv_by);
    3.65          free_identity_list(msg->cc);
    3.66          free_identity_list(msg->bcc);
    3.67 +        free_identity(msg->reply_to);
    3.68          free(msg->refering_id);
    3.69 -        free_message_ref_list(msg->refered_by);
    3.70          free(msg);
    3.71      }
    3.72  }
    3.73  
    3.74 +DYNAMIC_API message * message_dup(const message *src)
    3.75 +{
    3.76 +    message * msg = NULL;
    3.77 +    pEp_identity * from = NULL;
    3.78 +    identity_list * to = NULL;
    3.79 +
    3.80 +    assert(src);
    3.81 +
    3.82 +    from = identity_dup(src->from);
    3.83 +    if (from == NULL)
    3.84 +        goto enomem;
    3.85 +
    3.86 +    to = identity_list_dup(src->to);
    3.87 +    if (to == NULL)
    3.88 +        goto enomem;
    3.89 +
    3.90 +    msg = new_message(src->dir, from, to, src->shortmsg);
    3.91 +    if (msg == NULL)
    3.92 +        goto enomem;
    3.93 +
    3.94 +    if (src->id) {
    3.95 +        msg->id = strdup(src->id);
    3.96 +        assert(msg->id);
    3.97 +        if (msg->id == NULL)
    3.98 +            goto enomem;
    3.99 +    }
   3.100 +
   3.101 +    if (src->longmsg) {
   3.102 +        msg->longmsg = strdup(src->longmsg);
   3.103 +        assert(msg->longmsg);
   3.104 +        if (msg->longmsg == NULL)
   3.105 +            goto enomem;
   3.106 +    }
   3.107 +    
   3.108 +    if (src->longmsg_formatted) {
   3.109 +        msg->longmsg_formatted = strdup(src->longmsg_formatted);
   3.110 +        assert(msg->longmsg_formatted);
   3.111 +        if (msg->longmsg_formatted == NULL)
   3.112 +            goto enomem;
   3.113 +    }
   3.114 +
   3.115 +    if (src->attachments) {
   3.116 +        msg->attachments = bloblist_dup(src->attachments);
   3.117 +        if (msg->attachments == NULL)
   3.118 +            goto enomem;
   3.119 +    }
   3.120 +
   3.121 +    msg->rawmsg_ref = src->rawmsg_ref;
   3.122 +    msg->rawmsg_size = src->rawmsg_size;
   3.123 +    msg->sent = src->sent;
   3.124 +    msg->recv = src->recv;
   3.125 +
   3.126 +    if (src->recv_by) {
   3.127 +        msg->recv_by = identity_dup(src->recv_by);
   3.128 +        if (msg->recv_by == NULL)
   3.129 +            goto enomem;
   3.130 +    }
   3.131 +
   3.132 +    if (src->cc) {
   3.133 +        msg->cc = identity_list_dup(src->cc);
   3.134 +        if (msg->cc == NULL)
   3.135 +            goto enomem;
   3.136 +    }
   3.137 +
   3.138 +    if (src->bcc) {
   3.139 +        msg->bcc = identity_list_dup(src->bcc);
   3.140 +        if (msg->bcc == NULL)
   3.141 +            goto enomem;
   3.142 +    }
   3.143 +
   3.144 +    if (src->reply_to) {
   3.145 +        msg->reply_to = identity_dup(src->reply_to);
   3.146 +        if (msg->reply_to == NULL)
   3.147 +            goto enomem;
   3.148 +    }
   3.149 +
   3.150 +    if (src->refering_id) {
   3.151 +        msg->refering_id = strdup(src->refering_id);
   3.152 +        assert(msg->refering_id);
   3.153 +        if (msg->refering_id == NULL)
   3.154 +            goto enomem;
   3.155 +    }
   3.156 +
   3.157 +    msg->refering_msg_ref = src->refering_msg_ref;
   3.158 +    
   3.159 +    if (src->refered_by) {
   3.160 +        msg->refered_by = message_ref_list_dup(src->refered_by);
   3.161 +        if (msg->refered_by == NULL)
   3.162 +            goto enomem;
   3.163 +    }
   3.164 +
   3.165 +    msg->enc_format = src->enc_format;
   3.166 +
   3.167 +    return msg;
   3.168 +
   3.169 +enomem:
   3.170 +    if (msg) {
   3.171 +        free_message(msg);
   3.172 +    }
   3.173 +    else {
   3.174 +        free_identity(from);
   3.175 +        free_identity_list(to);
   3.176 +    }
   3.177 +
   3.178 +    return NULL;
   3.179 +}
   3.180 +
   3.181  DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
   3.182  {
   3.183      message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   3.184 @@ -232,6 +367,31 @@
   3.185      }
   3.186  }
   3.187  
   3.188 +DYNAMIC_API message_ref_list *message_ref_list_dup(
   3.189 +        const message_ref_list *src
   3.190 +    )
   3.191 +{
   3.192 +    message_ref_list * msg_list = NULL;
   3.193 +
   3.194 +    assert(src);
   3.195 +
   3.196 +    msg_list = new_message_ref_list(src->msg_ref);
   3.197 +    if (msg_list == NULL)
   3.198 +        goto enomem;
   3.199 +
   3.200 +    if (src->next) {
   3.201 +        msg_list->next = message_ref_list_dup(src->next);
   3.202 +        if (msg_list->next == NULL)
   3.203 +            goto enomem;
   3.204 +    }
   3.205 +
   3.206 +    return msg_list;
   3.207 +
   3.208 +enomem:
   3.209 +    free_message_ref_list(msg_list);
   3.210 +    return NULL;
   3.211 +}
   3.212 +
   3.213  DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   3.214  {
   3.215      assert(msg);
     4.1 --- a/src/transport.h	Sun Mar 01 01:07:35 2015 +0100
     4.2 +++ b/src/transport.h	Sun Mar 01 15:32:16 2015 +0100
     4.3 @@ -81,10 +81,11 @@
     4.4  
     4.5  DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident);
     4.6  
     4.7 -typedef enum _PEP_msg_format {
     4.8 -    PEP_format_plain = 0,
     4.9 -    PEP_format_html
    4.10 -} PEP_msg_format;
    4.11 +typedef enum _PEP_text_format {
    4.12 +    PEP_text_format_plain = 0,
    4.13 +    PEP_text_format_html,
    4.14 +    PEP_text_format_other = 0xff
    4.15 +} PEP_text_format;
    4.16  
    4.17  typedef enum _PEP_msg_direction {
    4.18      PEP_dir_incoming = 0,
    4.19 @@ -129,6 +130,19 @@
    4.20  DYNAMIC_API void free_bloblist(bloblist_t *bloblist);
    4.21  
    4.22  
    4.23 +// bloblist_dup() - duplicate bloblist
    4.24 +//
    4.25 +//  parameters:
    4.26 +//      src (in)    bloblist to duplicate
    4.27 +//
    4.28 +//  return value:
    4.29 +//      pointer to a new bloblist_t or NULL if out of memory
    4.30 +//
    4.31 +//  caveat:
    4.32 +//      this is an expensive operation because all blobs are copied
    4.33 +
    4.34 +DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src);
    4.35 +
    4.36  // bloblist_add() - add reference to a blob to bloblist
    4.37  //
    4.38  //  parameters:
    4.39 @@ -172,7 +186,6 @@
    4.40                                              // (plain)
    4.41      char * longmsg_formatted;               // UTF-8 string of long message
    4.42                                              // (formatted)
    4.43 -    PEP_msg_format format;                  // format type
    4.44      bloblist_t * attachments;               // blobs with attachements
    4.45      char * rawmsg_ref;                      // reference to raw message data
    4.46      size_t rawmsg_size;                     // size of raw message data
    4.47 @@ -225,15 +238,25 @@
    4.48  // free_message() - free message struct
    4.49  //
    4.50  //  parameters:
    4.51 -//      msg (in)        message struct to free
    4.52 +//      src (in)        message struct to free
    4.53  //
    4.54  //  caveat:
    4.55  //      raw data as well as referenced other messages aren't freed and remain
    4.56  //      in the ownership of the caller
    4.57  
    4.58 -DYNAMIC_API void free_message(message *msg);
    4.59 +DYNAMIC_API void free_message(message *src);
    4.60  
    4.61  
    4.62 +// message_dup - duplicate message (deep copy)
    4.63 +//
    4.64 +//  parameters:
    4.65 +//      msg (in)        message to duplicate
    4.66 +//
    4.67 +//  return value:
    4.68 +//      pointer to duplicate of message pointed by msg or NULL
    4.69 +
    4.70 +DYNAMIC_API message * message_dup(const message *msg);
    4.71 +
    4.72  // new_message_ref_list() - allocate new message reference list
    4.73  //
    4.74  //  parameters:
    4.75 @@ -253,6 +276,18 @@
    4.76  DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list);
    4.77  
    4.78  
    4.79 +// message_ref_list_dup() - duplicate message reference list
    4.80 +//
    4.81 +//  paramters:
    4.82 +//      src (in)        message_ref_list to duplicate
    4.83 +//
    4.84 +//  return value:
    4.85 +//      pointer to new message_ref_list or NULL if out of memory
    4.86 +
    4.87 +DYNAMIC_API message_ref_list *message_ref_list_dup(
    4.88 +        const message_ref_list *src
    4.89 +    );
    4.90 +
    4.91  // message_ref_list_add() - add a reference to a message to a message reference
    4.92  // list
    4.93  //
    4.94 @@ -280,7 +315,7 @@
    4.95                                              // long messages
    4.96      bool formatted_message_supported;       // flag if this transport supports
    4.97                                              // formatted messages
    4.98 -    PEP_msg_format native_format;           // native format of the transport
    4.99 +    PEP_text_format native_text_format;     // native format of the transport
   4.100  };
   4.101  
   4.102  typedef uint64_t transports_mask;