]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/Manager.java
Implement listStickerPacks command
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / Manager.java
1 package org.asamk.signal.manager;
2
3 import org.asamk.signal.manager.api.Configuration;
4 import org.asamk.signal.manager.api.Device;
5 import org.asamk.signal.manager.api.Group;
6 import org.asamk.signal.manager.api.Identity;
7 import org.asamk.signal.manager.api.InactiveGroupLinkException;
8 import org.asamk.signal.manager.api.InvalidDeviceLinkException;
9 import org.asamk.signal.manager.api.InvalidStickerException;
10 import org.asamk.signal.manager.api.Message;
11 import org.asamk.signal.manager.api.MessageEnvelope;
12 import org.asamk.signal.manager.api.Pair;
13 import org.asamk.signal.manager.api.RecipientIdentifier;
14 import org.asamk.signal.manager.api.SendGroupMessageResults;
15 import org.asamk.signal.manager.api.SendMessageResults;
16 import org.asamk.signal.manager.api.StickerPack;
17 import org.asamk.signal.manager.api.TypingAction;
18 import org.asamk.signal.manager.api.UnregisteredRecipientException;
19 import org.asamk.signal.manager.api.UpdateGroup;
20 import org.asamk.signal.manager.config.ServiceConfig;
21 import org.asamk.signal.manager.config.ServiceEnvironment;
22 import org.asamk.signal.manager.groups.GroupId;
23 import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
24 import org.asamk.signal.manager.groups.GroupNotFoundException;
25 import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
26 import org.asamk.signal.manager.groups.LastGroupAdminException;
27 import org.asamk.signal.manager.groups.NotAGroupMemberException;
28 import org.asamk.signal.manager.storage.SignalAccount;
29 import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
30 import org.asamk.signal.manager.storage.recipients.Contact;
31 import org.asamk.signal.manager.storage.recipients.Profile;
32 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
33 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
34
35 import java.io.Closeable;
36 import java.io.File;
37 import java.io.IOException;
38 import java.net.URI;
39 import java.time.Duration;
40 import java.util.Arrays;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.Optional;
44 import java.util.Set;
45 import java.util.UUID;
46
47 public interface Manager extends Closeable {
48
49 static Manager init(
50 String number,
51 File settingsPath,
52 ServiceEnvironment serviceEnvironment,
53 String userAgent,
54 TrustNewIdentity trustNewIdentity
55 ) throws IOException, NotRegisteredException {
56 var pathConfig = PathConfig.createDefault(settingsPath);
57
58 if (!SignalAccount.userExists(pathConfig.dataPath(), number)) {
59 throw new NotRegisteredException();
60 }
61
62 var account = SignalAccount.load(pathConfig.dataPath(), number, true, trustNewIdentity);
63
64 if (!account.isRegistered()) {
65 account.close();
66 throw new NotRegisteredException();
67 }
68
69 final var serviceEnvironmentConfig = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent);
70
71 return new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent);
72 }
73
74 static void initLogger() {
75 LibSignalLogger.initLogger();
76 }
77
78 static boolean isValidNumber(final String e164Number, final String countryCode) {
79 return PhoneNumberFormatter.isValidNumber(e164Number, countryCode);
80 }
81
82 static List<String> getAllLocalAccountNumbers(File settingsPath) {
83 var pathConfig = PathConfig.createDefault(settingsPath);
84 final var dataPath = pathConfig.dataPath();
85 final var files = dataPath.listFiles();
86
87 if (files == null) {
88 return List.of();
89 }
90
91 return Arrays.stream(files)
92 .filter(File::isFile)
93 .map(File::getName)
94 .filter(file -> PhoneNumberFormatter.isValidNumber(file, null))
95 .toList();
96 }
97
98 String getSelfNumber();
99
100 void checkAccountState() throws IOException;
101
102 /**
103 * This is used for checking a set of phone numbers for registration on Signal
104 *
105 * @param numbers The set of phone number in question
106 * @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null.
107 * @throws IOException if it's unable to get the contacts to check if they're registered
108 */
109 Map<String, Pair<String, UUID>> areUsersRegistered(Set<String> numbers) throws IOException;
110
111 void updateAccountAttributes(String deviceName) throws IOException;
112
113 Configuration getConfiguration();
114
115 void updateConfiguration(Configuration configuration) throws IOException, NotMasterDeviceException;
116
117 /**
118 * @param givenName if null, the previous givenName will be kept
119 * @param familyName if null, the previous familyName will be kept
120 * @param about if null, the previous about text will be kept
121 * @param aboutEmoji if null, the previous about emoji will be kept
122 * @param avatar if avatar is null the image from the local avatar store is used (if present),
123 */
124 void setProfile(
125 String givenName, String familyName, String about, String aboutEmoji, Optional<File> avatar
126 ) throws IOException;
127
128 void unregister() throws IOException;
129
130 void deleteAccount() throws IOException;
131
132 void submitRateLimitRecaptchaChallenge(String challenge, String captcha) throws IOException;
133
134 List<Device> getLinkedDevices() throws IOException;
135
136 void removeLinkedDevices(long deviceId) throws IOException;
137
138 void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException;
139
140 void setRegistrationLockPin(Optional<String> pin) throws IOException, NotMasterDeviceException;
141
142 Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
143
144 List<Group> getGroups();
145
146 SendGroupMessageResults quitGroup(
147 GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
148 ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException, UnregisteredRecipientException;
149
150 void deleteGroup(GroupId groupId) throws IOException;
151
152 Pair<GroupId, SendGroupMessageResults> createGroup(
153 String name, Set<RecipientIdentifier.Single> members, File avatarFile
154 ) throws IOException, AttachmentInvalidException, UnregisteredRecipientException;
155
156 SendGroupMessageResults updateGroup(
157 final GroupId groupId, final UpdateGroup updateGroup
158 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException, UnregisteredRecipientException;
159
160 Pair<GroupId, SendGroupMessageResults> joinGroup(
161 GroupInviteLinkUrl inviteLinkUrl
162 ) throws IOException, InactiveGroupLinkException;
163
164 SendMessageResults sendTypingMessage(
165 TypingAction action, Set<RecipientIdentifier> recipients
166 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
167
168 SendMessageResults sendReadReceipt(
169 RecipientIdentifier.Single sender, List<Long> messageIds
170 ) throws IOException;
171
172 SendMessageResults sendViewedReceipt(
173 RecipientIdentifier.Single sender, List<Long> messageIds
174 ) throws IOException;
175
176 SendMessageResults sendMessage(
177 Message message, Set<RecipientIdentifier> recipients
178 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException, InvalidStickerException;
179
180 SendMessageResults sendRemoteDeleteMessage(
181 long targetSentTimestamp, Set<RecipientIdentifier> recipients
182 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
183
184 SendMessageResults sendMessageReaction(
185 String emoji,
186 boolean remove,
187 RecipientIdentifier.Single targetAuthor,
188 long targetSentTimestamp,
189 Set<RecipientIdentifier> recipients
190 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException;
191
192 SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
193
194 void deleteRecipient(RecipientIdentifier.Single recipient);
195
196 void deleteContact(RecipientIdentifier.Single recipient);
197
198 void setContactName(
199 RecipientIdentifier.Single recipient, String name
200 ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
201
202 void setContactBlocked(
203 RecipientIdentifier.Single recipient, boolean blocked
204 ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
205
206 void setGroupBlocked(
207 GroupId groupId, boolean blocked
208 ) throws GroupNotFoundException, IOException, NotMasterDeviceException;
209
210 /**
211 * Change the expiration timer for a contact
212 */
213 void setExpirationTimer(
214 RecipientIdentifier.Single recipient, int messageExpirationTimer
215 ) throws IOException, UnregisteredRecipientException;
216
217 /**
218 * Upload the sticker pack from path.
219 *
220 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
221 * @return if successful, returns the URL to install the sticker pack in the signal app
222 */
223 URI uploadStickerPack(File path) throws IOException, StickerPackInvalidException;
224
225 List<StickerPack> getStickerPacks();
226
227 void requestAllSyncData() throws IOException;
228
229 /**
230 * Add a handler to receive new messages.
231 * Will start receiving messages from server, if not already started.
232 */
233 default void addReceiveHandler(ReceiveMessageHandler handler) {
234 addReceiveHandler(handler, false);
235 }
236
237 void addReceiveHandler(ReceiveMessageHandler handler, final boolean isWeakListener);
238
239 /**
240 * Remove a handler to receive new messages.
241 * Will stop receiving messages from server, if this was the last registered receiver.
242 */
243 void removeReceiveHandler(ReceiveMessageHandler handler);
244
245 boolean isReceiving();
246
247 /**
248 * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
249 */
250 void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException;
251
252 /**
253 * Receive new messages from server, returns only if the thread is interrupted.
254 */
255 void receiveMessages(ReceiveMessageHandler handler) throws IOException;
256
257 void setIgnoreAttachments(boolean ignoreAttachments);
258
259 boolean hasCaughtUpWithOldMessages();
260
261 boolean isContactBlocked(RecipientIdentifier.Single recipient);
262
263 void sendContacts() throws IOException;
264
265 List<Pair<RecipientAddress, Contact>> getContacts();
266
267 String getContactOrProfileName(RecipientIdentifier.Single recipient);
268
269 Group getGroup(GroupId groupId);
270
271 List<Identity> getIdentities();
272
273 List<Identity> getIdentities(RecipientIdentifier.Single recipient);
274
275 /**
276 * Trust this the identity with this fingerprint
277 *
278 * @param recipient account of the identity
279 * @param fingerprint Fingerprint
280 */
281 boolean trustIdentityVerified(
282 RecipientIdentifier.Single recipient, byte[] fingerprint
283 ) throws UnregisteredRecipientException;
284
285 /**
286 * Trust this the identity with this safety number
287 *
288 * @param recipient account of the identity
289 * @param safetyNumber Safety number
290 */
291 boolean trustIdentityVerifiedSafetyNumber(
292 RecipientIdentifier.Single recipient, String safetyNumber
293 ) throws UnregisteredRecipientException;
294
295 /**
296 * Trust this the identity with this scannable safety number
297 *
298 * @param recipient account of the identity
299 * @param safetyNumber Scannable safety number
300 */
301 boolean trustIdentityVerifiedSafetyNumber(
302 RecipientIdentifier.Single recipient, byte[] safetyNumber
303 ) throws UnregisteredRecipientException;
304
305 /**
306 * Trust all keys of this identity without verification
307 *
308 * @param recipient account of the identity
309 */
310 boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) throws UnregisteredRecipientException;
311
312 void addClosedListener(Runnable listener);
313
314 @Override
315 void close() throws IOException;
316
317 interface ReceiveMessageHandler {
318
319 ReceiveMessageHandler EMPTY = (envelope, e) -> {
320 };
321
322 void handleMessage(MessageEnvelope envelope, Throwable e);
323 }
324 }