]>
nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/util/CommandUtil.java
b86a99894328acc744fac71190f7f29d13803163
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
.InvalidNumberException
;
6 import org
.asamk
.signal
.manager
.api
.RecipientIdentifier
;
7 import org
.asamk
.signal
.manager
.groups
.GroupId
;
8 import org
.asamk
.signal
.manager
.groups
.GroupIdFormatException
;
10 import java
.util
.Collection
;
11 import java
.util
.HashSet
;
12 import java
.util
.List
;
15 public class CommandUtil
{
17 private CommandUtil() {
20 public static Set
<RecipientIdentifier
> getRecipientIdentifiers(
22 final boolean isNoteToSelf
,
23 final List
<String
> recipientStrings
,
24 final List
<String
> groupIdStrings
25 ) throws UserErrorException
{
26 final var recipientIdentifiers
= new HashSet
<RecipientIdentifier
>();
28 recipientIdentifiers
.add(RecipientIdentifier
.NoteToSelf
.INSTANCE
);
30 if (recipientStrings
!= null) {
31 final var localNumber
= m
.getSelfNumber();
32 recipientIdentifiers
.addAll(CommandUtil
.getSingleRecipientIdentifiers(recipientStrings
, localNumber
));
34 if (groupIdStrings
!= null) {
35 recipientIdentifiers
.addAll(CommandUtil
.getGroupIdentifiers(groupIdStrings
));
38 if (recipientIdentifiers
.isEmpty()) {
39 throw new UserErrorException("No recipients given");
41 return recipientIdentifiers
;
44 public static Set
<RecipientIdentifier
.Group
> getGroupIdentifiers(Collection
<String
> groupIdStrings
) throws UserErrorException
{
45 if (groupIdStrings
== null) {
48 final var groupIds
= new HashSet
<RecipientIdentifier
.Group
>();
49 for (final var groupIdString
: groupIdStrings
) {
50 groupIds
.add(new RecipientIdentifier
.Group(getGroupId(groupIdString
)));
55 public static Set
<GroupId
> getGroupIds(Collection
<String
> groupIdStrings
) throws UserErrorException
{
56 if (groupIdStrings
== null) {
59 final var groupIds
= new HashSet
<GroupId
>();
60 for (final var groupIdString
: groupIdStrings
) {
61 groupIds
.add(getGroupId(groupIdString
));
66 public static GroupId
getGroupId(String groupId
) throws UserErrorException
{
67 if (groupId
== null) {
71 return GroupId
.fromBase64(groupId
);
72 } catch (GroupIdFormatException e
) {
73 throw new UserErrorException("Invalid group id: " + e
.getMessage());
77 public static Set
<RecipientIdentifier
.Single
> getSingleRecipientIdentifiers(
78 final Collection
<String
> recipientStrings
, final String localNumber
79 ) throws UserErrorException
{
80 if (recipientStrings
== null) {
83 final var identifiers
= new HashSet
<RecipientIdentifier
.Single
>();
84 for (var recipientString
: recipientStrings
) {
85 identifiers
.add(getSingleRecipientIdentifier(recipientString
, localNumber
));
90 public static RecipientIdentifier
.Single
getSingleRecipientIdentifier(
91 final String recipientString
, final String localNumber
92 ) throws UserErrorException
{
94 return RecipientIdentifier
.Single
.fromString(recipientString
, localNumber
);
95 } catch (InvalidNumberException e
) {
96 throw new UserErrorException("Invalid phone number '" + recipientString
+ "': " + e
.getMessage(), e
);