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