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 ) throws UserErrorException
{
31 final var recipientIdentifiers
= new HashSet
<RecipientIdentifier
>();
33 recipientIdentifiers
.add(RecipientIdentifier
.NoteToSelf
.INSTANCE
);
35 if (recipientStrings
!= null) {
36 final var localNumber
= m
.getSelfNumber();
37 recipientIdentifiers
.addAll(CommandUtil
.getSingleRecipientIdentifiers(recipientStrings
, localNumber
));
39 if (groupIdStrings
!= null) {
40 recipientIdentifiers
.addAll(CommandUtil
.getGroupIdentifiers(groupIdStrings
));
43 if (recipientIdentifiers
.isEmpty()) {
44 throw new UserErrorException("No recipients given");
46 return recipientIdentifiers
;
49 public static Set
<RecipientIdentifier
.Group
> getGroupIdentifiers(Collection
<String
> groupIdStrings
) throws UserErrorException
{
50 if (groupIdStrings
== null) {
53 final var groupIds
= new HashSet
<RecipientIdentifier
.Group
>();
54 for (final var groupIdString
: groupIdStrings
) {
55 groupIds
.add(new RecipientIdentifier
.Group(getGroupId(groupIdString
)));
60 public static Set
<GroupId
> getGroupIds(Collection
<String
> groupIdStrings
) throws UserErrorException
{
61 if (groupIdStrings
== null) {
64 final var groupIds
= new HashSet
<GroupId
>();
65 for (final var groupIdString
: groupIdStrings
) {
66 groupIds
.add(getGroupId(groupIdString
));
71 public static GroupId
getGroupId(String groupId
) throws UserErrorException
{
72 if (groupId
== null) {
76 return GroupId
.fromBase64(groupId
);
77 } catch (GroupIdFormatException e
) {
78 throw new UserErrorException("Invalid group id: " + e
.getMessage());
82 public static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
83 final Collection
<String
> recipientStrings
, final String localNumber
84 ) throws UserErrorException
{
85 if (recipientStrings
== null) {
88 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
89 for (var recipientString
: recipientStrings
) {
90 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
95 public static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
96 final String recipientString
, final String localNumber
97 ) throws UserErrorException
{
99 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
100 } catch (InvalidNumberException e
) {
101 throw new UserErrorException("Invalid phone number '" + recipientString
+ "': " + e
.getMessage(), e
);
105 public static String
getCaptchaRequiredMessage(final CaptchaRequiredException e
, final boolean captchaProvided
) {
107 if (!captchaProvided
) {
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.""";
113 message
= "Invalid captcha given.";
115 if (e
.getNextAttemptTimestamp() > 0) {
116 message
+= "\nNext Captcha may be provided at " + DateUtils
.formatTimestamp(e
.getNextAttemptTimestamp());
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());
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"));
134 return new ReceiveConfig(ignoreAttachments
, ignoreStories
, sendReadReceipts
);