src/transport.c
author vb
Thu, 26 Feb 2015 14:30:32 +0100
changeset 76 6da29caa5dae
parent 63 f5b2641f4ae7
child 81 044ce1c7fc9c
permissions -rw-r--r--
...
vb@28
     1
#include "pEp_internal.h"
vb@46
     2
#include "trans_auto.h"
vb@23
     3
vb@23
     4
#include <memory.h>
vb@23
     5
#include <assert.h>
vb@23
     6
vb@62
     7
PEP_STATUS init_transport_system(PEP_SESSION session, bool in_first)
vb@23
     8
{
vb@62
     9
    static PEP_transport_t transports[PEP_trans__count];
vb@62
    10
    
vb@30
    11
    assert(session);
vb@62
    12
    session->transports = transports;
vb@30
    13
vb@62
    14
    if (in_first) {
vb@62
    15
        assert(PEP_trans__count == 1);
vb@62
    16
        memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
vb@28
    17
vb@62
    18
        transports[PEP_trans_auto].id = PEP_trans_auto;
vb@62
    19
        transports[PEP_trans_auto].sendto = auto_sendto;
vb@62
    20
        transports[PEP_trans_auto].readnext = auto_readnext;
vb@62
    21
    }
vb@23
    22
vb@23
    23
    return PEP_STATUS_OK;
vb@23
    24
}
vb@28
    25
vb@62
    26
void release_transport_system(PEP_SESSION session, bool out_last)
vb@28
    27
{
vb@30
    28
    assert(session);
vb@28
    29
    // nothing yet
vb@28
    30
}
vb@29
    31
vb@48
    32
DYNAMIC_API identity_list *new_identity_list(pEp_identity *ident)
vb@29
    33
{
vb@29
    34
    identity_list *id_list = calloc(1, sizeof(identity_list));
vb@29
    35
    assert(id_list);
vb@76
    36
    if (id_list == NULL)
vb@29
    37
        return NULL;
vb@29
    38
vb@39
    39
    id_list->ident = ident;
vb@29
    40
vb@29
    41
    return id_list;
vb@29
    42
}
vb@29
    43
vb@48
    44
DYNAMIC_API identity_list *identity_list_dup(const identity_list *src)
vb@37
    45
{
vb@37
    46
    assert(src);
vb@37
    47
vb@40
    48
    identity_list *id_list = new_identity_list(identity_dup(src->ident));
vb@37
    49
    assert(id_list);
vb@37
    50
    if (id_list == NULL)
vb@37
    51
        return NULL;
vb@37
    52
vb@37
    53
    if (src->next) {
vb@37
    54
        id_list->next = identity_list_dup(src->next);
vb@37
    55
        if (id_list->next == NULL) {
vb@37
    56
            free_identity_list(id_list);
vb@37
    57
            return NULL;
vb@37
    58
        }
vb@37
    59
    }
vb@37
    60
vb@37
    61
    return id_list;
vb@37
    62
}
vb@37
    63
vb@48
    64
DYNAMIC_API void free_identity_list(identity_list *id_list)
vb@29
    65
{
vb@29
    66
    if (id_list) {
vb@29
    67
        free_identity_list(id_list->next);
vb@29
    68
        free_identity(id_list->ident);
vb@29
    69
        free(id_list);
vb@29
    70
    }
vb@29
    71
}
vb@29
    72
vb@48
    73
DYNAMIC_API identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
vb@29
    74
{
vb@29
    75
    assert(ident);
vb@29
    76
vb@38
    77
    if (id_list == NULL)
vb@38
    78
        return new_identity_list(ident);
vb@38
    79
vb@29
    80
    if (id_list->ident == NULL) {
vb@39
    81
        id_list->ident = ident;
vb@39
    82
        return id_list;
vb@29
    83
    }
vb@29
    84
    else if (id_list->next == NULL) {
vb@29
    85
        id_list->next = new_identity_list(ident);
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@48
    93
DYNAMIC_API bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
vb@39
    94
        const char *file_name)
vb@38
    95
{
vb@38
    96
    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
vb@38
    97
    if (bloblist == NULL)
vb@38
    98
        return NULL;
vb@39
    99
    if (mime_type) {
vb@39
   100
        bloblist->mime_type = strdup(mime_type);
vb@39
   101
        if (bloblist->mime_type == NULL) {
vb@39
   102
            free(bloblist);
vb@39
   103
            return NULL;
vb@39
   104
        }
vb@39
   105
    }
vb@39
   106
    if (file_name) {
vb@39
   107
        bloblist->file_name = strdup(file_name);
vb@39
   108
        if (bloblist->file_name == NULL) {
vb@39
   109
            free(bloblist->mime_type);
vb@39
   110
            free(bloblist);
vb@39
   111
            return NULL;
vb@39
   112
        }
vb@39
   113
    }
vb@41
   114
    bloblist->data = blob;
vb@41
   115
    bloblist->size = size;
vb@38
   116
    return bloblist;
vb@38
   117
}
vb@38
   118
vb@48
   119
DYNAMIC_API void free_bloblist(bloblist_t *bloblist)
vb@38
   120
{
vb@39
   121
    if (bloblist) {
vb@39
   122
        if (bloblist->next)
vb@39
   123
            free_bloblist(bloblist->next);
vb@41
   124
        free(bloblist->data);
vb@41
   125
        free(bloblist->mime_type);
vb@41
   126
        free(bloblist->file_name);
vb@39
   127
        free(bloblist);
vb@39
   128
    }
vb@38
   129
}
vb@38
   130
vb@48
   131
DYNAMIC_API bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
vb@39
   132
        const char *mime_type, const char *file_name)
vb@38
   133
{
vb@38
   134
    assert(blob);
vb@38
   135
vb@38
   136
    if (bloblist == NULL)
vb@39
   137
        return new_bloblist(blob, size, mime_type, file_name);
vb@38
   138
vb@41
   139
    if (bloblist->data == NULL) {
vb@39
   140
        if (mime_type) {
vb@39
   141
            bloblist->mime_type = strdup(mime_type);
vb@39
   142
            if (bloblist->mime_type == NULL) {
vb@39
   143
                free(bloblist);
vb@39
   144
                return NULL;
vb@39
   145
            }
vb@39
   146
        }
vb@39
   147
        if (file_name) {
vb@39
   148
            bloblist->file_name = strdup(file_name);
vb@39
   149
            if (bloblist->file_name == NULL) {
vb@39
   150
                free(bloblist->mime_type);
vb@39
   151
                free(bloblist);
vb@39
   152
                return NULL;
vb@39
   153
            }
vb@39
   154
        }
vb@41
   155
        bloblist->data = blob;
vb@41
   156
        bloblist->size = size;
vb@38
   157
        return bloblist;
vb@38
   158
    }
vb@38
   159
vb@38
   160
    if (bloblist->next == NULL) {
vb@39
   161
        bloblist->next = new_bloblist(blob, size, mime_type, file_name);
vb@38
   162
        return bloblist->next;
vb@38
   163
    }
vb@38
   164
vb@39
   165
    return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
vb@38
   166
}
vb@38
   167
vb@48
   168
DYNAMIC_API message *new_message(
vb@38
   169
        PEP_msg_direction dir,
vb@39
   170
        pEp_identity *from,
vb@39
   171
        identity_list *to,
vb@29
   172
        const char *shortmsg
vb@29
   173
    )
vb@29
   174
{
vb@29
   175
    message *msg = calloc(1, sizeof(message));
vb@29
   176
    assert(msg);
vb@29
   177
    if (msg == NULL)
vb@29
   178
        return NULL;
vb@29
   179
vb@39
   180
    if (shortmsg) {
vb@37
   181
        msg->shortmsg = strdup(shortmsg);
vb@37
   182
        assert(msg->shortmsg);
vb@37
   183
        if (msg->shortmsg == NULL) {
vb@37
   184
            free(msg);
vb@37
   185
            return NULL;
vb@37
   186
        }
vb@29
   187
    }
vb@29
   188
vb@29
   189
    msg->dir = dir;
vb@39
   190
    msg->from = from;
vb@39
   191
    msg->to = to;
vb@29
   192
vb@29
   193
    return msg;
vb@29
   194
}
vb@29
   195
vb@48
   196
DYNAMIC_API void free_message(message *msg)
vb@29
   197
{
vb@63
   198
    if (msg) {
vb@63
   199
        free(msg->id);
vb@63
   200
        free(msg->shortmsg);
vb@63
   201
        free(msg->longmsg);
vb@63
   202
        free(msg->longmsg_formatted);
vb@63
   203
        free_bloblist(msg->attachments);
vb@63
   204
        free_identity(msg->from);
vb@63
   205
        free_identity_list(msg->to);
vb@63
   206
        free_identity(msg->recv_by);
vb@63
   207
        free_identity_list(msg->cc);
vb@63
   208
        free_identity_list(msg->bcc);
vb@63
   209
        free(msg->refering_id);
vb@63
   210
        free_message_ref_list(msg->refered_by);
vb@63
   211
        free(msg);
vb@63
   212
    }
vb@29
   213
}
vb@29
   214
vb@48
   215
DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
vb@29
   216
{
vb@29
   217
    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
vb@29
   218
    assert(msg_list);
vb@29
   219
    if (msg_list == NULL)
vb@29
   220
        return NULL;
vb@29
   221
vb@29
   222
    msg_list->msg_ref = msg;
vb@29
   223
vb@29
   224
    return msg_list;
vb@29
   225
}
vb@29
   226
vb@48
   227
DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
vb@29
   228
{
vb@29
   229
    if (msg_list) {
vb@29
   230
        free_message_ref_list(msg_list->next);
vb@29
   231
        free(msg_list);
vb@29
   232
    }
vb@29
   233
}
vb@29
   234
vb@48
   235
DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
vb@29
   236
{
vb@29
   237
    assert(msg);
vb@29
   238
vb@38
   239
    if (msg_list == NULL)
vb@38
   240
        return new_message_ref_list(msg);
vb@38
   241
vb@29
   242
    if (msg_list->msg_ref == NULL) {
vb@29
   243
        msg_list->msg_ref = msg;
vb@29
   244
        return msg_list;
vb@29
   245
    }
vb@29
   246
    else if (msg_list->next == NULL) {
vb@29
   247
        msg_list->next = new_message_ref_list(msg);
vb@29
   248
        assert(msg_list->next);
vb@29
   249
        return msg_list->next;
vb@29
   250
    }
vb@29
   251
    else {
vb@29
   252
        return message_ref_list_add(msg_list->next, msg);
vb@29
   253
    }
vb@29
   254
}
vb@29
   255