]> nmode's Git Repositories - signal-cli/blobdiff - lib/src/main/java/org/asamk/signal/manager/Manager.java
Make send behavior more deterministic if there are unregistered recipients
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / Manager.java
index d2eb0f8f04270bdfed61099ff360ea3e1148e37c..61f6e1d6ca316c67d1547ef28dc69dd82f9fa0c0 100644 (file)
@@ -1,40 +1,33 @@
 package org.asamk.signal.manager;
 
+import org.asamk.signal.manager.api.Configuration;
 import org.asamk.signal.manager.api.Device;
+import org.asamk.signal.manager.api.Group;
+import org.asamk.signal.manager.api.Identity;
+import org.asamk.signal.manager.api.InactiveGroupLinkException;
+import org.asamk.signal.manager.api.InvalidDeviceLinkException;
 import org.asamk.signal.manager.api.Message;
+import org.asamk.signal.manager.api.MessageEnvelope;
+import org.asamk.signal.manager.api.Pair;
 import org.asamk.signal.manager.api.RecipientIdentifier;
 import org.asamk.signal.manager.api.SendGroupMessageResults;
 import org.asamk.signal.manager.api.SendMessageResults;
 import org.asamk.signal.manager.api.TypingAction;
+import org.asamk.signal.manager.api.UpdateGroup;
 import org.asamk.signal.manager.config.ServiceConfig;
 import org.asamk.signal.manager.config.ServiceEnvironment;
 import org.asamk.signal.manager.groups.GroupId;
 import org.asamk.signal.manager.groups.GroupInviteLinkUrl;
-import org.asamk.signal.manager.groups.GroupLinkState;
 import org.asamk.signal.manager.groups.GroupNotFoundException;
-import org.asamk.signal.manager.groups.GroupPermission;
 import org.asamk.signal.manager.groups.GroupSendingNotAllowedException;
 import org.asamk.signal.manager.groups.LastGroupAdminException;
 import org.asamk.signal.manager.groups.NotAGroupMemberException;
 import org.asamk.signal.manager.storage.SignalAccount;
-import org.asamk.signal.manager.storage.groups.GroupInfo;
-import org.asamk.signal.manager.storage.identities.IdentityInfo;
 import org.asamk.signal.manager.storage.identities.TrustNewIdentity;
 import org.asamk.signal.manager.storage.recipients.Contact;
 import org.asamk.signal.manager.storage.recipients.Profile;
-import org.asamk.signal.manager.storage.recipients.RecipientId;
-import org.whispersystems.libsignal.IdentityKey;
-import org.whispersystems.libsignal.InvalidKeyException;
-import org.whispersystems.libsignal.util.Pair;
-import org.whispersystems.libsignal.util.guava.Optional;
-import org.whispersystems.signalservice.api.groupsv2.GroupLinkNotActiveException;
-import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentRemoteId;
-import org.whispersystems.signalservice.api.messages.SignalServiceContent;
-import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
-import org.whispersystems.signalservice.api.push.SignalServiceAddress;
-import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
+import org.asamk.signal.manager.storage.recipients.RecipientAddress;
 import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
-import org.whispersystems.signalservice.internal.contacts.crypto.UnauthenticatedResponseException;
 
 import java.io.Closeable;
 import java.io.File;
@@ -43,6 +36,7 @@ import java.net.URI;
 import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
+import java.util.Optional;
 import java.util.Set;
 import java.util.UUID;
 import java.util.concurrent.TimeUnit;
@@ -51,7 +45,7 @@ import java.util.stream.Collectors;
 public interface Manager extends Closeable {
 
     static Manager init(
-            String username,
+            String number,
             File settingsPath,
             ServiceEnvironment serviceEnvironment,
             String userAgent,
@@ -59,11 +53,11 @@ public interface Manager extends Closeable {
     ) throws IOException, NotRegisteredException {
         var pathConfig = PathConfig.createDefault(settingsPath);
 
-        if (!SignalAccount.userExists(pathConfig.getDataPath(), username)) {
+        if (!SignalAccount.userExists(pathConfig.dataPath(), number)) {
             throw new NotRegisteredException();
         }
 
-        var account = SignalAccount.load(pathConfig.getDataPath(), username, true, trustNewIdentity);
+        var account = SignalAccount.load(pathConfig.dataPath(), number, true, trustNewIdentity);
 
         if (!account.isRegistered()) {
             throw new NotRegisteredException();
@@ -74,9 +68,17 @@ public interface Manager extends Closeable {
         return new ManagerImpl(account, pathConfig, serviceEnvironmentConfig, userAgent);
     }
 
-    static List<String> getAllLocalUsernames(File settingsPath) {
+    static void initLogger() {
+        LibSignalLogger.initLogger();
+    }
+
+    static boolean isValidNumber(final String e164Number, final String countryCode) {
+        return PhoneNumberFormatter.isValidNumber(e164Number, countryCode);
+    }
+
+    static List<String> getAllLocalAccountNumbers(File settingsPath) {
         var pathConfig = PathConfig.createDefault(settingsPath);
-        final var dataPath = pathConfig.getDataPath();
+        final var dataPath = pathConfig.dataPath();
         final var files = dataPath.listFiles();
 
         if (files == null) {
@@ -90,11 +92,7 @@ public interface Manager extends Closeable {
                 .collect(Collectors.toList());
     }
 
-    String getUsername();
-
-    RecipientId getSelfRecipientId();
-
-    int getDeviceId();
+    String getSelfNumber();
 
     void checkAccountState() throws IOException;
 
@@ -102,6 +100,10 @@ public interface Manager extends Closeable {
 
     void updateAccountAttributes(String deviceName) throws IOException;
 
+    Configuration getConfiguration();
+
+    void updateConfiguration(Configuration configuration) throws IOException, NotMasterDeviceException;
+
     void setProfile(
             String givenName, String familyName, String about, String aboutEmoji, Optional<File> avatar
     ) throws IOException;
@@ -114,15 +116,15 @@ public interface Manager extends Closeable {
 
     List<Device> getLinkedDevices() throws IOException;
 
-    void removeLinkedDevices(int deviceId) throws IOException;
+    void removeLinkedDevices(long deviceId) throws IOException;
 
-    void addDeviceLink(URI linkUri) throws IOException, InvalidKeyException;
+    void addDeviceLink(URI linkUri) throws IOException, InvalidDeviceLinkException;
 
-    void setRegistrationLockPin(Optional<String> pin) throws IOException, UnauthenticatedResponseException;
+    void setRegistrationLockPin(Optional<String> pin) throws IOException;
 
-    Profile getRecipientProfile(RecipientId recipientId);
+    Profile getRecipientProfile(RecipientIdentifier.Single recipient) throws IOException;
 
-    List<GroupInfo> getGroups();
+    List<Group> getGroups();
 
     SendGroupMessageResults quitGroup(
             GroupId groupId, Set<RecipientIdentifier.Single> groupAdmins
@@ -135,37 +137,24 @@ public interface Manager extends Closeable {
     ) throws IOException, AttachmentInvalidException;
 
     SendGroupMessageResults updateGroup(
-            GroupId groupId,
-            String name,
-            String description,
-            Set<RecipientIdentifier.Single> members,
-            Set<RecipientIdentifier.Single> removeMembers,
-            Set<RecipientIdentifier.Single> admins,
-            Set<RecipientIdentifier.Single> removeAdmins,
-            boolean resetGroupLink,
-            GroupLinkState groupLinkState,
-            GroupPermission addMemberPermission,
-            GroupPermission editDetailsPermission,
-            File avatarFile,
-            Integer expirationTimer,
-            Boolean isAnnouncementGroup
+            final GroupId groupId, final UpdateGroup updateGroup
     ) throws IOException, GroupNotFoundException, AttachmentInvalidException, NotAGroupMemberException, GroupSendingNotAllowedException;
 
     Pair<GroupId, SendGroupMessageResults> joinGroup(
             GroupInviteLinkUrl inviteLinkUrl
-    ) throws IOException, GroupLinkNotActiveException;
+    ) throws IOException, InactiveGroupLinkException;
 
-    void sendTypingMessage(
+    SendMessageResults sendTypingMessage(
             TypingAction action, Set<RecipientIdentifier> recipients
-    ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
+    ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException;
 
-    void sendReadReceipt(
+    SendMessageResults sendReadReceipt(
             RecipientIdentifier.Single sender, List<Long> messageIds
-    ) throws IOException, UntrustedIdentityException;
+    ) throws IOException;
 
-    void sendViewedReceipt(
+    SendMessageResults sendViewedReceipt(
             RecipientIdentifier.Single sender, List<Long> messageIds
-    ) throws IOException, UntrustedIdentityException;
+    ) throws IOException;
 
     SendMessageResults sendMessage(
             Message message, Set<RecipientIdentifier> recipients
@@ -187,7 +176,7 @@ public interface Manager extends Closeable {
 
     void setContactName(
             RecipientIdentifier.Single recipient, String name
-    ) throws NotMasterDeviceException, UnregisteredUserException;
+    ) throws NotMasterDeviceException, IOException;
 
     void setContactBlocked(
             RecipientIdentifier.Single recipient, boolean blocked
@@ -195,7 +184,7 @@ public interface Manager extends Closeable {
 
     void setGroupBlocked(
             GroupId groupId, boolean blocked
-    ) throws GroupNotFoundException, IOException;
+    ) throws GroupNotFoundException, IOException, NotMasterDeviceException;
 
     void setExpirationTimer(
             RecipientIdentifier.Single recipient, int messageExpirationTimer
@@ -205,31 +194,51 @@ public interface Manager extends Closeable {
 
     void requestAllSyncData() throws IOException;
 
-    void receiveMessages(
-            long timeout,
-            TimeUnit unit,
-            boolean returnOnTimeout,
-            boolean ignoreAttachments,
-            ReceiveMessageHandler handler
-    ) throws IOException;
+    /**
+     * Add a handler to receive new messages.
+     * Will start receiving messages from server, if not already started.
+     */
+    default void addReceiveHandler(ReceiveMessageHandler handler) {
+        addReceiveHandler(handler, false);
+    }
+
+    void addReceiveHandler(ReceiveMessageHandler handler, final boolean isWeakListener);
+
+    /**
+     * Remove a handler to receive new messages.
+     * Will stop receiving messages from server, if this was the last registered receiver.
+     */
+    void removeReceiveHandler(ReceiveMessageHandler handler);
+
+    boolean isReceiving();
+
+    /**
+     * Receive new messages from server, returns if no new message arrive in a timespan of timeout.
+     */
+    void receiveMessages(long timeout, TimeUnit unit, ReceiveMessageHandler handler) throws IOException;
+
+    /**
+     * Receive new messages from server, returns only if the thread is interrupted.
+     */
+    void receiveMessages(ReceiveMessageHandler handler) throws IOException;
+
+    void setIgnoreAttachments(boolean ignoreAttachments);
 
     boolean hasCaughtUpWithOldMessages();
 
     boolean isContactBlocked(RecipientIdentifier.Single recipient);
 
-    File getAttachmentFile(SignalServiceAttachmentRemoteId attachmentId);
-
     void sendContacts() throws IOException;
 
-    List<Pair<RecipientId, Contact>> getContacts();
+    List<Pair<RecipientAddress, Contact>> getContacts();
 
-    String getContactOrProfileName(RecipientIdentifier.Single recipientIdentifier);
+    String getContactOrProfileName(RecipientIdentifier.Single recipient);
 
-    GroupInfo getGroup(GroupId groupId);
+    Group getGroup(GroupId groupId);
 
-    List<IdentityInfo> getIdentities();
+    List<Identity> getIdentities();
 
-    List<IdentityInfo> getIdentities(RecipientIdentifier.Single recipient);
+    List<Identity> getIdentities(RecipientIdentifier.Single recipient);
 
     boolean trustIdentityVerified(RecipientIdentifier.Single recipient, byte[] fingerprint);
 
@@ -239,21 +248,16 @@ public interface Manager extends Closeable {
 
     boolean trustIdentityAllKeys(RecipientIdentifier.Single recipient);
 
-    String computeSafetyNumber(SignalServiceAddress theirAddress, IdentityKey theirIdentityKey);
-
-    byte[] computeSafetyNumberForScanning(SignalServiceAddress theirAddress, IdentityKey theirIdentityKey);
-
-    SignalServiceAddress resolveSignalServiceAddress(SignalServiceAddress address);
-
-    SignalServiceAddress resolveSignalServiceAddress(UUID uuid);
-
-    SignalServiceAddress resolveSignalServiceAddress(RecipientId recipientId);
+    void addClosedListener(Runnable listener);
 
     @Override
     void close() throws IOException;
 
     interface ReceiveMessageHandler {
 
-        void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, Throwable e);
+        ReceiveMessageHandler EMPTY = (envelope, e) -> {
+        };
+
+        void handleMessage(MessageEnvelope envelope, Throwable e);
     }
 }