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