]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendReactionCommand.java
44286acb8d8bdc40f0275087a3b175b00beb0b10
[signal-cli] / src / main / java / org / asamk / signal / commands / SendReactionCommand.java
1 package org.asamk.signal.commands;
2
3 import net.sourceforge.argparse4j.impl.Arguments;
4 import net.sourceforge.argparse4j.inf.Namespace;
5 import net.sourceforge.argparse4j.inf.Subparser;
6
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;
16
17 import java.util.List;
18
19 public class SendReactionCommand implements DbusCommand {
20
21 @Override
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")
27 .required(true)
28 .help("Specify the emoji, should be a single unicode grapheme cluster.");
29 subparser.addArgument("-a", "--target-author")
30 .required(true)
31 .help("Specify the number of the author of the message to which to react.");
32 subparser.addArgument("-t", "--target-timestamp")
33 .required(true)
34 .type(long.class)
35 .help("Specify the timestamp of the message to which to react.");
36 subparser.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments.storeTrue());
37 }
38
39 @Override
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");
43
44 final var noRecipients = recipients == null || recipients.isEmpty();
45 if (noRecipients && groupIdString == null) {
46 throw new UserErrorException("No recipients given");
47 }
48 if (!noRecipients && groupIdString != null) {
49 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
50 }
51
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");
56
57 final var writer = new PlainTextWriterImpl(System.out);
58
59 byte[] groupId = null;
60 if (groupIdString != null) {
61 try {
62 groupId = Util.decodeGroupId(groupIdString).serialize();
63 } catch (GroupIdFormatException e) {
64 throw new UserErrorException("Invalid group id: " + e.getMessage());
65 }
66 }
67
68 try {
69 long timestamp;
70 if (groupId != null) {
71 timestamp = signal.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
72 } else {
73 timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
74 }
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());
84 }
85 }
86 }