2014-11-19 21:17:53 +01:00
|
|
|
//
|
|
|
|
// TSMessagesHandler.m
|
|
|
|
// TextSecureKit
|
|
|
|
//
|
|
|
|
// Created by Frederic Jacobs on 11/11/14.
|
|
|
|
// Copyright (c) 2014 Open Whisper Systems. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "TSMessagesManager.h"
|
|
|
|
|
2014-12-11 00:05:41 +01:00
|
|
|
#import <AxolotlKit/AxolotlExceptions.h>
|
2014-11-19 21:17:53 +01:00
|
|
|
#import <AxolotlKit/SessionCipher.h>
|
|
|
|
|
|
|
|
#import "Cryptography.h"
|
|
|
|
#import "IncomingPushMessageSignal.pb.h"
|
|
|
|
#import "NSData+Base64.h"
|
|
|
|
|
2014-12-01 23:24:35 +01:00
|
|
|
#import "PushManager.h"
|
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
#import "TSIncomingMessage.h"
|
|
|
|
#import "TSErrorMessage.h"
|
2015-02-16 10:27:08 +01:00
|
|
|
#import "TSInvalidIdentityKeyReceivingErrorMessage.h"
|
2014-11-19 21:17:53 +01:00
|
|
|
#import "TSInfoMessage.h"
|
|
|
|
|
|
|
|
#import "TSStorageManager+keyingMaterial.h"
|
|
|
|
#import "TSStorageManager+IdentityKeyStore.h"
|
|
|
|
#import "TSStorageManager+SessionStore.h"
|
|
|
|
#import "TSStorageManager+SignedPreKeyStore.h"
|
|
|
|
#import "TSStorageManager+PreKeyStore.h"
|
|
|
|
#import "TSNetworkManager.h"
|
|
|
|
#import "TSSubmitMessageRequest.h"
|
2014-12-21 12:52:42 +01:00
|
|
|
#import "TSMessagesManager+attachments.h"
|
2014-12-24 02:25:10 +01:00
|
|
|
#import "TSAttachmentPointer.h"
|
2014-11-19 21:17:53 +01:00
|
|
|
|
2015-01-07 18:57:58 +01:00
|
|
|
#import "SignalKeyingStorage.h"
|
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
#import "NSData+messagePadding.h"
|
|
|
|
|
2014-12-08 21:16:18 +01:00
|
|
|
#import "Environment.h"
|
|
|
|
#import "PreferencesUtil.h"
|
2014-12-29 11:20:20 +01:00
|
|
|
#import "ContactsManager.h"
|
2014-12-08 21:16:18 +01:00
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
#import <CocoaLumberjack/DDLog.h>
|
2015-01-27 02:34:38 +01:00
|
|
|
#import <YapDatabase/YapDatabaseSecondaryIndex.h>
|
2014-11-19 21:17:53 +01:00
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
#define ddLogLevel LOG_LEVEL_VERBOSE
|
2014-11-19 21:17:53 +01:00
|
|
|
|
|
|
|
@implementation TSMessagesManager
|
|
|
|
|
|
|
|
+ (instancetype)sharedManager {
|
|
|
|
static TSMessagesManager *sharedMyManager = nil;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedMyManager = [[self alloc] init];
|
|
|
|
});
|
|
|
|
return sharedMyManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (instancetype)init{
|
|
|
|
self = [super init];
|
|
|
|
|
|
|
|
if (self) {
|
2014-11-21 14:38:37 +01:00
|
|
|
_dbConnection = [TSStorageManager sharedManager].newDatabaseConnection;
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
- (void)handleMessageSignal:(IncomingPushMessageSignal*)messageSignal{
|
2014-11-21 14:38:37 +01:00
|
|
|
@try {
|
|
|
|
switch (messageSignal.type) {
|
|
|
|
case IncomingPushMessageSignalTypeCiphertext:
|
|
|
|
[self handleSecureMessage:messageSignal];
|
|
|
|
break;
|
|
|
|
|
|
|
|
case IncomingPushMessageSignalTypePrekeyBundle:
|
|
|
|
[self handlePreKeyBundle:messageSignal];
|
|
|
|
break;
|
|
|
|
|
2014-11-26 16:24:23 +01:00
|
|
|
// Other messages are just dismissed for now.
|
2014-11-21 14:38:37 +01:00
|
|
|
|
|
|
|
case IncomingPushMessageSignalTypeKeyExchange:
|
|
|
|
DDLogWarn(@"Received Key Exchange Message, not supported");
|
|
|
|
break;
|
|
|
|
case IncomingPushMessageSignalTypePlaintext:
|
|
|
|
DDLogWarn(@"Received a plaintext message");
|
|
|
|
break;
|
|
|
|
case IncomingPushMessageSignalTypeReceipt:
|
|
|
|
DDLogInfo(@"Received a delivery receipt");
|
2014-11-26 16:24:23 +01:00
|
|
|
[self handleDeliveryReceipt:messageSignal];
|
2014-11-21 14:38:37 +01:00
|
|
|
break;
|
|
|
|
case IncomingPushMessageSignalTypeUnknown:
|
|
|
|
DDLogWarn(@"Received an unknown message type");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
DDLogWarn(@"Received an incorrectly formatted protocol buffer: %@", exception.debugDescription);
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-26 16:24:23 +01:00
|
|
|
- (void)handleDeliveryReceipt:(IncomingPushMessageSignal*)signal{
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2015-01-27 02:34:38 +01:00
|
|
|
TSInteraction *interaction = [TSInteraction interactionForTimestamp:signal.timestamp withTransaction:transaction];
|
|
|
|
if ([interaction isKindOfClass:[TSOutgoingMessage class]]) {
|
|
|
|
TSOutgoingMessage *outgoingMessage = (TSOutgoingMessage*)interaction;
|
|
|
|
outgoingMessage.messageState = TSOutgoingMessageStateDelivered;
|
|
|
|
|
|
|
|
[outgoingMessage saveWithTransaction:transaction];
|
2014-12-17 06:44:36 +01:00
|
|
|
}
|
2014-11-26 16:24:23 +01:00
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
- (void)handleSecureMessage:(IncomingPushMessageSignal*)secureMessage{
|
|
|
|
@synchronized(self){
|
|
|
|
TSStorageManager *storageManager = [TSStorageManager sharedManager];
|
|
|
|
NSString *recipientId = secureMessage.source;
|
2014-11-25 21:33:29 +01:00
|
|
|
int deviceId = (int) secureMessage.sourceDevice;
|
2014-11-19 21:17:53 +01:00
|
|
|
|
|
|
|
if (![storageManager containsSession:recipientId deviceId:deviceId]) {
|
2014-11-26 16:24:23 +01:00
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2015-01-27 02:34:38 +01:00
|
|
|
TSErrorMessage *errorMessage = [TSErrorMessage missingSessionWithSignal:secureMessage
|
|
|
|
withTransaction:transaction];
|
2014-11-26 16:24:23 +01:00
|
|
|
[errorMessage saveWithTransaction:transaction];
|
|
|
|
}];
|
2014-11-27 03:10:00 +01:00
|
|
|
return;
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
PushMessageContent *content;
|
2014-11-19 21:17:53 +01:00
|
|
|
|
|
|
|
@try {
|
2014-11-21 14:38:37 +01:00
|
|
|
|
|
|
|
WhisperMessage *message = [[WhisperMessage alloc] initWithData:secureMessage.message];
|
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
SessionCipher *cipher = [[SessionCipher alloc] initWithSessionStore:storageManager
|
|
|
|
preKeyStore:storageManager
|
|
|
|
signedPreKeyStore:storageManager
|
|
|
|
identityKeyStore:storageManager
|
|
|
|
recipientId:recipientId
|
|
|
|
deviceId:deviceId];
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
NSData *plaintext = [[cipher decrypt:message] removePadding];
|
|
|
|
|
|
|
|
content = [PushMessageContent parseFromData:plaintext];
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
[self processException:exception pushSignal:secureMessage];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self handleIncomingMessage:secureMessage withPushContent:content];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handlePreKeyBundle:(IncomingPushMessageSignal*)preKeyMessage{
|
|
|
|
@synchronized(self){
|
|
|
|
TSStorageManager *storageManager = [TSStorageManager sharedManager];
|
|
|
|
NSString *recipientId = preKeyMessage.source;
|
2014-11-25 21:33:29 +01:00
|
|
|
int deviceId = (int)preKeyMessage.sourceDevice;
|
2014-11-19 21:17:53 +01:00
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
PushMessageContent *content;
|
2014-11-19 21:17:53 +01:00
|
|
|
|
|
|
|
@try {
|
2014-11-21 14:38:37 +01:00
|
|
|
PreKeyWhisperMessage *message = [[PreKeyWhisperMessage alloc] initWithData:preKeyMessage.message];
|
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
SessionCipher *cipher = [[SessionCipher alloc] initWithSessionStore:storageManager
|
|
|
|
preKeyStore:storageManager
|
|
|
|
signedPreKeyStore:storageManager
|
|
|
|
identityKeyStore:storageManager
|
|
|
|
recipientId:recipientId
|
|
|
|
deviceId:deviceId];
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
NSData *plaintext = [[cipher decrypt:message] removePadding];
|
|
|
|
|
|
|
|
content = [PushMessageContent parseFromData:plaintext];
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
@catch (NSException *exception) {
|
|
|
|
[self processException:exception pushSignal:preKeyMessage];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
[self handleIncomingMessage:preKeyMessage withPushContent:content];
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleIncomingMessage:(IncomingPushMessageSignal*)incomingMessage withPushContent:(PushMessageContent*)content{
|
2014-12-29 17:40:42 +01:00
|
|
|
if(content.hasGroup) {
|
2014-12-24 02:25:10 +01:00
|
|
|
__block BOOL ignoreMessage = NO;
|
|
|
|
[self.dbConnection readWithBlock:^(YapDatabaseReadTransaction *transaction) {
|
2015-01-31 12:37:36 +01:00
|
|
|
TSGroupModel *emptyModelToFillOutId = [[TSGroupModel alloc] initWithTitle:nil memberIds:nil image:nil groupId:content.group.id associatedAttachmentId:nil]; // TODO refactor the TSGroupThread to just take in an ID (as it is all that it uses). Should not take in more than it uses
|
2014-12-24 02:25:10 +01:00
|
|
|
TSGroupThread *gThread = [TSGroupThread threadWithGroupModel:emptyModelToFillOutId transaction:transaction];
|
2015-01-03 23:23:11 +01:00
|
|
|
if(gThread==nil && content.group.type != PushMessageContentGroupContextTypeUpdate) {
|
2014-12-24 02:25:10 +01:00
|
|
|
ignoreMessage = YES;
|
|
|
|
}
|
|
|
|
}];
|
|
|
|
if(ignoreMessage) {
|
2015-01-03 23:23:11 +01:00
|
|
|
DDLogDebug(@"Received message from group that I left or don't know about, ignoring");
|
2014-12-24 02:25:10 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2014-11-19 21:17:53 +01:00
|
|
|
if ((content.flags & PushMessageContentFlagsEndSession) != 0) {
|
|
|
|
DDLogVerbose(@"Received end session message...");
|
|
|
|
[self handleEndSessionMessage:incomingMessage withContent:content];
|
2014-12-24 02:25:10 +01:00
|
|
|
}
|
2014-12-29 17:40:42 +01:00
|
|
|
else if (content.attachments.count > 0 || (content.hasGroup && content.group.type == PushMessageContentGroupContextTypeUpdate && content.group.hasAvatar)) {
|
2014-12-24 02:25:10 +01:00
|
|
|
DDLogVerbose(@"Received push media message (attachment) or group with an avatar...");
|
2014-11-19 21:17:53 +01:00
|
|
|
[self handleReceivedMediaMessage:incomingMessage withContent:content];
|
2014-12-24 02:25:10 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-12-17 06:44:36 +01:00
|
|
|
DDLogVerbose(@"Received individual push text message...");
|
2014-11-19 21:17:53 +01:00
|
|
|
[self handleReceivedTextMessage:incomingMessage withContent:content];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleEndSessionMessage:(IncomingPushMessageSignal*)message withContent:(PushMessageContent*)content{
|
2014-11-21 14:38:37 +01:00
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2014-12-24 02:25:10 +01:00
|
|
|
TSContactThread *thread = [TSContactThread getOrCreateThreadWithContactId:message.source transaction:transaction];
|
2014-11-21 14:38:37 +01:00
|
|
|
uint64_t timeStamp = message.timestamp;
|
|
|
|
|
|
|
|
if (thread){
|
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:timeStamp inThread:thread messageType:TSInfoMessageTypeSessionDidEnd] saveWithTransaction:transaction];
|
|
|
|
}
|
|
|
|
}];
|
2014-11-19 21:17:53 +01:00
|
|
|
|
|
|
|
[[TSStorageManager sharedManager] deleteAllSessionsForContact:message.source];
|
|
|
|
}
|
|
|
|
|
2014-12-18 00:00:10 +01:00
|
|
|
- (void)handleReceivedTextMessage:(IncomingPushMessageSignal*)message withContent:(PushMessageContent*)content{
|
2014-12-17 06:44:36 +01:00
|
|
|
[self handleReceivedMessage:message withContent:content attachments:content.attachments];
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
|
2015-01-07 18:57:58 +01:00
|
|
|
-(void)handleSendToMyself:(TSOutgoingMessage*)outgoingMessage {
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
TSContactThread *cThread = [TSContactThread getOrCreateThreadWithContactId:[SignalKeyingStorage.localNumber toE164] transaction:transaction];
|
|
|
|
[cThread saveWithTransaction:transaction];
|
2015-01-26 01:15:22 +01:00
|
|
|
TSIncomingMessage *incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:(outgoingMessage.timestamp + 1) inThread:cThread messageBody:outgoingMessage.body attachments:outgoingMessage.attachments];
|
2015-01-07 18:57:58 +01:00
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
|
2015-01-26 06:40:19 +01:00
|
|
|
- (void)handleReceivedMessage:(IncomingPushMessageSignal *)message withContent:(PushMessageContent *)content attachments:(NSArray *)attachments{
|
|
|
|
[self handleReceivedMessage:message withContent:content attachments:attachments completionBlock:nil];
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)handleReceivedMessage:(IncomingPushMessageSignal*)message withContent:(PushMessageContent*)content attachments:(NSArray*)attachments completionBlock:(void (^)(NSString* messageIdentifier))completionBlock {
|
2014-11-19 21:17:53 +01:00
|
|
|
uint64_t timeStamp = message.timestamp;
|
|
|
|
NSString *body = content.body;
|
|
|
|
NSData *groupId = content.hasGroup?content.group.id:nil;
|
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
TSIncomingMessage *incomingMessage;
|
|
|
|
TSThread *thread;
|
|
|
|
if (groupId) {
|
2015-02-28 17:49:46 +01:00
|
|
|
TSGroupModel *model = [[TSGroupModel alloc] initWithTitle:content.group.name memberIds:[[[NSSet setWithArray:content.group.members] allObjects] mutableCopy] image:nil groupId:content.group.id associatedAttachmentId:nil];
|
2014-12-24 02:25:10 +01:00
|
|
|
TSGroupThread *gThread = [TSGroupThread getOrCreateThreadWithGroupModel:model transaction:transaction];
|
|
|
|
[gThread saveWithTransaction:transaction];
|
2014-12-17 06:44:36 +01:00
|
|
|
if(content.group.type==PushMessageContentGroupContextTypeUpdate) {
|
2014-12-24 02:25:10 +01:00
|
|
|
if([attachments count]==1) {
|
|
|
|
NSString* avatarId = [attachments firstObject];
|
|
|
|
TSAttachment *avatar = [TSAttachment fetchObjectWithUniqueID:avatarId];
|
|
|
|
if ([avatar isKindOfClass:[TSAttachmentStream class]]) {
|
|
|
|
TSAttachmentStream *stream = (TSAttachmentStream*)avatar;
|
|
|
|
if ([stream isImage]) {
|
2015-01-31 12:37:36 +01:00
|
|
|
model.associatedAttachmentId = stream.uniqueId;
|
2014-12-24 02:25:10 +01:00
|
|
|
model.groupImage = [stream image];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString* updateGroupInfo = [gThread.groupModel getInfoStringAboutUpdateTo:model];
|
|
|
|
gThread.groupModel = model;
|
|
|
|
[gThread saveWithTransaction:transaction];
|
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:timeStamp inThread:gThread messageType:TSInfoMessageTypeGroupUpdate customMessage:updateGroupInfo] saveWithTransaction:transaction];
|
|
|
|
}
|
|
|
|
else if(content.group.type==PushMessageContentGroupContextTypeQuit) {
|
2014-12-29 11:20:20 +01:00
|
|
|
NSString *nameString = [[Environment.getCurrent contactsManager] nameStringForPhoneIdentifier:message.source];
|
2015-02-16 10:27:08 +01:00
|
|
|
|
2014-12-29 11:20:20 +01:00
|
|
|
if (!nameString) {
|
|
|
|
nameString = message.source;
|
|
|
|
}
|
2015-02-16 10:27:08 +01:00
|
|
|
|
2015-02-18 23:21:03 +01:00
|
|
|
NSString* updateGroupInfo = [NSString stringWithFormat:NSLocalizedString(@"GROUP_MEMBER_LEFT", @""),nameString];
|
2014-12-24 02:25:10 +01:00
|
|
|
NSMutableArray *newGroupMembers = [NSMutableArray arrayWithArray:gThread.groupModel.groupMemberIds];
|
|
|
|
[newGroupMembers removeObject:message.source];
|
|
|
|
gThread.groupModel.groupMemberIds = newGroupMembers;
|
2014-12-29 11:20:20 +01:00
|
|
|
|
2014-12-24 02:25:10 +01:00
|
|
|
[gThread saveWithTransaction:transaction];
|
|
|
|
[[[TSInfoMessage alloc] initWithTimestamp:timeStamp inThread:gThread messageType:TSInfoMessageTypeGroupUpdate customMessage:updateGroupInfo] saveWithTransaction:transaction];
|
2015-01-27 23:30:28 +01:00
|
|
|
} else {
|
2014-12-17 06:44:36 +01:00
|
|
|
incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timeStamp inThread:gThread authorId:message.source messageBody:body attachments:attachments];
|
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
|
|
|
}
|
2015-01-27 23:30:28 +01:00
|
|
|
|
2014-11-21 14:38:37 +01:00
|
|
|
thread = gThread;
|
2015-01-27 23:30:28 +01:00
|
|
|
|
|
|
|
} else{
|
2015-01-27 02:34:38 +01:00
|
|
|
TSContactThread *cThread = [TSContactThread getOrCreateThreadWithContactId:message.source
|
2015-02-17 00:14:50 +01:00
|
|
|
transaction:transaction
|
|
|
|
pushSignal:message];
|
2015-03-03 01:33:33 +01:00
|
|
|
|
2015-01-27 02:34:38 +01:00
|
|
|
incomingMessage = [[TSIncomingMessage alloc] initWithTimestamp:timeStamp
|
|
|
|
inThread:cThread
|
|
|
|
messageBody:body
|
|
|
|
attachments:attachments];
|
2014-11-21 14:38:37 +01:00
|
|
|
thread = cThread;
|
2015-01-27 02:34:38 +01:00
|
|
|
|
2015-01-27 23:30:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (thread && incomingMessage) {
|
2015-01-29 08:40:24 +01:00
|
|
|
if ([attachments count] > 0 && body != nil && ![body isEqualToString:@""]) { // Android allows attachments to be sent with body.
|
|
|
|
uint64_t textMessageTimestamp = timeStamp+1000; // We want the text to be displayed under the attachment
|
2015-01-27 23:30:28 +01:00
|
|
|
|
|
|
|
if ([thread isGroupThread]) {
|
|
|
|
TSGroupThread *gThread = (TSGroupThread*)thread;
|
|
|
|
TSIncomingMessage *textMessage = [[TSIncomingMessage alloc] initWithTimestamp:textMessageTimestamp
|
|
|
|
inThread:gThread
|
|
|
|
authorId:message.source
|
|
|
|
messageBody:body
|
|
|
|
attachments:nil];
|
|
|
|
[textMessage saveWithTransaction:transaction];
|
|
|
|
} else{
|
|
|
|
TSContactThread *cThread= (TSContactThread*)thread;
|
|
|
|
TSIncomingMessage *textMessage = [[TSIncomingMessage alloc] initWithTimestamp:textMessageTimestamp
|
|
|
|
inThread:cThread
|
|
|
|
messageBody:body
|
|
|
|
attachments:nil];
|
|
|
|
[textMessage saveWithTransaction:transaction];
|
|
|
|
}
|
2015-01-26 06:40:19 +01:00
|
|
|
}
|
2015-01-27 23:30:28 +01:00
|
|
|
|
|
|
|
[incomingMessage saveWithTransaction:transaction];
|
2014-11-21 14:38:37 +01:00
|
|
|
}
|
2015-01-27 23:30:28 +01:00
|
|
|
|
|
|
|
if (completionBlock) {
|
|
|
|
completionBlock(incomingMessage.uniqueId);
|
|
|
|
}
|
|
|
|
|
2014-12-01 23:24:35 +01:00
|
|
|
NSString *name = [thread name];
|
|
|
|
[self notifyUserForIncomingMessage:incomingMessage from:name];
|
2014-11-21 14:38:37 +01:00
|
|
|
}];
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)processException:(NSException*)exception pushSignal:(IncomingPushMessageSignal*)signal{
|
2014-12-11 00:05:41 +01:00
|
|
|
DDLogError(@"Got exception: %@ of type: %@", exception.description, exception.name);
|
2014-11-25 16:38:33 +01:00
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
2014-12-11 00:05:41 +01:00
|
|
|
TSErrorMessage *errorMessage;
|
|
|
|
|
|
|
|
if ([exception.name isEqualToString:NoSessionException]) {
|
|
|
|
errorMessage = [TSErrorMessage missingSessionWithSignal:signal withTransaction:transaction];
|
|
|
|
} else if ([exception.name isEqualToString:InvalidKeyException]){
|
|
|
|
errorMessage = [TSErrorMessage invalidKeyExceptionWithSignal:signal withTransaction:transaction];
|
|
|
|
} else if ([exception.name isEqualToString:InvalidKeyIdException]){
|
|
|
|
errorMessage = [TSErrorMessage invalidKeyExceptionWithSignal:signal withTransaction:transaction];
|
|
|
|
} else if ([exception.name isEqualToString:DuplicateMessageException]){
|
|
|
|
// Duplicate messages are dismissed
|
|
|
|
return ;
|
|
|
|
} else if ([exception.name isEqualToString:InvalidVersionException]){
|
|
|
|
errorMessage = [TSErrorMessage invalidVersionWithSignal:signal withTransaction:transaction];
|
|
|
|
} else if ([exception.name isEqualToString:UntrustedIdentityKeyException]){
|
2015-02-16 10:27:08 +01:00
|
|
|
errorMessage = [TSInvalidIdentityKeyReceivingErrorMessage untrustedKeyWithSignal:signal withTransaction:transaction];
|
2014-12-11 00:05:41 +01:00
|
|
|
} else {
|
|
|
|
errorMessage = [TSErrorMessage corruptedMessageWithSignal:signal withTransaction:transaction];
|
|
|
|
}
|
|
|
|
|
2014-11-25 16:38:33 +01:00
|
|
|
[errorMessage saveWithTransaction:transaction];
|
|
|
|
}];
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
|
2015-02-16 10:27:08 +01:00
|
|
|
- (void)processException:(NSException*)exception outgoingMessage:(TSOutgoingMessage*)message inThread:(TSThread*)thread {
|
2014-11-27 03:10:00 +01:00
|
|
|
DDLogWarn(@"Got exception: %@", exception.description);
|
2015-02-16 10:27:08 +01:00
|
|
|
|
|
|
|
[self.dbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
|
|
|
|
TSErrorMessage *errorMessage;
|
|
|
|
|
|
|
|
if ([exception.name isEqualToString:UntrustedIdentityKeyException]) {
|
|
|
|
errorMessage = [TSInvalidIdentityKeySendingErrorMessage untrustedKeyWithOutgoingMessage:message inThread:thread forRecipient:exception.userInfo[TSInvalidRecipientKey] preKeyBundle:exception.userInfo[TSInvalidPreKeyBundleKey] withTransaction:transaction];
|
|
|
|
message.messageState = TSOutgoingMessageStateUnsent;
|
|
|
|
[message saveWithTransaction:transaction];
|
|
|
|
} else if (message.groupMetaMessage==TSGroupMessageNone) {
|
|
|
|
// Only update this with exception if it is not a group message as group messages may except for one group send but not another and the UI doesn't know how to handle that
|
2014-12-17 06:44:36 +01:00
|
|
|
[message setMessageState:TSOutgoingMessageStateUnsent];
|
|
|
|
[message saveWithTransaction:transaction];
|
2015-02-16 10:27:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
[errorMessage saveWithTransaction:transaction];
|
|
|
|
}];
|
2014-11-19 21:17:53 +01:00
|
|
|
}
|
|
|
|
|
2014-12-01 23:24:35 +01:00
|
|
|
- (void)notifyUserForIncomingMessage:(TSIncomingMessage*)message from:(NSString*)name{
|
2015-01-16 15:28:41 +01:00
|
|
|
//TODO: Warn user when message is received?
|
2014-12-08 21:16:18 +01:00
|
|
|
}
|
|
|
|
|
2014-11-19 21:17:53 +01:00
|
|
|
@end
|