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
;
6 import org
.asamk
.Signal
;
7 import org
.asamk
.signal
.AttachmentInvalidException
;
8 import org
.asamk
.signal
.GroupIdFormatException
;
9 import org
.asamk
.signal
.GroupNotFoundException
;
10 import org
.asamk
.signal
.NotAGroupMemberException
;
11 import org
.asamk
.signal
.util
.IOUtils
;
12 import org
.asamk
.signal
.util
.Util
;
13 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
14 import org
.whispersystems
.signalservice
.api
.push
.exceptions
.EncapsulatedExceptions
;
16 import java
.io
.IOException
;
17 import java
.nio
.charset
.Charset
;
18 import java
.util
.ArrayList
;
19 import java
.util
.List
;
21 import static org
.asamk
.signal
.util
.ErrorUtils
.*;
23 public class SendCommand
implements DbusCommand
{
26 public void attachToSubparser(final Subparser subparser
) {
27 subparser
.addArgument("-g", "--group")
28 .help("Specify the recipient group ID.");
29 subparser
.addArgument("recipient")
30 .help("Specify the recipients' phone number.")
32 subparser
.addArgument("-m", "--message")
33 .help("Specify the message, if missing standard input is used.");
34 subparser
.addArgument("-a", "--attachment")
36 .help("Add file as attachment");
37 subparser
.addArgument("-e", "--endsession")
38 .help("Clear session state and send end session message.")
39 .action(Arguments
.storeTrue());
43 public int handleCommand(final Namespace ns
, final Signal signal
) {
44 if (!signal
.isRegistered()) {
45 System
.err
.println("User is not registered.");
49 if ((ns
.getList("recipient") == null || ns
.getList("recipient").size() == 0) && (ns
.getBoolean("endsession") || ns
.getString("group") == null)) {
50 System
.err
.println("No recipients given");
51 System
.err
.println("Aborting sending.");
55 if (ns
.getBoolean("endsession")) {
57 signal
.sendEndSessionMessage(ns
.<String
>getList("recipient"));
59 } catch (IOException e
) {
62 } catch (EncapsulatedExceptions e
) {
63 handleEncapsulatedExceptions(e
);
65 } catch (AssertionError e
) {
66 handleAssertionError(e
);
68 } catch (DBusExecutionException e
) {
69 handleDBusExecutionException(e
);
74 String messageText
= ns
.getString("message");
75 if (messageText
== null) {
77 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
78 } catch (IOException e
) {
79 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
80 System
.err
.println("Aborting sending.");
86 List
<String
> attachments
= ns
.getList("attachment");
87 if (attachments
== null) {
88 attachments
= new ArrayList
<>();
90 if (ns
.getString("group") != null) {
91 byte[] groupId
= Util
.decodeGroupId(ns
.getString("group"));
92 signal
.sendGroupMessage(messageText
, attachments
, groupId
);
94 signal
.sendMessage(messageText
, attachments
, ns
.<String
>getList("recipient"));
97 } catch (IOException e
) {
100 } catch (EncapsulatedExceptions e
) {
101 handleEncapsulatedExceptions(e
);
103 } catch (AssertionError e
) {
104 handleAssertionError(e
);
106 } catch (GroupNotFoundException e
) {
107 handleGroupNotFoundException(e
);
109 } catch (NotAGroupMemberException e
) {
110 handleNotAGroupMemberException(e
);
112 } catch (AttachmentInvalidException e
) {
113 System
.err
.println("Failed to add attachment: " + e
.getMessage());
114 System
.err
.println("Aborting sending.");
116 } catch (DBusExecutionException e
) {
117 handleDBusExecutionException(e
);
119 } catch (GroupIdFormatException e
) {
120 handleGroupIdFormatException(e
);