1 package org
.asamk
.signal
.util
;
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
;
12 import java
.util
.Collection
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
17 public class CommandUtil
{
19 private CommandUtil() {
22 public static Set
<RecipientIdentifier
> getRecipientIdentifiers(
24 final boolean isNoteToSelf
,
25 final List
<String
> recipientStrings
,
26 final List
<String
> groupIdStrings
27 ) throws UserErrorException
{
28 final var recipientIdentifiers
= new HashSet
<RecipientIdentifier
>();
30 recipientIdentifiers
.add(RecipientIdentifier
.NoteToSelf
.INSTANCE
);
32 if (recipientStrings
!= null) {
33 final var localNumber
= m
.getSelfNumber();
34 recipientIdentifiers
.addAll(CommandUtil
.getSingleRecipientIdentifiers(recipientStrings
, localNumber
));
36 if (groupIdStrings
!= null) {
37 recipientIdentifiers
.addAll(CommandUtil
.getGroupIdentifiers(groupIdStrings
));
40 if (recipientIdentifiers
.isEmpty()) {
41 throw new UserErrorException("No recipients given");
43 return recipientIdentifiers
;
46 public static Set
<RecipientIdentifier
.Group
> getGroupIdentifiers(Collection
<String
> groupIdStrings
) throws UserErrorException
{
47 if (groupIdStrings
== null) {
50 final var groupIds
= new HashSet
<RecipientIdentifier
.Group
>();
51 for (final var groupIdString
: groupIdStrings
) {
52 groupIds
.add(new RecipientIdentifier
.Group(getGroupId(groupIdString
)));
57 public static Set
<GroupId
> getGroupIds(Collection
<String
> groupIdStrings
) throws UserErrorException
{
58 if (groupIdStrings
== null) {
61 final var groupIds
= new HashSet
<GroupId
>();
62 for (final var groupIdString
: groupIdStrings
) {
63 groupIds
.add(getGroupId(groupIdString
));
68 public static GroupId
getGroupId(String groupId
) throws UserErrorException
{
69 if (groupId
== null) {
73 return GroupId
.fromBase64(groupId
);
74 } catch (GroupIdFormatException e
) {
75 throw new UserErrorException("Invalid group id: " + e
.getMessage());
79 public static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
80 final Collection
<String
> recipientStrings
, final String localNumber
81 ) throws UserErrorException
{
82 if (recipientStrings
== null) {
85 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
86 for (var recipientString
: recipientStrings
) {
87 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
92 public static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
93 final String recipientString
, final String localNumber
94 ) throws UserErrorException
{
96 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
97 } catch (InvalidNumberException e
) {
98 throw new UserErrorException("Invalid phone number '" + recipientString
+ "': " + e
.getMessage(), e
);
102 public static String
getCaptchaRequiredMessage(final CaptchaRequiredException e
, final boolean captchaProvided
) {
104 if (!captchaProvided
) {
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.""";
111 message
= "Invalid captcha given.";
113 if (e
.getNextAttemptTimestamp() > 0) {
114 message
+= "\nNext Captcha may be provided at " + DateUtils
.formatTimestamp(e
.getNextAttemptTimestamp());
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());