]> nmode's Git Repositories - signal-cli/blob - src/main/java/org/asamk/signal/util/CommandUtil.java
Implement sending message quotes
[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.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;
9
10 import java.util.Collection;
11 import java.util.HashSet;
12 import java.util.List;
13 import java.util.Set;
14
15 public class CommandUtil {
16
17 private CommandUtil() {
18 }
19
20 public static Set<RecipientIdentifier> getRecipientIdentifiers(
21 final Manager m,
22 final boolean isNoteToSelf,
23 final List<String> recipientStrings,
24 final List<String> groupIdStrings
25 ) throws UserErrorException {
26 final var recipientIdentifiers = new HashSet<RecipientIdentifier>();
27 if (isNoteToSelf) {
28 recipientIdentifiers.add(RecipientIdentifier.NoteToSelf.INSTANCE);
29 }
30 if (recipientStrings != null) {
31 final var localNumber = m.getSelfNumber();
32 recipientIdentifiers.addAll(CommandUtil.getSingleRecipientIdentifiers(recipientStrings, localNumber));
33 }
34 if (groupIdStrings != null) {
35 recipientIdentifiers.addAll(CommandUtil.getGroupIdentifiers(groupIdStrings));
36 }
37
38 if (recipientIdentifiers.isEmpty()) {
39 throw new UserErrorException("No recipients given");
40 }
41 return recipientIdentifiers;
42 }
43
44 public static Set<RecipientIdentifier.Group> getGroupIdentifiers(Collection<String> groupIdStrings) throws UserErrorException {
45 if (groupIdStrings == null) {
46 return Set.of();
47 }
48 final var groupIds = new HashSet<RecipientIdentifier.Group>();
49 for (final var groupIdString : groupIdStrings) {
50 groupIds.add(new RecipientIdentifier.Group(getGroupId(groupIdString)));
51 }
52 return groupIds;
53 }
54
55 public static Set<GroupId> getGroupIds(Collection<String> groupIdStrings) throws UserErrorException {
56 if (groupIdStrings == null) {
57 return Set.of();
58 }
59 final var groupIds = new HashSet<GroupId>();
60 for (final var groupIdString : groupIdStrings) {
61 groupIds.add(getGroupId(groupIdString));
62 }
63 return groupIds;
64 }
65
66 public static GroupId getGroupId(String groupId) throws UserErrorException {
67 if (groupId == null) {
68 return null;
69 }
70 try {
71 return GroupId.fromBase64(groupId);
72 } catch (GroupIdFormatException e) {
73 throw new UserErrorException("Invalid group id: " + e.getMessage());
74 }
75 }
76
77 public static Set<RecipientIdentifier.Single> getSingleRecipientIdentifiers(
78 final Collection<String> recipientStrings, final String localNumber
79 ) throws UserErrorException {
80 if (recipientStrings == null) {
81 return Set.of();
82 }
83 final var identifiers = new HashSet<RecipientIdentifier.Single>();
84 for (var recipientString : recipientStrings) {
85 identifiers.add(getSingleRecipientIdentifier(recipientString, localNumber));
86 }
87 return identifiers;
88 }
89
90 public static RecipientIdentifier.Single getSingleRecipientIdentifier(
91 final String recipientString, final String localNumber
92 ) throws UserErrorException {
93 try {
94 return RecipientIdentifier.Single.fromString(recipientString, localNumber);
95 } catch (InvalidNumberException e) {
96 throw new UserErrorException("Invalid phone number '" + recipientString + "': " + e.getMessage(), e);
97 }
98 }
99 }