mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 10:30:38 +00:00
Move all message decryption to IncomingMessageHandler
This commit is contained in:
parent
5743cf4455
commit
32150b1aaa
2 changed files with 68 additions and 38 deletions
|
@ -58,7 +58,6 @@ import org.asamk.signal.manager.storage.stickers.StickerPackId;
|
|||
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;
|
||||
|
@ -818,21 +817,22 @@ public class Manager implements Closeable {
|
|||
) {
|
||||
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) {
|
||||
|
||||
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;
|
||||
}
|
||||
if (!envelope.hasSourceUuid()) {
|
||||
final var identifier = e.getSender();
|
||||
final var identifier = ((UntrustedIdentityException) exception).getSender();
|
||||
final var recipientId = account.getRecipientStore().resolveRecipient(identifier);
|
||||
try {
|
||||
account.getMessageCache().replaceSender(cachedMessage, recipientId);
|
||||
|
@ -841,14 +841,9 @@ public class Manager implements Closeable {
|
|||
}
|
||||
}
|
||||
return null;
|
||||
} catch (Exception er) {
|
||||
// All other errors are not recoverable, so delete the cached message
|
||||
cachedMessage.delete();
|
||||
return null;
|
||||
}
|
||||
actions = incomingMessageHandler.handleMessage(envelope, content, ignoreAttachments);
|
||||
}
|
||||
handler.handleMessage(envelope, content, null);
|
||||
|
||||
// If successful and for all other errors that are not recoverable, delete the cached message
|
||||
cachedMessage.delete();
|
||||
return actions;
|
||||
}
|
||||
|
|
|
@ -79,6 +79,28 @@ public final class IncomingMessageHandler {
|
|||
this.jobExecutor = jobExecutor;
|
||||
}
|
||||
|
||||
public Pair<List<HandleAction>, Exception> handleRetryEnvelope(
|
||||
final SignalServiceEnvelope envelope,
|
||||
final boolean ignoreAttachments,
|
||||
final Manager.ReceiveMessageHandler handler
|
||||
) {
|
||||
SignalServiceContent content = null;
|
||||
if (!envelope.isReceipt()) {
|
||||
try {
|
||||
content = dependencies.getCipher().decrypt(envelope);
|
||||
} catch (ProtocolUntrustedIdentityException e) {
|
||||
final var recipientId = account.getRecipientStore().resolveRecipient(e.getSender());
|
||||
final var exception = new UntrustedIdentityException(addressResolver.resolveSignalServiceAddress(
|
||||
recipientId), e.getSenderDevice());
|
||||
return new Pair<>(List.of(), exception);
|
||||
} catch (Exception e) {
|
||||
return new Pair<>(List.of(), e);
|
||||
}
|
||||
}
|
||||
final var actions = checkAndHandleMessage(envelope, content, ignoreAttachments, handler, null);
|
||||
return new Pair<>(actions, null);
|
||||
}
|
||||
|
||||
public Pair<List<HandleAction>, Exception> handleEnvelope(
|
||||
final SignalServiceEnvelope envelope,
|
||||
final boolean ignoreAttachments,
|
||||
|
@ -108,35 +130,48 @@ public final class IncomingMessageHandler {
|
|||
} catch (Exception e) {
|
||||
exception = e;
|
||||
}
|
||||
}
|
||||
|
||||
actions.addAll(checkAndHandleMessage(envelope, content, ignoreAttachments, handler, exception));
|
||||
return new Pair<>(actions, exception);
|
||||
}
|
||||
|
||||
private List<HandleAction> checkAndHandleMessage(
|
||||
final SignalServiceEnvelope envelope,
|
||||
final SignalServiceContent content,
|
||||
final boolean ignoreAttachments,
|
||||
final Manager.ReceiveMessageHandler handler,
|
||||
final Exception exception
|
||||
) {
|
||||
if (!envelope.hasSourceUuid() && content != null) {
|
||||
// Store uuid if we don't have it already
|
||||
// address/uuid is validated by unidentified sender certificate
|
||||
account.getRecipientStore().resolveRecipientTrusted(content.getSender());
|
||||
}
|
||||
}
|
||||
|
||||
if (isMessageBlocked(envelope, content)) {
|
||||
logger.info("Ignoring a message from blocked user/group: {}", envelope.getTimestamp());
|
||||
return List.of();
|
||||
} else if (isNotAllowedToSendToGroup(envelope, content)) {
|
||||
logger.info("Ignoring a group message from an unauthorized sender (no member or admin): {} {}",
|
||||
(envelope.hasSourceUuid() ? envelope.getSourceAddress() : content.getSender()).getIdentifier(),
|
||||
envelope.getTimestamp());
|
||||
return List.of();
|
||||
} else {
|
||||
actions.addAll(handleMessage(envelope, content, ignoreAttachments));
|
||||
handler.handleMessage(envelope, content, exception);
|
||||
List<HandleAction> actions;
|
||||
if (content != null) {
|
||||
actions = handleMessage(envelope, content, ignoreAttachments);
|
||||
} else {
|
||||
actions = List.of();
|
||||
}
|
||||
handler.handleMessage(envelope, content, exception);
|
||||
return actions;
|
||||
}
|
||||
return new Pair<>(actions, exception);
|
||||
}
|
||||
|
||||
public List<HandleAction> handleMessage(
|
||||
SignalServiceEnvelope envelope, SignalServiceContent content, boolean ignoreAttachments
|
||||
) {
|
||||
var actions = new ArrayList<HandleAction>();
|
||||
if (content == null) {
|
||||
return actions;
|
||||
}
|
||||
|
||||
final RecipientId sender;
|
||||
if (!envelope.isUnidentifiedSender() && envelope.hasSourceUuid()) {
|
||||
sender = recipientResolver.resolveRecipient(envelope.getSourceAddress());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue