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
.PlainTextWriterImpl
;
8 import org
.asamk
.signal
.commands
.exceptions
.CommandException
;
9 import org
.asamk
.signal
.commands
.exceptions
.IOErrorException
;
10 import org
.asamk
.signal
.commands
.exceptions
.UserErrorException
;
11 import org
.asamk
.signal
.manager
.Manager
;
12 import org
.asamk
.signal
.manager
.groups
.GroupId
;
13 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
14 import org
.asamk
.signal
.manager
.groups
.GroupNotFoundException
;
15 import org
.asamk
.signal
.manager
.groups
.NotAGroupMemberException
;
16 import org
.asamk
.signal
.util
.Util
;
17 import org
.whispersystems
.libsignal
.util
.Pair
;
18 import org
.whispersystems
.signalservice
.api
.messages
.SendMessageResult
;
19 import org
.whispersystems
.signalservice
.api
.util
.InvalidNumberException
;
21 import java
.io
.IOException
;
22 import java
.util
.List
;
24 import static org
.asamk
.signal
.util
.ErrorUtils
.handleAssertionError
;
25 import static org
.asamk
.signal
.util
.ErrorUtils
.handleTimestampAndSendMessageResults
;
27 public class SendReactionCommand
implements LocalCommand
{
30 public void attachToSubparser(final Subparser subparser
) {
31 subparser
.help("Send reaction to a previously received or sent message.");
32 subparser
.addArgument("-g", "--group").help("Specify the recipient group ID.");
33 subparser
.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
34 subparser
.addArgument("-e", "--emoji")
36 .help("Specify the emoji, should be a single unicode grapheme cluster.");
37 subparser
.addArgument("-a", "--target-author")
39 .help("Specify the number of the author of the message to which to react.");
40 subparser
.addArgument("-t", "--target-timestamp")
43 .help("Specify the timestamp of the message to which to react.");
44 subparser
.addArgument("-r", "--remove").help("Remove a reaction.").action(Arguments
.storeTrue());
48 public void handleCommand(final Namespace ns
, final Manager m
) throws CommandException
{
49 final List
<String
> recipients
= ns
.getList("recipient");
50 final var groupIdString
= ns
.getString("group");
52 final var noRecipients
= recipients
== null || recipients
.isEmpty();
53 if (noRecipients
&& groupIdString
== null) {
54 throw new UserErrorException("No recipients given");
56 if (!noRecipients
&& groupIdString
!= null) {
57 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
60 final var emoji
= ns
.getString("emoji");
61 final boolean isRemove
= ns
.getBoolean("remove");
62 final var targetAuthor
= ns
.getString("target_author");
63 final long targetTimestamp
= ns
.getLong("target_timestamp");
65 final var writer
= new PlainTextWriterImpl(System
.out
);
67 final Pair
<Long
, List
<SendMessageResult
>> results
;
69 GroupId groupId
= null;
70 if (groupId
!= null) {
72 groupId
= Util
.decodeGroupId(groupIdString
);
73 } catch (GroupIdFormatException e
) {
74 throw new UserErrorException("Invalid group id:" + e
.getMessage());
79 if (groupId
!= null) {
80 results
= m
.sendGroupMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, groupId
);
82 results
= m
.sendMessageReaction(emoji
, isRemove
, targetAuthor
, targetTimestamp
, recipients
);
84 handleTimestampAndSendMessageResults(writer
, results
.first(), results
.second());
85 } catch (IOException e
) {
86 throw new IOErrorException("Failed to send message: " + e
.getMessage());
87 } catch (AssertionError e
) {
88 handleAssertionError(e
);
90 } catch (GroupNotFoundException e
) {
91 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
92 } catch (NotAGroupMemberException e
) {
93 throw new UserErrorException("Failed to send to group: " + e
.getMessage());
94 } catch (InvalidNumberException e
) {
95 throw new UserErrorException("Invalid number: " + e
.getMessage());