1 package org
.asamk
.signal
.util
;
3 import net
.sourceforge
.argparse4j
.inf
.Namespace
;
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
;
15 import java
.util
.Collection
;
16 import java
.util
.HashSet
;
17 import java
.util
.List
;
20 public class CommandUtil
{
22 private CommandUtil() {
25 public static Set
<RecipientIdentifier
> getRecipientIdentifiers(
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
>();
34 recipientIdentifiers
.add(RecipientIdentifier
.NoteToSelf
.INSTANCE
);
36 if (recipientStrings
!= null) {
37 final var localNumber
= m
.getSelfNumber();
38 recipientIdentifiers
.addAll(CommandUtil
.getSingleRecipientIdentifiers(recipientStrings
, localNumber
));
40 if (groupIdStrings
!= null) {
41 recipientIdentifiers
.addAll(CommandUtil
.getGroupIdentifiers(groupIdStrings
));
43 if (usernameStrings
!= null) {
44 recipientIdentifiers
.addAll(CommandUtil
.getUsernameIdentifiers(usernameStrings
));
47 if (recipientIdentifiers
.isEmpty()) {
48 throw new UserErrorException("No recipients given");
50 return recipientIdentifiers
;
53 public static Set
<RecipientIdentifier
.Group
> getGroupIdentifiers(Collection
<String
> groupIdStrings
) throws UserErrorException
{
54 if (groupIdStrings
== null) {
57 final var groupIds
= new HashSet
<RecipientIdentifier
.Group
>();
58 for (final var groupIdString
: groupIdStrings
) {
59 groupIds
.add(new RecipientIdentifier
.Group(getGroupId(groupIdString
)));
64 public static Set
<GroupId
> getGroupIds(Collection
<String
> groupIdStrings
) throws UserErrorException
{
65 if (groupIdStrings
== null) {
68 final var groupIds
= new HashSet
<GroupId
>();
69 for (final var groupIdString
: groupIdStrings
) {
70 groupIds
.add(getGroupId(groupIdString
));
75 public static GroupId
getGroupId(String groupId
) throws UserErrorException
{
76 if (groupId
== null) {
80 return GroupId
.fromBase64(groupId
);
81 } catch (GroupIdFormatException e
) {
82 throw new UserErrorException("Invalid group id: " + e
.getMessage());
86 public static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
87 final Collection
<String
> recipientStrings
,
88 final String localNumber
89 ) throws UserErrorException
{
90 if (recipientStrings
== null) {
93 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
94 for (var recipientString
: recipientStrings
) {
95 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
100 public static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
101 final String recipientString
,
102 final String localNumber
103 ) throws UserErrorException
{
105 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
106 } catch (InvalidNumberException e
) {
107 throw new UserErrorException("Invalid phone number '" + recipientString
+ "': " + e
.getMessage(), e
);
111 public static Set
<RecipientIdentifier
.Username
> getUsernameIdentifiers(Collection
<String
> usernameIdStrings
) {
112 if (usernameIdStrings
== null) {
115 final var usernameIds
= new HashSet
<RecipientIdentifier
.Username
>();
116 for (final var usernameIdString
: usernameIdStrings
) {
117 usernameIds
.add(new RecipientIdentifier
.Username(usernameIdString
));
122 public static String
getCaptchaRequiredMessage(final CaptchaRequiredException e
, final boolean captchaProvided
) {
124 if (!captchaProvided
) {
126 Captcha required for verification, use --captcha CAPTCHA
127 To get the token, go to https://signalcaptchas.org/registration/generate.html
128 After solving the captcha, right-click on the "Open Signal" link and copy the link.""";
130 message
= "Invalid captcha given.";
132 if (e
.getNextAttemptTimestamp() > 0) {
133 message
+= "\nNext Captcha may be provided at " + DateUtils
.formatTimestamp(e
.getNextAttemptTimestamp());
138 public static String
getRateLimitMessage(final RateLimitException e
) {
139 String message
= "Rate limit reached";
140 if (e
.getNextAttemptTimestamp() > 0) {
141 message
+= "\nNext attempt may be tried at " + DateUtils
.formatTimestamp(e
.getNextAttemptTimestamp());
146 public static ReceiveConfig
getReceiveConfig(final Namespace ns
) {
147 final var ignoreAttachments
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-attachments"));
148 final var ignoreStories
= Boolean
.TRUE
.equals(ns
.getBoolean("ignore-stories"));
149 final var sendReadReceipts
= Boolean
.TRUE
.equals(ns
.getBoolean("send-read-receipts"));
151 return new ReceiveConfig(ignoreAttachments
, ignoreStories
, sendReadReceipts
);