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
.GroupSendingNotAllowedException
;
17 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
18 import org
.asamk
.signal
.util
.CommandUtil
;
19 import org
.asamk
.signal
.util
.ErrorUtils
;
20 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
21 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
23 import java
.io
.IOException
;
26 public class SendReactionCommand
implements DbusCommand
, JsonRpcLocalCommand
{
29 public String
getName() {
30 return "sendReaction";
34 public void attachToSubparser(final Subparser subparser
) {
35 subparser
.help("Send reaction to a previously received or sent message.");
36 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.").nargs("*");
37 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
38 subparser
.addArgument("--note-to-self")
39 .help("Send the reaction to self without notification.")
40 .action(Arguments
.storeTrue());
41 subparser
.addArgument("-e", "--emoji")
43 .help("Specify the emoji, should be a single unicode grapheme cluster.");
44 subparser
.addArgument("-a", "--target-author")
46 .help("Specify the number of the author of the message to which to react.");
47 subparser
.addArgument("-t", "--target-timestamp")
50 .help("Specify the timestamp of the message to which to react.");
51 subparser
.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments
.storeTrue());
55 public void handleCommand(
56 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
57 ) throws CommandException
{
58 final var isNoteToSelf
= ns
.getBoolean("note-to-self");
59 final var recipientStrings
= ns
.<String
>getList("recipient");
60 final var groupIdStrings
= ns
.<String
>getList("group-id");
62 final var recipientIdentifiers
= CommandUtil
.getRecipientIdentifiers(m
,
67 final var emoji
= ns
.getString("emoji");
68 final var isRemove
= ns
.getBoolean("remove");
69 final var targetAuthor
= ns
.getString("target-author");
70 final var targetTimestamp
= ns
.getLong("target-timestamp");
73 final var results
= m
.sendMessageReaction(emoji
,
75 CommandUtil
.getSingleRecipientIdentifier(targetAuthor
, m
.getSelfNumber()),
77 recipientIdentifiers
);
78 outputResult(outputWriter
, results
.getTimestamp());
79 ErrorUtils
.handleSendMessageResults(results
.getResults());
80 } catch (GroupNotFoundException
| NotAGroupMemberException
| GroupSendingNotAllowedException e
) {
81 throw new UserErrorException(e
.getMessage());
82 } catch (IOException e
) {
83 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
84 .getSimpleName() + ")", e
);
89 public void handleCommand(
90 final Namespace ns
, final Signal signal
, final OutputWriter outputWriter
91 ) throws CommandException
{
92 final var recipients
= ns
.<String
>getList("recipient");
93 final var groupIdStrings
= ns
.<String
>getList("group-id");
95 final var noRecipients
= recipients
== null || recipients
.isEmpty();
96 final var noGroups
= groupIdStrings
== null || groupIdStrings
.isEmpty();
97 if (noRecipients
&& noGroups
) {
98 throw new UserErrorException("No recipients given");
100 if (!noRecipients
&& !noGroups
) {
101 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
104 final var emoji
= ns
.getString("emoji");
105 final var isRemove
= ns
.getBoolean("remove");
106 final var targetAuthor
= ns
.getString("target-author");
107 final var targetTimestamp
= ns
.getLong("target-timestamp");
112 final var groupIds
= CommandUtil
.getGroupIds(groupIdStrings
);
113 for (final var groupId
: groupIds
) {
114 timestamp
= signal
.sendGroupMessageReaction(emoji
,
118 groupId
.serialize());
121 timestamp
= signal
.sendMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, recipients
);
123 outputResult(outputWriter
, timestamp
);
124 } catch (UnknownObject e
) {
125 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
126 } catch (Signal
.Error
.InvalidNumber e
) {
127 throw new UserErrorException("Invalid number: " + e
.getMessage());
128 } catch (Signal
.Error
.GroupNotFound e
) {
129 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
130 } catch (DBusExecutionException e
) {
131 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage() + " (" + e
.getClass()
132 .getSimpleName() + ")", e
);
136 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
137 if (outputWriter
instanceof PlainTextWriter
) {
138 final var writer
= (PlainTextWriter
) outputWriter
;
139 writer
.println("{}", timestamp
);
141 final var writer
= (JsonWriter
) outputWriter
;
142 writer
.write(Map
.of("timestamp", timestamp
));