mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-04 05:00:39 +00:00
Merge branch 'AsamK:master' into dev-http
This commit is contained in:
commit
48b4fd5b6f
22 changed files with 250 additions and 91 deletions
|
@ -23,6 +23,7 @@ import org.asamk.signal.dbus.DbusProvisioningManagerImpl;
|
|||
import org.asamk.signal.dbus.DbusRegistrationManagerImpl;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.RegistrationManager;
|
||||
import org.asamk.signal.manager.Settings;
|
||||
import org.asamk.signal.manager.SignalAccountFiles;
|
||||
import org.asamk.signal.manager.api.AccountCheckException;
|
||||
import org.asamk.signal.manager.api.NotRegisteredException;
|
||||
|
@ -101,6 +102,10 @@ public class App {
|
|||
.type(Arguments.enumStringType(TrustNewIdentityCli.class))
|
||||
.setDefault(TrustNewIdentityCli.ON_FIRST_USE);
|
||||
|
||||
parser.addArgument("--disable-send-log")
|
||||
.help("Disable message send log (for resending messages that recipient couldn't decrypt)")
|
||||
.action(Arguments.storeTrue());
|
||||
|
||||
var subparsers = parser.addSubparsers().title("subcommands").dest("command");
|
||||
|
||||
Commands.getCommandSubparserAttachers().forEach((key, value) -> {
|
||||
|
@ -167,12 +172,14 @@ public class App {
|
|||
? TrustNewIdentity.ON_FIRST_USE
|
||||
: trustNewIdentityCli == TrustNewIdentityCli.ALWAYS ? TrustNewIdentity.ALWAYS : TrustNewIdentity.NEVER;
|
||||
|
||||
final var disableSendLog = Boolean.TRUE.equals(ns.getBoolean("disable-send-log"));
|
||||
|
||||
final SignalAccountFiles signalAccountFiles;
|
||||
try {
|
||||
signalAccountFiles = new SignalAccountFiles(configPath,
|
||||
serviceEnvironment,
|
||||
BaseConfig.USER_AGENT,
|
||||
trustNewIdentity);
|
||||
new Settings(trustNewIdentity, disableSendLog));
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Failed to read local accounts list", e);
|
||||
}
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
package org.asamk.signal.commands;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
|
||||
import net.sourceforge.argparse4j.impl.Arguments;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
@ -19,9 +21,11 @@ import org.slf4j.LoggerFactory;
|
|||
|
||||
import java.io.IOException;
|
||||
import java.time.Duration;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public class ReceiveCommand implements LocalCommand {
|
||||
public class ReceiveCommand implements LocalCommand, JsonRpcSingleCommand<ReceiveCommand.ReceiveParams> {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(ReceiveCommand.class);
|
||||
|
||||
|
@ -37,6 +41,10 @@ public class ReceiveCommand implements LocalCommand {
|
|||
.type(double.class)
|
||||
.setDefault(3.0)
|
||||
.help("Number of seconds to wait for new messages (negative values disable timeout)");
|
||||
subparser.addArgument("--max-messages")
|
||||
.type(int.class)
|
||||
.setDefault(-1)
|
||||
.help("Maximum number of messages to receive, before returning.");
|
||||
subparser.addArgument("--ignore-attachments")
|
||||
.help("Don’t download attachments of received messages.")
|
||||
.action(Arguments.storeTrue());
|
||||
|
@ -58,6 +66,7 @@ public class ReceiveCommand implements LocalCommand {
|
|||
final Namespace ns, final Manager m, final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
final var timeout = ns.getDouble("timeout");
|
||||
final var maxMessagesRaw = ns.getInt("max-messages");
|
||||
final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
|
||||
final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories"));
|
||||
final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts"));
|
||||
|
@ -65,13 +74,37 @@ public class ReceiveCommand implements LocalCommand {
|
|||
try {
|
||||
final var handler = outputWriter instanceof JsonWriter ? new JsonReceiveMessageHandler(m,
|
||||
(JsonWriter) outputWriter) : new ReceiveMessageHandler(m, (PlainTextWriter) outputWriter);
|
||||
if (timeout < 0) {
|
||||
m.receiveMessages(handler);
|
||||
} else {
|
||||
m.receiveMessages(Duration.ofMillis((long) (timeout * 1000)), handler);
|
||||
}
|
||||
final var duration = timeout < 0 ? null : Duration.ofMillis((long) (timeout * 1000));
|
||||
final var maxMessages = maxMessagesRaw < 0 ? null : maxMessagesRaw;
|
||||
m.receiveMessages(Optional.ofNullable(duration), Optional.ofNullable(maxMessages), handler);
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Error while receiving messages: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public TypeReference<ReceiveParams> getRequestType() {
|
||||
return new TypeReference<>() {};
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handleCommand(
|
||||
final ReceiveParams request, final Manager m, final JsonWriter jsonWriter
|
||||
) throws CommandException {
|
||||
final var timeout = request.timeout() == null ? 3.0 : request.timeout();
|
||||
final var maxMessagesRaw = request.maxMessages() == null ? -1 : request.maxMessages();
|
||||
|
||||
try {
|
||||
final var messages = new ArrayList<>();
|
||||
final var handler = new JsonReceiveMessageHandler(m, messages::add);
|
||||
final var duration = timeout < 0 ? null : Duration.ofMillis((long) (timeout * 1000));
|
||||
final var maxMessages = maxMessagesRaw < 0 ? null : maxMessagesRaw;
|
||||
m.receiveMessages(Optional.ofNullable(duration), Optional.ofNullable(maxMessages), handler);
|
||||
jsonWriter.write(messages);
|
||||
} catch (IOException e) {
|
||||
throw new IOErrorException("Error while receiving messages: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
record ReceiveParams(Double timeout, Integer maxMessages) {}
|
||||
}
|
||||
|
|
|
@ -80,6 +80,10 @@ public class SendCommand implements JsonRpcLocalCommand {
|
|||
subparser.addArgument("--preview-title").help("Specify the title for the link preview (mandatory).");
|
||||
subparser.addArgument("--preview-description").help("Specify the description for the link preview (optional).");
|
||||
subparser.addArgument("--preview-image").help("Specify the image file for the link preview (optional).");
|
||||
subparser.addArgument("--story-timestamp")
|
||||
.type(long.class)
|
||||
.help("Specify the timestamp of a story to reply to.");
|
||||
subparser.addArgument("--story-author").help("Specify the number of the author of the story.");
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -170,18 +174,30 @@ public class SendCommand implements JsonRpcLocalCommand {
|
|||
previews = List.of();
|
||||
}
|
||||
|
||||
final Message.StoryReply storyReply;
|
||||
final var storyReplyTimestamp = ns.getLong("story-timestamp");
|
||||
if (storyReplyTimestamp != null) {
|
||||
final var storyAuthor = ns.getString("story-author");
|
||||
storyReply = new Message.StoryReply(storyReplyTimestamp,
|
||||
CommandUtil.getSingleRecipientIdentifier(storyAuthor, m.getSelfNumber()));
|
||||
} else {
|
||||
storyReply = null;
|
||||
}
|
||||
|
||||
if (messageText.isEmpty() && attachments.isEmpty() && sticker == null && quote == null) {
|
||||
throw new UserErrorException(
|
||||
"Sending empty message is not allowed, either a message, attachment or sticker must be given.");
|
||||
}
|
||||
|
||||
try {
|
||||
var results = m.sendMessage(new Message(messageText,
|
||||
final var message = new Message(messageText,
|
||||
attachments,
|
||||
mentions,
|
||||
Optional.ofNullable(quote),
|
||||
Optional.ofNullable(sticker),
|
||||
previews), recipientIdentifiers);
|
||||
previews,
|
||||
Optional.ofNullable((storyReply)));
|
||||
var results = m.sendMessage(message, recipientIdentifiers);
|
||||
outputResult(outputWriter, results);
|
||||
} catch (AttachmentInvalidException | IOException e) {
|
||||
throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
|
||||
|
|
|
@ -45,6 +45,9 @@ public class SendReactionCommand implements JsonRpcLocalCommand {
|
|||
.type(long.class)
|
||||
.help("Specify the timestamp of the message to which to react.");
|
||||
subparser.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments.storeTrue());
|
||||
subparser.addArgument("--story")
|
||||
.help("React to a story instead of a normal message")
|
||||
.action(Arguments.storeTrue());
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -64,13 +67,15 @@ public class SendReactionCommand implements JsonRpcLocalCommand {
|
|||
final var isRemove = Boolean.TRUE.equals(ns.getBoolean("remove"));
|
||||
final var targetAuthor = ns.getString("target-author");
|
||||
final var targetTimestamp = ns.getLong("target-timestamp");
|
||||
final var isStory = Boolean.TRUE.equals(ns.getBoolean("story"));
|
||||
|
||||
try {
|
||||
final var results = m.sendMessageReaction(emoji,
|
||||
isRemove,
|
||||
CommandUtil.getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()),
|
||||
targetTimestamp,
|
||||
recipientIdentifiers);
|
||||
recipientIdentifiers,
|
||||
isStory);
|
||||
outputResult(outputWriter, results);
|
||||
} catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
|
||||
throw new UserErrorException(e.getMessage());
|
||||
|
|
|
@ -58,6 +58,7 @@ import java.util.Map;
|
|||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
@ -364,7 +365,8 @@ public class DbusManagerImpl implements Manager {
|
|||
final boolean remove,
|
||||
final RecipientIdentifier.Single targetAuthor,
|
||||
final long targetSentTimestamp,
|
||||
final Set<RecipientIdentifier> recipients
|
||||
final Set<RecipientIdentifier> recipients,
|
||||
final boolean isStory
|
||||
) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException {
|
||||
return handleMessage(recipients,
|
||||
numbers -> signal.sendMessageReaction(emoji,
|
||||
|
@ -496,39 +498,46 @@ public class DbusManagerImpl implements Manager {
|
|||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessages(final ReceiveMessageHandler handler) throws IOException {
|
||||
addReceiveHandler(handler);
|
||||
try {
|
||||
synchronized (this) {
|
||||
this.wait();
|
||||
}
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
removeReceiveHandler(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessages(
|
||||
final Duration timeout, final ReceiveMessageHandler handler
|
||||
Optional<Duration> timeout, Optional<Integer> maxMessages, ReceiveMessageHandler handler
|
||||
) throws IOException {
|
||||
final var remainingMessages = new AtomicInteger(maxMessages.orElse(-1));
|
||||
final var lastMessage = new AtomicLong(System.currentTimeMillis());
|
||||
final var thread = Thread.currentThread();
|
||||
|
||||
final ReceiveMessageHandler receiveHandler = (envelope, e) -> {
|
||||
lastMessage.set(System.currentTimeMillis());
|
||||
handler.handleMessage(envelope, e);
|
||||
if (remainingMessages.get() > 0) {
|
||||
if (remainingMessages.decrementAndGet() <= 0) {
|
||||
remainingMessages.set(0);
|
||||
thread.interrupt();
|
||||
}
|
||||
}
|
||||
};
|
||||
addReceiveHandler(receiveHandler);
|
||||
while (true) {
|
||||
try {
|
||||
final var sleepTimeRemaining = timeout.toMillis() - (System.currentTimeMillis() - lastMessage.get());
|
||||
if (sleepTimeRemaining < 0) {
|
||||
break;
|
||||
if (timeout.isPresent()) {
|
||||
while (remainingMessages.get() != 0) {
|
||||
try {
|
||||
final var passedTime = System.currentTimeMillis() - lastMessage.get();
|
||||
final var sleepTimeRemaining = timeout.get().toMillis() - passedTime;
|
||||
if (sleepTimeRemaining < 0) {
|
||||
break;
|
||||
}
|
||||
Thread.sleep(sleepTimeRemaining);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
} else {
|
||||
try {
|
||||
synchronized (this) {
|
||||
this.wait();
|
||||
}
|
||||
Thread.sleep(sleepTimeRemaining);
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
}
|
||||
|
||||
removeReceiveHandler(receiveHandler);
|
||||
}
|
||||
|
||||
|
|
|
@ -218,7 +218,8 @@ public class DbusSignalImpl implements Signal {
|
|||
List.of(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of()),
|
||||
List.of(),
|
||||
Optional.empty()),
|
||||
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
|
||||
.map(RecipientIdentifier.class::cast)
|
||||
.collect(Collectors.toSet()));
|
||||
|
@ -287,7 +288,8 @@ public class DbusSignalImpl implements Signal {
|
|||
targetSentTimestamp,
|
||||
getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream()
|
||||
.map(RecipientIdentifier.class::cast)
|
||||
.collect(Collectors.toSet()));
|
||||
.collect(Collectors.toSet()),
|
||||
false);
|
||||
checkSendMessageResults(results);
|
||||
return results.timestamp();
|
||||
} catch (IOException e) {
|
||||
|
@ -385,7 +387,8 @@ public class DbusSignalImpl implements Signal {
|
|||
List.of(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
|
||||
List.of(),
|
||||
Optional.empty()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE));
|
||||
checkSendMessageResults(results);
|
||||
return results.timestamp();
|
||||
} catch (AttachmentInvalidException e) {
|
||||
|
@ -427,7 +430,8 @@ public class DbusSignalImpl implements Signal {
|
|||
List.of(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of()), Set.of(getGroupRecipientIdentifier(groupId)));
|
||||
List.of(),
|
||||
Optional.empty()), Set.of(getGroupRecipientIdentifier(groupId)));
|
||||
checkSendMessageResults(results);
|
||||
return results.timestamp();
|
||||
} catch (IOException | InvalidStickerException e) {
|
||||
|
@ -485,7 +489,8 @@ public class DbusSignalImpl implements Signal {
|
|||
remove,
|
||||
getSingleRecipientIdentifier(targetAuthor, m.getSelfNumber()),
|
||||
targetSentTimestamp,
|
||||
Set.of(getGroupRecipientIdentifier(groupId)));
|
||||
Set.of(getGroupRecipientIdentifier(groupId)),
|
||||
false);
|
||||
checkSendMessageResults(results);
|
||||
return results.timestamp();
|
||||
} catch (IOException e) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue