Skip to content

Commit

Permalink
Merge branch 'auto-otr' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Jul 12, 2016
2 parents e4ba4f9 + 8665b53 commit e83ebbb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 1 deletion.
10 changes: 9 additions & 1 deletion ChatSecure/Classes/Controllers/OTREncryptionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ typedef NS_ENUM(NSUInteger, OTREncryptionMessageState) {

@interface OTREncryptionManager:NSObject


@property (nonatomic, strong, readonly) OTRKit *otrKit;
@property (nonatomic, strong, readonly) OTRDataHandler *dataHandler;
@property (nonatomic, strong, readonly) OTRPushTLVHandler *pushTLVHandler;
Expand All @@ -63,6 +62,15 @@ typedef NS_ENUM(NSUInteger, OTREncryptionMessageState) {
completion:(void (^)(BOOL currentlyTrusted, BOOL hasTurstedFingerprints, OTRKitMessageState messageState))completionBlock
completionQueue:(dispatch_queue_t)queue;

/**
* This method takes a buddy key and collection. If it finds an object in the database and `hasGoneEncryptedBefore` is true
* It will try to initiate a new OTR session. This is useful when re-entering a converstaion with a buddy.
*
* @param buddyKey The Yap key for the buddy
* @param collection The Yap collection for the buddy
*/
- (void)maybeRefreshOTRSessionForBuddyKey:(NSString *)buddyKey collection:(NSString *)collection;

+ (BOOL) setFileProtection:(NSString*)fileProtection path:(NSString*)path;
+ (BOOL) addSkipBackupAttributeToItemAtURL:(NSURL *)URL;

Expand Down
33 changes: 33 additions & 0 deletions ChatSecure/Classes/Controllers/OTREncryptionManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,34 @@ - (void)currentEncryptionState:(NSString *)username accountName:(NSString *)acco
});
}

- (void)maybeRefreshOTRSessionForBuddyKey:(NSString *)buddyKey collection:(NSString *)collection {
__block OTRBuddy *buddy = nil;
__block OTRAccount *account = nil;
[[OTRDatabaseManager sharedInstance].readOnlyDatabaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction * _Nonnull transaction) {
id databaseObject = [transaction objectForKey:buddyKey inCollection:collection];
if ([databaseObject isKindOfClass:[OTRBuddy class]]) {
buddy = databaseObject;
account = [buddy accountWithTransaction:transaction];
}
} completionQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionBlock:^{

if (buddy.status == OTRThreadStatusOffline) {
//If the buddy if offline then don't try to start the session up
return;
}

[self.otrKit allFingerprintsForUsername:buddy.username accountName:account.username protocol:account.protocolTypeString completion:^(NSArray<NSString *> *activeFingerprint) {
if ([activeFingerprint count] > 0) {
[self.otrKit messageStateForUsername:buddy.username accountName:account.username protocol:account.protocolTypeString completion:^(OTRKitMessageState messageState) {
if (messageState != OTRKitMessageStateEncrypted) {
[self.otrKit initiateEncryptionWithUsername:buddy.username accountName:account.username protocol:account.protocolTypeString];
}
}];
}
}];
}];
}


#pragma mark OTRKitDelegate methods

Expand Down Expand Up @@ -270,6 +298,11 @@ - (void)otrKit:(OTRKit *)otrKit updateMessageState:(OTRKitMessageState)messageSt
[[OTRDatabaseManager sharedInstance].readWriteDatabaseConnection asyncReadWithBlock:^(YapDatabaseReadTransaction *transaction) {
buddy = [OTRBuddy fetchBuddyForUsername:username accountName:accountName transaction:transaction];
} completionBlock:^{
if(!buddy) {
// We couldn't find the budy. This is very strange and shouldn't happen.
return;
}

[[NSNotificationCenter defaultCenter] postNotificationName:OTRMessageStateDidChangeNotification object:buddy userInfo:@{OTRMessageStateKey:@([[self class] convertEncryptionState:messageState])}];
}];
}
Expand Down
3 changes: 3 additions & 0 deletions ChatSecure/Classes/Controllers/OTRSplitViewCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class OTRSplitViewCoordinator: NSObject, OTRConversationViewControllerDel
buddy = OTRBuddy.fetchObjectWithUniqueID(buddyKey, transaction: transaction)
}
if let b = buddy {
OTRProtocolManager.sharedInstance().encryptionManager.maybeRefreshOTRSessionForBuddyKey(b.threadIdentifier(), collection: b.threadCollection())
self.enterConversationWithThread(b, sender: nil)
}
}
Expand Down Expand Up @@ -70,6 +71,8 @@ public class OTRSplitViewCoordinator: NSObject, OTRConversationViewControllerDel
return
}

OTRProtocolManager.sharedInstance().encryptionManager.maybeRefreshOTRSessionForBuddyKey(threadOwner.threadIdentifier(), collection: threadOwner.threadCollection())

//Set nav controller root view controller to mVC and then show detail with nav controller

mVC.setThreadKey(threadOwner.threadIdentifier(), collection: threadOwner.threadCollection())
Expand Down

0 comments on commit e83ebbb

Please sign in to comment.