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