From 50c432aaf0b7deb8d8f4f890ff0a8542de5faf9f Mon Sep 17 00:00:00 2001 From: Colin Snover Date: Thu, 10 Dec 2015 23:45:07 -0600 Subject: [PATCH] Strip separators prior to validating non-email SMS recipients Refs #1516. Refs #3099. Refs #4833. Closes #4861 --- .../securesms/jobs/SmsSendJob.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/org/thoughtcrime/securesms/jobs/SmsSendJob.java b/src/org/thoughtcrime/securesms/jobs/SmsSendJob.java index 9ba215934c..f93c42b266 100644 --- a/src/org/thoughtcrime/securesms/jobs/SmsSendJob.java +++ b/src/org/thoughtcrime/securesms/jobs/SmsSendJob.java @@ -4,6 +4,7 @@ import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.net.Uri; +import android.telephony.PhoneNumberUtils; import android.telephony.SmsManager; import android.util.Log; @@ -19,7 +20,6 @@ import org.thoughtcrime.securesms.jobs.requirements.ServiceRequirement; import org.thoughtcrime.securesms.notifications.MessageNotifier; import org.thoughtcrime.securesms.recipients.Recipients; import org.thoughtcrime.securesms.service.SmsDeliveryListener; -import org.thoughtcrime.securesms.transport.InsecureFallbackApprovalException; import org.thoughtcrime.securesms.transport.UndeliverableMessageException; import org.thoughtcrime.securesms.util.NumberUtil; import org.thoughtcrime.securesms.util.TextSecurePreferences; @@ -78,26 +78,27 @@ public class SmsSendJob extends SendJob { private void deliver(SmsMessageRecord message) throws UndeliverableMessageException { - if (!NumberUtil.isValidSmsOrEmail(message.getIndividualRecipient().getNumber())) { - throw new UndeliverableMessageException("Not a valid SMS destination! " + message.getIndividualRecipient().getNumber()); - } - if (message.isSecure() || message.isKeyExchange() || message.isEndSession()) { throw new UndeliverableMessageException("Trying to send a secure SMS?"); } + String recipient = message.getIndividualRecipient().getNumber(); + + // See issue #1516 for bug report, and discussion on commits related to #4833 for problems + // related to the original fix to #1516. This still may not be a correct fix if networks allow + // SMS/MMS sending to alphanumeric recipients other than email addresses, but should also + // help to fix issue #3099. + if (!NumberUtil.isValidEmail(recipient)) { + recipient = PhoneNumberUtils.stripSeparators(PhoneNumberUtils.convertKeypadLettersToDigits(recipient)); + } + + if (!NumberUtil.isValidSmsOrEmail(recipient)) { + throw new UndeliverableMessageException("Not a valid SMS destination! " + recipient); + } + ArrayList messages = SmsManager.getDefault().divideMessage(message.getBody().getBody()); ArrayList sentIntents = constructSentIntents(message.getId(), message.getType(), messages, false); ArrayList deliveredIntents = constructDeliveredIntents(message.getId(), message.getType(), messages); - String recipient = message.getIndividualRecipient().getNumber(); - - // remove characters for visible number formatting - they break sending sms on some phones (issue #1516) - // stripSeparator() to rigid? what will people try to send here? - see discussion on #3099 - //recipient = PhoneNumberUtils.stripSeparators(recipient); - // minimum required to fix the issue - brackets are no dialable chars... - if (!NumberUtil.isValidEmail(recipient)) { - recipient = recipient.replaceAll("[^0-9+]", ""); - } // NOTE 11/04/14 -- There's apparently a bug where for some unknown recipients // and messages, this will throw an NPE. We have no idea why, so we're just