]>
nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendReactionCommand.java
44286acb8d8bdc40f0275087a3b175b00beb0b10
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 public class SendReactionCommand
implements DbusCommand
{
22 public void attachToSubparser(final Subparser subparser
) {
23 subparser
.help("Send reaction to a previously received or sent message.");
24 subparser
.addArgument("-g", "--group").help("Specify the recipient group ID.");
25 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
26 subparser
.addArgument("-e", "--emoji")
28 .help("Specify the emoji, should be a single unicode grapheme cluster.");
29 subparser
.addArgument("-a", "--target-author")
31 .help("Specify the number of the author of the message to which to react.");
32 subparser
.addArgument("-t", "--target-timestamp")
35 .help("Specify the timestamp of the message to which to react.");
36 subparser
.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments
.storeTrue());
40 public void handleCommand(final Namespace ns
, final Signal signal
) throws CommandException
{
41 final List
<String
> recipients
= ns
.getList("recipient");
42 final var groupIdString
= ns
.getString("group");
44 final var noRecipients
= recipients
== null || recipients
.isEmpty();
45 if (noRecipients
&& groupIdString
== null) {
46 throw new UserErrorException("No recipients given");
48 if (!noRecipients
&& groupIdString
!= null) {
49 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
52 final var emoji
= ns
.getString("emoji");
53 final boolean isRemove
= ns
.getBoolean("remove");
54 final var targetAuthor
= ns
.getString("target-author");
55 final long targetTimestamp
= ns
.getLong("target-timestamp");
57 final var writer
= new PlainTextWriterImpl(System
.out
);
59 byte[] groupId
= null;
60 if (groupIdString
!= null) {
62 groupId
= Util
.decodeGroupId(groupIdString
).serialize();
63 } catch (GroupIdFormatException e
) {
64 throw new UserErrorException("Invalid group id: " + e
.getMessage());
70 if (groupId
!= null) {
71 timestamp
= signal
.sendGroupMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, groupId
);
73 timestamp
= signal
.sendMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, recipients
);
75 writer
.println("{}", timestamp
);
76 } catch (UnknownObject e
) {
77 throw new UserErrorException("Failed to find dbus object, maybe missing the -u flag: " + e
.getMessage());
78 } catch (Signal
.Error
.InvalidNumber e
) {
79 throw new UserErrorException("Invalid number: " + e
.getMessage());
80 } catch (Signal
.Error
.GroupNotFound e
) {
81 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
82 } catch (DBusExecutionException e
) {
83 throw new UnexpectedErrorException("Failed to send message: " + e
.getMessage());