X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/82bb4f22f07bae40ff42570d0ada81125adf4392..fa5c09d23b830f2999a52421c189a3e4661da99f:/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java diff --git a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java index 9f6f1340..812110b3 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -2,6 +2,7 @@ package org.asamk.signal.dbus; import org.asamk.Signal; import org.asamk.signal.BaseConfig; +import org.asamk.signal.DbusReceiveMessageHandler; import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.NotMasterDeviceException; @@ -60,26 +61,40 @@ public class DbusSignalImpl implements Signal { private final Manager m; private final DBusConnection connection; private final String objectPath; + private final boolean noReceiveOnStart; private DBusPath thisDevice; private final List devices = new ArrayList<>(); private final List groups = new ArrayList<>(); + private DbusReceiveMessageHandler dbusMessageHandler; + private int subscriberCount; private final static Logger logger = LoggerFactory.getLogger(DbusSignalImpl.class); - public DbusSignalImpl(final Manager m, DBusConnection connection, final String objectPath) { + public DbusSignalImpl( + final Manager m, DBusConnection connection, final String objectPath, final boolean noReceiveOnStart + ) { this.m = m; this.connection = connection; this.objectPath = objectPath; + this.noReceiveOnStart = noReceiveOnStart; } public void initObjects() { + if (!noReceiveOnStart) { + subscribeReceive(); + } + updateDevices(); updateGroups(); updateConfiguration(); } public void close() { + if (dbusMessageHandler != null) { + m.removeReceiveHandler(dbusMessageHandler); + dbusMessageHandler = null; + } unExportDevices(); unExportGroups(); unExportConfiguration(); @@ -96,9 +111,25 @@ public class DbusSignalImpl implements Signal { } @Override - public void submitRateLimitChallenge(String challenge, String captchaString) { - final var captcha = captchaString == null ? null : captchaString.replace("signalcaptcha://", ""); + public void subscribeReceive() { + if (dbusMessageHandler == null) { + dbusMessageHandler = new DbusReceiveMessageHandler(m, connection, objectPath); + m.addReceiveHandler(dbusMessageHandler); + } + subscriberCount++; + } + + @Override + public void unsubscribeReceive() { + subscriberCount = Math.max(0, subscriberCount - 1); + if (subscriberCount == 0 && dbusMessageHandler != null) { + m.removeReceiveHandler(dbusMessageHandler); + dbusMessageHandler = null; + } + } + @Override + public void submitRateLimitChallenge(String challenge, String captcha) { try { m.submitRateLimitRecaptchaChallenge(challenge, captcha); } catch (IOException e) { @@ -107,6 +138,24 @@ public class DbusSignalImpl implements Signal { } + @Override + public void unregister() throws Error.Failure { + try { + m.unregister(); + } catch (IOException e) { + throw new Error.Failure("Failed to unregister: " + e.getMessage()); + } + } + + @Override + public void deleteAccount() throws Error.Failure { + try { + m.deleteAccount(); + } catch (IOException e) { + throw new Error.Failure("Failed to delete account: " + e.getMessage()); + } + } + @Override public void addDevice(String uri) { try { @@ -152,7 +201,7 @@ public class DbusSignalImpl implements Signal { @Override public long sendMessage(final String message, final List attachments, final List recipients) { try { - final var results = m.sendMessage(new Message(message, attachments), + final var results = m.sendMessage(new Message(message, attachments, List.of()), getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); @@ -318,7 +367,7 @@ public class DbusSignalImpl implements Signal { final String message, final List attachments ) throws Error.AttachmentInvalid, Error.Failure, Error.UntrustedIdentity { try { - final var results = m.sendMessage(new Message(message, attachments), + final var results = m.sendMessage(new Message(message, attachments, List.of()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE)); checkSendMessageResults(results.timestamp(), results.results()); return results.timestamp(); @@ -344,7 +393,7 @@ public class DbusSignalImpl implements Signal { @Override public long sendGroupMessage(final String message, final List attachments, final byte[] groupId) { try { - var results = m.sendMessage(new Message(message, attachments), + var results = m.sendMessage(new Message(message, attachments, List.of()), Set.of(new RecipientIdentifier.Group(getGroupId(groupId)))); checkSendMessageResults(results.timestamp(), results.results()); return results.timestamp();