You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

199 lines
4.4 KiB

8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
8 years ago
  1. #include "pEp_internal.h"
  2. #include <memory.h>
  3. #include <assert.h>
  4. PEP_STATUS init_transport_system(PEP_SESSION session)
  5. {
  6. assert(session);
  7. pEpSession *_session = (pEpSession *) session;
  8. PEP_transport_t* transports = _session->transports;
  9. assert(PEP_trans__count == 1);
  10. memset(transports, 0, sizeof(PEP_transport_t) * PEP_trans__count);
  11. transports[0].id = PEP_trans_auto;
  12. return PEP_STATUS_OK;
  13. }
  14. void release_transport_system(PEP_SESSION session)
  15. {
  16. assert(session);
  17. // nothing yet
  18. }
  19. pEp_identity *identity_dup(const pEp_identity *src)
  20. {
  21. assert(src);
  22. pEp_identity *dup = new_identity(src->address, src->fpr, src->user_id, src->username);
  23. assert(dup);
  24. if (dup == NULL)
  25. return NULL;
  26. dup->address_size = strlen(dup->address);
  27. dup->fpr_size = strlen(dup->fpr);
  28. dup->user_id_size = strlen(dup->user_id);
  29. dup->username_size = strlen(dup->username);
  30. dup->comm_type = src->comm_type;
  31. dup->lang[0] = src->lang[0];
  32. dup->lang[1] = src->lang[1];
  33. dup->lang[2] = 0;
  34. dup->me = src->me;
  35. }
  36. identity_list *new_identity_list(const pEp_identity *ident)
  37. {
  38. identity_list *id_list = calloc(1, sizeof(identity_list));
  39. assert(id_list);
  40. if (id_list == NULL)
  41. return NULL;
  42. if (ident) {
  43. id_list->ident = identity_dup(ident);
  44. assert(id_list->ident);
  45. if (id_list->ident == NULL) {
  46. free(id_list);
  47. return NULL;
  48. }
  49. }
  50. return id_list;
  51. }
  52. void free_identity_list(identity_list *id_list)
  53. {
  54. if (id_list) {
  55. free_identity_list(id_list->next);
  56. free_identity(id_list->ident);
  57. free(id_list);
  58. }
  59. }
  60. identity_list *identity_list_add(identity_list *id_list, const pEp_identity *ident)
  61. {
  62. assert(id_list);
  63. assert(ident);
  64. if (id_list->ident == NULL) {
  65. id_list->ident = identity_dup(ident);
  66. assert(id_list->ident);
  67. if (id_list->ident == NULL)
  68. return NULL;
  69. else
  70. return id_list;
  71. }
  72. else if (id_list->next == NULL) {
  73. id_list->next = new_identity_list(ident);
  74. assert(id_list->next);
  75. return id_list->next;
  76. }
  77. else {
  78. return identity_list_add(id_list->next, ident);
  79. }
  80. }
  81. message *new_message(
  82. msg_direction dir,
  83. const pEp_identity *from,
  84. const pEp_identity *to,
  85. const char *shortmsg
  86. )
  87. {
  88. message *msg = calloc(1, sizeof(message));
  89. assert(msg);
  90. if (msg == NULL)
  91. return NULL;
  92. msg->shortmsg = strdup(shortmsg);
  93. assert(msg->shortmsg);
  94. if (msg->shortmsg == NULL) {
  95. free(msg);
  96. return NULL;
  97. }
  98. msg->shortmsg_size = strlen(msg->shortmsg);
  99. msg->dir = dir;
  100. msg->from = identity_dup(from);
  101. assert(msg->from);
  102. if (msg->from == NULL) {
  103. free_message(msg);
  104. return NULL;
  105. }
  106. if (dir == dir_incoming) {
  107. msg->recv_by = identity_dup(to);
  108. assert(msg->recv_by);
  109. if (msg->recv_by == NULL) {
  110. free_message(msg);
  111. return NULL;
  112. }
  113. }
  114. msg->to = new_identity_list(to);
  115. assert(msg->to);
  116. if (msg->to == NULL) {
  117. free_message(msg);
  118. return NULL;
  119. }
  120. return msg;
  121. }
  122. void free_message(message *msg)
  123. {
  124. free(msg->id);
  125. free(msg->shortmsg);
  126. free(msg->longmsg);
  127. free(msg->longmsg_formatted);
  128. free(msg->rawmsg);
  129. free_identity_list(msg->to);
  130. free_identity_list(msg->cc);
  131. free_identity_list(msg->bcc);
  132. free(msg->refering_id);
  133. free_message_ref_list(msg->refered_by);
  134. free(msg);
  135. }
  136. message_ref_list *new_message_ref_list(message *msg)
  137. {
  138. message_ref_list *msg_list = calloc(1, sizeof(message_ref_list));
  139. assert(msg_list);
  140. if (msg_list == NULL)
  141. return NULL;
  142. msg_list->msg_ref = msg;
  143. return msg_list;
  144. }
  145. void free_message_ref_list(message_ref_list *msg_list)
  146. {
  147. if (msg_list) {
  148. free_message_ref_list(msg_list->next);
  149. free(msg_list);
  150. }
  151. }
  152. message_ref_list *message_ref_list_add(message_ref_list *msg_list, message *msg)
  153. {
  154. assert(msg_list);
  155. assert(msg);
  156. if (msg_list->msg_ref == NULL) {
  157. msg_list->msg_ref = msg;
  158. return msg_list;
  159. }
  160. else if (msg_list->next == NULL) {
  161. msg_list->next = new_message_ref_list(msg);
  162. assert(msg_list->next);
  163. return msg_list->next;
  164. }
  165. else {
  166. return message_ref_list_add(msg_list->next, msg);
  167. }
  168. }