X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/8a216e324571db898a83f7e127018b22a0318ac5..06e93b84da2718c31111e820cd35a3354f22bae2:/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 047b4f99..0c59429c 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusSignalImpl.java @@ -6,7 +6,6 @@ import org.asamk.signal.manager.AttachmentInvalidException; import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.NotMasterDeviceException; import org.asamk.signal.manager.StickerPackInvalidException; -import org.asamk.signal.manager.UntrustedIdentityException; import org.asamk.signal.manager.api.Identity; import org.asamk.signal.manager.api.InactiveGroupLinkException; import org.asamk.signal.manager.api.InvalidDeviceLinkException; @@ -27,11 +26,12 @@ import org.asamk.signal.manager.groups.LastGroupAdminException; import org.asamk.signal.manager.groups.NotAGroupMemberException; import org.asamk.signal.manager.storage.recipients.Profile; import org.asamk.signal.manager.storage.recipients.RecipientAddress; -import org.asamk.signal.util.ErrorUtils; +import org.asamk.signal.util.SendMessageResultUtils; import org.freedesktop.dbus.DBusPath; import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusExecutionException; +import org.freedesktop.dbus.interfaces.DBusInterface; import org.freedesktop.dbus.types.Variant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -59,26 +59,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(); @@ -95,9 +109,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(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) { @@ -106,6 +136,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 { @@ -151,7 +199,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(), Optional.empty()), getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); @@ -255,16 +303,15 @@ public class DbusSignalImpl implements Signal { try { var recipients = new ArrayList(1); recipients.add(recipient); - m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START, + final var results = m.sendTypingMessage(stop ? TypingAction.STOP : TypingAction.START, getSingleRecipientIdentifiers(recipients, m.getSelfNumber()).stream() .map(RecipientIdentifier.class::cast) .collect(Collectors.toSet())); + checkSendMessageResults(results.timestamp(), results.results()); } catch (IOException e) { throw new Error.Failure(e.getMessage()); } catch (GroupNotFoundException | NotAGroupMemberException | GroupSendingNotAllowedException e) { throw new Error.GroupNotFound(e.getMessage()); - } catch (UntrustedIdentityException e) { - throw new Error.UntrustedIdentity(e.getMessage()); } } @@ -273,11 +320,11 @@ public class DbusSignalImpl implements Signal { final String recipient, final List messageIds ) throws Error.Failure, Error.UntrustedIdentity { try { - m.sendReadReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), messageIds); + final var results = m.sendReadReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), + messageIds); + checkSendMessageResults(results.timestamp(), results.results()); } catch (IOException e) { throw new Error.Failure(e.getMessage()); - } catch (UntrustedIdentityException e) { - throw new Error.UntrustedIdentity(e.getMessage()); } } @@ -286,11 +333,11 @@ public class DbusSignalImpl implements Signal { final String recipient, final List messageIds ) throws Error.Failure, Error.UntrustedIdentity { try { - m.sendViewedReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), messageIds); + final var results = m.sendViewedReceipt(getSingleRecipientIdentifier(recipient, m.getSelfNumber()), + messageIds); + checkSendMessageResults(results.timestamp(), results.results()); } catch (IOException e) { throw new Error.Failure(e.getMessage()); - } catch (UntrustedIdentityException e) { - throw new Error.UntrustedIdentity(e.getMessage()); } } @@ -317,7 +364,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(), Optional.empty()), Set.of(RecipientIdentifier.NoteToSelf.INSTANCE)); checkSendMessageResults(results.timestamp(), results.results()); return results.timestamp(); @@ -340,10 +387,28 @@ public class DbusSignalImpl implements Signal { } } + @Override + public void deleteRecipient(final String recipient) throws Error.Failure { + try { + m.deleteRecipient(getSingleRecipientIdentifier(recipient, m.getSelfNumber())); + } catch (IOException e) { + throw new Error.Failure("Recipient not found"); + } + } + + @Override + public void deleteContact(final String recipient) throws Error.Failure { + try { + m.deleteContact(getSingleRecipientIdentifier(recipient, m.getSelfNumber())); + } catch (IOException e) { + throw new Error.Failure("Contact not found"); + } + } + @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(), Optional.empty()), Set.of(new RecipientIdentifier.Group(getGroupId(groupId)))); checkSendMessageResults(results.timestamp(), results.results()); return results.timestamp(); @@ -614,7 +679,7 @@ public class DbusSignalImpl implements Signal { public List listNumbers() { return Stream.concat(m.getIdentities().stream().map(Identity::recipient), m.getContacts().stream().map(Pair::first)) - .map(a -> a.getNumber().orElse(null)) + .map(a -> a.number().orElse(null)) .filter(Objects::nonNull) .distinct() .collect(Collectors.toList()); @@ -633,7 +698,7 @@ public class DbusSignalImpl implements Signal { // Try profiles if no contact name was found for (var identity : m.getIdentities()) { final var address = identity.recipient(); - var number = address.getNumber().orElse(null); + var number = address.number().orElse(null); if (number != null) { Profile profile = null; try { @@ -716,7 +781,7 @@ public class DbusSignalImpl implements Signal { } private static void checkSendMessageResult(long timestamp, SendMessageResult result) throws DBusExecutionException { - var error = ErrorUtils.getErrorMessageFromSendMessageResult(result); + var error = SendMessageResultUtils.getErrorMessageFromSendMessageResult(result); if (error == null) { return; @@ -740,7 +805,7 @@ public class DbusSignalImpl implements Signal { return; } - var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); + var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results); if (errors.size() == 0) { return; } @@ -763,7 +828,7 @@ public class DbusSignalImpl implements Signal { return; } - var errors = ErrorUtils.getErrorMessagesFromSendMessageResults(results); + var errors = SendMessageResultUtils.getErrorMessagesFromSendMessageResults(results); if (errors.size() == 0) { return; } @@ -839,12 +904,7 @@ public class DbusSignalImpl implements Signal { linkedDevices.forEach(d -> { final var object = new DbusSignalDeviceImpl(d); final var deviceObjectPath = object.getObjectPath(); - try { - connection.exportObject(object); - logger.debug("Exported dbus object: " + deviceObjectPath); - } catch (DBusException e) { - e.printStackTrace(); - } + exportObject(object); if (d.isThisDevice()) { thisDevice = new DBusPath(deviceObjectPath); } @@ -876,12 +936,7 @@ public class DbusSignalImpl implements Signal { groups.forEach(g -> { final var object = new DbusSignalGroupImpl(g.groupId()); - try { - connection.exportObject(object); - logger.debug("Exported dbus object: " + object.getObjectPath()); - } catch (DBusException e) { - e.printStackTrace(); - } + exportObject(object); this.groups.add(new StructGroup(new DBusPath(object.getObjectPath()), g.groupId().serialize(), emptyIfNull(g.title()))); @@ -898,14 +953,9 @@ public class DbusSignalImpl implements Signal { } private void updateConfiguration() { - try { - unExportConfiguration(); - final var object = new DbusSignalConfigurationImpl(); - connection.exportObject(object); - logger.debug("Exported dbus object: " + objectPath + "/Configuration"); - } catch (DBusException e) { - e.printStackTrace(); - } + unExportConfiguration(); + final var object = new DbusSignalConfigurationImpl(); + exportObject(object); } private void unExportConfiguration() { @@ -913,6 +963,15 @@ public class DbusSignalImpl implements Signal { connection.unExportObject(objectPath); } + private void exportObject(final DBusInterface object) { + try { + connection.exportObject(object); + logger.debug("Exported dbus object: " + object.getObjectPath()); + } catch (DBusException e) { + e.printStackTrace(); + } + } + public class DbusSignalDeviceImpl extends DbusProperties implements Signal.Device { private final org.asamk.signal.manager.api.Device device;