diff --git a/Off the Record/OTRCodec.h b/Off the Record/OTRCodec.h index 1be1456c5..5a6bf49c4 100644 --- a/Off the Record/OTRCodec.h +++ b/Off the Record/OTRCodec.h @@ -27,7 +27,7 @@ @interface OTRCodec : NSObject -+ (void)decodeMessage:(OTRManagedMessage*)theMessage; ++ (void)decodeMessage:(OTRManagedMessage*)theMessage completionBlock:(void (^)(OTRManagedMessage * message))completionBlock; + (void)encodeMessage:(OTRManagedMessage*)theMessage completionBlock:(void (^)(OTRManagedMessage * message))completionBlock; + (void)generateOtrInitiateOrRefreshMessageTobuddy:(OTRManagedBuddy*)buddy diff --git a/Off the Record/OTRCodec.m b/Off the Record/OTRCodec.m index 0263ad603..747cbc383 100644 --- a/Off the Record/OTRCodec.m +++ b/Off the Record/OTRCodec.m @@ -29,28 +29,37 @@ @implementation OTRCodec -+(void) decodeMessage:(OTRManagedMessage*)theMessage; ++(void) decodeMessage:(OTRManagedMessage*)theMessage completionBlock:(void (^)(OTRManagedMessage *))completionBlock { NSString *message = theMessage.message; NSString *friendAccount = theMessage.buddy.accountName; NSString *protocol = theMessage.buddy.account.protocol; NSString *myAccountName = theMessage.buddy.account.username; + NSManagedObjectID *messageObjectID = theMessage.objectID; [[OTRKit sharedInstance] decodeMessage:message recipient:friendAccount accountName:myAccountName protocol:protocol completionBlock:^(NSString *decodedMessageString) { + NSError *error = nil; + OTRManagedMessage *localMessage = (OTRManagedMessage*)[[NSManagedObjectContext MR_contextForCurrentThread] existingObjectWithID:messageObjectID error:&error]; + if (error) { + DDLogError(@"Error fetching message: %@", error); + error = nil; + } if([decodedMessageString length]) { - theMessage.message = [OTRUtilities stripHTML:decodedMessageString]; - [theMessage setIsEncryptedValue:NO]; + localMessage.message = [OTRUtilities stripHTML:decodedMessageString]; + [localMessage setIsEncryptedValue:NO]; } else { - [theMessage setIsEncryptedValue:YES]; + [localMessage setIsEncryptedValue:YES]; } - + OTRKitMessageState messageState = [[OTRKit sharedInstance] messageStateForUsername:friendAccount accountName:myAccountName protocol:protocol]; + [localMessage.buddy setNewEncryptionStatus:messageState]; NSManagedObjectContext * context = [NSManagedObjectContext MR_contextForCurrentThread]; [context MR_saveToPersistentStoreAndWait]; - OTRKitMessageState messageState = [[OTRKit sharedInstance] messageStateForUsername:friendAccount accountName:myAccountName protocol:protocol]; - [theMessage.buddy setNewEncryptionStatus:messageState]; + if (completionBlock) { + completionBlock(localMessage); + } }]; } @@ -60,13 +69,26 @@ +(void)encodeMessage:(OTRManagedMessage *)theMessage completionBlock:(void (^)(O NSString *recipientAccount = theMessage.buddy.accountName; NSString *protocol = theMessage.buddy.account.protocol; NSString *sendingAccount = theMessage.buddy.account.username; + NSManagedObjectID *buddyObjectID = theMessage.buddy.objectID; + NSManagedObjectID *messageObjectID = theMessage.objectID; //theMessage.isEncryptedValue = NO; //NSString *encodedMessageString = [[OTRKit sharedInstance] encodeMessage:message recipient:recipientAccount accountName:sendingAccount protocol:protocol]; [[OTRKit sharedInstance] encodeMessage:message recipient:recipientAccount accountName:sendingAccount protocol:protocol completionBlock:^(NSString *message) { - OTRManagedMessage *newOTRMessage = [OTRManagedMessage newMessageToBuddy:theMessage.buddy message:message encrypted:YES]; - newOTRMessage.date = theMessage.date; - newOTRMessage.uniqueID = theMessage.uniqueID; + NSError *error = nil; + OTRManagedBuddy *localBuddy = (OTRManagedBuddy*)[[NSManagedObjectContext MR_contextForCurrentThread] existingObjectWithID:buddyObjectID error:&error]; + if (error) { + DDLogError(@"Error fetching buddy: %@", error); + error = nil; + } + OTRManagedMessage *localMessage = (OTRManagedMessage*)[[NSManagedObjectContext MR_contextForCurrentThread] existingObjectWithID:messageObjectID error:&error]; + if (error) { + DDLogError(@"Error fetching message: %@", error); + error = nil; + } + OTRManagedMessage *newOTRMessage = [OTRManagedMessage newMessageToBuddy:localBuddy message:message encrypted:YES]; + newOTRMessage.date = localMessage.date; + newOTRMessage.uniqueID = localMessage.uniqueID; NSManagedObjectContext * context = [NSManagedObjectContext MR_contextForCurrentThread]; [context MR_saveToPersistentStoreAndWait]; diff --git a/Off the Record/OTRConstants.h b/Off the Record/OTRConstants.h index e17cc5094..4cb0b0505 100644 --- a/Off the Record/OTRConstants.h +++ b/Off the Record/OTRConstants.h @@ -26,7 +26,6 @@ #define kOTRProtocolLoginFailSSLStatusKey @"kOTRProtocolLoginFailSSLStatusKey" #define kOTRProtocolLoginFailHostnameKey @"kOTRProtocolLoginFailHostnameKey" #define kOTRProtocolLoginFailSSLCertificateDataKey @"kOTRProtocolLoginFailSSLCertificateData" -#define kOTRBuddyListUpdate @"BuddyListUpdateNotification" #define kOTRMessageReceived @"MessageReceivedNotification" #define kOTRMessageReceiptResonseReceived @"MessageReceiptResponseNotification" #define kOTRStatusUpdate @"StatusUpdatedNotification" diff --git a/Off the Record/OTRManagedBuddy.h b/Off the Record/OTRManagedBuddy.h index 473639e31..1a1f70942 100644 --- a/Off the Record/OTRManagedBuddy.h +++ b/Off the Record/OTRManagedBuddy.h @@ -31,7 +31,6 @@ @interface OTRManagedBuddy : _OTRManagedBuddy --(void)receiveMessage:(NSString *)message; -(void)receiveChatStateMessage:(OTRChatState) chatState; -(BOOL)protocolIsXMPP; diff --git a/Off the Record/OTRManagedBuddy.m b/Off the Record/OTRManagedBuddy.m index 41be2af93..6b6c2016a 100644 --- a/Off the Record/OTRManagedBuddy.m +++ b/Off the Record/OTRManagedBuddy.m @@ -94,39 +94,6 @@ -(void)invalidateInactiveChatStateTimer OTRXMPPManager * protocol = (OTRXMPPManager *)[protocolManager protocolForAccount:self.account]; [[protocol inactiveChatStateTimerForBuddyObjectID:self.objectID] invalidate]; } - -} - --(void)receiveMessage:(NSString *)message -{ - //DDLogVerbose(@"received: %@",message); - if (message) { - self.lastMessageDisconnected = NO; - // Strip the shit out of it, but hopefully you're talking with someone who is trusted in the first place - // TODO: fix this so it doesn't break some cyrillic encodings - //NSString *rawMessage = [[[message stringByConvertingHTMLToPlainText]stringByEncodingHTMLEntities] stringByLinkifyingURLs]; - NSString * rawMessage = [message stringByConvertingHTMLToPlainText]; - - //[[NSNotificationCenter defaultCenter] postNotificationName:MESSAGE_PROCESSED_NOTIFICATION object:self]; - - if (![[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) - { - // We are not active, so use a local notification instead - UILocalNotification *localNotification = [[UILocalNotification alloc] init]; - localNotification.alertAction = REPLY_STRING; - localNotification.soundName = UILocalNotificationDefaultSoundName; - localNotification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; - localNotification.alertBody = [NSString stringWithFormat:@"%@: %@",self.displayName,rawMessage]; - - NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:3]; - [userInfo setObject:self.accountName forKey:kOTRNotificationUserNameKey]; - [userInfo setObject:self.account.username forKey:kOTRNotificationAccountNameKey]; - [userInfo setObject:self.account.protocol forKey:kOTRNotificationProtocolKey]; - localNotification.userInfo = userInfo; - - [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; - } - } } -(void)receiveChatStateMessage:(OTRChatState) newChatState diff --git a/Off the Record/OTRManagedMessage.h b/Off the Record/OTRManagedMessage.h index 128e0e664..770d1f674 100644 --- a/Off the Record/OTRManagedMessage.h +++ b/Off the Record/OTRManagedMessage.h @@ -29,6 +29,8 @@ @interface OTRManagedMessage : _OTRManagedMessage ++ (void) showLocalNotificationForMessage:(OTRManagedMessage*)message; + +(OTRManagedMessage*)newMessageFromBuddy:(OTRManagedBuddy *)theBuddy message:(NSString *)theMessage encrypted:(BOOL)encryptionStatus delayedDate:(NSDate *)date; +(OTRManagedMessage *)newMessageToBuddy:(OTRManagedBuddy *)theBuddy message:(NSString *)theMessage encrypted:(BOOL)encryptionStatus; diff --git a/Off the Record/OTRManagedMessage.m b/Off the Record/OTRManagedMessage.m index 87eb514df..17670e8c0 100644 --- a/Off the Record/OTRManagedMessage.m +++ b/Off the Record/OTRManagedMessage.m @@ -25,6 +25,9 @@ #import "OTRConstants.h" #import "OTRProtocolManager.h" #import "OTRUtilities.h" +#import "NSString+HTML.h" +#import "Strings.h" + @implementation OTRManagedMessage @@ -67,6 +70,35 @@ +(OTRManagedMessage*)newMessageFromBuddy:(OTRManagedBuddy *)theBuddy message:(NS } ++ (void) showLocalNotificationForMessage:(OTRManagedMessage*)message { + OTRManagedMessage *localMessage = [message MR_inThreadContext]; + if (localMessage.isEncryptedValue) { + DDLogWarn(@"Message was unable to be decrypted, not showing local notification"); + return; + } + localMessage.buddy.lastMessageDisconnected = NO; + NSString * rawMessage = [localMessage.message stringByConvertingHTMLToPlainText]; + [[NSManagedObjectContext MR_contextForCurrentThread] MR_saveToPersistentStoreAndWait]; + if (![[UIApplication sharedApplication] applicationState] == UIApplicationStateActive) + { + // We are not active, so use a local notification instead + UILocalNotification *localNotification = [[UILocalNotification alloc] init]; + localNotification.alertAction = REPLY_STRING; + localNotification.soundName = UILocalNotificationDefaultSoundName; + localNotification.applicationIconBadgeNumber = [UIApplication sharedApplication].applicationIconBadgeNumber + 1; + localNotification.alertBody = [NSString stringWithFormat:@"%@: %@",localMessage.buddy.displayName,rawMessage]; + + NSMutableDictionary *userInfo = [NSMutableDictionary dictionaryWithCapacity:3]; + [userInfo setObject:localMessage.buddy.accountName forKey:kOTRNotificationUserNameKey]; + [userInfo setObject:localMessage.buddy.account.username forKey:kOTRNotificationAccountNameKey]; + [userInfo setObject:localMessage.buddy.account.protocol forKey:kOTRNotificationProtocolKey]; + localNotification.userInfo = userInfo; + dispatch_async(dispatch_get_main_queue(), ^{ + [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification]; + }); + } +} + +(OTRManagedMessage *)newMessageToBuddy:(OTRManagedBuddy *)theBuddy message:(NSString *)theMessage encrypted:(BOOL)encryptionStatus { OTRManagedMessage *message = [OTRManagedMessage newMessageWithBuddy:theBuddy message:theMessage]; diff --git a/Off the Record/OTRXMPPManager.m b/Off the Record/OTRXMPPManager.m index a0243acf6..e37aa8826 100644 --- a/Off the Record/OTRXMPPManager.m +++ b/Off the Record/OTRXMPPManager.m @@ -514,13 +514,9 @@ - (void)xmppStream:(XMPPStream *)sender didReceiveMessage:(XMPPMessage *)message NSDate * date = [message delayedDeliveryDate]; OTRManagedMessage *otrMessage = [OTRManagedMessage newMessageFromBuddy:messageBuddy message:body encrypted:YES delayedDate:date]; - [OTRCodec decodeMessage:otrMessage]; - - if(otrMessage && !otrMessage.isEncryptedValue) - { - [messageBuddy receiveMessage:otrMessage.message]; - - } + [OTRCodec decodeMessage:otrMessage completionBlock:^(OTRManagedMessage *message) { + [OTRManagedMessage showLocalNotificationForMessage:message]; + }]; } } diff --git a/Off the Record/Off the Record-Prefix.pch b/Off the Record/Off the Record-Prefix.pch index 6609d74ea..584328b6f 100644 --- a/Off the Record/Off the Record-Prefix.pch +++ b/Off the Record/Off the Record-Prefix.pch @@ -20,6 +20,7 @@ #define MR_ENABLE_ACTIVE_RECORD_LOGGING 0 #else static int ddLogLevel = LOG_LEVEL_OFF; + #define MR_ENABLE_ACTIVE_RECORD_LOGGING 0 #endif #import "CoreData+MagicalRecord.h"