message implementation
authorvb
Tue, 09 Sep 2014 13:00:10 +0200
changeset 2931881fe94d3c
parent 28 7fc9fc3e680c
child 30 49f7cbcb91df
message implementation
src/transport.c
src/transport.h
     1.1 --- a/src/transport.c	Mon Sep 01 20:56:05 2014 +0200
     1.2 +++ b/src/transport.c	Tue Sep 09 13:00:10 2014 +0200
     1.3 @@ -1,6 +1,5 @@
     1.4  #include "pEp_internal.h"
     1.5  
     1.6 -#include <stdlib.h>
     1.7  #include <memory.h>
     1.8  #include <assert.h>
     1.9  
    1.10 @@ -21,3 +20,175 @@
    1.11  {
    1.12      // nothing yet
    1.13  }
    1.14 +
    1.15 +pEp_identity *identity_dup(const pEp_identity *src)
    1.16 +{
    1.17 +    pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
    1.18 +    assert(dup);
    1.19 +    if (dup == NULL)
    1.20 +        return NULL;
    1.21 +    
    1.22 +    dup->address_size = strlen(dup->address);
    1.23 +    dup->fpr_size = strlen(dup->fpr);
    1.24 +    dup->user_id_size = strlen(dup->user_id);
    1.25 +    dup->username_size = strlen(dup->username);
    1.26 +    dup->comm_type = src->comm_type;
    1.27 +    dup->lang[0] = src->lang[0];
    1.28 +    dup->lang[1] = src->lang[1];
    1.29 +    dup->lang[2] = 0;
    1.30 +    dup->me = src->me;
    1.31 +}
    1.32 +
    1.33 +identity_list *new_identity_list(const pEp_identity *ident)
    1.34 +{
    1.35 +    identity_list *id_list = calloc(1, sizeof(identity_list));
    1.36 +    assert(id_list);
    1.37 +    if (id_list == NULL)
    1.38 +        return NULL;
    1.39 +
    1.40 +    if (ident) {
    1.41 +        id_list->ident = identity_dup(ident);
    1.42 +        assert(id_list->ident);
    1.43 +        if (id_list->ident == NULL) {
    1.44 +            free(id_list);
    1.45 +            return NULL;
    1.46 +        }
    1.47 +    }
    1.48 +
    1.49 +    return id_list;
    1.50 +}
    1.51 +
    1.52 +void free_identity_list(identity_list *id_list)
    1.53 +{
    1.54 +    if (id_list) {
    1.55 +        free_identity_list(id_list->next);
    1.56 +        free_identity(id_list->ident);
    1.57 +        free(id_list);
    1.58 +    }
    1.59 +}
    1.60 +
    1.61 +identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident)
    1.62 +{
    1.63 +    assert(id_list);
    1.64 +    assert(ident);
    1.65 +
    1.66 +    if (id_list->ident == NULL) {
    1.67 +        id_list->ident = identity_dup(ident);
    1.68 +        assert(id_list->ident);
    1.69 +        if (id_list->ident == NULL)
    1.70 +            return NULL;
    1.71 +        else
    1.72 +            return id_list;
    1.73 +    }
    1.74 +    else if (id_list->next == NULL) {
    1.75 +        id_list->next = new_identity_list(ident);
    1.76 +        assert(id_list->next);
    1.77 +        return id_list->next;
    1.78 +    }
    1.79 +    else {
    1.80 +        return identity_list_add(id_list->next, ident);
    1.81 +    }
    1.82 +}
    1.83 +
    1.84 +message *new_message(
    1.85 +        msg_direction dir,
    1.86 +        const pEp_identity *from,
    1.87 +        const pEp_identity *to,
    1.88 +        const char *shortmsg
    1.89 +    )
    1.90 +{
    1.91 +    message *msg = calloc(1, sizeof(message));
    1.92 +    assert(msg);
    1.93 +    if (msg == NULL)
    1.94 +        return NULL;
    1.95 +
    1.96 +    msg->shortmsg = strdup(shortmsg);
    1.97 +    assert(msg->shortmsg);
    1.98 +    if (msg->shortmsg == NULL) {
    1.99 +        free(msg);
   1.100 +        return NULL;
   1.101 +    }
   1.102 +    msg->shortmsg_size = strlen(msg->shortmsg);
   1.103 +
   1.104 +    msg->dir = dir;
   1.105 +
   1.106 +    msg->from = identity_dup(from);
   1.107 +    assert(msg->from);
   1.108 +    if (msg->from == NULL) {
   1.109 +        free_message(msg);
   1.110 +        return NULL;
   1.111 +    }
   1.112 +
   1.113 +    if (dir == dir_incoming) {
   1.114 +        msg->recv_by = identity_dup(to);
   1.115 +        assert(msg->recv_by);
   1.116 +        if (msg->recv_by == NULL) {
   1.117 +            free_message(msg);
   1.118 +            return NULL;
   1.119 +        }
   1.120 +    }
   1.121 +
   1.122 +    msg->to = new_identity_list(to);
   1.123 +    assert(msg->to);
   1.124 +    if (msg->to == NULL) {
   1.125 +        free_message(msg);
   1.126 +        return NULL;
   1.127 +    }
   1.128 +
   1.129 +    return msg;
   1.130 +}
   1.131 +
   1.132 +void free_message(message *msg)
   1.133 +{
   1.134 +    free(msg->id);
   1.135 +    free(msg->shortmsg);
   1.136 +    free(msg->longmsg);
   1.137 +    free(msg->longmsg_formatted);
   1.138 +    free(msg->rawmsg);
   1.139 +    free_identity_list(msg->to);
   1.140 +    free_identity_list(msg->cc);
   1.141 +    free_identity_list(msg->bcc);
   1.142 +    free(msg->refering_id);
   1.143 +    free_message_ref_list(msg->refered_by);
   1.144 +    free(msg);
   1.145 +}
   1.146 +
   1.147 +message_ref_list *new_message_ref_list(message *msg)
   1.148 +{
   1.149 +    message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
   1.150 +    assert(msg_list);
   1.151 +    if (msg_list == NULL)
   1.152 +        return NULL;
   1.153 +
   1.154 +    msg_list->msg_ref = msg;
   1.155 +
   1.156 +    return msg_list;
   1.157 +}
   1.158 +
   1.159 +void free_message_ref_list(message_ref_list *msg_list)
   1.160 +{
   1.161 +    if (msg_list) {
   1.162 +        free_message_ref_list(msg_list->next);
   1.163 +        free(msg_list);
   1.164 +    }
   1.165 +}
   1.166 +
   1.167 +message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
   1.168 +{
   1.169 +    assert(msg_list);
   1.170 +    assert(msg);
   1.171 +
   1.172 +    if (msg_list->msg_ref == NULL) {
   1.173 +        msg_list->msg_ref = msg;
   1.174 +        return msg_list;
   1.175 +    }
   1.176 +    else if (msg_list->next == NULL) {
   1.177 +        msg_list->next = new_message_ref_list(msg);
   1.178 +        assert(msg_list->next);
   1.179 +        return msg_list->next;
   1.180 +    }
   1.181 +    else {
   1.182 +        return message_ref_list_add(msg_list->next, msg);
   1.183 +    }
   1.184 +}
   1.185 +
     2.1 --- a/src/transport.h	Mon Sep 01 20:56:05 2014 +0200
     2.2 +++ b/src/transport.h	Tue Sep 09 13:00:10 2014 +0200
     2.3 @@ -1,6 +1,10 @@
     2.4  #pragma once
     2.5  
     2.6  #include "pEpEngine.h"
     2.7 +#include <time.h>
     2.8 +#include <stdlib.h>
     2.9 +
    2.10 +typedef struct tm timestamp;
    2.11  
    2.12  typedef enum _PEP_transports {
    2.13      PEP_trans_auto = 0,
    2.14 @@ -12,8 +16,75 @@
    2.15  
    2.16  typedef struct _PEP_transport_t PEP_transport_t;
    2.17  
    2.18 -typedef PEP_STATUS (*sendto_t)(PEP_SESSION session, const pEp_identity *address, const char *shortmsg, const char *longmsg, const char *longmsg_formatted);
    2.19 -typedef PEP_STATUS (*readnext_t)(PEP_SESSION session, pEp_identity *from, pEp_identity *reached, char **shortmsg, size_t shortmsg_size, char ** longmsg, size_t longmsg_size, char ** longmsg_formatted, size_t longmsg_formatted_size, PEP_transport_t **via);
    2.20 +pEp_identity *identity_dup(const pEp_identity *src);
    2.21 +
    2.22 +typedef struct _identity_list {
    2.23 +    pEp_identity *ident;
    2.24 +    struct _identity_list *next;
    2.25 +} identity_list;
    2.26 +
    2.27 +identity_list *new_identity_list(const pEp_identity *ident);
    2.28 +void free_identity_list(identity_list *id_list);
    2.29 +identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident);
    2.30 +
    2.31 +typedef enum _msg_format {
    2.32 +    format_plain = 0,
    2.33 +    format_html
    2.34 +} msg_format;
    2.35 +
    2.36 +typedef enum _msg_direction {
    2.37 +    dir_incoming = 0,
    2.38 +    dir_outgoing
    2.39 +} msg_direction;
    2.40 +
    2.41 +struct _message_ref_list;
    2.42 +
    2.43 +typedef struct _message {
    2.44 +    msg_direction dir;
    2.45 +    char * id;
    2.46 +    size_t id_size;
    2.47 +    char * shortmsg;
    2.48 +    size_t shortmsg_size;
    2.49 +    char * longmsg;
    2.50 +    size_t longmsg_size;
    2.51 +    char * longmsg_formatted;
    2.52 +    size_t longmsg_formatted_size;
    2.53 +    msg_format format;
    2.54 +    char * rawmsg;
    2.55 +    size_t rawmsg_size;
    2.56 +    timestamp sent;
    2.57 +    timestamp recv;
    2.58 +    pEp_identity *from;
    2.59 +    identity_list *to;
    2.60 +    pEp_identity *recv_by;
    2.61 +    identity_list *cc;
    2.62 +    identity_list *bcc;
    2.63 +    char * refering_id;
    2.64 +    size_t refering_id_size;
    2.65 +    struct _message *refering_msg;
    2.66 +    struct _message_ref_list *refered_by;
    2.67 +} message;
    2.68 +
    2.69 +typedef struct _message_ref_list {
    2.70 +    message *msg_ref;
    2.71 +    struct _message_ref_list *next;
    2.72 +} message_ref_list;
    2.73 +
    2.74 +message *new_message(
    2.75 +        msg_direction dir,
    2.76 +        const pEp_identity *from,
    2.77 +        const pEp_identity *to,
    2.78 +        const char *shortmsg
    2.79 +    );
    2.80 +
    2.81 +void free_message(message *msg);
    2.82 +
    2.83 +message_ref_list *new_message_ref_list(message *msg);
    2.84 +void free_message_ref_list(message_ref_list *msg_list);
    2.85 +message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg);
    2.86 +
    2.87 +typedef PEP_STATUS (*sendto_t)(PEP_SESSION session, const message *msg);
    2.88 +typedef PEP_STATUS (*readnext_t)(PEP_SESSION session, message **msg, PEP_transport_t **via);
    2.89  
    2.90  struct _PEP_transport_t {
    2.91      uint8_t id;