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