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