src/message.c
author Neal H. Walfield <neal@pep.foundation>
Wed, 08 May 2019 12:20:37 +0200
branchemail_comparison
changeset 3649 1dd837adc30b
parent 2615 5cd3625e0d91
child 3884 49d25cfc873c
permissions -rw-r--r--
Fix public key.
     1 // This file is under GNU General Public License 3.0
     2 // see LICENSE.txt
     3 
     4 #include "pEp_internal.h"
     5 
     6 #include <stdlib.h>
     7 #include <string.h>
     8 #include <assert.h>
     9 
    10 #include "message.h"
    11 
    12 DYNAMIC_API message *new_message(
    13         PEP_msg_direction dir
    14     )
    15 {
    16     message *msg = calloc(1, sizeof(message));
    17     assert(msg);
    18     if (msg == NULL)
    19         return NULL;
    20 
    21     msg->dir = dir;
    22 
    23     return msg;
    24 }
    25 
    26 DYNAMIC_API void free_message(message *msg)
    27 {
    28     if (msg) {
    29         free(msg->id);
    30         free(msg->shortmsg);
    31         free(msg->longmsg);
    32         free(msg->longmsg_formatted);
    33         free_bloblist(msg->attachments);
    34         free_timestamp(msg->sent);
    35         free_timestamp(msg->recv);
    36         free_identity(msg->from);
    37         free_identity_list(msg->to);
    38         free_identity(msg->recv_by);
    39         free_identity_list(msg->cc);
    40         free_identity_list(msg->bcc);
    41         free_identity_list(msg->reply_to);
    42         free_stringlist(msg->in_reply_to);
    43         free_stringlist(msg->references);
    44         free_stringlist(msg->keywords);
    45         free(msg->comments);
    46         free_stringpair_list(msg->opt_fields);
    47         free(msg);
    48     }
    49 }
    50 
    51 DYNAMIC_API message * message_dup(const message *src)
    52 {
    53     message * msg = NULL;
    54     pEp_identity * from = NULL;
    55     identity_list * to = NULL;
    56 
    57     assert(src);
    58 
    59     msg = new_message(src->dir);
    60     if (msg == NULL)
    61         goto enomem;
    62 
    63     if (src->id) {
    64         msg->id = strdup(src->id);
    65         assert(msg->id);
    66         if (msg->id == NULL)
    67             goto enomem;
    68     }
    69 
    70     if (src->shortmsg) {
    71         msg->shortmsg = strdup(src->shortmsg);
    72         assert(msg->shortmsg);
    73         if (msg->shortmsg == NULL)
    74             goto enomem;
    75     }
    76 
    77     if (src->longmsg) {
    78         msg->longmsg = strdup(src->longmsg);
    79         assert(msg->longmsg);
    80         if (msg->longmsg == NULL)
    81             goto enomem;
    82     }
    83     
    84     if (src->longmsg_formatted) {
    85         msg->longmsg_formatted = strdup(src->longmsg_formatted);
    86         assert(msg->longmsg_formatted);
    87         if (msg->longmsg_formatted == NULL)
    88             goto enomem;
    89     }
    90 
    91     if (src->attachments) {
    92         msg->attachments = bloblist_dup(src->attachments);
    93         if (msg->attachments == NULL)
    94             goto enomem;
    95     }
    96 
    97     msg->rawmsg_ref = src->rawmsg_ref;
    98     msg->rawmsg_size = src->rawmsg_size;
    99 
   100     if (src->sent) {
   101         msg->sent = timestamp_dup(src->sent);
   102         if (msg->sent == NULL)
   103             goto enomem;
   104     }
   105 
   106     if (src->recv) {
   107         msg->recv = timestamp_dup(src->recv);
   108         if (msg->recv == NULL)
   109             goto enomem;
   110     }
   111 
   112     if (src->from) {
   113         msg->from = identity_dup(src->from);
   114         if (msg->from == NULL)
   115             goto enomem;
   116     }
   117 
   118     if (src->recv_by) {
   119         msg->recv_by = identity_dup(src->recv_by);
   120         if (msg->recv_by == NULL)
   121             goto enomem;
   122     }
   123 
   124     if (src->to) {
   125         msg->to = identity_list_dup(src->to);
   126         if (msg->to == NULL)
   127             goto enomem;
   128     }
   129 
   130     if (src->cc) {
   131         msg->cc = identity_list_dup(src->cc);
   132         if (msg->cc == NULL)
   133             goto enomem;
   134     }
   135 
   136     if (src->bcc) {
   137         msg->bcc = identity_list_dup(src->bcc);
   138         if (msg->bcc == NULL)
   139             goto enomem;
   140     }
   141 
   142     if (src->reply_to) {
   143         msg->reply_to = identity_list_dup(src->reply_to);
   144         if (msg->reply_to == NULL)
   145             goto enomem;
   146     }
   147 
   148     if (src->in_reply_to) {
   149         msg->in_reply_to = stringlist_dup(src->in_reply_to);
   150         assert(msg->in_reply_to);
   151         if (msg->in_reply_to == NULL)
   152             goto enomem;
   153     }
   154 
   155     msg->refering_msg_ref = src->refering_msg_ref;
   156     
   157     if (src->references) {
   158         msg->references = stringlist_dup(src->references);
   159         if (msg->references == NULL)
   160             goto enomem;
   161     }
   162 
   163     if (src->refered_by) {
   164         msg->refered_by = message_ref_list_dup(src->refered_by);
   165         if (msg->refered_by == NULL)
   166             goto enomem;
   167     }
   168 
   169     if (src->keywords) {
   170         msg->keywords = stringlist_dup(src->keywords);
   171         if (msg->keywords == NULL)
   172             goto enomem;
   173     }
   174 
   175     if (src->comments) {
   176         msg->comments = strdup(src->comments);
   177         assert(msg->comments);
   178         if (msg->comments == NULL)
   179             goto enomem;
   180     }
   181 
   182     if (src->opt_fields) {
   183         msg->opt_fields = stringpair_list_dup(src->opt_fields);
   184         if (msg->opt_fields == NULL)
   185             goto enomem;
   186     }
   187 
   188     msg->enc_format = src->enc_format;
   189 
   190     return msg;
   191 
   192 enomem:
   193     if (msg) {
   194         free_message(msg);
   195     }
   196     else {
   197         free_identity(from);
   198         free_identity_list(to);
   199     }
   200 
   201     return NULL;
   202 }
   203 
   204 DYNAMIC_API void message_transfer(message* dst, message *src)
   205 {
   206     assert(dst);
   207     assert(src);
   208 
   209     dst->dir = src->dir;
   210     dst->rawmsg_ref = src->rawmsg_ref;
   211     dst->rawmsg_size = src->rawmsg_size;
   212     dst->refering_msg_ref = src->refering_msg_ref;
   213     dst->enc_format = src->enc_format;
   214 
   215     /* Strings */
   216     free(dst->id);
   217     free(dst->shortmsg);
   218     free(dst->longmsg);
   219     free(dst->longmsg_formatted);
   220     free(dst->comments);
   221     dst->id = src->id;
   222     dst->shortmsg = src->shortmsg;
   223     dst->longmsg = src->longmsg;
   224     dst->longmsg_formatted = src->longmsg_formatted;
   225     dst->comments = src->comments;    
   226     src->id = src->shortmsg = src->longmsg = src->longmsg_formatted = NULL;
   227     src->comments = NULL;
   228     
   229     /* bloblists */
   230     free_bloblist(dst->attachments);
   231     dst->attachments = src->attachments;
   232     src->attachments = NULL;
   233     
   234     /* timestamps */
   235     free_timestamp(dst->sent);
   236     free_timestamp(dst->recv);
   237     dst->sent = src->sent;
   238     dst->recv = src->recv;
   239     src->sent = src->recv = NULL;
   240     
   241     /* identities */
   242     free_identity(dst->from);
   243     free_identity(dst->recv_by);
   244     dst->from = src->from;
   245     dst->recv_by = src->recv_by;
   246     src->from = src->recv_by = NULL;
   247     
   248     /* identity lists */
   249     free_identity_list(dst->to);
   250     free_identity_list(dst->cc);
   251     free_identity_list(dst->bcc);
   252     free_identity_list(dst->reply_to);
   253     dst->to = src->to;
   254     dst->cc = src->cc;
   255     dst->bcc = src->bcc;
   256     dst->reply_to = src->reply_to;
   257     src->to = src->cc = src->bcc = src->reply_to = NULL;
   258 
   259     /* stringlists */
   260     free_stringlist(dst->references);
   261     free_stringlist(dst->keywords);
   262     free_stringlist(dst->in_reply_to);
   263     dst->references = src->references;
   264     dst->keywords = src->keywords;
   265     dst->in_reply_to = src->in_reply_to;
   266     src->references = src->keywords = src->in_reply_to = NULL;
   267 
   268     /* message ref list */
   269     free_message_ref_list(dst->refered_by);
   270     dst->refered_by = src->refered_by;
   271     src->refered_by = NULL;
   272     
   273     /* stringpair lists */
   274     free_stringpair_list(dst->opt_fields);
   275     dst->opt_fields = src->opt_fields;
   276     src->opt_fields = NULL;
   277 }
   278 
   279 DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
   280 {
   281     message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   282     assert(msg_list);
   283     if (msg_list == NULL)
   284         return NULL;
   285 
   286     msg_list->msg_ref = msg;
   287 
   288     return msg_list;
   289 }
   290 
   291 DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
   292 {
   293     if (msg_list) {
   294         free_message_ref_list(msg_list->next);
   295         free(msg_list);
   296     }
   297 }
   298 
   299 DYNAMIC_API message_ref_list *message_ref_list_dup(
   300         const message_ref_list *src
   301     )
   302 {
   303     message_ref_list * msg_list = NULL;
   304 
   305     assert(src);
   306 
   307     msg_list = new_message_ref_list(src->msg_ref);
   308     if (msg_list == NULL)
   309         goto enomem;
   310 
   311     if (src->next) {
   312         msg_list->next = message_ref_list_dup(src->next);
   313         if (msg_list->next == NULL)
   314             goto enomem;
   315     }
   316 
   317     return msg_list;
   318 
   319 enomem:
   320     free_message_ref_list(msg_list);
   321     return NULL;
   322 }
   323 
   324 DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   325 {
   326     assert(msg);
   327 
   328     if (msg_list == NULL)
   329         return new_message_ref_list(msg);
   330 
   331     if (msg_list->msg_ref == NULL) {
   332         msg_list->msg_ref = msg;
   333         return msg_list;
   334     }
   335     else if (msg_list->next == NULL) {
   336         msg_list->next = new_message_ref_list(msg);
   337         assert(msg_list->next);
   338         return msg_list->next;
   339     }
   340     else {
   341         return message_ref_list_add(msg_list->next, msg);
   342     }
   343 }