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
.manager
.groups
.GroupIdFormatException
;
9 import org
.asamk
.signal
.util
.IOUtils
;
10 import org
.asamk
.signal
.util
.Util
;
11 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
12 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
14 import java
.io
.IOException
;
15 import java
.nio
.charset
.Charset
;
16 import java
.util
.List
;
18 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
19 import static org
.asamk
.signal
.util
.ErrorUtils
.handleGroupIdFormatException
;
21 public class SendCommand
implements DbusCommand
{
24 public void attachToSubparser(final Subparser subparser
) {
25 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
26 final var mutuallyExclusiveGroup
= subparser
.addMutuallyExclusiveGroup();
27 mutuallyExclusiveGroup
.addArgument("-g", "--group").help("Specify the recipient group ID.");
28 mutuallyExclusiveGroup
.addArgument("--note-to-self")
29 .help("Send the message to self without notification.")
30 .action(Arguments
.storeTrue());
32 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
33 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
34 subparser
.addArgument("-e", "--endsession")
35 .help("Clear session state and send end session message.")
36 .action(Arguments
.storeTrue());
40 public int handleCommand(final Namespace ns
, final Signal signal
) {
41 final List
<String
> recipients
= ns
.getList("recipient");
42 final var isEndSession
= ns
.getBoolean("endsession");
43 final var groupIdString
= ns
.getString("group");
44 final var isNoteToSelf
= ns
.getBoolean("note_to_self");
46 final var noRecipients
= recipients
== null || recipients
.isEmpty();
47 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null && !isNoteToSelf
)) {
48 System
.err
.println("No recipients given");
49 System
.err
.println("Aborting sending.");
52 if (!noRecipients
&& groupIdString
!= null) {
53 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");
56 if (!noRecipients
&& isNoteToSelf
) {
57 System
.err
.println("You cannot specify recipients by phone number and not to self at the same time");
63 signal
.sendEndSessionMessage(recipients
);
65 } catch (AssertionError e
) {
66 handleAssertionError(e
);
68 } catch (Signal
.Error
.UntrustedIdentity e
) {
69 System
.err
.println("Failed to send message: " + e
.getMessage());
71 } catch (DBusExecutionException e
) {
72 System
.err
.println("Failed to send message: " + e
.getMessage());
77 var messageText
= ns
.getString("message");
78 if (messageText
== null) {
80 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
81 } catch (IOException e
) {
82 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
83 System
.err
.println("Aborting sending.");
88 List
<String
> attachments
= ns
.getList("attachment");
89 if (attachments
== null) {
90 attachments
= List
.of();
93 if (groupIdString
!= null) {
97 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
98 } catch (GroupIdFormatException e
) {
99 handleGroupIdFormatException(e
);
103 var timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
104 System
.out
.println(timestamp
);
106 } catch (AssertionError e
) {
107 handleAssertionError(e
);
109 } catch (DBusExecutionException e
) {
110 System
.err
.println("Failed to send group message: " + e
.getMessage());
117 var timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
118 System
.out
.println(timestamp
);
120 } catch (AssertionError e
) {
121 handleAssertionError(e
);
123 } catch (Signal
.Error
.UntrustedIdentity e
) {
124 System
.err
.println("Failed to send message: " + e
.getMessage());
126 } catch (DBusExecutionException e
) {
127 System
.err
.println("Failed to send note to self message: " + e
.getMessage());
133 var timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
134 System
.out
.println(timestamp
);
136 } catch (AssertionError e
) {
137 handleAssertionError(e
);
139 } catch (UnknownObject e
) {
140 System
.err
.println("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
142 } catch (Signal
.Error
.UntrustedIdentity e
) {
143 System
.err
.println("Failed to send message: " + e
.getMessage());
145 } catch (DBusExecutionException e
) {
146 System
.err
.println("Failed to send message: " + e
.getMessage());