X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/32818a8608f5bddc46ad5c7dc442f509c939791c..5cd5697aea9c2a032abc9ffc3a935e356ce65583:/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java diff --git a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java index 0d5ed98e..d2b1bec7 100644 --- a/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java +++ b/src/main/java/org/asamk/signal/dbus/DbusManagerImpl.java @@ -6,7 +6,7 @@ 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.Configuration; import org.asamk.signal.manager.api.Device; import org.asamk.signal.manager.api.Group; import org.asamk.signal.manager.api.Identity; @@ -54,6 +54,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.function.Supplier; import java.util.stream.Collectors; +import java.util.stream.Stream; /** * This class implements the Manager interface using the DBus Signal interface, where possible. @@ -64,7 +65,9 @@ public class DbusManagerImpl implements Manager { private final Signal signal; private final DBusConnection connection; + private final Set weakHandlers = new HashSet<>(); private final Set messageHandlers = new HashSet<>(); + private final List closedListeners = new ArrayList<>(); private DBusSigHandler dbusMsgHandler; private DBusSigHandler dbusRcptHandler; private DBusSigHandler dbusSyncHandler; @@ -106,12 +109,12 @@ public class DbusManagerImpl implements Manager { } @Override - public void updateConfiguration( - final Boolean readReceipts, - final Boolean unidentifiedDeliveryIndicators, - final Boolean typingIndicators, - final Boolean linkPreviews - ) throws IOException { + public Configuration getConfiguration() { + throw new UnsupportedOperationException(); + } + + @Override + public void updateConfiguration(Configuration configuration) throws IOException { throw new UnsupportedOperationException(); } @@ -294,31 +297,34 @@ public class DbusManagerImpl implements Manager { } @Override - public void sendTypingMessage( + public SendMessageResults sendTypingMessage( final TypingAction action, final Set recipients - ) throws IOException, UntrustedIdentityException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { - for (final var recipient : recipients) { - if (recipient instanceof RecipientIdentifier.Single) { - signal.sendTyping(((RecipientIdentifier.Single) recipient).getIdentifier(), - action == TypingAction.STOP); - } else if (recipient instanceof RecipientIdentifier.Group) { - throw new UnsupportedOperationException(); - } - } + ) throws IOException, NotAGroupMemberException, GroupNotFoundException, GroupSendingNotAllowedException { + return handleMessage(recipients, numbers -> { + numbers.forEach(n -> signal.sendTyping(n, action == TypingAction.STOP)); + return 0L; + }, () -> { + signal.sendTyping(signal.getSelfNumber(), action == TypingAction.STOP); + return 0L; + }, groupId -> { + throw new UnsupportedOperationException(); + }); } @Override - public void sendReadReceipt( + public SendMessageResults sendReadReceipt( final RecipientIdentifier.Single sender, final List messageIds - ) throws IOException, UntrustedIdentityException { + ) { signal.sendReadReceipt(sender.getIdentifier(), messageIds); + return new SendMessageResults(0, Map.of()); } @Override - public void sendViewedReceipt( + public SendMessageResults sendViewedReceipt( final RecipientIdentifier.Single sender, final List messageIds - ) throws IOException, UntrustedIdentityException { + ) { signal.sendViewedReceipt(sender.getIdentifier(), messageIds); + return new SendMessageResults(0, Map.of()); } @Override @@ -423,27 +429,26 @@ public class DbusManagerImpl implements Manager { } @Override - public void addReceiveHandler(final ReceiveMessageHandler handler) { + public void addReceiveHandler(final ReceiveMessageHandler handler, final boolean isWeakListener) { synchronized (messageHandlers) { - if (messageHandlers.size() == 0) { - installMessageHandlers(); + if (isWeakListener) { + weakHandlers.add(handler); + } else { + if (messageHandlers.size() == 0) { + installMessageHandlers(); + } + messageHandlers.add(handler); } - messageHandlers.add(handler); } } @Override public void removeReceiveHandler(final ReceiveMessageHandler handler) { synchronized (messageHandlers) { + weakHandlers.remove(handler); messageHandlers.remove(handler); if (messageHandlers.size() == 0) { - try { - connection.removeSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler); - connection.removeSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler); - connection.removeSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler); - } catch (DBusException e) { - e.printStackTrace(); - } + uninstallMessageHandlers(); } } } @@ -581,8 +586,29 @@ public class DbusManagerImpl implements Manager { throw new UnsupportedOperationException(); } + @Override + public void addClosedListener(final Runnable listener) { + synchronized (closedListeners) { + closedListeners.add(listener); + } + } + @Override public void close() throws IOException { + synchronized (this) { + this.notify(); + } + synchronized (messageHandlers) { + if (messageHandlers.size() > 0) { + uninstallMessageHandlers(); + } + weakHandlers.clear(); + messageHandlers.clear(); + } + synchronized (closedListeners) { + closedListeners.forEach(Runnable::run); + closedListeners.clear(); + } } private SendMessageResults handleMessage( @@ -654,6 +680,7 @@ public class DbusManagerImpl implements Manager { false, Optional.empty(), Optional.empty(), + Optional.empty(), getAttachments(extras), Optional.empty(), Optional.empty(), @@ -662,11 +689,7 @@ public class DbusManagerImpl implements Manager { List.of())), Optional.empty(), Optional.empty()); - synchronized (messageHandlers) { - for (final var messageHandler : messageHandlers) { - messageHandler.handleMessage(envelope, null); - } - } + notifyMessageHandlers(envelope); }; connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler); @@ -691,11 +714,7 @@ public class DbusManagerImpl implements Manager { Optional.empty(), Optional.empty(), Optional.empty()); - synchronized (messageHandlers) { - for (final var messageHandler : messageHandlers) { - messageHandler.handleMessage(envelope, null); - } - } + notifyMessageHandlers(envelope); }; connection.addSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler); @@ -731,6 +750,7 @@ public class DbusManagerImpl implements Manager { false, Optional.empty(), Optional.empty(), + Optional.empty(), getAttachments(extras), Optional.empty(), Optional.empty(), @@ -745,16 +765,32 @@ public class DbusManagerImpl implements Manager { Optional.empty(), Optional.empty())), Optional.empty()); - synchronized (messageHandlers) { - for (final var messageHandler : messageHandlers) { - messageHandler.handleMessage(envelope, null); - } - } + notifyMessageHandlers(envelope); }; connection.addSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler); } catch (DBusException e) { e.printStackTrace(); } + signal.subscribeReceive(); + } + + private void notifyMessageHandlers(final MessageEnvelope envelope) { + synchronized (messageHandlers) { + Stream.concat(messageHandlers.stream(), weakHandlers.stream()).forEach(h -> { + h.handleMessage(envelope, null); + }); + } + } + + private void uninstallMessageHandlers() { + try { + signal.unsubscribeReceive(); + connection.removeSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler); + connection.removeSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler); + connection.removeSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler); + } catch (DBusException e) { + e.printStackTrace(); + } } private List getAttachments(final Map> extras) {