Add new dbus message received signals with extendable extras map

This commit is contained in:
AsamK 2021-10-31 12:40:21 +01:00
parent 204aa31885
commit f39983f78a
3 changed files with 330 additions and 57 deletions

View file

@ -10,8 +10,10 @@ import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.freedesktop.dbus.interfaces.DBusInterface; import org.freedesktop.dbus.interfaces.DBusInterface;
import org.freedesktop.dbus.interfaces.Properties; import org.freedesktop.dbus.interfaces.Properties;
import org.freedesktop.dbus.messages.DBusSignal; import org.freedesktop.dbus.messages.DBusSignal;
import org.freedesktop.dbus.types.Variant;
import java.util.List; import java.util.List;
import java.util.Map;
/** /**
* DBus interface for the org.asamk.Signal service. * 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; 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 { class MessageReceived extends DBusSignal {
private final long timestamp; 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 { class SyncMessageReceived extends DBusSignal {
private final long timestamp; 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 { class StructDevice extends Struct {
@Position(0) @Position(0)

View file

@ -3,15 +3,23 @@ package org.asamk.signal;
import org.asamk.Signal; import org.asamk.Signal;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.manager.groups.GroupUtils; import org.asamk.signal.manager.groups.GroupUtils;
import org.asamk.signal.util.Util;
import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException; 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.SignalServiceContent;
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage; import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
import org.whispersystems.signalservice.api.messages.SignalServiceGroup; 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.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import static org.asamk.signal.util.Util.getLegacyIdentifier; import static org.asamk.signal.util.Util.getLegacyIdentifier;
@ -29,31 +37,45 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
@Override @Override
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) { 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()) { 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
// A receipt envelope always has a source address getLegacyIdentifier(envelope.getSourceAddress())));
getLegacyIdentifier(envelope.getSourceAddress()))); conn.sendMessage(new Signal.ReceiptReceivedV2(objectPath, envelope.getTimestamp(),
} catch (DBusException e) { // A receipt envelope always has a source address
e.printStackTrace(); getLegacyIdentifier(envelope.getSourceAddress()), "delivery", Map.of()));
}
} else if (content != null) { } else if (content != null) {
final var sender = !envelope.isUnidentifiedSender() && envelope.hasSourceUuid() final var sender = !envelope.isUnidentifiedSender() && envelope.hasSourceUuid()
? envelope.getSourceAddress() ? envelope.getSourceAddress()
: content.getSender(); : content.getSender();
final var senderString = getLegacyIdentifier(sender);
if (content.getReceiptMessage().isPresent()) { if (content.getReceiptMessage().isPresent()) {
final var receiptMessage = content.getReceiptMessage().get(); final var receiptMessage = content.getReceiptMessage().get();
if (receiptMessage.isDeliveryReceipt()) { final var type = switch (receiptMessage.getType()) {
for (long timestamp : receiptMessage.getTimestamps()) { case READ -> "read";
try { case VIEWED -> "viewed";
conn.sendMessage(new Signal.ReceiptReceived(objectPath, case DELIVERY -> "delivery";
timestamp, case UNKNOWN -> "unknown";
getLegacyIdentifier(sender))); };
} catch (DBusException e) { for (long timestamp : receiptMessage.getTimestamps()) {
e.printStackTrace(); 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()) { } else if (content.getDataMessage().isPresent()) {
var message = content.getDataMessage().get(); var message = content.getDataMessage().get();
@ -63,16 +85,18 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
|| message.getGroupContext().get().getGroupV1Type() == 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(),
message.getTimestamp(), senderString,
getLegacyIdentifier(sender), groupId != null ? groupId : new byte[0],
groupId != null ? groupId : new byte[0], message.getBody().or(""),
message.getBody().isPresent() ? message.getBody().get() : "", getAttachments(message)));
getAttachments(message, m))); conn.sendMessage(new Signal.MessageReceivedV2(objectPath,
} catch (DBusException e) { message.getTimestamp(),
e.printStackTrace(); senderString,
} groupId != null ? groupId : new byte[0],
message.getBody().or(""),
getMessageExtras(message)));
} }
} else if (content.getSyncMessage().isPresent()) { } else if (content.getSyncMessage().isPresent()) {
var sync_message = content.getSyncMessage().get(); var sync_message = content.getSyncMessage().get();
@ -85,31 +109,32 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
var message = transcript.getMessage(); var message = transcript.getMessage();
var groupId = getGroupId(message); var groupId = getGroupId(message);
try { conn.sendMessage(new Signal.SyncMessageReceived(objectPath,
conn.sendMessage(new Signal.SyncMessageReceived(objectPath, transcript.getTimestamp(),
transcript.getTimestamp(), senderString,
getLegacyIdentifier(sender), transcript.getDestination().transform(Util::getLegacyIdentifier).or(""),
transcript.getDestination().isPresent() groupId != null ? groupId : new byte[0],
? getLegacyIdentifier(transcript.getDestination().get()) message.getBody().or(""),
: "", getAttachments(message)));
groupId != null ? groupId : new byte[0], conn.sendMessage(new Signal.SyncMessageReceivedV2(objectPath,
message.getBody().isPresent() ? message.getBody().get() : "", transcript.getTimestamp(),
getAttachments(message, m))); senderString,
} catch (DBusException e) { transcript.getDestination().transform(Util::getLegacyIdentifier).or(""),
e.printStackTrace(); 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()) return message.getGroupContext().isPresent() ? GroupUtils.getGroupId(message.getGroupContext().get())
.serialize() : null; .serialize() : null;
} }
static private List<String> getAttachments(SignalServiceDataMessage message, Manager m) { private List<String> getAttachments(SignalServiceDataMessage message) {
var attachments = new ArrayList<String>(); var attachments = new ArrayList<String>();
if (message.getAttachments().isPresent()) { if (message.getAttachments().isPresent()) {
for (var attachment : message.getAttachments().get()) { for (var attachment : message.getAttachments().get()) {
@ -120,4 +145,102 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
} }
return attachments; 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()));
}
} }

View file

@ -17,8 +17,10 @@ import org.asamk.signal.commands.exceptions.UnexpectedErrorException;
import org.asamk.signal.json.JsonMessageEnvelope; import org.asamk.signal.json.JsonMessageEnvelope;
import org.asamk.signal.manager.Manager; import org.asamk.signal.manager.Manager;
import org.asamk.signal.util.DateUtils; import org.asamk.signal.util.DateUtils;
import org.freedesktop.dbus.DBusMap;
import org.freedesktop.dbus.connections.impl.DBusConnection; import org.freedesktop.dbus.connections.impl.DBusConnection;
import org.freedesktop.dbus.exceptions.DBusException; import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.types.Variant;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@ -80,7 +82,7 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
} else { } else {
final var writer = (PlainTextWriter) outputWriter; 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("Envelope from: {}", messageReceived.getSender());
writer.println("Timestamp: {}", DateUtils.formatTimestamp(messageReceived.getTimestamp())); writer.println("Timestamp: {}", DateUtils.formatTimestamp(messageReceived.getTimestamp()));
writer.println("Body: {}", messageReceived.getMessage()); writer.println("Body: {}", messageReceived.getMessage());
@ -89,21 +91,18 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
writer.indentedWriter() writer.indentedWriter()
.println("Id: {}", Base64.getEncoder().encodeToString(messageReceived.getGroupId())); .println("Id: {}", Base64.getEncoder().encodeToString(messageReceived.getGroupId()));
} }
if (messageReceived.getAttachments().size() > 0) { final var extras = messageReceived.getExtras();
writer.println("Attachments:"); printMessageExtras(writer, extras);
for (var attachment : messageReceived.getAttachments()) {
writer.println("- Stored plaintext in: {}", attachment);
}
}
writer.println(); writer.println();
}); });
dbusconnection.addSigHandler(Signal.ReceiptReceived.class, signal, receiptReceived -> { dbusconnection.addSigHandler(Signal.ReceiptReceivedV2.class, signal, receiptReceived -> {
writer.println("Receipt from: {}", receiptReceived.getSender()); writer.println("Receipt from: {}", receiptReceived.getSender());
writer.println("Timestamp: {}", DateUtils.formatTimestamp(receiptReceived.getTimestamp())); 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: {}", writer.println("Sync Envelope from: {} to: {}",
syncReceived.getSource(), syncReceived.getSource(),
syncReceived.getDestination()); syncReceived.getDestination());
@ -114,12 +113,8 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
writer.indentedWriter() writer.indentedWriter()
.println("Id: {}", Base64.getEncoder().encodeToString(syncReceived.getGroupId())); .println("Id: {}", Base64.getEncoder().encodeToString(syncReceived.getGroupId()));
} }
if (syncReceived.getAttachments().size() > 0) { final var extras = syncReceived.getExtras();
writer.println("Attachments:"); printMessageExtras(writer, extras);
for (var attachment : syncReceived.getAttachments()) {
writer.println("- Stored plaintext in: {}", attachment);
}
}
writer.println(); 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 @Override
public void handleCommand( public void handleCommand(
final Namespace ns, final Manager m, final OutputWriter outputWriter final Namespace ns, final Manager m, final OutputWriter outputWriter