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
.PlainTextWriterImpl
;
9 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
10 import org
.asamk
.signal
.util
.IOUtils
;
11 import org
.asamk
.signal
.util
.Util
;
12 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
13 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
14 import org
.slf4j
.Logger
;
15 import org
.slf4j
.LoggerFactory
;
17 import java
.io
.IOException
;
18 import java
.nio
.charset
.Charset
;
19 import java
.util
.List
;
21 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
22 import static org
.asamk
.signal
.util
.ErrorUtils
.handleGroupIdFormatException
;
24 public class SendCommand
implements DbusCommand
{
26 private final static Logger logger
= LoggerFactory
.getLogger(SendCommand
.class);
29 public void attachToSubparser(final Subparser subparser
) {
30 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
31 final var mutuallyExclusiveGroup
= subparser
.addMutuallyExclusiveGroup();
32 mutuallyExclusiveGroup
.addArgument("-g", "--group").help("Specify the recipient group ID.");
33 mutuallyExclusiveGroup
.addArgument("--note-to-self")
34 .help("Send the message to self without notification.")
35 .action(Arguments
.storeTrue());
37 subparser
.addArgument("-m", "--message").help("Specify the message, if missing standard input is used.");
38 subparser
.addArgument("-a", "--attachment").nargs("*").help("Add file as attachment");
39 subparser
.addArgument("-e", "--endsession")
40 .help("Clear session state and send end session message.")
41 .action(Arguments
.storeTrue());
45 public int handleCommand(final Namespace ns
, final Signal signal
) {
46 final List
<String
> recipients
= ns
.getList("recipient");
47 final var isEndSession
= ns
.getBoolean("endsession");
48 final var groupIdString
= ns
.getString("group");
49 final var isNoteToSelf
= ns
.getBoolean("note_to_self");
51 final var noRecipients
= recipients
== null || recipients
.isEmpty();
52 if ((noRecipients
&& isEndSession
) || (noRecipients
&& groupIdString
== null && !isNoteToSelf
)) {
53 System
.err
.println("No recipients given");
54 System
.err
.println("Aborting sending.");
57 if (!noRecipients
&& groupIdString
!= null) {
58 System
.err
.println("You cannot specify recipients by phone number and groups at the same time");
61 if (!noRecipients
&& isNoteToSelf
) {
62 System
.err
.println("You cannot specify recipients by phone number and not to self at the same time");
68 signal
.sendEndSessionMessage(recipients
);
70 } catch (AssertionError e
) {
71 handleAssertionError(e
);
73 } catch (Signal
.Error
.UntrustedIdentity e
) {
74 System
.err
.println("Failed to send message: " + e
.getMessage());
76 } catch (DBusExecutionException e
) {
77 System
.err
.println("Failed to send message: " + e
.getMessage());
82 var messageText
= ns
.getString("message");
83 if (messageText
== null) {
85 messageText
= IOUtils
.readAll(System
.in, Charset
.defaultCharset());
86 } catch (IOException e
) {
87 System
.err
.println("Failed to read message from stdin: " + e
.getMessage());
88 System
.err
.println("Aborting sending.");
93 List
<String
> attachments
= ns
.getList("attachment");
94 if (attachments
== null) {
95 attachments
= List
.of();
98 final var writer
= new PlainTextWriterImpl(System
.out
);
100 if (groupIdString
!= null) {
103 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
104 } catch (GroupIdFormatException e
) {
105 handleGroupIdFormatException(e
);
110 var timestamp
= signal
.sendGroupMessage(messageText
, attachments
, groupId
);
111 writer
.println("{}", timestamp
);
113 } catch (AssertionError e
) {
114 handleAssertionError(e
);
116 } catch (DBusExecutionException e
) {
117 System
.err
.println("Failed to send group message: " + e
.getMessage());
119 } catch (IOException e
) {
127 var timestamp
= signal
.sendNoteToSelfMessage(messageText
, attachments
);
128 writer
.println("{}", timestamp
);
130 } catch (AssertionError e
) {
131 handleAssertionError(e
);
133 } catch (Signal
.Error
.UntrustedIdentity e
) {
134 System
.err
.println("Failed to send message: " + e
.getMessage());
136 } catch (DBusExecutionException e
) {
137 System
.err
.println("Failed to send note to self message: " + e
.getMessage());
139 } catch (IOException e
) {
146 var timestamp
= signal
.sendMessage(messageText
, attachments
, recipients
);
147 writer
.println("{}", timestamp
);
149 } catch (AssertionError e
) {
150 handleAssertionError(e
);
152 } catch (UnknownObject e
) {
153 System
.err
.println("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
155 } catch (Signal
.Error
.UntrustedIdentity e
) {
156 System
.err
.println("Failed to send message: " + e
.getMessage());
158 } catch (DBusExecutionException e
) {
159 System
.err
.println("Failed to send message: " + e
.getMessage());
161 } catch (IOException e
) {