import org.asamk.signal.manager.util.KeyUtils;
import org.asamk.signal.manager.util.StickerUtils;
import org.asamk.signal.manager.util.Utils;
-import org.signal.libsignal.metadata.ProtocolUntrustedIdentityException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.whispersystems.libsignal.IdentityKey;
private final IncomingMessageHandler incomingMessageHandler;
private final Context context;
+ private boolean hasCaughtUpWithOldMessages = false;
Manager(
SignalAccount account,
return LEGACY_LOCK::unlock;
}
};
- this.dependencies = new SignalDependencies(account.getSelfAddress(),
- serviceEnvironmentConfig,
+ this.dependencies = new SignalDependencies(serviceEnvironmentConfig,
userAgent,
credentialsProvider,
account.getSignalProtocolStore(),
avatarStore,
account.getProfileStore()::getProfileKey,
unidentifiedAccessHelper::getAccessFor,
- dependencies::getProfileService,
- dependencies::getMessageReceiver,
this::resolveSignalServiceAddress);
final GroupV2Helper groupV2Helper = new GroupV2Helper(profileHelper::getRecipientProfileKeyCredential,
this::getRecipientProfile,
this::resolveSignalServiceAddress);
this.context = new Context(account,
- dependencies.getAccountManager(),
- dependencies.getMessageReceiver(),
+ dependencies,
stickerPackStore,
sendHelper,
groupHelper,
}
public void deleteAccount() throws IOException {
+ try {
+ pinHelper.removeRegistrationLockPin();
+ } catch (UnauthenticatedResponseException e) {
+ logger.warn("Failed to remove registration lock pin");
+ }
+ account.setRegistrationLockPin(null, null);
+
dependencies.getAccountManager().deleteAccount();
account.setRegistered(false);
) {
var envelope = cachedMessage.loadEnvelope();
if (envelope == null) {
+ cachedMessage.delete();
return null;
}
- SignalServiceContent content = null;
- List<HandleAction> actions = null;
- if (!envelope.isReceipt()) {
- try {
- content = dependencies.getCipher().decrypt(envelope);
- } catch (ProtocolUntrustedIdentityException e) {
- if (System.currentTimeMillis() - envelope.getServerDeliveredTimestamp() > 1000L * 60 * 60 * 24 * 30) {
- // Envelope is more than a month old, cleaning up.
- cachedMessage.delete();
- return null;
- }
- if (!envelope.hasSourceUuid()) {
- final var identifier = e.getSender();
- final var recipientId = account.getRecipientStore().resolveRecipient(identifier);
- try {
- account.getMessageCache().replaceSender(cachedMessage, recipientId);
- } catch (IOException ioException) {
- logger.warn("Failed to move cached message to recipient folder: {}", ioException.getMessage());
- }
- }
- return null;
- } catch (Exception er) {
- // All other errors are not recoverable, so delete the cached message
+
+ final var result = incomingMessageHandler.handleRetryEnvelope(envelope, ignoreAttachments, handler);
+ final var actions = result.first();
+ final var exception = result.second();
+
+ if (exception instanceof UntrustedIdentityException) {
+ if (System.currentTimeMillis() - envelope.getServerDeliveredTimestamp() > 1000L * 60 * 60 * 24 * 30) {
+ // Envelope is more than a month old, cleaning up.
cachedMessage.delete();
return null;
}
- actions = incomingMessageHandler.handleMessage(envelope, content, ignoreAttachments);
+ if (!envelope.hasSourceUuid()) {
+ final var identifier = ((UntrustedIdentityException) exception).getSender();
+ final var recipientId = account.getRecipientStore().resolveRecipient(identifier);
+ try {
+ account.getMessageCache().replaceSender(cachedMessage, recipientId);
+ } catch (IOException ioException) {
+ logger.warn("Failed to move cached message to recipient folder: {}", ioException.getMessage());
+ }
+ }
+ return null;
}
- handler.handleMessage(envelope, content, null);
+
+ // If successful and for all other errors that are not recoverable, delete the cached message
cachedMessage.delete();
return actions;
}
final var signalWebSocket = dependencies.getSignalWebSocket();
signalWebSocket.connect();
- var hasCaughtUpWithOldMessages = false;
+ hasCaughtUpWithOldMessages = false;
while (!Thread.interrupted()) {
SignalServiceEnvelope envelope;
envelope = result.get();
} else {
// Received indicator that server queue is empty
- hasCaughtUpWithOldMessages = true;
-
handleQueuedActions(queuedActions);
queuedActions.clear();
+ hasCaughtUpWithOldMessages = true;
+ synchronized (this) {
+ this.notifyAll();
+ }
+
// Continue to wait another timeout for new messages
continue;
}
handleQueuedActions(queuedActions);
}
+ public boolean hasCaughtUpWithOldMessages() {
+ return hasCaughtUpWithOldMessages;
+ }
+
private void handleQueuedActions(final Collection<HandleAction> queuedActions) {
+ var interrupted = false;
for (var action : queuedActions) {
try {
action.execute(context);
} catch (Throwable e) {
- if (e instanceof AssertionError && e.getCause() instanceof InterruptedException) {
- Thread.currentThread().interrupt();
+ if ((e instanceof AssertionError || e instanceof RuntimeException)
+ && e.getCause() instanceof InterruptedException) {
+ interrupted = true;
+ continue;
}
logger.warn("Message action failed.", e);
}
}
+ if (interrupted) {
+ Thread.currentThread().interrupt();
+ }
}
public boolean isContactBlocked(final RecipientIdentifier.Single recipient) {
}
public SignalServiceAddress resolveSignalServiceAddress(SignalServiceAddress address) {
- if (address.matches(account.getSelfAddress())) {
- return account.getSelfAddress();
- }
-
return resolveSignalServiceAddress(resolveRecipient(address));
}