src/transport.c
author vb
Sun, 01 Mar 2015 15:32:16 +0100
changeset 81 044ce1c7fc9c
parent 76 6da29caa5dae
child 89 aef5a4bc78f3
permissions -rw-r--r--
...
     1 #include "pEp_internal.h"
     2 #include "trans_auto.h"
     3 
     4 #include <memory.h>
     5 #include <assert.h>
     6 
     7 PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first)
     8 {
     9     static PEP_transport_t transports[PEP_trans__count];
    10     
    11     assert(session);
    12     session->transports = transports;
    13 
    14     if (in_first) {
    15         assert(PEP_trans__count == 1);
    16         memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
    17 
    18         transports[PEP_trans_auto].id = PEP_trans_auto;
    19         transports[PEP_trans_auto].sendto = auto_sendto;
    20         transports[PEP_trans_auto].readnext = auto_readnext;
    21     }
    22 
    23     return PEP_STATUS_OK;
    24 }
    25 
    26 void release_transport_system(PEP_SESSION session, bool out_last)
    27 {
    28     assert(session);
    29     // nothing yet
    30 }
    31 
    32 DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
    33 {
    34     identity_list *id_list = calloc(1, sizeof(identity_list));
    35     assert(id_list);
    36     if (id_list == NULL)
    37         return NULL;
    38 
    39     id_list->ident = ident;
    40 
    41     return id_list;
    42 }
    43 
    44 DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
    45 {
    46     assert(src);
    47 
    48     identity_list *id_list = new_identity_list(identity_dup(src->ident));
    49     assert(id_list);
    50     if (id_list == NULL)
    51         return NULL;
    52 
    53     if (src->next) {
    54         id_list->next = identity_list_dup(src->next);
    55         if (id_list->next == NULL) {
    56             free_identity_list(id_list);
    57             return NULL;
    58         }
    59     }
    60 
    61     return id_list;
    62 }
    63 
    64 DYNAMIC_API void free_identity_list(identity_list *id_list)
    65 {
    66     if (id_list) {
    67         free_identity_list(id_list->next);
    68         free_identity(id_list->ident);
    69         free(id_list);
    70     }
    71 }
    72 
    73 DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
    74 {
    75     assert(ident);
    76 
    77     if (id_list == NULL)
    78         return new_identity_list(ident);
    79 
    80     if (id_list->ident == NULL) {
    81         id_list->ident = ident;
    82         return id_list;
    83     }
    84     else if (id_list->next == NULL) {
    85         id_list->next = new_identity_list(ident);
    86         return id_list->next;
    87     }
    88     else {
    89         return identity_list_add(id_list->next, ident);
    90     }
    91 }
    92 
    93 DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
    94         const char *file_name)
    95 {
    96     bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
    97     assert(bloblist);
    98     if (bloblist == NULL)
    99         return NULL;
   100 
   101     if (mime_type) {
   102         bloblist->mime_type = strdup(mime_type);
   103         if (bloblist->mime_type == NULL) {
   104             free(bloblist);
   105             return NULL;
   106         }
   107     }
   108 
   109     if (file_name) {
   110         bloblist->file_name = strdup(file_name);
   111         if (bloblist->file_name == NULL) {
   112             free(bloblist->mime_type);
   113             free(bloblist);
   114             return NULL;
   115         }
   116     }
   117 
   118     bloblist->data = blob;
   119     bloblist->size = size;
   120 
   121     return bloblist;
   122 }
   123 
   124 DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
   125 {
   126     if (bloblist) {
   127         if (bloblist->next)
   128             free_bloblist(bloblist->next);
   129         free(bloblist->data);
   130         free(bloblist->mime_type);
   131         free(bloblist->file_name);
   132         free(bloblist);
   133     }
   134 }
   135 
   136 DYNAMIC_API bloblist_t *bloblist_dup(const bloblist_t *src)
   137 {
   138     bloblist_t *bloblist = NULL;
   139 
   140     assert(src);
   141 
   142     bloblist = new_bloblist(src->data, src->size, src->mime_type, src->file_name);
   143     if (bloblist == NULL)
   144         goto enomem;
   145 
   146     if (src->next) {
   147         bloblist->next = bloblist_dup(src->next);
   148         if (bloblist->next == NULL)
   149             goto enomem;
   150     }
   151 
   152     return bloblist;
   153 
   154 enomem:
   155     free_bloblist(bloblist);
   156     return NULL;
   157 }
   158 
   159 DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
   160         const char *mime_type, const char *file_name)
   161 {
   162     assert(blob);
   163 
   164     if (bloblist == NULL)
   165         return new_bloblist(blob, size, mime_type, file_name);
   166 
   167     if (bloblist->data == NULL) {
   168         if (mime_type) {
   169             bloblist->mime_type = strdup(mime_type);
   170             if (bloblist->mime_type == NULL) {
   171                 free(bloblist);
   172                 return NULL;
   173             }
   174         }
   175         if (file_name) {
   176             bloblist->file_name = strdup(file_name);
   177             if (bloblist->file_name == NULL) {
   178                 free(bloblist->mime_type);
   179                 free(bloblist);
   180                 return NULL;
   181             }
   182         }
   183         bloblist->data = blob;
   184         bloblist->size = size;
   185         return bloblist;
   186     }
   187 
   188     if (bloblist->next == NULL) {
   189         bloblist->next = new_bloblist(blob, size, mime_type, file_name);
   190         return bloblist->next;
   191     }
   192 
   193     return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
   194 }
   195 
   196 DYNAMIC_API message *new_message(
   197         PEP_msg_direction dir,
   198         pEp_identity *from,
   199         identity_list *to,
   200         const char *shortmsg
   201     )
   202 {
   203     message *msg = calloc(1, sizeof(message));
   204     assert(msg);
   205     if (msg == NULL)
   206         return NULL;
   207 
   208     if (shortmsg) {
   209         msg->shortmsg = strdup(shortmsg);
   210         assert(msg->shortmsg);
   211         if (msg->shortmsg == NULL) {
   212             free(msg);
   213             return NULL;
   214         }
   215     }
   216 
   217     msg->dir = dir;
   218     msg->from = from;
   219     msg->to = to;
   220 
   221     return msg;
   222 }
   223 
   224 DYNAMIC_API void free_message(message *msg)
   225 {
   226     if (msg) {
   227         free(msg->id);
   228         free(msg->shortmsg);
   229         free(msg->longmsg);
   230         free(msg->longmsg_formatted);
   231         free_bloblist(msg->attachments);
   232         free_identity(msg->from);
   233         free_identity_list(msg->to);
   234         free_identity(msg->recv_by);
   235         free_identity_list(msg->cc);
   236         free_identity_list(msg->bcc);
   237         free_identity(msg->reply_to);
   238         free(msg->refering_id);
   239         free(msg);
   240     }
   241 }
   242 
   243 DYNAMIC_API message * message_dup(const message *src)
   244 {
   245     message * msg = NULL;
   246     pEp_identity * from = NULL;
   247     identity_list * to = NULL;
   248 
   249     assert(src);
   250 
   251     from = identity_dup(src->from);
   252     if (from == NULL)
   253         goto enomem;
   254 
   255     to = identity_list_dup(src->to);
   256     if (to == NULL)
   257         goto enomem;
   258 
   259     msg = new_message(src->dir, from, to, src->shortmsg);
   260     if (msg == NULL)
   261         goto enomem;
   262 
   263     if (src->id) {
   264         msg->id = strdup(src->id);
   265         assert(msg->id);
   266         if (msg->id == NULL)
   267             goto enomem;
   268     }
   269 
   270     if (src->longmsg) {
   271         msg->longmsg = strdup(src->longmsg);
   272         assert(msg->longmsg);
   273         if (msg->longmsg == NULL)
   274             goto enomem;
   275     }
   276     
   277     if (src->longmsg_formatted) {
   278         msg->longmsg_formatted = strdup(src->longmsg_formatted);
   279         assert(msg->longmsg_formatted);
   280         if (msg->longmsg_formatted == NULL)
   281             goto enomem;
   282     }
   283 
   284     if (src->attachments) {
   285         msg->attachments = bloblist_dup(src->attachments);
   286         if (msg->attachments == NULL)
   287             goto enomem;
   288     }
   289 
   290     msg->rawmsg_ref = src->rawmsg_ref;
   291     msg->rawmsg_size = src->rawmsg_size;
   292     msg->sent = src->sent;
   293     msg->recv = src->recv;
   294 
   295     if (src->recv_by) {
   296         msg->recv_by = identity_dup(src->recv_by);
   297         if (msg->recv_by == NULL)
   298             goto enomem;
   299     }
   300 
   301     if (src->cc) {
   302         msg->cc = identity_list_dup(src->cc);
   303         if (msg->cc == NULL)
   304             goto enomem;
   305     }
   306 
   307     if (src->bcc) {
   308         msg->bcc = identity_list_dup(src->bcc);
   309         if (msg->bcc == NULL)
   310             goto enomem;
   311     }
   312 
   313     if (src->reply_to) {
   314         msg->reply_to = identity_dup(src->reply_to);
   315         if (msg->reply_to == NULL)
   316             goto enomem;
   317     }
   318 
   319     if (src->refering_id) {
   320         msg->refering_id = strdup(src->refering_id);
   321         assert(msg->refering_id);
   322         if (msg->refering_id == NULL)
   323             goto enomem;
   324     }
   325 
   326     msg->refering_msg_ref = src->refering_msg_ref;
   327     
   328     if (src->refered_by) {
   329         msg->refered_by = message_ref_list_dup(src->refered_by);
   330         if (msg->refered_by == NULL)
   331             goto enomem;
   332     }
   333 
   334     msg->enc_format = src->enc_format;
   335 
   336     return msg;
   337 
   338 enomem:
   339     if (msg) {
   340         free_message(msg);
   341     }
   342     else {
   343         free_identity(from);
   344         free_identity_list(to);
   345     }
   346 
   347     return NULL;
   348 }
   349 
   350 DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
   351 {
   352     message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   353     assert(msg_list);
   354     if (msg_list == NULL)
   355         return NULL;
   356 
   357     msg_list->msg_ref = msg;
   358 
   359     return msg_list;
   360 }
   361 
   362 DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
   363 {
   364     if (msg_list) {
   365         free_message_ref_list(msg_list->next);
   366         free(msg_list);
   367     }
   368 }
   369 
   370 DYNAMIC_API message_ref_list *message_ref_list_dup(
   371         const message_ref_list *src
   372     )
   373 {
   374     message_ref_list * msg_list = NULL;
   375 
   376     assert(src);
   377 
   378     msg_list = new_message_ref_list(src->msg_ref);
   379     if (msg_list == NULL)
   380         goto enomem;
   381 
   382     if (src->next) {
   383         msg_list->next = message_ref_list_dup(src->next);
   384         if (msg_list->next == NULL)
   385             goto enomem;
   386     }
   387 
   388     return msg_list;
   389 
   390 enomem:
   391     free_message_ref_list(msg_list);
   392     return NULL;
   393 }
   394 
   395 DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   396 {
   397     assert(msg);
   398 
   399     if (msg_list == NULL)
   400         return new_message_ref_list(msg);
   401 
   402     if (msg_list->msg_ref == NULL) {
   403         msg_list->msg_ref = msg;
   404         return msg_list;
   405     }
   406     else if (msg_list->next == NULL) {
   407         msg_list->next = new_message_ref_list(msg);
   408         assert(msg_list->next);
   409         return msg_list->next;
   410     }
   411     else {
   412         return message_ref_list_add(msg_list->next, msg);
   413     }
   414 }
   415