1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
8 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
9 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
10 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
11 import org
.asamk
.signal
.manager
.Manager
;
12 import org
.asamk
.signal
.manager
.api
.Message
;
13 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
14 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
15 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
16 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
17 import org
.asamk
.signal
.output
.JsonWriter
;
18 import org
.asamk
.signal
.output
.OutputWriter
;
19 import org
.asamk
.signal
.output
.PlainTextWriter
;
20 import org
.asamk
.signal
.util
.CommandUtil
;
21 import org
.asamk
.signal
.util
.ErrorUtils
;
22 import org
.asamk
.signal
.util
.IOUtils
;
23 import org
.slf4j
.Logger
;
24 import org
.slf4j
.LoggerFactory
;
26 import java
.io
.IOException
;
27 import java
.nio
.charset
.Charset
;
28 import java
.util
.ArrayList
;
29 import java
.util
.List
;
31 import java
.util
.Optional
;
32 import java
.util
.regex
.Pattern
;
33 import java
.util
.stream
.Collectors
;
35 public class SendCommand
implements JsonRpcLocalCommand
{
37 private final static Logger logger
= LoggerFactory
.getLogger(SendCommand
.class);
40 public String
getName() {
45 public void attachToSubparser(final Subparser subparser
) {
46 subparser
.help("Send a message to another user or group.");
47 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
48 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
49 subparser
.addArgument("--note-to-self")
50 .help("Send the message to self without notification.")
51 .action(Arguments
.storeTrue());
53 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
54 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
55 subparser
.addArgument("-e", "--end-session", "--endsession")
56 .help("Clear session state and send end session message.")
57 .action(Arguments
.storeTrue());
58 subparser
.addArgument("--mention")
60 .help("Mention another group member (syntax: start:length:recipientNumber)");
61 subparser
.addArgument("--quote-timestamp")
63 .help("Specify the timestamp of a previous message with the recipient or group to add a quote to the new message.");
64 subparser
.addArgument("--quote-author").help("Specify the number of the author of the original message.");
65 subparser
.addArgument("--quote-message").help("Specify the message of the original message.");
66 subparser
.addArgument("--quote-mention")
68 .help("Quote with mention of another group member (syntax: start:length:recipientNumber)");
72 public void handleCommand(
73 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
74 ) throws CommandException
{
75 final var isNoteToSelf
= Boolean
.TRUE
.equals(ns
.getBoolean("note-to-self"));
76 final var recipientStrings
= ns
.<String
>getList("recipient");
77 final var groupIdStrings
= ns
.<String
>getList("group-id");
79 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
84 final var isEndSession
= Boolean
.TRUE
.equals(ns
.getBoolean("end-session"));
86 final var singleRecipients
= recipientIdentifiers
.stream()
87 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
88 .map(RecipientIdentifier
.Single
.class::cast
)
89 .collect(Collectors
.toSet());
90 if (singleRecipients
.isEmpty()) {
91 throw new UserErrorException("No recipients given");
95 final var results
= m
.sendEndSessionMessage(singleRecipients
);
96 outputResult(outputWriter
, results
.timestamp());
97 ErrorUtils
.handleSendMessageResults(results
.results());
99 } catch (IOException e
) {
100 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
101 .getSimpleName() + ")", e
);
105 var messageText
= ns
.getString("message");
106 if (messageText
== null) {
108 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
109 } catch (IOException e
) {
110 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
114 List
<String
> attachments
= ns
.getList("attachment");
115 if (attachments
== null) {
116 attachments
= List
.of();
119 List
<String
> mentionStrings
= ns
.getList("mention");
120 final var mentions
= mentionStrings
== null ? List
.<Message
.Mention
>of() : parseMentions(m
, mentionStrings
);
122 final Message
.Quote quote
;
123 final var quoteTimestamp
= ns
.getLong("quote-timestamp");
124 if (quoteTimestamp
!= null) {
125 final var quoteAuthor
= ns
.getString("quote-author");
126 final var quoteMessage
= ns
.getString("quote-message");
127 List
<String
> quoteMentionStrings
= ns
.getList("quote-mention");
128 final var quoteMentions
= quoteMentionStrings
== null
129 ? List
.<Message
.Mention
>of()
130 : parseMentions(m
, quoteMentionStrings
);
131 quote
= new Message
.Quote(quoteTimestamp
,
132 CommandUtil
.getSingleRecipientIdentifier(quoteAuthor
, m
.getSelfNumber()),
140 var results
= m
.sendMessage(new Message(messageText
, attachments
, mentions
, Optional
.ofNullable(quote
)),
141 recipientIdentifiers
);
142 outputResult(outputWriter
, results
.timestamp());
143 ErrorUtils
.handleSendMessageResults(results
.results());
144 } catch (AttachmentInvalidException
| IOException e
) {
145 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
146 .getSimpleName() + ")", e
);
147 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
148 throw new UserErrorException(e
.getMessage());
152 private List
<Message
.Mention
> parseMentions(
153 final Manager m
, final List
<String
> mentionStrings
154 ) throws UserErrorException
{
155 List
<Message
.Mention
> mentions
;
156 final Pattern mentionPattern
= Pattern
.compile("([0-9]+):([0-9]+):(.+)");
157 mentions
= new ArrayList
<>();
158 for (final var mention
: mentionStrings
) {
159 final var matcher
= mentionPattern
.matcher(mention
);
160 if (!matcher
.matches()) {
161 throw new UserErrorException("Invalid mention syntax ("
163 + ") expected 'start:end:recipientNumber'");
165 mentions
.add(new Message
.Mention(CommandUtil
.getSingleRecipientIdentifier(matcher
.group(3),
166 m
.getSelfNumber()), Integer
.parseInt(matcher
.group(1)), Integer
.parseInt(matcher
.group(2))));
171 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
172 if (outputWriter
instanceof PlainTextWriter writer
) {
173 writer
.println("{}", timestamp
);
175 final var writer
= (JsonWriter
) outputWriter
;
176 writer
.write(Map
.of("timestamp", timestamp
));