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
.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
;
21 import java
.io
.IOException
;
22 import java
.nio
.charset
.Charset
;
23 import java
.util
.List
;
25 public class SendCommand
implements DbusCommand
{
27 private final static Logger logger
= LoggerFactory
.getLogger(SendCommand
.class);
30 public void attachToSubparser(final Subparser subparser
) {
31 subparser
.help("Send a message to another user or group.");
32 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
33 final var mutuallyExclusiveGroup
= subparser
.addMutuallyExclusiveGroup();
34 mutuallyExclusiveGroup
.addArgument("-g", "--group").help("Specify the recipient group ID.");
35 mutuallyExclusiveGroup
.addArgument("--note-to-self")
36 .help("Send the message to self without notification.")
37 .action(Arguments
.storeTrue());
39 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
40 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
41 subparser
.addArgument("-e", "--endsession")
42 .help("Clear session state and send end session message.")
43 .action(Arguments
.storeTrue());
47 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
48 final List
<String
> recipients
= ns
.getList("recipient");
49 final var isEndSession
= ns
.getBoolean("endsession");
50 final var groupIdString
= ns
.getString("group");
51 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
53 final var noRecipients
= recipients
== null || recipients
.isEmpty();
54 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null && !isNoteToSelf
)) {
55 throw new UserErrorException("No recipients given");
57 if (!noRecipients
&& groupIdString
!= null) {
58 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
60 if (!noRecipients
&& isNoteToSelf
) {
61 throw new UserErrorException(
62 "You cannot specify recipients by phone number and note to self at the same time");
67 signal
.sendEndSessionMessage(recipients
);
69 } catch (Signal
.Error
.UntrustedIdentity e
) {
70 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
71 } catch (DBusExecutionException e
) {
72 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
76 var messageText
= ns
.getString("message");
77 if (messageText
== null) {
79 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
80 } catch (IOException e
) {
81 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
85 List
<String
> attachments
= ns
.getList("attachment");
86 if (attachments
== null) {
87 attachments
= List
.of();
90 final var writer
= new PlainTextWriterImpl(System
.out
);
92 if (groupIdString
!= null) {
95 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
96 } catch (GroupIdFormatException e
) {
97 throw new UserErrorException("Invalid group id: " + e
.getMessage());
101 var timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
102 writer
.println("{}", timestamp
);
104 } catch (DBusExecutionException e
) {
105 throw new UnexpectedErrorException("Failed to send group message: " + e
.getMessage());
111 var timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
112 writer
.println("{}", timestamp
);
114 } catch (Signal
.Error
.UntrustedIdentity e
) {
115 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
116 } catch (DBusExecutionException e
) {
117 throw new UnexpectedErrorException("Failed to send note to self message: " + e
.getMessage());
122 var timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
123 writer
.println("{}", timestamp
);
124 } catch (UnknownObject e
) {
125 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
126 } catch (Signal
.Error
.UntrustedIdentity e
) {
127 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
128 } catch (DBusExecutionException e
) {
129 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());