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