2014-11-08 20:35:58 +01:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.crypto.storage.TextSecureAxolotlStore;
|
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2014-11-12 04:57:53 +01:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2014-11-08 20:35:58 +01:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
|
|
|
import org.thoughtcrime.securesms.sms.IncomingIdentityUpdateMessage;
|
|
|
|
import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException;
|
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
|
|
|
import org.thoughtcrime.securesms.transport.SecureFallbackApprovalException;
|
|
|
|
import org.whispersystems.libaxolotl.state.AxolotlStore;
|
|
|
|
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
2014-11-08 22:37:57 +01:00
|
|
|
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
|
2014-11-12 04:57:53 +01:00
|
|
|
import org.whispersystems.textsecure.api.messages.TextSecureMessage;
|
2014-11-12 20:35:54 +01:00
|
|
|
import org.whispersystems.textsecure.api.push.PushAddress;
|
|
|
|
import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException;
|
2014-11-12 20:15:05 +01:00
|
|
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
2014-11-08 20:35:58 +01:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-11-12 04:57:53 +01:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
|
|
|
import static org.thoughtcrime.securesms.dependencies.TextSecureCommunicationModule.TextSecureMessageSenderFactory;
|
|
|
|
|
|
|
|
public class PushTextSendJob extends PushSendJob implements InjectableType {
|
2014-11-08 20:35:58 +01:00
|
|
|
|
|
|
|
private static final String TAG = PushTextSendJob.class.getSimpleName();
|
|
|
|
|
2014-11-12 04:57:53 +01:00
|
|
|
@Inject transient TextSecureMessageSenderFactory messageSenderFactory;
|
|
|
|
|
2014-11-08 20:35:58 +01:00
|
|
|
private final long messageId;
|
|
|
|
|
|
|
|
public PushTextSendJob(Context context, long messageId, String destination) {
|
|
|
|
super(context, constructParameters(context, destination));
|
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onAdded() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2014-11-12 04:57:53 +01:00
|
|
|
public void onRun(MasterSecret masterSecret) throws NoSuchMessageException, RetryLaterException {
|
2014-11-08 20:35:58 +01:00
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
SmsMessageRecord record = database.getMessage(masterSecret, messageId);
|
|
|
|
String destination = record.getIndividualRecipient().getNumber();
|
|
|
|
|
|
|
|
try {
|
|
|
|
Log.w(TAG, "Sending message: " + messageId);
|
|
|
|
|
2014-12-03 23:15:54 +01:00
|
|
|
if (deliver(masterSecret, record, destination)) {
|
|
|
|
database.markAsPush(messageId);
|
|
|
|
database.markAsSecure(messageId);
|
|
|
|
database.markAsSent(messageId);
|
|
|
|
}
|
2014-11-08 20:35:58 +01:00
|
|
|
} catch (InsecureFallbackApprovalException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsPendingInsecureSmsFallback(record.getId());
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipients(), record.getThreadId());
|
|
|
|
} catch (SecureFallbackApprovalException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsPendingSecureSmsFallback(record.getId());
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipients(), record.getThreadId());
|
|
|
|
} catch (UntrustedIdentityException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
IncomingIdentityUpdateMessage identityUpdateMessage = IncomingIdentityUpdateMessage.createFor(e.getE164Number(), e.getIdentityKey());
|
|
|
|
database.insertMessageInbox(masterSecret, identityUpdateMessage);
|
|
|
|
database.markAsSentFailed(record.getId());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-12 04:57:53 +01:00
|
|
|
@Override
|
2014-11-12 06:11:57 +01:00
|
|
|
public boolean onShouldRetryThrowable(Exception exception) {
|
|
|
|
if (exception instanceof RetryLaterException) return true;
|
2014-11-12 04:57:53 +01:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
|
|
|
|
long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
|
|
|
|
Recipients recipients = DatabaseFactory.getThreadDatabase(context).getRecipientsForThreadId(threadId);
|
|
|
|
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipients, threadId);
|
|
|
|
}
|
|
|
|
|
2014-12-03 23:15:54 +01:00
|
|
|
private boolean deliver(MasterSecret masterSecret, SmsMessageRecord message, String destination)
|
2014-11-08 20:35:58 +01:00
|
|
|
throws UntrustedIdentityException, SecureFallbackApprovalException,
|
|
|
|
InsecureFallbackApprovalException, RetryLaterException
|
|
|
|
{
|
|
|
|
boolean isSmsFallbackSupported = isSmsFallbackSupported(context, destination);
|
|
|
|
|
|
|
|
try {
|
|
|
|
PushAddress address = getPushAddress(message.getIndividualRecipient());
|
2014-11-12 04:57:53 +01:00
|
|
|
TextSecureMessageSender messageSender = messageSenderFactory.create(masterSecret);
|
2014-11-08 20:35:58 +01:00
|
|
|
|
|
|
|
if (message.isEndSession()) {
|
|
|
|
messageSender.sendMessage(address, new TextSecureMessage(message.getDateSent(), null,
|
|
|
|
null, null, true, true));
|
|
|
|
} else {
|
|
|
|
messageSender.sendMessage(address, new TextSecureMessage(message.getDateSent(), null,
|
|
|
|
message.getBody().getBody()));
|
|
|
|
}
|
2014-12-03 23:15:54 +01:00
|
|
|
|
|
|
|
return true;
|
2014-11-08 20:35:58 +01:00
|
|
|
} catch (InvalidNumberException | UnregisteredUserException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
if (isSmsFallbackSupported) fallbackOrAskApproval(masterSecret, message, destination);
|
|
|
|
else DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
if (isSmsFallbackSupported) fallbackOrAskApproval(masterSecret, message, destination);
|
|
|
|
else throw new RetryLaterException(e);
|
|
|
|
}
|
2014-12-03 23:15:54 +01:00
|
|
|
|
|
|
|
return false;
|
2014-11-08 20:35:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private void fallbackOrAskApproval(MasterSecret masterSecret, SmsMessageRecord smsMessage, String destination)
|
|
|
|
throws SecureFallbackApprovalException, InsecureFallbackApprovalException
|
|
|
|
{
|
|
|
|
Recipient recipient = smsMessage.getIndividualRecipient();
|
|
|
|
boolean isSmsFallbackApprovalRequired = isSmsFallbackApprovalRequired(destination);
|
|
|
|
AxolotlStore axolotlStore = new TextSecureAxolotlStore(context, masterSecret);
|
|
|
|
|
|
|
|
if (!isSmsFallbackApprovalRequired) {
|
|
|
|
Log.w(TAG, "Falling back to SMS");
|
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsForcedSms(smsMessage.getId());
|
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new SmsSendJob(context, messageId, destination));
|
2014-11-12 18:09:55 +01:00
|
|
|
} else if (!axolotlStore.containsSession(recipient.getRecipientId(), PushAddress.DEFAULT_DEVICE_ID)) {
|
2014-11-08 20:35:58 +01:00
|
|
|
Log.w(TAG, "Marking message as pending insecure fallback.");
|
|
|
|
throw new InsecureFallbackApprovalException("Pending user approval for fallback to insecure SMS");
|
|
|
|
} else {
|
|
|
|
Log.w(TAG, "Marking message as pending secure fallback.");
|
|
|
|
throw new SecureFallbackApprovalException("Pending user approval for fallback to secure SMS");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|