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