src/transport.c
author vb
Thu, 26 Feb 2015 14:30:32 +0100
changeset 76 6da29caa5dae
parent 63 f5b2641f4ae7
child 81 044ce1c7fc9c
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     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     if (msg) {
   199         free(msg->id);
   200         free(msg->shortmsg);
   201         free(msg->longmsg);
   202         free(msg->longmsg_formatted);
   203         free_bloblist(msg->attachments);
   204         free_identity(msg->from);
   205         free_identity_list(msg->to);
   206         free_identity(msg->recv_by);
   207         free_identity_list(msg->cc);
   208         free_identity_list(msg->bcc);
   209         free(msg->refering_id);
   210         free_message_ref_list(msg->refered_by);
   211         free(msg);
   212     }
   213 }
   214 
   215 DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
   216 {
   217     message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   218     assert(msg_list);
   219     if (msg_list == NULL)
   220         return NULL;
   221 
   222     msg_list->msg_ref = msg;
   223 
   224     return msg_list;
   225 }
   226 
   227 DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
   228 {
   229     if (msg_list) {
   230         free_message_ref_list(msg_list->next);
   231         free(msg_list);
   232     }
   233 }
   234 
   235 DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   236 {
   237     assert(msg);
   238 
   239     if (msg_list == NULL)
   240         return new_message_ref_list(msg);
   241 
   242     if (msg_list->msg_ref == NULL) {
   243         msg_list->msg_ref = msg;
   244         return msg_list;
   245     }
   246     else if (msg_list->next == NULL) {
   247         msg_list->next = new_message_ref_list(msg);
   248         assert(msg_list->next);
   249         return msg_list->next;
   250     }
   251     else {
   252         return message_ref_list_add(msg_list->next, msg);
   253     }
   254 }
   255