]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/commands/SendTypingCommand.java
Refactor output writers
[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 LocalCommand {
24
25 public SendTypingCommand(final OutputWriter outputWriter) {
26 }
27
28 public static void attachToSubparser(final Subparser subparser) {
29 subparser.help(
30 "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.");
31 subparser.addArgument("-g", "--group").help("Specify the recipient group ID.");
32 subparser.addArgument("recipient").help("Specify the recipients' phone number.").nargs("*");
33 subparser.addArgument("-s", "--stop").help("Send a typing STOP message.").action(Arguments.storeTrue());
34 }
35
36 @Override
37 public void handleCommand(final Namespace ns, final Manager m) throws CommandException {
38 final var recipients = ns.<String>getList("recipient");
39 final var groupIdString = ns.getString("group");
40
41 final var noRecipients = recipients == null || recipients.isEmpty();
42 if (noRecipients && groupIdString == null) {
43 throw new UserErrorException("No recipients given");
44 }
45 if (!noRecipients && groupIdString != null) {
46 throw new UserErrorException("You cannot specify recipients by phone number and groups at the same time");
47 }
48
49 final var action = ns.getBoolean("stop") ? TypingAction.STOP : TypingAction.START;
50
51 GroupId groupId = null;
52 if (groupIdString != null) {
53 try {
54 groupId = Util.decodeGroupId(groupIdString);
55 } catch (GroupIdFormatException e) {
56 throw new UserErrorException("Invalid group id: " + e.getMessage());
57 }
58 }
59
60 try {
61 if (groupId != null) {
62 m.sendGroupTypingMessage(action, groupId);
63 } else {
64 m.sendTypingMessage(action, new HashSet<>(recipients));
65 }
66 } catch (IOException | UntrustedIdentityException e) {
67 throw new UserErrorException("Failed to send message: " + e.getMessage());
68 } catch (GroupNotFoundException | NotAGroupMemberException e) {
69 throw new UserErrorException("Failed to send to group: " + e.getMessage());
70 } catch (InvalidNumberException e) {
71 throw new UserErrorException("Invalid number: " + e.getMessage());
72 }
73 }
74 }