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
.util
.GroupIdFormatException
;
9 import org
.asamk
.signal
.util
.IOUtils
;
10 import org
.asamk
.signal
.util
.Util
;
11 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
13 import java
.io
.IOException
;
14 import java
.nio
.charset
.Charset
;
15 import java
.util
.ArrayList
;
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("-g", "--group").help("Specify the recipient group ID.");
26 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
27 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
28 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
29 subparser
.addArgument("-e", "--endsession")
30 .help("Clear session state and send end session message.")
31 .action(Arguments
.storeTrue());
35 public int handleCommand(final Namespace ns
, final Signal signal
) {
36 if (!signal
.isRegistered()) {
37 System
.err
.println("User is not registered.");
41 if ((ns
.getList("recipient") == null || ns
.getList("recipient").size() == 0) && (
42 ns
.getBoolean("endsession") || ns
.getString("group") == null
44 System
.err
.println("No recipients given");
45 System
.err
.println("Aborting sending.");
49 if (ns
.getBoolean("endsession")) {
51 signal
.sendEndSessionMessage(ns
.getList("recipient"));
53 } catch (AssertionError e
) {
54 handleAssertionError(e
);
56 } catch (DBusExecutionException e
) {
57 System
.err
.println("Failed to send message: " + e
.getMessage());
62 String messageText
= ns
.getString("message");
63 if (messageText
== null) {
65 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
66 } catch (IOException e
) {
67 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
68 System
.err
.println("Aborting sending.");
73 List
<String
> attachments
= ns
.getList("attachment");
74 if (attachments
== null) {
75 attachments
= new ArrayList
<>();
79 if (ns
.getString("group") != null) {
82 groupId
= Util
.decodeGroupId(ns
.getString("group"));
83 } catch (GroupIdFormatException e
) {
84 handleGroupIdFormatException(e
);
88 long timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
89 System
.out
.println(timestamp
);
92 } catch (AssertionError e
) {
93 handleAssertionError(e
);
95 } catch (DBusExecutionException e
) {
96 System
.err
.println("Failed to send message: " + e
.getMessage());
101 long timestamp
= signal
.sendMessage(messageText
, attachments
, ns
.getList("recipient"));
102 System
.out
.println(timestamp
);
104 } catch (AssertionError e
) {
105 handleAssertionError(e
);
107 } catch (DBusExecutionException e
) {
108 System
.err
.println("Failed to send message: " + e
.getMessage());