]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/Manager.java
b4579274b738c4372a8d0f9ace7213bbf08eb718
[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.Device;
4 import org.asamk.signal.manager.api.Group;
5 import org.asamk.signal.manager.api.Identity;
6 import org.asamk.signal.manager.api.InactiveGroupLinkException;
7 import org.asamk.signal.manager.api.InvalidDeviceLinkException;
8 import org.asamk.signal.manager.api.Message;
9 import org.asamk.signal.manager.api.MessageEnvelope;
10 import org.asamk.signal.manager.api.Pair;
11 import org.asamk.signal.manager.api.RecipientIdentifier;
12 import org.asamk.signal.manager.api.SendGroupMessageResults;
13 import org.asamk.signal.manager.api.SendMessageResults;
14 import org.asamk.signal.manager.api.TypingAction;
15 import org.asamk.signal.manager.api.UpdateGroup;
16 import org.asamk.signal.manager.config.ServiceConfig;
17 import org.asamk.signal.manager.config.ServiceEnvironment;
18 import org.asamk.signal.manager.groups.GroupId;
19 import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
20 import org.asamk.signal.manager.groups.GroupNotFoundException;
21 import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
22 import org.asamk.signal.manager.groups.LastGroupAdminException;
23 import org.asamk.signal.manager.groups.NotAGroupMemberException;
24 import org.asamk.signal.manager.storage.SignalAccount;
25 import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
26 import org.asamk.signal.manager.storage.recipients.Contact;
27 import org.asamk.signal.manager.storage.recipients.Profile;
28 import org.asamk.signal.manager.storage.recipients.RecipientAddress;
29 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
30
31 import java.io.Closeable;
32 import java.io.File;
33 import java.io.IOException;
34 import java.net.URI;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.Optional;
39 import java.util.Set;
40 import java.util.UUID;
41 import java.util.concurrent.TimeUnit;
42 import java.util.stream.Collectors;
43
44 public interface Manager extends Closeable {
45
46 static Manager init(
47 String number,
48 File settingsPath,
49 ServiceEnvironment serviceEnvironment,
50 String userAgent,
51 TrustNewIdentity trustNewIdentity
52 ) throws IOException, NotRegisteredException {
53 var pathConfig = PathConfig.createDefault(settingsPath);
54
55 if (!SignalAccount.userExists(pathConfig.dataPath(), number)) {
56 throw new NotRegisteredException();
57 }
58
59 var account = SignalAccount.load(pathConfig.dataPath(), number, true, trustNewIdentity);
60
61 if (!account.isRegistered()) {
62 throw new NotRegisteredException();
63 }
64
65 final var serviceEnvironmentConfig = ServiceConfig.getServiceEnvironmentConfig(serviceEnvironment, userAgent);
66
67 return new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent);
68 }
69
70 static void initLogger() {
71 LibSignalLogger.initLogger();
72 }
73
74 static boolean isValidNumber(final String e164Number, final String countryCode) {
75 return PhoneNumberFormatter.isValidNumber(e164Number, countryCode);
76 }
77
78 static List<String> getAllLocalNumbers(File settingsPath) {
79 var pathConfig = PathConfig.createDefault(settingsPath);
80 final var dataPath = pathConfig.dataPath();
81 final var files = dataPath.listFiles();
82
83 if (files == null) {
84 return List.of();
85 }
86
87 return Arrays.stream(files)
88 .filter(File::isFile)
89 .map(File::getName)
90 .filter(file -> PhoneNumberFormatter.isValidNumber(file, null))
91 .collect(Collectors.toList());
92 }
93
94 String getSelfNumber();
95
96 void checkAccountState() throws IOException;
97
98 Map<String, Pair<String, UUID>> areUsersRegistered(Set<String> numbers) throws IOException;
99
100 void updateAccountAttributes(String deviceName) throws IOException;
101
102 void updateConfiguration(
103 final Boolean readReceipts,
104 final Boolean unidentifiedDeliveryIndicators,
105 final Boolean typingIndicators,
106 final Boolean linkPreviews
107 ) throws IOException, NotMasterDeviceException;
108
109 void setProfile(
110 String givenName, String familyName, String about, String aboutEmoji, Optional<File> avatar
111 ) throws IOException;
112
113 void unregister() throws IOException;
114
115 void deleteAccount() throws IOException;
116
117 void submitRateLimitRecaptchaChallenge(String challenge, String captcha) throws IOException;
118
119 List<Device> getLinkedDevices() throws IOException;
120
121 void removeLinkedDevices(long deviceId) throws IOException;
122
123 void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException;
124
125 void setRegistrationLockPin(Optional<String> pin) throws IOException;
126
127 Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException;
128
129 List<Group> getGroups();
130
131 SendGroupMessageResults quitGroup(
132 GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
133 ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException;
134
135 void deleteGroup(GroupId groupId) throws IOException;
136
137 Pair<GroupId, SendGroupMessageResults> createGroup(
138 String name, Set<RecipientIdentifier.Single> members, File avatarFile
139 ) throws IOException, AttachmentInvalidException;
140
141 SendGroupMessageResults updateGroup(
142 final GroupId groupId, final UpdateGroup updateGroup
143 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException;
144
145 Pair<GroupId, SendGroupMessageResults> joinGroup(
146 GroupInviteLinkUrl inviteLinkUrl
147 ) throws IOException, InactiveGroupLinkException;
148
149 void sendTypingMessage(
150 TypingAction action, Set<RecipientIdentifier> recipients
151 ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
152
153 void sendReadReceipt(
154 RecipientIdentifier.Single sender, List<Long> messageIds
155 ) throws IOException, UntrustedIdentityException;
156
157 void sendViewedReceipt(
158 RecipientIdentifier.Single sender, List<Long> messageIds
159 ) throws IOException, UntrustedIdentityException;
160
161 SendMessageResults sendMessage(
162 Message message, Set<RecipientIdentifier> recipients
163 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
164
165 SendMessageResults sendRemoteDeleteMessage(
166 long targetSentTimestamp, Set<RecipientIdentifier> recipients
167 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
168
169 SendMessageResults sendMessageReaction(
170 String emoji,
171 boolean remove,
172 RecipientIdentifier.Single targetAuthor,
173 long targetSentTimestamp,
174 Set<RecipientIdentifier> recipients
175 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
176
177 SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
178
179 void setContactName(
180 RecipientIdentifier.Single recipient, String name
181 ) throws NotMasterDeviceException, IOException;
182
183 void setContactBlocked(
184 RecipientIdentifier.Single recipient, boolean blocked
185 ) throws NotMasterDeviceException, IOException;
186
187 void setGroupBlocked(
188 GroupId groupId, boolean blocked
189 ) throws GroupNotFoundException, IOException, NotMasterDeviceException;
190
191 void setExpirationTimer(
192 RecipientIdentifier.Single recipient, int messageExpirationTimer
193 ) throws IOException;
194
195 URI uploadStickerPack(File path) throws IOException, StickerPackInvalidException;
196
197 void requestAllSyncData() throws IOException;
198
199 /**
200 * Add a handler to receive new messages.
201 * Will start receiving messages from server, if not already started.
202 */
203 void addReceiveHandler(ReceiveMessageHandler handler);
204
205 /**
206 * Remove a handler to receive new messages.
207 * Will stop receiving messages from server, if this was the last registered receiver.
208 */
209 void removeReceiveHandler(ReceiveMessageHandler handler);
210
211 boolean isReceiving();
212
213 /**
214 * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
215 */
216 void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException;
217
218 /**
219 * Receive new messages from server, returns only if the thread is interrupted.
220 */
221 void receiveMessages(ReceiveMessageHandler handler) throws IOException;
222
223 void setIgnoreAttachments(boolean ignoreAttachments);
224
225 boolean hasCaughtUpWithOldMessages();
226
227 boolean isContactBlocked(RecipientIdentifier.Single recipient);
228
229 File getAttachmentFile(String attachmentId);
230
231 void sendContacts() throws IOException;
232
233 List<Pair<RecipientAddress, Contact>> getContacts();
234
235 String getContactOrProfileName(RecipientIdentifier.Single recipient);
236
237 Group getGroup(GroupId groupId);
238
239 List<Identity> getIdentities();
240
241 List<Identity> getIdentities(RecipientIdentifier.Single recipient);
242
243 boolean trustIdentityVerified(RecipientIdentifier.Single recipient, byte[] fingerprint);
244
245 boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, String safetyNumber);
246
247 boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, byte[] safetyNumber);
248
249 boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient);
250
251 @Override
252 void close() throws IOException;
253
254 interface ReceiveMessageHandler {
255
256 void handleMessage(MessageEnvelope envelope, Throwable e);
257 }
258 }