]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendCommand.java
90aaa09a492dd7006ea8834113f27059e368eeaf
[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;
8 import org.asamk.signal.PlainTextWriterImpl;
9 import org.asamk.signal.commands.exceptions.CommandException;
10 import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
11 import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
12 import org.asamk.signal.commands.exceptions.UserErrorException;
13 import org.asamk.signal.manager.groups.GroupIdFormatException;
14 import org.asamk.signal.util.IOUtils;
15 import org.asamk.signal.util.Util;
16 import org.freedesktop.dbus.errors.UnknownObject;
17 import org.freedesktop.dbus.exceptions.DBusExecutionException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.io.IOException;
22 import java.nio.charset.Charset;
23 import java.util.List;
24
25 public class SendCommand implements DbusCommand {
26
27 private final static Logger logger = LoggerFactory.getLogger(SendCommand.class);
28
29 @Override
30 public void attachToSubparser(final Subparser subparser) {
31 subparser.help("Send a message to another user or group.");
32 subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
33 final var mutuallyExclusiveGroup = subparser.addMutuallyExclusiveGroup();
34 mutuallyExclusiveGroup.addArgument("-g", "--group").help("Specify the recipient group ID.");
35 mutuallyExclusiveGroup.addArgument("--note-to-self")
36 .help("Send the message to self without notification.")
37 .action(Arguments.storeTrue());
38
39 subparser.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
40 subparser.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
41 subparser.addArgument("-e", "--endsession")
42 .help("Clear session state and send end session message.")
43 .action(Arguments.storeTrue());
44 }
45
46 @Override
47 public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
48 final List<String> recipients = ns.getList("recipient");
49 final var isEndSession = ns.getBoolean("endsession");
50 final var groupIdString = ns.getString("group");
51 final var isNoteToSelf = ns.getBoolean("note-to-self");
52
53 final var noRecipients = recipients == null || recipients.isEmpty();
54 if ((noRecipients && isEndSession) || (noRecipients && groupIdString == null && !isNoteToSelf)) {
55 throw new UserErrorException("No recipients given");
56 }
57 if (!noRecipients && groupIdString != null) {
58 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
59 }
60 if (!noRecipients && isNoteToSelf) {
61 throw new UserErrorException(
62 "You cannot specify recipients by phone number and note to self at the same time");
63 }
64
65 if (isEndSession) {
66 try {
67 signal.sendEndSessionMessage(recipients);
68 return;
69 } catch (Signal.Error.UntrustedIdentity e) {
70 throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
71 } catch (DBusExecutionException e) {
72 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
73 }
74 }
75
76 var messageText = ns.getString("message");
77 if (messageText == null) {
78 try {
79 messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
80 } catch (IOException e) {
81 throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
82 }
83 }
84
85 List<String> attachments = ns.getList("attachment");
86 if (attachments == null) {
87 attachments = List.of();
88 }
89
90 final var writer = new PlainTextWriterImpl(System.out);
91
92 if (groupIdString != null) {
93 byte[] groupId;
94 try {
95 groupId = Util.decodeGroupId(groupIdString).serialize();
96 } catch (GroupIdFormatException e) {
97 throw new UserErrorException("Invalid group id: " + e.getMessage());
98 }
99
100 try {
101 var timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
102 writer.println("{}", timestamp);
103 return;
104 } catch (DBusExecutionException e) {
105 throw new UnexpectedErrorException("Failed to send group message: " + e.getMessage());
106 }
107 }
108
109 if (isNoteToSelf) {
110 try {
111 var timestamp = signal.sendNoteToSelfMessage(messageText, attachments);
112 writer.println("{}", timestamp);
113 return;
114 } catch (Signal.Error.UntrustedIdentity e) {
115 throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
116 } catch (DBusExecutionException e) {
117 throw new UnexpectedErrorException("Failed to send note to self message: " + e.getMessage());
118 }
119 }
120
121 try {
122 var timestamp = signal.sendMessage(messageText, attachments, recipients);
123 writer.println("{}", timestamp);
124 } catch (UnknownObject e) {
125 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
126 } catch (Signal.Error.UntrustedIdentity e) {
127 throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
128 } catch (DBusExecutionException e) {
129 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
130 }
131 }
132 }