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
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
32 final var mutuallyExclusiveGroup
= subparser
.addMutuallyExclusiveGroup();
33 mutuallyExclusiveGroup
.addArgument("-g", "--group").help("Specify the recipient group ID.");
34 mutuallyExclusiveGroup
.addArgument("--note-to-self")
35 .help("Send the message to self without notification.")
36 .action(Arguments
.storeTrue());
38 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
39 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
40 subparser
.addArgument("-e", "--endsession")
41 .help("Clear session state and send end session message.")
42 .action(Arguments
.storeTrue());
46 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
47 final List
<String
> recipients
= ns
.getList("recipient");
48 final var isEndSession
= ns
.getBoolean("endsession");
49 final var groupIdString
= ns
.getString("group");
50 final var isNoteToSelf
= ns
.getBoolean("note_to_self");
52 final var noRecipients
= recipients
== null || recipients
.isEmpty();
53 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null && !isNoteToSelf
)) {
54 throw new UserErrorException("No recipients given");
56 if (!noRecipients
&& groupIdString
!= null) {
57 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
59 if (!noRecipients
&& isNoteToSelf
) {
60 throw new UserErrorException(
61 "You cannot specify recipients by phone number and note to self at the same time");
66 signal
.sendEndSessionMessage(recipients
);
68 } catch (Signal
.Error
.UntrustedIdentity e
) {
69 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
70 } catch (DBusExecutionException e
) {
71 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
75 var messageText
= ns
.getString("message");
76 if (messageText
== null) {
78 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
79 } catch (IOException e
) {
80 throw new UserErrorException("Failed to read message from stdin: " + e
.getMessage());
84 List
<String
> attachments
= ns
.getList("attachment");
85 if (attachments
== null) {
86 attachments
= List
.of();
89 final var writer
= new PlainTextWriterImpl(System
.out
);
91 if (groupIdString
!= null) {
94 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
95 } catch (GroupIdFormatException e
) {
96 throw new UserErrorException("Invalid group id: " + e
.getMessage());
100 var timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
101 writer
.println("{}", timestamp
);
103 } catch (DBusExecutionException e
) {
104 throw new UnexpectedErrorException("Failed to send group message: " + e
.getMessage());
110 var timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
111 writer
.println("{}", timestamp
);
113 } catch (Signal
.Error
.UntrustedIdentity e
) {
114 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
115 } catch (DBusExecutionException e
) {
116 throw new UnexpectedErrorException("Failed to send note to self message: " + e
.getMessage());
121 var timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
122 writer
.println("{}", timestamp
);
123 } catch (UnknownObject e
) {
124 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
125 } catch (Signal
.Error
.UntrustedIdentity e
) {
126 throw new UntrustedKeyErrorException("Failed to send message: " + e
.getMessage());
127 } catch (DBusExecutionException e
) {
128 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());