1 package org
.asamk
.signal
.commands
;
3 import net
.sourceforge
.argparse4j
.impl
.Arguments
;
4 import net
.sourceforge
.argparse4j
.inf
.MutuallyExclusiveGroup
;
5 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
6 import net
.sourceforge
.argparse4j
.inf
.Subparser
;
8 import org
.asamk
.Signal
;
9 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
10 import org
.asamk
.signal
.util
.IOUtils
;
11 import org
.asamk
.signal
.util
.Util
;
12 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
13 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
15 import java
.io
.IOException
;
16 import java
.nio
.charset
.Charset
;
17 import java
.util
.List
;
19 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
20 import static org
.asamk
.signal
.util
.ErrorUtils
.handleGroupIdFormatException
;
22 public class SendCommand
implements DbusCommand
{
25 public void attachToSubparser(final Subparser subparser
) {
26 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
27 final MutuallyExclusiveGroup mutuallyExclusiveGroup
= subparser
.addMutuallyExclusiveGroup();
28 mutuallyExclusiveGroup
.addArgument("-g", "--group").help("Specify the recipient group ID.");
29 mutuallyExclusiveGroup
.addArgument("--note-to-self")
30 .help("Send the message to self without notification.")
31 .action(Arguments
.storeTrue());
33 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
34 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
35 subparser
.addArgument("-e", "--endsession")
36 .help("Clear session state and send end session message.")
37 .action(Arguments
.storeTrue());
41 public int handleCommand(final Namespace ns
, final Signal signal
) {
42 final List
<String
> recipients
= ns
.getList("recipient");
43 final Boolean isEndSession
= ns
.getBoolean("endsession");
44 final String groupIdString
= ns
.getString("group");
45 final Boolean isNoteToSelf
= ns
.getBoolean("note_to_self");
47 final boolean noRecipients
= recipients
== null || recipients
.isEmpty();
48 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null && !isNoteToSelf
)) {
49 System
.err
.println("No recipients given");
50 System
.err
.println("Aborting sending.");
53 if (!noRecipients
&& groupIdString
!= null) {
54 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");
57 if (!noRecipients
&& isNoteToSelf
) {
58 System
.err
.println("You cannot specify recipients by phone number and not to self at the same time");
64 signal
.sendEndSessionMessage(recipients
);
66 } catch (AssertionError e
) {
67 handleAssertionError(e
);
69 } catch (DBusExecutionException e
) {
70 System
.err
.println("Failed to send message: " + e
.getMessage());
75 String messageText
= ns
.getString("message");
76 if (messageText
== null) {
78 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
79 } catch (IOException e
) {
80 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
81 System
.err
.println("Aborting sending.");
86 List
<String
> attachments
= ns
.getList("attachment");
87 if (attachments
== null) {
88 attachments
= List
.of();
91 if (groupIdString
!= null) {
95 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
96 } catch (GroupIdFormatException e
) {
97 handleGroupIdFormatException(e
);
101 long timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
102 System
.out
.println(timestamp
);
104 } catch (AssertionError e
) {
105 handleAssertionError(e
);
107 } catch (DBusExecutionException e
) {
108 System
.err
.println("Failed to send group message: " + e
.getMessage());
115 long timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
116 System
.out
.println(timestamp
);
118 } catch (AssertionError e
) {
119 handleAssertionError(e
);
121 } catch (DBusExecutionException e
) {
122 System
.err
.println("Failed to send note to self message: " + e
.getMessage());
128 long timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
129 System
.out
.println(timestamp
);
131 } catch (AssertionError e
) {
132 handleAssertionError(e
);
134 } catch (UnknownObject e
) {
135 System
.err
.println("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
137 } catch (DBusExecutionException e
) {
138 System
.err
.println("Failed to send message: " + e
.getMessage());