1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
5 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
7 import org
.asamk
.Signal
;
8 import org
.asamk
.signal
.JsonWriter
;
9 import org
.asamk
.signal
.OutputWriter
;
10 import org
.asamk
.signal
.PlainTextWriter
;
11 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
12 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
13 import org
.asamk
.signal
.commands
.exceptions
.UntrustedKeyErrorException
;
14 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
15 import org
.asamk
.signal
.manager
.AttachmentInvalidException
;
16 import org
.asamk
.signal
.manager
.Manager
;
17 import org
.asamk
.signal
.manager
.api
.Message
;
18 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
19 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
20 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
21 import org
.asamk
.signal
.util
.CommandUtil
;
22 import org
.asamk
.signal
.util
.ErrorUtils
;
23 import org
.asamk
.signal
.util
.IOUtils
;
24 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
25 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
26 import org
.slf4j
.Logger
;
27 import org
.slf4j
.LoggerFactory
;
29 import java
.io
.IOException
;
30 import java
.nio
.charset
.Charset
;
31 import java
.util
.List
;
33 import java
.util
.stream
.Collectors
;
35 public class SendCommand
implements DbusCommand
, JsonRpcLocalCommand
{
37 private final static Logger logger
= LoggerFactory
.getLogger(SendCommand
.class);
40 public String
getName() {
45 public void attachToSubparser(final Subparser subparser
) {
46 subparser
.help("Send a message to another user or group.");
47 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
48 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
49 subparser
.addArgument("--note-to-self")
50 .help("Send the message to self without notification.")
51 .action(Arguments
.storeTrue());
53 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
54 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
55 subparser
.addArgument("-e", "--end-session", "--endsession")
56 .help("Clear session state and send end session message.")
57 .action(Arguments
.storeTrue());
61 public void handleCommand(
62 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
63 ) throws CommandException
{
64 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
65 final var recipientStrings
= ns
.<String
>getList("recipient");
66 final var groupIdStrings
= ns
.<String
>getList("group-id");
68 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
73 final var isEndSession
= ns
.getBoolean("end-session");
75 final var singleRecipients
= recipientIdentifiers
.stream()
76 .filter(r
-> r
instanceof RecipientIdentifier
.Single
)
77 .map(RecipientIdentifier
.Single
.class::cast
)
78 .collect(Collectors
.toSet());
79 if (singleRecipients
.isEmpty()) {
80 throw new UserErrorException("No recipients given");
84 m
.sendEndSessionMessage(singleRecipients
);
86 } catch (IOException e
) {
87 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
91 var messageText
= ns
.getString("message");
92 if (messageText
== null) {
94 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
95 } catch (IOException e
) {
96 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
100 List
<String
> attachments
= ns
.getList("attachment");
101 if (attachments
== null) {
102 attachments
= List
.of();
106 var results
= m
.sendMessage(new Message(messageText
, attachments
), recipientIdentifiers
);
107 outputResult(outputWriter
, results
.getTimestamp());
108 ErrorUtils
.handleSendMessageResults(results
.getResults());
109 } catch (AttachmentInvalidException
| IOException e
) {
110 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
111 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
112 throw new UserErrorException(e
.getMessage());
117 public void handleCommand(
118 final Namespace ns
, final Signal signal
, final OutputWriter outputWriter
119 ) throws CommandException
{
120 final var recipients
= ns
.<String
>getList("recipient");
121 final var isEndSession
= ns
.getBoolean("end-session");
122 final var groupIdStrings
= ns
.<String
>getList("group-id");
123 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
125 final var noRecipients
= recipients
== null || recipients
.isEmpty();
126 final var noGroups
= groupIdStrings
== null || groupIdStrings
.isEmpty();
127 if ((noRecipients
&& isEndSession
) || (noRecipients
&& noGroups
&& !isNoteToSelf
)) {
128 throw new UserErrorException("No recipients given");
130 if (!noRecipients
&& !noGroups
) {
131 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
133 if (!noRecipients
&& isNoteToSelf
) {
134 throw new UserErrorException(
135 "You cannot specify recipients by phone number and note to self at the same time");
140 signal
.sendEndSessionMessage(recipients
);
142 } catch (Signal
.Error
.UntrustedIdentity e
) {
143 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
144 } catch (DBusExecutionException e
) {
145 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
149 var messageText
= ns
.getString("message");
150 if (messageText
== null) {
152 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
153 } catch (IOException e
) {
154 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
158 List
<String
> attachments
= ns
.getList("attachment");
159 if (attachments
== null) {
160 attachments
= List
.of();
164 final var groupIds
= CommandUtil
.getGroupIds(groupIdStrings
);
168 for (final var groupId
: groupIds
) {
169 timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
.serialize());
171 outputResult(outputWriter
, timestamp
);
173 } catch (DBusExecutionException e
) {
174 throw new UnexpectedErrorException("Failed to send group message: " + e
.getMessage());
180 var timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
181 outputResult(outputWriter
, timestamp
);
183 } catch (Signal
.Error
.UntrustedIdentity e
) {
184 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
185 } catch (DBusExecutionException e
) {
186 throw new UnexpectedErrorException("Failed to send note to self message: " + e
.getMessage());
191 var timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
192 outputResult(outputWriter
, timestamp
);
193 } catch (UnknownObject e
) {
194 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
195 } catch (Signal
.Error
.UntrustedIdentity e
) {
196 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
197 } catch (DBusExecutionException e
) {
198 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
202 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
203 if (outputWriter
instanceof PlainTextWriter
) {
204 final var writer
= (PlainTextWriter
) outputWriter
;
205 writer
.println("{}", timestamp
);
207 final var writer
= (JsonWriter
) outputWriter
;
208 writer
.write(Map
.of("timestamp", timestamp
));