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
.OutputWriter
;
9 import org
.asamk
.signal
.PlainTextWriterImpl
;
10 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
11 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
12 import org
.asamk
.signal
.commands
.exceptions
.UntrustedKeyErrorException
;
13 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
14 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
15 import org
.asamk
.signal
.util
.IOUtils
;
16 import org
.asamk
.signal
.util
.Util
;
17 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
18 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
19 import org
.slf4j
.Logger
;
20 import org
.slf4j
.LoggerFactory
;
22 import java
.io
.IOException
;
23 import java
.nio
.charset
.Charset
;
24 import java
.util
.List
;
26 public class SendCommand
implements DbusCommand
{
28 private final static Logger logger
= LoggerFactory
.getLogger(SendCommand
.class);
29 private final OutputWriter outputWriter
;
31 public SendCommand(final OutputWriter outputWriter
) {
32 this.outputWriter
= outputWriter
;
35 public static void attachToSubparser(final Subparser subparser
) {
36 subparser
.help("Send a message to another user or group.");
37 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
38 final var mutuallyExclusiveGroup
= subparser
.addMutuallyExclusiveGroup();
39 mutuallyExclusiveGroup
.addArgument("-g", "--group").help("Specify the recipient group ID.");
40 mutuallyExclusiveGroup
.addArgument("--note-to-self")
41 .help("Send the message to self without notification.")
42 .action(Arguments
.storeTrue());
44 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
45 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
46 subparser
.addArgument("-e", "--endsession")
47 .help("Clear session state and send end session message.")
48 .action(Arguments
.storeTrue());
52 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
53 final List
<String
> recipients
= ns
.getList("recipient");
54 final var isEndSession
= ns
.getBoolean("endsession");
55 final var groupIdString
= ns
.getString("group");
56 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
58 final var noRecipients
= recipients
== null || recipients
.isEmpty();
59 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null && !isNoteToSelf
)) {
60 throw new UserErrorException("No recipients given");
62 if (!noRecipients
&& groupIdString
!= null) {
63 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
65 if (!noRecipients
&& isNoteToSelf
) {
66 throw new UserErrorException(
67 "You cannot specify recipients by phone number and note to self at the same time");
72 signal
.sendEndSessionMessage(recipients
);
74 } catch (Signal
.Error
.UntrustedIdentity e
) {
75 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
76 } catch (DBusExecutionException e
) {
77 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
81 var messageText
= ns
.getString("message");
82 if (messageText
== null) {
84 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
85 } catch (IOException e
) {
86 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
90 List
<String
> attachments
= ns
.getList("attachment");
91 if (attachments
== null) {
92 attachments
= List
.of();
95 final var writer
= (PlainTextWriterImpl
) outputWriter
;
97 if (groupIdString
!= null) {
100 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
101 } catch (GroupIdFormatException e
) {
102 throw new UserErrorException("Invalid group id: " + e
.getMessage());
106 var timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
107 writer
.println("{}", timestamp
);
109 } catch (DBusExecutionException e
) {
110 throw new UnexpectedErrorException("Failed to send group message: " + e
.getMessage());
116 var timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
117 writer
.println("{}", timestamp
);
119 } catch (Signal
.Error
.UntrustedIdentity e
) {
120 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
121 } catch (DBusExecutionException e
) {
122 throw new UnexpectedErrorException("Failed to send note to self message: " + e
.getMessage());
127 var timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
128 writer
.println("{}", timestamp
);
129 } catch (UnknownObject e
) {
130 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
131 } catch (Signal
.Error
.UntrustedIdentity e
) {
132 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
133 } catch (DBusExecutionException e
) {
134 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());