]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendCommand.java
Send self messages as normal messages, new flag --note-to-self for sync message
[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.MutuallyExclusiveGroup;
5 import net.sourceforge.argparse4j.inf.Namespace;
6 import net.sourceforge.argparse4j.inf.Subparser;
7
8 import org.asamk.Signal;
9 import org.asamk.signal.manager.groups.GroupIdFormatException;
10 import org.asamk.signal.util.IOUtils;
11 import org.asamk.signal.util.Util;
12 import org.freedesktop.dbus.errors.UnknownObject;
13 import org.freedesktop.dbus.exceptions.DBusExecutionException;
14
15 import java.io.IOException;
16 import java.nio.charset.Charset;
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("recipient").help("Specify the recipients' phone number.").nargs("*");
27 final MutuallyExclusiveGroup mutuallyExclusiveGroup = subparser.addMutuallyExclusiveGroup();
28 mutuallyExclusiveGroup.addArgument("-g", "--group").help("Specify the recipient group ID.");
29 mutuallyExclusiveGroup.addArgument("--note-to-self")
30 .help("Send the message to self without notification.")
31 .action(Arguments.storeTrue());
32
33 subparser.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
34 subparser.addArgument("-a", "--attachment").nargs("*").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 final List<String> recipients = ns.getList("recipient");
43 final Boolean isEndSession = ns.getBoolean("endsession");
44 final String groupIdString = ns.getString("group");
45 final Boolean isNoteToSelf = ns.getBoolean("note_to_self");
46
47 final boolean noRecipients = recipients == null || recipients.isEmpty();
48 if ((noRecipients && isEndSession) || (noRecipients && groupIdString == null && !isNoteToSelf)) {
49 System.err.println("No recipients given");
50 System.err.println("Aborting sending.");
51 return 1;
52 }
53 if (!noRecipients && groupIdString != null) {
54 System.err.println("You cannot specify recipients by phone number and groups at the same time");
55 return 1;
56 }
57 if (!noRecipients && isNoteToSelf) {
58 System.err.println("You cannot specify recipients by phone number and not to self at the same time");
59 return 1;
60 }
61
62 if (isEndSession) {
63 try {
64 signal.sendEndSessionMessage(recipients);
65 return 0;
66 } catch (AssertionError e) {
67 handleAssertionError(e);
68 return 1;
69 } catch (DBusExecutionException e) {
70 System.err.println("Failed to send message: " + e.getMessage());
71 return 2;
72 }
73 }
74
75 String messageText = ns.getString("message");
76 if (messageText == null) {
77 try {
78 messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
79 } catch (IOException e) {
80 System.err.println("Failed to read message from stdin: " + e.getMessage());
81 System.err.println("Aborting sending.");
82 return 1;
83 }
84 }
85
86 List<String> attachments = ns.getList("attachment");
87 if (attachments == null) {
88 attachments = List.of();
89 }
90
91 if (groupIdString != null) {
92 try {
93 byte[] groupId;
94 try {
95 groupId = Util.decodeGroupId(groupIdString).serialize();
96 } catch (GroupIdFormatException e) {
97 handleGroupIdFormatException(e);
98 return 1;
99 }
100
101 long timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
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 group message: " + e.getMessage());
109 return 2;
110 }
111 }
112
113 if (isNoteToSelf) {
114 try {
115 long timestamp = signal.sendNoteToSelfMessage(messageText, attachments);
116 System.out.println(timestamp);
117 return 0;
118 } catch (AssertionError e) {
119 handleAssertionError(e);
120 return 1;
121 } catch (DBusExecutionException e) {
122 System.err.println("Failed to send note to self message: " + e.getMessage());
123 return 2;
124 }
125 }
126
127 try {
128 long timestamp = signal.sendMessage(messageText, attachments, recipients);
129 System.out.println(timestamp);
130 return 0;
131 } catch (AssertionError e) {
132 handleAssertionError(e);
133 return 1;
134 } catch (UnknownObject e) {
135 System.err.println("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
136 return 1;
137 } catch (DBusExecutionException e) {
138 System.err.println("Failed to send message: " + e.getMessage());
139 return 2;
140 }
141 }
142 }