Skip to content

Commit

Permalink
Merge branch 'molly-7.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
valldrac committed Feb 21, 2024
2 parents c3786d0 + 66b254d commit 821d257
Show file tree
Hide file tree
Showing 79 changed files with 3,011 additions and 2,923 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ apply {
from("fix-profm.gradle")
}

val canonicalVersionCode = 1394
val canonicalVersionName = "7.0.0"
val canonicalVersionCode = 1395
val canonicalVersionName = "7.0.1"
val mollyRevision = 1

val postFixSize = 100
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ class NewCallActivity : ContactSelectionActivity(), ContactSelectionListFragment
override fun onSelectionChanged() = Unit

override fun onBeforeContactSelected(isFromUnknownSearchKey: Boolean, recipientId: Optional<RecipientId?>, number: String?, callback: Consumer<Boolean?>) {
if (isFromUnknownSearchKey) {
if (recipientId.isPresent) {
launch(Recipient.resolved(recipientId.get()))
} else {
Log.i(TAG, "[onContactSelected] Maybe creating a new recipient.")
if (SignalStore.account().isRegistered) {
Log.i(TAG, "[onContactSelected] Doing contact refresh.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ protected void onRun() throws Exception {
ThreadUtil.sleep(10000);
}

Log.i(TAG, "[" + sentTimestamp + " ] Resending message to " + recipientId + " (urgent: " + urgent + ", contentHint: " + contentHint.name() + ", groupId: " + groupId + ", distributionId: " + distributionId + ")");

SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
Recipient recipient = Recipient.resolved(recipientId);

Expand Down Expand Up @@ -174,7 +176,20 @@ protected void onRun() throws Exception {
contentToSend = contentToSend.newBuilder().senderKeyDistributionMessage(distributionBytes).build();
}

SendMessageResult result = messageSender.resendContent(address, access, sentTimestamp, contentToSend, contentHint, Optional.ofNullable(groupId).map(GroupId::getDecodedId), urgent);
SendMessageResult result;

try {
result = messageSender.resendContent(address, access, sentTimestamp, contentToSend, contentHint, Optional.ofNullable(groupId).map(GroupId::getDecodedId), urgent);
} catch (IllegalStateException e) {
Log.w(TAG, "Failed to resend content. Archiving session and trying again.", e);
ApplicationDependencies.getProtocolStore().aci().sessions().archiveSessions(recipientId, SignalServiceAddress.DEFAULT_DEVICE_ID);
ApplicationDependencies.getProtocolStore().aci().sessions().archiveSiblingSessions(recipient.requireServiceId().toProtocolAddress(SignalServiceAddress.DEFAULT_DEVICE_ID));
ApplicationDependencies.getProtocolStore().pni().sessions().archiveSessions(recipientId, SignalServiceAddress.DEFAULT_DEVICE_ID);
ApplicationDependencies.getProtocolStore().pni().sessions().archiveSiblingSessions(recipient.requireServiceId().toProtocolAddress(SignalServiceAddress.DEFAULT_DEVICE_ID));
SignalDatabase.senderKeyShared().deleteAllFor(recipientId);

result = messageSender.resendContent(address, access, sentTimestamp, contentToSend, contentHint, Optional.ofNullable(groupId).map(GroupId::getDecodedId), urgent);
}

if (result.isSuccess() && distributionId != null) {
List<SignalProtocolAddress> addresses = result.getSuccess()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,12 @@ public final class LogSectionCapabilities implements LogSection {
RecipientRecord.Capabilities globalCapabilities = SignalDatabase.recipients().getCapabilities(self.getId());

StringBuilder builder = new StringBuilder().append("-- Local").append("\n")
.append("Sender Key : ").append(localCapabilities.getSenderKey()).append("\n")
.append("Announcement Groups: ").append(localCapabilities.getAnnouncementGroup()).append("\n")
.append("Change Number : ").append(localCapabilities.getChangeNumber()).append("\n")
.append("Stories : ").append(localCapabilities.getStories()).append("\n")
.append("Gift Badges : ").append(localCapabilities.getGiftBadges()).append("\n")
.append("PNP/PNI: ").append(localCapabilities.getPni()).append("\n")
.append("\n")
.append("-- Global").append("\n");

if (globalCapabilities != null) {
builder.append("GV1 Migration : ").append(globalCapabilities.getGroupsV1MigrationCapability()).append("\n")
.append("Sender Key : ").append(globalCapabilities.getSenderKeyCapability()).append("\n")
.append("Announcement Groups: ").append(globalCapabilities.getAnnouncementGroupCapability()).append("\n")
.append("Change Number : ").append(globalCapabilities.getChangeNumberCapability()).append("\n")
.append("Stories : ").append(globalCapabilities.getStoriesCapability()).append("\n")
.append("Gift Badges : ").append(globalCapabilities.getGiftBadgesCapability()).append("\n");
builder.append("PNP/PNI: ").append(globalCapabilities.getPnpCapability()).append("\n");
} else {
builder.append("Self not found!");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,11 @@ open class MessageContentProcessor(private val context: Context) {
private fun handleIndividualRetryReceipt(requester: Recipient, messageLogEntry: MessageLogEntry?, envelope: Envelope, metadata: EnvelopeMetadata, decryptionErrorMessage: DecryptionErrorMessage) {
var archivedSession = false

// TODO [pnp] Ignore retry receipts that have a PNI destinationUuid
if (ServiceId.parseOrNull(envelope.destinationServiceId) is ServiceId.PNI) {
warn(envelope.timestamp!!, "[RetryReceipt-I] Destination is our PNI. Ignoring.")
return
}

if (decryptionErrorMessage.ratchetKey.isPresent &&
ratchetKeyMatches(requester, metadata.sourceDeviceId, decryptionErrorMessage.ratchetKey.get())
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ private void onUiStateChanged(@NonNull UsernameEditViewModel.State state) {
case DISCRIMINATOR_TOO_LONG -> getString(R.string.UsernameEditFragment__invalid_username_enter_a_maximum_of_d_digits, UsernameUtil.MAX_DISCRIMINATOR_LENGTH);
case DISCRIMINATOR_TOO_SHORT -> getString(R.string.UsernameEditFragment__invalid_username_enter_a_minimum_of_d_digits, UsernameUtil.MIN_DISCRIMINATOR_LENGTH);
case DISCRIMINATOR_CANNOT_BE_00 -> getString(R.string.UsernameEditFragment__this_number_cant_be_00);
case DISCRIMINATOR_CANNOT_START_WITH_00 -> getString(R.string.UsernameEditFragment__this_number_cant_start_with_00);
case DISCRIMINATOR_CANNOT_START_WITH_0 -> getString(R.string.UsernameEditFragment__this_number_cant_start_with_0);
};

int colorRes = error != null ? R.color.signal_colorError : R.color.signal_colorPrimary;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import org.thoughtcrime.securesms.profiles.manage.UsernameRepository.UsernameSet
import org.thoughtcrime.securesms.util.NetworkUtil
import org.thoughtcrime.securesms.util.UsernameUtil.InvalidReason
import org.thoughtcrime.securesms.util.UsernameUtil.checkDiscriminator
import org.thoughtcrime.securesms.util.UsernameUtil.checkUsername
import org.thoughtcrime.securesms.util.UsernameUtil.checkNickname
import org.thoughtcrime.securesms.util.rx.RxStore
import org.whispersystems.signalservice.api.util.Usernames
import java.util.concurrent.TimeUnit
Expand Down Expand Up @@ -159,7 +159,7 @@ internal class UsernameEditViewModel private constructor(private val mode: Usern
return
}

val invalidReason: InvalidReason? = checkUsername(usernameState.getNickname())
val invalidReason: InvalidReason? = checkNickname(usernameState.getNickname())
if (invalidReason != null) {
Log.w(TAG, "Username was submitted, but did not pass validity checks. Reason: $invalidReason")
uiState.update { it.copy(buttonState = ButtonState.SUBMIT_DISABLED, usernameStatus = mapNicknameError(invalidReason)) }
Expand Down Expand Up @@ -257,7 +257,7 @@ internal class UsernameEditViewModel private constructor(private val mode: Usern
return
}

val invalidReason: InvalidReason? = checkUsername(nickname)
val invalidReason: InvalidReason? = checkNickname(nickname)
if (invalidReason != null) {
uiState.update { uiState ->
uiState.copy(
Expand Down Expand Up @@ -366,7 +366,7 @@ internal class UsernameEditViewModel private constructor(private val mode: Usern
DISCRIMINATOR_TOO_LONG,
DISCRIMINATOR_HAS_INVALID_CHARACTERS,
DISCRIMINATOR_CANNOT_BE_00,
DISCRIMINATOR_CANNOT_START_WITH_00
DISCRIMINATOR_CANNOT_START_WITH_0
}

enum class ButtonState {
Expand All @@ -386,15 +386,17 @@ internal class UsernameEditViewModel private constructor(private val mode: Usern
companion object {
private val TAG = Log.tag(UsernameEditViewModel::class.java)

private const val NICKNAME_PUBLISHER_DEBOUNCE_TIMEOUT_MILLIS: Long = 1000
private const val NICKNAME_PUBLISHER_DEBOUNCE_TIMEOUT_MILLIS: Long = 500

private fun mapNicknameError(invalidReason: InvalidReason): UsernameStatus {
return when (invalidReason) {
InvalidReason.TOO_SHORT -> UsernameStatus.TOO_SHORT
InvalidReason.TOO_LONG -> UsernameStatus.TOO_LONG
InvalidReason.STARTS_WITH_NUMBER -> UsernameStatus.CANNOT_START_WITH_NUMBER
InvalidReason.INVALID_CHARACTERS -> UsernameStatus.INVALID_CHARACTERS
InvalidReason.INVALID_NUMBER, InvalidReason.INVALID_NUMBER_PREFIX -> error("Unexpected reason $invalidReason")
InvalidReason.INVALID_NUMBER,
InvalidReason.INVALID_NUMBER_00,
InvalidReason.INVALID_NUMBER_PREFIX_0 -> error("Unexpected reason $invalidReason")
}
}

Expand All @@ -403,8 +405,8 @@ internal class UsernameEditViewModel private constructor(private val mode: Usern
InvalidReason.TOO_SHORT -> UsernameStatus.DISCRIMINATOR_TOO_SHORT
InvalidReason.TOO_LONG -> UsernameStatus.DISCRIMINATOR_TOO_LONG
InvalidReason.INVALID_CHARACTERS -> UsernameStatus.DISCRIMINATOR_HAS_INVALID_CHARACTERS
InvalidReason.INVALID_NUMBER -> UsernameStatus.DISCRIMINATOR_CANNOT_BE_00
InvalidReason.INVALID_NUMBER_PREFIX -> UsernameStatus.DISCRIMINATOR_CANNOT_START_WITH_00
InvalidReason.INVALID_NUMBER_00 -> UsernameStatus.DISCRIMINATOR_CANNOT_BE_00
InvalidReason.INVALID_NUMBER_PREFIX_0 -> UsernameStatus.DISCRIMINATOR_CANNOT_START_WITH_0
else -> UsernameStatus.INVALID_GENERIC
}
}
Expand Down
117 changes: 77 additions & 40 deletions app/src/main/java/org/thoughtcrime/securesms/util/UsernameUtil.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,19 @@
package org.thoughtcrime.securesms.util

import org.signal.core.util.logging.Log
import org.signal.libsignal.usernames.BadDiscriminatorCharacterException
import org.signal.libsignal.usernames.BadNicknameCharacterException
import org.signal.libsignal.usernames.BaseUsernameException
import org.signal.libsignal.usernames.CannotBeEmptyException
import org.signal.libsignal.usernames.CannotStartWithDigitException
import org.signal.libsignal.usernames.DiscriminatorCannotBeEmptyException
import org.signal.libsignal.usernames.DiscriminatorCannotBeSingleDigitException
import org.signal.libsignal.usernames.DiscriminatorCannotBeZeroException
import org.signal.libsignal.usernames.DiscriminatorCannotHaveLeadingZerosException
import org.signal.libsignal.usernames.DiscriminatorTooLargeException
import org.signal.libsignal.usernames.NicknameTooLongException
import org.signal.libsignal.usernames.NicknameTooShortException
import org.signal.libsignal.usernames.Username
import java.util.Locale
import java.util.regex.Pattern

Expand Down Expand Up @@ -36,52 +49,75 @@ object UsernameUtil {
}

@JvmStatic
fun checkUsername(value: String?): InvalidReason? {
return when {
value == null -> {
InvalidReason.TOO_SHORT
}
value.length < MIN_NICKNAME_LENGTH -> {
InvalidReason.TOO_SHORT
}
value.length > MAX_NICKNAME_LENGTH -> {
InvalidReason.TOO_LONG
}
DIGIT_START_PATTERN.matcher(value).matches() -> {
InvalidReason.STARTS_WITH_NUMBER
}
!FULL_PATTERN.matcher(value).matches() -> {
InvalidReason.INVALID_CHARACTERS
}
else -> {
null
}
fun checkNickname(value: String?): InvalidReason? {
if (value == null) {
return InvalidReason.TOO_SHORT
}

return try {
// We only want to check the nickname, so we pass in a known-valid discriminator
Username.fromParts(value, "01", MIN_NICKNAME_LENGTH, MAX_NICKNAME_LENGTH)
null
} catch (e: BadNicknameCharacterException) {
InvalidReason.INVALID_CHARACTERS
} catch (e: CannotBeEmptyException) {
InvalidReason.TOO_SHORT
} catch (e: CannotStartWithDigitException) {
InvalidReason.STARTS_WITH_NUMBER
} catch (e: NicknameTooLongException) {
InvalidReason.TOO_LONG
} catch (e: NicknameTooShortException) {
InvalidReason.TOO_SHORT
} catch (e: BaseUsernameException) {
Log.w(TAG, "Unhandled verification exception!", e)
InvalidReason.INVALID_CHARACTERS
}
}

fun checkDiscriminator(value: String?): InvalidReason? {
return when {
value == null -> {
null
}
value == "00" -> {
InvalidReason.INVALID_NUMBER
}
value.startsWith("00") -> {
InvalidReason.INVALID_NUMBER_PREFIX
}
value.length < MIN_DISCRIMINATOR_LENGTH -> {
if (value == null) {
return null
}

if (value.length < MIN_DISCRIMINATOR_LENGTH) {
return InvalidReason.TOO_SHORT
}

if (value.length > MAX_DISCRIMINATOR_LENGTH) {
return InvalidReason.TOO_LONG
}

return try {
// We only want to check the discriminator, so we pass in a known-valid nickname
Username.fromParts("spiderman", value, MIN_NICKNAME_LENGTH, MAX_NICKNAME_LENGTH)
null
} catch (e: BadDiscriminatorCharacterException) {
InvalidReason.INVALID_CHARACTERS
} catch (e: DiscriminatorCannotBeEmptyException) {
InvalidReason.TOO_SHORT
} catch (e: DiscriminatorCannotBeSingleDigitException) {
InvalidReason.TOO_SHORT
} catch (e: DiscriminatorCannotBeZeroException) {
if (value.length < 2) {
InvalidReason.TOO_SHORT
} else if (value == "00") {
InvalidReason.INVALID_NUMBER_00
} else {
InvalidReason.INVALID_NUMBER_PREFIX_0
}
value.length > MAX_DISCRIMINATOR_LENGTH -> {
InvalidReason.TOO_LONG
}
value.toIntOrNull() == null -> {
InvalidReason.INVALID_CHARACTERS
}
else -> {
null
} catch (e: DiscriminatorCannotHaveLeadingZerosException) {
if (value.length < 2) {
InvalidReason.TOO_SHORT
} else if (value == "00") {
InvalidReason.INVALID_NUMBER_00
} else {
InvalidReason.INVALID_NUMBER_PREFIX_0
}
} catch (e: DiscriminatorTooLargeException) {
InvalidReason.TOO_LONG
} catch (e: BaseUsernameException) {
Log.w(TAG, "Unhandled verification exception!", e)
InvalidReason.INVALID_CHARACTERS
}
}

Expand All @@ -91,6 +127,7 @@ object UsernameUtil {
INVALID_CHARACTERS,
STARTS_WITH_NUMBER,
INVALID_NUMBER,
INVALID_NUMBER_PREFIX
INVALID_NUMBER_00,
INVALID_NUMBER_PREFIX_0
}
}
Loading

0 comments on commit 821d257

Please sign in to comment.