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
.GroupNotFoundException
;
13 import org
.asamk
.signal
.manager
.api
.GroupSendingNotAllowedException
;
14 import org
.asamk
.signal
.manager
.api
.InvalidStickerException
;
15 import org
.asamk
.signal
.manager
.api
.Message
;
16 import org
.asamk
.signal
.manager
.api
.NotAGroupMemberException
;
17 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
18 import org
.asamk
.signal
.manager
.api
.TextStyle
;
19 import org
.asamk
.signal
.manager
.api
.UnregisteredRecipientException
;
20 import org
.asamk
.signal
.output
.OutputWriter
;
21 import org
.asamk
.signal
.util
.CommandUtil
;
22 import org
.asamk
.signal
.util
.Hex
;
23 import org
.asamk
.signal
.util
.IOUtils
;
24 import org
.slf4j
.Logger
;
25 import org
.slf4j
.LoggerFactory
;
27 import java
.io
.IOException
;
28 import java
.util
.ArrayList
;
29 import java
.util
.List
;
30 import java
.util
.Optional
;
31 import java
.util
.regex
.Pattern
;
32 import java
.util
.stream
.Collectors
;
34 import static org
.asamk
.signal
.util
.SendMessageResultUtils
.outputResult
;
36 public class SendCommand
implements JsonRpcLocalCommand
{
38 private static final Logger logger
= LoggerFactory
.getLogger(SendCommand
.class);
41 public String
getName() {
46 public void attachToSubparser(final Subparser subparser
) {
47 subparser
.help("Send a message to another user or group.");
48 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
49 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
50 subparser
.addArgument("-u", "--username").help("Specify the recipient username or username link.").nargs("*");
51 subparser
.addArgument("--note-to-self")
52 .help("Send the message to self without notification.")
53 .action(Arguments
.storeTrue());
54 subparser
.addArgument("--notify-self")
55 .help("If self is part of recipients/groups send a normal message, not a sync message.")
56 .action(Arguments
.storeTrue());
58 var mut
= subparser
.addMutuallyExclusiveGroup();
59 mut
.addArgument("-m", "--message").help("Specify the message to be sent.");
60 mut
.addArgument("--message-from-stdin")
61 .action(Arguments
.storeTrue())
62 .help("Read the message from standard input.");
64 subparser
.addArgument("-a", "--attachment")
66 .help("Add an attachment. "
67 + "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. "
68 + "data:<MIME-TYPE>;filename=<FILENAME>;base64,<BASE64 ENCODED DATA>.");
69 subparser
.addArgument("--view-once")
70 .action(Arguments
.storeTrue())
71 .help("Send the message as a view once message");
72 subparser
.addArgument("-e", "--end-session", "--endsession")
73 .help("Clear session state and send end session message.")
74 .action(Arguments
.storeTrue());
75 subparser
.addArgument("--mention")
77 .help("Mention another group member (syntax: start:length:recipientNumber). "
78 + "Unit of start and length is UTF-16 code units, NOT Unicode code points.");
79 subparser
.addArgument("--text-style")
81 .help("Style parts of the message text (syntax: start:length:STYLE). "
82 + "Unit of start and length is UTF-16 code units, NOT Unicode code points.");
83 subparser
.addArgument("--quote-timestamp")
85 .help("Specify the timestamp of a previous message with the recipient or group to add a quote to the new message.");
86 subparser
.addArgument("--quote-author").help("Specify the number of the author of the original message.");
87 subparser
.addArgument("--quote-message").help("Specify the message of the original message.");
88 subparser
.addArgument("--quote-mention")
90 .help("Quote with mention of another group member (syntax: start:length:recipientNumber)");
91 subparser
.addArgument("--quote-attachment")
93 .help("Specify the attachments of the original message (syntax: contentType[:filename[:previewFile]]), e.g. 'audio/aac' or 'image/png:test.png:/tmp/preview.jpg'.");
94 subparser
.addArgument("--quote-text-style")
96 .help("Quote with style parts of the message text (syntax: start:length:STYLE)");
97 subparser
.addArgument("--sticker").help("Send a sticker (syntax: stickerPackId:stickerId)");
98 subparser
.addArgument("--preview-url")
99 .help("Specify the url for the link preview (the same url must also appear in the message body).");
100 subparser
.addArgument("--preview-title").help("Specify the title for the link preview (mandatory).");
101 subparser
.addArgument("--preview-description").help("Specify the description for the link preview (optional).");
102 subparser
.addArgument("--preview-image").help("Specify the image file for the link preview (optional).");
103 subparser
.addArgument("--story-timestamp")
105 .help("Specify the timestamp of a story to reply to.");
106 subparser
.addArgument("--story-author").help("Specify the number of the author of the story.");
107 subparser
.addArgument("--edit-timestamp")
109 .help("Specify the timestamp of a previous message with the recipient or group to send an edited message.");
113 public void handleCommand(
116 final OutputWriter outputWriter
117 ) throws CommandException
{
118 final var notifySelf
= Boolean
.TRUE
.equals(ns
.getBoolean("notify-self"));
119 final var isNoteToSelf
= Boolean
.TRUE
.equals(ns
.getBoolean("note-to-self"));
120 final var recipientStrings
= ns
.<String
>getList("recipient");
121 final var groupIdStrings
= ns
.<String
>getList("group-id");
122 final var usernameStrings
= ns
.<String
>getList("username");
124 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
130 final var isEndSession
= Boolean
.TRUE
.equals(ns
.getBoolean("end-session"));
132 final var singleRecipients
= recipientIdentifiers
.stream()
133 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
134 .map(RecipientIdentifier
.Single
.class::cast
)
135 .collect(Collectors
.toSet());
136 if (singleRecipients
.isEmpty()) {
137 throw new UserErrorException("No recipients given");
141 final var results
= m
.sendEndSessionMessage(singleRecipients
);
142 outputResult(outputWriter
, results
);
144 } catch (IOException e
) {
145 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
146 .getSimpleName() + ")", e
);
150 final var stickerString
= ns
.getString("sticker");
151 final var sticker
= stickerString
== null ?
null : parseSticker(stickerString
);
153 var messageText
= ns
.getString("message");
154 final var readMessageFromStdin
= ns
.getBoolean("message-from-stdin") == Boolean
.TRUE
;
155 if (readMessageFromStdin
) {
156 logger
.debug("Reading message from stdin...");
158 messageText
= IOUtils
.readAll(System
.in, IOUtils
.getConsoleCharset());
159 } catch (IOException e
) {
160 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
162 } else if (messageText
== null) {
166 var attachments
= ns
.<String
>getList("attachment");
167 if (attachments
== null) {
168 attachments
= List
.of();
170 final var viewOnce
= ns
.getBoolean("view-once");
172 final var selfNumber
= m
.getSelfNumber();
174 final var mentionStrings
= ns
.<String
>getList("mention");
175 final var mentions
= mentionStrings
== null
176 ? List
.<Message
.Mention
>of()
177 : parseMentions(selfNumber
, mentionStrings
);
179 final var textStyleStrings
= ns
.<String
>getList("text-style");
180 final var textStyles
= textStyleStrings
== null ? List
.<TextStyle
>of() : parseTextStyles(textStyleStrings
);
182 final Message
.Quote quote
;
183 final var quoteTimestamp
= ns
.getLong("quote-timestamp");
184 if (quoteTimestamp
!= null) {
185 final var quoteAuthor
= ns
.getString("quote-author");
186 if (quoteAuthor
== null) {
187 throw new UserErrorException("Quote author parameter is missing");
189 final var quoteMessage
= ns
.getString("quote-message");
190 final var quoteMentionStrings
= ns
.<String
>getList("quote-mention");
191 final var quoteMentions
= quoteMentionStrings
== null
192 ? List
.<Message
.Mention
>of()
193 : parseMentions(selfNumber
, quoteMentionStrings
);
194 final var quoteTextStyleStrings
= ns
.<String
>getList("quote-text-style");
195 final var quoteAttachmentStrings
= ns
.<String
>getList("quote-attachment");
196 final var quoteTextStyles
= quoteTextStyleStrings
== null
197 ? List
.<TextStyle
>of()
198 : parseTextStyles(quoteTextStyleStrings
);
199 final var quoteAttachments
= quoteAttachmentStrings
== null
200 ? List
.<Message
.Quote
.Attachment
>of()
201 : parseQuoteAttachments(quoteAttachmentStrings
);
202 quote
= new Message
.Quote(quoteTimestamp
,
203 CommandUtil
.getSingleRecipientIdentifier(quoteAuthor
, selfNumber
),
204 quoteMessage
== null ?
"" : quoteMessage
,
212 final List
<Message
.Preview
> previews
;
213 final var previewUrl
= ns
.getString("preview-url");
214 if (previewUrl
!= null) {
215 final var previewTitle
= ns
.getString("preview-title");
216 final var previewDescription
= ns
.getString("preview-description");
217 final var previewImage
= ns
.getString("preview-image");
218 previews
= List
.of(new Message
.Preview(previewUrl
,
219 Optional
.ofNullable(previewTitle
).orElse(""),
220 Optional
.ofNullable(previewDescription
).orElse(""),
221 Optional
.ofNullable(previewImage
)));
223 previews
= List
.of();
226 final Message
.StoryReply storyReply
;
227 final var storyReplyTimestamp
= ns
.getLong("story-timestamp");
228 if (storyReplyTimestamp
!= null) {
229 final var storyAuthor
= ns
.getString("story-author");
230 storyReply
= new Message
.StoryReply(storyReplyTimestamp
,
231 CommandUtil
.getSingleRecipientIdentifier(storyAuthor
, selfNumber
));
236 if (messageText
.isEmpty() && attachments
.isEmpty() && sticker
== null && quote
== null) {
237 throw new UserErrorException(
238 "Sending empty message is not allowed, either a message, attachment or sticker must be given.");
241 final var editTimestamp
= ns
.getLong("edit-timestamp");
244 final var message
= new Message(messageText
,
248 Optional
.ofNullable(quote
),
249 Optional
.ofNullable(sticker
),
251 Optional
.ofNullable((storyReply
)),
253 var results
= editTimestamp
!= null
254 ? m
.sendEditMessage(message
, recipientIdentifiers
, editTimestamp
)
255 : m
.sendMessage(message
, recipientIdentifiers
, notifySelf
);
256 outputResult(outputWriter
, results
);
257 } catch (AttachmentInvalidException
| IOException e
) {
258 if (e
instanceof IOException io
&& io
.getMessage().contains("No prekeys available")) {
259 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
260 .getSimpleName() + "), maybe one of the devices of the recipient wasn't online for a while.",
263 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
264 .getSimpleName() + ")", e
);
266 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
267 throw new UserErrorException(e
.getMessage());
268 } catch (UnregisteredRecipientException e
) {
269 throw new UserErrorException("The user " + e
.getSender().getIdentifier() + " is not registered.");
270 } catch (InvalidStickerException e
) {
271 throw new UserErrorException("Failed to send sticker: " + e
.getMessage(), e
);
275 private List
<Message
.Mention
> parseMentions(
276 final String selfNumber
,
277 final List
<String
> mentionStrings
278 ) throws UserErrorException
{
279 final var mentionPattern
= Pattern
.compile("(\\d+):(\\d+):(.+)");
280 final var mentions
= new ArrayList
<Message
.Mention
>();
281 for (final var mention
: mentionStrings
) {
282 final var matcher
= mentionPattern
.matcher(mention
);
283 if (!matcher
.matches()) {
284 throw new UserErrorException("Invalid mention syntax ("
286 + ") expected 'start:length:recipientNumber'");
288 mentions
.add(new Message
.Mention(CommandUtil
.getSingleRecipientIdentifier(matcher
.group(3), selfNumber
),
289 Integer
.parseInt(matcher
.group(1)),
290 Integer
.parseInt(matcher
.group(2))));
295 private List
<TextStyle
> parseTextStyles(
296 final List
<String
> textStylesStrings
297 ) throws UserErrorException
{
298 final var textStylePattern
= Pattern
.compile("(\\d+):(\\d+):(.+)");
299 final var textStyles
= new ArrayList
<TextStyle
>();
300 for (final var textStyle
: textStylesStrings
) {
301 final var matcher
= textStylePattern
.matcher(textStyle
);
302 if (!matcher
.matches()) {
303 throw new UserErrorException("Invalid textStyle syntax ("
305 + ") expected 'start:length:STYLE'");
307 final var style
= TextStyle
.Style
.from(matcher
.group(3));
309 throw new UserErrorException("Invalid style: " + matcher
.group(3));
311 textStyles
.add(new TextStyle(style
,
312 Integer
.parseInt(matcher
.group(1)),
313 Integer
.parseInt(matcher
.group(2))));
318 private Message
.Sticker
parseSticker(final String stickerString
) throws UserErrorException
{
319 final var stickerPattern
= Pattern
.compile("([\\da-f]+):(\\d+)");
320 final var matcher
= stickerPattern
.matcher(stickerString
);
321 if (!matcher
.matches() || matcher
.group(1).length() % 2 != 0) {
322 throw new UserErrorException("Invalid sticker syntax ("
324 + ") expected 'stickerPackId:stickerId'");
326 return new Message
.Sticker(Hex
.toByteArray(matcher
.group(1)), Integer
.parseInt(matcher
.group(2)));
329 private List
<Message
.Quote
.Attachment
> parseQuoteAttachments(
330 final List
<String
> attachmentStrings
331 ) throws UserErrorException
{
332 final var attachmentPattern
= Pattern
.compile("([^:]+)(:([^:]+)(:(.+))?)?");
333 final var attachments
= new ArrayList
<Message
.Quote
.Attachment
>();
334 for (final var attachment
: attachmentStrings
) {
335 final var matcher
= attachmentPattern
.matcher(attachment
);
336 if (!matcher
.matches()) {
337 throw new UserErrorException("Invalid attachment syntax ("
339 + ") expected 'contentType[:filename[:previewFile]]'");
341 attachments
.add(new Message
.Quote
.Attachment(matcher
.group(1), matcher
.group(3), matcher
.group(5)));