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
.ArrayList
;
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("-g", "--group").help("Specify the recipient group ID.");
27 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
28 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
29 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
30 subparser
.addArgument("-e", "--endsession")
31 .help("Clear session state and send end session message.")
32 .action(Arguments
.storeTrue());
36 public int handleCommand(final Namespace ns
, final Signal signal
) {
37 final List
<String
> recipients
= ns
.getList("recipient");
38 final Boolean isEndSession
= ns
.getBoolean("endsession");
39 final String groupIdString
= ns
.getString("group");
41 final boolean noRecipients
= recipients
== null || recipients
.isEmpty();
42 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null)) {
43 System
.err
.println("No recipients given");
44 System
.err
.println("Aborting sending.");
47 if (!noRecipients
&& groupIdString
!= null) {
48 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");
54 signal
.sendEndSessionMessage(recipients
);
56 } catch (AssertionError e
) {
57 handleAssertionError(e
);
59 } catch (DBusExecutionException e
) {
60 System
.err
.println("Failed to send message: " + e
.getMessage());
65 String messageText
= ns
.getString("message");
66 if (messageText
== null) {
68 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
69 } catch (IOException e
) {
70 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
71 System
.err
.println("Aborting sending.");
76 List
<String
> attachments
= ns
.getList("attachment");
77 if (attachments
== null) {
78 attachments
= new ArrayList
<>();
82 if (groupIdString
!= null) {
85 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
86 } catch (GroupIdFormatException e
) {
87 handleGroupIdFormatException(e
);
91 long timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
92 System
.out
.println(timestamp
);
95 } catch (AssertionError e
) {
96 handleAssertionError(e
);
98 } catch (DBusExecutionException e
) {
99 System
.err
.println("Failed to send message: " + e
.getMessage());
104 long timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
105 System
.out
.println(timestamp
);
107 } catch (AssertionError e
) {
108 handleAssertionError(e
);
110 } catch (UnknownObject e
) {
111 System
.err
.println("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
113 } catch (DBusExecutionException e
) {
114 System
.err
.println("Failed to send message: " + e
.getMessage());