]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendCommand.java
Move recipients/group argument check to send commands
[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.ArrayList;
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("-g", "--group").help("Specify the recipient group ID.");
27 subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
28 subparser.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
29 subparser.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
30 subparser.addArgument("-e", "--endsession")
31 .help("Clear session state and send end session message.")
32 .action(Arguments.storeTrue());
33 }
34
35 @Override
36 public int handleCommand(final Namespace ns, final Signal signal) {
37 final List<String> recipients = ns.getList("recipient");
38 final Boolean isEndSession = ns.getBoolean("endsession");
39 final String groupIdString = ns.getString("group");
40
41 final boolean noRecipients = recipients == null || recipients.isEmpty();
42 if ((noRecipients && isEndSession) || (noRecipients && groupIdString == null)) {
43 System.err.println("No recipients given");
44 System.err.println("Aborting sending.");
45 return 1;
46 }
47 if (!noRecipients && groupIdString != null) {
48 System.err.println("You cannot specify recipients by phone number and groups at the same time");
49 return 1;
50 }
51
52 if (isEndSession) {
53 try {
54 signal.sendEndSessionMessage(recipients);
55 return 0;
56 } catch (AssertionError e) {
57 handleAssertionError(e);
58 return 1;
59 } catch (DBusExecutionException e) {
60 System.err.println("Failed to send message: " + e.getMessage());
61 return 2;
62 }
63 }
64
65 String messageText = ns.getString("message");
66 if (messageText == null) {
67 try {
68 messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
69 } catch (IOException e) {
70 System.err.println("Failed to read message from stdin: " + e.getMessage());
71 System.err.println("Aborting sending.");
72 return 1;
73 }
74 }
75
76 List<String> attachments = ns.getList("attachment");
77 if (attachments == null) {
78 attachments = new ArrayList<>();
79 }
80
81 try {
82 if (groupIdString != null) {
83 byte[] groupId;
84 try {
85 groupId = Util.decodeGroupId(groupIdString).serialize();
86 } catch (GroupIdFormatException e) {
87 handleGroupIdFormatException(e);
88 return 1;
89 }
90
91 long timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
92 System.out.println(timestamp);
93 return 0;
94 }
95 } catch (AssertionError e) {
96 handleAssertionError(e);
97 return 1;
98 } catch (DBusExecutionException e) {
99 System.err.println("Failed to send message: " + e.getMessage());
100 return 2;
101 }
102
103 try {
104 long timestamp = signal.sendMessage(messageText, attachments, recipients);
105 System.out.println(timestamp);
106 return 0;
107 } catch (AssertionError e) {
108 handleAssertionError(e);
109 return 1;
110 } catch (UnknownObject e) {
111 System.err.println("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
112 return 1;
113 } catch (DBusExecutionException e) {
114 System.err.println("Failed to send message: " + e.getMessage());
115 return 2;
116 }
117 }
118 }