message api
authorvb
Sun, 28 Dec 2014 23:07:55 +0100
changeset 377928e0e6eee9
parent 36 7857c4a83b5f
child 38 271bea5552dc
message api
Makefile
Makefile.conf
README.txt
src/message_api.c
src/message_api.h
src/pEpEngine.c
src/pEpEngine.h
src/transport.c
src/transport.h
     1.1 --- a/Makefile	Sun Dec 28 17:31:52 2014 +0100
     1.2 +++ b/Makefile	Sun Dec 28 23:07:55 2014 +0100
     1.3 @@ -19,7 +19,7 @@
     1.4  	$(MAKE) -C test test
     1.5  
     1.6  package: clean
     1.7 -	cd .. ; COPYFILE_DISABLE=true tar  cjf pEpEngine.tar.bz2 pEpEngine
     1.8 +	cd .. ; COPYFILE_DISABLE=true tar cjf pEpEngine.tar.bz2 pEpEngine
     1.9  
    1.10  windist:
    1.11  ifneq ($(BUILD_FOR),Windoze)
     2.1 --- a/Makefile.conf	Sun Dec 28 17:31:52 2014 +0100
     2.2 +++ b/Makefile.conf	Sun Dec 28 23:07:55 2014 +0100
     2.3 @@ -1,7 +1,7 @@
     2.4  BUILD_ON=$(shell uname)
     2.5  BUILD_FOR=$(BUILD_ON)
     2.6 -OPTIMIZE=-g -O0
     2.7 -#OPTIMIZE=-O3 -DNDEBUG
     2.8 +#OPTIMIZE=-g -O0
     2.9 +OPTIMIZE=-O3 -DNDEBUG
    2.10  # the next two lines are ignored on Windoze
    2.11  SYSTEM_DB=/usr/local/share/pEp/system.db
    2.12  PREFIX=$(HOME)
     3.1 --- a/README.txt	Sun Dec 28 17:31:52 2014 +0100
     3.2 +++ b/README.txt	Sun Dec 28 23:07:55 2014 +0100
     3.3 @@ -7,6 +7,8 @@
     3.4  The pEp Engine encapsulates all real functionality of pEp.
     3.5  It has an old style Makefile for building it.
     3.6  
     3.7 +The build is configured in Makefile.conf
     3.8 +
     3.9  It supports the common targets
    3.10  
    3.11  $ make all
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/src/message_api.c	Sun Dec 28 23:07:55 2014 +0100
     4.3 @@ -0,0 +1,106 @@
     4.4 +#include "message_api.h"
     4.5 +#include "keymanagement.h"
     4.6 +
     4.7 +#include <libetpan/libetpan.h>
     4.8 +#include <assert.h>
     4.9 +#include <string.h>
    4.10 +
    4.11 +PEP_STATUS encrypt_message(
    4.12 +        PEP_SESSION session,
    4.13 +        const message *src,
    4.14 +        stringlist_t * extra,
    4.15 +        message **dst
    4.16 +    )
    4.17 +{
    4.18 +    PEP_STATUS status = PEP_STATUS_OK;
    4.19 +
    4.20 +    assert(session);
    4.21 +    assert(src);
    4.22 +    assert(dst);
    4.23 +    *dst = NULL;
    4.24 +
    4.25 +    message *msg = new_message(src->dir, src->from, src->to, NULL);
    4.26 +    if (msg == NULL)
    4.27 +        return PEP_OUT_OF_MEMORY;
    4.28 +
    4.29 +    src->from->me = true;
    4.30 +
    4.31 +    status = myself(session, src->from);
    4.32 +    if (status != PEP_STATUS_OK) {
    4.33 +        free_message(msg);
    4.34 +        return status;
    4.35 +    }
    4.36 +
    4.37 +    stringlist_t * keys = new_stringlist(src->from->fpr);
    4.38 +    if (keys == NULL) {
    4.39 +        free_message(msg);
    4.40 +        return PEP_OUT_OF_MEMORY;
    4.41 +    }
    4.42 +
    4.43 +    stringlist_t *_x;
    4.44 +    for (_x = extra; _x && _x->value; _x = _x->next) {
    4.45 +        if (stringlist_add(keys, _x->value) == NULL) {
    4.46 +            free_message(msg);
    4.47 +            free_stringlist(keys);
    4.48 +            return PEP_OUT_OF_MEMORY;
    4.49 +        }
    4.50 +    }
    4.51 +    
    4.52 +    identity_list * _il;
    4.53 +    for (_il = src->to; _il && _il->ident; _il = _il->next) {
    4.54 +        status = update_identity(session, _il->ident);
    4.55 +        if (status != PEP_STATUS_OK) {
    4.56 +            free_message(msg);
    4.57 +            free_stringlist(keys);
    4.58 +            return status;
    4.59 +        }
    4.60 +        if (_il->ident->fpr) {
    4.61 +            if (stringlist_add(keys, _il->ident->fpr) == NULL) {
    4.62 +                free_message(msg);
    4.63 +                free_stringlist(keys);
    4.64 +                return PEP_OUT_OF_MEMORY;
    4.65 +            }
    4.66 +        }
    4.67 +        else
    4.68 +            status = PEP_KEY_NOT_FOUND;
    4.69 +    }
    4.70 +
    4.71 +    int _own_keys = 1;
    4.72 +    if (extra)
    4.73 +        _own_keys += stringlist_length(extra);
    4.74 +    
    4.75 +    if (stringlist_length(keys) > _own_keys) {
    4.76 +        char *ptext = NULL;
    4.77 +        char *ctext = NULL;
    4.78 +        size_t csize = 0;
    4.79 +
    4.80 +        // TODO: set ptext to MIME text
    4.81 +
    4.82 +        status = encrypt_and_sign(session, keys, ptext, strlen(ptext), &ctext, &csize);
    4.83 +        if (ctext) {
    4.84 +            msg->longmsg = ctext;
    4.85 +            msg->longmsg_size = csize;
    4.86 +            *dst = msg;
    4.87 +        }
    4.88 +        else
    4.89 +            free_message(msg);
    4.90 +        free(ptext);
    4.91 +    }
    4.92 +    else
    4.93 +        free_message(msg);
    4.94 +
    4.95 +    free_stringlist(keys);
    4.96 +    return status;
    4.97 +}
    4.98 +
    4.99 +PEP_STATUS decrypt_message(
   4.100 +        PEP_SESSION session,
   4.101 +        const message *src,
   4.102 +        message **dst
   4.103 +    )
   4.104 +{
   4.105 +    PEP_STATUS status = PEP_STATUS_OK;
   4.106 +
   4.107 +    return status;
   4.108 +}
   4.109 +
     5.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     5.2 +++ b/src/message_api.h	Sun Dec 28 23:07:55 2014 +0100
     5.3 @@ -0,0 +1,23 @@
     5.4 +#ifdef __cplusplus
     5.5 +extern "C" {
     5.6 +#endif
     5.7 +
     5.8 +#include "transport.h"
     5.9 +
    5.10 +PEP_STATUS encrypt_message(
    5.11 +        PEP_SESSION session,
    5.12 +        const message *src,
    5.13 +        stringlist_t *extra,
    5.14 +        message **dst
    5.15 +    );
    5.16 +
    5.17 +PEP_STATUS decrypt_message(
    5.18 +        PEP_SESSION session,
    5.19 +        const message *src,
    5.20 +        message **dst
    5.21 +    );
    5.22 +
    5.23 +#ifdef __cplusplus
    5.24 +}
    5.25 +#endif
    5.26 +
     6.1 --- a/src/pEpEngine.c	Sun Dec 28 17:31:52 2014 +0100
     6.2 +++ b/src/pEpEngine.c	Sun Dec 28 23:07:55 2014 +0100
     6.3 @@ -534,6 +534,24 @@
     6.4      return result;
     6.5  }
     6.6  
     6.7 +pEp_identity *identity_dup(const pEp_identity *src)
     6.8 +{
     6.9 +    assert(src);
    6.10 +
    6.11 +    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
    6.12 +    assert(dup);
    6.13 +    if (dup == NULL)
    6.14 +        return NULL;
    6.15 +    
    6.16 +    dup->comm_type = src->comm_type;
    6.17 +    dup->lang[0] = src->lang[0];
    6.18 +    dup->lang[1] = src->lang[1];
    6.19 +    dup->lang[2] = 0;
    6.20 +    dup->me = src->me;
    6.21 +
    6.22 +    return dup;
    6.23 +}
    6.24 +
    6.25  void free_identity(pEp_identity *identity)
    6.26  {
    6.27      if (identity) {
     7.1 --- a/src/pEpEngine.h	Sun Dec 28 17:31:52 2014 +0100
     7.2 +++ b/src/pEpEngine.h	Sun Dec 28 23:07:55 2014 +0100
     7.3 @@ -415,6 +415,21 @@
     7.4      );
     7.5  
     7.6  
     7.7 +// identity_dup() - allocate memory and set the string and size fields
     7.8 +//
     7.9 +//  parameters:
    7.10 +//      src (in)            identity to duplicate
    7.11 +//
    7.12 +//  return value:
    7.13 +//      pEp_identity struct with correct size values or NULL if out of memory
    7.14 +//
    7.15 +//  caveat:
    7.16 +//      the strings are copied; the original strings are still being owned by
    7.17 +//      the caller
    7.18 +
    7.19 +DYNAMIC_API pEp_identity *identity_dup(const pEp_identity *src);
    7.20 +
    7.21 +
    7.22  // free_identity() - free all memory being occupied by a pEp_identity struct
    7.23  //
    7.24  //  parameters:
     8.1 --- a/src/transport.c	Sun Dec 28 17:31:52 2014 +0100
     8.2 +++ b/src/transport.c	Sun Dec 28 23:07:55 2014 +0100
     8.3 @@ -24,28 +24,6 @@
     8.4      // nothing yet
     8.5  }
     8.6  
     8.7 -pEp_identity *identity_dup(const pEp_identity *src)
     8.8 -{
     8.9 -    assert(src);
    8.10 -
    8.11 -    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
    8.12 -    assert(dup);
    8.13 -    if (dup == NULL)
    8.14 -        return NULL;
    8.15 -    
    8.16 -    dup->address_size = strlen(dup->address);
    8.17 -    dup->fpr_size = strlen(dup->fpr);
    8.18 -    dup->user_id_size = strlen(dup->user_id);
    8.19 -    dup->username_size = strlen(dup->username);
    8.20 -    dup->comm_type = src->comm_type;
    8.21 -    dup->lang[0] = src->lang[0];
    8.22 -    dup->lang[1] = src->lang[1];
    8.23 -    dup->lang[2] = 0;
    8.24 -    dup->me = src->me;
    8.25 -
    8.26 -    return dup;
    8.27 -}
    8.28 -
    8.29  identity_list *new_identity_list(const pEp_identity *ident)
    8.30  {
    8.31      identity_list *id_list = calloc(1, sizeof(identity_list));
    8.32 @@ -65,6 +43,26 @@
    8.33      return id_list;
    8.34  }
    8.35  
    8.36 +identity_list *identity_list_dup(const identity_list *src)
    8.37 +{
    8.38 +    assert(src);
    8.39 +
    8.40 +    identity_list *id_list = new_identity_list(src->ident);
    8.41 +    assert(id_list);
    8.42 +    if (id_list == NULL)
    8.43 +        return NULL;
    8.44 +
    8.45 +    if (src->next) {
    8.46 +        id_list->next = identity_list_dup(src->next);
    8.47 +        if (id_list->next == NULL) {
    8.48 +            free_identity_list(id_list);
    8.49 +            return NULL;
    8.50 +        }
    8.51 +    }
    8.52 +
    8.53 +    return id_list;
    8.54 +}
    8.55 +
    8.56  void free_identity_list(identity_list *id_list)
    8.57  {
    8.58      if (id_list) {
    8.59 @@ -100,7 +98,7 @@
    8.60  message *new_message(
    8.61          msg_direction dir,
    8.62          const pEp_identity *from,
    8.63 -        const pEp_identity *to,
    8.64 +        const identity_list *to,
    8.65          const char *shortmsg
    8.66      )
    8.67  {
    8.68 @@ -109,13 +107,15 @@
    8.69      if (msg == NULL)
    8.70          return NULL;
    8.71  
    8.72 -    msg->shortmsg = strdup(shortmsg);
    8.73 -    assert(msg->shortmsg);
    8.74 -    if (msg->shortmsg == NULL) {
    8.75 -        free(msg);
    8.76 -        return NULL;
    8.77 +    if (msg->shortmsg) {
    8.78 +        msg->shortmsg = strdup(shortmsg);
    8.79 +        assert(msg->shortmsg);
    8.80 +        if (msg->shortmsg == NULL) {
    8.81 +            free(msg);
    8.82 +            return NULL;
    8.83 +        }
    8.84 +        msg->shortmsg_size = strlen(msg->shortmsg);
    8.85      }
    8.86 -    msg->shortmsg_size = strlen(msg->shortmsg);
    8.87  
    8.88      msg->dir = dir;
    8.89  
    8.90 @@ -126,16 +126,7 @@
    8.91          return NULL;
    8.92      }
    8.93  
    8.94 -    if (dir == dir_incoming) {
    8.95 -        msg->recv_by = identity_dup(to);
    8.96 -        assert(msg->recv_by);
    8.97 -        if (msg->recv_by == NULL) {
    8.98 -            free_message(msg);
    8.99 -            return NULL;
   8.100 -        }
   8.101 -    }
   8.102 -
   8.103 -    msg->to = new_identity_list(to);
   8.104 +    msg->to = identity_list_dup(to);
   8.105      assert(msg->to);
   8.106      if (msg->to == NULL) {
   8.107          free_message(msg);
     9.1 --- a/src/transport.h	Sun Dec 28 17:31:52 2014 +0100
     9.2 +++ b/src/transport.h	Sun Dec 28 23:07:55 2014 +0100
     9.3 @@ -4,9 +4,12 @@
     9.4  #include <time.h>
     9.5  #include <stdlib.h>
     9.6  
     9.7 +// all functions are using POSIX struct tm
     9.8 +
     9.9  typedef struct tm timestamp;
    9.10  
    9.11  typedef enum _PEP_transports {
    9.12 +    // auto transport chooses transport per message automatically
    9.13      PEP_trans_auto = 0,
    9.14  //    PEP_trans_email,
    9.15  //    PEP_trans_whatsapp,
    9.16 @@ -16,14 +19,13 @@
    9.17  
    9.18  typedef struct _PEP_transport_t PEP_transport_t;
    9.19  
    9.20 -pEp_identity *identity_dup(const pEp_identity *src);
    9.21 -
    9.22  typedef struct _identity_list {
    9.23      pEp_identity *ident;
    9.24      struct _identity_list *next;
    9.25  } identity_list;
    9.26  
    9.27  identity_list *new_identity_list(const pEp_identity *ident);
    9.28 +identity_list *identity_list_dup(const identity_list *src);
    9.29  void free_identity_list(identity_list *id_list);
    9.30  identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident);
    9.31  
    9.32 @@ -73,7 +75,7 @@
    9.33  message *new_message(
    9.34          msg_direction dir,
    9.35          const pEp_identity *from,
    9.36 -        const pEp_identity *to,
    9.37 +        const identity_list *to,
    9.38          const char *shortmsg
    9.39      );
    9.40  
    9.41 @@ -98,3 +100,4 @@
    9.42  
    9.43  PEP_STATUS init_transport_system(PEP_SESSION session);
    9.44  void release_transport_system(PEP_SESSION session);
    9.45 +