src/transport.c
changeset 29 31881fe94d3c
parent 28 7fc9fc3e680c
child 30 49f7cbcb91df
     1.1 --- a/src/transport.c	Mon Sep 01 20:56:05 2014 +0200
     1.2 +++ b/src/transport.c	Tue Sep 09 13:00:10 2014 +0200
     1.3 @@ -1,6 +1,5 @@
     1.4  #include "pEp_internal.h"
     1.5  
     1.6 -#include <stdlib.h>
     1.7  #include <memory.h>
     1.8  #include <assert.h>
     1.9  
    1.10 @@ -21,3 +20,175 @@
    1.11  {
    1.12      // nothing yet
    1.13  }
    1.14 +
    1.15 +pEp_identity *identity_dup(const pEp_identity *src)
    1.16 +{
    1.17 +    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
    1.18 +    assert(dup);
    1.19 +    if (dup == NULL)
    1.20 +        return NULL;
    1.21 +    
    1.22 +    dup->address_size = strlen(dup->address);
    1.23 +    dup->fpr_size = strlen(dup->fpr);
    1.24 +    dup->user_id_size = strlen(dup->user_id);
    1.25 +    dup->username_size = strlen(dup->username);
    1.26 +    dup->comm_type = src->comm_type;
    1.27 +    dup->lang[0] = src->lang[0];
    1.28 +    dup->lang[1] = src->lang[1];
    1.29 +    dup->lang[2] = 0;
    1.30 +    dup->me = src->me;
    1.31 +}
    1.32 +
    1.33 +identity_list *new_identity_list(const pEp_identity *ident)
    1.34 +{
    1.35 +    identity_list *id_list = calloc(1, sizeof(identity_list));
    1.36 +    assert(id_list);
    1.37 +    if (id_list == NULL)
    1.38 +        return NULL;
    1.39 +
    1.40 +    if (ident) {
    1.41 +        id_list->ident = identity_dup(ident);
    1.42 +        assert(id_list->ident);
    1.43 +        if (id_list->ident == NULL) {
    1.44 +            free(id_list);
    1.45 +            return NULL;
    1.46 +        }
    1.47 +    }
    1.48 +
    1.49 +    return id_list;
    1.50 +}
    1.51 +
    1.52 +void free_identity_list(identity_list *id_list)
    1.53 +{
    1.54 +    if (id_list) {
    1.55 +        free_identity_list(id_list->next);
    1.56 +        free_identity(id_list->ident);
    1.57 +        free(id_list);
    1.58 +    }
    1.59 +}
    1.60 +
    1.61 +identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident)
    1.62 +{
    1.63 +    assert(id_list);
    1.64 +    assert(ident);
    1.65 +
    1.66 +    if (id_list->ident == NULL) {
    1.67 +        id_list->ident = identity_dup(ident);
    1.68 +        assert(id_list->ident);
    1.69 +        if (id_list->ident == NULL)
    1.70 +            return NULL;
    1.71 +        else
    1.72 +            return id_list;
    1.73 +    }
    1.74 +    else if (id_list->next == NULL) {
    1.75 +        id_list->next = new_identity_list(ident);
    1.76 +        assert(id_list->next);
    1.77 +        return id_list->next;
    1.78 +    }
    1.79 +    else {
    1.80 +        return identity_list_add(id_list->next, ident);
    1.81 +    }
    1.82 +}
    1.83 +
    1.84 +message *new_message(
    1.85 +        msg_direction dir,
    1.86 +        const pEp_identity *from,
    1.87 +        const pEp_identity *to,
    1.88 +        const char *shortmsg
    1.89 +    )
    1.90 +{
    1.91 +    message *msg = calloc(1, sizeof(message));
    1.92 +    assert(msg);
    1.93 +    if (msg == NULL)
    1.94 +        return NULL;
    1.95 +
    1.96 +    msg->shortmsg = strdup(shortmsg);
    1.97 +    assert(msg->shortmsg);
    1.98 +    if (msg->shortmsg == NULL) {
    1.99 +        free(msg);
   1.100 +        return NULL;
   1.101 +    }
   1.102 +    msg->shortmsg_size = strlen(msg->shortmsg);
   1.103 +
   1.104 +    msg->dir = dir;
   1.105 +
   1.106 +    msg->from = identity_dup(from);
   1.107 +    assert(msg->from);
   1.108 +    if (msg->from == NULL) {
   1.109 +        free_message(msg);
   1.110 +        return NULL;
   1.111 +    }
   1.112 +
   1.113 +    if (dir == dir_incoming) {
   1.114 +        msg->recv_by = identity_dup(to);
   1.115 +        assert(msg->recv_by);
   1.116 +        if (msg->recv_by == NULL) {
   1.117 +            free_message(msg);
   1.118 +            return NULL;
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    msg->to = new_identity_list(to);
   1.123 +    assert(msg->to);
   1.124 +    if (msg->to == NULL) {
   1.125 +        free_message(msg);
   1.126 +        return NULL;
   1.127 +    }
   1.128 +
   1.129 +    return msg;
   1.130 +}
   1.131 +
   1.132 +void free_message(message *msg)
   1.133 +{
   1.134 +    free(msg->id);
   1.135 +    free(msg->shortmsg);
   1.136 +    free(msg->longmsg);
   1.137 +    free(msg->longmsg_formatted);
   1.138 +    free(msg->rawmsg);
   1.139 +    free_identity_list(msg->to);
   1.140 +    free_identity_list(msg->cc);
   1.141 +    free_identity_list(msg->bcc);
   1.142 +    free(msg->refering_id);
   1.143 +    free_message_ref_list(msg->refered_by);
   1.144 +    free(msg);
   1.145 +}
   1.146 +
   1.147 +message_ref_list *new_message_ref_list(message *msg)
   1.148 +{
   1.149 +    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   1.150 +    assert(msg_list);
   1.151 +    if (msg_list == NULL)
   1.152 +        return NULL;
   1.153 +
   1.154 +    msg_list->msg_ref = msg;
   1.155 +
   1.156 +    return msg_list;
   1.157 +}
   1.158 +
   1.159 +void free_message_ref_list(message_ref_list *msg_list)
   1.160 +{
   1.161 +    if (msg_list) {
   1.162 +        free_message_ref_list(msg_list->next);
   1.163 +        free(msg_list);
   1.164 +    }
   1.165 +}
   1.166 +
   1.167 +message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   1.168 +{
   1.169 +    assert(msg_list);
   1.170 +    assert(msg);
   1.171 +
   1.172 +    if (msg_list->msg_ref == NULL) {
   1.173 +        msg_list->msg_ref = msg;
   1.174 +        return msg_list;
   1.175 +    }
   1.176 +    else if (msg_list->next == NULL) {
   1.177 +        msg_list->next = new_message_ref_list(msg);
   1.178 +        assert(msg_list->next);
   1.179 +        return msg_list->next;
   1.180 +    }
   1.181 +    else {
   1.182 +        return message_ref_list_add(msg_list->next, msg);
   1.183 +    }
   1.184 +}
   1.185 +