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