]> nmode's Git Repositories - signal-cli/commitdiff
Add new dbus message received signals with extendable extras map
authorAsamK <asamk@gmx.de>
Sun, 31 Oct 2021 11:40:21 +0000 (12:40 +0100)
committerAsamK <asamk@gmx.de>
Sun, 31 Oct 2021 11:40:21 +0000 (12:40 +0100)
src/main/java/org/asamk/Signal.java
src/main/java/org/asamk/signal/DbusReceiveMessageHandler.java
src/main/java/org/asamk/signal/commands/ReceiveCommand.java

index 18bdbdb563a604fd0fb41a5d5fe1e79a348b5ce1..67b4ebc94fb442746457e119189da41681f47c9a 100644 (file)
@@ -10,8 +10,10 @@ import org.freedesktop.dbus.exceptions.DBusExecutionException;
 import org.freedesktop.dbus.interfaces.DBusInterface;
 import org.freedesktop.dbus.interfaces.Properties;
 import org.freedesktop.dbus.messages.DBusSignal;
+import org.freedesktop.dbus.types.Variant;
 
 import java.util.List;
+import java.util.Map;
 
 /**
  * DBus interface for the org.asamk.Signal service.
@@ -166,6 +168,51 @@ public interface Signal extends DBusInterface {
 
     void submitRateLimitChallenge(String challenge, String captchaString) throws IOErrorException;
 
+    class MessageReceivedV2 extends DBusSignal {
+
+        private final long timestamp;
+        private final String sender;
+        private final byte[] groupId;
+        private final String message;
+        private final Map<String, Variant<?>> extras;
+
+        public MessageReceivedV2(
+                String objectpath,
+                long timestamp,
+                String sender,
+                byte[] groupId,
+                String message,
+                final Map<String, Variant<?>> extras
+        ) throws DBusException {
+            super(objectpath, timestamp, sender, groupId, message, extras);
+            this.timestamp = timestamp;
+            this.sender = sender;
+            this.groupId = groupId;
+            this.message = message;
+            this.extras = extras;
+        }
+
+        public long getTimestamp() {
+            return timestamp;
+        }
+
+        public String getSender() {
+            return sender;
+        }
+
+        public byte[] getGroupId() {
+            return groupId;
+        }
+
+        public String getMessage() {
+            return message;
+        }
+
+        public Map<String, Variant<?>> getExtras() {
+            return extras;
+        }
+    }
+
     class MessageReceived extends DBusSignal {
 
         private final long timestamp;
@@ -231,6 +278,44 @@ public interface Signal extends DBusInterface {
         }
     }
 
+    class ReceiptReceivedV2 extends DBusSignal {
+
+        private final long timestamp;
+        private final String sender;
+        private final String type;
+        private final Map<String, Variant<?>> extras;
+
+        public ReceiptReceivedV2(
+                String objectpath,
+                long timestamp,
+                String sender,
+                final String type,
+                final Map<String, Variant<?>> extras
+        ) throws DBusException {
+            super(objectpath, timestamp, sender, type, extras);
+            this.timestamp = timestamp;
+            this.sender = sender;
+            this.type = type;
+            this.extras = extras;
+        }
+
+        public long getTimestamp() {
+            return timestamp;
+        }
+
+        public String getSender() {
+            return sender;
+        }
+
+        public String getReceiptType() {
+            return type;
+        }
+
+        public Map<String, Variant<?>> getExtras() {
+            return extras;
+        }
+    }
+
     class SyncMessageReceived extends DBusSignal {
 
         private final long timestamp;
@@ -283,6 +368,58 @@ public interface Signal extends DBusInterface {
         }
     }
 
+    class SyncMessageReceivedV2 extends DBusSignal {
+
+        private final long timestamp;
+        private final String source;
+        private final String destination;
+        private final byte[] groupId;
+        private final String message;
+        private final Map<String, Variant<?>> extras;
+
+        public SyncMessageReceivedV2(
+                String objectpath,
+                long timestamp,
+                String source,
+                String destination,
+                byte[] groupId,
+                String message,
+                final Map<String, Variant<?>> extras
+        ) throws DBusException {
+            super(objectpath, timestamp, source, destination, groupId, message, extras);
+            this.timestamp = timestamp;
+            this.source = source;
+            this.destination = destination;
+            this.groupId = groupId;
+            this.message = message;
+            this.extras = extras;
+        }
+
+        public long getTimestamp() {
+            return timestamp;
+        }
+
+        public String getSource() {
+            return source;
+        }
+
+        public String getDestination() {
+            return destination;
+        }
+
+        public byte[] getGroupId() {
+            return groupId;
+        }
+
+        public String getMessage() {
+            return message;
+        }
+
+        public Map<String, Variant<?>> getExtras() {
+            return extras;
+        }
+    }
+
     class StructDevice extends Struct {
 
         @Position(0)
index ccf72538d1f810a0d7f972b48665fbbf57d0a1ca..95185052fddf99d9403194e310314ea0eb129921 100644 (file)
@@ -3,15 +3,23 @@ package org.asamk.signal;
 import org.asamk.Signal;
 import org.asamk.signal.manager.Manager;
 import org.asamk.signal.manager.groups.GroupUtils;
+import org.asamk.signal.util.Util;
 import org.freedesktop.dbus.connections.impl.DBusConnection;
 import org.freedesktop.dbus.exceptions.DBusException;
+import org.freedesktop.dbus.types.Variant;
+import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
 import org.whispersystems.signalservice.api.messages.SignalServiceContent;
 import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
 import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
 import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
+import org.whispersystems.signalservice.api.push.SignalServiceAddress;
 
+import java.io.Serializable;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
+import java.util.stream.Collectors;
 
 import static org.asamk.signal.util.Util.getLegacyIdentifier;
 
@@ -29,31 +37,45 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
 
     @Override
     public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
+        try {
+            sendDbusMessages(envelope, content);
+        } catch (DBusException e) {
+            e.printStackTrace();
+        }
+    }
+
+    private void sendDbusMessages(
+            final SignalServiceEnvelope envelope, final SignalServiceContent content
+    ) throws DBusException {
         if (envelope.isReceipt()) {
-            try {
-                conn.sendMessage(new Signal.ReceiptReceived(objectPath, envelope.getTimestamp(),
-                        // A receipt envelope always has a source address
-                        getLegacyIdentifier(envelope.getSourceAddress())));
-            } catch (DBusException e) {
-                e.printStackTrace();
-            }
+            conn.sendMessage(new Signal.ReceiptReceived(objectPath, envelope.getTimestamp(),
+                    // A receipt envelope always has a source address
+                    getLegacyIdentifier(envelope.getSourceAddress())));
+            conn.sendMessage(new Signal.ReceiptReceivedV2(objectPath, envelope.getTimestamp(),
+                    // A receipt envelope always has a source address
+                    getLegacyIdentifier(envelope.getSourceAddress()), "delivery", Map.of()));
         } else if (content != null) {
             final var sender = !envelope.isUnidentifiedSender() && envelope.hasSourceUuid()
                     ? envelope.getSourceAddress()
                     : content.getSender();
+            final var senderString = getLegacyIdentifier(sender);
             if (content.getReceiptMessage().isPresent()) {
                 final var receiptMessage = content.getReceiptMessage().get();
-                if (receiptMessage.isDeliveryReceipt()) {
-                    for (long timestamp : receiptMessage.getTimestamps()) {
-                        try {
-                            conn.sendMessage(new Signal.ReceiptReceived(objectPath,
-                                    timestamp,
-                                    getLegacyIdentifier(sender)));
-                        } catch (DBusException e) {
-                            e.printStackTrace();
-                        }
-                    }
+                final var type = switch (receiptMessage.getType()) {
+                    case READ -> "read";
+                    case VIEWED -> "viewed";
+                    case DELIVERY -> "delivery";
+                    case UNKNOWN -> "unknown";
+                };
+                for (long timestamp : receiptMessage.getTimestamps()) {
+                    conn.sendMessage(new Signal.ReceiptReceived(objectPath, timestamp, senderString));
+                    conn.sendMessage(new Signal.ReceiptReceivedV2(objectPath,
+                            envelope.getTimestamp(),
+                            senderString,
+                            type,
+                            Map.of()));
                 }
+
             } else if (content.getDataMessage().isPresent()) {
                 var message = content.getDataMessage().get();
 
@@ -63,16 +85,18 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
                                 || message.getGroupContext().get().getGroupV1Type() == null
                                 || message.getGroupContext().get().getGroupV1Type() == SignalServiceGroup.Type.DELIVER
                 )) {
-                    try {
-                        conn.sendMessage(new Signal.MessageReceived(objectPath,
-                                message.getTimestamp(),
-                                getLegacyIdentifier(sender),
-                                groupId != null ? groupId : new byte[0],
-                                message.getBody().isPresent() ? message.getBody().get() : "",
-                                getAttachments(message, m)));
-                    } catch (DBusException e) {
-                        e.printStackTrace();
-                    }
+                    conn.sendMessage(new Signal.MessageReceived(objectPath,
+                            message.getTimestamp(),
+                            senderString,
+                            groupId != null ? groupId : new byte[0],
+                            message.getBody().or(""),
+                            getAttachments(message)));
+                    conn.sendMessage(new Signal.MessageReceivedV2(objectPath,
+                            message.getTimestamp(),
+                            senderString,
+                            groupId != null ? groupId : new byte[0],
+                            message.getBody().or(""),
+                            getMessageExtras(message)));
                 }
             } else if (content.getSyncMessage().isPresent()) {
                 var sync_message = content.getSyncMessage().get();
@@ -85,31 +109,32 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
                         var message = transcript.getMessage();
                         var groupId = getGroupId(message);
 
-                        try {
-                            conn.sendMessage(new Signal.SyncMessageReceived(objectPath,
-                                    transcript.getTimestamp(),
-                                    getLegacyIdentifier(sender),
-                                    transcript.getDestination().isPresent()
-                                            ? getLegacyIdentifier(transcript.getDestination().get())
-                                            : "",
-                                    groupId != null ? groupId : new byte[0],
-                                    message.getBody().isPresent() ? message.getBody().get() : "",
-                                    getAttachments(message, m)));
-                        } catch (DBusException e) {
-                            e.printStackTrace();
-                        }
+                        conn.sendMessage(new Signal.SyncMessageReceived(objectPath,
+                                transcript.getTimestamp(),
+                                senderString,
+                                transcript.getDestination().transform(Util::getLegacyIdentifier).or(""),
+                                groupId != null ? groupId : new byte[0],
+                                message.getBody().or(""),
+                                getAttachments(message)));
+                        conn.sendMessage(new Signal.SyncMessageReceivedV2(objectPath,
+                                transcript.getTimestamp(),
+                                senderString,
+                                transcript.getDestination().transform(Util::getLegacyIdentifier).or(""),
+                                groupId != null ? groupId : new byte[0],
+                                message.getBody().or(""),
+                                getMessageExtras(message)));
                     }
                 }
             }
         }
     }
 
-    private static byte[] getGroupId(final SignalServiceDataMessage message) {
+    private byte[] getGroupId(final SignalServiceDataMessage message) {
         return message.getGroupContext().isPresent() ? GroupUtils.getGroupId(message.getGroupContext().get())
                 .serialize() : null;
     }
 
-    static private List<String> getAttachments(SignalServiceDataMessage message, Manager m) {
+    private List<String> getAttachments(SignalServiceDataMessage message) {
         var attachments = new ArrayList<String>();
         if (message.getAttachments().isPresent()) {
             for (var attachment : message.getAttachments().get()) {
@@ -120,4 +145,102 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
         }
         return attachments;
     }
+
+    private HashMap<String, Variant<?>> getMessageExtras(SignalServiceDataMessage message) {
+        var extras = new HashMap<String, Variant<?>>();
+        if (message.getAttachments().isPresent()) {
+            var attachments = message.getAttachments()
+                    .get()
+                    .stream()
+                    .filter(SignalServiceAttachment::isPointer)
+                    .map(a -> getAttachmentMap(m, a))
+                    .collect(Collectors.toList());
+            extras.put("attachments", new Variant<>(attachments, "aa{sv}"));
+        }
+        if (message.getMentions().isPresent()) {
+            var mentions = message.getMentions()
+                    .get()
+                    .stream()
+                    .map(mention -> getMentionMap(m, mention))
+                    .collect(Collectors.toList());
+            extras.put("mentions", new Variant<>(mentions, "aa{sv}"));
+        }
+        extras.put("expiresInSeconds", new Variant<>(message.getExpiresInSeconds()));
+        if (message.getQuote().isPresent()) {
+            extras.put("quote", new Variant<>(getQuoteMap(message.getQuote().get()), "a{sv}"));
+        }
+        if (message.getReaction().isPresent()) {
+            final var reaction = message.getReaction().get();
+            extras.put("reaction", new Variant<>(getReactionMap(reaction), "a{sv}"));
+        }
+        if (message.getRemoteDelete().isPresent()) {
+            extras.put("remoteDelete",
+                    new Variant<>(Map.of("timestamp", new Variant<>(message.getRemoteDelete())), "a{sv}"));
+        }
+        if (message.getSticker().isPresent()) {
+            final var sticker = message.getSticker().get();
+            extras.put("sticker", new Variant<>(getStickerMap(sticker), "a{sv}"));
+        }
+        extras.put("isViewOnce", new Variant<>(message.isViewOnce()));
+        return extras;
+    }
+
+    private Map<String, Variant<?>> getQuoteMap(final SignalServiceDataMessage.Quote quote) {
+        return Map.of("id",
+                new Variant<>(quote.getId()),
+                "author",
+                new Variant<>(getLegacyIdentifier(m.resolveSignalServiceAddress(quote.getAuthor()))),
+                "text",
+                new Variant<>(quote.getText()));
+    }
+
+    private Map<String, Variant<? extends Serializable>> getStickerMap(final SignalServiceDataMessage.Sticker sticker) {
+        return Map.of("packId", new Variant<>(sticker.getPackId()), "stickerId", new Variant<>(sticker.getStickerId()));
+    }
+
+    private Map<String, Variant<?>> getReactionMap(final SignalServiceDataMessage.Reaction reaction) {
+        return Map.of("emoji",
+                new Variant<>(reaction.getEmoji()),
+                "targetAuthor",
+                new Variant<>(getLegacyIdentifier(m.resolveSignalServiceAddress(reaction.getTargetAuthor()))),
+                "targetSentTimestamp",
+                new Variant<>(reaction.getTargetSentTimestamp()),
+                "isRemove",
+                new Variant<>(reaction.isRemove()));
+    }
+
+    private Map<String, Variant<?>> getAttachmentMap(final Manager m, final SignalServiceAttachment attachment) {
+        final var a = attachment.asPointer();
+        final var map = new HashMap<String, Variant<?>>();
+        map.put("file", new Variant<>(m.getAttachmentFile(a.getRemoteId()).getAbsolutePath()));
+        map.put("remoteId", new Variant<>(a.getRemoteId().toString()));
+        map.put("isVoiceNote", new Variant<>(a.getVoiceNote()));
+        map.put("isBorderless", new Variant<>(a.isBorderless()));
+        map.put("isGif", new Variant<>(a.isGif()));
+        if (a.getCaption().isPresent()) {
+            map.put("caption", new Variant<>(a.getCaption().get()));
+        }
+        if (a.getFileName().isPresent()) {
+            map.put("fileName", new Variant<>(a.getFileName().get()));
+        }
+        if (a.getSize().isPresent()) {
+            map.put("size", new Variant<>(a.getSize().get()));
+        }
+        if (a.getWidth() > 0 || a.getHeight() > 0) {
+            map.put("height", new Variant<>(a.getHeight()));
+            map.put("width", new Variant<>(a.getWidth()));
+        }
+        return map;
+    }
+
+    private Map<String, Variant<?>> getMentionMap(
+            final Manager m, final SignalServiceDataMessage.Mention mention
+    ) {
+        return Map.of("recipient",
+                new Variant<>(getLegacyIdentifier(m.resolveSignalServiceAddress(new SignalServiceAddress(mention.getUuid())))),
+                "start",
+                new Variant<>(mention.getStart()),
+                "length",
+                new Variant<>(mention.getLength()));
+    }
 }
index fe062ffc58b116bae801d6976f8b482e6537dd40..a69be7de90e60213b87680fd0a6003d7f52c2861 100644 (file)
@@ -17,8 +17,10 @@ import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
 import org.asamk.signal.json.JsonMessageEnvelope;
 import org.asamk.signal.manager.Manager;
 import org.asamk.signal.util.DateUtils;
+import org.freedesktop.dbus.DBusMap;
 import org.freedesktop.dbus.connections.impl.DBusConnection;
 import org.freedesktop.dbus.exceptions.DBusException;
+import org.freedesktop.dbus.types.Variant;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -80,7 +82,7 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
             } else {
                 final var writer = (PlainTextWriter) outputWriter;
 
-                dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> {
+                dbusconnection.addSigHandler(Signal.MessageReceivedV2.class, signal, messageReceived -> {
                     writer.println("Envelope from: {}", messageReceived.getSender());
                     writer.println("Timestamp: {}", DateUtils.formatTimestamp(messageReceived.getTimestamp()));
                     writer.println("Body: {}", messageReceived.getMessage());
@@ -89,21 +91,18 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
                         writer.indentedWriter()
                                 .println("Id: {}", Base64.getEncoder().encodeToString(messageReceived.getGroupId()));
                     }
-                    if (messageReceived.getAttachments().size() > 0) {
-                        writer.println("Attachments:");
-                        for (var attachment : messageReceived.getAttachments()) {
-                            writer.println("- Stored plaintext in: {}", attachment);
-                        }
-                    }
+                    final var extras = messageReceived.getExtras();
+                    printMessageExtras(writer, extras);
                     writer.println();
                 });
 
-                dbusconnection.addSigHandler(Signal.ReceiptReceived.class, signal, receiptReceived -> {
+                dbusconnection.addSigHandler(Signal.ReceiptReceivedV2.class, signal, receiptReceived -> {
                     writer.println("Receipt from: {}", receiptReceived.getSender());
                     writer.println("Timestamp: {}", DateUtils.formatTimestamp(receiptReceived.getTimestamp()));
+                    writer.println("Type: {}", receiptReceived.getReceiptType());
                 });
 
-                dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, signal, syncReceived -> {
+                dbusconnection.addSigHandler(Signal.SyncMessageReceivedV2.class, signal, syncReceived -> {
                     writer.println("Sync Envelope from: {} to: {}",
                             syncReceived.getSource(),
                             syncReceived.getDestination());
@@ -114,12 +113,8 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
                         writer.indentedWriter()
                                 .println("Id: {}", Base64.getEncoder().encodeToString(syncReceived.getGroupId()));
                     }
-                    if (syncReceived.getAttachments().size() > 0) {
-                        writer.println("Attachments:");
-                        for (var attachment : syncReceived.getAttachments()) {
-                            writer.println("- Stored plaintext in: {}", attachment);
-                        }
-                    }
+                    final var extras = syncReceived.getExtras();
+                    printMessageExtras(writer, extras);
                     writer.println();
                 });
             }
@@ -143,6 +138,24 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
         }
     }
 
+    private void printMessageExtras(final PlainTextWriter writer, final Map<String, Variant<?>> extras) {
+        if (extras.containsKey("attachments")) {
+            final List<DBusMap<String, Variant<?>>> attachments = getValue(extras, "attachments");
+            if (attachments.size() > 0) {
+                writer.println("Attachments:");
+                for (var attachment : attachments) {
+                    final String value = getValue(attachment, "file");
+                    writer.println("- Stored plaintext in: {}", value);
+                }
+            }
+        }
+    }
+
+    @SuppressWarnings("unchecked")
+    private <T> T getValue(final Map<String, Variant<?>> stringVariantMap, final String field) {
+        return (T) stringVariantMap.get(field).getValue();
+    }
+
     @Override
     public void handleCommand(
             final Namespace ns, final Manager m, final OutputWriter outputWriter