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
.dbus
.DbusSignalImpl
;
16 import org
.asamk
.signal
.manager
.Manager
;
17 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
18 import org
.asamk
.signal
.util
.IOUtils
;
19 import org
.asamk
.signal
.util
.Util
;
20 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
21 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
22 import org
.slf4j
.Logger
;
23 import org
.slf4j
.LoggerFactory
;
25 import java
.io
.IOException
;
26 import java
.nio
.charset
.Charset
;
27 import java
.util
.List
;
30 public class SendCommand
implements DbusCommand
, JsonRpcLocalCommand
{
32 private final static Logger logger
= LoggerFactory
.getLogger(SendCommand
.class);
33 private final OutputWriter outputWriter
;
35 public SendCommand(final OutputWriter outputWriter
) {
36 this.outputWriter
= outputWriter
;
39 public static void attachToSubparser(final Subparser subparser
) {
40 subparser
.help("Send a message to another user or group.");
41 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
42 final var mutuallyExclusiveGroup
= subparser
.addMutuallyExclusiveGroup();
43 mutuallyExclusiveGroup
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.");
44 mutuallyExclusiveGroup
.addArgument("--note-to-self")
45 .help("Send the message to self without notification.")
46 .action(Arguments
.storeTrue());
48 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
49 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
50 subparser
.addArgument("-e", "--end-session", "--endsession")
51 .help("Clear session state and send end session message.")
52 .action(Arguments
.storeTrue());
56 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
57 final List
<String
> recipients
= ns
.getList("recipient");
58 final var isEndSession
= ns
.getBoolean("end-session");
59 final var groupIdString
= ns
.getString("group-id");
60 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
62 final var noRecipients
= recipients
== null || recipients
.isEmpty();
63 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null && !isNoteToSelf
)) {
64 throw new UserErrorException("No recipients given");
66 if (!noRecipients
&& groupIdString
!= null) {
67 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
69 if (!noRecipients
&& isNoteToSelf
) {
70 throw new UserErrorException(
71 "You cannot specify recipients by phone number and note to self at the same time");
76 signal
.sendEndSessionMessage(recipients
);
78 } catch (Signal
.Error
.UntrustedIdentity e
) {
79 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
80 } catch (DBusExecutionException e
) {
81 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
85 var messageText
= ns
.getString("message");
86 if (messageText
== null) {
88 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
89 } catch (IOException e
) {
90 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
94 List
<String
> attachments
= ns
.getList("attachment");
95 if (attachments
== null) {
96 attachments
= List
.of();
99 if (groupIdString
!= null) {
102 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
103 } catch (GroupIdFormatException e
) {
104 throw new UserErrorException("Invalid group id: " + e
.getMessage());
108 var timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
109 outputResult(timestamp
);
111 } catch (DBusExecutionException e
) {
112 throw new UnexpectedErrorException("Failed to send group message: " + e
.getMessage());
118 var timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
119 outputResult(timestamp
);
121 } catch (Signal
.Error
.UntrustedIdentity e
) {
122 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
123 } catch (DBusExecutionException e
) {
124 throw new UnexpectedErrorException("Failed to send note to self message: " + e
.getMessage());
129 var timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
130 outputResult(timestamp
);
131 } catch (UnknownObject e
) {
132 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
133 } catch (Signal
.Error
.UntrustedIdentity e
) {
134 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
135 } catch (DBusExecutionException e
) {
136 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
140 private void outputResult(final long timestamp
) {
141 if (outputWriter
instanceof PlainTextWriter
) {
142 final var writer
= (PlainTextWriter
) outputWriter
;
143 writer
.println("{}", timestamp
);
145 final var writer
= (JsonWriter
) outputWriter
;
146 writer
.write(Map
.of("timestamp", timestamp
));
151 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
152 handleCommand(ns
, new DbusSignalImpl(m
, null));