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
.OutputWriter
;
18 import org
.asamk
.signal
.util
.CommandUtil
;
19 import org
.asamk
.signal
.util
.IOUtils
;
20 import org
.slf4j
.Logger
;
21 import org
.slf4j
.LoggerFactory
;
23 import java
.io
.IOException
;
24 import java
.nio
.charset
.Charset
;
25 import java
.util
.ArrayList
;
26 import java
.util
.List
;
27 import java
.util
.Optional
;
28 import java
.util
.regex
.Pattern
;
29 import java
.util
.stream
.Collectors
;
31 import static org
.asamk
.signal
.util
.SendMessageResultUtils
.outputResult
;
33 public class SendCommand
implements JsonRpcLocalCommand
{
35 private final static Logger logger
= LoggerFactory
.getLogger(SendCommand
.class);
38 public String
getName() {
43 public void attachToSubparser(final Subparser subparser
) {
44 subparser
.help("Send a message to another user or group.");
45 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
46 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
47 subparser
.addArgument("--note-to-self")
48 .help("Send the message to self without notification.")
49 .action(Arguments
.storeTrue());
51 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
52 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
53 subparser
.addArgument("-e", "--end-session", "--endsession")
54 .help("Clear session state and send end session message.")
55 .action(Arguments
.storeTrue());
56 subparser
.addArgument("--mention")
58 .help("Mention another group member (syntax: start:length:recipientNumber)");
59 subparser
.addArgument("--quote-timestamp")
61 .help("Specify the timestamp of a previous message with the recipient or group to add a quote to the new message.");
62 subparser
.addArgument("--quote-author").help("Specify the number of the author of the original message.");
63 subparser
.addArgument("--quote-message").help("Specify the message of the original message.");
64 subparser
.addArgument("--quote-mention")
66 .help("Quote with mention of another group member (syntax: start:length:recipientNumber)");
70 public void handleCommand(
71 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
72 ) throws CommandException
{
73 final var isNoteToSelf
= Boolean
.TRUE
.equals(ns
.getBoolean("note-to-self"));
74 final var recipientStrings
= ns
.<String
>getList("recipient");
75 final var groupIdStrings
= ns
.<String
>getList("group-id");
77 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
82 final var isEndSession
= Boolean
.TRUE
.equals(ns
.getBoolean("end-session"));
84 final var singleRecipients
= recipientIdentifiers
.stream()
85 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
86 .map(RecipientIdentifier
.Single
.class::cast
)
87 .collect(Collectors
.toSet());
88 if (singleRecipients
.isEmpty()) {
89 throw new UserErrorException("No recipients given");
93 final var results
= m
.sendEndSessionMessage(singleRecipients
);
94 outputResult(outputWriter
, results
);
96 } catch (IOException e
) {
97 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
98 .getSimpleName() + ")", e
);
102 var messageText
= ns
.getString("message");
103 if (messageText
== null) {
104 logger
.debug("Reading message from stdin...");
106 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
107 } catch (IOException e
) {
108 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
112 List
<String
> attachments
= ns
.getList("attachment");
113 if (attachments
== null) {
114 attachments
= List
.of();
117 List
<String
> mentionStrings
= ns
.getList("mention");
118 final var mentions
= mentionStrings
== null ? List
.<Message
.Mention
>of() : parseMentions(m
, mentionStrings
);
120 final Message
.Quote quote
;
121 final var quoteTimestamp
= ns
.getLong("quote-timestamp");
122 if (quoteTimestamp
!= null) {
123 final var quoteAuthor
= ns
.getString("quote-author");
124 final var quoteMessage
= ns
.getString("quote-message");
125 List
<String
> quoteMentionStrings
= ns
.getList("quote-mention");
126 final var quoteMentions
= quoteMentionStrings
== null
127 ? List
.<Message
.Mention
>of()
128 : parseMentions(m
, quoteMentionStrings
);
129 quote
= new Message
.Quote(quoteTimestamp
,
130 CommandUtil
.getSingleRecipientIdentifier(quoteAuthor
, m
.getSelfNumber()),
138 var results
= m
.sendMessage(new Message(messageText
, attachments
, mentions
, Optional
.ofNullable(quote
)),
139 recipientIdentifiers
);
140 outputResult(outputWriter
, results
);
141 } catch (AttachmentInvalidException
| IOException e
) {
142 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
143 .getSimpleName() + ")", e
);
144 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
145 throw new UserErrorException(e
.getMessage());
149 private List
<Message
.Mention
> parseMentions(
150 final Manager m
, final List
<String
> mentionStrings
151 ) throws UserErrorException
{
152 List
<Message
.Mention
> mentions
;
153 final Pattern mentionPattern
= Pattern
.compile("([0-9]+):([0-9]+):(.+)");
154 mentions
= new ArrayList
<>();
155 for (final var mention
: mentionStrings
) {
156 final var matcher
= mentionPattern
.matcher(mention
);
157 if (!matcher
.matches()) {
158 throw new UserErrorException("Invalid mention syntax ("
160 + ") expected 'start:end:recipientNumber'");
162 mentions
.add(new Message
.Mention(CommandUtil
.getSingleRecipientIdentifier(matcher
.group(3),
163 m
.getSelfNumber()), Integer
.parseInt(matcher
.group(1)), Integer
.parseInt(matcher
.group(2))));