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