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