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