]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendCommand.java
23a7fb37667eb42b760a08eebc9f2b265f9330d3
[signal-cli] / src / main / java / org / asamk / signal / commands / SendCommand.java
1 package org.asamk.signal.commands;
2
3 import net.sourceforge.argparse4j.impl.Arguments;
4 import net.sourceforge.argparse4j.inf.Namespace;
5 import net.sourceforge.argparse4j.inf.Subparser;
6
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;
25
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;
32
33 import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
34
35 public class SendCommand implements JsonRpcLocalCommand {
36
37 private final static Logger logger = LoggerFactory.getLogger(SendCommand.class);
38
39 @Override
40 public String getName() {
41 return "send";
42 }
43
44 @Override
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());
52
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")
59 .nargs("*")
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")
67 .nargs("*")
68 .help("Mention another group member (syntax: start:length:recipientNumber)");
69 subparser.addArgument("--quote-timestamp")
70 .type(long.class)
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")
75 .nargs("*")
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).");
83 }
84
85 @Override
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");
92
93 final var recipientIdentifiers = CommandUtil.getRecipientIdentifiers(m,
94 isNoteToSelf,
95 recipientStrings,
96 groupIdStrings);
97
98 final var isEndSession = Boolean.TRUE.equals(ns.getBoolean("end-session"));
99 if (isEndSession) {
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");
106 }
107
108 try {
109 final var results = m.sendEndSessionMessage(singleRecipients);
110 outputResult(outputWriter, results);
111 return;
112 } catch (IOException e) {
113 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
114 .getSimpleName() + ")", e);
115 }
116 }
117
118 final var stickerString = ns.getString("sticker");
119 final var sticker = stickerString == null ? null : parseSticker(stickerString);
120
121 var messageText = ns.getString("message");
122 final var readMessageFromStdin = ns.getBoolean("message-from-stdin") == Boolean.TRUE;
123 if (readMessageFromStdin || (messageText == null && sticker == null)) {
124 logger.debug("Reading message from stdin...");
125 try {
126 messageText = IOUtils.readAll(System.in, IOUtils.getConsoleCharset());
127 } catch (IOException e) {
128 throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
129 }
130 }
131
132 List<String> attachments = ns.getList("attachment");
133 if (attachments == null) {
134 attachments = List.of();
135 }
136
137 List<String> mentionStrings = ns.getList("mention");
138 final var mentions = mentionStrings == null ? List.<Message.Mention>of() : parseMentions(m, mentionStrings);
139
140 final Message.Quote quote;
141 final var quoteTimestamp = ns.getLong("quote-timestamp");
142 if (quoteTimestamp != null) {
143 final var quoteAuthor = ns.getString("quote-author");
144 final var quoteMessage = ns.getString("quote-message");
145 List<String> quoteMentionStrings = ns.getList("quote-mention");
146 final var quoteMentions = quoteMentionStrings == null
147 ? List.<Message.Mention>of()
148 : parseMentions(m, quoteMentionStrings);
149 quote = new Message.Quote(quoteTimestamp,
150 CommandUtil.getSingleRecipientIdentifier(quoteAuthor, m.getSelfNumber()),
151 quoteMessage == null ? "" : quoteMessage,
152 quoteMentions);
153 } else {
154 quote = null;
155 }
156
157 final List<Message.Preview> previews;
158 String previewUrl = ns.getString("preview-url");
159 if (previewUrl != null) {
160 String previewTitle = ns.getString("preview-title");
161 String previewDescription = ns.getString("preview-description");
162 String previewImage = ns.getString("preview-image");
163 previews = List.of(new Message.Preview(previewUrl,
164 Optional.ofNullable(previewTitle).orElse(""),
165 Optional.ofNullable(previewDescription).orElse(""),
166 Optional.ofNullable(previewImage)));
167 } else {
168 previews = List.of();
169 }
170
171 try {
172 var results = m.sendMessage(new Message(messageText == null ? "" : messageText,
173 attachments,
174 mentions,
175 Optional.ofNullable(quote),
176 Optional.ofNullable(sticker),
177 previews), recipientIdentifiers);
178 outputResult(outputWriter, results);
179 } catch (AttachmentInvalidException | IOException e) {
180 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
181 .getSimpleName() + ")", e);
182 } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
183 throw new UserErrorException(e.getMessage());
184 } catch (UnregisteredRecipientException e) {
185 throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
186 } catch (InvalidStickerException e) {
187 throw new UserErrorException("Failed to send sticker: " + e.getMessage(), e);
188 }
189 }
190
191 private List<Message.Mention> parseMentions(
192 final Manager m, final List<String> mentionStrings
193 ) throws UserErrorException {
194 List<Message.Mention> mentions;
195 final Pattern mentionPattern = Pattern.compile("(\\d+):(\\d+):(.+)");
196 mentions = new ArrayList<>();
197 for (final var mention : mentionStrings) {
198 final var matcher = mentionPattern.matcher(mention);
199 if (!matcher.matches()) {
200 throw new UserErrorException("Invalid mention syntax ("
201 + mention
202 + ") expected 'start:end:recipientNumber'");
203 }
204 mentions.add(new Message.Mention(CommandUtil.getSingleRecipientIdentifier(matcher.group(3),
205 m.getSelfNumber()), Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2))));
206 }
207 return mentions;
208 }
209
210 private Message.Sticker parseSticker(final String stickerString) throws UserErrorException {
211 final Pattern stickerPattern = Pattern.compile("([\\da-f]+):(\\d+)");
212 final var matcher = stickerPattern.matcher(stickerString);
213 if (!matcher.matches() || matcher.group(1).length() % 2 != 0) {
214 throw new UserErrorException("Invalid sticker syntax ("
215 + stickerString
216 + ") expected 'stickerPackId:stickerId'");
217 }
218 return new Message.Sticker(Hex.toByteArray(matcher.group(1)), Integer.parseInt(matcher.group(2)));
219 }
220 }