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