From ec945cd227935470817027b262fc1ddc648cc380 Mon Sep 17 00:00:00 2001 From: AsamK Date: Sun, 20 Feb 2022 14:18:20 +0100 Subject: [PATCH] Add --message-from-stdin flag for send command --- README.md | 5 ++-- man/signal-cli.1.adoc | 8 ++++-- .../asamk/signal/commands/SendCommand.java | 25 ++++++++++--------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 3632d08d9e..e6f74c4602 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,8 @@ System requirements: - at least Java Runtime Environment (JRE) 17 - native library: libsignal-client - The native libs are bundled for x86_64 Linux (with recent enough glibc, see #643), Windows and MacOS. For other systems/architectures + The native libs are bundled for x86_64 Linux (with recent enough glibc, see #643), Windows and MacOS. For other + systems/architectures see: [Provide native lib for libsignal](https://github.com/AsamK/signal-cli/wiki/Provide-native-lib-for-libsignal) ### Install system-wide on Linux @@ -75,7 +76,7 @@ of all country codes.) * Pipe the message content from another process. - uname -a | signal-cli -a ACCOUNT send RECIPIENT + uname -a | signal-cli -a ACCOUNT send --message-from-stdin RECIPIENT * Receive messages diff --git a/man/signal-cli.1.adoc b/man/signal-cli.1.adoc index 02ae4ca44b..4754d4e5b2 100644 --- a/man/signal-cli.1.adoc +++ b/man/signal-cli.1.adoc @@ -212,7 +212,11 @@ Send the message to self without notification. Specify the recipient group ID in base64 encoding. *-m* MESSAGE, *--message* MESSAGE:: -Specify the message, if missing, standard input is used. +Specify the message. +Currently, signal-cli reads the message from stdin if `-m` is missing, but this will change in a future version and the explicit flag `--message-from-stdin` should be used instead. + +*--message-from-stdin*:: +Read the message from standard input. *-a* [ATTACHMENT [ATTACHMENT ...]], *--attachment* [ATTACHMENT [ATTACHMENT ...]]:: Add one or more files as attachment. @@ -578,7 +582,7 @@ Send a message to one or more recipients:: signal-cli -a ACCOUNT send -m "This is a message" [RECIPIENT [RECIPIENT ...]] [-a [ATTACHMENT [ATTACHMENT ...]]] Pipe the message content from another process:: -uname -a | signal-cli -a ACCOUNT send [RECIPIENT [RECIPIENT ...]] +uname -a | signal-cli -a ACCOUNT send --message-from-stdin [RECIPIENT [RECIPIENT ...]] Create a group:: signal-cli -a ACCOUNT updateGroup -n "Group name" -m [MEMBER [MEMBER ...]] diff --git a/src/main/java/org/asamk/signal/commands/SendCommand.java b/src/main/java/org/asamk/signal/commands/SendCommand.java index fda0c8e6fd..5e81047ae5 100644 --- a/src/main/java/org/asamk/signal/commands/SendCommand.java +++ b/src/main/java/org/asamk/signal/commands/SendCommand.java @@ -51,7 +51,11 @@ public void attachToSubparser(final Subparser subparser) { .help("Send the message to self without notification.") .action(Arguments.storeTrue()); - subparser.addArgument("-m", "--message").help("Specify the message, if missing standard input is used."); + var mut = subparser.addMutuallyExclusiveGroup(); + mut.addArgument("-m", "--message").help("Specify the message to be sent."); + mut.addArgument("--message-from-stdin") + .action(Arguments.storeTrue()) + .help("Read the message from standard input."); subparser.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment"); subparser.addArgument("-e", "--end-session", "--endsession") .help("Clear session state and send end session message.") @@ -107,16 +111,13 @@ public void handleCommand( final var sticker = stickerString == null ? null : parseSticker(stickerString); var messageText = ns.getString("message"); - if (messageText == null) { - if (sticker != null) { - messageText = ""; - } else { - logger.debug("Reading message from stdin..."); - try { - messageText = IOUtils.readAll(System.in, Charset.defaultCharset()); - } catch (IOException e) { - throw new UserErrorException("Failed to read message from stdin: " + e.getMessage()); - } + final var readMessageFromStdin = ns.getBoolean("message-from-stdin") == Boolean.TRUE; + if (readMessageFromStdin || (messageText == null && sticker == null)) { + logger.debug("Reading message from stdin..."); + try { + messageText = IOUtils.readAll(System.in, Charset.defaultCharset()); + } catch (IOException e) { + throw new UserErrorException("Failed to read message from stdin: " + e.getMessage()); } } @@ -146,7 +147,7 @@ public void handleCommand( } try { - var results = m.sendMessage(new Message(messageText, + var results = m.sendMessage(new Message(messageText == null ? "" : messageText, attachments, mentions, Optional.ofNullable(quote),