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
.AttachmentInvalidException
;
9 import org
.asamk
.signal
.GroupIdFormatException
;
10 import org
.asamk
.signal
.GroupNotFoundException
;
11 import org
.asamk
.signal
.NotAGroupMemberException
;
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
;
17 import java
.io
.IOException
;
18 import java
.nio
.charset
.Charset
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
22 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
23 import static org
.asamk
.signal
.util
.ErrorUtils
.handleDBusExecutionException
;
24 import static org
.asamk
.signal
.util
.ErrorUtils
.handleEncapsulatedExceptions
;
25 import static org
.asamk
.signal
.util
.ErrorUtils
.handleGroupIdFormatException
;
26 import static org
.asamk
.signal
.util
.ErrorUtils
.handleGroupNotFoundException
;
27 import static org
.asamk
.signal
.util
.ErrorUtils
.handleIOException
;
28 import static org
.asamk
.signal
.util
.ErrorUtils
.handleNotAGroupMemberException
;
30 public class SendCommand
implements DbusCommand
{
33 public void attachToSubparser(final Subparser subparser
) {
34 subparser
.addArgument("-g", "--group")
35 .help("Specify the recipient group ID.");
36 subparser
.addArgument("recipient")
37 .help("Specify the recipients' phone number.")
39 subparser
.addArgument("-m", "--message")
40 .help("Specify the message, if missing standard input is used.");
41 subparser
.addArgument("-a", "--attachment")
43 .help("Add file as attachment");
44 subparser
.addArgument("-e", "--endsession")
45 .help("Clear session state and send end session message.")
46 .action(Arguments
.storeTrue());
50 public int handleCommand(final Namespace ns
, final Signal signal
) {
51 if (!signal
.isRegistered()) {
52 System
.err
.println("User is not registered.");
56 if ((ns
.getList("recipient") == null || ns
.getList("recipient").size() == 0) && (ns
.getBoolean("endsession") || ns
.getString("group") == null)) {
57 System
.err
.println("No recipients given");
58 System
.err
.println("Aborting sending.");
62 if (ns
.getBoolean("endsession")) {
64 signal
.sendEndSessionMessage(ns
.<String
>getList("recipient"));
66 } catch (IOException e
) {
69 } catch (EncapsulatedExceptions e
) {
70 handleEncapsulatedExceptions(e
);
72 } catch (AssertionError e
) {
73 handleAssertionError(e
);
75 } catch (DBusExecutionException e
) {
76 handleDBusExecutionException(e
);
81 String messageText
= ns
.getString("message");
82 if (messageText
== null) {
84 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
85 } catch (IOException e
) {
86 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
87 System
.err
.println("Aborting sending.");
93 List
<String
> attachments
= ns
.getList("attachment");
94 if (attachments
== null) {
95 attachments
= new ArrayList
<>();
97 if (ns
.getString("group") != null) {
98 byte[] groupId
= Util
.decodeGroupId(ns
.getString("group"));
99 signal
.sendGroupMessage(messageText
, attachments
, groupId
);
101 signal
.sendMessage(messageText
, attachments
, ns
.<String
>getList("recipient"));
104 } catch (IOException e
) {
105 handleIOException(e
);
107 } catch (EncapsulatedExceptions e
) {
108 handleEncapsulatedExceptions(e
);
110 } catch (AssertionError e
) {
111 handleAssertionError(e
);
113 } catch (GroupNotFoundException e
) {
114 handleGroupNotFoundException(e
);
116 } catch (NotAGroupMemberException e
) {
117 handleNotAGroupMemberException(e
);
119 } catch (AttachmentInvalidException e
) {
120 System
.err
.println("Failed to add attachment: " + e
.getMessage());
121 System
.err
.println("Aborting sending.");
123 } catch (DBusExecutionException e
) {
124 handleDBusExecutionException(e
);
126 } catch (GroupIdFormatException e
) {
127 handleGroupIdFormatException(e
);