]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendCommand.java
Allow calling signal-cli without -u flag
[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 if ((ns.getList("recipient") == null || ns.getList("recipient").size() == 0) && (
38 ns.getBoolean("endsession") || ns.getString("group") == null
39 )) {
40 System.err.println("No recipients given");
41 System.err.println("Aborting sending.");
42 return 1;
43 }
44
45 if (ns.getBoolean("endsession")) {
46 try {
47 signal.sendEndSessionMessage(ns.getList("recipient"));
48 return 0;
49 } catch (AssertionError e) {
50 handleAssertionError(e);
51 return 1;
52 } catch (DBusExecutionException e) {
53 System.err.println("Failed to send message: " + e.getMessage());
54 return 2;
55 }
56 }
57
58 String messageText = ns.getString("message");
59 if (messageText == null) {
60 try {
61 messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
62 } catch (IOException e) {
63 System.err.println("Failed to read message from stdin: " + e.getMessage());
64 System.err.println("Aborting sending.");
65 return 1;
66 }
67 }
68
69 List<String> attachments = ns.getList("attachment");
70 if (attachments == null) {
71 attachments = new ArrayList<>();
72 }
73
74 try {
75 if (ns.getString("group") != null) {
76 byte[] groupId;
77 try {
78 groupId = Util.decodeGroupId(ns.getString("group")).serialize();
79 } catch (GroupIdFormatException e) {
80 handleGroupIdFormatException(e);
81 return 1;
82 }
83
84 long timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
85 System.out.println(timestamp);
86 return 0;
87 }
88 } catch (AssertionError e) {
89 handleAssertionError(e);
90 return 1;
91 } catch (DBusExecutionException e) {
92 System.err.println("Failed to send message: " + e.getMessage());
93 return 2;
94 }
95
96 try {
97 long timestamp = signal.sendMessage(messageText, attachments, ns.getList("recipient"));
98 System.out.println(timestamp);
99 return 0;
100 } catch (AssertionError e) {
101 handleAssertionError(e);
102 return 1;
103 } catch (UnknownObject e) {
104 System.err.println("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
105 return 1;
106 } catch (DBusExecutionException e) {
107 System.err.println("Failed to send message: " + e.getMessage());
108 return 2;
109 }
110 }
111 }