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