2014-11-03 15:16:04 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2015-07-06 17:36:49 -07:00
|
|
|
import android.support.annotation.NonNull;
|
2014-11-03 15:16:04 -08:00
|
|
|
import android.util.Log;
|
|
|
|
import android.util.Pair;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.attachments.DatabaseAttachment;
|
|
|
|
import org.thoughtcrime.securesms.attachments.PointerAttachment;
|
2015-07-06 17:36:49 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.IdentityKeyUtil;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
2015-07-06 17:36:49 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUnion;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecretUtil;
|
2014-11-04 15:01:32 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.SecurityEvent;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.storage.SignalProtocolStoreImpl;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.crypto.storage.TextSecureSessionStore;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
|
2016-02-19 17:07:41 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MessagingDatabase.SyncMessageId;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.database.PushDatabase;
|
2015-10-12 18:25:05 -07:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.groups.GroupMessageProcessor;
|
|
|
|
import org.thoughtcrime.securesms.mms.IncomingMediaMessage;
|
2016-08-15 20:23:56 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingExpirationUpdateMessage;
|
2015-05-18 15:16:27 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingSecureMediaMessage;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.recipients.RecipientFactory;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.service.KeyCachingService;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingEncryptedMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingEndSessionMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingPreKeyBundleMessage;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingTextMessage;
|
2015-05-18 15:16:27 -07:00
|
|
|
import org.thoughtcrime.securesms.sms.OutgoingTextMessage;
|
2014-11-12 11:15:05 -08:00
|
|
|
import org.thoughtcrime.securesms.util.Base64;
|
2015-05-18 15:16:27 -07:00
|
|
|
import org.thoughtcrime.securesms.util.GroupUtil;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.whispersystems.jobqueue.JobParameters;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.DuplicateMessageException;
|
|
|
|
import org.whispersystems.libsignal.IdentityKey;
|
|
|
|
import org.whispersystems.libsignal.InvalidKeyException;
|
|
|
|
import org.whispersystems.libsignal.InvalidKeyIdException;
|
|
|
|
import org.whispersystems.libsignal.InvalidMessageException;
|
|
|
|
import org.whispersystems.libsignal.InvalidVersionException;
|
|
|
|
import org.whispersystems.libsignal.LegacyMessageException;
|
|
|
|
import org.whispersystems.libsignal.NoSessionException;
|
|
|
|
import org.whispersystems.libsignal.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.libsignal.protocol.PreKeySignalMessage;
|
|
|
|
import org.whispersystems.libsignal.state.SessionStore;
|
2016-08-15 20:23:56 -07:00
|
|
|
import org.whispersystems.libsignal.state.SignalProtocolStore;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
|
2016-08-15 20:23:56 -07:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.ReadMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.RequestMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage;
|
|
|
|
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-08-24 15:24:31 -07:00
|
|
|
import java.util.List;
|
2015-03-18 14:25:27 -07:00
|
|
|
import java.util.concurrent.TimeUnit;
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
import ws.com.google.android.mms.MmsException;
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
public class PushDecryptJob extends ContextJob {
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private static final long serialVersionUID = 2L;
|
2015-06-24 18:26:51 -07:00
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
public static final String TAG = PushDecryptJob.class.getSimpleName();
|
|
|
|
|
|
|
|
private final long messageId;
|
2015-01-15 13:35:35 -08:00
|
|
|
private final long smsMessageId;
|
|
|
|
|
|
|
|
public PushDecryptJob(Context context, long pushMessageId, String sender) {
|
|
|
|
this(context, pushMessageId, -1, sender);
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-01-15 13:35:35 -08:00
|
|
|
public PushDecryptJob(Context context, long pushMessageId, long smsMessageId, String sender) {
|
2014-11-03 15:16:04 -08:00
|
|
|
super(context, JobParameters.newBuilder()
|
|
|
|
.withPersistence()
|
2015-10-16 10:07:50 -07:00
|
|
|
.withGroupId("__PUSH_DECRYPT_JOB__")
|
2015-03-18 14:25:27 -07:00
|
|
|
.withWakeLock(true, 5, TimeUnit.SECONDS)
|
2014-11-03 15:16:04 -08:00
|
|
|
.create());
|
2015-01-15 13:35:35 -08:00
|
|
|
this.messageId = pushMessageId;
|
|
|
|
this.smsMessageId = smsMessageId;
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-07-20 18:00:48 -07:00
|
|
|
public void onAdded() {}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
@Override
|
2015-07-06 17:36:49 -07:00
|
|
|
public void onRun() throws NoSuchMessageException {
|
2015-10-16 10:07:50 -07:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
if (!IdentityKeyUtil.hasIdentityKey(context)) {
|
|
|
|
Log.w(TAG, "Skipping job, waiting for migration...");
|
2015-07-21 16:03:56 -07:00
|
|
|
MessageNotifier.updateNotification(context, null, true, -2);
|
2015-07-06 17:36:49 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
MasterSecret masterSecret = KeyCachingService.getMasterSecret(context);
|
|
|
|
PushDatabase database = DatabaseFactory.getPushDatabase(context);
|
|
|
|
SignalServiceEnvelope envelope = database.get(messageId);
|
|
|
|
Optional<Long> optionalSmsMessageId = smsMessageId > 0 ? Optional.of(smsMessageId) :
|
2015-05-18 15:16:27 -07:00
|
|
|
Optional.<Long>absent();
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
MasterSecretUnion masterSecretUnion;
|
|
|
|
|
|
|
|
if (masterSecret == null) masterSecretUnion = new MasterSecretUnion(MasterSecretUtil.getAsymmetricMasterSecret(context, null));
|
|
|
|
else masterSecretUnion = new MasterSecretUnion(masterSecret);
|
|
|
|
|
|
|
|
handleMessage(masterSecretUnion, envelope, optionalSmsMessageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
database.delete(messageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-07-06 17:36:49 -07:00
|
|
|
public boolean onShouldRetry(Exception exception) {
|
2014-11-11 19:57:53 -08:00
|
|
|
return false;
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-11 19:57:53 -08:00
|
|
|
public void onCanceled() {
|
|
|
|
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
private void handleMessage(MasterSecretUnion masterSecret, SignalServiceEnvelope envelope, Optional<Long> smsMessageId) {
|
2014-11-03 15:16:04 -08:00
|
|
|
try {
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalProtocolStore axolotlStore = new SignalProtocolStoreImpl(context);
|
|
|
|
SignalServiceAddress localAddress = new SignalServiceAddress(TextSecurePreferences.getLocalNumber(context));
|
|
|
|
SignalServiceCipher cipher = new SignalServiceCipher(localAddress, axolotlStore);
|
2014-11-04 15:01:32 -08:00
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceContent content = cipher.decrypt(envelope);
|
2014-11-04 15:01:32 -08:00
|
|
|
|
2015-05-29 16:23:47 -07:00
|
|
|
if (content.getDataMessage().isPresent()) {
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceDataMessage message = content.getDataMessage().get();
|
2015-05-29 16:23:47 -07:00
|
|
|
|
|
|
|
if (message.isEndSession()) handleEndSessionMessage(masterSecret, envelope, message, smsMessageId);
|
|
|
|
else if (message.isGroupUpdate()) handleGroupMessage(masterSecret, envelope, message, smsMessageId);
|
2016-08-15 20:23:56 -07:00
|
|
|
else if (message.isExpirationUpdate()) handleExpirationUpdate(masterSecret, envelope, message, smsMessageId);
|
2015-05-29 16:23:47 -07:00
|
|
|
else if (message.getAttachments().isPresent()) handleMediaMessage(masterSecret, envelope, message, smsMessageId);
|
|
|
|
else handleTextMessage(masterSecret, envelope, message, smsMessageId);
|
|
|
|
} else if (content.getSyncMessage().isPresent()) {
|
2016-03-23 10:34:41 -07:00
|
|
|
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
|
2015-05-29 16:23:47 -07:00
|
|
|
|
2015-10-15 17:10:54 -07:00
|
|
|
if (syncMessage.getSent().isPresent()) handleSynchronizeSentMessage(masterSecret, envelope, syncMessage.getSent().get(), smsMessageId);
|
2015-06-19 22:02:10 -07:00
|
|
|
else if (syncMessage.getRequest().isPresent()) handleSynchronizeRequestMessage(masterSecret, syncMessage.getRequest().get());
|
2016-02-19 17:07:41 -08:00
|
|
|
else if (syncMessage.getRead().isPresent()) handleSynchronizeReadMessage(masterSecret, syncMessage.getRead().get());
|
|
|
|
else Log.w(TAG, "Contains no known sync types...");
|
2015-05-29 16:23:47 -07:00
|
|
|
}
|
2014-11-11 20:29:55 -08:00
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
if (envelope.isPreKeySignalMessage()) {
|
2014-11-11 20:29:55 -08:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new RefreshPreKeysJob(context));
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (InvalidVersionException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-01-15 13:35:35 -08:00
|
|
|
handleInvalidVersionMessage(masterSecret, envelope, smsMessageId);
|
2015-03-03 11:44:49 -08:00
|
|
|
} catch (InvalidMessageException | InvalidKeyIdException | InvalidKeyException | MmsException e) {
|
2014-11-03 15:16:04 -08:00
|
|
|
Log.w(TAG, e);
|
2015-01-15 13:35:35 -08:00
|
|
|
handleCorruptMessage(masterSecret, envelope, smsMessageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (NoSessionException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-01-15 13:35:35 -08:00
|
|
|
handleNoSessionMessage(masterSecret, envelope, smsMessageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (LegacyMessageException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-01-15 13:35:35 -08:00
|
|
|
handleLegacyMessage(masterSecret, envelope, smsMessageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (DuplicateMessageException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-01-15 13:35:35 -08:00
|
|
|
handleDuplicateMessage(masterSecret, envelope, smsMessageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
} catch (UntrustedIdentityException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-01-15 13:35:35 -08:00
|
|
|
handleUntrustedIdentityMessage(masterSecret, envelope, smsMessageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
private void handleEndSessionMessage(@NonNull MasterSecretUnion masterSecret,
|
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
|
|
|
@NonNull SignalServiceDataMessage message,
|
|
|
|
@NonNull Optional<Long> smsMessageId)
|
2014-11-04 15:01:32 -08:00
|
|
|
{
|
2015-01-15 13:35:35 -08:00
|
|
|
EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
IncomingTextMessage incomingTextMessage = new IncomingTextMessage(envelope.getSource(),
|
|
|
|
envelope.getSourceDevice(),
|
|
|
|
message.getTimestamp(),
|
2016-08-15 20:23:56 -07:00
|
|
|
"", Optional.<SignalServiceGroup>absent(), 0);
|
2015-01-15 13:35:35 -08:00
|
|
|
|
|
|
|
long threadId;
|
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
if (!smsMessageId.isPresent()) {
|
2015-01-15 13:35:35 -08:00
|
|
|
IncomingEndSessionMessage incomingEndSessionMessage = new IncomingEndSessionMessage(incomingTextMessage);
|
|
|
|
Pair<Long, Long> messageAndThreadId = smsDatabase.insertMessageInbox(masterSecret, incomingEndSessionMessage);
|
2015-07-06 17:36:49 -07:00
|
|
|
|
2015-01-15 13:35:35 -08:00
|
|
|
threadId = messageAndThreadId.second;
|
|
|
|
} else {
|
2015-05-18 15:16:27 -07:00
|
|
|
smsDatabase.markAsEndSession(smsMessageId.get());
|
|
|
|
threadId = smsDatabase.getThreadIdForMessage(smsMessageId.get());
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
SessionStore sessionStore = new TextSecureSessionStore(context);
|
2015-03-03 11:44:49 -08:00
|
|
|
sessionStore.deleteAllSessions(envelope.getSource());
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-09-23 15:47:48 -07:00
|
|
|
SecurityEvent.broadcastSecurityUpdateEvent(context);
|
2015-07-06 17:36:49 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), threadId);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleGroupMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
|
|
|
@NonNull SignalServiceDataMessage message,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
{
|
2015-10-15 17:10:54 -07:00
|
|
|
GroupMessageProcessor.process(context, masterSecret, envelope, message, false);
|
2015-01-15 13:35:35 -08:00
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
if (smsMessageId.isPresent()) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
private void handleExpirationUpdate(@NonNull MasterSecretUnion masterSecret,
|
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
|
|
|
@NonNull SignalServiceDataMessage message,
|
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
|
|
|
Recipients recipients = getMessageDestination(envelope, message);
|
|
|
|
IncomingMediaMessage mediaMessage = new IncomingMediaMessage(masterSecret, envelope.getSource(),
|
|
|
|
localNumber, message.getTimestamp(), -1,
|
|
|
|
message.getExpiresInSeconds() * 1000, true,
|
|
|
|
Optional.fromNullable(envelope.getRelay()),
|
|
|
|
Optional.<String>absent(), message.getGroupInfo(),
|
|
|
|
Optional.<List<SignalServiceAttachment>>absent());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
database.insertSecureDecryptedMessageInbox(masterSecret, mediaMessage, -1);
|
|
|
|
|
|
|
|
DatabaseFactory.getRecipientPreferenceDatabase(context).setExpireMessages(recipients, message.getExpiresInSeconds());
|
|
|
|
|
|
|
|
if (smsMessageId.isPresent()) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleSynchronizeSentMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull SentTranscriptMessage message,
|
|
|
|
@NonNull Optional<Long> smsMessageId)
|
2014-11-03 15:16:04 -08:00
|
|
|
throws MmsException
|
|
|
|
{
|
2015-11-24 16:01:21 -08:00
|
|
|
Long threadId;
|
|
|
|
|
2015-10-15 17:10:54 -07:00
|
|
|
if (message.getMessage().isGroupUpdate()) {
|
2015-11-24 16:01:21 -08:00
|
|
|
threadId = GroupMessageProcessor.process(context, masterSecret, envelope, message.getMessage(), true);
|
2016-08-15 20:23:56 -07:00
|
|
|
} else if (message.getMessage().isExpirationUpdate()) {
|
|
|
|
threadId = handleSynchronizeSentExpirationUpdate(masterSecret, message, smsMessageId);
|
2015-10-15 17:10:54 -07:00
|
|
|
} else if (message.getMessage().getAttachments().isPresent()) {
|
2015-11-24 16:01:21 -08:00
|
|
|
threadId = handleSynchronizeSentMediaMessage(masterSecret, message, smsMessageId);
|
2014-11-03 15:16:04 -08:00
|
|
|
} else {
|
2015-11-24 16:01:21 -08:00
|
|
|
threadId = handleSynchronizeSentTextMessage(masterSecret, message, smsMessageId);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (threadId != null) {
|
|
|
|
DatabaseFactory.getThreadDatabase(getContext()).setRead(threadId);
|
|
|
|
MessageNotifier.updateNotification(getContext(), masterSecret.getMasterSecret().orNull());
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
2015-05-29 16:23:47 -07:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleSynchronizeRequestMessage(@NonNull MasterSecretUnion masterSecret,
|
|
|
|
@NonNull RequestMessage message)
|
|
|
|
{
|
2015-06-19 22:02:10 -07:00
|
|
|
if (message.isContactsRequest()) {
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceContactUpdateJob(getContext()));
|
|
|
|
}
|
2015-06-22 14:49:04 -07:00
|
|
|
|
|
|
|
if (message.isGroupsRequest()) {
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceGroupUpdateJob(getContext()));
|
|
|
|
}
|
2016-08-26 16:53:23 -07:00
|
|
|
|
|
|
|
if (message.isBlockedListRequest()) {
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
|
|
|
.add(new MultiDeviceBlockedUpdateJob(getContext()));
|
|
|
|
}
|
2015-06-19 22:02:10 -07:00
|
|
|
}
|
|
|
|
|
2016-02-19 17:07:41 -08:00
|
|
|
private void handleSynchronizeReadMessage(@NonNull MasterSecretUnion masterSecret,
|
|
|
|
@NonNull List<ReadMessage> readMessages)
|
|
|
|
{
|
|
|
|
for (ReadMessage readMessage : readMessages) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).setTimestampRead(new SyncMessageId(readMessage.getSender(), readMessage.getTimestamp()));
|
|
|
|
DatabaseFactory.getMmsDatabase(context).setTimestampRead(new SyncMessageId(readMessage.getSender(), readMessage.getTimestamp()));
|
|
|
|
}
|
|
|
|
|
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull());
|
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleMediaMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
|
|
|
@NonNull SignalServiceDataMessage message,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
2015-05-29 16:23:47 -07:00
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
String localNumber = TextSecurePreferences.getLocalNumber(context);
|
2016-08-15 20:23:56 -07:00
|
|
|
Recipients recipients = getMessageDestination(envelope, message);
|
2015-05-29 16:23:47 -07:00
|
|
|
IncomingMediaMessage mediaMessage = new IncomingMediaMessage(masterSecret, envelope.getSource(),
|
2016-02-05 16:10:33 -08:00
|
|
|
localNumber, message.getTimestamp(), -1,
|
2016-08-15 20:23:56 -07:00
|
|
|
message.getExpiresInSeconds() * 1000, false,
|
2015-05-29 16:23:47 -07:00
|
|
|
Optional.fromNullable(envelope.getRelay()),
|
|
|
|
message.getBody(),
|
|
|
|
message.getGroupInfo(),
|
|
|
|
message.getAttachments());
|
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
if (message.getExpiresInSeconds() != recipients.getExpireMessages()) {
|
|
|
|
handleExpirationUpdate(masterSecret, envelope, message, Optional.<Long>absent());
|
|
|
|
}
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
Pair<Long, Long> messageAndThreadId = database.insertSecureDecryptedMessageInbox(masterSecret, mediaMessage, -1);
|
|
|
|
List<DatabaseAttachment> attachments = DatabaseFactory.getAttachmentDatabase(context).getAttachmentsForMessage(messageAndThreadId.first);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
for (DatabaseAttachment attachment : attachments) {
|
2015-08-24 15:24:31 -07:00
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
2015-10-12 18:25:05 -07:00
|
|
|
.add(new AttachmentDownloadJob(context, messageAndThreadId.first,
|
|
|
|
attachment.getAttachmentId()));
|
2015-08-24 15:24:31 -07:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
if (smsMessageId.isPresent()) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
private long handleSynchronizeSentExpirationUpdate(@NonNull MasterSecretUnion masterSecret,
|
|
|
|
@NonNull SentTranscriptMessage message,
|
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
Recipients recipients = getSyncMessageDestination(message);
|
|
|
|
|
|
|
|
OutgoingExpirationUpdateMessage expirationUpdateMessage = new OutgoingExpirationUpdateMessage(recipients,
|
|
|
|
message.getTimestamp(),
|
|
|
|
message.getMessage().getExpiresInSeconds() * 1000);
|
|
|
|
|
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
|
|
|
long messageId = database.insertMessageOutbox(masterSecret, expirationUpdateMessage, threadId, false);
|
|
|
|
|
|
|
|
database.markAsSent(messageId);
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
|
|
|
|
DatabaseFactory.getRecipientPreferenceDatabase(context).setExpireMessages(recipients, message.getMessage().getExpiresInSeconds());
|
|
|
|
|
|
|
|
database.markExpireStarted(messageId, message.getExpirationStartTimestamp());
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getExpiringMessageManager()
|
|
|
|
.scheduleDeletion(messageId, true,
|
|
|
|
message.getExpirationStartTimestamp(),
|
|
|
|
message.getMessage().getExpiresInSeconds());
|
|
|
|
|
|
|
|
if (smsMessageId.isPresent()) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
|
|
|
|
}
|
|
|
|
|
|
|
|
return threadId;
|
|
|
|
}
|
|
|
|
|
2015-11-24 16:01:21 -08:00
|
|
|
private long handleSynchronizeSentMediaMessage(@NonNull MasterSecretUnion masterSecret,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull SentTranscriptMessage message,
|
|
|
|
@NonNull Optional<Long> smsMessageId)
|
2015-05-29 16:23:47 -07:00
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
Recipients recipients = getSyncMessageDestination(message);
|
2015-10-12 18:25:05 -07:00
|
|
|
OutgoingMediaMessage mediaMessage = new OutgoingMediaMessage(recipients, message.getMessage().getBody().orNull(),
|
|
|
|
PointerAttachment.forPointers(masterSecret, message.getMessage().getAttachments()),
|
2016-08-15 20:23:56 -07:00
|
|
|
message.getTimestamp(), -1,
|
|
|
|
message.getMessage().getExpiresInSeconds() * 1000,
|
|
|
|
ThreadDatabase.DistributionTypes.DEFAULT);
|
2015-05-29 16:23:47 -07:00
|
|
|
|
|
|
|
mediaMessage = new OutgoingSecureMediaMessage(mediaMessage);
|
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
if (recipients.getExpireMessages() != message.getMessage().getExpiresInSeconds()) {
|
|
|
|
handleSynchronizeSentExpirationUpdate(masterSecret, message, Optional.<Long>absent());
|
|
|
|
}
|
|
|
|
|
2015-05-29 16:23:47 -07:00
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
2015-10-12 18:25:05 -07:00
|
|
|
long messageId = database.insertMessageOutbox(masterSecret, mediaMessage, threadId, false);
|
2015-05-29 16:23:47 -07:00
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
database.markAsSent(messageId);
|
2015-05-29 16:23:47 -07:00
|
|
|
database.markAsPush(messageId);
|
|
|
|
|
2015-10-12 18:25:05 -07:00
|
|
|
for (DatabaseAttachment attachment : DatabaseFactory.getAttachmentDatabase(context).getAttachmentsForMessage(messageId)) {
|
2015-08-24 15:24:31 -07:00
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getJobManager()
|
2015-10-12 18:25:05 -07:00
|
|
|
.add(new AttachmentDownloadJob(context, messageId, attachment.getAttachmentId()));
|
2015-08-24 15:24:31 -07:00
|
|
|
}
|
2015-05-29 16:23:47 -07:00
|
|
|
|
|
|
|
if (smsMessageId.isPresent()) {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).deleteMessage(smsMessageId.get());
|
|
|
|
}
|
2015-11-24 16:01:21 -08:00
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
if (message.getMessage().getExpiresInSeconds() > 0) {
|
|
|
|
database.markExpireStarted(messageId, message.getExpirationStartTimestamp());
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getExpiringMessageManager()
|
|
|
|
.scheduleDeletion(messageId, true,
|
|
|
|
message.getExpirationStartTimestamp(),
|
|
|
|
message.getMessage().getExpiresInSeconds());
|
|
|
|
}
|
|
|
|
|
2015-11-24 16:01:21 -08:00
|
|
|
return threadId;
|
2015-05-29 16:23:47 -07:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleTextMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
|
|
|
@NonNull SignalServiceDataMessage message,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
2016-08-15 20:23:56 -07:00
|
|
|
throws MmsException
|
2015-01-15 13:35:35 -08:00
|
|
|
{
|
2016-08-15 20:23:56 -07:00
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
String body = message.getBody().isPresent() ? message.getBody().get() : "";
|
|
|
|
Recipients recipients = getMessageDestination(envelope, message);
|
|
|
|
|
|
|
|
if (message.getExpiresInSeconds() != recipients.getExpireMessages()) {
|
|
|
|
handleExpirationUpdate(masterSecret, envelope, message, Optional.<Long>absent());
|
|
|
|
}
|
2015-05-29 16:23:47 -07:00
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
Pair<Long, Long> messageAndThreadId;
|
2015-01-15 13:35:35 -08:00
|
|
|
|
2015-12-10 04:34:20 -08:00
|
|
|
if (smsMessageId.isPresent() && !message.getGroupInfo().isPresent()) {
|
2015-05-29 16:23:47 -07:00
|
|
|
messageAndThreadId = database.updateBundleMessageBody(masterSecret, smsMessageId.get(), body);
|
2015-01-15 13:35:35 -08:00
|
|
|
} else {
|
2015-05-29 16:23:47 -07:00
|
|
|
IncomingTextMessage textMessage = new IncomingTextMessage(envelope.getSource(),
|
|
|
|
envelope.getSourceDevice(),
|
|
|
|
message.getTimestamp(), body,
|
2016-08-15 20:23:56 -07:00
|
|
|
message.getGroupInfo(),
|
|
|
|
message.getExpiresInSeconds() * 1000);
|
2015-05-29 16:23:47 -07:00
|
|
|
|
|
|
|
textMessage = new IncomingEncryptedMessage(textMessage, body);
|
|
|
|
messageAndThreadId = database.insertMessageInbox(masterSecret, textMessage);
|
2015-12-10 04:34:20 -08:00
|
|
|
|
|
|
|
if (smsMessageId.isPresent()) database.deleteMessage(smsMessageId.get());
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2015-05-18 15:16:27 -07:00
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-11-24 16:01:21 -08:00
|
|
|
private long handleSynchronizeSentTextMessage(@NonNull MasterSecretUnion masterSecret,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull SentTranscriptMessage message,
|
|
|
|
@NonNull Optional<Long> smsMessageId)
|
2016-08-15 20:23:56 -07:00
|
|
|
throws MmsException
|
2015-05-29 16:23:47 -07:00
|
|
|
{
|
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
Recipients recipients = getSyncMessageDestination(message);
|
|
|
|
String body = message.getMessage().getBody().or("");
|
2016-08-15 20:23:56 -07:00
|
|
|
long expiresInMillis = message.getMessage().getExpiresInSeconds() * 1000;
|
|
|
|
OutgoingTextMessage outgoingTextMessage = new OutgoingTextMessage(recipients, body, expiresInMillis, -1);
|
|
|
|
|
|
|
|
if (recipients.getExpireMessages() != message.getMessage().getExpiresInSeconds()) {
|
|
|
|
handleSynchronizeSentExpirationUpdate(masterSecret, message, Optional.<Long>absent());
|
|
|
|
}
|
2015-05-29 16:23:47 -07:00
|
|
|
|
|
|
|
long threadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
|
|
|
long messageId = database.insertMessageOutbox(masterSecret, threadId, outgoingTextMessage, false, message.getTimestamp());
|
|
|
|
|
|
|
|
database.markAsSent(messageId);
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
database.markAsSecure(messageId);
|
|
|
|
|
|
|
|
if (smsMessageId.isPresent()) {
|
|
|
|
database.deleteMessage(smsMessageId.get());
|
|
|
|
}
|
2015-11-24 16:01:21 -08:00
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
if (expiresInMillis > 0) {
|
|
|
|
database.markExpireStarted(messageId, message.getExpirationStartTimestamp());
|
|
|
|
ApplicationContext.getInstance(context)
|
|
|
|
.getExpiringMessageManager()
|
|
|
|
.scheduleDeletion(messageId, false, message.getExpirationStartTimestamp(), expiresInMillis);
|
|
|
|
}
|
|
|
|
|
2015-11-24 16:01:21 -08:00
|
|
|
return threadId;
|
2015-05-29 16:23:47 -07:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleInvalidVersionMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
{
|
2015-01-15 13:35:35 -08:00
|
|
|
EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
if (!smsMessageId.isPresent()) {
|
2015-07-06 17:36:49 -07:00
|
|
|
Pair<Long, Long> messageAndThreadId = insertPlaceholder(envelope);
|
2015-01-15 13:35:35 -08:00
|
|
|
smsDatabase.markAsInvalidVersionKeyExchange(messageAndThreadId.first);
|
2015-07-06 17:36:49 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
|
2015-01-15 13:35:35 -08:00
|
|
|
} else {
|
2015-05-18 15:16:27 -07:00
|
|
|
smsDatabase.markAsInvalidVersionKeyExchange(smsMessageId.get());
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleCorruptMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
{
|
2015-01-15 13:35:35 -08:00
|
|
|
EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
if (!smsMessageId.isPresent()) {
|
2015-07-06 17:36:49 -07:00
|
|
|
Pair<Long, Long> messageAndThreadId = insertPlaceholder(envelope);
|
2015-01-15 13:35:35 -08:00
|
|
|
smsDatabase.markAsDecryptFailed(messageAndThreadId.first);
|
2015-07-06 17:36:49 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
|
2015-01-15 13:35:35 -08:00
|
|
|
} else {
|
2015-05-18 15:16:27 -07:00
|
|
|
smsDatabase.markAsDecryptFailed(smsMessageId.get());
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleNoSessionMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
{
|
2015-01-15 13:35:35 -08:00
|
|
|
EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
if (!smsMessageId.isPresent()) {
|
2015-07-06 17:36:49 -07:00
|
|
|
Pair<Long, Long> messageAndThreadId = insertPlaceholder(envelope);
|
2015-01-15 13:35:35 -08:00
|
|
|
smsDatabase.markAsNoSession(messageAndThreadId.first);
|
2015-07-06 17:36:49 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
|
2015-01-15 13:35:35 -08:00
|
|
|
} else {
|
2015-05-18 15:16:27 -07:00
|
|
|
smsDatabase.markAsNoSession(smsMessageId.get());
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleLegacyMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
{
|
2015-01-15 13:35:35 -08:00
|
|
|
EncryptingSmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
if (!smsMessageId.isPresent()) {
|
2015-07-06 17:36:49 -07:00
|
|
|
Pair<Long, Long> messageAndThreadId = insertPlaceholder(envelope);
|
2015-01-15 13:35:35 -08:00
|
|
|
smsDatabase.markAsLegacyVersion(messageAndThreadId.first);
|
2015-07-06 17:36:49 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
|
2015-01-15 13:35:35 -08:00
|
|
|
} else {
|
2015-05-18 15:16:27 -07:00
|
|
|
smsDatabase.markAsLegacyVersion(smsMessageId.get());
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleDuplicateMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
{
|
2015-01-15 13:35:35 -08:00
|
|
|
// Let's start ignoring these now
|
|
|
|
// SmsDatabase smsDatabase = DatabaseFactory.getEncryptingSmsDatabase(context);
|
2015-01-25 17:43:24 -08:00
|
|
|
//
|
2015-01-15 13:35:35 -08:00
|
|
|
// if (smsMessageId <= 0) {
|
|
|
|
// Pair<Long, Long> messageAndThreadId = insertPlaceholder(masterSecret, envelope);
|
|
|
|
// smsDatabase.markAsDecryptDuplicate(messageAndThreadId.first);
|
|
|
|
// MessageNotifier.updateNotification(context, masterSecret, messageAndThreadId.second);
|
|
|
|
// } else {
|
|
|
|
// smsDatabase.markAsDecryptDuplicate(smsMessageId);
|
|
|
|
// }
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2015-07-06 17:36:49 -07:00
|
|
|
private void handleUntrustedIdentityMessage(@NonNull MasterSecretUnion masterSecret,
|
2016-03-23 10:34:41 -07:00
|
|
|
@NonNull SignalServiceEnvelope envelope,
|
2015-07-06 17:36:49 -07:00
|
|
|
@NonNull Optional<Long> smsMessageId)
|
|
|
|
{
|
2015-01-15 13:35:35 -08:00
|
|
|
try {
|
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
Recipients recipients = RecipientFactory.getRecipientsFromString(context, envelope.getSource(), false);
|
|
|
|
long recipientId = recipients.getPrimaryRecipient().getRecipientId();
|
2016-03-23 10:34:41 -07:00
|
|
|
PreKeySignalMessage whisperMessage = new PreKeySignalMessage(envelope.getLegacyMessage());
|
2015-01-15 13:35:35 -08:00
|
|
|
IdentityKey identityKey = whisperMessage.getIdentityKey();
|
2015-05-29 16:23:47 -07:00
|
|
|
String encoded = Base64.encodeBytes(envelope.getLegacyMessage());
|
2015-01-15 13:35:35 -08:00
|
|
|
IncomingTextMessage textMessage = new IncomingTextMessage(envelope.getSource(), envelope.getSourceDevice(),
|
|
|
|
envelope.getTimestamp(), encoded,
|
2016-08-15 20:23:56 -07:00
|
|
|
Optional.<SignalServiceGroup>absent(), 0);
|
2015-01-15 13:35:35 -08:00
|
|
|
|
2015-05-18 15:16:27 -07:00
|
|
|
if (!smsMessageId.isPresent()) {
|
2015-01-15 13:35:35 -08:00
|
|
|
IncomingPreKeyBundleMessage bundleMessage = new IncomingPreKeyBundleMessage(textMessage, encoded);
|
|
|
|
Pair<Long, Long> messageAndThreadId = database.insertMessageInbox(masterSecret, bundleMessage);
|
|
|
|
|
2015-05-26 13:12:11 -07:00
|
|
|
database.setMismatchedIdentity(messageAndThreadId.first, recipientId, identityKey);
|
2015-07-06 17:36:49 -07:00
|
|
|
MessageNotifier.updateNotification(context, masterSecret.getMasterSecret().orNull(), messageAndThreadId.second);
|
2015-01-15 13:35:35 -08:00
|
|
|
} else {
|
2015-05-18 15:16:27 -07:00
|
|
|
database.updateMessageBody(masterSecret, smsMessageId.get(), encoded);
|
|
|
|
database.markAsPreKeyBundle(smsMessageId.get());
|
2015-05-26 13:12:11 -07:00
|
|
|
database.setMismatchedIdentity(smsMessageId.get(), recipientId, identityKey);
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
2015-03-03 11:44:49 -08:00
|
|
|
} catch (InvalidMessageException | InvalidVersionException e) {
|
2015-01-15 13:35:35 -08:00
|
|
|
throw new AssertionError(e);
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
|
|
|
|
2016-03-23 10:34:41 -07:00
|
|
|
private Pair<Long, Long> insertPlaceholder(@NonNull SignalServiceEnvelope envelope) {
|
2015-07-06 17:36:49 -07:00
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
IncomingTextMessage textMessage = new IncomingTextMessage(envelope.getSource(), envelope.getSourceDevice(),
|
|
|
|
envelope.getTimestamp(), "",
|
2016-08-15 20:23:56 -07:00
|
|
|
Optional.<SignalServiceGroup>absent(), 0);
|
2014-11-03 15:16:04 -08:00
|
|
|
|
|
|
|
textMessage = new IncomingEncryptedMessage(textMessage, "");
|
2015-07-06 17:36:49 -07:00
|
|
|
return database.insertMessageInbox(textMessage);
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|
2015-05-18 15:16:27 -07:00
|
|
|
|
2015-05-29 16:23:47 -07:00
|
|
|
private Recipients getSyncMessageDestination(SentTranscriptMessage message) {
|
|
|
|
if (message.getMessage().getGroupInfo().isPresent()) {
|
|
|
|
return RecipientFactory.getRecipientsFromString(context, GroupUtil.getEncodedId(message.getMessage().getGroupInfo().get().getGroupId()), false);
|
2015-05-18 15:16:27 -07:00
|
|
|
} else {
|
2015-05-29 16:23:47 -07:00
|
|
|
return RecipientFactory.getRecipientsFromString(context, message.getDestination().get(), false);
|
2015-05-18 15:16:27 -07:00
|
|
|
}
|
|
|
|
}
|
2016-08-15 20:23:56 -07:00
|
|
|
|
|
|
|
private Recipients getMessageDestination(SignalServiceEnvelope envelope, SignalServiceDataMessage message) {
|
|
|
|
if (message.getGroupInfo().isPresent()) {
|
|
|
|
return RecipientFactory.getRecipientsFromString(context, GroupUtil.getEncodedId(message.getGroupInfo().get().getGroupId()), false);
|
|
|
|
} else {
|
|
|
|
return RecipientFactory.getRecipientsFromString(context, envelope.getSource(), false);
|
|
|
|
}
|
|
|
|
}
|
2014-11-03 15:16:04 -08:00
|
|
|
}
|