src/transport.c
author vb
Tue, 30 Dec 2014 13:04:17 +0100
changeset 39 66b5cc6cb987
parent 38 271bea5552dc
child 40 3e7aa2f67b7e
permissions -rw-r--r--
...
     1 #include "pEp_internal.h"
     2 
     3 #include <memory.h>
     4 #include <assert.h>
     5 
     6 PEP_STATUS init_transport_system(PEP_SESSION session)
     7 {
     8     assert(session);
     9 
    10     pEpSession *_session = (pEpSession *) session;
    11     PEP_transport_t* transports = _session->transports;
    12 
    13     assert(PEP_trans__count == 1);
    14     memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
    15 
    16     transports[0].id = PEP_trans_auto;
    17 
    18     return PEP_STATUS_OK;
    19 }
    20 
    21 void release_transport_system(PEP_SESSION session)
    22 {
    23     assert(session);
    24     // nothing yet
    25 }
    26 
    27 identity_list *new_identity_list(pEp_identity *ident)
    28 {
    29     identity_list *id_list = calloc(1, sizeof(identity_list));
    30     assert(id_list);
    31     if (id_list == NULL)
    32         return NULL;
    33 
    34     id_list->ident = ident;
    35 
    36     return id_list;
    37 }
    38 
    39 identity_list *identity_list_dup(const identity_list *src)
    40 {
    41     assert(src);
    42 
    43     identity_list *id_list = new_identity_list(src->ident);
    44     assert(id_list);
    45     if (id_list == NULL)
    46         return NULL;
    47 
    48     if (src->next) {
    49         id_list->next = identity_list_dup(src->next);
    50         if (id_list->next == NULL) {
    51             free_identity_list(id_list);
    52             return NULL;
    53         }
    54     }
    55 
    56     return id_list;
    57 }
    58 
    59 void free_identity_list(identity_list *id_list)
    60 {
    61     if (id_list) {
    62         free_identity_list(id_list->next);
    63         free_identity(id_list->ident);
    64         free(id_list);
    65     }
    66 }
    67 
    68 identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
    69 {
    70     assert(ident);
    71 
    72     if (id_list == NULL)
    73         return new_identity_list(ident);
    74 
    75     if (id_list->ident == NULL) {
    76         id_list->ident = ident;
    77         return id_list;
    78     }
    79     else if (id_list->next == NULL) {
    80         id_list->next = new_identity_list(ident);
    81         return id_list->next;
    82     }
    83     else {
    84         return identity_list_add(id_list->next, ident);
    85     }
    86 }
    87 
    88 bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
    89         const char *file_name)
    90 {
    91     bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
    92     if (bloblist == NULL)
    93         return NULL;
    94     bloblist->data_ref = blob;
    95     bloblist->size = size;
    96     if (mime_type) {
    97         bloblist->mime_type = strdup(mime_type);
    98         if (bloblist->mime_type == NULL) {
    99             free(bloblist);
   100             return NULL;
   101         }
   102     }
   103     if (file_name) {
   104         bloblist->file_name = strdup(file_name);
   105         if (bloblist->file_name == NULL) {
   106             free(bloblist->mime_type);
   107             free(bloblist);
   108             return NULL;
   109         }
   110     }
   111     return bloblist;
   112 }
   113 
   114 bloblist_t *bloblist_dup(const bloblist_t *src)
   115 {
   116     assert(src);
   117 
   118     if (src) {
   119         bloblist_t * dst = new_bloblist(src->data_ref, src->size,
   120                 src->mime_type, src->file_name);
   121         if (dst == NULL)
   122             return NULL;
   123         dst->next = bloblist_dup(src->next);
   124         return dst;
   125     }
   126     else
   127         return NULL;
   128 }
   129 
   130 void free_bloblist(bloblist_t *bloblist)
   131 {
   132     if (bloblist) {
   133         if (bloblist->next)
   134             free_bloblist(bloblist->next);
   135         if (bloblist->mime_type)
   136             free(bloblist->mime_type);
   137         if (bloblist->file_name)
   138             free(bloblist->file_name);
   139         free(bloblist);
   140     }
   141 }
   142 
   143 bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
   144         const char *mime_type, const char *file_name)
   145 {
   146     assert(blob);
   147 
   148     if (bloblist == NULL)
   149         return new_bloblist(blob, size, mime_type, file_name);
   150 
   151     if (bloblist->data_ref == NULL) {
   152         bloblist->data_ref = blob;
   153         bloblist->size = size;
   154         if (mime_type) {
   155             bloblist->mime_type = strdup(mime_type);
   156             if (bloblist->mime_type == NULL) {
   157                 free(bloblist);
   158                 return NULL;
   159             }
   160         }
   161         if (file_name) {
   162             bloblist->file_name = strdup(file_name);
   163             if (bloblist->file_name == NULL) {
   164                 free(bloblist->mime_type);
   165                 free(bloblist);
   166                 return NULL;
   167             }
   168         }
   169         return bloblist;
   170     }
   171 
   172     if (bloblist->next == NULL) {
   173         bloblist->next = new_bloblist(blob, size, mime_type, file_name);
   174         return bloblist->next;
   175     }
   176 
   177     return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
   178 }
   179 
   180 message *new_message(
   181         PEP_msg_direction dir,
   182         pEp_identity *from,
   183         identity_list *to,
   184         const char *shortmsg
   185     )
   186 {
   187     message *msg = calloc(1, sizeof(message));
   188     assert(msg);
   189     if (msg == NULL)
   190         return NULL;
   191 
   192     if (shortmsg) {
   193         msg->shortmsg = strdup(shortmsg);
   194         assert(msg->shortmsg);
   195         if (msg->shortmsg == NULL) {
   196             free(msg);
   197             return NULL;
   198         }
   199     }
   200 
   201     msg->dir = dir;
   202     msg->from = from;
   203     msg->to = to;
   204 
   205     return msg;
   206 }
   207 
   208 void free_message(message *msg)
   209 {
   210     free(msg->id);
   211     free(msg->shortmsg);
   212     free(msg->longmsg);
   213     free(msg->longmsg_formatted);
   214     free_bloblist(msg->attachments);
   215     free_identity_list(msg->to);
   216     free_identity_list(msg->cc);
   217     free_identity_list(msg->bcc);
   218     free(msg->refering_id);
   219     free_message_ref_list(msg->refered_by);
   220     free(msg);
   221 }
   222 
   223 message_ref_list *new_message_ref_list(message *msg)
   224 {
   225     message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   226     assert(msg_list);
   227     if (msg_list == NULL)
   228         return NULL;
   229 
   230     msg_list->msg_ref = msg;
   231 
   232     return msg_list;
   233 }
   234 
   235 void free_message_ref_list(message_ref_list *msg_list)
   236 {
   237     if (msg_list) {
   238         free_message_ref_list(msg_list->next);
   239         free(msg_list);
   240     }
   241 }
   242 
   243 message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   244 {
   245     assert(msg);
   246 
   247     if (msg_list == NULL)
   248         return new_message_ref_list(msg);
   249 
   250     if (msg_list->msg_ref == NULL) {
   251         msg_list->msg_ref = msg;
   252         return msg_list;
   253     }
   254     else if (msg_list->next == NULL) {
   255         msg_list->next = new_message_ref_list(msg);
   256         assert(msg_list->next);
   257         return msg_list->next;
   258     }
   259     else {
   260         return message_ref_list_add(msg_list->next, msg);
   261     }
   262 }
   263