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
.OutputWriter
;
9 import org
.asamk
.signal
.PlainTextWriter
;
10 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
11 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
12 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
13 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
14 import org
.asamk
.signal
.util
.Util
;
15 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
16 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
18 import java
.util
.List
;
20 public class SendReactionCommand
implements DbusCommand
{
22 private final OutputWriter outputWriter
;
24 public SendReactionCommand(final OutputWriter outputWriter
) {
25 this.outputWriter
= outputWriter
;
28 public static void attachToSubparser(final Subparser subparser
) {
29 subparser
.help("Send reaction to a previously received or sent message.");
30 subparser
.addArgument("-g", "--group").help("Specify the recipient group ID.");
31 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
32 subparser
.addArgument("-e", "--emoji")
34 .help("Specify the emoji, should be a single unicode grapheme cluster.");
35 subparser
.addArgument("-a", "--target-author")
37 .help("Specify the number of the author of the message to which to react.");
38 subparser
.addArgument("-t", "--target-timestamp")
41 .help("Specify the timestamp of the message to which to react.");
42 subparser
.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments
.storeTrue());
46 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
47 final List
<String
> recipients
= ns
.getList("recipient");
48 final var groupIdString
= ns
.getString("group");
50 final var noRecipients
= recipients
== null || recipients
.isEmpty();
51 if (noRecipients
&& groupIdString
== null) {
52 throw new UserErrorException("No recipients given");
54 if (!noRecipients
&& groupIdString
!= null) {
55 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
58 final var emoji
= ns
.getString("emoji");
59 final boolean isRemove
= ns
.getBoolean("remove");
60 final var targetAuthor
= ns
.getString("target-author");
61 final long targetTimestamp
= ns
.getLong("target-timestamp");
63 final var writer
= (PlainTextWriter
) outputWriter
;
65 byte[] groupId
= null;
66 if (groupIdString
!= null) {
68 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
69 } catch (GroupIdFormatException e
) {
70 throw new UserErrorException("Invalid group id: " + e
.getMessage());
76 if (groupId
!= null) {
77 timestamp
= signal
.sendGroupMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, groupId
);
79 timestamp
= signal
.sendMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, recipients
);
81 writer
.println("{}", timestamp
);
82 } catch (UnknownObject e
) {
83 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
84 } catch (Signal
.Error
.InvalidNumber e
) {
85 throw new UserErrorException("Invalid number: " + e
.getMessage());
86 } catch (Signal
.Error
.GroupNotFound e
) {
87 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
88 } catch (DBusExecutionException e
) {
89 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());