src/transport.c
author vb
Sun, 28 Dec 2014 17:31:52 +0100
changeset 36 7857c4a83b5f
parent 30 49f7cbcb91df
child 37 7928e0e6eee9
permissions -rw-r--r--
warnings with clang
     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 pEp_identity *identity_dup(const pEp_identity *src)
    28 {
    29     assert(src);
    30 
    31     pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
    32     assert(dup);
    33     if (dup == NULL)
    34         return NULL;
    35     
    36     dup->address_size = strlen(dup->address);
    37     dup->fpr_size = strlen(dup->fpr);
    38     dup->user_id_size = strlen(dup->user_id);
    39     dup->username_size = strlen(dup->username);
    40     dup->comm_type = src->comm_type;
    41     dup->lang[0] = src->lang[0];
    42     dup->lang[1] = src->lang[1];
    43     dup->lang[2] = 0;
    44     dup->me = src->me;
    45 
    46     return dup;
    47 }
    48 
    49 identity_list *new_identity_list(const pEp_identity *ident)
    50 {
    51     identity_list *id_list = calloc(1, sizeof(identity_list));
    52     assert(id_list);
    53     if (id_list == NULL)
    54         return NULL;
    55 
    56     if (ident) {
    57         id_list->ident = identity_dup(ident);
    58         assert(id_list->ident);
    59         if (id_list->ident == NULL) {
    60             free(id_list);
    61             return NULL;
    62         }
    63     }
    64 
    65     return id_list;
    66 }
    67 
    68 void free_identity_list(identity_list *id_list)
    69 {
    70     if (id_list) {
    71         free_identity_list(id_list->next);
    72         free_identity(id_list->ident);
    73         free(id_list);
    74     }
    75 }
    76 
    77 identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident)
    78 {
    79     assert(id_list);
    80     assert(ident);
    81 
    82     if (id_list->ident == NULL) {
    83         id_list->ident = identity_dup(ident);
    84         assert(id_list->ident);
    85         if (id_list->ident == NULL)
    86             return NULL;
    87         else
    88             return id_list;
    89     }
    90     else if (id_list->next == NULL) {
    91         id_list->next = new_identity_list(ident);
    92         assert(id_list->next);
    93         return id_list->next;
    94     }
    95     else {
    96         return identity_list_add(id_list->next, ident);
    97     }
    98 }
    99 
   100 message *new_message(
   101         msg_direction dir,
   102         const pEp_identity *from,
   103         const pEp_identity *to,
   104         const char *shortmsg
   105     )
   106 {
   107     message *msg = calloc(1, sizeof(message));
   108     assert(msg);
   109     if (msg == NULL)
   110         return NULL;
   111 
   112     msg->shortmsg = strdup(shortmsg);
   113     assert(msg->shortmsg);
   114     if (msg->shortmsg == NULL) {
   115         free(msg);
   116         return NULL;
   117     }
   118     msg->shortmsg_size = strlen(msg->shortmsg);
   119 
   120     msg->dir = dir;
   121 
   122     msg->from = identity_dup(from);
   123     assert(msg->from);
   124     if (msg->from == NULL) {
   125         free_message(msg);
   126         return NULL;
   127     }
   128 
   129     if (dir == dir_incoming) {
   130         msg->recv_by = identity_dup(to);
   131         assert(msg->recv_by);
   132         if (msg->recv_by == NULL) {
   133             free_message(msg);
   134             return NULL;
   135         }
   136     }
   137 
   138     msg->to = new_identity_list(to);
   139     assert(msg->to);
   140     if (msg->to == NULL) {
   141         free_message(msg);
   142         return NULL;
   143     }
   144 
   145     return msg;
   146 }
   147 
   148 void free_message(message *msg)
   149 {
   150     free(msg->id);
   151     free(msg->shortmsg);
   152     free(msg->longmsg);
   153     free(msg->longmsg_formatted);
   154     free(msg->rawmsg);
   155     free_identity_list(msg->to);
   156     free_identity_list(msg->cc);
   157     free_identity_list(msg->bcc);
   158     free(msg->refering_id);
   159     free_message_ref_list(msg->refered_by);
   160     free(msg);
   161 }
   162 
   163 message_ref_list *new_message_ref_list(message *msg)
   164 {
   165     message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   166     assert(msg_list);
   167     if (msg_list == NULL)
   168         return NULL;
   169 
   170     msg_list->msg_ref = msg;
   171 
   172     return msg_list;
   173 }
   174 
   175 void free_message_ref_list(message_ref_list *msg_list)
   176 {
   177     if (msg_list) {
   178         free_message_ref_list(msg_list->next);
   179         free(msg_list);
   180     }
   181 }
   182 
   183 message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   184 {
   185     assert(msg_list);
   186     assert(msg);
   187 
   188     if (msg_list->msg_ref == NULL) {
   189         msg_list->msg_ref = msg;
   190         return msg_list;
   191     }
   192     else if (msg_list->next == NULL) {
   193         msg_list->next = new_message_ref_list(msg);
   194         assert(msg_list->next);
   195         return msg_list->next;
   196     }
   197     else {
   198         return message_ref_list_add(msg_list->next, msg);
   199     }
   200 }
   201