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