]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendReactionCommand.java
Add PlainTextWriter interface
[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.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;
17
18 import java.util.List;
19
20 public class SendReactionCommand implements DbusCommand {
21
22 private final OutputWriter outputWriter;
23
24 public SendReactionCommand(final OutputWriter outputWriter) {
25 this.outputWriter = outputWriter;
26 }
27
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")
33 .required(true)
34 .help("Specify the emoji, should be a single unicode grapheme cluster.");
35 subparser.addArgument("-a", "--target-author")
36 .required(true)
37 .help("Specify the number of the author of the message to which to react.");
38 subparser.addArgument("-t", "--target-timestamp")
39 .required(true)
40 .type(long.class)
41 .help("Specify the timestamp of the message to which to react.");
42 subparser.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments.storeTrue());
43 }
44
45 @Override
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");
49
50 final var noRecipients = recipients == null || recipients.isEmpty();
51 if (noRecipients && groupIdString == null) {
52 throw new UserErrorException("No recipients given");
53 }
54 if (!noRecipients && groupIdString != null) {
55 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
56 }
57
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");
62
63 final var writer = (PlainTextWriter) outputWriter;
64
65 byte[] groupId = null;
66 if (groupIdString != null) {
67 try {
68 groupId = Util.decodeGroupId(groupIdString).serialize();
69 } catch (GroupIdFormatException e) {
70 throw new UserErrorException("Invalid group id: " + e.getMessage());
71 }
72 }
73
74 try {
75 long timestamp;
76 if (groupId != null) {
77 timestamp = signal.sendGroupMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, groupId);
78 } else {
79 timestamp = signal.sendMessageReaction(emoji, isRemove, targetAuthor, targetTimestamp, recipients);
80 }
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());
90 }
91 }
92 }