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 if ((ns
.getList("recipient") == null || ns
.getList("recipient").size() == 0) && (
38 ns
.getBoolean("endsession") || ns
.getString("group") == null
40 System
.err
.println("No recipients given");
41 System
.err
.println("Aborting sending.");
45 if (ns
.getBoolean("endsession")) {
47 signal
.sendEndSessionMessage(ns
.getList("recipient"));
49 } catch (AssertionError e
) {
50 handleAssertionError(e
);
52 } catch (DBusExecutionException e
) {
53 System
.err
.println("Failed to send message: " + e
.getMessage());
58 String messageText
= ns
.getString("message");
59 if (messageText
== null) {
61 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
62 } catch (IOException e
) {
63 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
64 System
.err
.println("Aborting sending.");
69 List
<String
> attachments
= ns
.getList("attachment");
70 if (attachments
== null) {
71 attachments
= new ArrayList
<>();
75 if (ns
.getString("group") != null) {
78 groupId
= Util
.decodeGroupId(ns
.getString("group")).serialize();
79 } catch (GroupIdFormatException e
) {
80 handleGroupIdFormatException(e
);
84 long timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
85 System
.out
.println(timestamp
);
88 } catch (AssertionError e
) {
89 handleAssertionError(e
);
91 } catch (DBusExecutionException e
) {
92 System
.err
.println("Failed to send message: " + e
.getMessage());
97 long timestamp
= signal
.sendMessage(messageText
, attachments
, ns
.getList("recipient"));
98 System
.out
.println(timestamp
);
100 } catch (AssertionError e
) {
101 handleAssertionError(e
);
103 } catch (UnknownObject e
) {
104 System
.err
.println("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
106 } catch (DBusExecutionException e
) {
107 System
.err
.println("Failed to send message: " + e
.getMessage());