pEpObjCAdapter/PEPIdentity.m
author Dirk Zimmermann <dz@pep.security>
Mon, 13 Jan 2020 13:46:19 +0100
branchIOSAD-164
changeset 1395 d07913ef56ee
parent 1198 ab5c05c35481
child 1396 dd79b44a1352
permissions -rw-r--r--
IOSAD-164 Reset flags, too
dirk@304
     1
//
dirk@304
     2
//  PEPIdentity.m
dirk@304
     3
//  pEpObjCAdapter
dirk@304
     4
//
dirk@304
     5
//  Created by Dirk Zimmermann on 30.10.17.
dirk@304
     6
//  Copyright © 2017 p≡p. All rights reserved.
dirk@304
     7
//
dirk@304
     8
dz@902
     9
#import "PEPIdentity.h"
dz@902
    10
#import "PEPConstants.h"
dirk@304
    11
dirk@305
    12
#import "pEpEngine.h"
dirk@367
    13
#import "PEPMessageUtil.h"
dirk@497
    14
#import "PEPSession.h"
dirk@305
    15
dirk@594
    16
#import "NSObject+Extension.h"
dirk@594
    17
dirk@304
    18
@implementation PEPIdentity
dirk@304
    19
dirk@305
    20
- (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
dirk@305
    21
                                 userID:(NSString * _Nullable)userID
dirk@305
    22
                               userName:(NSString * _Nullable)userName
dirk@356
    23
                                  isOwn:(BOOL)isOwn
dirk@305
    24
                            fingerPrint:(NSString * _Nullable)fingerPrint
dz@833
    25
                               commType:(PEPCommType)commType
dirk@305
    26
                               language:(NSString * _Nullable)language {
dirk@304
    27
    if (self = [super init]) {
dirk@304
    28
        self.address = address;
dirk@305
    29
        self.userID = userID;
dirk@304
    30
        self.userName = userName;
dirk@356
    31
        self.isOwn = isOwn;
dirk@305
    32
        self.fingerPrint = fingerPrint;
dirk@305
    33
        self.commType = commType;
dirk@305
    34
        self.language = language;
dirk@304
    35
    }
dirk@304
    36
    return self;
dirk@304
    37
}
dirk@304
    38
dirk@305
    39
- (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
dirk@312
    40
                                 userID:(NSString * _Nullable)userID
dirk@312
    41
                               userName:(NSString * _Nullable)userName
dirk@356
    42
                                  isOwn:(BOOL)isOwn
dirk@312
    43
                            fingerPrint:(NSString * _Nullable)fingerPrint
dirk@312
    44
{
dirk@356
    45
    return [self initWithAddress:address userID:userID userName:userName isOwn:isOwn
dz@988
    46
                     fingerPrint:fingerPrint commType:PEPCommTypeUnknown language:nil];
dirk@312
    47
}
dirk@312
    48
dirk@314
    49
- (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
dirk@316
    50
                                 userID:(NSString * _Nullable)userID
dirk@316
    51
                               userName:(NSString * _Nullable)userName
dirk@356
    52
                                  isOwn:(BOOL)isOwn
dirk@316
    53
{
dirk@356
    54
    return [self initWithAddress:address userID:userID userName:userName
dz@988
    55
                           isOwn:isOwn fingerPrint:nil commType:PEPCommTypeUnknown language:nil];
dirk@316
    56
}
dirk@316
    57
dirk@316
    58
- (nonnull instancetype)initWithAddress:(NSString * _Nonnull)address
dirk@314
    59
{
dirk@356
    60
    return [self initWithAddress:address userID:nil userName:nil isOwn:NO fingerPrint:nil
dz@988
    61
                        commType:PEPCommTypeUnknown language:nil];
dirk@314
    62
}
dirk@314
    63
dirk@342
    64
- (nonnull instancetype)initWithDictionary:(NSDictionary *)dictionary
dirk@342
    65
{
dirk@342
    66
    return [self initWithAddress:dictionary[kPepAddress] userID:dictionary[kPepUserID]
dirk@342
    67
                        userName:dictionary[kPepUsername]
dirk@356
    68
                           isOwn:[dictionary[kPepIsOwnIdentity] boolValue]
dirk@342
    69
                     fingerPrint:dictionary[kPepFingerprint]
dirk@342
    70
                        commType:[dictionary[kPepCommType] intValue]
dirk@342
    71
                        language:dictionary[@"lang"]];
dirk@342
    72
}
dirk@342
    73
dirk@350
    74
- (nonnull instancetype)initWithIdentity:(PEPIdentity * _Nonnull)identity
dirk@350
    75
{
dirk@350
    76
    return [self initWithAddress:identity.address userID:identity.userID
dirk@350
    77
                        userName:identity.userName
dirk@356
    78
                           isOwn:identity.isOwn
dirk@350
    79
                     fingerPrint:identity.fingerPrint
dirk@350
    80
                        commType:identity.commType
dirk@350
    81
                        language:identity.language];
dirk@350
    82
}
dirk@350
    83
dirk@370
    84
// MARK: Faking directory
dirk@370
    85
dirk@347
    86
- (PEPDict * _Nonnull)dictionary
dirk@347
    87
{
dirk@347
    88
    // most adapter use should be ok.
dirk@347
    89
    return (PEPDict *) self;
dirk@347
    90
}
dirk@347
    91
dirk@349
    92
- (PEPMutableDict * _Nonnull)mutableDictionary
dirk@349
    93
{
dirk@349
    94
    // most adapter use should be ok.
dirk@349
    95
    return (PEPMutableDict *) self;
dirk@349
    96
}
dirk@349
    97
dirk@567
    98
- (NSNumber * _Nullable)isPEPUser:(PEPSession * _Nullable)session
dirk@567
    99
                            error:(NSError * _Nullable * _Nullable)error
dirk@497
   100
{
dirk@497
   101
    if (!session) {
dirk@497
   102
        session = [PEPSession new];
dirk@497
   103
    }
dirk@567
   104
    return [session isPEPUser:self error:error];
dirk@497
   105
}
dirk@497
   106
dirk@351
   107
- (BOOL)isConfirmed
dirk@351
   108
{
dirk@351
   109
    return self.commType & PEP_ct_confirmed;
dirk@351
   110
}
dirk@351
   111
dirk@313
   112
// MARK: - Equality
dirk@313
   113
dirk@594
   114
/**
dirk@594
   115
 The keys that should be used to decide `isEqual` and compute the `hash`.
dirk@594
   116
 */
dirk@594
   117
static NSArray *s_keys;
dirk@594
   118
dirk@594
   119
- (BOOL)isEqualToPEPIdentity:(PEPIdentity * _Nonnull)identity
dirk@304
   120
{
dirk@594
   121
    return [self isEqualToObject:identity basedOnKeys:s_keys];
dirk@304
   122
}
dirk@304
   123
dirk@304
   124
- (NSUInteger)hash
dirk@304
   125
{
dirk@594
   126
    return [self hashBasedOnKeys:s_keys];
dirk@594
   127
}
dirk@594
   128
dirk@594
   129
- (BOOL)isEqual:(id)object
dirk@594
   130
{
dirk@594
   131
    if (object == self) {
dirk@594
   132
        return YES;
dirk@594
   133
    }
dirk@594
   134
    if (!object || ![object isKindOfClass:[self class]]) {
dirk@594
   135
        return NO;
dirk@594
   136
    }
dirk@594
   137
dirk@594
   138
    return [self isEqualToPEPIdentity:object];
dirk@304
   139
}
dirk@304
   140
dirk@313
   141
// MARK: - NSKeyValueCoding
dirk@313
   142
dirk@313
   143
- (NSUInteger)comm_type
dirk@313
   144
{
dirk@313
   145
    return self.commType;
dirk@313
   146
}
dirk@313
   147
dz@988
   148
- (void)setComm_type:(PEPCommType)ct
dirk@313
   149
{
dirk@313
   150
    self.commType = ct;
dirk@313
   151
}
dirk@313
   152
dirk@313
   153
- (NSString *)fpr
dirk@313
   154
{
dirk@313
   155
    return self.fingerPrint;
dirk@313
   156
}
dirk@313
   157
dirk@313
   158
- (void)setFpr:(NSString *)fpr
dirk@313
   159
{
dirk@313
   160
    self.fingerPrint = fpr;
dirk@313
   161
}
dirk@313
   162
dirk@313
   163
- (NSString *)user_id
dirk@313
   164
{
dirk@313
   165
    return self.userID;
dirk@313
   166
}
dirk@313
   167
dirk@313
   168
- (void)setUser_id:(NSString *)uid
dirk@313
   169
{
dirk@313
   170
    self.userID = uid;
dirk@313
   171
}
dirk@313
   172
dirk@313
   173
- (NSString *)username
dirk@313
   174
{
dirk@313
   175
    return self.userName;
dirk@313
   176
}
dirk@313
   177
dirk@313
   178
- (void)setUsername:(NSString *)userName
dirk@313
   179
{
dirk@313
   180
    self.userName = userName;
dirk@313
   181
}
dirk@313
   182
dirk@313
   183
- (NSString *)lang
dirk@313
   184
{
dirk@313
   185
    return self.language;
dirk@313
   186
}
dirk@313
   187
dirk@313
   188
- (void)setLang:(NSString *)l
dirk@313
   189
{
dirk@313
   190
    self.language = l;
dirk@313
   191
}
dirk@313
   192
dirk@477
   193
- (void)reset
dirk@477
   194
{
dirk@477
   195
    self.commType = PEP_ct_unknown;
dirk@477
   196
    self.language = nil;
dirk@477
   197
    self.fingerPrint = nil;
dirk@477
   198
    self.userID = nil;
dirk@477
   199
    self.userName = nil;
dirk@477
   200
    self.isOwn = NO;
dz@1395
   201
    self.flags = 0;
dirk@477
   202
}
dirk@477
   203
dz@1198
   204
- (BOOL)enableKeySync:(NSError * _Nullable * _Nullable)error
dz@1193
   205
{
dz@1193
   206
    PEPSession *session = [PEPSession new];
dz@1193
   207
    return [session enableSyncForIdentity:self error:error];
dz@1193
   208
}
dz@1193
   209
dz@1198
   210
- (BOOL)disableKeySync:(NSError * _Nullable * _Nullable)error
dz@1194
   211
{
dz@1194
   212
    PEPSession *session = [PEPSession new];
dz@1194
   213
    return [session disableSyncForIdentity:self error:error];
dz@1194
   214
}
dz@1194
   215
dz@1198
   216
- (NSNumber * _Nullable)queryKeySyncEnabled:(NSError * _Nullable * _Nullable)error
dz@1195
   217
{
dz@1195
   218
    PEPSession *session = [PEPSession new];
dz@1195
   219
    return [session queryKeySyncEnabledForIdentity:self error:error];
dz@1195
   220
}
dz@1195
   221
dirk@354
   222
// MARK: - NSDictionary - Helpers
dirk@354
   223
dirk@354
   224
- (NSArray<NSArray<NSString *> *> *)keyValuePairs
dirk@354
   225
{
dirk@354
   226
    NSMutableArray *result = [@[ @[kPepAddress, self.address],
dirk@354
   227
                                 @[kPepCommType,
dirk@354
   228
                                   [NSNumber numberWithInteger:(NSInteger) self.commType]],
dirk@354
   229
                                 @[kPepIsOwnIdentity, [NSNumber numberWithBool:self.isOwn]]]
dirk@354
   230
                              mutableCopy];
dirk@354
   231
dirk@354
   232
    if (self.fingerPrint) {
dirk@354
   233
        [result addObject:@[kPepFingerprint, self.fingerPrint]];
dirk@354
   234
    }
dirk@354
   235
dirk@354
   236
    if (self.userID) {
dirk@354
   237
        [result addObject:@[kPepUserID, self.userID]];
dirk@354
   238
    }
dirk@354
   239
dirk@354
   240
    if (self.userName) {
dirk@354
   241
        [result addObject:@[kPepUsername, self.userName]];
dirk@354
   242
    }
dirk@354
   243
dirk@354
   244
    if (self.language) {
dirk@354
   245
        [result addObject:@[@"lang", self.language]];
dirk@354
   246
    }
dirk@354
   247
dirk@354
   248
    return result;
dirk@354
   249
}
dirk@354
   250
dirk@313
   251
// MARK: - NSDictionary
dirk@313
   252
dirk@313
   253
- (nullable id)objectForKey:(NSString *)key
dirk@313
   254
{
dirk@313
   255
    return [self valueForKey:key];
dirk@313
   256
}
dirk@313
   257
dirk@354
   258
- (NSInteger)count
dirk@354
   259
{
dirk@354
   260
    return [[self keyValuePairs] count];
dirk@354
   261
}
dirk@354
   262
dirk@354
   263
- (void)enumerateKeysAndObjectsUsingBlock:(void (^)(id key, id obj, BOOL *stop))block
dirk@354
   264
{
dirk@354
   265
    BOOL stop = NO;
dirk@354
   266
    NSArray *pairs = [self keyValuePairs];
dirk@354
   267
    for (NSArray *pair in pairs) {
dirk@354
   268
        block(pair[0], pair[1], &stop);
dirk@354
   269
        if (stop) {
dirk@354
   270
            break;
dirk@354
   271
        }
dirk@354
   272
    }
dirk@354
   273
}
dirk@354
   274
dirk@336
   275
// MARK: - NSMutableCopying
dirk@336
   276
dirk@336
   277
- (id)mutableCopyWithZone:(nullable NSZone *)zone
dirk@336
   278
{
dirk@336
   279
    return [[PEPIdentity alloc] initWithAddress:self.address userID:self.userID
dirk@356
   280
                                       userName:self.userName isOwn:self.isOwn
dirk@356
   281
                                    fingerPrint:self.fingerPrint
dirk@336
   282
                                       commType:self.commType language:self.language];
dirk@336
   283
}
dirk@336
   284
dirk@370
   285
// MARK: - Debug
dirk@370
   286
dirk@337
   287
- (NSString *)description
dirk@337
   288
{
dirk@337
   289
    return [NSString stringWithFormat:
dirk@357
   290
            @"<PEPIdentity %@ userID:%@ userName:%@ isOwn:%d fpr:%@ ct:%ld lang:%@>",
dirk@357
   291
            self.address, self.userID, self.userName, self.isOwn, self.fingerPrint,
dirk@337
   292
            (long) self.commType, self.language];
dirk@337
   293
}
dirk@337
   294
dirk@594
   295
// MARK: - Static Initialization
dirk@594
   296
dirk@594
   297
+ (void)initialize
dirk@594
   298
{
dirk@594
   299
    s_keys = @[@"address"];
dirk@594
   300
}
dirk@594
   301
dirk@304
   302
@end