mirror of
https://github.com/AsamK/signal-cli
synced 2025-09-03 20:50:38 +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
|
@ -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());
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue