+
+ private void installMessageHandlers() {
+ try {
+ this.dbusMsgHandler = messageReceived -> {
+ final var extras = messageReceived.getExtras();
+ final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
+ 0,
+ messageReceived.getTimestamp(),
+ 0,
+ 0,
+ false,
+ Optional.empty(),
+ Optional.empty(),
+ Optional.of(new MessageEnvelope.Data(messageReceived.getTimestamp(),
+ messageReceived.getGroupId().length > 0
+ ? Optional.of(new MessageEnvelope.Data.GroupContext(GroupId.unknownVersion(
+ messageReceived.getGroupId()), false, 0))
+ : Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.of(messageReceived.getMessage()),
+ 0,
+ false,
+ false,
+ false,
+ false,
+ false,
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ getAttachments(extras),
+ Optional.empty(),
+ Optional.empty(),
+ List.of(),
+ getMentions(extras),
+ List.of(),
+ List.of())),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty());
+ notifyMessageHandlers(envelope);
+ };
+ connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
+ this.dbusEditMsgHandler = messageReceived -> {
+ final var extras = messageReceived.getExtras();
+ final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(messageReceived.getSender())),
+ 0,
+ messageReceived.getTimestamp(),
+ 0,
+ 0,
+ false,
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.of(new MessageEnvelope.Edit(messageReceived.getTargetSentTimestamp(),
+ new MessageEnvelope.Data(messageReceived.getTimestamp(),
+ messageReceived.getGroupId().length > 0
+ ? Optional.of(new MessageEnvelope.Data.GroupContext(GroupId.unknownVersion(
+ messageReceived.getGroupId()), false, 0))
+ : Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.of(messageReceived.getMessage()),
+ 0,
+ false,
+ false,
+ false,
+ false,
+ false,
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ getAttachments(extras),
+ Optional.empty(),
+ Optional.empty(),
+ List.of(),
+ getMentions(extras),
+ List.of(),
+ List.of()))),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty());
+ notifyMessageHandlers(envelope);
+ };
+ connection.addSigHandler(Signal.EditMessageReceived.class, signal, this.dbusEditMsgHandler);
+
+ this.dbusRcptHandler = receiptReceived -> {
+ final var type = switch (receiptReceived.getReceiptType()) {
+ case "read" -> MessageEnvelope.Receipt.Type.READ;
+ case "viewed" -> MessageEnvelope.Receipt.Type.VIEWED;
+ case "delivery" -> MessageEnvelope.Receipt.Type.DELIVERY;
+ default -> MessageEnvelope.Receipt.Type.UNKNOWN;
+ };
+ final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(receiptReceived.getSender())),
+ 0,
+ receiptReceived.getTimestamp(),
+ 0,
+ 0,
+ false,
+ Optional.of(new MessageEnvelope.Receipt(receiptReceived.getTimestamp(),
+ type,
+ List.of(receiptReceived.getTimestamp()))),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty());
+ notifyMessageHandlers(envelope);
+ };
+ connection.addSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler);
+
+ this.dbusSyncHandler = syncReceived -> {
+ final var extras = syncReceived.getExtras();
+ final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(syncReceived.getSource())),
+ 0,
+ syncReceived.getTimestamp(),
+ 0,
+ 0,
+ false,
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.of(new MessageEnvelope.Sync(Optional.of(new MessageEnvelope.Sync.Sent(syncReceived.getTimestamp(),
+ syncReceived.getTimestamp(),
+ syncReceived.getDestination().isEmpty()
+ ? Optional.empty()
+ : Optional.of(new RecipientAddress(syncReceived.getDestination())),
+ Set.of(),
+ Optional.of(new MessageEnvelope.Data(syncReceived.getTimestamp(),
+ syncReceived.getGroupId().length > 0
+ ? Optional.of(new MessageEnvelope.Data.GroupContext(GroupId.unknownVersion(
+ syncReceived.getGroupId()), false, 0))
+ : Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.of(syncReceived.getMessage()),
+ 0,
+ false,
+ false,
+ false,
+ false,
+ false,
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ getAttachments(extras),
+ Optional.empty(),
+ Optional.empty(),
+ List.of(),
+ getMentions(extras),
+ List.of(),
+ List.of())),
+ Optional.empty(),
+ Optional.empty())),
+ Optional.empty(),
+ List.of(),
+ List.of(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty())),
+ Optional.empty(),
+ Optional.empty());
+ notifyMessageHandlers(envelope);
+ };
+ connection.addSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler);
+ } catch (DBusException e) {
+ throw new RuntimeException(e);
+ }
+ 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.EditMessageReceived.class, signal, this.dbusEditMsgHandler);
+ connection.removeSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler);
+ connection.removeSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler);
+ } catch (DBusException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private List<MessageEnvelope.Data.Attachment> getAttachments(final Map<String, Variant<?>> extras) {
+ if (!extras.containsKey("attachments")) {
+ return List.of();
+ }
+
+ final List<DBusMap<String, Variant<?>>> attachments = getValue(extras, "attachments");
+ return attachments.stream().map(a -> {
+ final String file = a.containsKey("file") ? getValue(a, "file") : null;
+ return new MessageEnvelope.Data.Attachment(a.containsKey("remoteId")
+ ? Optional.of(getValue(a, "remoteId"))
+ : Optional.empty(),
+ file != null ? Optional.of(new File(file)) : Optional.empty(),
+ Optional.empty(),
+ getValue(a, "contentType"),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ Optional.empty(),
+ getValue(a, "isVoiceNote"),
+ getValue(a, "isGif"),
+ getValue(a, "isBorderless"));
+ }).toList();
+ }
+
+ private List<MessageEnvelope.Data.Mention> getMentions(final Map<String, Variant<?>> extras) {
+ if (!extras.containsKey("mentions")) {
+ return List.of();
+ }
+
+ final List<DBusMap<String, Variant<?>>> mentions = getValue(extras, "mentions");
+ return mentions.stream()
+ .map(a -> new MessageEnvelope.Data.Mention(new RecipientAddress(this.<String>getValue(a, "recipient")),
+ getValue(a, "start"),
+ getValue(a, "length")))
+ .toList();
+ }
+
+ @Override
+ public InputStream retrieveAttachment(final String id) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public InputStream retrieveContactAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public InputStream retrieveProfileAvatar(final RecipientIdentifier.Single recipient) throws IOException, UnregisteredRecipientException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public InputStream retrieveGroupAvatar(final GroupId groupId) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @Override
+ public InputStream retrieveSticker(final StickerPackId stickerPackId, final int stickerId) throws IOException {
+ throw new UnsupportedOperationException();
+ }
+
+ @SuppressWarnings("unchecked")
+ private <T> T getValue(
+ final Map<String, Variant<?>> stringVariantMap, final String field
+ ) {
+ return (T) stringVariantMap.get(field).getValue();
+ }