import org.asamk.Signal;
import org.asamk.signal.manager.Manager;
-import org.asamk.signal.manager.groups.GroupUtils;
+import org.asamk.signal.manager.api.MessageEnvelope;
+import org.asamk.signal.manager.groups.GroupId;
+import org.asamk.signal.manager.storage.recipients.RecipientAddress;
import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException;
-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.freedesktop.dbus.types.Variant;
+import java.io.Serializable;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
-
-import static org.asamk.signal.util.Util.getLegacyIdentifier;
+import java.util.Map;
+import java.util.stream.Collectors;
public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler {
}
@Override
- public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
- 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();
+ public void handleMessage(MessageEnvelope envelope, Throwable exception) {
+ try {
+ sendDbusMessages(envelope);
+ } catch (DBusException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void sendDbusMessages(MessageEnvelope envelope) throws DBusException {
+ final var senderString = envelope.sourceAddress().map(RecipientAddress::getLegacyIdentifier).orElse("");
+ if (envelope.receipt().isPresent()) {
+ final var receiptMessage = envelope.receipt().get();
+ final var type = switch (receiptMessage.type()) {
+ case READ -> "read";
+ case VIEWED -> "viewed";
+ case DELIVERY -> "delivery";
+ case UNKNOWN -> "unknown";
+ };
+ for (long timestamp : receiptMessage.timestamps()) {
+ conn.sendMessage(new Signal.ReceiptReceived(objectPath, timestamp, senderString));
+ conn.sendMessage(new Signal.ReceiptReceivedV2(objectPath, timestamp, senderString, type, Map.of()));
}
- } else if (content != null) {
- final var sender = !envelope.isUnidentifiedSender() && envelope.hasSourceUuid()
- ? envelope.getSourceAddress()
- : content.getSender();
- 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();
- }
- }
- }
- } else if (content.getDataMessage().isPresent()) {
- var message = content.getDataMessage().get();
-
- var groupId = getGroupId(message);
- if (!message.isEndSession() && (
- groupId == null
- || 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();
- }
- }
- } else if (content.getSyncMessage().isPresent()) {
- var sync_message = content.getSyncMessage().get();
- if (sync_message.getSent().isPresent()) {
- var transcript = sync_message.getSent().get();
-
- if (transcript.getDestination().isPresent() || transcript.getMessage()
- .getGroupContext()
- .isPresent()) {
- 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();
- }
- }
+ }
+ if (envelope.data().isPresent()) {
+ var message = envelope.data().get();
+
+ var groupId = message.groupContext()
+ .map(MessageEnvelope.Data.GroupContext::groupId)
+ .map(GroupId::serialize)
+ .orElseGet(() -> new byte[0]);
+ var isGroupUpdate = message.groupContext()
+ .map(MessageEnvelope.Data.GroupContext::isGroupUpdate)
+ .orElse(false);
+ if (!message.isEndSession() && !isGroupUpdate) {
+ conn.sendMessage(new Signal.MessageReceived(objectPath,
+ message.timestamp(),
+ senderString,
+ groupId,
+ message.body().orElse(""),
+ getAttachments(message)));
+ conn.sendMessage(new Signal.MessageReceivedV2(objectPath,
+ message.timestamp(),
+ senderString,
+ groupId,
+ message.body().orElse(""),
+ getMessageExtras(message)));
+ }
+ }
+ if (envelope.sync().isPresent()) {
+ var syncMessage = envelope.sync().get();
+ if (syncMessage.sent().isPresent()) {
+ var transcript = syncMessage.sent().get();
+
+ if (transcript.destination().isPresent() || transcript.message().groupContext().isPresent()) {
+ var message = transcript.message();
+ var groupId = message.groupContext()
+ .map(MessageEnvelope.Data.GroupContext::groupId)
+ .map(GroupId::serialize)
+ .orElseGet(() -> new byte[0]);
+
+ conn.sendMessage(new Signal.SyncMessageReceived(objectPath,
+ transcript.message().timestamp(),
+ senderString,
+ transcript.destination().map(RecipientAddress::getLegacyIdentifier).orElse(""),
+ groupId,
+ message.body().orElse(""),
+ getAttachments(message)));
+ conn.sendMessage(new Signal.SyncMessageReceivedV2(objectPath,
+ transcript.message().timestamp(),
+ senderString,
+ transcript.destination().map(RecipientAddress::getLegacyIdentifier).orElse(""),
+ groupId,
+ message.body().orElse(""),
+ getMessageExtras(message)));
}
}
}
- }
- 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) {
+ private List<String> getAttachments(MessageEnvelope.Data message) {
var attachments = new ArrayList<String>();
- if (message.getAttachments().isPresent()) {
- for (var attachment : message.getAttachments().get()) {
- if (attachment.isPointer()) {
- attachments.add(m.getAttachmentFile(attachment.asPointer().getRemoteId()).getAbsolutePath());
+ if (message.attachments().size() > 0) {
+ for (var attachment : message.attachments()) {
+ if (attachment.file().isPresent()) {
+ attachments.add(attachment.file().get().getAbsolutePath());
}
}
}
return attachments;
}
+
+ private HashMap<String, Variant<?>> getMessageExtras(MessageEnvelope.Data message) {
+ var extras = new HashMap<String, Variant<?>>();
+ if (message.attachments().size() > 0) {
+ var attachments = message.attachments()
+ .stream()
+ .filter(a -> a.id().isPresent())
+ .map(a -> getAttachmentMap(m, a))
+ .collect(Collectors.toList());
+ extras.put("attachments", new Variant<>(attachments, "aa{sv}"));
+ }
+ if (message.mentions().size() > 0) {
+ var mentions = message.mentions().stream().map(this::getMentionMap).collect(Collectors.toList());
+ extras.put("mentions", new Variant<>(mentions, "aa{sv}"));
+ }
+ extras.put("expiresInSeconds", new Variant<>(message.expiresInSeconds()));
+ if (message.quote().isPresent()) {
+ extras.put("quote", new Variant<>(getQuoteMap(message.quote().get()), "a{sv}"));
+ }
+ if (message.reaction().isPresent()) {
+ final var reaction = message.reaction().get();
+ extras.put("reaction", new Variant<>(getReactionMap(reaction), "a{sv}"));
+ }
+ if (message.remoteDeleteId().isPresent()) {
+ extras.put("remoteDelete",
+ new Variant<>(Map.of("timestamp", new Variant<>(message.remoteDeleteId().get())), "a{sv}"));
+ }
+ if (message.sticker().isPresent()) {
+ final var sticker = message.sticker().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 MessageEnvelope.Data.Quote quote) {
+ return Map.of("id",
+ new Variant<>(quote.id()),
+ "author",
+ new Variant<>(quote.author().getLegacyIdentifier()),
+ "text",
+ new Variant<>(quote.text().orElse("")));
+ }
+
+ private Map<String, Variant<? extends Serializable>> getStickerMap(final MessageEnvelope.Data.Sticker sticker) {
+ return Map.of("packId", new Variant<>(sticker.packId()), "stickerId", new Variant<>(sticker.stickerId()));
+ }
+
+ private Map<String, Variant<?>> getReactionMap(final MessageEnvelope.Data.Reaction reaction) {
+ return Map.of("emoji",
+ new Variant<>(reaction.emoji()),
+ "targetAuthor",
+ new Variant<>(reaction.targetAuthor().getLegacyIdentifier()),
+ "targetSentTimestamp",
+ new Variant<>(reaction.targetSentTimestamp()),
+ "isRemove",
+ new Variant<>(reaction.isRemove()));
+ }
+
+ private Map<String, Variant<?>> getAttachmentMap(
+ final Manager m, final MessageEnvelope.Data.Attachment a
+ ) {
+ final var map = new HashMap<String, Variant<?>>();
+ if (a.id().isPresent()) {
+ map.put("remoteId", new Variant<>(a.id().get()));
+ }
+ if (a.file().isPresent()) {
+ map.put("file", new Variant<>(a.file().get().getAbsolutePath()));
+ }
+ map.put("contentType", new Variant<>(a.contentType()));
+ map.put("isVoiceNote", new Variant<>(a.isVoiceNote()));
+ map.put("isBorderless", new Variant<>(a.isBorderless()));
+ map.put("isGif", new Variant<>(a.isGif()));
+ if (a.caption().isPresent()) {
+ map.put("caption", new Variant<>(a.caption().get()));
+ }
+ if (a.fileName().isPresent()) {
+ map.put("fileName", new Variant<>(a.fileName().get()));
+ }
+ if (a.size().isPresent()) {
+ map.put("size", new Variant<>(a.size().get()));
+ }
+ if (a.width().isPresent()) {
+ map.put("width", new Variant<>(a.width().get()));
+ }
+ if (a.height().isPresent()) {
+ map.put("height", new Variant<>(a.height().get()));
+ }
+ return map;
+ }
+
+ private Map<String, Variant<?>> getMentionMap(
+ final MessageEnvelope.Data.Mention mention
+ ) {
+ return Map.of("recipient",
+ new Variant<>(mention.recipient().getLegacyIdentifier()),
+ "start",
+ new Variant<>(mention.start()),
+ "length",
+ new Variant<>(mention.length()));
+ }
}