src/message.c
author Volker Birk <vb@pep.foundation>
Wed, 29 Aug 2018 17:10:23 +0200
branchsync
changeset 2899 63b619aef131
parent 2615 5cd3625e0d91
child 3884 49d25cfc873c
permissions -rw-r--r--
somewhat bigger
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
vb@125
     4
#include "pEp_internal.h"
vb@125
     5
vb@100
     6
#include <stdlib.h>
vb@100
     7
#include <string.h>
vb@100
     8
#include <assert.h>
vb@99
     9
vb@100
    10
#include "message.h"
vb@99
    11
vb@99
    12
DYNAMIC_API message *new_message(
vb@305
    13
        PEP_msg_direction dir
vb@99
    14
    )
vb@99
    15
{
vb@99
    16
    message *msg = calloc(1, sizeof(message));
vb@99
    17
    assert(msg);
vb@99
    18
    if (msg == NULL)
vb@99
    19
        return NULL;
vb@99
    20
vb@99
    21
    msg->dir = dir;
vb@99
    22
vb@99
    23
    return msg;
vb@99
    24
}
vb@99
    25
vb@99
    26
DYNAMIC_API void free_message(message *msg)
vb@99
    27
{
vb@99
    28
    if (msg) {
vb@99
    29
        free(msg->id);
vb@99
    30
        free(msg->shortmsg);
vb@99
    31
        free(msg->longmsg);
vb@99
    32
        free(msg->longmsg_formatted);
vb@99
    33
        free_bloblist(msg->attachments);
vb@113
    34
        free_timestamp(msg->sent);
vb@113
    35
        free_timestamp(msg->recv);
vb@99
    36
        free_identity(msg->from);
vb@99
    37
        free_identity_list(msg->to);
vb@99
    38
        free_identity(msg->recv_by);
vb@99
    39
        free_identity_list(msg->cc);
vb@99
    40
        free_identity_list(msg->bcc);
vb@99
    41
        free_identity_list(msg->reply_to);
vb@99
    42
        free_stringlist(msg->in_reply_to);
vb@99
    43
        free_stringlist(msg->references);
vb@99
    44
        free_stringlist(msg->keywords);
vb@99
    45
        free(msg->comments);
vb@99
    46
        free_stringpair_list(msg->opt_fields);
vb@99
    47
        free(msg);
vb@99
    48
    }
vb@99
    49
}
vb@99
    50
vb@99
    51
DYNAMIC_API message * message_dup(const message *src)
vb@99
    52
{
vb@99
    53
    message * msg = NULL;
vb@99
    54
    pEp_identity * from = NULL;
vb@99
    55
    identity_list * to = NULL;
vb@99
    56
vb@99
    57
    assert(src);
vb@99
    58
vb@305
    59
    msg = new_message(src->dir);
vb@99
    60
    if (msg == NULL)
vb@99
    61
        goto enomem;
vb@99
    62
vb@99
    63
    if (src->id) {
vb@99
    64
        msg->id = strdup(src->id);
vb@99
    65
        assert(msg->id);
vb@99
    66
        if (msg->id == NULL)
vb@99
    67
            goto enomem;
vb@99
    68
    }
vb@99
    69
vb@305
    70
    if (src->shortmsg) {
vb@305
    71
        msg->shortmsg = strdup(src->shortmsg);
vb@305
    72
        assert(msg->shortmsg);
vb@305
    73
        if (msg->shortmsg == NULL)
vb@305
    74
            goto enomem;
vb@305
    75
    }
vb@305
    76
vb@99
    77
    if (src->longmsg) {
vb@99
    78
        msg->longmsg = strdup(src->longmsg);
vb@99
    79
        assert(msg->longmsg);
vb@99
    80
        if (msg->longmsg == NULL)
vb@99
    81
            goto enomem;
vb@99
    82
    }
vb@99
    83
    
vb@99
    84
    if (src->longmsg_formatted) {
vb@99
    85
        msg->longmsg_formatted = strdup(src->longmsg_formatted);
vb@99
    86
        assert(msg->longmsg_formatted);
vb@99
    87
        if (msg->longmsg_formatted == NULL)
vb@99
    88
            goto enomem;
vb@99
    89
    }
vb@99
    90
vb@99
    91
    if (src->attachments) {
vb@99
    92
        msg->attachments = bloblist_dup(src->attachments);
vb@99
    93
        if (msg->attachments == NULL)
vb@99
    94
            goto enomem;
vb@99
    95
    }
vb@99
    96
vb@99
    97
    msg->rawmsg_ref = src->rawmsg_ref;
vb@99
    98
    msg->rawmsg_size = src->rawmsg_size;
vb@99
    99
vb@99
   100
    if (src->sent) {
vb@113
   101
        msg->sent = timestamp_dup(src->sent);
vb@99
   102
        if (msg->sent == NULL)
vb@99
   103
            goto enomem;
vb@99
   104
    }
vb@99
   105
vb@99
   106
    if (src->recv) {
vb@113
   107
        msg->recv = timestamp_dup(src->recv);
vb@99
   108
        if (msg->recv == NULL)
vb@99
   109
            goto enomem;
vb@99
   110
    }
vb@99
   111
vb@305
   112
    if (src->from) {
vb@305
   113
        msg->from = identity_dup(src->from);
vb@305
   114
        if (msg->from == NULL)
vb@305
   115
            goto enomem;
vb@305
   116
    }
vb@305
   117
vb@99
   118
    if (src->recv_by) {
vb@99
   119
        msg->recv_by = identity_dup(src->recv_by);
vb@99
   120
        if (msg->recv_by == NULL)
vb@99
   121
            goto enomem;
vb@99
   122
    }
vb@99
   123
vb@305
   124
    if (src->to) {
vb@305
   125
        msg->to = identity_list_dup(src->to);
vb@305
   126
        if (msg->to == NULL)
vb@305
   127
            goto enomem;
vb@305
   128
    }
vb@305
   129
vb@99
   130
    if (src->cc) {
vb@99
   131
        msg->cc = identity_list_dup(src->cc);
vb@99
   132
        if (msg->cc == NULL)
vb@99
   133
            goto enomem;
vb@99
   134
    }
vb@99
   135
vb@99
   136
    if (src->bcc) {
vb@99
   137
        msg->bcc = identity_list_dup(src->bcc);
vb@99
   138
        if (msg->bcc == NULL)
vb@99
   139
            goto enomem;
vb@99
   140
    }
vb@99
   141
vb@99
   142
    if (src->reply_to) {
vb@99
   143
        msg->reply_to = identity_list_dup(src->reply_to);
vb@99
   144
        if (msg->reply_to == NULL)
vb@99
   145
            goto enomem;
vb@99
   146
    }
vb@99
   147
vb@99
   148
    if (src->in_reply_to) {
vb@99
   149
        msg->in_reply_to = stringlist_dup(src->in_reply_to);
vb@99
   150
        assert(msg->in_reply_to);
vb@99
   151
        if (msg->in_reply_to == NULL)
vb@99
   152
            goto enomem;
vb@99
   153
    }
vb@99
   154
vb@99
   155
    msg->refering_msg_ref = src->refering_msg_ref;
vb@99
   156
    
vb@99
   157
    if (src->references) {
vb@99
   158
        msg->references = stringlist_dup(src->references);
vb@99
   159
        if (msg->references == NULL)
vb@99
   160
            goto enomem;
vb@99
   161
    }
vb@99
   162
vb@99
   163
    if (src->refered_by) {
vb@99
   164
        msg->refered_by = message_ref_list_dup(src->refered_by);
vb@99
   165
        if (msg->refered_by == NULL)
vb@99
   166
            goto enomem;
vb@99
   167
    }
vb@99
   168
vb@99
   169
    if (src->keywords) {
vb@99
   170
        msg->keywords = stringlist_dup(src->keywords);
vb@99
   171
        if (msg->keywords == NULL)
vb@99
   172
            goto enomem;
vb@99
   173
    }
vb@99
   174
vb@99
   175
    if (src->comments) {
vb@99
   176
        msg->comments = strdup(src->comments);
vb@99
   177
        assert(msg->comments);
vb@99
   178
        if (msg->comments == NULL)
vb@99
   179
            goto enomem;
vb@99
   180
    }
vb@99
   181
vb@99
   182
    if (src->opt_fields) {
vb@99
   183
        msg->opt_fields = stringpair_list_dup(src->opt_fields);
vb@99
   184
        if (msg->opt_fields == NULL)
vb@99
   185
            goto enomem;
vb@99
   186
    }
vb@99
   187
vb@99
   188
    msg->enc_format = src->enc_format;
vb@99
   189
vb@99
   190
    return msg;
vb@99
   191
vb@99
   192
enomem:
vb@99
   193
    if (msg) {
vb@99
   194
        free_message(msg);
vb@99
   195
    }
vb@99
   196
    else {
vb@99
   197
        free_identity(from);
vb@99
   198
        free_identity_list(to);
vb@99
   199
    }
vb@99
   200
vb@99
   201
    return NULL;
vb@99
   202
}
vb@99
   203
krista@2615
   204
DYNAMIC_API void message_transfer(message* dst, message *src)
krista@2615
   205
{
krista@2615
   206
    assert(dst);
krista@2615
   207
    assert(src);
krista@2615
   208
krista@2615
   209
    dst->dir = src->dir;
krista@2615
   210
    dst->rawmsg_ref = src->rawmsg_ref;
krista@2615
   211
    dst->rawmsg_size = src->rawmsg_size;
krista@2615
   212
    dst->refering_msg_ref = src->refering_msg_ref;
krista@2615
   213
    dst->enc_format = src->enc_format;
krista@2615
   214
krista@2615
   215
    /* Strings */
krista@2615
   216
    free(dst->id);
krista@2615
   217
    free(dst->shortmsg);
krista@2615
   218
    free(dst->longmsg);
krista@2615
   219
    free(dst->longmsg_formatted);
krista@2615
   220
    free(dst->comments);
krista@2615
   221
    dst->id = src->id;
krista@2615
   222
    dst->shortmsg = src->shortmsg;
krista@2615
   223
    dst->longmsg = src->longmsg;
krista@2615
   224
    dst->longmsg_formatted = src->longmsg_formatted;
krista@2615
   225
    dst->comments = src->comments;    
krista@2615
   226
    src->id = src->shortmsg = src->longmsg = src->longmsg_formatted = NULL;
krista@2615
   227
    src->comments = NULL;
krista@2615
   228
    
krista@2615
   229
    /* bloblists */
krista@2615
   230
    free_bloblist(dst->attachments);
krista@2615
   231
    dst->attachments = src->attachments;
krista@2615
   232
    src->attachments = NULL;
krista@2615
   233
    
krista@2615
   234
    /* timestamps */
krista@2615
   235
    free_timestamp(dst->sent);
krista@2615
   236
    free_timestamp(dst->recv);
krista@2615
   237
    dst->sent = src->sent;
krista@2615
   238
    dst->recv = src->recv;
krista@2615
   239
    src->sent = src->recv = NULL;
krista@2615
   240
    
krista@2615
   241
    /* identities */
krista@2615
   242
    free_identity(dst->from);
krista@2615
   243
    free_identity(dst->recv_by);
krista@2615
   244
    dst->from = src->from;
krista@2615
   245
    dst->recv_by = src->recv_by;
krista@2615
   246
    src->from = src->recv_by = NULL;
krista@2615
   247
    
krista@2615
   248
    /* identity lists */
krista@2615
   249
    free_identity_list(dst->to);
krista@2615
   250
    free_identity_list(dst->cc);
krista@2615
   251
    free_identity_list(dst->bcc);
krista@2615
   252
    free_identity_list(dst->reply_to);
krista@2615
   253
    dst->to = src->to;
krista@2615
   254
    dst->cc = src->cc;
krista@2615
   255
    dst->bcc = src->bcc;
krista@2615
   256
    dst->reply_to = src->reply_to;
krista@2615
   257
    src->to = src->cc = src->bcc = src->reply_to = NULL;
krista@2615
   258
krista@2615
   259
    /* stringlists */
krista@2615
   260
    free_stringlist(dst->references);
krista@2615
   261
    free_stringlist(dst->keywords);
krista@2615
   262
    free_stringlist(dst->in_reply_to);
krista@2615
   263
    dst->references = src->references;
krista@2615
   264
    dst->keywords = src->keywords;
krista@2615
   265
    dst->in_reply_to = src->in_reply_to;
krista@2615
   266
    src->references = src->keywords = src->in_reply_to = NULL;
krista@2615
   267
krista@2615
   268
    /* message ref list */
krista@2615
   269
    free_message_ref_list(dst->refered_by);
krista@2615
   270
    dst->refered_by = src->refered_by;
krista@2615
   271
    src->refered_by = NULL;
krista@2615
   272
    
krista@2615
   273
    /* stringpair lists */
krista@2615
   274
    free_stringpair_list(dst->opt_fields);
krista@2615
   275
    dst->opt_fields = src->opt_fields;
krista@2615
   276
    src->opt_fields = NULL;
krista@2615
   277
}
krista@2615
   278
vb@99
   279
DYNAMIC_API message_ref_list *new_message_ref_list(message *msg)
vb@99
   280
{
vb@99
   281
    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
vb@99
   282
    assert(msg_list);
vb@99
   283
    if (msg_list == NULL)
vb@99
   284
        return NULL;
vb@99
   285
vb@99
   286
    msg_list->msg_ref = msg;
vb@99
   287
vb@99
   288
    return msg_list;
vb@99
   289
}
vb@99
   290
vb@99
   291
DYNAMIC_API void free_message_ref_list(message_ref_list *msg_list)
vb@99
   292
{
vb@99
   293
    if (msg_list) {
vb@99
   294
        free_message_ref_list(msg_list->next);
vb@99
   295
        free(msg_list);
vb@99
   296
    }
vb@99
   297
}
vb@99
   298
vb@99
   299
DYNAMIC_API message_ref_list *message_ref_list_dup(
vb@99
   300
        const message_ref_list *src
vb@99
   301
    )
vb@99
   302
{
vb@99
   303
    message_ref_list * msg_list = NULL;
vb@99
   304
vb@99
   305
    assert(src);
vb@99
   306
vb@99
   307
    msg_list = new_message_ref_list(src->msg_ref);
vb@99
   308
    if (msg_list == NULL)
vb@99
   309
        goto enomem;
vb@99
   310
vb@99
   311
    if (src->next) {
vb@99
   312
        msg_list->next = message_ref_list_dup(src->next);
vb@99
   313
        if (msg_list->next == NULL)
vb@99
   314
            goto enomem;
vb@99
   315
    }
vb@99
   316
vb@99
   317
    return msg_list;
vb@99
   318
vb@99
   319
enomem:
vb@99
   320
    free_message_ref_list(msg_list);
vb@99
   321
    return NULL;
vb@99
   322
}
vb@99
   323
vb@99
   324
DYNAMIC_API message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
vb@99
   325
{
vb@99
   326
    assert(msg);
vb@99
   327
vb@99
   328
    if (msg_list == NULL)
vb@99
   329
        return new_message_ref_list(msg);
vb@99
   330
vb@99
   331
    if (msg_list->msg_ref == NULL) {
vb@99
   332
        msg_list->msg_ref = msg;
vb@99
   333
        return msg_list;
vb@99
   334
    }
vb@99
   335
    else if (msg_list->next == NULL) {
vb@99
   336
        msg_list->next = new_message_ref_list(msg);
vb@99
   337
        assert(msg_list->next);
vb@99
   338
        return msg_list->next;
vb@99
   339
    }
vb@99
   340
    else {
vb@99
   341
        return message_ref_list_add(msg_list->next, msg);
vb@99
   342
    }
vb@99
   343
}