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--
...
vb@28
     1
#include "pEp_internal.h"
vb@23
     2
vb@23
     3
#include <memory.h>
vb@23
     4
#include <assert.h>
vb@23
     5
vb@28
     6
PEP_STATUS init_transport_system(PEP_SESSION session)
vb@23
     7
{
vb@30
     8
    assert(session);
vb@30
     9
vb@28
    10
    pEpSession *_session = (pEpSession *) session;
vb@28
    11
    PEP_transport_t* transports = _session->transports;
vb@28
    12
vb@23
    13
    assert(PEP_trans__count == 1);
vb@23
    14
    memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
vb@23
    15
vb@23
    16
    transports[0].id = PEP_trans_auto;
vb@23
    17
vb@23
    18
    return PEP_STATUS_OK;
vb@23
    19
}
vb@28
    20
vb@28
    21
void release_transport_system(PEP_SESSION session)
vb@28
    22
{
vb@30
    23
    assert(session);
vb@28
    24
    // nothing yet
vb@28
    25
}
vb@29
    26
vb@29
    27
pEp_identity *identity_dup(const pEp_identity *src)
vb@29
    28
{
vb@30
    29
    assert(src);
vb@30
    30
vb@29
    31
    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
vb@29
    32
    assert(dup);
vb@29
    33
    if (dup == NULL)
vb@29
    34
        return NULL;
vb@29
    35
    
vb@29
    36
    dup->address_size = strlen(dup->address);
vb@29
    37
    dup->fpr_size = strlen(dup->fpr);
vb@29
    38
    dup->user_id_size = strlen(dup->user_id);
vb@29
    39
    dup->username_size = strlen(dup->username);
vb@29
    40
    dup->comm_type = src->comm_type;
vb@29
    41
    dup->lang[0] = src->lang[0];
vb@29
    42
    dup->lang[1] = src->lang[1];
vb@29
    43
    dup->lang[2] = 0;
vb@29
    44
    dup->me = src->me;
vb@29
    45
}
vb@29
    46
vb@29
    47
identity_list *new_identity_list(const pEp_identity *ident)
vb@29
    48
{
vb@29
    49
    identity_list *id_list = calloc(1, sizeof(identity_list));
vb@29
    50
    assert(id_list);
vb@29
    51
    if (id_list == NULL)
vb@29
    52
        return NULL;
vb@29
    53
vb@29
    54
    if (ident) {
vb@29
    55
        id_list->ident = identity_dup(ident);
vb@29
    56
        assert(id_list->ident);
vb@29
    57
        if (id_list->ident == NULL) {
vb@29
    58
            free(id_list);
vb@29
    59
            return NULL;
vb@29
    60
        }
vb@29
    61
    }
vb@29
    62
vb@29
    63
    return id_list;
vb@29
    64
}
vb@29
    65
vb@29
    66
void free_identity_list(identity_list *id_list)
vb@29
    67
{
vb@29
    68
    if (id_list) {
vb@29
    69
        free_identity_list(id_list->next);
vb@29
    70
        free_identity(id_list->ident);
vb@29
    71
        free(id_list);
vb@29
    72
    }
vb@29
    73
}
vb@29
    74
vb@29
    75
identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident)
vb@29
    76
{
vb@29
    77
    assert(id_list);
vb@29
    78
    assert(ident);
vb@29
    79
vb@29
    80
    if (id_list->ident == NULL) {
vb@29
    81
        id_list->ident = identity_dup(ident);
vb@29
    82
        assert(id_list->ident);
vb@29
    83
        if (id_list->ident == NULL)
vb@29
    84
            return NULL;
vb@29
    85
        else
vb@29
    86
            return id_list;
vb@29
    87
    }
vb@29
    88
    else if (id_list->next == NULL) {
vb@29
    89
        id_list->next = new_identity_list(ident);
vb@29
    90
        assert(id_list->next);
vb@29
    91
        return id_list->next;
vb@29
    92
    }
vb@29
    93
    else {
vb@29
    94
        return identity_list_add(id_list->next, ident);
vb@29
    95
    }
vb@29
    96
}
vb@29
    97
vb@29
    98
message *new_message(
vb@29
    99
        msg_direction dir,
vb@29
   100
        const pEp_identity *from,
vb@29
   101
        const pEp_identity *to,
vb@29
   102
        const char *shortmsg
vb@29
   103
    )
vb@29
   104
{
vb@29
   105
    message *msg = calloc(1, sizeof(message));
vb@29
   106
    assert(msg);
vb@29
   107
    if (msg == NULL)
vb@29
   108
        return NULL;
vb@29
   109
vb@29
   110
    msg->shortmsg = strdup(shortmsg);
vb@29
   111
    assert(msg->shortmsg);
vb@29
   112
    if (msg->shortmsg == NULL) {
vb@29
   113
        free(msg);
vb@29
   114
        return NULL;
vb@29
   115
    }
vb@29
   116
    msg->shortmsg_size = strlen(msg->shortmsg);
vb@29
   117
vb@29
   118
    msg->dir = dir;
vb@29
   119
vb@29
   120
    msg->from = identity_dup(from);
vb@29
   121
    assert(msg->from);
vb@29
   122
    if (msg->from == NULL) {
vb@29
   123
        free_message(msg);
vb@29
   124
        return NULL;
vb@29
   125
    }
vb@29
   126
vb@29
   127
    if (dir == dir_incoming) {
vb@29
   128
        msg->recv_by = identity_dup(to);
vb@29
   129
        assert(msg->recv_by);
vb@29
   130
        if (msg->recv_by == NULL) {
vb@29
   131
            free_message(msg);
vb@29
   132
            return NULL;
vb@29
   133
        }
vb@29
   134
    }
vb@29
   135
vb@29
   136
    msg->to = new_identity_list(to);
vb@29
   137
    assert(msg->to);
vb@29
   138
    if (msg->to == NULL) {
vb@29
   139
        free_message(msg);
vb@29
   140
        return NULL;
vb@29
   141
    }
vb@29
   142
vb@29
   143
    return msg;
vb@29
   144
}
vb@29
   145
vb@29
   146
void free_message(message *msg)
vb@29
   147
{
vb@29
   148
    free(msg->id);
vb@29
   149
    free(msg->shortmsg);
vb@29
   150
    free(msg->longmsg);
vb@29
   151
    free(msg->longmsg_formatted);
vb@29
   152
    free(msg->rawmsg);
vb@29
   153
    free_identity_list(msg->to);
vb@29
   154
    free_identity_list(msg->cc);
vb@29
   155
    free_identity_list(msg->bcc);
vb@29
   156
    free(msg->refering_id);
vb@29
   157
    free_message_ref_list(msg->refered_by);
vb@29
   158
    free(msg);
vb@29
   159
}
vb@29
   160
vb@29
   161
message_ref_list *new_message_ref_list(message *msg)
vb@29
   162
{
vb@29
   163
    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
vb@29
   164
    assert(msg_list);
vb@29
   165
    if (msg_list == NULL)
vb@29
   166
        return NULL;
vb@29
   167
vb@29
   168
    msg_list->msg_ref = msg;
vb@29
   169
vb@29
   170
    return msg_list;
vb@29
   171
}
vb@29
   172
vb@29
   173
void free_message_ref_list(message_ref_list *msg_list)
vb@29
   174
{
vb@29
   175
    if (msg_list) {
vb@29
   176
        free_message_ref_list(msg_list->next);
vb@29
   177
        free(msg_list);
vb@29
   178
    }
vb@29
   179
}
vb@29
   180
vb@29
   181
message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
vb@29
   182
{
vb@29
   183
    assert(msg_list);
vb@29
   184
    assert(msg);
vb@29
   185
vb@29
   186
    if (msg_list->msg_ref == NULL) {
vb@29
   187
        msg_list->msg_ref = msg;
vb@29
   188
        return msg_list;
vb@29
   189
    }
vb@29
   190
    else if (msg_list->next == NULL) {
vb@29
   191
        msg_list->next = new_message_ref_list(msg);
vb@29
   192
        assert(msg_list->next);
vb@29
   193
        return msg_list->next;
vb@29
   194
    }
vb@29
   195
    else {
vb@29
   196
        return message_ref_list_add(msg_list->next, msg);
vb@29
   197
    }
vb@29
   198
}
vb@29
   199