]> nmode's Git Repositories - signal-cli/blobdiff - src/main/java/org/asamk/signal/JsonDbusReceiveMessageHandler.java
Send self messages as normal messages, new flag --note-to-self for sync message
[signal-cli] / src / main / java / org / asamk / signal / JsonDbusReceiveMessageHandler.java
index 4e4c33cfaf30e339c9061069f13c5446febb4d1c..0cffd7b1dce73be67eb9b21ea655b70e846badc5 100644 (file)
@@ -2,6 +2,7 @@ package org.asamk.signal;
 
 import org.asamk.Signal;
 import org.asamk.signal.manager.Manager;
+import org.asamk.signal.manager.groups.GroupUtils;
 import org.freedesktop.dbus.connections.impl.DBusConnection;
 import org.freedesktop.dbus.exceptions.DBusException;
 import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
@@ -29,30 +30,33 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
         this.objectPath = objectPath;
     }
 
-    static void sendReceivedMessageToDbus(SignalServiceEnvelope envelope, SignalServiceContent content, DBusConnection conn, final String objectPath, Manager m) {
+    static void sendReceivedMessageToDbus(
+            SignalServiceEnvelope envelope,
+            SignalServiceContent content,
+            DBusConnection conn,
+            final String objectPath,
+            Manager m
+    ) {
         if (envelope.isReceipt()) {
             try {
-                conn.sendMessage(new Signal.ReceiptReceived(
-                        objectPath,
-                        envelope.getTimestamp(),
+                conn.sendMessage(new Signal.ReceiptReceived(objectPath, envelope.getTimestamp(),
                         // A receipt envelope always has a source address
-                        envelope.getSourceAddress().getLegacyIdentifier()
-                ));
+                        envelope.getSourceAddress().getLegacyIdentifier()));
             } catch (DBusException e) {
                 e.printStackTrace();
             }
         } else if (content != null) {
-            final SignalServiceAddress sender = !envelope.isUnidentifiedSender() && envelope.hasSource() ? envelope.getSourceAddress() : content.getSender();
+            final SignalServiceAddress sender = !envelope.isUnidentifiedSender() && envelope.hasSource()
+                    ? envelope.getSourceAddress()
+                    : content.getSender();
             if (content.getReceiptMessage().isPresent()) {
                 final SignalServiceReceiptMessage receiptMessage = content.getReceiptMessage().get();
                 if (receiptMessage.isDeliveryReceipt()) {
                     for (long timestamp : receiptMessage.getTimestamps()) {
                         try {
-                            conn.sendMessage(new Signal.ReceiptReceived(
-                                    objectPath,
+                            conn.sendMessage(new Signal.ReceiptReceived(objectPath,
                                     timestamp,
-                                    sender.getLegacyIdentifier()
-                            ));
+                                    sender.getLegacyIdentifier()));
                         } catch (DBusException e) {
                             e.printStackTrace();
                         }
@@ -61,14 +65,14 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
             } else if (content.getDataMessage().isPresent()) {
                 SignalServiceDataMessage message = content.getDataMessage().get();
 
-                byte[] groupId = getGroupId(m, message);
-                if (!message.isEndSession() &&
-                        (groupId == null
+                byte[] groupId = getGroupId(message);
+                if (!message.isEndSession() && (
+                        groupId == null
                                 || message.getGroupContext().get().getGroupV1Type() == null
-                                || message.getGroupContext().get().getGroupV1Type() == SignalServiceGroup.Type.DELIVER)) {
+                                || message.getGroupContext().get().getGroupV1Type() == SignalServiceGroup.Type.DELIVER
+                )) {
                     try {
-                        conn.sendMessage(new Signal.MessageReceived(
-                                objectPath,
+                        conn.sendMessage(new Signal.MessageReceived(objectPath,
                                 message.getTimestamp(),
                                 sender.getLegacyIdentifier(),
                                 groupId != null ? groupId : new byte[0],
@@ -83,16 +87,19 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
                 if (sync_message.getSent().isPresent()) {
                     SentTranscriptMessage transcript = sync_message.getSent().get();
 
-                    if (transcript.getDestination().isPresent() || transcript.getMessage().getGroupContext().isPresent()) {
+                    if (transcript.getDestination().isPresent() || transcript.getMessage()
+                            .getGroupContext()
+                            .isPresent()) {
                         SignalServiceDataMessage message = transcript.getMessage();
-                        byte[] groupId = getGroupId(m, message);
+                        byte[] groupId = getGroupId(message);
 
                         try {
-                            conn.sendMessage(new Signal.SyncMessageReceived(
-                                    objectPath,
+                            conn.sendMessage(new Signal.SyncMessageReceived(objectPath,
                                     transcript.getTimestamp(),
                                     sender.getLegacyIdentifier(),
-                                    transcript.getDestination().isPresent() ? transcript.getDestination().get().getLegacyIdentifier() : "",
+                                    transcript.getDestination().isPresent() ? transcript.getDestination()
+                                            .get()
+                                            .getLegacyIdentifier() : "",
                                     groupId != null ? groupId : new byte[0],
                                     message.getBody().isPresent() ? message.getBody().get() : "",
                                     JsonDbusReceiveMessageHandler.getAttachments(message, m)));
@@ -105,20 +112,9 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
         }
     }
 
-    private static byte[] getGroupId(final Manager m, final SignalServiceDataMessage message) {
-        byte[] groupId;
-        if (message.getGroupContext().isPresent()) {
-            if (message.getGroupContext().get().getGroupV1().isPresent()) {
-                groupId = message.getGroupContext().get().getGroupV1().get().getGroupId();
-            } else if (message.getGroupContext().get().getGroupV2().isPresent()) {
-                groupId = m.getGroupId(message.getGroupContext().get().getGroupV2().get().getMasterKey());
-            } else {
-                groupId = null;
-            }
-        } else {
-            groupId = null;
-        }
-        return groupId;
+    private static 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) {