Add --ignore-stories flag to prevent receiving story messages

This commit is contained in:
AsamK 2022-10-19 19:09:23 +02:00
parent 3f7d8c60b9
commit 9ffacfe90e
8 changed files with 51 additions and 20 deletions

View file

@ -40,6 +40,8 @@ public class SignalDependencies {
private final ExecutorService executor; private final ExecutorService executor;
private final SignalSessionLock sessionLock; private final SignalSessionLock sessionLock;
private boolean allowStories = true;
private SignalServiceAccountManager accountManager; private SignalServiceAccountManager accountManager;
private GroupsV2Api groupsV2Api; private GroupsV2Api groupsV2Api;
private GroupsV2Operations groupsV2Operations; private GroupsV2Operations groupsV2Operations;
@ -72,6 +74,14 @@ public class SignalDependencies {
public void resetAfterAddressChange() { public void resetAfterAddressChange() {
this.messageSender = null; this.messageSender = null;
this.cipher = null; this.cipher = null;
getSignalWebSocket().forceNewWebSockets();
}
/**
* This method needs to be called before the first websocket is created
*/
public void setAllowStories(final boolean allowStories) {
this.allowStories = allowStories;
} }
public ServiceEnvironmentConfig getServiceEnvironmentConfig() { public ServiceEnvironmentConfig getServiceEnvironmentConfig() {
@ -135,7 +145,7 @@ public class SignalDependencies {
Optional.of(credentialsProvider), Optional.of(credentialsProvider),
userAgent, userAgent,
healthMonitor, healthMonitor,
true); allowStories);
} }
@Override @Override
@ -145,7 +155,7 @@ public class SignalDependencies {
Optional.empty(), Optional.empty(),
userAgent, userAgent,
healthMonitor, healthMonitor,
true); allowStories);
} }
}; };
signalWebSocket = new SignalWebSocket(webSocketFactory); signalWebSocket = new SignalWebSocket(webSocketFactory);

View file

@ -1,3 +1,3 @@
package org.asamk.signal.manager.api; package org.asamk.signal.manager.api;
public record ReceiveConfig(boolean ignoreAttachments, boolean sendReadReceipts) {} public record ReceiveConfig(boolean ignoreAttachments, boolean ignoreStories, boolean sendReadReceipts) {}

View file

@ -110,7 +110,6 @@ public class AccountHelper {
// TODO check and update remote storage // TODO check and update remote storage
context.getUnidentifiedAccessHelper().rotateSenderCertificates(); context.getUnidentifiedAccessHelper().rotateSenderCertificates();
dependencies.resetAfterAddressChange(); dependencies.resetAfterAddressChange();
dependencies.getSignalWebSocket().forceNewWebSockets();
context.getAccountFileUpdater().updateAccountIdentifiers(account.getNumber(), account.getAci()); context.getAccountFileUpdater().updateAccountIdentifiers(account.getNumber(), account.getAci());
} }

View file

@ -9,6 +9,7 @@ import org.asamk.signal.manager.storage.SignalAccount;
import org.asamk.signal.manager.storage.messageCache.CachedMessage; import org.asamk.signal.manager.storage.messageCache.CachedMessage;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.whispersystems.signalservice.api.SignalWebSocket;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState; import org.whispersystems.signalservice.api.websocket.WebSocketConnectionState;
import org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException; import org.whispersystems.signalservice.api.websocket.WebSocketUnavailableException;
@ -35,7 +36,7 @@ public class ReceiveHelper {
private final SignalDependencies dependencies; private final SignalDependencies dependencies;
private final Context context; private final Context context;
private ReceiveConfig receiveConfig = new ReceiveConfig(false, false); private ReceiveConfig receiveConfig = new ReceiveConfig(false, false, false);
private boolean needsToRetryFailedMessages = false; private boolean needsToRetryFailedMessages = false;
private boolean hasCaughtUpWithOldMessages = false; private boolean hasCaughtUpWithOldMessages = false;
private boolean isWaitingForMessage = false; private boolean isWaitingForMessage = false;
@ -51,6 +52,7 @@ public class ReceiveHelper {
public void setReceiveConfig(final ReceiveConfig receiveConfig) { public void setReceiveConfig(final ReceiveConfig receiveConfig) {
this.receiveConfig = receiveConfig; this.receiveConfig = receiveConfig;
dependencies.setAllowStories(!receiveConfig.ignoreStories());
} }
public void setNeedsToRetryFailedMessages(final boolean needsToRetryFailedMessages) { public void setNeedsToRetryFailedMessages(final boolean needsToRetryFailedMessages) {
@ -104,25 +106,24 @@ public class ReceiveHelper {
signalWebSocket.connect(); signalWebSocket.connect();
try { try {
receiveMessagesInternal(timeout, returnOnTimeout, handler, queuedActions); receiveMessagesInternal(signalWebSocket, timeout, returnOnTimeout, handler, queuedActions);
} finally { } finally {
hasCaughtUpWithOldMessages = false; hasCaughtUpWithOldMessages = false;
handleQueuedActions(queuedActions.keySet()); handleQueuedActions(queuedActions.keySet());
queuedActions.clear(); queuedActions.clear();
dependencies.getSignalWebSocket().disconnect(); signalWebSocket.disconnect();
webSocketStateDisposable.dispose(); webSocketStateDisposable.dispose();
shouldStop = false; shouldStop = false;
} }
} }
private void receiveMessagesInternal( private void receiveMessagesInternal(
final SignalWebSocket signalWebSocket,
Duration timeout, Duration timeout,
boolean returnOnTimeout, boolean returnOnTimeout,
Manager.ReceiveMessageHandler handler, Manager.ReceiveMessageHandler handler,
final Map<HandleAction, HandleAction> queuedActions final Map<HandleAction, HandleAction> queuedActions
) throws IOException { ) throws IOException {
final var signalWebSocket = dependencies.getSignalWebSocket();
var backOffCounter = 0; var backOffCounter = 0;
isWaitingForMessage = false; isWaitingForMessage = false;

View file

@ -359,9 +359,14 @@ In json mode this is outputted as one json object per line.
*-t* TIMEOUT, *--timeout* TIMEOUT:: *-t* TIMEOUT, *--timeout* TIMEOUT::
Number of seconds to wait for new messages (negative values disable timeout). Number of seconds to wait for new messages (negative values disable timeout).
Default is 5 seconds. Default is 5 seconds.
*--ignore-attachments*:: *--ignore-attachments*::
Dont download attachments of received messages. Dont download attachments of received messages.
*--ignore-stories*::
Dont receive story messages from the server.
*--send-read-receipts*:: *--send-read-receipts*::
Send read receipts for all incoming data messages (in addition to the default delivery receipts) Send read receipts for all incoming data messages (in addition to the default delivery receipts)
@ -639,6 +644,9 @@ See signal-cli-jsonrpc (5) for info on the JSON-RPC interface.
*--ignore-attachments*:: *--ignore-attachments*::
Dont download attachments of received messages. Dont download attachments of received messages.
*--ignore-stories*::
Dont receive story messages from the server.
*--send-read-receipts*:: *--send-read-receipts*::
Send read receipts for all incoming data messages (in addition to the default delivery receipts) Send read receipts for all incoming data messages (in addition to the default delivery receipts)

View file

@ -79,6 +79,9 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
subparser.addArgument("--ignore-attachments") subparser.addArgument("--ignore-attachments")
.help("Dont download attachments of received messages.") .help("Dont download attachments of received messages.")
.action(Arguments.storeTrue()); .action(Arguments.storeTrue());
subparser.addArgument("--ignore-stories")
.help("Dont receive story messages from the server.")
.action(Arguments.storeTrue());
subparser.addArgument("--send-read-receipts") subparser.addArgument("--send-read-receipts")
.help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)") .help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)")
.action(Arguments.storeTrue()); .action(Arguments.storeTrue());
@ -97,9 +100,10 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
final var noReceiveStdOut = Boolean.TRUE.equals(ns.getBoolean("no-receive-stdout")); final var noReceiveStdOut = Boolean.TRUE.equals(ns.getBoolean("no-receive-stdout"));
final var receiveMode = ns.<ReceiveMode>get("receive-mode"); final var receiveMode = ns.<ReceiveMode>get("receive-mode");
final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
final boolean sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts")); final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories"));
final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts"));
m.setReceiveConfig(new ReceiveConfig(ignoreAttachments, sendReadReceipts)); m.setReceiveConfig(new ReceiveConfig(ignoreAttachments, ignoreStories, sendReadReceipts));
addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START); addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START);
final Channel inheritedChannel; final Channel inheritedChannel;
@ -160,9 +164,10 @@ public class DaemonCommand implements MultiLocalCommand, LocalCommand {
final var noReceiveStdOut = Boolean.TRUE.equals(ns.getBoolean("no-receive-stdout")); final var noReceiveStdOut = Boolean.TRUE.equals(ns.getBoolean("no-receive-stdout"));
final var receiveMode = ns.<ReceiveMode>get("receive-mode"); final var receiveMode = ns.<ReceiveMode>get("receive-mode");
final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
final boolean sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts")); final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories"));
final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts"));
final var receiveConfig = new ReceiveConfig(ignoreAttachments, sendReadReceipts); final var receiveConfig = new ReceiveConfig(ignoreAttachments, ignoreStories, sendReadReceipts);
c.getManagers().forEach(m -> { c.getManagers().forEach(m -> {
m.setReceiveConfig(receiveConfig); m.setReceiveConfig(receiveConfig);
addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START); addDefaultReceiveHandler(m, noReceiveStdOut ? null : outputWriter, receiveMode != ReceiveMode.ON_START);

View file

@ -34,6 +34,9 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
subparser.addArgument("--ignore-attachments") subparser.addArgument("--ignore-attachments")
.help("Dont download attachments of received messages.") .help("Dont download attachments of received messages.")
.action(Arguments.storeTrue()); .action(Arguments.storeTrue());
subparser.addArgument("--ignore-stories")
.help("Dont receive story messages from the server.")
.action(Arguments.storeTrue());
subparser.addArgument("--send-read-receipts") subparser.addArgument("--send-read-receipts")
.help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)") .help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)")
.action(Arguments.storeTrue()); .action(Arguments.storeTrue());
@ -48,9 +51,10 @@ public class JsonRpcDispatcherCommand implements LocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
final boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
final boolean sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts")); final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories"));
m.setReceiveConfig(new ReceiveConfig(ignoreAttachments, sendReadReceipts)); final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts"));
m.setReceiveConfig(new ReceiveConfig(ignoreAttachments, ignoreStories, sendReadReceipts));
final var jsonOutputWriter = (JsonWriter) outputWriter; final var jsonOutputWriter = (JsonWriter) outputWriter;
final Supplier<String> lineSupplier = IOUtils.getLineSupplier(new InputStreamReader(System.in, final Supplier<String> lineSupplier = IOUtils.getLineSupplier(new InputStreamReader(System.in,

View file

@ -40,6 +40,9 @@ public class ReceiveCommand implements LocalCommand {
subparser.addArgument("--ignore-attachments") subparser.addArgument("--ignore-attachments")
.help("Dont download attachments of received messages.") .help("Dont download attachments of received messages.")
.action(Arguments.storeTrue()); .action(Arguments.storeTrue());
subparser.addArgument("--ignore-stories")
.help("Dont receive story messages from the server.")
.action(Arguments.storeTrue());
subparser.addArgument("--send-read-receipts") subparser.addArgument("--send-read-receipts")
.help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)") .help("Send read receipts for all incoming data messages (in addition to the default delivery receipts)")
.action(Arguments.storeTrue()); .action(Arguments.storeTrue());
@ -54,10 +57,11 @@ public class ReceiveCommand implements LocalCommand {
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter
) throws CommandException { ) throws CommandException {
double timeout = ns.getDouble("timeout"); final var timeout = ns.getDouble("timeout");
boolean ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments")); final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
boolean sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts")); final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories"));
m.setReceiveConfig(new ReceiveConfig(ignoreAttachments, sendReadReceipts)); final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts"));
m.setReceiveConfig(new ReceiveConfig(ignoreAttachments, ignoreStories, sendReadReceipts));
try { try {
final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m, final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m,
(JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter); (JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter);