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