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