]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/util/CommandUtil.java
039ac2a7f4d06edd36e2a17aacfab4769cfe41d4
[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 final List<String> usernameStrings
31 ) throws UserErrorException {
32 final var recipientIdentifiers = new HashSet<RecipientIdentifier>();
33 if (isNoteToSelf) {
34 recipientIdentifiers.add(RecipientIdentifier.NoteToSelf.INSTANCE);
35 }
36 if (recipientStrings != null) {
37 final var localNumber = m.getSelfNumber();
38 recipientIdentifiers.addAll(CommandUtil.getSingleRecipientIdentifiers(recipientStrings, localNumber));
39 }
40 if (groupIdStrings != null) {
41 recipientIdentifiers.addAll(CommandUtil.getGroupIdentifiers(groupIdStrings));
42 }
43 if (usernameStrings != null) {
44 recipientIdentifiers.addAll(CommandUtil.getUsernameIdentifiers(usernameStrings));
45 }
46
47 if (recipientIdentifiers.isEmpty()) {
48 throw new UserErrorException("No recipients given");
49 }
50 return recipientIdentifiers;
51 }
52
53 public static Set<RecipientIdentifier.Group> getGroupIdentifiers(Collection<String> groupIdStrings) throws UserErrorException {
54 if (groupIdStrings == null) {
55 return Set.of();
56 }
57 final var groupIds = new HashSet<RecipientIdentifier.Group>();
58 for (final var groupIdString : groupIdStrings) {
59 groupIds.add(new RecipientIdentifier.Group(getGroupId(groupIdString)));
60 }
61 return groupIds;
62 }
63
64 public static Set<GroupId> getGroupIds(Collection<String> groupIdStrings) throws UserErrorException {
65 if (groupIdStrings == null) {
66 return Set.of();
67 }
68 final var groupIds = new HashSet<GroupId>();
69 for (final var groupIdString : groupIdStrings) {
70 groupIds.add(getGroupId(groupIdString));
71 }
72 return groupIds;
73 }
74
75 public static GroupId getGroupId(String groupId) throws UserErrorException {
76 if (groupId == null) {
77 return null;
78 }
79 try {
80 return GroupId.fromBase64(groupId);
81 } catch (GroupIdFormatException e) {
82 throw new UserErrorException("Invalid group id: " + e.getMessage());
83 }
84 }
85
86 public static Set<RecipientIdentifier.Single> getSingleRecipientIdentifiers(
87 final Collection<String> recipientStrings, final String localNumber
88 ) throws UserErrorException {
89 if (recipientStrings == null) {
90 return Set.of();
91 }
92 final var identifiers = new HashSet<RecipientIdentifier.Single>();
93 for (var recipientString : recipientStrings) {
94 identifiers.add(getSingleRecipientIdentifier(recipientString, localNumber));
95 }
96 return identifiers;
97 }
98
99 public static RecipientIdentifier.Single getSingleRecipientIdentifier(
100 final String recipientString, final String localNumber
101 ) throws UserErrorException {
102 try {
103 return RecipientIdentifier.Single.fromString(recipientString, localNumber);
104 } catch (InvalidNumberException e) {
105 throw new UserErrorException("Invalid phone number '" + recipientString + "': " + e.getMessage(), e);
106 }
107 }
108
109 public static Set<RecipientIdentifier.Username> getUsernameIdentifiers(Collection<String> usernameIdStrings) {
110 if (usernameIdStrings == null) {
111 return Set.of();
112 }
113 final var usernameIds = new HashSet<RecipientIdentifier.Username>();
114 for (final var usernameIdString : usernameIdStrings) {
115 usernameIds.add(new RecipientIdentifier.Username(usernameIdString));
116 }
117 return usernameIds;
118 }
119
120 public static String getCaptchaRequiredMessage(final CaptchaRequiredException e, final boolean captchaProvided) {
121 String message;
122 if (!captchaProvided) {
123 message = """
124 Captcha required for verification, use --captcha CAPTCHA
125 To get the token, go to https://signalcaptchas.org/registration/generate.html
126 After solving the captcha, right-click on the "Open Signal" link and copy the link.""";
127 } else {
128 message = "Invalid captcha given.";
129 }
130 if (e.getNextAttemptTimestamp() > 0) {
131 message += "\nNext Captcha may be provided at " + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
132 }
133 return message;
134 }
135
136 public static String getRateLimitMessage(final RateLimitException e) {
137 String message = "Rate limit reached";
138 if (e.getNextAttemptTimestamp() > 0) {
139 message += "\nNext attempt may be tried at " + DateUtils.formatTimestamp(e.getNextAttemptTimestamp());
140 }
141 return message;
142 }
143
144 public static ReceiveConfig getReceiveConfig(final Namespace ns) {
145 final var ignoreAttachments = Boolean.TRUE.equals(ns.getBoolean("ignore-attachments"));
146 final var ignoreStories = Boolean.TRUE.equals(ns.getBoolean("ignore-stories"));
147 final var sendReadReceipts = Boolean.TRUE.equals(ns.getBoolean("send-read-receipts"));
148
149 return new ReceiveConfig(ignoreAttachments, ignoreStories, sendReadReceipts);
150 }
151 }