decorate_message()
authorVolker Birk <vb@pep-project.org>
Tue, 10 May 2016 12:28:40 +0200
changeset 587ed8aa5e5b02c
parent 586 2eac603617fe
child 588 b7f954296900
decorate_message()
src/baseprotocol.c
src/baseprotocol.h
     1.1 --- a/src/baseprotocol.c	Tue May 10 12:15:14 2016 +0200
     1.2 +++ b/src/baseprotocol.c	Tue May 10 12:28:40 2016 +0200
     1.3 @@ -1,5 +1,30 @@
     1.4  #include "pEp_internal.h"
     1.5  
     1.6 +PEP_STATUS decorate_message(
     1.7 +        message *msg,
     1.8 +        char *payload,
     1.9 +        size_t size
    1.10 +    )
    1.11 +{
    1.12 +    assert(msg);
    1.13 +    assert(payload);
    1.14 +    assert(size);
    1.15 +
    1.16 +    if (!(msg && payload && size))
    1.17 +        return PEP_ILLEGAL_VALUE;
    1.18 +
    1.19 +    bloblist_t *bl = bloblist_add(msg->attachments, payload, size,
    1.20 +            "application/pEp", "auto.pEp");
    1.21 +    if (bl == NULL)
    1.22 +        goto enomem;
    1.23 +
    1.24 +    msg->attachments = bl;
    1.25 +    return PEP_STATUS_OK;
    1.26 +
    1.27 +enomem:
    1.28 +    return PEP_OUT_OF_MEMORY;
    1.29 +}
    1.30 +
    1.31  PEP_STATUS prepare_message(
    1.32          const pEp_identity *me,
    1.33          const pEp_identity *partner,
    1.34 @@ -8,9 +33,16 @@
    1.35          message **result
    1.36      )
    1.37  {
    1.38 +    PEP_STATUS status = PEP_STATUS_OK;
    1.39 +
    1.40      assert(me);
    1.41      assert(partner);
    1.42      assert(payload);
    1.43 +    assert(size);
    1.44 +    assert(result);
    1.45 +
    1.46 +    if (!(me && partner && payload && size && result))
    1.47 +        return PEP_ILLEGAL_VALUE;
    1.48  
    1.49      *result = NULL;
    1.50  
    1.51 @@ -37,12 +69,10 @@
    1.52      if (!msg->longmsg)
    1.53          goto enomem;
    1.54  
    1.55 -    msg->attachments = new_bloblist(payload, size, "application/pEp", "auto.pEp");
    1.56 -    if (msg->attachments == NULL)
    1.57 -        goto enomem;
    1.58 -
    1.59 -    *result = msg;
    1.60 -    return PEP_STATUS_OK;
    1.61 +    status = decorate_message(msg, payload, size);
    1.62 +    if (status == PEP_STATUS_OK)
    1.63 +        *result = msg;
    1.64 +    return status;
    1.65  
    1.66  enomem:
    1.67      free_message(msg);
     2.1 --- a/src/baseprotocol.h	Tue May 10 12:15:14 2016 +0200
     2.2 +++ b/src/baseprotocol.h	Tue May 10 12:28:40 2016 +0200
     2.3 @@ -6,6 +6,28 @@
     2.4  extern "C" {
     2.5  #endif
     2.6  
     2.7 +
     2.8 +// decorate_message() - decorate a message with payload
     2.9 +//
    2.10 +//  parameters:
    2.11 +//      msg (inout)     message to decorate
    2.12 +//      payload (in)    payload to send
    2.13 +//      size (in)       size of payload
    2.14 +//
    2.15 +//  returns:
    2.16 +//      PEP_STATUS_OK and result on success or an error on failure
    2.17 +//
    2.18 +//  caveat:
    2.19 +//      on success (and only then) payload goes to the ownership of the msg
    2.20 +//      the ownership of the msg remains with the caller
    2.21 +
    2.22 +PEP_STATUS decorate_message(
    2.23 +        message *msg,
    2.24 +        char *payload,
    2.25 +        size_t size
    2.26 +    );
    2.27 +
    2.28 +
    2.29  // prepare_message() - prepare a sync message with payload
    2.30  //
    2.31  //  parameters:
    2.32 @@ -16,11 +38,11 @@
    2.33  //      result (out)    message with payload
    2.34  //
    2.35  //  returns:
    2.36 -//      PEP_STATUS_OK on success or PEP_OUT_OF_MEMORY
    2.37 +//      PEP_STATUS_OK and result on success or an error on failure
    2.38  //
    2.39  //  caveat:
    2.40 -//      on success (and only then) payload goes to the ownership of the message
    2.41 -//      created
    2.42 +//      on success (and only then) payload goes to the ownership of the result
    2.43 +//      the ownership of the result goes to the caller
    2.44  
    2.45  PEP_STATUS prepare_message(
    2.46          const pEp_identity *me,
    2.47 @@ -30,6 +52,7 @@
    2.48          message **result
    2.49      );
    2.50  
    2.51 +
    2.52  #ifdef __cplusplus
    2.53  }
    2.54  #endif