]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/util/CommandUtil.java
Extract getReceiveConfig to utils
[signal-cli] / src / main / java / org / asamk / signal / util / CommandUtil.java
1 package org.asamk.signal.util;
2
3 import net.sourceforge.argparse4j.inf.Namespace;
4
5 import org.asamk.signal.commands.exceptions.UserErrorException;
6 import org.asamk.signal.manager.Manager;
7 import org.asamk.signal.manager.api.CaptchaRequiredException;
8 import org.asamk.signal.manager.api.GroupId;
9 import org.asamk.signal.manager.api.GroupIdFormatException;
10 import org.asamk.signal.manager.api.InvalidNumberException;
11 import org.asamk.signal.manager.api.RateLimitException;
12 import org.asamk.signal.manager.api.ReceiveConfig;
13 import org.asamk.signal.manager.api.RecipientIdentifier;
14
15 import java.util.Collection;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Set;
19
20 public class CommandUtil {
21
22 private CommandUtil() {
23 }
24
25 public static Set<RecipientIdentifier> getRecipientIdentifiers(
26 final Manager m,
27 final boolean isNoteToSelf,
28 final List<String> recipientStrings,
29 final List<String> groupIdStrings
30 ) throws UserErrorException {
31 final var recipientIdentifiers = new HashSet<RecipientIdentifier>();
32 if (isNoteToSelf) {
33 recipientIdentifiers.add(RecipientIdentifier.NoteToSelf.INSTANCE);
34 }
35 if (recipientStrings != null) {
36 final var localNumber = m.getSelfNumber();
37 recipientIdentifiers.addAll(CommandUtil.getSingleRecipientIdentifiers(recipientStrings, localNumber));
38 }
39 if (groupIdStrings != null) {
40 recipientIdentifiers.addAll(CommandUtil.getGroupIdentifiers(groupIdStrings));
41 }
42
43 if (recipientIdentifiers.isEmpty()) {
44 throw new UserErrorException("No recipients given");
45 }
46 return recipientIdentifiers;
47 }
48
49 public static Set<RecipientIdentifier.Group> getGroupIdentifiers(Collection<String> groupIdStrings) throws UserErrorException {
50 if (groupIdStrings == null) {
51 return Set.of();
52 }
53 final var groupIds = new HashSet<RecipientIdentifier.Group>();
54 for (final var groupIdString : groupIdStrings) {
55 groupIds.add(new RecipientIdentifier.Group(getGroupId(groupIdString)));
56 }
57 return groupIds;
58 }
59
60 public static Set<GroupId> getGroupIds(Collection<String> groupIdStrings) throws UserErrorException {
61 if (groupIdStrings == null) {
62 return Set.of();
63 }
64 final var groupIds = new HashSet<GroupId>();
65 for (final var groupIdString : groupIdStrings) {
66 groupIds.add(getGroupId(groupIdString));
67 }
68 return groupIds;
69 }
70
71 public static GroupId getGroupId(String groupId) throws UserErrorException {
72 if (groupId == null) {
73 return null;
74 }
75 try {
76 return GroupId.fromBase64(groupId);
77 } catch (GroupIdFormatException e) {
78 throw new UserErrorException("Invalid group id: " + e.getMessage());
79 }
80 }
81
82 public static Set<RecipientIdentifier.Single> getSingleRecipientIdentifiers(
83 final Collection<String> recipientStrings, final String localNumber
84 ) throws UserErrorException {
85 if (recipientStrings == null) {
86 return Set.of();
87 }
88 final var identifiers = new HashSet<RecipientIdentifier.Single>();
89 for (var recipientString : recipientStrings) {
90 identifiers.add(getSingleRecipientIdentifier(recipientString, localNumber));
91 }
92 return identifiers;
93 }
94
95 public static RecipientIdentifier.Single getSingleRecipientIdentifier(
96 final String recipientString, final String localNumber
97 ) throws UserErrorException {
98 try {
99 return RecipientIdentifier.Single.fromString(recipientString, localNumber);
100 } catch (InvalidNumberException e) {
101 throw new UserErrorException("Invalid phone number '" + recipientString + "': " + e.getMessage(), e);
102 }
103 }
104
105 public static String getCaptchaRequiredMessage(final CaptchaRequiredException e, final boolean captchaProvided) {
106 String message;
107 if (!captchaProvided) {
108 message = """
109 Captcha required for verification, use --captcha CAPTCHA
110 To get the token, go to https://signalcaptchas.org/registration/generate.html
111 After solving the captcha, right-click on the "Open Signal" link and copy the link.""";
112 } else {
113 message = "Invalid captcha given.";
114 }
115 if (e.getNextAttemptTimestamp() > 0) {
116 message += "\nNext Captcha may be provided at " + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
117 }
118 return message;
119 }
120
121 public static String getRateLimitMessage(final RateLimitException e) {
122 String message = "Rate limit reached";
123 if (e.getNextAttemptTimestamp() > 0) {
124 message += "\nNext attempt may be tried at " + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
125 }
126 return message;
127 }
128
129 public static ReceiveConfig getReceiveConfig(final Namespace ns) {
130 final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
131 final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories"));
132 final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts"));
133
134 return new ReceiveConfig(ignoreAttachments, ignoreStories, sendReadReceipts);
135 }
136 }