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
.AttachmentInvalidException
;
9 import org
.asamk
.signal
.manager
.GroupNotFoundException
;
10 import org
.asamk
.signal
.manager
.NotAGroupMemberException
;
11 import org
.asamk
.signal
.util
.GroupIdFormatException
;
12 import org
.asamk
.signal
.util
.IOUtils
;
13 import org
.asamk
.signal
.util
.Util
;
14 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
15 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.EncapsulatedExceptions
;
16 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
18 import java
.io
.IOException
;
19 import java
.nio
.charset
.Charset
;
20 import java
.util
.ArrayList
;
21 import java
.util
.List
;
23 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
24 import static org
.asamk
.signal
.util
.ErrorUtils
.handleDBusExecutionException
;
25 import static org
.asamk
.signal
.util
.ErrorUtils
.handleEncapsulatedExceptions
;
26 import static org
.asamk
.signal
.util
.ErrorUtils
.handleGroupIdFormatException
;
27 import static org
.asamk
.signal
.util
.ErrorUtils
.handleGroupNotFoundException
;
28 import static org
.asamk
.signal
.util
.ErrorUtils
.handleIOException
;
29 import static org
.asamk
.signal
.util
.ErrorUtils
.handleInvalidNumberException
;
30 import static org
.asamk
.signal
.util
.ErrorUtils
.handleNotAGroupMemberException
;
32 public class SendCommand
implements DbusCommand
{
35 public void attachToSubparser(final Subparser subparser
) {
36 subparser
.addArgument("-g", "--group")
37 .help("Specify the recipient group ID.");
38 subparser
.addArgument("recipient")
39 .help("Specify the recipients' phone number.")
41 subparser
.addArgument("-m", "--message")
42 .help("Specify the message, if missing standard input is used.");
43 subparser
.addArgument("-a", "--attachment")
45 .help("Add file as attachment");
46 subparser
.addArgument("-e", "--endsession")
47 .help("Clear session state and send end session message.")
48 .action(Arguments
.storeTrue());
52 public int handleCommand(final Namespace ns
, final Signal signal
) {
53 if (!signal
.isRegistered()) {
54 System
.err
.println("User is not registered.");
58 if ((ns
.getList("recipient") == null || ns
.getList("recipient").size() == 0) && (ns
.getBoolean("endsession") || ns
.getString("group") == null)) {
59 System
.err
.println("No recipients given");
60 System
.err
.println("Aborting sending.");
64 if (ns
.getBoolean("endsession")) {
66 signal
.sendEndSessionMessage(ns
.getList("recipient"));
68 } catch (IOException e
) {
71 } catch (EncapsulatedExceptions e
) {
72 handleEncapsulatedExceptions(e
);
74 } catch (AssertionError e
) {
75 handleAssertionError(e
);
77 } catch (DBusExecutionException e
) {
78 handleDBusExecutionException(e
);
80 } catch (InvalidNumberException e
) {
81 handleInvalidNumberException(e
);
86 String messageText
= ns
.getString("message");
87 if (messageText
== null) {
89 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
90 } catch (IOException e
) {
91 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
92 System
.err
.println("Aborting sending.");
98 List
<String
> attachments
= ns
.getList("attachment");
99 if (attachments
== null) {
100 attachments
= new ArrayList
<>();
103 if (ns
.getString("group") != null) {
104 byte[] groupId
= Util
.decodeGroupId(ns
.getString("group"));
105 timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
107 timestamp
= signal
.sendMessage(messageText
, attachments
, ns
.getList("recipient"));
109 System
.out
.println(timestamp
);
111 } catch (IOException e
) {
112 handleIOException(e
);
114 } catch (EncapsulatedExceptions e
) {
115 handleEncapsulatedExceptions(e
);
117 } catch (AssertionError e
) {
118 handleAssertionError(e
);
120 } catch (GroupNotFoundException e
) {
121 handleGroupNotFoundException(e
);
123 } catch (NotAGroupMemberException e
) {
124 handleNotAGroupMemberException(e
);
126 } catch (AttachmentInvalidException e
) {
127 System
.err
.println("Failed to add attachment: " + e
.getMessage());
128 System
.err
.println("Aborting sending.");
130 } catch (DBusExecutionException e
) {
131 handleDBusExecutionException(e
);
133 } catch (GroupIdFormatException e
) {
134 handleGroupIdFormatException(e
);
136 } catch (InvalidNumberException e
) {
137 handleInvalidNumberException(e
);