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
.JsonWriter
;
9 import org
.asamk
.signal
.OutputWriter
;
10 import org
.asamk
.signal
.PlainTextWriter
;
11 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
12 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
13 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
14 import org
.asamk
.signal
.manager
.Manager
;
15 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
16 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
17 import org
.asamk
.signal
.util
.CommandUtil
;
18 import org
.asamk
.signal
.util
.ErrorUtils
;
19 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
20 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
22 import java
.io
.IOException
;
25 public class SendReactionCommand
implements DbusCommand
, JsonRpcLocalCommand
{
28 public String
getName() {
29 return "sendReaction";
33 public void attachToSubparser(final Subparser subparser
) {
34 subparser
.help("Send reaction to a previously received or sent message.");
35 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
36 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
37 subparser
.addArgument("--note-to-self")
38 .help("Send the reaction to self without notification.")
39 .action(Arguments
.storeTrue());
40 subparser
.addArgument("-e", "--emoji")
42 .help("Specify the emoji, should be a single unicode grapheme cluster.");
43 subparser
.addArgument("-a", "--target-author")
45 .help("Specify the number of the author of the message to which to react.");
46 subparser
.addArgument("-t", "--target-timestamp")
49 .help("Specify the timestamp of the message to which to react.");
50 subparser
.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments
.storeTrue());
54 public void handleCommand(
55 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
56 ) throws CommandException
{
57 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
58 final var recipientStrings
= ns
.<String
>getList("recipient");
59 final var groupIdStrings
= ns
.<String
>getList("group-id");
61 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
66 final var emoji
= ns
.getString("emoji");
67 final var isRemove
= ns
.getBoolean("remove");
68 final var targetAuthor
= ns
.getString("target-author");
69 final var targetTimestamp
= ns
.getLong("target-timestamp");
72 final var results
= m
.sendMessageReaction(emoji
,
74 CommandUtil
.getSingleRecipientIdentifier(targetAuthor
, m
.getUsername()),
76 recipientIdentifiers
);
77 outputResult(outputWriter
, results
.getTimestamp());
78 ErrorUtils
.handleSendMessageResults(results
.getResults());
79 } catch (GroupNotFoundException
| NotAGroupMemberException e
) {
80 throw new UserErrorException(e
.getMessage());
81 } catch (IOException e
) {
82 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
87 public void handleCommand(
88 final Namespace ns
, final Signal signal
, final OutputWriter outputWriter
89 ) throws CommandException
{
90 final var recipients
= ns
.<String
>getList("recipient");
91 final var groupIdStrings
= ns
.<String
>getList("group-id");
93 final var noRecipients
= recipients
== null || recipients
.isEmpty();
94 final var noGroups
= groupIdStrings
== null || groupIdStrings
.isEmpty();
95 if (noRecipients
&& noGroups
) {
96 throw new UserErrorException("No recipients given");
98 if (!noRecipients
&& !noGroups
) {
99 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
102 final var emoji
= ns
.getString("emoji");
103 final var isRemove
= ns
.getBoolean("remove");
104 final var targetAuthor
= ns
.getString("target-author");
105 final var targetTimestamp
= ns
.getLong("target-timestamp");
110 final var groupIds
= CommandUtil
.getGroupIds(groupIdStrings
);
111 for (final var groupId
: groupIds
) {
112 timestamp
= signal
.sendGroupMessageReaction(emoji
,
116 groupId
.serialize());
119 timestamp
= signal
.sendMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, recipients
);
121 outputResult(outputWriter
, timestamp
);
122 } catch (UnknownObject e
) {
123 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
124 } catch (Signal
.Error
.InvalidNumber e
) {
125 throw new UserErrorException("Invalid number: " + e
.getMessage());
126 } catch (Signal
.Error
.GroupNotFound e
) {
127 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
128 } catch (DBusExecutionException e
) {
129 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
133 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
134 if (outputWriter
instanceof PlainTextWriter
) {
135 final var writer
= (PlainTextWriter
) outputWriter
;
136 writer
.println("{}", timestamp
);
138 final var writer
= (JsonWriter
) outputWriter
;
139 writer
.write(Map
.of("timestamp", timestamp
));