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