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
.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 ((ns
.getList("recipient") == null || ns
.getList("recipient").size() == 0) && (
37 ns
.getBoolean("endsession") || ns
.getString("group") == null
39 System
.err
.println("No recipients given");
40 System
.err
.println("Aborting sending.");
44 if (ns
.getBoolean("endsession")) {
46 signal
.sendEndSessionMessage(ns
.getList("recipient"));
48 } catch (AssertionError e
) {
49 handleAssertionError(e
);
51 } catch (DBusExecutionException e
) {
52 System
.err
.println("Failed to send message: " + e
.getMessage());
57 String messageText
= ns
.getString("message");
58 if (messageText
== null) {
60 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
61 } catch (IOException e
) {
62 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
63 System
.err
.println("Aborting sending.");
68 List
<String
> attachments
= ns
.getList("attachment");
69 if (attachments
== null) {
70 attachments
= new ArrayList
<>();
74 if (ns
.getString("group") != null) {
77 groupId
= Util
.decodeGroupId(ns
.getString("group")).serialize();
78 } catch (GroupIdFormatException e
) {
79 handleGroupIdFormatException(e
);
83 long timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
84 System
.out
.println(timestamp
);
87 } catch (AssertionError e
) {
88 handleAssertionError(e
);
90 } catch (DBusExecutionException e
) {
91 System
.err
.println("Failed to send message: " + e
.getMessage());
96 long timestamp
= signal
.sendMessage(messageText
, attachments
, ns
.getList("recipient"));
97 System
.out
.println(timestamp
);
99 } catch (AssertionError e
) {
100 handleAssertionError(e
);
102 } catch (DBusExecutionException e
) {
103 System
.err
.println("Failed to send message: " + e
.getMessage());