]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/Manager.java
8b373e38dac8371f8f381017b2b54f35ba1e98c4
[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.UpdateGroup;
17 import org.asamk.signal.manager.config.ServiceConfig;
18 import org.asamk.signal.manager.config.ServiceEnvironment;
19 import org.asamk.signal.manager.groups.GroupId;
20 import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
21 import org.asamk.signal.manager.groups.GroupNotFoundException;
22 import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
23 import org.asamk.signal.manager.groups.LastGroupAdminException;
24 import org.asamk.signal.manager.groups.NotAGroupMemberException;
25 import org.asamk.signal.manager.storage.SignalAccount;
26 import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
27 import org.asamk.signal.manager.storage.recipients.Contact;
28 import org.asamk.signal.manager.storage.recipients.Profile;
29 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
30 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
31
32 import java.io.Closeable;
33 import java.io.File;
34 import java.io.IOException;
35 import java.net.URI;
36 import java.time.Duration;
37 import java.util.Arrays;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.Optional;
41 import java.util.Set;
42 import java.util.UUID;
43 import java.util.concurrent.TimeUnit;
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 Map<String, Pair<String, UUID>> areUsersRegistered(Set<String> numbers) throws IOException;
101
102 void updateAccountAttributes(String deviceName) throws IOException;
103
104 Configuration getConfiguration();
105
106 void updateConfiguration(Configuration configuration) throws IOException, NotMasterDeviceException;
107
108 void setProfile(
109 String givenName, String familyName, String about, String aboutEmoji, Optional<File> avatar
110 ) throws IOException;
111
112 void unregister() throws IOException;
113
114 void deleteAccount() throws IOException;
115
116 void submitRateLimitRecaptchaChallenge(String challenge, String captcha) throws IOException;
117
118 List<Device> getLinkedDevices() throws IOException;
119
120 void removeLinkedDevices(long deviceId) throws IOException;
121
122 void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException;
123
124 void setRegistrationLockPin(Optional<String> pin) throws IOException;
125
126 Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException;
127
128 List<Group> getGroups();
129
130 SendGroupMessageResults quitGroup(
131 GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
132 ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException;
133
134 void deleteGroup(GroupId groupId) throws IOException;
135
136 Pair<GroupId, SendGroupMessageResults> createGroup(
137 String name, Set<RecipientIdentifier.Single> members, File avatarFile
138 ) throws IOException, AttachmentInvalidException;
139
140 SendGroupMessageResults updateGroup(
141 final GroupId groupId, final UpdateGroup updateGroup
142 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException;
143
144 Pair<GroupId, SendGroupMessageResults> joinGroup(
145 GroupInviteLinkUrl inviteLinkUrl
146 ) throws IOException, InactiveGroupLinkException;
147
148 SendMessageResults sendTypingMessage(
149 TypingAction action, Set<RecipientIdentifier> recipients
150 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
151
152 SendMessageResults sendReadReceipt(
153 RecipientIdentifier.Single sender, List<Long> messageIds
154 ) throws IOException;
155
156 SendMessageResults sendViewedReceipt(
157 RecipientIdentifier.Single sender, List<Long> messageIds
158 ) throws IOException;
159
160 SendMessageResults sendMessage(
161 Message message, Set<RecipientIdentifier> recipients
162 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
163
164 SendMessageResults sendRemoteDeleteMessage(
165 long targetSentTimestamp, Set<RecipientIdentifier> recipients
166 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
167
168 SendMessageResults sendMessageReaction(
169 String emoji,
170 boolean remove,
171 RecipientIdentifier.Single targetAuthor,
172 long targetSentTimestamp,
173 Set<RecipientIdentifier> recipients
174 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
175
176 SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
177
178 void deleteRecipient(RecipientIdentifier.Single recipient) throws IOException;
179
180 void deleteContact(RecipientIdentifier.Single recipient) throws IOException;
181
182 void setContactName(
183 RecipientIdentifier.Single recipient, String name
184 ) throws NotMasterDeviceException, IOException;
185
186 void setContactBlocked(
187 RecipientIdentifier.Single recipient, boolean blocked
188 ) throws NotMasterDeviceException, IOException;
189
190 void setGroupBlocked(
191 GroupId groupId, boolean blocked
192 ) throws GroupNotFoundException, IOException, NotMasterDeviceException;
193
194 void setExpirationTimer(
195 RecipientIdentifier.Single recipient, int messageExpirationTimer
196 ) throws IOException;
197
198 URI uploadStickerPack(File path) throws IOException, StickerPackInvalidException;
199
200 void requestAllSyncData() throws IOException;
201
202 /**
203 * Add a handler to receive new messages.
204 * Will start receiving messages from server, if not already started.
205 */
206 default void addReceiveHandler(ReceiveMessageHandler handler) {
207 addReceiveHandler(handler, false);
208 }
209
210 void addReceiveHandler(ReceiveMessageHandler handler, final boolean isWeakListener);
211
212 /**
213 * Remove a handler to receive new messages.
214 * Will stop receiving messages from server, if this was the last registered receiver.
215 */
216 void removeReceiveHandler(ReceiveMessageHandler handler);
217
218 boolean isReceiving();
219
220 /**
221 * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
222 */
223 void receiveMessages(Duration timeout, ReceiveMessageHandler handler) throws IOException;
224
225 /**
226 * Receive new messages from server, returns only if the thread is interrupted.
227 */
228 void receiveMessages(ReceiveMessageHandler handler) throws IOException;
229
230 void setIgnoreAttachments(boolean ignoreAttachments);
231
232 boolean hasCaughtUpWithOldMessages();
233
234 boolean isContactBlocked(RecipientIdentifier.Single recipient);
235
236 void sendContacts() throws IOException;
237
238 List<Pair<RecipientAddress, Contact>> getContacts();
239
240 String getContactOrProfileName(RecipientIdentifier.Single recipient);
241
242 Group getGroup(GroupId groupId);
243
244 List<Identity> getIdentities();
245
246 List<Identity> getIdentities(RecipientIdentifier.Single recipient);
247
248 boolean trustIdentityVerified(RecipientIdentifier.Single recipient, byte[] fingerprint);
249
250 boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, String safetyNumber);
251
252 boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, byte[] safetyNumber);
253
254 boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient);
255
256 void addClosedListener(Runnable listener);
257
258 @Override
259 void close() throws IOException;
260
261 interface ReceiveMessageHandler {
262
263 ReceiveMessageHandler EMPTY = (envelope, e) -> {
264 };
265
266 void handleMessage(MessageEnvelope envelope, Throwable e);
267 }
268 }