]>
nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendReactionCommand.java
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
.commands
.exceptions
.CommandException
;
10 import org
.asamk
.signal
.commands
.exceptions
.UnexpectedErrorException
;
11 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
12 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
13 import org
.asamk
.signal
.util
.Util
;
14 import org
.freedesktop
.dbus
.errors
.UnknownObject
;
15 import org
.freedesktop
.dbus
.exceptions
.DBusExecutionException
;
17 import java
.util
.List
;
19 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
21 public class SendReactionCommand
implements DbusCommand
{
24 public void attachToSubparser(final Subparser subparser
) {
25 subparser
.help("Send reaction to a previously received or sent message.");
26 subparser
.addArgument("-g", "--group").help("Specify the recipient group ID.");
27 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
28 subparser
.addArgument("-e", "--emoji")
30 .help("Specify the emoji, should be a single unicode grapheme cluster.");
31 subparser
.addArgument("-a", "--target-author")
33 .help("Specify the number of the author of the message to which to react.");
34 subparser
.addArgument("-t", "--target-timestamp")
37 .help("Specify the timestamp of the message to which to react.");
38 subparser
.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments
.storeTrue());
42 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
43 final List
<String
> recipients
= ns
.getList("recipient");
44 final var groupIdString
= ns
.getString("group");
46 final var noRecipients
= recipients
== null || recipients
.isEmpty();
47 if (noRecipients
&& groupIdString
== null) {
48 throw new UserErrorException("No recipients given");
50 if (!noRecipients
&& groupIdString
!= null) {
51 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
54 final var emoji
= ns
.getString("emoji");
55 final boolean isRemove
= ns
.getBoolean("remove");
56 final var targetAuthor
= ns
.getString("target_author");
57 final long targetTimestamp
= ns
.getLong("target_timestamp");
59 final var writer
= new PlainTextWriterImpl(System
.out
);
61 byte[] groupId
= null;
62 if (groupIdString
!= null) {
64 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
65 } catch (GroupIdFormatException e
) {
66 throw new UserErrorException("Invalid group id: " + e
.getMessage());
72 if (groupId
!= null) {
73 timestamp
= signal
.sendGroupMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, groupId
);
75 timestamp
= signal
.sendMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, recipients
);
77 writer
.println("{}", timestamp
);
78 } catch (AssertionError e
) {
79 handleAssertionError(e
);
81 } catch (UnknownObject e
) {
82 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
83 } catch (Signal
.Error
.InvalidNumber e
) {
84 throw new UserErrorException("Invalid number: " + e
.getMessage());
85 } catch (Signal
.Error
.GroupNotFound e
) {
86 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
87 } catch (DBusExecutionException e
) {
88 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());