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