]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendCommand.java
Remove error handling for oracle unlimited crypto issue
[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.PlainTextWriterImpl;
9 import org.asamk.signal.commands.exceptions.CommandException;
10 import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
11 import org.asamk.signal.commands.exceptions.UntrustedKeyErrorException;
12 import org.asamk.signal.commands.exceptions.UserErrorException;
13 import org.asamk.signal.manager.groups.GroupIdFormatException;
14 import org.asamk.signal.util.IOUtils;
15 import org.asamk.signal.util.Util;
16 import org.freedesktop.dbus.errors.UnknownObject;
17 import org.freedesktop.dbus.exceptions.DBusExecutionException;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 import java.io.IOException;
22 import java.nio.charset.Charset;
23 import java.util.List;
24
25 public class SendCommand implements DbusCommand {
26
27 private final static Logger logger = LoggerFactory.getLogger(SendCommand.class);
28
29 @Override
30 public void attachToSubparser(final Subparser subparser) {
31 subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
32 final var mutuallyExclusiveGroup = subparser.addMutuallyExclusiveGroup();
33 mutuallyExclusiveGroup.addArgument("-g", "--group").help("Specify the recipient group ID.");
34 mutuallyExclusiveGroup.addArgument("--note-to-self")
35 .help("Send the message to self without notification.")
36 .action(Arguments.storeTrue());
37
38 subparser.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
39 subparser.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
40 subparser.addArgument("-e", "--endsession")
41 .help("Clear session state and send end session message.")
42 .action(Arguments.storeTrue());
43 }
44
45 @Override
46 public void handleCommand(final Namespace ns, final Signal signal) throws CommandException {
47 final List<String> recipients = ns.getList("recipient");
48 final var isEndSession = ns.getBoolean("endsession");
49 final var groupIdString = ns.getString("group");
50 final var isNoteToSelf = ns.getBoolean("note_to_self");
51
52 final var noRecipients = recipients == null || recipients.isEmpty();
53 if ((noRecipients && isEndSession) || (noRecipients && groupIdString == null && !isNoteToSelf)) {
54 throw new UserErrorException("No recipients given");
55 }
56 if (!noRecipients && groupIdString != null) {
57 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
58 }
59 if (!noRecipients && isNoteToSelf) {
60 throw new UserErrorException(
61 "You cannot specify recipients by phone number and not to self at the same time");
62 }
63
64 if (isEndSession) {
65 try {
66 signal.sendEndSessionMessage(recipients);
67 return;
68 } catch (Signal.Error.UntrustedIdentity e) {
69 throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
70 } catch (DBusExecutionException e) {
71 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
72 }
73 }
74
75 var messageText = ns.getString("message");
76 if (messageText == null) {
77 try {
78 messageText = IOUtils.readAll(System.in, Charset.defaultCharset());
79 } catch (IOException e) {
80 throw new UserErrorException("Failed to read message from stdin: " + e.getMessage());
81 }
82 }
83
84 List<String> attachments = ns.getList("attachment");
85 if (attachments == null) {
86 attachments = List.of();
87 }
88
89 final var writer = new PlainTextWriterImpl(System.out);
90
91 if (groupIdString != null) {
92 byte[] groupId;
93 try {
94 groupId = Util.decodeGroupId(groupIdString).serialize();
95 } catch (GroupIdFormatException e) {
96 throw new UserErrorException("Invalid group id: " + e.getMessage());
97 }
98
99 try {
100 var timestamp = signal.sendGroupMessage(messageText, attachments, groupId);
101 writer.println("{}", timestamp);
102 return;
103 } catch (DBusExecutionException e) {
104 throw new UnexpectedErrorException("Failed to send group message: " + e.getMessage());
105 }
106 }
107
108 if (isNoteToSelf) {
109 try {
110 var timestamp = signal.sendNoteToSelfMessage(messageText, attachments);
111 writer.println("{}", timestamp);
112 return;
113 } catch (Signal.Error.UntrustedIdentity e) {
114 throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
115 } catch (DBusExecutionException e) {
116 throw new UnexpectedErrorException("Failed to send note to self message: " + e.getMessage());
117 }
118 }
119
120 try {
121 var timestamp = signal.sendMessage(messageText, attachments, recipients);
122 writer.println("{}", timestamp);
123 } catch (UnknownObject e) {
124 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e.getMessage());
125 } catch (Signal.Error.UntrustedIdentity e) {
126 throw new UntrustedKeyErrorException("Failed to send message: " + e.getMessage());
127 } catch (DBusExecutionException e) {
128 throw new UnexpectedErrorException("Failed to send message: " + e.getMessage());
129 }
130 }
131 }