2014-11-08 11:35:58 -08:00
|
|
|
package org.thoughtcrime.securesms.jobs;
|
|
|
|
|
|
|
|
import android.content.Context;
|
2018-08-09 10:15:43 -04:00
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
|
2018-10-11 16:45:22 -07:00
|
|
|
import org.thoughtcrime.securesms.database.RecipientDatabase.UnidentifiedAccessMode;
|
2018-08-09 10:15:43 -04:00
|
|
|
import org.thoughtcrime.securesms.jobmanager.SafeData;
|
2018-08-01 11:09:24 -04:00
|
|
|
import org.thoughtcrime.securesms.logging.Log;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.thoughtcrime.securesms.crypto.UnidentifiedAccessUtil;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
|
|
|
import org.thoughtcrime.securesms.database.NoSuchMessageException;
|
2018-01-24 19:17:44 -08:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.model.SmsMessageRecord;
|
2014-11-11 19:57:53 -08:00
|
|
|
import org.thoughtcrime.securesms.dependencies.InjectableType;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.notifications.MessageNotifier;
|
2017-08-01 08:56:00 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipient;
|
2016-08-15 20:23:56 -07:00
|
|
|
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException;
|
|
|
|
import org.thoughtcrime.securesms.transport.RetryLaterException;
|
2018-10-11 16:45:22 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
2017-08-14 18:11:13 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
|
2018-05-22 02:13:10 -07:00
|
|
|
import org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
|
|
|
|
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
|
|
|
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
|
|
|
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
import androidx.work.Data;
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
public class PushTextSendJob extends PushSendJob implements InjectableType {
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2015-06-24 18:26:51 -07:00
|
|
|
private static final long serialVersionUID = 1L;
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static final String TAG = PushTextSendJob.class.getSimpleName();
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private static final String KEY_MESSAGE_ID = "message_id";
|
|
|
|
|
2017-09-15 22:38:53 -07:00
|
|
|
@Inject transient SignalServiceMessageSender messageSender;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
private long messageId;
|
|
|
|
|
|
|
|
public PushTextSendJob() {
|
|
|
|
super(null, null);
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public PushTextSendJob(Context context, long messageId, Address destination) {
|
2018-08-09 10:15:43 -04:00
|
|
|
super(context, constructParameters(destination));
|
2014-11-08 11:35:58 -08:00
|
|
|
this.messageId = messageId;
|
|
|
|
}
|
|
|
|
|
2018-08-09 10:15:43 -04:00
|
|
|
@Override
|
|
|
|
protected void initialize(@NonNull SafeData data) {
|
|
|
|
messageId = data.getLong(KEY_MESSAGE_ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected @NonNull Data serialize(@NonNull Data.Builder dataBuilder) {
|
|
|
|
return dataBuilder.putLong(KEY_MESSAGE_ID, messageId).build();
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
@Override
|
2018-08-02 09:50:36 -04:00
|
|
|
public void onAdded() {
|
|
|
|
Log.i(TAG, "onAdded() messageId: " + messageId);
|
2018-10-11 16:45:22 -07:00
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsSending(messageId);
|
2018-08-02 09:50:36 -04:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
@Override
|
2018-02-01 19:22:48 -08:00
|
|
|
public void onPushSend() throws NoSuchMessageException, RetryLaterException {
|
2016-08-15 20:23:56 -07:00
|
|
|
ExpiringMessageManager expirationManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
2018-01-24 19:17:44 -08:00
|
|
|
SmsDatabase database = DatabaseFactory.getSmsDatabase(context);
|
|
|
|
SmsMessageRecord record = database.getMessage(messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-11-14 11:39:23 -08:00
|
|
|
if (!record.isPending() && !record.isFailed()) {
|
|
|
|
Log.w(TAG, "Message " + messageId + " was already sent. Ignoring.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
try {
|
2018-08-02 09:25:33 -04:00
|
|
|
Log.i(TAG, "Sending message: " + messageId);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2018-10-30 16:59:00 -07:00
|
|
|
Recipient recipient = record.getRecipient().resolve();
|
|
|
|
byte[] profileKey = recipient.getProfileKey();
|
|
|
|
UnidentifiedAccessMode accessMode = recipient.getUnidentifiedAccessMode();
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
boolean unidentified = deliver(record);
|
2018-10-30 16:59:00 -07:00
|
|
|
|
2016-12-21 09:49:01 -08:00
|
|
|
database.markAsSent(messageId, true);
|
2018-05-22 02:13:10 -07:00
|
|
|
database.markUnidentified(messageId, unidentified);
|
2015-03-11 14:23:45 -07:00
|
|
|
|
2018-10-11 16:45:22 -07:00
|
|
|
if (TextSecurePreferences.isUnidentifiedDeliveryEnabled(context)) {
|
2018-10-30 16:59:00 -07:00
|
|
|
if (unidentified && accessMode == UnidentifiedAccessMode.UNKNOWN && profileKey == null) {
|
|
|
|
Log.i(TAG, "Marking recipient as UD-unrestricted following a UD send.");
|
|
|
|
DatabaseFactory.getRecipientDatabase(context).setUnidentifiedAccessMode(recipient, UnidentifiedAccessMode.UNRESTRICTED);
|
|
|
|
} else if (unidentified && accessMode == UnidentifiedAccessMode.UNKNOWN) {
|
2018-10-11 16:45:22 -07:00
|
|
|
Log.i(TAG, "Marking recipient as UD-enabled following a UD send.");
|
|
|
|
DatabaseFactory.getRecipientDatabase(context).setUnidentifiedAccessMode(recipient, UnidentifiedAccessMode.ENABLED);
|
|
|
|
} else if (!unidentified && accessMode != UnidentifiedAccessMode.DISABLED) {
|
|
|
|
Log.i(TAG, "Marking recipient as UD-disabled following a non-UD send.");
|
|
|
|
DatabaseFactory.getRecipientDatabase(context).setUnidentifiedAccessMode(recipient, UnidentifiedAccessMode.DISABLED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
if (record.getExpiresIn() > 0) {
|
|
|
|
database.markExpireStarted(messageId);
|
|
|
|
expirationManager.scheduleDeletion(record.getId(), record.isMms(), record.getExpiresIn());
|
|
|
|
}
|
|
|
|
|
2018-08-02 09:50:36 -04:00
|
|
|
Log.i(TAG, "Sent message: " + messageId);
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (InsecureFallbackApprovalException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
database.markAsPendingInsecureSmsFallback(record.getId());
|
2017-08-01 08:56:00 -07:00
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, record.getRecipient(), record.getThreadId());
|
2017-11-21 11:54:18 -08:00
|
|
|
ApplicationContext.getInstance(context).getJobManager().add(new DirectoryRefreshJob(context, false));
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (UntrustedIdentityException e) {
|
|
|
|
Log.w(TAG, e);
|
2017-07-26 09:59:15 -07:00
|
|
|
database.addMismatchedIdentity(record.getId(), Address.fromSerialized(e.getE164Number()), e.getIdentityKey());
|
2014-11-08 11:35:58 -08:00
|
|
|
database.markAsSentFailed(record.getId());
|
2015-01-15 13:35:35 -08:00
|
|
|
database.markAsPush(record.getId());
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-11 19:57:53 -08:00
|
|
|
@Override
|
2018-11-15 12:05:08 -08:00
|
|
|
public boolean onShouldRetry(Exception exception) {
|
2014-11-11 21:11:57 -08:00
|
|
|
if (exception instanceof RetryLaterException) return true;
|
2014-11-11 19:57:53 -08:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onCanceled() {
|
|
|
|
DatabaseFactory.getSmsDatabase(context).markAsSentFailed(messageId);
|
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
long threadId = DatabaseFactory.getSmsDatabase(context).getThreadIdForMessage(messageId);
|
|
|
|
Recipient recipient = DatabaseFactory.getThreadDatabase(context).getRecipientForThreadId(threadId);
|
2014-11-11 19:57:53 -08:00
|
|
|
|
2017-08-01 08:56:00 -07:00
|
|
|
if (threadId != -1 && recipient != null) {
|
|
|
|
MessageNotifier.notifyMessageDeliveryFailed(context, recipient, threadId);
|
2015-04-13 08:57:20 -07:00
|
|
|
}
|
2014-11-11 19:57:53 -08:00
|
|
|
}
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
private boolean deliver(SmsMessageRecord message)
|
2015-03-11 14:23:45 -07:00
|
|
|
throws UntrustedIdentityException, InsecureFallbackApprovalException, RetryLaterException
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
|
|
|
try {
|
2018-11-21 17:26:06 -08:00
|
|
|
rotateSenderCertificateIfNecessary();
|
|
|
|
|
2018-05-22 02:13:10 -07:00
|
|
|
SignalServiceAddress address = getPushAddress(message.getIndividualRecipient().getAddress());
|
|
|
|
Optional<byte[]> profileKey = getProfileKey(message.getIndividualRecipient());
|
|
|
|
Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, message.getIndividualRecipient());
|
|
|
|
|
|
|
|
Log.w(TAG, "Have access key to use: " + unidentifiedAccess.isPresent());
|
|
|
|
|
|
|
|
SignalServiceDataMessage textSecureMessage = SignalServiceDataMessage.newBuilder()
|
|
|
|
.withTimestamp(message.getDateSent())
|
|
|
|
.withBody(message.getBody())
|
|
|
|
.withExpiration((int)(message.getExpiresIn() / 1000))
|
|
|
|
.withProfileKey(profileKey.orNull())
|
|
|
|
.asEndSessionMessage(message.isEndSession())
|
|
|
|
.build();
|
|
|
|
|
|
|
|
return messageSender.sendMessage(address, unidentifiedAccess, textSecureMessage).getSuccess().isUnidentified();
|
2017-07-26 09:59:15 -07:00
|
|
|
} catch (UnregisteredUserException e) {
|
2014-11-08 11:35:58 -08:00
|
|
|
Log.w(TAG, e);
|
2015-03-11 14:23:45 -07:00
|
|
|
throw new InsecureFallbackApprovalException(e);
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (IOException e) {
|
|
|
|
Log.w(TAG, e);
|
2015-03-11 14:23:45 -07:00
|
|
|
throw new RetryLaterException(e);
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|