pEpObjCAdapter/PEPMessage.m
author Dirk Zimmermann <dz@pep.security>
Wed, 20 Mar 2019 16:40:35 +0100
branchIOS-1480
changeset 902 32760267cd59
parent 851 e62e76f93075
child 1053 6f79c7c8dcf1
permissions -rw-r--r--
IOS-1480 Avoid framework imports.
     1 //
     2 //  PEPMessage.m
     3 //  pEpObjCAdapter
     4 //
     5 //  Created by Dirk Zimmermann on 10.11.17.
     6 //  Copyright © 2017 p≡p. All rights reserved.
     7 //
     8 
     9 #import "PEPMessage.h"
    10 
    11 #import "PEPIdentity.h"
    12 #import "PEPConstants.h"
    13 
    14 #import "NSObject+Extension.h"
    15 #import "NSMutableDictionary+PEP.h"
    16 #import "PEPMessageUtil.h"
    17 
    18 @implementation PEPMessage
    19 
    20 - (instancetype _Nonnull)initWithDictionary:(PEPDict *)dict
    21 {
    22     self = [super init];
    23     if (self) {
    24         self.messageID = [dict objectForKey:kPepID];
    25         self.from = [dict objectForKey:kPepFrom];
    26         self.to = [dict objectForKey:kPepTo];
    27         self.cc = [dict objectForKey:kPepCC];
    28         self.bcc = [dict objectForKey:kPepBCC];
    29         self.shortMessage = [dict objectForKey:kPepShortMessage];
    30         self.longMessage = [dict objectForKey:kPepLongMessage];
    31         self.longMessageFormatted = [dict objectForKey:kPepLongMessageFormatted];
    32         self.replyTo = [dict objectForKey:kPepReplyTo];
    33         self.inReplyTo = [dict objectForKey:kPepInReplyTo];
    34         self.references = [dict objectForKey:kPepReferences];
    35         self.sentDate = [dict objectForKey:kPepSent];
    36         self.receivedDate = [dict objectForKey:kPepReceived];
    37         self.attachments = [dict objectForKey:kPepAttachments];
    38         self.optionalFields = [dict objectForKey:kPepOptFields];
    39         self.keywords = [dict objectForKey:kPepKeywords];
    40         self.receivedBy = [dict objectForKey:kPepReceivedBy];
    41 
    42         NSNumber *boolNum = [dict objectForKey:kPepOutgoing];
    43         if (boolNum.boolValue) {
    44             self.direction = PEP_dir_outgoing;
    45         } else {
    46             self.direction = PEP_dir_incoming;
    47         }
    48     }
    49     return self;
    50 }
    51 
    52 // MARK: - NSKeyValueCoding
    53 
    54 - (BOOL)outgoing
    55 {
    56     return self.direction == PEP_dir_outgoing;
    57 }
    58 
    59 - (void)setOutgoing:(BOOL)outgoing
    60 {
    61     self.direction = outgoing ? PEP_dir_outgoing:PEP_dir_incoming;
    62 }
    63 
    64 - (NSString *)id
    65 {
    66     return self.messageID;
    67 }
    68 
    69 - (void)setId:(NSString *)theID
    70 {
    71     self.messageID = theID;
    72 }
    73 
    74 - (NSString *)shortmsg
    75 {
    76     return self.shortMessage;
    77 }
    78 
    79 - (void)setShortmsg:(NSString *)shortMsg
    80 {
    81     self.shortMessage = shortMsg;
    82 }
    83 
    84 - (NSDate *)sent
    85 {
    86     return self.sentDate;
    87 }
    88 
    89 - (void)setSent:(NSDate *)sentDate
    90 {
    91     self.sentDate = sentDate;
    92 }
    93 
    94 - (NSDate *)recv
    95 {
    96     return self.receivedDate;
    97 }
    98 
    99 - (void)setRecv:(NSDate *)receivedDate
   100 {
   101     self.receivedDate = receivedDate;
   102 }
   103 
   104 - (PEPIdentity *)recv_by
   105 {
   106     return self.receivedBy;
   107 }
   108 
   109 - (void)setRecv_by:(PEPIdentity *)receivedBy
   110 {
   111     self.receivedBy = receivedBy;
   112 }
   113 
   114 - (NSArray *)reply_to
   115 {
   116     return self.replyTo;
   117 }
   118 
   119 - (void)setReply_to:(NSArray *)replyTo
   120 {
   121     self.replyTo = replyTo;
   122 }
   123 
   124 - (NSArray *)in_reply_to
   125 {
   126     return self.inReplyTo;
   127 }
   128 
   129 - (void)setIn_reply_to:(NSArray *)inReplyTo
   130 {
   131     self.inReplyTo = inReplyTo;
   132 }
   133 
   134 - (NSArray *)opt_fields
   135 {
   136     return self.optionalFields;
   137 }
   138 
   139 - (void)setOpt_fields:(NSArray *)optFields
   140 {
   141     self.optionalFields = optFields;
   142 }
   143 
   144 - (NSString *)longmsg
   145 {
   146     return self.longMessage;
   147 }
   148 
   149 - (void)setLongmsg:(NSString *)longMsg
   150 {
   151     self.longMessage = longMsg;
   152 }
   153 
   154 - (NSString *)longmsg_formatted
   155 {
   156     return self.longMessageFormatted;
   157 }
   158 
   159 - (void)setLongmsg_formatted:(NSString *)longMsgFormatted
   160 {
   161     self.longMessageFormatted = longMsgFormatted;
   162 }
   163 
   164 // MARK: - Faking directory
   165 
   166 - (PEPDict * _Nonnull)dictionary
   167 {
   168     // most adapter use should be ok.
   169     return (PEPDict *) self;
   170 }
   171 
   172 - (PEPMutableDict * _Nonnull)mutableDictionary
   173 {
   174     // most adapter use should be ok.
   175     return (PEPMutableDict *) self;
   176 }
   177 
   178 - (void)removeAllObjects
   179 {
   180     self.messageID = nil;
   181     self.from = nil;
   182     self.to = nil;
   183     self.cc = nil;
   184     self.bcc = nil;
   185     self.shortMessage = nil;
   186     self.longMessage = nil;
   187     self.longMessageFormatted = nil;
   188     self.replyTo = nil;
   189     self.inReplyTo = nil;
   190     self.references = nil;
   191     self.sentDate = nil;
   192     self.receivedDate = nil;
   193     self.attachments = nil;
   194     self.optionalFields = nil;
   195     self.keywords = nil;
   196     self.receivedBy = nil;
   197     self.direction = PEP_dir_incoming; // basically, 0
   198 }
   199 
   200 // MARK: - Faking the pEp directory extension
   201 
   202 - (void)replaceWithMessage:(message *)message
   203 {
   204     replaceDictionaryContentsWithMessage(self.mutableDictionary, message);
   205 }
   206 
   207 // MARK: - NSDictionary - Helpers
   208 
   209 - (NSArray<NSArray<NSString *> *> *)keyValuePairs
   210 {
   211     NSMutableArray *result = [NSMutableArray new];
   212 
   213     if (self.from) {
   214         [result addObject:@[kPepFrom, self.from]];
   215     }
   216 
   217     if (self.to) {
   218         [result addObject:@[kPepTo, self.to]];
   219     }
   220 
   221     if (self.cc) {
   222         [result addObject:@[kPepCC, self.cc]];
   223     }
   224 
   225     if (self.shortMessage) {
   226         [result addObject:@[kPepShortMessage, self.shortMessage]];
   227     }
   228 
   229     if (self.longMessage) {
   230         [result addObject:@[kPepLongMessage, self.longMessage]];
   231     }
   232 
   233     if (self.longMessageFormatted) {
   234         [result addObject:@[kPepLongMessageFormatted, self.longMessageFormatted]];
   235     }
   236 
   237     BOOL outgoing = self.direction == PEP_dir_outgoing ? YES:NO;
   238     [result addObject:@[kPepOutgoing, [NSNumber numberWithBool:outgoing]]];
   239 
   240     return result;
   241 }
   242 
   243 // MARK: - NSDictionary
   244 
   245 - (nullable id)objectForKey:(NSString *)key
   246 {
   247     return [self valueForKey:key];
   248 }
   249 
   250 - (void)removeObjectForKey:(NSString *)key
   251 {
   252     if ([@"bcc" isEqualToString:key]) {
   253         self.bcc = nil;
   254     } else if ([@"cc" isEqualToString:key]) {
   255         self.cc = nil;
   256     } else if ([@"to" isEqualToString:key]) {
   257         self.to = nil;
   258     } else {
   259         NSAssert1(false, @"Unsupported key for removeObjectForKey: |%@|",  key);
   260     }
   261 }
   262 
   263 - (NSInteger)count
   264 {
   265     return [[self keyValuePairs] count];
   266 }
   267 
   268 - (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block
   269 {
   270     BOOL stop = NO;
   271     NSArray *pairs = [self keyValuePairs];
   272     for (NSArray *pair in pairs) {
   273         block(pair[0], pair[1], &stop);
   274         if (stop) {
   275             break;
   276         }
   277     }
   278 }
   279 
   280 // MARK: - NSMutableCopying
   281 
   282 - (id)mutableCopyWithZone:(nullable NSZone *)zone
   283 {
   284     PEPMessage *newMessage = [PEPMessage new];
   285     newMessage.messageID = self.messageID;
   286     newMessage.from = self.from;
   287     newMessage.to = self.to;
   288     newMessage.cc = self.cc;
   289     newMessage.bcc = self.bcc;
   290     newMessage.shortMessage = self.shortMessage;
   291     newMessage.longMessage = self.longMessage;
   292     newMessage.longMessageFormatted = self.longMessageFormatted;
   293     newMessage.replyTo = self.replyTo;
   294     newMessage.inReplyTo = self.inReplyTo;
   295     newMessage.references = self.references;
   296     newMessage.sentDate = self.sentDate;
   297     newMessage.receivedDate = self.receivedDate;
   298     newMessage.attachments = self.attachments;
   299     newMessage.optionalFields = self.optionalFields;
   300     newMessage.keywords = self.keywords;
   301     newMessage.receivedBy = self.receivedBy;
   302     newMessage.direction = self.direction;
   303     return newMessage;
   304 }
   305 
   306 // MARK: - Equality
   307 
   308 /**
   309  The keys that should be used to decide `isEqual` and compute the `hash`.
   310  */
   311 static NSArray *s_keys;
   312 
   313 - (BOOL)isEqualToPEPMessage:(PEPMessage * _Nonnull)message
   314 {
   315     return [self isEqualToObject:message basedOnKeys:s_keys];
   316 }
   317 
   318 - (NSUInteger)hash
   319 {
   320     return [self hashBasedOnKeys:s_keys];
   321 }
   322 
   323 - (BOOL)isEqual:(id)object
   324 {
   325     if (object == self) {
   326         return YES;
   327     }
   328     if (!object || ![object isKindOfClass:[self class]]) {
   329         return NO;
   330     }
   331 
   332     return [self isEqualToPEPMessage:object];
   333 }
   334 
   335 // MARK: - Static Initialization
   336 
   337 + (void)initialize
   338 {
   339     s_keys = @[
   340                @"attachments",
   341                @"bcc",
   342                @"cc",
   343                @"direction",
   344                @"from",
   345                @"inReplyTo",
   346                @"keywords",
   347                @"longMessage",
   348                @"longMessageFormatted",
   349                @"messageID",
   350                @"optionalFields",
   351                @"receivedBy",
   352                @"receivedDate",
   353                @"references",
   354                @"replyTo",
   355                @"sentDate",
   356                @"shortMessage",
   357                @"to",
   358                ];
   359 }
   360 
   361 @end