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