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