]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendReactionCommand.java
a80b6a23bc72fe1273eabca6f82b83c153242361
[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 import static org.asamk.signal.util.ErrorUtils.handleAssertionError;
20
21 public class SendReactionCommand implements DbusCommand {
22
23 @Override
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")
29 .required(true)
30 .help("Specify the emoji, should be a single unicode grapheme cluster.");
31 subparser.addArgument("-a", "--target-author")
32 .required(true)
33 .help("Specify the number of the author of the message to which to react.");
34 subparser.addArgument("-t", "--target-timestamp")
35 .required(true)
36 .type(long.class)
37 .help("Specify the timestamp of the message to which to react.");
38 subparser.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments.storeTrue());
39 }
40
41 @Override
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");
45
46 final var noRecipients = recipients == null || recipients.isEmpty();
47 if (noRecipients && groupIdString == null) {
48 throw new UserErrorException("No recipients given");
49 }
50 if (!noRecipients && groupIdString != null) {
51 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
52 }
53
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");
58
59 final var writer = new PlainTextWriterImpl(System.out);
60
61 byte[] groupId = null;
62 if (groupIdString != null) {
63 try {
64 groupId = Util.decodeGroupId(groupIdString).serialize();
65 } catch (GroupIdFormatException e) {
66 throw new UserErrorException("Invalid group id: " + e.getMessage());
67 }
68 }
69
70 try {
71 long timestamp;
72 if (groupId != null) {
73 timestamp = signal.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
74 } else {
75 timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
76 }
77 writer.println("{}", timestamp);
78 } catch (AssertionError e) {
79 handleAssertionError(e);
80 throw 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());
89 }
90 }
91 }