]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/Manager.java
4b0869944e361c493dfb78ac53e3f00baec11197
[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, boolean voiceVerification, String captcha
134 ) throws RateLimitException, IOException, CaptchaRequiredException, NonNormalizedPhoneNumberException, NotPrimaryDeviceException, VerificationMethodNotAvailableException;
135
136 void finishChangeNumber(
137 String newNumber, String verificationCode, String pin
138 ) throws IncorrectPinException, PinLockedException, IOException, NotPrimaryDeviceException;
139
140 void unregister() throws IOException;
141
142 void deleteAccount() throws IOException;
143
144 void submitRateLimitRecaptchaChallenge(
145 String challenge, String captcha
146 ) throws IOException, CaptchaRejectedException;
147
148 List<Device> getLinkedDevices() throws IOException;
149
150 void removeLinkedDevices(int deviceId) throws IOException, NotPrimaryDeviceException;
151
152 void addDeviceLink(DeviceLinkUrl linkUri) throws IOException, InvalidDeviceLinkException, NotPrimaryDeviceException, DeviceLimitExceededException;
153
154 void setRegistrationLockPin(Optional<String> pin) throws IOException, NotPrimaryDeviceException;
155
156 List<Group> getGroups();
157
158 SendGroupMessageResults quitGroup(
159 GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
160 ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException, UnregisteredRecipientException;
161
162 void deleteGroup(GroupId groupId) throws IOException;
163
164 Pair<GroupId, SendGroupMessageResults> createGroup(
165 String name, Set<RecipientIdentifier.Single> members, String avatarFile
166 ) throws IOException, AttachmentInvalidException, UnregisteredRecipientException;
167
168 SendGroupMessageResults updateGroup(
169 final GroupId groupId, final UpdateGroup updateGroup
170 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException, UnregisteredRecipientException;
171
172 Pair<GroupId, SendGroupMessageResults> joinGroup(
173 GroupInviteLinkUrl inviteLinkUrl
174 ) throws IOException, InactiveGroupLinkException, PendingAdminApprovalException;
175
176 SendMessageResults sendTypingMessage(
177 TypingAction action, Set<RecipientIdentifier> recipients
178 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
179
180 SendMessageResults sendReadReceipt(
181 RecipientIdentifier.Single sender, List<Long> messageIds
182 );
183
184 SendMessageResults sendViewedReceipt(
185 RecipientIdentifier.Single sender, List<Long> messageIds
186 );
187
188 SendMessageResults sendMessage(
189 Message message, Set<RecipientIdentifier> recipients, boolean notifySelf
190 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException, InvalidStickerException;
191
192 SendMessageResults sendEditMessage(
193 Message message, Set<RecipientIdentifier> recipients, long editTargetTimestamp
194 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException, InvalidStickerException;
195
196 SendMessageResults sendRemoteDeleteMessage(
197 long targetSentTimestamp, Set<RecipientIdentifier> recipients
198 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
199
200 SendMessageResults sendMessageReaction(
201 String emoji,
202 boolean remove,
203 RecipientIdentifier.Single targetAuthor,
204 long targetSentTimestamp,
205 Set<RecipientIdentifier> recipients,
206 final boolean isStory
207 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException;
208
209 SendMessageResults sendPaymentNotificationMessage(
210 byte[] receipt, String note, RecipientIdentifier.Single recipient
211 ) throws IOException;
212
213 SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
214
215 SendMessageResults sendMessageRequestResponse(
216 MessageEnvelope.Sync.MessageRequestResponse.Type type, Set<RecipientIdentifier> recipientIdentifiers
217 );
218
219 void hideRecipient(RecipientIdentifier.Single recipient);
220
221 void deleteRecipient(RecipientIdentifier.Single recipient);
222
223 void deleteContact(RecipientIdentifier.Single recipient);
224
225 void setContactName(
226 RecipientIdentifier.Single recipient, String givenName, final String familyName
227 ) throws NotPrimaryDeviceException, UnregisteredRecipientException;
228
229 void setContactsBlocked(
230 Collection<RecipientIdentifier.Single> recipient, boolean blocked
231 ) throws NotPrimaryDeviceException, IOException, UnregisteredRecipientException;
232
233 void setGroupsBlocked(
234 Collection<GroupId> groupId, boolean blocked
235 ) throws GroupNotFoundException, IOException, NotPrimaryDeviceException;
236
237 /**
238 * Change the expiration timer for a contact
239 */
240 void setExpirationTimer(
241 RecipientIdentifier.Single recipient, int messageExpirationTimer
242 ) throws IOException, UnregisteredRecipientException;
243
244 /**
245 * Upload the sticker pack from path.
246 *
247 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
248 * @return if successful, returns the URL to install the sticker pack in the signal app
249 */
250 StickerPackUrl uploadStickerPack(File path) throws IOException, StickerPackInvalidException;
251
252 void installStickerPack(StickerPackUrl url) throws IOException;
253
254 List<StickerPack> getStickerPacks();
255
256 void requestAllSyncData() throws IOException;
257
258 /**
259 * Add a handler to receive new messages.
260 * Will start receiving messages from server, if not already started.
261 */
262 default void addReceiveHandler(ReceiveMessageHandler handler) {
263 addReceiveHandler(handler, false);
264 }
265
266 void addReceiveHandler(ReceiveMessageHandler handler, final boolean isWeakListener);
267
268 /**
269 * Remove a handler to receive new messages.
270 * Will stop receiving messages from server, if this was the last registered receiver.
271 */
272 void removeReceiveHandler(ReceiveMessageHandler handler);
273
274 boolean isReceiving();
275
276 /**
277 * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
278 */
279 void receiveMessages(
280 Optional<Duration> timeout, Optional<Integer> maxMessages, ReceiveMessageHandler handler
281 ) throws IOException, AlreadyReceivingException;
282
283 void stopReceiveMessages();
284
285 void setReceiveConfig(ReceiveConfig receiveConfig);
286
287 boolean isContactBlocked(RecipientIdentifier.Single recipient);
288
289 void sendContacts() throws IOException;
290
291 List<Recipient> getRecipients(
292 boolean onlyContacts,
293 Optional<Boolean> blocked,
294 Collection<RecipientIdentifier.Single> address,
295 Optional<String> name
296 );
297
298 String getContactOrProfileName(RecipientIdentifier.Single recipient);
299
300 Group getGroup(GroupId groupId);
301
302 List<Identity> getIdentities();
303
304 List<Identity> getIdentities(RecipientIdentifier.Single recipient);
305
306 /**
307 * Trust this the identity with this fingerprint/safetyNumber
308 *
309 * @param recipient account of the identity
310 */
311 boolean trustIdentityVerified(
312 RecipientIdentifier.Single recipient, IdentityVerificationCode verificationCode
313 ) throws UnregisteredRecipientException;
314
315 /**
316 * Trust all keys of this identity without verification
317 *
318 * @param recipient account of the identity
319 */
320 boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) throws UnregisteredRecipientException;
321
322 void addAddressChangedListener(Runnable listener);
323
324 void addClosedListener(Runnable listener);
325
326 InputStream retrieveAttachment(final String id) throws IOException;
327
328 InputStream retrieveContactAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
329
330 InputStream retrieveProfileAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
331
332 InputStream retrieveGroupAvatar(final GroupId groupId) throws IOException;
333
334 InputStream retrieveSticker(final StickerPackId stickerPackId, final int stickerId) throws IOException;
335
336 @Override
337 void close();
338
339 interface ReceiveMessageHandler {
340
341 ReceiveMessageHandler EMPTY = (envelope, e) -> {
342 };
343
344 void handleMessage(MessageEnvelope envelope, Throwable e);
345 }
346 }