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
.Manager
;
11 import org
.asamk
.signal
.manager
.api
.AttachmentInvalidException
;
12 import org
.asamk
.signal
.manager
.api
.InvalidStickerException
;
13 import org
.asamk
.signal
.manager
.api
.Message
;
14 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
15 import org
.asamk
.signal
.manager
.api
.UnregisteredRecipientException
;
16 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
17 import org
.asamk
.signal
.manager
.groups
.GroupSendingNotAllowedException
;
18 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
19 import org
.asamk
.signal
.output
.OutputWriter
;
20 import org
.asamk
.signal
.util
.CommandUtil
;
21 import org
.asamk
.signal
.util
.Hex
;
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
.util
.ArrayList
;
28 import java
.util
.List
;
29 import java
.util
.Optional
;
30 import java
.util
.regex
.Pattern
;
31 import java
.util
.stream
.Collectors
;
33 import static org
.asamk
.signal
.util
.SendMessageResultUtils
.outputResult
;
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 var mut
= subparser
.addMutuallyExclusiveGroup();
54 mut
.addArgument("-m", "--message").help("Specify the message to be sent.");
55 mut
.addArgument("--message-from-stdin")
56 .action(Arguments
.storeTrue())
57 .help("Read the message from standard input.");
58 subparser
.addArgument("-a", "--attachment")
60 .help("Add an attachment. "
61 + "Can be either a file path or a data URI. Data URI encoded attachments must follow the RFC 2397. Additionally a file name can be added, e.g. "
62 + "data:<MIME-TYPE>;filename=<FILENAME>;base64,<BASE64 ENCODED DATA>.");
63 subparser
.addArgument("-e", "--end-session", "--endsession")
64 .help("Clear session state and send end session message.")
65 .action(Arguments
.storeTrue());
66 subparser
.addArgument("--mention")
68 .help("Mention another group member (syntax: start:length:recipientNumber)");
69 subparser
.addArgument("--quote-timestamp")
71 .help("Specify the timestamp of a previous message with the recipient or group to add a quote to the new message.");
72 subparser
.addArgument("--quote-author").help("Specify the number of the author of the original message.");
73 subparser
.addArgument("--quote-message").help("Specify the message of the original message.");
74 subparser
.addArgument("--quote-mention")
76 .help("Quote with mention of another group member (syntax: start:length:recipientNumber)");
77 subparser
.addArgument("--sticker").help("Send a sticker (syntax: stickerPackId:stickerId)");
78 subparser
.addArgument("--preview-url")
79 .help("Specify the url for the link preview (the same url must also appear in the message body).");
80 subparser
.addArgument("--preview-title").help("Specify the title for the link preview (mandatory).");
81 subparser
.addArgument("--preview-description").help("Specify the description for the link preview (optional).");
82 subparser
.addArgument("--preview-image").help("Specify the image file for the link preview (optional).");
86 public void handleCommand(
87 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
88 ) throws CommandException
{
89 final var isNoteToSelf
= Boolean
.TRUE
.equals(ns
.getBoolean("note-to-self"));
90 final var recipientStrings
= ns
.<String
>getList("recipient");
91 final var groupIdStrings
= ns
.<String
>getList("group-id");
93 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
98 final var isEndSession
= Boolean
.TRUE
.equals(ns
.getBoolean("end-session"));
100 final var singleRecipients
= recipientIdentifiers
.stream()
101 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
102 .map(RecipientIdentifier
.Single
.class::cast
)
103 .collect(Collectors
.toSet());
104 if (singleRecipients
.isEmpty()) {
105 throw new UserErrorException("No recipients given");
109 final var results
= m
.sendEndSessionMessage(singleRecipients
);
110 outputResult(outputWriter
, results
);
112 } catch (IOException e
) {
113 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
114 .getSimpleName() + ")", e
);
118 final var stickerString
= ns
.getString("sticker");
119 final var sticker
= stickerString
== null ?
null : parseSticker(stickerString
);
121 var messageText
= ns
.getString("message");
122 final var readMessageFromStdin
= ns
.getBoolean("message-from-stdin") == Boolean
.TRUE
;
123 if (readMessageFromStdin
) {
124 logger
.debug("Reading message from stdin...");
126 messageText
= IOUtils
.readAll(System
.in, IOUtils
.getConsoleCharset());
127 } catch (IOException e
) {
128 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
130 } else if (messageText
== null) {
134 List
<String
> attachments
= ns
.getList("attachment");
135 if (attachments
== null) {
136 attachments
= List
.of();
139 List
<String
> mentionStrings
= ns
.getList("mention");
140 final var mentions
= mentionStrings
== null ? List
.<Message
.Mention
>of() : parseMentions(m
, mentionStrings
);
142 final Message
.Quote quote
;
143 final var quoteTimestamp
= ns
.getLong("quote-timestamp");
144 if (quoteTimestamp
!= null) {
145 final var quoteAuthor
= ns
.getString("quote-author");
146 final var quoteMessage
= ns
.getString("quote-message");
147 List
<String
> quoteMentionStrings
= ns
.getList("quote-mention");
148 final var quoteMentions
= quoteMentionStrings
== null
149 ? List
.<Message
.Mention
>of()
150 : parseMentions(m
, quoteMentionStrings
);
151 quote
= new Message
.Quote(quoteTimestamp
,
152 CommandUtil
.getSingleRecipientIdentifier(quoteAuthor
, m
.getSelfNumber()),
153 quoteMessage
== null ?
"" : quoteMessage
,
159 final List
<Message
.Preview
> previews
;
160 String previewUrl
= ns
.getString("preview-url");
161 if (previewUrl
!= null) {
162 String previewTitle
= ns
.getString("preview-title");
163 String previewDescription
= ns
.getString("preview-description");
164 String previewImage
= ns
.getString("preview-image");
165 previews
= List
.of(new Message
.Preview(previewUrl
,
166 Optional
.ofNullable(previewTitle
).orElse(""),
167 Optional
.ofNullable(previewDescription
).orElse(""),
168 Optional
.ofNullable(previewImage
)));
170 previews
= List
.of();
173 if (messageText
.isEmpty() && attachments
.isEmpty() && sticker
== null && quote
== null) {
174 throw new UserErrorException(
175 "Sending empty message is not allowed, either a message, attachment or sticker must be given.");
179 var results
= m
.sendMessage(new Message(messageText
,
182 Optional
.ofNullable(quote
),
183 Optional
.ofNullable(sticker
),
184 previews
), recipientIdentifiers
);
185 outputResult(outputWriter
, results
);
186 } catch (AttachmentInvalidException
| IOException e
) {
187 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
188 .getSimpleName() + ")", e
);
189 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
190 throw new UserErrorException(e
.getMessage());
191 } catch (UnregisteredRecipientException e
) {
192 throw new UserErrorException("The user " + e
.getSender().getIdentifier() + " is not registered.");
193 } catch (InvalidStickerException e
) {
194 throw new UserErrorException("Failed to send sticker: " + e
.getMessage(), e
);
198 private List
<Message
.Mention
> parseMentions(
199 final Manager m
, final List
<String
> mentionStrings
200 ) throws UserErrorException
{
201 List
<Message
.Mention
> mentions
;
202 final Pattern mentionPattern
= Pattern
.compile("(\\d+):(\\d+):(.+)");
203 mentions
= new ArrayList
<>();
204 for (final var mention
: mentionStrings
) {
205 final var matcher
= mentionPattern
.matcher(mention
);
206 if (!matcher
.matches()) {
207 throw new UserErrorException("Invalid mention syntax ("
209 + ") expected 'start:end:recipientNumber'");
211 mentions
.add(new Message
.Mention(CommandUtil
.getSingleRecipientIdentifier(matcher
.group(3),
212 m
.getSelfNumber()), Integer
.parseInt(matcher
.group(1)), Integer
.parseInt(matcher
.group(2))));
217 private Message
.Sticker
parseSticker(final String stickerString
) throws UserErrorException
{
218 final Pattern stickerPattern
= Pattern
.compile("([\\da-f]+):(\\d+)");
219 final var matcher
= stickerPattern
.matcher(stickerString
);
220 if (!matcher
.matches() || matcher
.group(1).length() % 2 != 0) {
221 throw new UserErrorException("Invalid sticker syntax ("
223 + ") expected 'stickerPackId:stickerId'");
225 return new Message
.Sticker(Hex
.toByteArray(matcher
.group(1)), Integer
.parseInt(matcher
.group(2)));