src/transport.c
author vb
Tue, 30 Dec 2014 23:33:42 +0100
changeset 41 b30cc1842d9c
parent 40 3e7aa2f67b7e
child 46 7471e31bb278
permissions -rw-r--r--
blob ownership to bloblist
     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     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     bloblist->data = blob;
   112     bloblist->size = size;
   113     return bloblist;
   114 }
   115 
   116 void free_bloblist(bloblist_t *bloblist)
   117 {
   118     if (bloblist) {
   119         if (bloblist->next)
   120             free_bloblist(bloblist->next);
   121         free(bloblist->data);
   122         free(bloblist->mime_type);
   123         free(bloblist->file_name);
   124         free(bloblist);
   125     }
   126 }
   127 
   128 bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
   129         const char *mime_type, const char *file_name)
   130 {
   131     assert(blob);
   132 
   133     if (bloblist == NULL)
   134         return new_bloblist(blob, size, mime_type, file_name);
   135 
   136     if (bloblist->data == NULL) {
   137         if (mime_type) {
   138             bloblist->mime_type = strdup(mime_type);
   139             if (bloblist->mime_type == NULL) {
   140                 free(bloblist);
   141                 return NULL;
   142             }
   143         }
   144         if (file_name) {
   145             bloblist->file_name = strdup(file_name);
   146             if (bloblist->file_name == NULL) {
   147                 free(bloblist->mime_type);
   148                 free(bloblist);
   149                 return NULL;
   150             }
   151         }
   152         bloblist->data = blob;
   153         bloblist->size = size;
   154         return bloblist;
   155     }
   156 
   157     if (bloblist->next == NULL) {
   158         bloblist->next = new_bloblist(blob, size, mime_type, file_name);
   159         return bloblist->next;
   160     }
   161 
   162     return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
   163 }
   164 
   165 message *new_message(
   166         PEP_msg_direction dir,
   167         pEp_identity *from,
   168         identity_list *to,
   169         const char *shortmsg
   170     )
   171 {
   172     message *msg = calloc(1, sizeof(message));
   173     assert(msg);
   174     if (msg == NULL)
   175         return NULL;
   176 
   177     if (shortmsg) {
   178         msg->shortmsg = strdup(shortmsg);
   179         assert(msg->shortmsg);
   180         if (msg->shortmsg == NULL) {
   181             free(msg);
   182             return NULL;
   183         }
   184     }
   185 
   186     msg->dir = dir;
   187     msg->from = from;
   188     msg->to = to;
   189 
   190     return msg;
   191 }
   192 
   193 void free_message(message *msg)
   194 {
   195     free(msg->id);
   196     free(msg->shortmsg);
   197     free(msg->longmsg);
   198     free(msg->longmsg_formatted);
   199     free_bloblist(msg->attachments);
   200     free_identity(msg->from);
   201     free_identity_list(msg->to);
   202     free_identity(msg->recv_by);
   203     free_identity_list(msg->cc);
   204     free_identity_list(msg->bcc);
   205     free(msg->refering_id);
   206     free_message_ref_list(msg->refered_by);
   207     free(msg);
   208 }
   209 
   210 message_ref_list *new_message_ref_list(message *msg)
   211 {
   212     message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   213     assert(msg_list);
   214     if (msg_list == NULL)
   215         return NULL;
   216 
   217     msg_list->msg_ref = msg;
   218 
   219     return msg_list;
   220 }
   221 
   222 void free_message_ref_list(message_ref_list *msg_list)
   223 {
   224     if (msg_list) {
   225         free_message_ref_list(msg_list->next);
   226         free(msg_list);
   227     }
   228 }
   229 
   230 message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   231 {
   232     assert(msg);
   233 
   234     if (msg_list == NULL)
   235         return new_message_ref_list(msg);
   236 
   237     if (msg_list->msg_ref == NULL) {
   238         msg_list->msg_ref = msg;
   239         return msg_list;
   240     }
   241     else if (msg_list->next == NULL) {
   242         msg_list->next = new_message_ref_list(msg);
   243         assert(msg_list->next);
   244         return msg_list->next;
   245     }
   246     else {
   247         return message_ref_list_add(msg_list->next, msg);
   248     }
   249 }
   250