opt_fields added with decrypt
authorVolker Birk <vb@pep-project.org>
Mon, 25 May 2015 23:31:17 +0200
changeset 2819f7691853773
parent 280 1ed189760822
child 282 e7d04b49f8ab
opt_fields added with decrypt
src/message.c
src/message_api.c
src/mime.c
src/stringpair.c
src/stringpair.h
test/message_api_test.cc
test/mime_test.cc
     1.1 --- a/src/message.c	Sun May 24 11:06:29 2015 +0200
     1.2 +++ b/src/message.c	Mon May 25 23:31:17 2015 +0200
     1.3 @@ -31,16 +31,6 @@
     1.4      msg->from = from;
     1.5      msg->to = to;
     1.6  
     1.7 -    stringpair_t version;
     1.8 -    version.key = "X-pEp-Version";
     1.9 -    version.value = PEP_VERSION;
    1.10 -
    1.11 -    msg->opt_fields = new_stringpair_list(&version);
    1.12 -    if (msg->opt_fields == NULL) {
    1.13 -        free_message(msg);
    1.14 -        return NULL;
    1.15 -    }
    1.16 -
    1.17      return msg;
    1.18  }
    1.19  
     2.1 --- a/src/message_api.c	Sun May 24 11:06:29 2015 +0200
     2.2 +++ b/src/message_api.c	Mon May 25 23:31:17 2015 +0200
     2.3 @@ -534,7 +534,6 @@
     2.4      PEP_STATUS status = PEP_STATUS_OK;
     2.5      message * msg = NULL;
     2.6      stringlist_t * keys = NULL;
     2.7 -    bool free_src = false;
     2.8  
     2.9      assert(session);
    2.10      assert(src);
    2.11 @@ -621,8 +620,6 @@
    2.12      }
    2.13  
    2.14      free_stringlist(keys);
    2.15 -    if (free_src)
    2.16 -        free_message(src);
    2.17  
    2.18      if (msg->shortmsg == NULL)
    2.19          msg->shortmsg = strdup("pEp");
    2.20 @@ -636,8 +633,6 @@
    2.21  pep_error:
    2.22      free_stringlist(keys);
    2.23      free_message(msg);
    2.24 -    if (free_src)
    2.25 -        free_message(src);
    2.26  
    2.27      return status;
    2.28  }
    2.29 @@ -790,6 +785,96 @@
    2.30      return color;
    2.31  }
    2.32  
    2.33 +static void add_opt_field(message *msg, const char *name, const char *value)
    2.34 +{
    2.35 +    assert(msg);
    2.36 +
    2.37 +    if (msg && name && value) {
    2.38 +        stringpair_t *pair = new_stringpair(name, value);
    2.39 +        if (pair == NULL)
    2.40 +            return;
    2.41 +
    2.42 +        stringpair_list_t *field = stringpair_list_add(msg->opt_fields, pair);
    2.43 +        if (field == NULL)
    2.44 +            return;
    2.45 +
    2.46 +        if (msg->opt_fields == NULL)
    2.47 +            msg->opt_fields = field;
    2.48 +    }
    2.49 +}
    2.50 +
    2.51 +static char * keylist_to_string(const stringlist_t *keylist)
    2.52 +{
    2.53 +    if (keylist) {
    2.54 +        size_t size = stringlist_length(keylist);
    2.55 +
    2.56 +        const stringlist_t *_kl;
    2.57 +        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
    2.58 +            size += strlen(_kl->value);
    2.59 +        }
    2.60 +
    2.61 +        char *result = calloc(1, size);
    2.62 +        if (result == NULL)
    2.63 +            return NULL;
    2.64 +
    2.65 +        char *_r = result;
    2.66 +        for (_kl = keylist; _kl && _kl->value; _kl = _kl->next) {
    2.67 +            _r = stpcpy(_r, _kl->value);
    2.68 +            if (_kl->next && _kl->next->value)
    2.69 +                _r = stpcpy(_r, ",");
    2.70 +        }
    2.71 +
    2.72 +        return result;
    2.73 +    }
    2.74 +    else {
    2.75 +        return NULL;
    2.76 +    }
    2.77 +}
    2.78 +
    2.79 +static const char * color_to_string(PEP_color color)
    2.80 +{
    2.81 +    switch (color) {
    2.82 +        case PEP_rating_cannot_decrypt:
    2.83 +            return "cannot_decrypt";
    2.84 +        case PEP_rating_have_no_key:
    2.85 +            return "have_no_key";
    2.86 +        case PEP_rating_unencrypted:
    2.87 +            return "unencrypted";
    2.88 +        case PEP_rating_unreliable:
    2.89 +            return "unreliable";
    2.90 +        case PEP_rating_reliable:
    2.91 +            return "reliable";
    2.92 +        case PEP_rating_trusted:
    2.93 +            return "trusted";
    2.94 +        case PEP_rating_trusted_and_anonymized:
    2.95 +            return "trusted_and_anonymized";
    2.96 +        case PEP_rating_fully_anonymous:
    2.97 +            return "fully_anonymous";
    2.98 +        case PEP_rating_under_attack:
    2.99 +            return "unter_attack";
   2.100 +        case PEP_rating_b0rken:
   2.101 +            return "b0rken";
   2.102 +        default:
   2.103 +            return "undefined";
   2.104 +    }
   2.105 +}
   2.106 +
   2.107 +static void decorate_message(
   2.108 +        message *msg,
   2.109 +        stringlist_t *keylist,
   2.110 +        PEP_color color
   2.111 +    )
   2.112 +{
   2.113 +    assert(msg);
   2.114 +
   2.115 +    add_opt_field(msg, "X-pEp-Version", "1.0");
   2.116 +    add_opt_field(msg, "X-EncStatus", color_to_string(color));
   2.117 +
   2.118 +    char *_keylist = keylist_to_string(keylist);
   2.119 +    add_opt_field(msg, "X-KeyList", _keylist);
   2.120 +    free(_keylist);
   2.121 +}
   2.122 +
   2.123  DYNAMIC_API PEP_STATUS decrypt_message(
   2.124          PEP_SESSION session,
   2.125          message *src,
   2.126 @@ -806,7 +891,6 @@
   2.127      char *ptext = NULL;
   2.128      size_t psize;
   2.129      stringlist_t *_keylist = NULL;
   2.130 -    bool free_src = false;
   2.131  
   2.132      assert(session);
   2.133      assert(src);
   2.134 @@ -975,8 +1059,7 @@
   2.135      }
   2.136  
   2.137  theend:
   2.138 -    if (free_src)
   2.139 -        free_message(src);
   2.140 +    decorate_message(msg, _keylist, *color);
   2.141  
   2.142      *dst = msg;
   2.143      *keylist = _keylist;
   2.144 @@ -989,8 +1072,6 @@
   2.145  pep_error:
   2.146      free_message(msg);
   2.147      free_stringlist(_keylist);
   2.148 -    if (free_src)
   2.149 -        free_message(src);
   2.150  
   2.151      return status;
   2.152  }
     3.1 --- a/src/mime.c	Sun May 24 11:06:29 2015 +0200
     3.2 +++ b/src/mime.c	Mon May 25 23:31:17 2015 +0200
     3.3 @@ -553,17 +553,19 @@
     3.4  
     3.5      if (msg->opt_fields) {
     3.6          stringpair_list_t *_l;
     3.7 -        for (_l = msg->opt_fields; _l; _l = _l->next) {
     3.8 +        for (_l = msg->opt_fields; _l && _l->value; _l = _l->next) {
     3.9              char *key = _l->value->key;
    3.10              char *value = _l->value->value;
    3.11 -            char *_value = mailmime_encode_subject_header("utf-8", value, 0);
    3.12 -            if (_value == NULL)
    3.13 -                goto enomem;
    3.14 +            if (key && value) {
    3.15 +                char *_value = mailmime_encode_subject_header("utf-8", value, 0);
    3.16 +                if (_value == NULL)
    3.17 +                    goto enomem;
    3.18  
    3.19 -            r = _append_optional_field(fields_list, key, _value);
    3.20 -            free(_value);
    3.21 -            if (r)
    3.22 -                goto enomem;
    3.23 +                r = _append_optional_field(fields_list, key, _value);
    3.24 +                free(_value);
    3.25 +                if (r)
    3.26 +                    goto enomem;
    3.27 +            }
    3.28          }
    3.29      }
    3.30  
    3.31 @@ -1161,11 +1163,11 @@
    3.32                      if (r)
    3.33                          goto enomem;
    3.34  
    3.35 -                    stringpair_t pair;
    3.36 -                    pair.key = name;
    3.37 -                    pair.value = _value;
    3.38 +                    stringpair_t *pair = new_stringpair(name, _value);
    3.39 +                    if (pair == NULL)
    3.40 +                        goto enomem;
    3.41  
    3.42 -                    opt = stringpair_list_add(opt, &pair);
    3.43 +                    opt = stringpair_list_add(opt, pair);
    3.44                      free(_value);
    3.45                      if (opt == NULL)
    3.46                          goto enomem;
     4.1 --- a/src/stringpair.c	Sun May 24 11:06:29 2015 +0200
     4.2 +++ b/src/stringpair.c	Mon May 25 23:31:17 2015 +0200
     4.3 @@ -50,18 +50,13 @@
     4.4      return new_stringpair(src->key, src->value);
     4.5  }
     4.6  
     4.7 -DYNAMIC_API stringpair_list_t *new_stringpair_list(const stringpair_t *value)
     4.8 +DYNAMIC_API stringpair_list_t *new_stringpair_list(stringpair_t *value)
     4.9  {
    4.10      stringpair_list_t *result = calloc(1, sizeof(stringpair_list_t));
    4.11      assert(result);
    4.12  
    4.13 -    if (result && value) {
    4.14 -        result->value = stringpair_dup(value);
    4.15 -        if (result->value == 0) {
    4.16 -            free(result);
    4.17 -            return NULL;
    4.18 -        }
    4.19 -    }
    4.20 +    if (result && value)
    4.21 +        result->value = value;
    4.22  
    4.23      return result;
    4.24  }
    4.25 @@ -91,7 +86,7 @@
    4.26  
    4.27  DYNAMIC_API stringpair_list_t *stringpair_list_add(
    4.28          stringpair_list_t *stringpair_list,
    4.29 -        const stringpair_t *value
    4.30 +        stringpair_t *value
    4.31      )
    4.32  {
    4.33      assert(value);
    4.34 @@ -99,12 +94,11 @@
    4.35      if (stringpair_list == NULL)
    4.36          return new_stringpair_list(value);
    4.37  
    4.38 -    if (stringpair_list->next != NULL)
    4.39 +    if (stringpair_list->next)
    4.40          return stringpair_list_add(stringpair_list->next, value);
    4.41 +
    4.42      if (stringpair_list->value == NULL) {
    4.43 -        stringpair_list->value = stringpair_dup(value);
    4.44 -        if (stringpair_list->value == NULL)
    4.45 -            return NULL;
    4.46 +        stringpair_list->value = value;
    4.47          return stringpair_list;
    4.48      }
    4.49  
     5.1 --- a/src/stringpair.h	Sun May 24 11:06:29 2015 +0200
     5.2 +++ b/src/stringpair.h	Mon May 25 23:31:17 2015 +0200
     5.3 @@ -56,17 +56,15 @@
     5.4  // new_stringpair_list() - allocate a new stringpair_list
     5.5  //
     5.6  //  parameters:
     5.7 -//      value (in)              initial value as C string or NULL for empty
     5.8 -//                              list
     5.9 +//      value (in)              initial value
    5.10  //
    5.11  //  return value:
    5.12  //      pointer to stringpair_list_t object or NULL if out of memory
    5.13  //
    5.14  //  caveat:
    5.15 -//      the value is being copied before being added to the list
    5.16 -//      the original value is still being owned by the caller
    5.17 +//      the ownership of the value goes to the stringpair_list
    5.18  
    5.19 -DYNAMIC_API stringpair_list_t *new_stringpair_list(const stringpair_t *value);
    5.20 +DYNAMIC_API stringpair_list_t *new_stringpair_list(stringpair_t *value);
    5.21  
    5.22  
    5.23  // stringpair_list_dup() - duplicate a stringpair_list
    5.24 @@ -87,18 +85,17 @@
    5.25  //  parameters:
    5.26  //      stringpair_list (in)    stringpair_list struct or NULL to create a new
    5.27  //                              one
    5.28 -//      value (in)              value as C string
    5.29 +//      value (in)              stringpair to add
    5.30  //
    5.31  //  return value:
    5.32  //      pointer to last element in stringpair_list or NULL if out of memory
    5.33  //
    5.34  //  caveat:
    5.35 -//      the value is being copied before being added to the list
    5.36 -//      the original value is still being owned by the caller
    5.37 +//      the ownership of the value goes to the stringpair_list
    5.38  
    5.39  DYNAMIC_API stringpair_list_t *stringpair_list_add(
    5.40          stringpair_list_t *stringpair_list,
    5.41 -        const stringpair_t *value
    5.42 +        stringpair_t *value
    5.43      );
    5.44  
    5.45  
     6.1 --- a/test/message_api_test.cc	Sun May 24 11:06:29 2015 +0200
     6.2 +++ b/test/message_api_test.cc	Mon May 25 23:31:17 2015 +0200
     6.3 @@ -54,7 +54,7 @@
     6.4      cout << text2 << "\n";
     6.5  
     6.6      message *msg3;
     6.7 -    PEP_STATUS status3 = mime_decode_message(text2, &msg3);
     6.8 +    PEP_STATUS status3 = mime_decode_message(text2, strlen(text2), &msg3);
     6.9      assert(status3 == PEP_STATUS_OK);
    6.10      string string3 = text2;
    6.11      free(text2);
     7.1 --- a/test/mime_test.cc	Sun May 24 11:06:29 2015 +0200
     7.2 +++ b/test/mime_test.cc	Mon May 25 23:31:17 2015 +0200
     7.3 @@ -26,7 +26,7 @@
     7.4  
     7.5      cout << "decoding message…\n";
     7.6      message *msg3;
     7.7 -    PEP_STATUS status3 = mime_decode_message(mimetext3.c_str(), &msg3);
     7.8 +    PEP_STATUS status3 = mime_decode_message(mimetext3.c_str(), mimetext3.length(), &msg3);
     7.9      assert(status3 == PEP_STATUS_OK);
    7.10      assert(msg3);
    7.11      cout << "decoded.\n\n";