src/baseprotocol.c
author Volker Birk <vb@pep.foundation>
Tue, 20 Nov 2018 13:05:29 +0100
branchsync
changeset 3130 7416d52013ff
parent 3129 ad8aa4d09cf0
child 3143 81c80e27cbf7
permissions -rw-r--r--
switch to base_extract_message()
vb@1517
     1
// This file is under GNU General Public License 3.0
vb@1517
     2
// see LICENSE.txt
vb@1517
     3
vb@585
     4
#include "pEp_internal.h"
vb@952
     5
#include "message_api.h"
vb@585
     6
vb@2846
     7
PEP_STATUS base_decorate_message(
vb@587
     8
        message *msg,
vb@587
     9
        char *payload,
vb@3129
    10
        size_t size,
vb@3129
    11
        char *fpr
vb@587
    12
    )
vb@587
    13
{
vb@587
    14
    assert(msg);
vb@587
    15
    assert(payload);
vb@587
    16
    assert(size);
vb@587
    17
vb@587
    18
    if (!(msg && payload && size))
vb@587
    19
        return PEP_ILLEGAL_VALUE;
vb@587
    20
vb@587
    21
    bloblist_t *bl = bloblist_add(msg->attachments, payload, size,
krista@1871
    22
            "application/pEp.sync", "ignore_this_attachment.pEp");
vb@587
    23
    if (bl == NULL)
vb@587
    24
        goto enomem;
vb@587
    25
vb@3129
    26
    if (fpr) {
vb@3129
    27
        // add signature
vb@3129
    28
    }
vb@3129
    29
vb@587
    30
    msg->attachments = bl;
vb@587
    31
    return PEP_STATUS_OK;
vb@587
    32
vb@587
    33
enomem:
vb@587
    34
    return PEP_OUT_OF_MEMORY;
vb@587
    35
}
vb@587
    36
vb@2846
    37
PEP_STATUS base_prepare_message(
vb@585
    38
        const pEp_identity *me,
vb@585
    39
        const pEp_identity *partner,
vb@585
    40
        char *payload,
vb@585
    41
        size_t size,
vb@3129
    42
        char *fpr,
vb@585
    43
        message **result
vb@585
    44
    )
vb@585
    45
{
vb@587
    46
    PEP_STATUS status = PEP_STATUS_OK;
vb@587
    47
vb@585
    48
    assert(me);
vb@585
    49
    assert(partner);
vb@585
    50
    assert(payload);
vb@587
    51
    assert(size);
vb@587
    52
    assert(result);
vb@587
    53
vb@587
    54
    if (!(me && partner && payload && size && result))
vb@587
    55
        return PEP_ILLEGAL_VALUE;
vb@585
    56
vb@585
    57
    *result = NULL;
vb@585
    58
vb@585
    59
    message *msg = new_message(PEP_dir_outgoing);
vb@585
    60
    if (!msg)
vb@585
    61
        goto enomem;
vb@585
    62
vb@1132
    63
    add_opt_field(msg, "pEp-auto-consume", "yes");
vb@952
    64
vb@585
    65
    msg->from = identity_dup(me);
vb@585
    66
    if (!msg->from)
vb@585
    67
        goto enomem;
vb@585
    68
vb@585
    69
    msg->to = new_identity_list(identity_dup(partner));
vb@585
    70
    if (!msg->to)
vb@585
    71
        goto enomem;
vb@585
    72
vb@952
    73
    msg->shortmsg = strdup("p≡p synchronization message - please ignore");
vb@585
    74
    assert(msg->shortmsg);
vb@585
    75
    if (!msg->shortmsg)
vb@585
    76
        goto enomem;
vb@585
    77
vb@585
    78
    msg->longmsg = strdup("This message is part of p≡p's concept to synchronize.\n\n"
vb@585
    79
                        "You can safely ignore it. It will be deleted automatically.\n");
vb@585
    80
    assert(msg->longmsg);
vb@585
    81
    if (!msg->longmsg)
vb@585
    82
        goto enomem;
vb@585
    83
vb@3129
    84
    status = base_decorate_message(msg, payload, size, fpr);
vb@587
    85
    if (status == PEP_STATUS_OK)
vb@587
    86
        *result = msg;
vb@587
    87
    return status;
vb@585
    88
vb@585
    89
enomem:
vb@585
    90
    free_message(msg);
vb@585
    91
    return PEP_OUT_OF_MEMORY;
vb@585
    92
}
krista@1871
    93
vb@3130
    94
PEP_STATUS base_extract_message(message *msg, size_t *size, const char **payload)
vb@3130
    95
{
vb@3130
    96
    PEP_STATUS status = PEP_STATUS_OK;
vb@3130
    97
vb@3130
    98
    assert(msg && size && payload);
vb@3130
    99
    if (!(msg && size && payload))
vb@3130
   100
        return PEP_ILLEGAL_VALUE;
vb@3130
   101
vb@3130
   102
    *size = 0;
vb@3130
   103
    *payload = NULL;
vb@3130
   104
vb@3130
   105
    for (bloblist_t *bl = msg->attachments; bl ; bl = bl->next) {
vb@3130
   106
        if (bl->mime_type && strcasecmp(bl->mime_type, "application/pEp.sync") == 0) {
vb@3130
   107
            *size = bl->size;
vb@3130
   108
            *payload = bl->value;
vb@3130
   109
            break;
vb@3130
   110
        }
vb@3130
   111
    }
vb@3130
   112
vb@3130
   113
    return status;
vb@3130
   114
}
vb@3130
   115