]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/Manager.java
Extract ProvisioningManager and RegistrationManager interfaces
[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.Configuration;
4 import org.asamk.signal.manager.api.Device;
5 import org.asamk.signal.manager.api.Group;
6 import org.asamk.signal.manager.api.Identity;
7 import org.asamk.signal.manager.api.InactiveGroupLinkException;
8 import org.asamk.signal.manager.api.InvalidDeviceLinkException;
9 import org.asamk.signal.manager.api.Message;
10 import org.asamk.signal.manager.api.MessageEnvelope;
11 import org.asamk.signal.manager.api.Pair;
12 import org.asamk.signal.manager.api.RecipientIdentifier;
13 import org.asamk.signal.manager.api.SendGroupMessageResults;
14 import org.asamk.signal.manager.api.SendMessageResults;
15 import org.asamk.signal.manager.api.TypingAction;
16 import org.asamk.signal.manager.api.UnregisteredRecipientException;
17 import org.asamk.signal.manager.api.UpdateGroup;
18 import org.asamk.signal.manager.config.ServiceConfig;
19 import org.asamk.signal.manager.config.ServiceEnvironment;
20 import org.asamk.signal.manager.groups.GroupId;
21 import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
22 import org.asamk.signal.manager.groups.GroupNotFoundException;
23 import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
24 import org.asamk.signal.manager.groups.LastGroupAdminException;
25 import org.asamk.signal.manager.groups.NotAGroupMemberException;
26 import org.asamk.signal.manager.storage.SignalAccount;
27 import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
28 import org.asamk.signal.manager.storage.recipients.Contact;
29 import org.asamk.signal.manager.storage.recipients.Profile;
30 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
31 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
32
33 import java.io.Closeable;
34 import java.io.File;
35 import java.io.IOException;
36 import java.net.URI;
37 import java.time.Duration;
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.Optional;
42 import java.util.Set;
43 import java.util.UUID;
44
45 public interface Manager extends Closeable {
46
47 static Manager init(
48 String number,
49 File settingsPath,
50 ServiceEnvironment serviceEnvironment,
51 String userAgent,
52 TrustNewIdentity trustNewIdentity
53 ) throws IOException, NotRegisteredException {
54 var pathConfig = PathConfig.createDefault(settingsPath);
55
56 if (!SignalAccount.userExists(pathConfig.dataPath(), number)) {
57 throw new NotRegisteredException();
58 }
59
60 var account = SignalAccount.load(pathConfig.dataPath(), number, true, trustNewIdentity);
61
62 if (!account.isRegistered()) {
63 account.close();
64 throw new NotRegisteredException();
65 }
66
67 final var serviceEnvironmentConfig = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent);
68
69 return new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent);
70 }
71
72 static void initLogger() {
73 LibSignalLogger.initLogger();
74 }
75
76 static boolean isValidNumber(final String e164Number, final String countryCode) {
77 return PhoneNumberFormatter.isValidNumber(e164Number, countryCode);
78 }
79
80 static List<String> getAllLocalAccountNumbers(File settingsPath) {
81 var pathConfig = PathConfig.createDefault(settingsPath);
82 final var dataPath = pathConfig.dataPath();
83 final var files = dataPath.listFiles();
84
85 if (files == null) {
86 return List.of();
87 }
88
89 return Arrays.stream(files)
90 .filter(File::isFile)
91 .map(File::getName)
92 .filter(file -> PhoneNumberFormatter.isValidNumber(file, null))
93 .toList();
94 }
95
96 String getSelfNumber();
97
98 void checkAccountState() throws IOException;
99
100 /**
101 * This is used for checking a set of phone numbers for registration on Signal
102 *
103 * @param numbers The set of phone number in question
104 * @return A map of numbers to canonicalized number and uuid. If a number is not registered the uuid is null.
105 * @throws IOException if it's unable to get the contacts to check if they're registered
106 */
107 Map<String, Pair<String, UUID>> areUsersRegistered(Set<String> numbers) throws IOException;
108
109 void updateAccountAttributes(String deviceName) throws IOException;
110
111 Configuration getConfiguration();
112
113 void updateConfiguration(Configuration configuration) throws IOException, NotMasterDeviceException;
114
115 /**
116 * @param givenName if null, the previous givenName will be kept
117 * @param familyName if null, the previous familyName will be kept
118 * @param about if null, the previous about text will be kept
119 * @param aboutEmoji if null, the previous about emoji will be kept
120 * @param avatar if avatar is null the image from the local avatar store is used (if present),
121 */
122 void setProfile(
123 String givenName, String familyName, String about, String aboutEmoji, Optional<File> avatar
124 ) throws IOException;
125
126 void unregister() throws IOException;
127
128 void deleteAccount() throws IOException;
129
130 void submitRateLimitRecaptchaChallenge(String challenge, String captcha) throws IOException;
131
132 List<Device> getLinkedDevices() throws IOException;
133
134 void removeLinkedDevices(long deviceId) throws IOException;
135
136 void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException;
137
138 void setRegistrationLockPin(Optional<String> pin) throws IOException, NotMasterDeviceException;
139
140 Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException;
141
142 List<Group> getGroups();
143
144 SendGroupMessageResults quitGroup(
145 GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
146 ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException, UnregisteredRecipientException;
147
148 void deleteGroup(GroupId groupId) throws IOException;
149
150 Pair<GroupId, SendGroupMessageResults> createGroup(
151 String name, Set<RecipientIdentifier.Single> members, File avatarFile
152 ) throws IOException, AttachmentInvalidException, UnregisteredRecipientException;
153
154 SendGroupMessageResults updateGroup(
155 final GroupId groupId, final UpdateGroup updateGroup
156 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException, UnregisteredRecipientException;
157
158 Pair<GroupId, SendGroupMessageResults> joinGroup(
159 GroupInviteLinkUrl inviteLinkUrl
160 ) throws IOException, InactiveGroupLinkException;
161
162 SendMessageResults sendTypingMessage(
163 TypingAction action, Set<RecipientIdentifier> recipients
164 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
165
166 SendMessageResults sendReadReceipt(
167 RecipientIdentifier.Single sender, List<Long> messageIds
168 ) throws IOException;
169
170 SendMessageResults sendViewedReceipt(
171 RecipientIdentifier.Single sender, List<Long> messageIds
172 ) throws IOException;
173
174 SendMessageResults sendMessage(
175 Message message, Set<RecipientIdentifier> recipients
176 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException;
177
178 SendMessageResults sendRemoteDeleteMessage(
179 long targetSentTimestamp, Set<RecipientIdentifier> recipients
180 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
181
182 SendMessageResults sendMessageReaction(
183 String emoji,
184 boolean remove,
185 RecipientIdentifier.Single targetAuthor,
186 long targetSentTimestamp,
187 Set<RecipientIdentifier> recipients
188 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException, UnregisteredRecipientException;
189
190 SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
191
192 void deleteRecipient(RecipientIdentifier.Single recipient);
193
194 void deleteContact(RecipientIdentifier.Single recipient);
195
196 void setContactName(
197 RecipientIdentifier.Single recipient, String name
198 ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
199
200 void setContactBlocked(
201 RecipientIdentifier.Single recipient, boolean blocked
202 ) throws NotMasterDeviceException, IOException, UnregisteredRecipientException;
203
204 void setGroupBlocked(
205 GroupId groupId, boolean blocked
206 ) throws GroupNotFoundException, IOException, NotMasterDeviceException;
207
208 /**
209 * Change the expiration timer for a contact
210 */
211 void setExpirationTimer(
212 RecipientIdentifier.Single recipient, int messageExpirationTimer
213 ) throws IOException, UnregisteredRecipientException;
214
215 /**
216 * Upload the sticker pack from path.
217 *
218 * @param path Path can be a path to a manifest.json file or to a zip file that contains a manifest.json file
219 * @return if successful, returns the URL to install the sticker pack in the signal app
220 */
221 URI uploadStickerPack(File path) throws IOException, StickerPackInvalidException;
222
223 void requestAllSyncData() throws IOException;
224
225 /**
226 * Add a handler to receive new messages.
227 * Will start receiving messages from server, if not already started.
228 */
229 default void addReceiveHandler(ReceiveMessageHandler handler) {
230 addReceiveHandler(handler, false);
231 }
232
233 void addReceiveHandler(ReceiveMessageHandler handler, final boolean isWeakListener);
234
235 /**
236 * Remove a handler to receive new messages.
237 * Will stop receiving messages from server, if this was the last registered receiver.
238 */
239 void removeReceiveHandler(ReceiveMessageHandler handler);
240
241 boolean isReceiving();
242
243 /**
244 * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
245 */
246 void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException;
247
248 /**
249 * Receive new messages from server, returns only if the thread is interrupted.
250 */
251 void receiveMessages(ReceiveMessageHandler handler) throws IOException;
252
253 void setIgnoreAttachments(boolean ignoreAttachments);
254
255 boolean hasCaughtUpWithOldMessages();
256
257 boolean isContactBlocked(RecipientIdentifier.Single recipient);
258
259 void sendContacts() throws IOException;
260
261 List<Pair<RecipientAddress, Contact>> getContacts();
262
263 String getContactOrProfileName(RecipientIdentifier.Single recipient);
264
265 Group getGroup(GroupId groupId);
266
267 List<Identity> getIdentities();
268
269 List<Identity> getIdentities(RecipientIdentifier.Single recipient);
270
271 /**
272 * Trust this the identity with this fingerprint
273 *
274 * @param recipient account of the identity
275 * @param fingerprint Fingerprint
276 */
277 boolean trustIdentityVerified(
278 RecipientIdentifier.Single recipient, byte[] fingerprint
279 ) throws UnregisteredRecipientException;
280
281 /**
282 * Trust this the identity with this safety number
283 *
284 * @param recipient account of the identity
285 * @param safetyNumber Safety number
286 */
287 boolean trustIdentityVerifiedSafetyNumber(
288 RecipientIdentifier.Single recipient, String safetyNumber
289 ) throws UnregisteredRecipientException;
290
291 /**
292 * Trust this the identity with this scannable safety number
293 *
294 * @param recipient account of the identity
295 * @param safetyNumber Scannable safety number
296 */
297 boolean trustIdentityVerifiedSafetyNumber(
298 RecipientIdentifier.Single recipient, byte[] safetyNumber
299 ) throws UnregisteredRecipientException;
300
301 /**
302 * Trust all keys of this identity without verification
303 *
304 * @param recipient account of the identity
305 */
306 boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient) throws UnregisteredRecipientException;
307
308 void addClosedListener(Runnable listener);
309
310 @Override
311 void close() throws IOException;
312
313 interface ReceiveMessageHandler {
314
315 ReceiveMessageHandler EMPTY = (envelope, e) -> {
316 };
317
318 void handleMessage(MessageEnvelope envelope, Throwable e);
319 }
320 }