]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendTypingCommand.java
Let commands specify their own default output if none is provided by the user
[signal-cli] / src / main / java / org / asamk / signal / commands / SendTypingCommand.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.OutputWriter;
8 import org.asamk.signal.commands.exceptions.CommandException;
9 import org.asamk.signal.commands.exceptions.UserErrorException;
10 import org.asamk.signal.manager.Manager;
11 import org.asamk.signal.manager.api.TypingAction;
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.signalservice.api.crypto.UntrustedIdentityException;
18 import org.whispersystems.signalservice.api.util.InvalidNumberException;
19
20 import java.io.IOException;
21 import java.util.HashSet;
22
23 public class SendTypingCommand implements JsonRpcLocalCommand {
24
25 @Override
26 public String getName() {
27 return "sendTyping";
28 }
29
30 @Override
31 public void attachToSubparser(final Subparser subparser) {
32 subparser.help(
33 "Send typing message to trigger a typing indicator for the recipient. Indicator will be shown for 15seconds unless a typing STOP message is sent first.");
34 subparser.addArgument("-g", "--group-id", "--group").help("Specify the recipient group ID.");
35 subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
36 subparser.addArgument("-s", "--stop").help("Send a typing STOP message.").action(Arguments.storeTrue());
37 }
38
39 @Override
40 public void handleCommand(
41 final Namespace ns, final Manager m, final OutputWriter outputWriter
42 ) throws CommandException {
43 final var recipients = ns.<String>getList("recipient");
44 final var groupIdString = ns.getString("group-id");
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 action = ns.getBoolean("stop") ? TypingAction.STOP : TypingAction.START;
55
56 GroupId groupId = null;
57 if (groupIdString != null) {
58 try {
59 groupId = Util.decodeGroupId(groupIdString);
60 } catch (GroupIdFormatException e) {
61 throw new UserErrorException("Invalid group id: " + e.getMessage());
62 }
63 }
64
65 try {
66 if (groupId != null) {
67 m.sendGroupTypingMessage(action, groupId);
68 } else {
69 m.sendTypingMessage(action, new HashSet<>(recipients));
70 }
71 } catch (IOException | UntrustedIdentityException e) {
72 throw new UserErrorException("Failed to send message: " + e.getMessage());
73 } catch (GroupNotFoundException | NotAGroupMemberException e) {
74 throw new UserErrorException("Failed to send to group: " + e.getMessage());
75 } catch (InvalidNumberException e) {
76 throw new UserErrorException("Invalid number: " + e.getMessage());
77 }
78 }
79 }