]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/Manager.java
Refactor receive api
[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 List<String> getAllLocalNumbers(File settingsPath) {
71 var pathConfig = PathConfig.createDefault(settingsPath);
72 final var dataPath = pathConfig.dataPath();
73 final var files = dataPath.listFiles();
74
75 if (files == null) {
76 return List.of();
77 }
78
79 return Arrays.stream(files)
80 .filter(File::isFile)
81 .map(File::getName)
82 .filter(file -> PhoneNumberFormatter.isValidNumber(file, null))
83 .collect(Collectors.toList());
84 }
85
86 String getSelfNumber();
87
88 void checkAccountState() throws IOException;
89
90 Map<String, Pair<String, UUID>> areUsersRegistered(Set<String> numbers) throws IOException;
91
92 void updateAccountAttributes(String deviceName) throws IOException;
93
94 void updateConfiguration(
95 final Boolean readReceipts,
96 final Boolean unidentifiedDeliveryIndicators,
97 final Boolean typingIndicators,
98 final Boolean linkPreviews
99 ) throws IOException, NotMasterDeviceException;
100
101 void setProfile(
102 String givenName, String familyName, String about, String aboutEmoji, Optional<File> avatar
103 ) throws IOException;
104
105 void unregister() throws IOException;
106
107 void deleteAccount() throws IOException;
108
109 void submitRateLimitRecaptchaChallenge(String challenge, String captcha) throws IOException;
110
111 List<Device> getLinkedDevices() throws IOException;
112
113 void removeLinkedDevices(long deviceId) throws IOException;
114
115 void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException;
116
117 void setRegistrationLockPin(Optional<String> pin) throws IOException;
118
119 Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException;
120
121 List<Group> getGroups();
122
123 SendGroupMessageResults quitGroup(
124 GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
125 ) throws GroupNotFoundException, IOException, NotAGroupMemberException, LastGroupAdminException;
126
127 void deleteGroup(GroupId groupId) throws IOException;
128
129 Pair<GroupId, SendGroupMessageResults> createGroup(
130 String name, Set<RecipientIdentifier.Single> members, File avatarFile
131 ) throws IOException, AttachmentInvalidException;
132
133 SendGroupMessageResults updateGroup(
134 final GroupId groupId, final UpdateGroup updateGroup
135 ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException;
136
137 Pair<GroupId, SendGroupMessageResults> joinGroup(
138 GroupInviteLinkUrl inviteLinkUrl
139 ) throws IOException, InactiveGroupLinkException;
140
141 void sendTypingMessage(
142 TypingAction action, Set<RecipientIdentifier> recipients
143 ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
144
145 void sendReadReceipt(
146 RecipientIdentifier.Single sender, List<Long> messageIds
147 ) throws IOException, UntrustedIdentityException;
148
149 void sendViewedReceipt(
150 RecipientIdentifier.Single sender, List<Long> messageIds
151 ) throws IOException, UntrustedIdentityException;
152
153 SendMessageResults sendMessage(
154 Message message, Set<RecipientIdentifier> recipients
155 ) throws IOException, AttachmentInvalidException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
156
157 SendMessageResults sendRemoteDeleteMessage(
158 long targetSentTimestamp, Set<RecipientIdentifier> recipients
159 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
160
161 SendMessageResults sendMessageReaction(
162 String emoji,
163 boolean remove,
164 RecipientIdentifier.Single targetAuthor,
165 long targetSentTimestamp,
166 Set<RecipientIdentifier> recipients
167 ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
168
169 SendMessageResults sendEndSessionMessage(Set<RecipientIdentifier.Single> recipients) throws IOException;
170
171 void setContactName(
172 RecipientIdentifier.Single recipient, String name
173 ) throws NotMasterDeviceException, IOException;
174
175 void setContactBlocked(
176 RecipientIdentifier.Single recipient, boolean blocked
177 ) throws NotMasterDeviceException, IOException;
178
179 void setGroupBlocked(
180 GroupId groupId, boolean blocked
181 ) throws GroupNotFoundException, IOException, NotMasterDeviceException;
182
183 void setExpirationTimer(
184 RecipientIdentifier.Single recipient, int messageExpirationTimer
185 ) throws IOException;
186
187 URI uploadStickerPack(File path) throws IOException, StickerPackInvalidException;
188
189 void requestAllSyncData() throws IOException;
190
191 /**
192 * Add a handler to receive new messages.
193 * Will start receiving messages from server, if not already started.
194 */
195 void addReceiveHandler(ReceiveMessageHandler handler);
196
197 /**
198 * Remove a handler to receive new messages.
199 * Will stop receiving messages from server, if this was the last registered receiver.
200 */
201 void removeReceiveHandler(ReceiveMessageHandler handler);
202
203 boolean isReceiving();
204
205 /**
206 * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
207 */
208 void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException;
209
210 /**
211 * Receive new messages from server, returns only if the thread is interrupted.
212 */
213 void receiveMessages(ReceiveMessageHandler handler) throws IOException;
214
215 void setIgnoreAttachments(boolean ignoreAttachments);
216
217 boolean hasCaughtUpWithOldMessages();
218
219 boolean isContactBlocked(RecipientIdentifier.Single recipient);
220
221 File getAttachmentFile(String attachmentId);
222
223 void sendContacts() throws IOException;
224
225 List<Pair<RecipientAddress, Contact>> getContacts();
226
227 String getContactOrProfileName(RecipientIdentifier.Single recipient);
228
229 Group getGroup(GroupId groupId);
230
231 List<Identity> getIdentities();
232
233 List<Identity> getIdentities(RecipientIdentifier.Single recipient);
234
235 boolean trustIdentityVerified(RecipientIdentifier.Single recipient, byte[] fingerprint);
236
237 boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, String safetyNumber);
238
239 boolean trustIdentityVerifiedSafetyNumber(RecipientIdentifier.Single recipient, byte[] safetyNumber);
240
241 boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient);
242
243 @Override
244 void close() throws IOException;
245
246 interface ReceiveMessageHandler {
247
248 void handleMessage(MessageEnvelope envelope, Throwable e);
249 }
250 }