src/transport.c
author vb
Tue, 30 Dec 2014 23:33:42 +0100
changeset 41 b30cc1842d9c
parent 40 3e7aa2f67b7e
child 46 7471e31bb278
permissions -rw-r--r--
blob ownership to bloblist
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@39
    27
identity_list *new_identity_list(pEp_identity *ident)
vb@29
    28
{
vb@29
    29
    identity_list *id_list = calloc(1, sizeof(identity_list));
vb@29
    30
    assert(id_list);
vb@40
    31
    if (id_list == NULL) {
vb@40
    32
        free(ident);
vb@29
    33
        return NULL;
vb@40
    34
    }
vb@29
    35
vb@39
    36
    id_list->ident = ident;
vb@29
    37
vb@29
    38
    return id_list;
vb@29
    39
}
vb@29
    40
vb@37
    41
identity_list *identity_list_dup(const identity_list *src)
vb@37
    42
{
vb@37
    43
    assert(src);
vb@37
    44
vb@40
    45
    identity_list *id_list = new_identity_list(identity_dup(src->ident));
vb@37
    46
    assert(id_list);
vb@37
    47
    if (id_list == NULL)
vb@37
    48
        return NULL;
vb@37
    49
vb@37
    50
    if (src->next) {
vb@37
    51
        id_list->next = identity_list_dup(src->next);
vb@37
    52
        if (id_list->next == NULL) {
vb@37
    53
            free_identity_list(id_list);
vb@37
    54
            return NULL;
vb@37
    55
        }
vb@37
    56
    }
vb@37
    57
vb@37
    58
    return id_list;
vb@37
    59
}
vb@37
    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@39
    70
identity_list *identity_list_add(identity_list *id_list, pEp_identity *ident)
vb@29
    71
{
vb@29
    72
    assert(ident);
vb@29
    73
vb@38
    74
    if (id_list == NULL)
vb@38
    75
        return new_identity_list(ident);
vb@38
    76
vb@29
    77
    if (id_list->ident == NULL) {
vb@39
    78
        id_list->ident = ident;
vb@39
    79
        return id_list;
vb@29
    80
    }
vb@29
    81
    else if (id_list->next == NULL) {
vb@29
    82
        id_list->next = new_identity_list(ident);
vb@29
    83
        return id_list->next;
vb@29
    84
    }
vb@29
    85
    else {
vb@29
    86
        return identity_list_add(id_list->next, ident);
vb@29
    87
    }
vb@29
    88
}
vb@29
    89
vb@39
    90
bloblist_t *new_bloblist(char *blob, size_t size, const char *mime_type,
vb@39
    91
        const char *file_name)
vb@38
    92
{
vb@38
    93
    bloblist_t * bloblist = calloc(1, sizeof(bloblist_t));
vb@38
    94
    if (bloblist == NULL)
vb@38
    95
        return NULL;
vb@39
    96
    if (mime_type) {
vb@39
    97
        bloblist->mime_type = strdup(mime_type);
vb@39
    98
        if (bloblist->mime_type == NULL) {
vb@39
    99
            free(bloblist);
vb@39
   100
            return NULL;
vb@39
   101
        }
vb@39
   102
    }
vb@39
   103
    if (file_name) {
vb@39
   104
        bloblist->file_name = strdup(file_name);
vb@39
   105
        if (bloblist->file_name == NULL) {
vb@39
   106
            free(bloblist->mime_type);
vb@39
   107
            free(bloblist);
vb@39
   108
            return NULL;
vb@39
   109
        }
vb@39
   110
    }
vb@41
   111
    bloblist->data = blob;
vb@41
   112
    bloblist->size = size;
vb@38
   113
    return bloblist;
vb@38
   114
}
vb@38
   115
vb@38
   116
void free_bloblist(bloblist_t *bloblist)
vb@38
   117
{
vb@39
   118
    if (bloblist) {
vb@39
   119
        if (bloblist->next)
vb@39
   120
            free_bloblist(bloblist->next);
vb@41
   121
        free(bloblist->data);
vb@41
   122
        free(bloblist->mime_type);
vb@41
   123
        free(bloblist->file_name);
vb@39
   124
        free(bloblist);
vb@39
   125
    }
vb@38
   126
}
vb@38
   127
vb@39
   128
bloblist_t *bloblist_add(bloblist_t *bloblist, char *blob, size_t size,
vb@39
   129
        const char *mime_type, const char *file_name)
vb@38
   130
{
vb@38
   131
    assert(blob);
vb@38
   132
vb@38
   133
    if (bloblist == NULL)
vb@39
   134
        return new_bloblist(blob, size, mime_type, file_name);
vb@38
   135
vb@41
   136
    if (bloblist->data == NULL) {
vb@39
   137
        if (mime_type) {
vb@39
   138
            bloblist->mime_type = strdup(mime_type);
vb@39
   139
            if (bloblist->mime_type == NULL) {
vb@39
   140
                free(bloblist);
vb@39
   141
                return NULL;
vb@39
   142
            }
vb@39
   143
        }
vb@39
   144
        if (file_name) {
vb@39
   145
            bloblist->file_name = strdup(file_name);
vb@39
   146
            if (bloblist->file_name == NULL) {
vb@39
   147
                free(bloblist->mime_type);
vb@39
   148
                free(bloblist);
vb@39
   149
                return NULL;
vb@39
   150
            }
vb@39
   151
        }
vb@41
   152
        bloblist->data = blob;
vb@41
   153
        bloblist->size = size;
vb@38
   154
        return bloblist;
vb@38
   155
    }
vb@38
   156
vb@38
   157
    if (bloblist->next == NULL) {
vb@39
   158
        bloblist->next = new_bloblist(blob, size, mime_type, file_name);
vb@38
   159
        return bloblist->next;
vb@38
   160
    }
vb@38
   161
vb@39
   162
    return bloblist_add(bloblist->next, blob, size, mime_type, file_name);
vb@38
   163
}
vb@38
   164
vb@29
   165
message *new_message(
vb@38
   166
        PEP_msg_direction dir,
vb@39
   167
        pEp_identity *from,
vb@39
   168
        identity_list *to,
vb@29
   169
        const char *shortmsg
vb@29
   170
    )
vb@29
   171
{
vb@29
   172
    message *msg = calloc(1, sizeof(message));
vb@29
   173
    assert(msg);
vb@29
   174
    if (msg == NULL)
vb@29
   175
        return NULL;
vb@29
   176
vb@39
   177
    if (shortmsg) {
vb@37
   178
        msg->shortmsg = strdup(shortmsg);
vb@37
   179
        assert(msg->shortmsg);
vb@37
   180
        if (msg->shortmsg == NULL) {
vb@37
   181
            free(msg);
vb@37
   182
            return NULL;
vb@37
   183
        }
vb@29
   184
    }
vb@29
   185
vb@29
   186
    msg->dir = dir;
vb@39
   187
    msg->from = from;
vb@39
   188
    msg->to = to;
vb@29
   189
vb@29
   190
    return msg;
vb@29
   191
}
vb@29
   192
vb@29
   193
void free_message(message *msg)
vb@29
   194
{
vb@29
   195
    free(msg->id);
vb@29
   196
    free(msg->shortmsg);
vb@29
   197
    free(msg->longmsg);
vb@29
   198
    free(msg->longmsg_formatted);
vb@38
   199
    free_bloblist(msg->attachments);
vb@40
   200
    free_identity(msg->from);
vb@29
   201
    free_identity_list(msg->to);
vb@40
   202
    free_identity(msg->recv_by);
vb@29
   203
    free_identity_list(msg->cc);
vb@29
   204
    free_identity_list(msg->bcc);
vb@29
   205
    free(msg->refering_id);
vb@29
   206
    free_message_ref_list(msg->refered_by);
vb@29
   207
    free(msg);
vb@29
   208
}
vb@29
   209
vb@29
   210
message_ref_list *new_message_ref_list(message *msg)
vb@29
   211
{
vb@29
   212
    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
vb@29
   213
    assert(msg_list);
vb@29
   214
    if (msg_list == NULL)
vb@29
   215
        return NULL;
vb@29
   216
vb@29
   217
    msg_list->msg_ref = msg;
vb@29
   218
vb@29
   219
    return msg_list;
vb@29
   220
}
vb@29
   221
vb@29
   222
void free_message_ref_list(message_ref_list *msg_list)
vb@29
   223
{
vb@29
   224
    if (msg_list) {
vb@29
   225
        free_message_ref_list(msg_list->next);
vb@29
   226
        free(msg_list);
vb@29
   227
    }
vb@29
   228
}
vb@29
   229
vb@29
   230
message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
vb@29
   231
{
vb@29
   232
    assert(msg);
vb@29
   233
vb@38
   234
    if (msg_list == NULL)
vb@38
   235
        return new_message_ref_list(msg);
vb@38
   236
vb@29
   237
    if (msg_list->msg_ref == NULL) {
vb@29
   238
        msg_list->msg_ref = msg;
vb@29
   239
        return msg_list;
vb@29
   240
    }
vb@29
   241
    else if (msg_list->next == NULL) {
vb@29
   242
        msg_list->next = new_message_ref_list(msg);
vb@29
   243
        assert(msg_list->next);
vb@29
   244
        return msg_list->next;
vb@29
   245
    }
vb@29
   246
    else {
vb@29
   247
        return message_ref_list_add(msg_list->next, msg);
vb@29
   248
    }
vb@29
   249
}
vb@29
   250