src/transport.c
author vb
Sun, 08 Feb 2015 11:33:04 -0500
changeset 48 9ff5d2dfa75a
parent 46 7471e31bb278
child 62 ad5e484720e1
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)
     8 {
     9     assert(session);
    10 
    11     pEpSession *_session = (pEpSession *) session;
    12     PEP_transport_t* transports = _session->transports;
    13 
    14     assert(PEP_trans__count == 1);
    15     memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
    16 
    17     transports[PEP_trans_auto].id = PEP_trans_auto;
    18     transports[PEP_trans_auto].sendto = auto_sendto;
    19     transports[PEP_trans_auto].readnext = auto_readnext;
    20 
    21     return PEP_STATUS_OK;
    22 }
    23 
    24 void release_transport_system(PEP_SESSION session)
    25 {
    26     assert(session);
    27     // nothing yet
    28 }
    29 
    30 DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
    31 {
    32     identity_list *id_list = calloc(1, sizeof(identity_list));
    33     assert(id_list);
    34     if (id_list == NULL) {
    35         free(ident);
    36         return NULL;
    37     }
    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     if (bloblist == NULL)
    98         return NULL;
    99     if (mime_type) {
   100         bloblist->mime_type = strdup(mime_type);
   101         if (bloblist->mime_type == NULL) {
   102             free(bloblist);
   103             return NULL;
   104         }
   105     }
   106     if (file_name) {
   107         bloblist->file_name = strdup(file_name);
   108         if (bloblist->file_name == NULL) {
   109             free(bloblist->mime_type);
   110             free(bloblist);
   111             return NULL;
   112         }
   113     }
   114     bloblist->data = blob;
   115     bloblist->size = size;
   116     return bloblist;
   117 }
   118 
   119 DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
   120 {
   121     if (bloblist) {
   122         if (bloblist->next)
   123             free_bloblist(bloblist->next);
   124         free(bloblist->data);
   125         free(bloblist->mime_type);
   126         free(bloblist->file_name);
   127         free(bloblist);
   128     }
   129 }
   130 
   131 DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
   132         const char *mime_type, const char *file_name)
   133 {
   134     assert(blob);
   135 
   136     if (bloblist == NULL)
   137         return new_bloblist(blob, size, mime_type, file_name);
   138 
   139     if (bloblist->data == NULL) {
   140         if (mime_type) {
   141             bloblist->mime_type = strdup(mime_type);
   142             if (bloblist->mime_type == NULL) {
   143                 free(bloblist);
   144                 return NULL;
   145             }
   146         }
   147         if (file_name) {
   148             bloblist->file_name = strdup(file_name);
   149             if (bloblist->file_name == NULL) {
   150                 free(bloblist->mime_type);
   151                 free(bloblist);
   152                 return NULL;
   153             }
   154         }
   155         bloblist->data = blob;
   156         bloblist->size = size;
   157         return bloblist;
   158     }
   159 
   160     if (bloblist->next == NULL) {
   161         bloblist->next = new_bloblist(blob, size, mime_type, file_name);
   162         return bloblist->next;
   163     }
   164 
   165     return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
   166 }
   167 
   168 DYNAMIC_API message *new_message(
   169         PEP_msg_direction dir,
   170         pEp_identity *from,
   171         identity_list *to,
   172         const char *shortmsg
   173     )
   174 {
   175     message *msg = calloc(1, sizeof(message));
   176     assert(msg);
   177     if (msg == NULL)
   178         return NULL;
   179 
   180     if (shortmsg) {
   181         msg->shortmsg = strdup(shortmsg);
   182         assert(msg->shortmsg);
   183         if (msg->shortmsg == NULL) {
   184             free(msg);
   185             return NULL;
   186         }
   187     }
   188 
   189     msg->dir = dir;
   190     msg->from = from;
   191     msg->to = to;
   192 
   193     return msg;
   194 }
   195 
   196 DYNAMIC_API void free_message(message *msg)
   197 {
   198     free(msg->id);
   199     free(msg->shortmsg);
   200     free(msg->longmsg);
   201     free(msg->longmsg_formatted);
   202     free_bloblist(msg->attachments);
   203     free_identity(msg->from);
   204     free_identity_list(msg->to);
   205     free_identity(msg->recv_by);
   206     free_identity_list(msg->cc);
   207     free_identity_list(msg->bcc);
   208     free(msg->refering_id);
   209     free_message_ref_list(msg->refered_by);
   210     free(msg);
   211 }
   212 
   213 DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
   214 {
   215     message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   216     assert(msg_list);
   217     if (msg_list == NULL)
   218         return NULL;
   219 
   220     msg_list->msg_ref = msg;
   221 
   222     return msg_list;
   223 }
   224 
   225 DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
   226 {
   227     if (msg_list) {
   228         free_message_ref_list(msg_list->next);
   229         free(msg_list);
   230     }
   231 }
   232 
   233 DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   234 {
   235     assert(msg);
   236 
   237     if (msg_list == NULL)
   238         return new_message_ref_list(msg);
   239 
   240     if (msg_list->msg_ref == NULL) {
   241         msg_list->msg_ref = msg;
   242         return msg_list;
   243     }
   244     else if (msg_list->next == NULL) {
   245         msg_list->next = new_message_ref_list(msg);
   246         assert(msg_list->next);
   247         return msg_list->next;
   248     }
   249     else {
   250         return message_ref_list_add(msg_list->next, msg);
   251     }
   252 }
   253