Adapter for libpepmime, offering ObjC API.
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.

217 lines
5.5 KiB

  1. //
  2. // PEPMessage+Engine.m
  3. // pEpiOSAdapter
  4. //
  5. // Created by Dirk Zimmermann on 21.10.20.
  6. // Copyright © 2020 pp. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. #import "PEPMessage+Engine.h"
  10. #import "pEp_string.h"
  11. #import "PEPMessage.h"
  12. #import "PEPIdentity+Engine.h"
  13. #import "NSArray+Engine.h"
  14. @implementation PEPMessage (Engine)
  15. + (instancetype _Nullable)fromStruct:(message * _Nullable)msg
  16. {
  17. if (!msg) {
  18. return nil;
  19. }
  20. PEPMessage *theMessage = [PEPMessage new];
  21. [theMessage overWriteFromStruct:msg];
  22. return theMessage;
  23. }
  24. - (message * _Nullable)toStruct
  25. {
  26. PEP_msg_direction dir = self.direction == PEPMsgDirectionIncoming ? PEP_dir_incoming : PEP_dir_outgoing;
  27. message *msg = new_message(dir);
  28. if (!msg) {
  29. return NULL;
  30. }
  31. if (self.messageID)
  32. msg->id = new_string([[self.messageID precomposedStringWithCanonicalMapping]
  33. UTF8String], 0);
  34. if (self.shortMessage)
  35. msg->shortmsg = new_string([[self.shortMessage
  36. precomposedStringWithCanonicalMapping] UTF8String], 0);
  37. if (self.sentDate)
  38. msg->sent = new_timestamp([self.sentDate timeIntervalSince1970]);
  39. if (self.receivedDate)
  40. msg->recv = new_timestamp([self.receivedDate timeIntervalSince1970]);
  41. if (self.from)
  42. msg->from = [self.from toStruct];
  43. if (self.to)
  44. msg->to = [self.to toIdentityList];
  45. if (self.receivedBy)
  46. msg->recv_by = [self.receivedBy toStruct];
  47. if (self.cc)
  48. msg->cc = [self.cc toIdentityList];
  49. if (self.bcc)
  50. msg->bcc = [self.bcc toIdentityList];
  51. if (self.replyTo)
  52. msg->reply_to = [self.replyTo toIdentityList];
  53. if (self.inReplyTo)
  54. msg->in_reply_to = [self.inReplyTo toStringList];
  55. if (self.references)
  56. msg->references = [self.references toStringList];
  57. if (self.keywords)
  58. msg->keywords = [self.keywords toStringList];
  59. if (self.optionalFields)
  60. msg->opt_fields = [self.optionalFields toStringPairlist];
  61. if (self.longMessage)
  62. msg->longmsg = new_string([[self.longMessage
  63. precomposedStringWithCanonicalMapping] UTF8String], 0);
  64. if (self.longMessageFormatted)
  65. msg->longmsg_formatted = new_string([[self.longMessageFormatted
  66. precomposedStringWithCanonicalMapping]
  67. UTF8String], 0);
  68. if (self.attachments) {
  69. msg->attachments = [self.attachments toBloblist];
  70. }
  71. return msg;
  72. }
  73. - (instancetype)removeEmptyRecipients
  74. {
  75. if (self.to.count == 0) {
  76. self.to = nil;
  77. }
  78. if (self.cc.count == 0) {
  79. self.cc = nil;
  80. }
  81. if (self.bcc.count == 0) {
  82. self.bcc = nil;
  83. }
  84. return self;
  85. }
  86. - (void)overWriteFromStruct:(message * _Nonnull)message
  87. {
  88. [self reset];
  89. self.direction = message->dir == PEP_dir_outgoing ? PEPMsgDirectionOutgoing : PEPMsgDirectionIncoming;
  90. if (message->id) {
  91. self.messageID = [NSString stringWithUTF8String:message->id];
  92. }
  93. if (message->shortmsg) {
  94. self.shortMessage = [NSString stringWithUTF8String:message->shortmsg];
  95. }
  96. if (message->sent) {
  97. self.sentDate = [NSDate dateWithTimeIntervalSince1970:timegm(message->sent)];
  98. }
  99. if (message->recv) {
  100. self.receivedDate = [NSDate dateWithTimeIntervalSince1970:mktime(message->recv)];
  101. }
  102. if (message->from) {
  103. self.from = [PEPIdentity fromStruct:message->from];
  104. }
  105. if (message->to && message->to->ident) {
  106. self.to = [NSArray arrayFromIdentityList:message->to];
  107. }
  108. if (message->recv_by) {
  109. self.receivedBy = [PEPIdentity fromStruct:message->recv_by];
  110. }
  111. if (message->cc && message->cc->ident) {
  112. self.cc = [NSArray arrayFromIdentityList:message->cc];
  113. }
  114. if (message->bcc && message->bcc->ident) {
  115. self.bcc = [NSArray arrayFromIdentityList:message->bcc];
  116. }
  117. if (message->reply_to && message->reply_to->ident) {
  118. self.replyTo = [NSArray arrayFromIdentityList:message->reply_to];
  119. }
  120. if (message->in_reply_to) {
  121. self.inReplyTo = [NSArray arrayFromStringlist:message->in_reply_to];
  122. }
  123. if (message->references && message->references->value) {
  124. self.references = [NSArray arrayFromStringlist:message->references];
  125. }
  126. if (message->keywords && message->keywords->value) {
  127. self.keywords = [NSArray arrayFromStringlist:message->keywords];
  128. }
  129. if (message->opt_fields) {
  130. self.optionalFields = [NSArray arrayFromStringPairlist:message->opt_fields];
  131. }
  132. if (message->longmsg_formatted) {
  133. self.longMessageFormatted = [NSString stringWithUTF8String:message->longmsg_formatted];
  134. }
  135. if (message->longmsg) {
  136. self.longMessage = [NSString stringWithUTF8String:message->longmsg];
  137. }
  138. if (message->attachments && message->attachments->value) {
  139. self.attachments = [NSArray arrayFromBloblist:message->attachments];
  140. }
  141. }
  142. // MARK: - Private
  143. - (void)reset
  144. {
  145. self.messageID = nil;
  146. self.from = nil;
  147. self.to = nil;
  148. self.cc = nil;
  149. self.bcc = nil;
  150. self.shortMessage = nil;
  151. self.longMessage = nil;
  152. self.longMessageFormatted = nil;
  153. self.replyTo = nil;
  154. self.inReplyTo = nil;
  155. self.references = nil;
  156. self.sentDate = nil;
  157. self.receivedDate = nil;
  158. self.attachments = nil;
  159. self.optionalFields = nil;
  160. self.keywords = nil;
  161. self.receivedBy = nil;
  162. self.direction = PEP_dir_incoming; // basically, 0
  163. }
  164. @end