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