]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/Manager.java
85644f61104d657471bd82eddf1708e0e7755eb7
[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.AlreadyReceivingException;
4 import org.asamk.signal.manager.api.AttachmentInvalidException;
5 import org.asamk.signal.manager.api.CaptchaRejectedException;
6 import org.asamk.signal.manager.api.CaptchaRequiredException;
7 import org.asamk.signal.manager.api.Configuration;
8 import org.asamk.signal.manager.api.Device;
9 import org.asamk.signal.manager.api.DeviceLimitExceededException;
10 import org.asamk.signal.manager.api.DeviceLinkUrl;
11 import org.asamk.signal.manager.api.Group;
12 import org.asamk.signal.manager.api.GroupId;
13 import org.asamk.signal.manager.api.GroupInviteLinkUrl;
14 import org.asamk.signal.manager.api.GroupNotFoundException;
15 import org.asamk.signal.manager.api.GroupSendingNotAllowedException;
16 import org.asamk.signal.manager.api.Identity;
17 import org.asamk.signal.manager.api.IdentityVerificationCode;
18 import org.asamk.signal.manager.api.InactiveGroupLinkException;
19 import org.asamk.signal.manager.api.IncorrectPinException;
20 import org.asamk.signal.manager.api.InvalidDeviceLinkException;
21 import org.asamk.signal.manager.api.InvalidStickerException;
22 import org.asamk.signal.manager.api.InvalidUsernameException;
23 import org.asamk.signal.manager.api.LastGroupAdminException;
24 import org.asamk.signal.manager.api.Message;
25 import org.asamk.signal.manager.api.MessageEnvelope;
26 import org.asamk.signal.manager.api.NonNormalizedPhoneNumberException;
27 import org.asamk.signal.manager.api.NotAGroupMemberException;
28 import org.asamk.signal.manager.api.NotPrimaryDeviceException;
29 import org.asamk.signal.manager.api.Pair;
30 import org.asamk.signal.manager.api.PendingAdminApprovalException;
31 import org.asamk.signal.manager.api.PinLockedException;
32 import org.asamk.signal.manager.api.RateLimitException;
33 import org.asamk.signal.manager.api.ReceiveConfig;
34 import org.asamk.signal.manager.api.Recipient;
35 import org.asamk.signal.manager.api.RecipientIdentifier;
36 import org.asamk.signal.manager.api.SendGroupMessageResults;
37 import org.asamk.signal.manager.api.SendMessageResults;
38 import org.asamk.signal.manager.api.StickerPack;
39 import org.asamk.signal.manager.api.StickerPackId;
40 import org.asamk.signal.manager.api.StickerPackInvalidException;
41 import org.asamk.signal.manager.api.StickerPackUrl;
42 import org.asamk.signal.manager.api.TypingAction;
43 import org.asamk.signal.manager.api.UnregisteredRecipientException;
44 import org.asamk.signal.manager.api.UpdateGroup;
45 import org.asamk.signal.manager.api.UpdateProfile;
46 import org.asamk.signal.manager.api.UserStatus;
47 import org.asamk.signal.manager.api.UsernameLinkUrl;
48 import org.asamk.signal.manager.api.UsernameStatus;
49 import org.asamk.signal.manager.api.VerificationMethodNotAvailableException;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
52 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
53
54 import java.io.Closeable;
55 import java.io.File;
56 import java.io.IOException;
57 import java.io.InputStream;
58 import java.time.Duration;
59 import java.util.Collection;
60 import java.util.List;
61 import java.util.Map;
62 import java.util.Optional;
63 import java.util.Set;
64
65 public interface Manager extends Closeable {
66
67 static boolean isValidNumber(final String e164Number, final String countryCode) {
68 return PhoneNumberFormatter.isValidNumber(e164Number, countryCode);
69 }
70
71 static boolean isSignalClientAvailable() {
72 final Logger logger = LoggerFactory.getLogger(Manager.class);
73 try {
74 try {
75 org.signal.libsignal.internal.Native.UuidCiphertext_CheckValidContents(new byte[0]);
76 } catch (Exception e) {
77 logger.trace("Expected exception when checking libsignal-client: {}", e.getMessage());
78 }
79 return true;
80 } catch (UnsatisfiedLinkError e) {
81 logger.warn("Failed to call libsignal-client: {}", e.getMessage());
82 return false;
83 }
84 }
85
86 String getSelfNumber();
87
88 /**
89 * This is used for checking a set of phone numbers for registration on Signal
90 *
91 * @param numbers The set of phone number in question
92 * @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null.
93 * @throws IOException if it's unable to get the contacts to check if they're registered
94 */
95 Map<String, UserStatus> getUserStatus(Set<String> numbers) throws IOException, RateLimitException;
96
97 Map<String, UsernameStatus> getUsernameStatus(Set<String> usernames);
98
99 void updateAccountAttributes(
100 String deviceName,
101 Boolean unrestrictedUnidentifiedSender,
102 final Boolean discoverableByNumber,
103 final Boolean numberSharing
104 ) throws IOException;
105
106 Configuration getConfiguration();
107
108 void updateConfiguration(Configuration configuration) throws NotPrimaryDeviceException;
109
110 /**
111 * Update the user's profile.
112 * If a field is null, the previous value will be kept.
113 */
114 void updateProfile(UpdateProfile updateProfile) throws IOException;
115
116 String getUsername();
117
118 UsernameLinkUrl getUsernameLink();
119
120 /**
121 * Set a username for the account.
122 * If the username is null, it will be deleted.
123 */
124 void setUsername(String username) throws IOException, InvalidUsernameException;
125
126 /**
127 * Set a username for the account.
128 * If the username is null, it will be deleted.
129 */
130 void deleteUsername() throws IOException;
131
132 void startChangeNumber(
133 String newNumber,
134 boolean voiceVerification,
135 String captcha
136 ) throws RateLimitException, IOException, CaptchaRequiredException, NonNormalizedPhoneNumberException, NotPrimaryDeviceException, VerificationMethodNotAvailableException;
137
138 void finishChangeNumber(
139 String newNumber,
140 String verificationCode,
141 String pin
142 ) throws IncorrectPinException, PinLockedException, IOException, NotPrimaryDeviceException;
143
144 void unregister() throws IOException;
145
146 void deleteAccount() throws IOException;
147
148 void submitRateLimitRecaptchaChallenge(
149 String challenge,
150 String captcha
151 ) throws IOException, CaptchaRejectedException;
152
153 List<Device> getLinkedDevices() throws IOException;
154
155 void removeLinkedDevices(int deviceId) throws IOException, NotPrimaryDeviceException;
156
157 void addDeviceLink(DeviceLinkUrl linkUri) throws IOException, InvalidDeviceLinkException, NotPrimaryDeviceException, DeviceLimitExceededException;
158
159 void setRegistrationLockPin(Optional<String> pin) throws IOException, NotPrimaryDeviceException;
160
161 List<Group> getGroups();
162
163 SendGroupMessageResults quitGroup(
164 GroupId groupId,
165 Set<RecipientIdentifier.Single> groupAdmins
166 ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException, UnregisteredRecipientException;
167
168 void deleteGroup(GroupId groupId) throws IOException;
169
170 Pair<GroupId, SendGroupMessageResults> createGroup(
171 String name,
172 Set<RecipientIdentifier.Single> members,
173 String avatarFile
174 ) throws IOException, AttachmentInvalidException, UnregisteredRecipientException;
175
176 SendGroupMessageResults updateGroup(
177 final GroupId groupId,
178 final UpdateGroup updateGroup
179 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException, UnregisteredRecipientException;
180
181 Pair<GroupId, SendGroupMessageResults> joinGroup(
182 GroupInviteLinkUrl inviteLinkUrl
183 ) throws IOException, InactiveGroupLinkException, PendingAdminApprovalException;
184
185 SendMessageResults sendTypingMessage(
186 TypingAction action,
187 Set<RecipientIdentifier> recipients
188 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
189
190 SendMessageResults sendReadReceipt(RecipientIdentifier.Single sender, List<Long> messageIds);
191
192 SendMessageResults sendViewedReceipt(RecipientIdentifier.Single sender, List<Long> messageIds);
193
194 SendMessageResults sendMessage(
195 Message message,
196 Set<RecipientIdentifier> recipients,
197 boolean notifySelf
198 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException, InvalidStickerException;
199
200 SendMessageResults sendEditMessage(
201 Message message,
202 Set<RecipientIdentifier> recipients,
203 long editTargetTimestamp
204 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException, InvalidStickerException;
205
206 SendMessageResults sendRemoteDeleteMessage(
207 long targetSentTimestamp,
208 Set<RecipientIdentifier> recipients
209 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
210
211 SendMessageResults sendMessageReaction(
212 String emoji,
213 boolean remove,
214 RecipientIdentifier.Single targetAuthor,
215 long targetSentTimestamp,
216 Set<RecipientIdentifier> recipients,
217 final boolean isStory
218 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException;
219
220 SendMessageResults sendPaymentNotificationMessage(
221 byte[] receipt,
222 String note,
223 RecipientIdentifier.Single recipient
224 ) throws IOException;
225
226 SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
227
228 SendMessageResults sendMessageRequestResponse(
229 MessageEnvelope.Sync.MessageRequestResponse.Type type,
230 Set<RecipientIdentifier> recipientIdentifiers
231 );
232
233 void hideRecipient(RecipientIdentifier.Single recipient);
234
235 void deleteRecipient(RecipientIdentifier.Single recipient);
236
237 void deleteContact(RecipientIdentifier.Single recipient);
238
239 void setContactName(
240 RecipientIdentifier.Single recipient,
241 String givenName,
242 final String familyName
243 ) throws NotPrimaryDeviceException, UnregisteredRecipientException;
244
245 void setContactsBlocked(
246 Collection<RecipientIdentifier.Single> recipient,
247 boolean blocked
248 ) throws NotPrimaryDeviceException, IOException, UnregisteredRecipientException;
249
250 void setGroupsBlocked(
251 Collection<GroupId> groupId,
252 boolean blocked
253 ) throws GroupNotFoundException, IOException, NotPrimaryDeviceException;
254
255 /**
256 * Change the expiration timer for a contact
257 */
258 void setExpirationTimer(
259 RecipientIdentifier.Single recipient,
260 int messageExpirationTimer
261 ) throws IOException, UnregisteredRecipientException;
262
263 /**
264 * Upload the sticker pack from path.
265 *
266 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
267 * @return if successful, returns the URL to install the sticker pack in the signal app
268 */
269 StickerPackUrl uploadStickerPack(File path) throws IOException, StickerPackInvalidException;
270
271 void installStickerPack(StickerPackUrl url) throws IOException;
272
273 List<StickerPack> getStickerPacks();
274
275 void requestAllSyncData() throws IOException;
276
277 /**
278 * Add a handler to receive new messages.
279 * Will start receiving messages from server, if not already started.
280 */
281 default void addReceiveHandler(ReceiveMessageHandler handler) {
282 addReceiveHandler(handler, false);
283 }
284
285 void addReceiveHandler(ReceiveMessageHandler handler, final boolean isWeakListener);
286
287 /**
288 * Remove a handler to receive new messages.
289 * Will stop receiving messages from server, if this was the last registered receiver.
290 */
291 void removeReceiveHandler(ReceiveMessageHandler handler);
292
293 boolean isReceiving();
294
295 /**
296 * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
297 */
298 void receiveMessages(
299 Optional<Duration> timeout,
300 Optional<Integer> maxMessages,
301 ReceiveMessageHandler handler
302 ) throws IOException, AlreadyReceivingException;
303
304 void stopReceiveMessages();
305
306 void setReceiveConfig(ReceiveConfig receiveConfig);
307
308 boolean isContactBlocked(RecipientIdentifier.Single recipient);
309
310 void sendContacts() throws IOException;
311
312 List<Recipient> getRecipients(
313 boolean onlyContacts,
314 Optional<Boolean> blocked,
315 Collection<RecipientIdentifier.Single> address,
316 Optional<String> name
317 );
318
319 String getContactOrProfileName(RecipientIdentifier.Single recipient);
320
321 Group getGroup(GroupId groupId);
322
323 List<Identity> getIdentities();
324
325 List<Identity> getIdentities(RecipientIdentifier.Single recipient);
326
327 /**
328 * Trust this the identity with this fingerprint/safetyNumber
329 *
330 * @param recipient account of the identity
331 */
332 boolean trustIdentityVerified(
333 RecipientIdentifier.Single recipient,
334 IdentityVerificationCode verificationCode
335 ) throws UnregisteredRecipientException;
336
337 /**
338 * Trust all keys of this identity without verification
339 *
340 * @param recipient account of the identity
341 */
342 boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) throws UnregisteredRecipientException;
343
344 void addAddressChangedListener(Runnable listener);
345
346 void addClosedListener(Runnable listener);
347
348 InputStream retrieveAttachment(final String id) throws IOException;
349
350 InputStream retrieveContactAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
351
352 InputStream retrieveProfileAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
353
354 InputStream retrieveGroupAvatar(final GroupId groupId) throws IOException;
355
356 InputStream retrieveSticker(final StickerPackId stickerPackId, final int stickerId) throws IOException;
357
358 @Override
359 void close();
360
361 interface ReceiveMessageHandler {
362
363 ReceiveMessageHandler EMPTY = (envelope, e) -> {
364 };
365
366 void handleMessage(MessageEnvelope envelope, Throwable e);
367 }
368 }