]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendCommand.java
Implement sending link previews
[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.nio.charset.Charset;
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;
33
34 import static org.asamk.signal.util.SendMessageResultUtils.outputResult;
35
36 public class SendCommand implements JsonRpcLocalCommand {
37
38 private final static Logger logger = LoggerFactory.getLogger(SendCommand.class);
39
40 @Override
41 public String getName() {
42 return "send";
43 }
44
45 @Override
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("--note-to-self")
51 .help("Send the message to self without notification.")
52 .action(Arguments.storeTrue());
53
54 var mut = subparser.addMutuallyExclusiveGroup();
55 mut.addArgument("-m", "--message").help("Specify the message to be sent.");
56 mut.addArgument("--message-from-stdin")
57 .action(Arguments.storeTrue())
58 .help("Read the message from standard input.");
59 subparser.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
60 subparser.addArgument("-e", "--end-session", "--endsession")
61 .help("Clear session state and send end session message.")
62 .action(Arguments.storeTrue());
63 subparser.addArgument("--mention")
64 .nargs("*")
65 .help("Mention another group member (syntax: start:length:recipientNumber)");
66 subparser.addArgument("--quote-timestamp")
67 .type(long.class)
68 .help("Specify the timestamp of a previous message with the recipient or group to add a quote to the new message.");
69 subparser.addArgument("--quote-author").help("Specify the number of the author of the original message.");
70 subparser.addArgument("--quote-message").help("Specify the message of the original message.");
71 subparser.addArgument("--quote-mention")
72 .nargs("*")
73 .help("Quote with mention of another group member (syntax: start:length:recipientNumber)");
74 subparser.addArgument("--sticker").help("Send a sticker (syntax: stickerPackId:stickerId)");
75 subparser.addArgument("--preview-url")
76 .help("Specify the url for the link preview (the same url must also appear in the message body).");
77 subparser.addArgument("--preview-title").help("Specify the title for the link preview (mandatory).");
78 subparser.addArgument("--preview-description").help("Specify the description for the link preview (optional).");
79 subparser.addArgument("--preview-image").help("Specify the image file for the link preview (optional).");
80 }
81
82 @Override
83 public void handleCommand(
84 final Namespace ns, final Manager m, final OutputWriter outputWriter
85 ) throws CommandException {
86 final var isNoteToSelf = Boolean.TRUE.equals(ns.getBoolean("note-to-self"));
87 final var recipientStrings = ns.<String>getList("recipient");
88 final var groupIdStrings = ns.<String>getList("group-id");
89
90 final var recipientIdentifiers = CommandUtil.getRecipientIdentifiers(m,
91 isNoteToSelf,
92 recipientStrings,
93 groupIdStrings);
94
95 final var isEndSession = Boolean.TRUE.equals(ns.getBoolean("end-session"));
96 if (isEndSession) {
97 final var singleRecipients = recipientIdentifiers.stream()
98 .filter(r -> r instanceof RecipientIdentifier.Single)
99 .map(RecipientIdentifier.Single.class::cast)
100 .collect(Collectors.toSet());
101 if (singleRecipients.isEmpty()) {
102 throw new UserErrorException("No recipients given");
103 }
104
105 try {
106 final var results = m.sendEndSessionMessage(singleRecipients);
107 outputResult(outputWriter, results);
108 return;
109 } catch (IOException e) {
110 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
111 .getSimpleName() + ")", e);
112 }
113 }
114
115 final var stickerString = ns.getString("sticker");
116 final var sticker = stickerString == null ? null : parseSticker(stickerString);
117
118 var messageText = ns.getString("message");
119 final var readMessageFromStdin = ns.getBoolean("message-from-stdin") == Boolean.TRUE;
120 if (readMessageFromStdin || (messageText == null && sticker == null)) {
121 logger.debug("Reading message from stdin...");
122 try {
123 messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
124 } catch (IOException e) {
125 throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
126 }
127 }
128
129 List<String> attachments = ns.getList("attachment");
130 if (attachments == null) {
131 attachments = List.of();
132 }
133
134 List<String> mentionStrings = ns.getList("mention");
135 final var mentions = mentionStrings == null ? List.<Message.Mention>of() : parseMentions(m, mentionStrings);
136
137 final Message.Quote quote;
138 final var quoteTimestamp = ns.getLong("quote-timestamp");
139 if (quoteTimestamp != null) {
140 final var quoteAuthor = ns.getString("quote-author");
141 final var quoteMessage = ns.getString("quote-message");
142 List<String> quoteMentionStrings = ns.getList("quote-mention");
143 final var quoteMentions = quoteMentionStrings == null
144 ? List.<Message.Mention>of()
145 : parseMentions(m, quoteMentionStrings);
146 quote = new Message.Quote(quoteTimestamp,
147 CommandUtil.getSingleRecipientIdentifier(quoteAuthor, m.getSelfNumber()),
148 quoteMessage == null ? "" : quoteMessage,
149 quoteMentions);
150 } else {
151 quote = null;
152 }
153
154 final List<Message.Preview> previews;
155 String previewUrl = ns.getString("preview-url");
156 if (previewUrl != null) {
157 String previewTitle = ns.getString("preview-title");
158 String previewDescription = ns.getString("preview-description");
159 String previewImage = ns.getString("preview-image");
160 previews = List.of(new Message.Preview(previewUrl,
161 Optional.ofNullable(previewTitle).orElse(""),
162 Optional.ofNullable(previewDescription).orElse(""),
163 Optional.ofNullable(previewImage)));
164 } else {
165 previews = List.of();
166 }
167
168 try {
169 var results = m.sendMessage(new Message(messageText == null ? "" : messageText,
170 attachments,
171 mentions,
172 Optional.ofNullable(quote),
173 Optional.ofNullable(sticker),
174 previews), recipientIdentifiers);
175 outputResult(outputWriter, results);
176 } catch (AttachmentInvalidException | IOException e) {
177 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage() + " (" + e.getClass()
178 .getSimpleName() + ")", e);
179 } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) {
180 throw new UserErrorException(e.getMessage());
181 } catch (UnregisteredRecipientException e) {
182 throw new UserErrorException("The user " + e.getSender().getIdentifier() + " is not registered.");
183 } catch (InvalidStickerException e) {
184 throw new UserErrorException("Failed to send sticker: " + e.getMessage(), e);
185 }
186 }
187
188 private List<Message.Mention> parseMentions(
189 final Manager m, final List<String> mentionStrings
190 ) throws UserErrorException {
191 List<Message.Mention> mentions;
192 final Pattern mentionPattern = Pattern.compile("([0-9]+):([0-9]+):(.+)");
193 mentions = new ArrayList<>();
194 for (final var mention : mentionStrings) {
195 final var matcher = mentionPattern.matcher(mention);
196 if (!matcher.matches()) {
197 throw new UserErrorException("Invalid mention syntax ("
198 + mention
199 + ") expected 'start:end:recipientNumber'");
200 }
201 mentions.add(new Message.Mention(CommandUtil.getSingleRecipientIdentifier(matcher.group(3),
202 m.getSelfNumber()), Integer.parseInt(matcher.group(1)), Integer.parseInt(matcher.group(2))));
203 }
204 return mentions;
205 }
206
207 private Message.Sticker parseSticker(final String stickerString) throws UserErrorException {
208 final Pattern stickerPattern = Pattern.compile("([0-9a-f]+):([0-9]+)");
209 final var matcher = stickerPattern.matcher(stickerString);
210 if (!matcher.matches() || matcher.group(1).length() % 2 != 0) {
211 throw new UserErrorException("Invalid sticker syntax ("
212 + stickerString
213 + ") expected 'stickerPackId:stickerId'");
214 }
215 return new Message.Sticker(Hex.toByteArray(matcher.group(1)), Integer.parseInt(matcher.group(2)));
216 }
217 }