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