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
.dbus
.DbusSignalImpl
;
15 import org
.asamk
.signal
.manager
.Manager
;
16 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
17 import org
.asamk
.signal
.util
.Util
;
18 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
19 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
21 import java
.util
.List
;
24 public class SendReactionCommand
implements DbusCommand
, JsonRpcLocalCommand
{
27 public String
getName() {
28 return "sendReaction";
32 public void attachToSubparser(final Subparser subparser
) {
33 subparser
.help("Send reaction to a previously received or sent message.");
34 subparser
.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.");
35 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
36 subparser
.addArgument("-e", "--emoji")
38 .help("Specify the emoji, should be a single unicode grapheme cluster.");
39 subparser
.addArgument("-a", "--target-author")
41 .help("Specify the number of the author of the message to which to react.");
42 subparser
.addArgument("-t", "--target-timestamp")
45 .help("Specify the timestamp of the message to which to react.");
46 subparser
.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments
.storeTrue());
50 public void handleCommand(
51 final Namespace ns
, final Signal signal
, final OutputWriter outputWriter
52 ) throws CommandException
{
53 final List
<String
> recipients
= ns
.getList("recipient");
54 final var groupIdString
= ns
.getString("group-id");
56 final var noRecipients
= recipients
== null || recipients
.isEmpty();
57 if (noRecipients
&& groupIdString
== null) {
58 throw new UserErrorException("No recipients given");
60 if (!noRecipients
&& groupIdString
!= null) {
61 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
64 final var emoji
= ns
.getString("emoji");
65 final boolean isRemove
= ns
.getBoolean("remove");
66 final var targetAuthor
= ns
.getString("target-author");
67 final long targetTimestamp
= ns
.getLong("target-timestamp");
69 byte[] groupId
= null;
70 if (groupIdString
!= null) {
72 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
73 } catch (GroupIdFormatException e
) {
74 throw new UserErrorException("Invalid group id: " + e
.getMessage());
80 if (groupId
!= null) {
81 timestamp
= signal
.sendGroupMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, groupId
);
83 timestamp
= signal
.sendMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, recipients
);
85 outputResult(outputWriter
, timestamp
);
86 } catch (UnknownObject e
) {
87 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
88 } catch (Signal
.Error
.InvalidNumber e
) {
89 throw new UserErrorException("Invalid number: " + e
.getMessage());
90 } catch (Signal
.Error
.GroupNotFound e
) {
91 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
92 } catch (DBusExecutionException e
) {
93 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());
98 public void handleCommand(
99 final Namespace ns
, final Manager m
, final OutputWriter outputWriter
100 ) throws CommandException
{
101 handleCommand(ns
, new DbusSignalImpl(m
, null), outputWriter
);
104 private void outputResult(final OutputWriter outputWriter
, final long timestamp
) {
105 if (outputWriter
instanceof PlainTextWriter
) {
106 final var writer
= (PlainTextWriter
) outputWriter
;
107 writer
.println("{}", timestamp
);
109 final var writer
= (JsonWriter
) outputWriter
;
110 writer
.write(Map
.of("timestamp", timestamp
));