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);
35 public String
getName() {
40 public void attachToSubparser(final Subparser subparser
) {
41 subparser
.help("Send a message to another user or group.");
42 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
43 final var mutuallyExclusiveGroup
= subparser
.addMutuallyExclusiveGroup();
44 mutuallyExclusiveGroup
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.");
45 mutuallyExclusiveGroup
.addArgument("--note-to-self")
46 .help("Send the message to self without notification.")
47 .action(Arguments
.storeTrue());
49 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
50 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
51 subparser
.addArgument("-e", "--end-session", "--endsession")
52 .help("Clear session state and send end session message.")
53 .action(Arguments
.storeTrue());
57 public void handleCommand(
58 final Namespace ns
, final Signal signal
, final OutputWriter outputWriter
59 ) throws CommandException
{
60 final List
<String
> recipients
= ns
.getList("recipient");
61 final var isEndSession
= ns
.getBoolean("end-session");
62 final var groupIdString
= ns
.getString("group-id");
63 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
65 final var noRecipients
= recipients
== null || recipients
.isEmpty();
66 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null && !isNoteToSelf
)) {
67 throw new UserErrorException("No recipients given");
69 if (!noRecipients
&& groupIdString
!= null) {
70 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
72 if (!noRecipients
&& isNoteToSelf
) {
73 throw new UserErrorException(
74 "You cannot specify recipients by phone number and note to self at the same time");
79 signal
.sendEndSessionMessage(recipients
);
81 } catch (Signal
.Error
.UntrustedIdentity e
) {
82 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
83 } catch (DBusExecutionException e
) {
84 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
88 var messageText
= ns
.getString("message");
89 if (messageText
== null) {
91 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
92 } catch (IOException e
) {
93 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
97 List
<String
> attachments
= ns
.getList("attachment");
98 if (attachments
== null) {
99 attachments
= List
.of();
102 if (groupIdString
!= null) {
105 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
106 } catch (GroupIdFormatException e
) {
107 throw new UserErrorException("Invalid group id: " + e
.getMessage());
111 var timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
112 outputResult(outputWriter
, timestamp
);
114 } catch (DBusExecutionException e
) {
115 throw new UnexpectedErrorException("Failed to send group message: " + e
.getMessage());
121 var timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
122 outputResult(outputWriter
, timestamp
);
124 } catch (Signal
.Error
.UntrustedIdentity e
) {
125 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
126 } catch (DBusExecutionException e
) {
127 throw new UnexpectedErrorException("Failed to send note to self message: " + e
.getMessage());
132 var timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
133 outputResult(outputWriter
, timestamp
);
134 } catch (UnknownObject e
) {
135 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
136 } catch (Signal
.Error
.UntrustedIdentity e
) {
137 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
138 } catch (DBusExecutionException e
) {
139 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
143 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
144 if (outputWriter
instanceof PlainTextWriter
) {
145 final var writer
= (PlainTextWriter
) outputWriter
;
146 writer
.println("{}", timestamp
);
148 final var writer
= (JsonWriter
) outputWriter
;
149 writer
.write(Map
.of("timestamp", timestamp
));
154 public void handleCommand(
155 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
156 ) throws CommandException
{
157 handleCommand(ns
, new DbusSignalImpl(m
, null), outputWriter
);