2012-10-21 17:41:44 -07:00
|
|
|
/**
|
2011-12-20 10:20:44 -08:00
|
|
|
* Copyright (C) 2011 Whisper Systems
|
2012-10-21 17:41:44 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2012-10-21 17:41:44 -07:00
|
|
|
*
|
2011-12-20 10:20:44 -08:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
package org.thoughtcrime.securesms.sms;
|
|
|
|
|
2012-10-21 17:41:44 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.util.Log;
|
2014-10-22 18:28:03 -07:00
|
|
|
import android.util.Pair;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.ApplicationContext;
|
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;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.database.Address;
|
2014-11-03 15:16:04 -08:00
|
|
|
import org.thoughtcrime.securesms.database.DatabaseFactory;
|
2014-10-22 18:28:03 -07:00
|
|
|
import org.thoughtcrime.securesms.database.EncryptingSmsDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.MmsDatabase;
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.thoughtcrime.securesms.database.NotInDirectoryException;
|
2017-04-22 16:29:26 -07:00
|
|
|
import org.thoughtcrime.securesms.database.SmsDatabase;
|
2014-11-09 20:35:08 -08:00
|
|
|
import org.thoughtcrime.securesms.database.TextSecureDirectory;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.thoughtcrime.securesms.database.ThreadDatabase;
|
|
|
|
import org.thoughtcrime.securesms.database.model.MessageRecord;
|
|
|
|
import org.thoughtcrime.securesms.jobs.MmsSendJob;
|
|
|
|
import org.thoughtcrime.securesms.jobs.PushGroupSendJob;
|
|
|
|
import org.thoughtcrime.securesms.jobs.PushMediaSendJob;
|
|
|
|
import org.thoughtcrime.securesms.jobs.PushTextSendJob;
|
|
|
|
import org.thoughtcrime.securesms.jobs.SmsSendJob;
|
2017-07-26 09:59:15 -07:00
|
|
|
import org.thoughtcrime.securesms.mms.MmsException;
|
2014-02-19 21:06:54 -08:00
|
|
|
import org.thoughtcrime.securesms.mms.OutgoingMediaMessage;
|
2016-12-20 09:55:52 -08:00
|
|
|
import org.thoughtcrime.securesms.push.AccountManagerFactory;
|
2014-10-22 18:28:03 -07:00
|
|
|
import org.thoughtcrime.securesms.recipients.Recipients;
|
2016-08-15 20:23:56 -07:00
|
|
|
import org.thoughtcrime.securesms.service.ExpiringMessageManager;
|
2014-10-22 18:28:03 -07:00
|
|
|
import org.thoughtcrime.securesms.util.TextSecurePreferences;
|
|
|
|
import org.thoughtcrime.securesms.util.Util;
|
2014-11-08 11:35:58 -08:00
|
|
|
import org.whispersystems.jobqueue.JobManager;
|
2016-03-23 10:34:41 -07:00
|
|
|
import org.whispersystems.libsignal.util.guava.Optional;
|
|
|
|
import org.whispersystems.signalservice.api.SignalServiceAccountManager;
|
|
|
|
import org.whispersystems.signalservice.api.push.ContactTokenDetails;
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
import java.io.IOException;
|
Major storage layer refactoring to set the stage for clean GCM.
1) We now try to hand out cursors at a minimum. There has always been
a fairly clean insertion layer that handles encrypting message bodies,
but the process of decrypting message bodies has always been less than
ideal. Here we introduce a "Reader" interface that will decrypt message
bodies when appropriate and return objects that encapsulate record state.
No more MessageDisplayHelper. The MmsSmsDatabase interface is also more
sane.
2) We finally rid ourselves of the technical debt associated with TextSecure's
initial usage of the default SMS DB. In that world, we weren't able to use
anything other than the default "Inbox, Outbox, Sent" types to describe a
message, and had to overload the message content itself with a set of
local "prefixes" to describe what it was (encrypted, asymetric encrypted,
remote encrypted, a key exchange, procssed key exchange), and so on.
This includes a major schema update that transforms the "type" field into
a bitmask that describes everything that used to be encoded in a prefix,
and prefixes have been completely eliminated from the system.
No more Prefix.java
3) Refactoring of the MultipartMessageHandler code. It's less of a mess, and
hopefully more clear as to what's going on.
The next step is to remove what we can from SmsTransportDetails and genericize
that interface for a GCM equivalent.
2013-04-20 12:22:04 -07:00
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
public class MessageSender {
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static final String TAG = MessageSender.class.getSimpleName();
|
|
|
|
|
|
|
|
public static long send(final Context context,
|
|
|
|
final MasterSecret masterSecret,
|
|
|
|
final OutgoingTextMessage message,
|
|
|
|
final long threadId,
|
2017-04-22 16:29:26 -07:00
|
|
|
final boolean forceSms,
|
|
|
|
final SmsDatabase.InsertListener insertListener)
|
2012-10-21 17:41:44 -07:00
|
|
|
{
|
2014-11-08 11:35:58 -08:00
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
Recipients recipients = message.getRecipients();
|
|
|
|
boolean keyExchange = message.isKeyExchange();
|
|
|
|
|
|
|
|
long allocatedThreadId;
|
2014-10-22 18:28:03 -07:00
|
|
|
|
|
|
|
if (threadId == -1) {
|
2014-11-08 11:35:58 -08:00
|
|
|
allocatedThreadId = DatabaseFactory.getThreadDatabase(context).getThreadIdFor(recipients);
|
|
|
|
} else {
|
|
|
|
allocatedThreadId = threadId;
|
2014-10-22 18:28:03 -07:00
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2016-02-05 16:10:33 -08:00
|
|
|
long messageId = database.insertMessageOutbox(new MasterSecretUnion(masterSecret), allocatedThreadId,
|
2017-04-22 16:29:26 -07:00
|
|
|
message, forceSms, System.currentTimeMillis(), insertListener);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
sendTextMessage(context, recipients, forceSms, keyExchange, messageId, message.getExpiresIn());
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
return allocatedThreadId;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static long send(final Context context,
|
|
|
|
final MasterSecret masterSecret,
|
|
|
|
final OutgoingMediaMessage message,
|
|
|
|
final long threadId,
|
2017-04-22 16:29:26 -07:00
|
|
|
final boolean forceSms,
|
|
|
|
final SmsDatabase.InsertListener insertListener)
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
|
|
|
try {
|
|
|
|
ThreadDatabase threadDatabase = DatabaseFactory.getThreadDatabase(context);
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
2011-12-20 10:20:44 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
long allocatedThreadId;
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
if (threadId == -1) {
|
|
|
|
allocatedThreadId = threadDatabase.getThreadIdFor(message.getRecipients(), message.getDistributionType());
|
|
|
|
} else {
|
|
|
|
allocatedThreadId = threadId;
|
2014-10-22 18:28:03 -07:00
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
Recipients recipients = message.getRecipients();
|
2017-04-22 16:29:26 -07:00
|
|
|
long messageId = database.insertMessageOutbox(new MasterSecretUnion(masterSecret), message, allocatedThreadId, forceSms, insertListener);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
sendMediaMessage(context, masterSecret, recipients, forceSms, messageId, message.getExpiresIn());
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
return allocatedThreadId;
|
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
return threadId;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
public static void resendGroupMessage(Context context, MasterSecret masterSecret, MessageRecord messageRecord, Address filterAddress) {
|
2015-01-15 13:35:35 -08:00
|
|
|
if (!messageRecord.isMms()) throw new AssertionError("Not Group");
|
|
|
|
|
|
|
|
Recipients recipients = DatabaseFactory.getMmsAddressDatabase(context).getRecipientsForId(messageRecord.getId());
|
2017-07-26 09:59:15 -07:00
|
|
|
sendGroupPush(context, recipients, messageRecord.getId(), filterAddress);
|
2015-01-15 13:35:35 -08:00
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
public static void resend(Context context, MasterSecret masterSecret, MessageRecord messageRecord) {
|
|
|
|
try {
|
|
|
|
long messageId = messageRecord.getId();
|
|
|
|
boolean forceSms = messageRecord.isForcedSms();
|
|
|
|
boolean keyExchange = messageRecord.isKeyExchange();
|
2016-08-15 20:23:56 -07:00
|
|
|
long expiresIn = messageRecord.getExpiresIn();
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
if (messageRecord.isMms()) {
|
2015-01-15 13:35:35 -08:00
|
|
|
Recipients recipients = DatabaseFactory.getMmsAddressDatabase(context).getRecipientsForId(messageId);
|
2016-08-15 20:23:56 -07:00
|
|
|
sendMediaMessage(context, masterSecret, recipients, forceSms, messageId, expiresIn);
|
2014-11-08 11:35:58 -08:00
|
|
|
} else {
|
2015-01-15 13:35:35 -08:00
|
|
|
Recipients recipients = messageRecord.getRecipients();
|
2016-08-15 20:23:56 -07:00
|
|
|
sendTextMessage(context, recipients, forceSms, keyExchange, messageId, expiresIn);
|
2014-10-22 18:28:03 -07:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (MmsException e) {
|
|
|
|
Log.w(TAG, e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendMediaMessage(Context context, MasterSecret masterSecret,
|
2016-08-15 20:23:56 -07:00
|
|
|
Recipients recipients, boolean forceSms,
|
|
|
|
long messageId, long expiresIn)
|
2014-11-08 11:35:58 -08:00
|
|
|
throws MmsException
|
|
|
|
{
|
|
|
|
if (!forceSms && isSelfSend(context, recipients)) {
|
2016-08-15 20:23:56 -07:00
|
|
|
sendMediaSelf(context, masterSecret, messageId, expiresIn);
|
2014-11-08 11:35:58 -08:00
|
|
|
} else if (isGroupPushSend(recipients)) {
|
2017-07-26 09:59:15 -07:00
|
|
|
sendGroupPush(context, recipients, messageId, null);
|
2014-11-08 11:35:58 -08:00
|
|
|
} else if (!forceSms && isPushMediaSend(context, recipients)) {
|
|
|
|
sendMediaPush(context, recipients, messageId);
|
|
|
|
} else {
|
|
|
|
sendMms(context, messageId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static void sendTextMessage(Context context, Recipients recipients,
|
2016-08-15 20:23:56 -07:00
|
|
|
boolean forceSms, boolean keyExchange,
|
|
|
|
long messageId, long expiresIn)
|
2014-11-08 11:35:58 -08:00
|
|
|
{
|
|
|
|
if (!forceSms && isSelfSend(context, recipients)) {
|
2016-08-15 20:23:56 -07:00
|
|
|
sendTextSelf(context, messageId, expiresIn);
|
2014-11-08 11:35:58 -08:00
|
|
|
} else if (!forceSms && isPushTextSend(context, recipients, keyExchange)) {
|
|
|
|
sendTextPush(context, recipients, messageId);
|
|
|
|
} else {
|
|
|
|
sendSms(context, recipients, messageId);
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
private static void sendTextSelf(Context context, long messageId, long expiresIn) {
|
2014-11-08 11:35:58 -08:00
|
|
|
EncryptingSmsDatabase database = DatabaseFactory.getEncryptingSmsDatabase(context);
|
|
|
|
|
2016-12-21 09:49:01 -08:00
|
|
|
database.markAsSent(messageId, true);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
Pair<Long, Long> messageAndThreadId = database.copyMessageInbox(messageId);
|
|
|
|
database.markAsPush(messageAndThreadId.first);
|
2016-08-15 20:23:56 -07:00
|
|
|
|
|
|
|
if (expiresIn > 0) {
|
|
|
|
ExpiringMessageManager expiringMessageManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
|
|
|
|
|
|
|
database.markExpireStarted(messageId);
|
|
|
|
expiringMessageManager.scheduleDeletion(messageId, false, expiresIn);
|
|
|
|
}
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|
2012-10-21 17:41:44 -07:00
|
|
|
|
2016-08-15 20:23:56 -07:00
|
|
|
private static void sendMediaSelf(Context context, MasterSecret masterSecret,
|
|
|
|
long messageId, long expiresIn)
|
2014-02-19 21:06:54 -08:00
|
|
|
throws MmsException
|
|
|
|
{
|
2016-08-15 20:23:56 -07:00
|
|
|
ExpiringMessageManager expiringMessageManager = ApplicationContext.getInstance(context).getExpiringMessageManager();
|
|
|
|
MmsDatabase database = DatabaseFactory.getMmsDatabase(context);
|
|
|
|
|
2016-12-21 09:49:01 -08:00
|
|
|
database.markAsSent(messageId, true);
|
|
|
|
database.copyMessageInbox(masterSecret, messageId);
|
2016-08-15 20:23:56 -07:00
|
|
|
|
|
|
|
if (expiresIn > 0) {
|
|
|
|
database.markExpireStarted(messageId);
|
|
|
|
expiringMessageManager.scheduleDeletion(messageId, true, expiresIn);
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
2014-02-19 21:06:54 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static void sendTextPush(Context context, Recipients recipients, long messageId) {
|
|
|
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
2017-07-26 09:59:15 -07:00
|
|
|
jobManager.add(new PushTextSendJob(context, messageId, recipients.getPrimaryRecipient().getAddress()));
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
2014-02-19 21:06:54 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static void sendMediaPush(Context context, Recipients recipients, long messageId) {
|
|
|
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
2017-07-26 09:59:15 -07:00
|
|
|
jobManager.add(new PushMediaSendJob(context, messageId, recipients.getPrimaryRecipient().getAddress()));
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
2014-02-19 21:06:54 -08:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private static void sendGroupPush(Context context, Recipients recipients, long messageId, Address filterAddress) {
|
2014-11-08 11:35:58 -08:00
|
|
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
2017-07-26 09:59:15 -07:00
|
|
|
jobManager.add(new PushGroupSendJob(context, messageId, recipients.getPrimaryRecipient().getAddress(), filterAddress));
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
2014-02-19 21:06:54 -08:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static void sendSms(Context context, Recipients recipients, long messageId) {
|
|
|
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
|
|
|
jobManager.add(new SmsSendJob(context, messageId, recipients.getPrimaryRecipient().getName()));
|
2014-02-19 21:06:54 -08:00
|
|
|
}
|
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static void sendMms(Context context, long messageId) {
|
|
|
|
JobManager jobManager = ApplicationContext.getInstance(context).getJobManager();
|
|
|
|
jobManager.add(new MmsSendJob(context, messageId));
|
|
|
|
}
|
2013-10-13 12:53:41 +02:00
|
|
|
|
2014-11-08 11:35:58 -08:00
|
|
|
private static boolean isPushTextSend(Context context, Recipients recipients, boolean keyExchange) {
|
2017-07-26 09:59:15 -07:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
if (keyExchange) {
|
2014-11-08 11:35:58 -08:00
|
|
|
return false;
|
2013-10-13 12:53:41 +02:00
|
|
|
}
|
2017-07-26 09:59:15 -07:00
|
|
|
|
|
|
|
return isPushDestination(context, recipients.getPrimaryRecipient().getAddress());
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isPushMediaSend(Context context, Recipients recipients) {
|
2017-07-26 09:59:15 -07:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-08 11:35:58 -08:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
if (recipients.getRecipientsList().size() > 1) {
|
2014-11-08 11:35:58 -08:00
|
|
|
return false;
|
|
|
|
}
|
2017-07-26 09:59:15 -07:00
|
|
|
|
|
|
|
return isPushDestination(context, recipients.getPrimaryRecipient().getAddress());
|
2014-11-08 11:35:58 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private static boolean isGroupPushSend(Recipients recipients) {
|
2017-07-26 09:59:15 -07:00
|
|
|
return recipients.getPrimaryRecipient().getAddress().isGroup();
|
2013-10-13 12:53:41 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 18:28:03 -07:00
|
|
|
private static boolean isSelfSend(Context context, Recipients recipients) {
|
2016-02-14 14:58:39 +01:00
|
|
|
if (!TextSecurePreferences.isPushRegistered(context)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-22 18:28:03 -07:00
|
|
|
|
2016-02-14 14:58:39 +01:00
|
|
|
if (!recipients.isSingleRecipient()) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-11-12 12:28:11 -08:00
|
|
|
|
2016-02-14 14:58:39 +01:00
|
|
|
if (recipients.isGroupRecipient()) {
|
2014-10-22 18:28:03 -07:00
|
|
|
return false;
|
|
|
|
}
|
2016-02-14 14:58:39 +01:00
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
return Util.isOwnNumber(context, recipients.getPrimaryRecipient().getAddress());
|
2014-10-22 18:28:03 -07:00
|
|
|
}
|
|
|
|
|
2017-07-26 09:59:15 -07:00
|
|
|
private static boolean isPushDestination(Context context, Address destination) {
|
2014-11-09 20:35:08 -08:00
|
|
|
TextSecureDirectory directory = TextSecureDirectory.getInstance(context);
|
2014-11-08 11:35:58 -08:00
|
|
|
|
|
|
|
try {
|
2015-09-29 14:26:37 -07:00
|
|
|
return directory.isSecureTextSupported(destination);
|
2014-11-08 11:35:58 -08:00
|
|
|
} catch (NotInDirectoryException e) {
|
|
|
|
try {
|
2016-12-20 09:55:52 -08:00
|
|
|
SignalServiceAccountManager accountManager = AccountManagerFactory.createManager(context);
|
2017-07-26 09:59:15 -07:00
|
|
|
Optional<ContactTokenDetails> registeredUser = accountManager.getContact(destination.serialize());
|
2014-11-09 20:35:08 -08:00
|
|
|
|
|
|
|
if (!registeredUser.isPresent()) {
|
|
|
|
registeredUser = Optional.of(new ContactTokenDetails());
|
2017-07-26 09:59:15 -07:00
|
|
|
registeredUser.get().setNumber(destination.serialize());
|
2014-11-09 20:35:08 -08:00
|
|
|
directory.setNumber(registeredUser.get(), false);
|
2014-11-08 11:35:58 -08:00
|
|
|
return false;
|
|
|
|
} else {
|
2017-07-26 09:59:15 -07:00
|
|
|
registeredUser.get().setNumber(destination.toPhoneString());
|
2014-11-09 20:35:08 -08:00
|
|
|
directory.setNumber(registeredUser.get(), true);
|
2014-11-08 11:35:58 -08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} catch (IOException e1) {
|
|
|
|
Log.w(TAG, e1);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-20 10:20:44 -08:00
|
|
|
}
|