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