]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendCommand.java
Separate DbusSignal implementation from Manager
[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.util.GroupIdFormatException;
9 import org.asamk.signal.util.IOUtils;
10 import org.asamk.signal.util.Util;
11 import org.freedesktop.dbus.exceptions.DBusExecutionException;
12
13 import java.io.IOException;
14 import java.nio.charset.Charset;
15 import java.util.ArrayList;
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("-g", "--group")
26 .help("Specify the recipient group ID.");
27 subparser.addArgument("recipient")
28 .help("Specify the recipients' phone number.")
29 .nargs("*");
30 subparser.addArgument("-m", "--message")
31 .help("Specify the message, if missing standard input is used.");
32 subparser.addArgument("-a", "--attachment")
33 .nargs("*")
34 .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 if (!signal.isRegistered()) {
43 System.err.println("User is not registered.");
44 return 1;
45 }
46
47 if ((ns.getList("recipient") == null || ns.getList("recipient").size() == 0) && (ns.getBoolean("endsession") || ns.getString("group") == null)) {
48 System.err.println("No recipients given");
49 System.err.println("Aborting sending.");
50 return 1;
51 }
52
53 if (ns.getBoolean("endsession")) {
54 try {
55 signal.sendEndSessionMessage(ns.getList("recipient"));
56 return 0;
57 } catch (AssertionError e) {
58 handleAssertionError(e);
59 return 1;
60 } catch (DBusExecutionException e) {
61 System.err.println("Failed to send message: " + e.getMessage());
62 return 1;
63 }
64 }
65
66 String messageText = ns.getString("message");
67 if (messageText == null) {
68 try {
69 messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
70 } catch (IOException e) {
71 System.err.println("Failed to read message from stdin: " + e.getMessage());
72 System.err.println("Aborting sending.");
73 return 1;
74 }
75 }
76
77 List<String> attachments = ns.getList("attachment");
78 if (attachments == null) {
79 attachments = new ArrayList<>();
80 }
81
82 try {
83 if (ns.getString("group") != null) {
84 byte[] groupId;
85 try {
86 groupId = Util.decodeGroupId(ns.getString("group"));
87 } catch (GroupIdFormatException e) {
88 handleGroupIdFormatException(e);
89 return 1;
90 }
91
92 long timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
93 System.out.println(timestamp);
94 return 0;
95 }
96 } catch (AssertionError e) {
97 handleAssertionError(e);
98 return 1;
99 } catch (DBusExecutionException e) {
100 System.err.println("Failed to send message: " + e.getMessage());
101 return 1;
102 }
103
104 try {
105 long timestamp = signal.sendMessage(messageText, attachments, ns.getList("recipient"));
106 System.out.println(timestamp);
107 return 0;
108 } catch (AssertionError e) {
109 handleAssertionError(e);
110 return 1;
111 } catch (DBusExecutionException e) {
112 System.err.println("Failed to send message: " + e.getMessage());
113 return 1;
114 }
115 }
116 }