mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Refactor ReceiveCommand in dbus mode and remove ExtendedDbusCommand
This commit is contained in:
parent
eec7aec069
commit
32818a8608
16 changed files with 301 additions and 261 deletions
|
@ -8,7 +8,6 @@ import net.sourceforge.argparse4j.inf.Namespace;
|
|||
import org.asamk.Signal;
|
||||
import org.asamk.signal.commands.Command;
|
||||
import org.asamk.signal.commands.Commands;
|
||||
import org.asamk.signal.commands.ExtendedDbusCommand;
|
||||
import org.asamk.signal.commands.LocalCommand;
|
||||
import org.asamk.signal.commands.MultiLocalCommand;
|
||||
import org.asamk.signal.commands.ProvisioningCommand;
|
||||
|
@ -151,20 +150,20 @@ public class App {
|
|||
? TrustNewIdentity.ON_FIRST_USE
|
||||
: trustNewIdentityCli == TrustNewIdentityCli.ALWAYS ? TrustNewIdentity.ALWAYS : TrustNewIdentity.NEVER;
|
||||
|
||||
if (command instanceof ProvisioningCommand) {
|
||||
if (command instanceof ProvisioningCommand provisioningCommand) {
|
||||
if (username != null) {
|
||||
throw new UserErrorException("You cannot specify a username (phone number) when linking");
|
||||
}
|
||||
|
||||
handleProvisioningCommand((ProvisioningCommand) command, dataPath, serviceEnvironment, outputWriter);
|
||||
handleProvisioningCommand(provisioningCommand, dataPath, serviceEnvironment, outputWriter);
|
||||
return;
|
||||
}
|
||||
|
||||
if (username == null) {
|
||||
var usernames = Manager.getAllLocalNumbers(dataPath);
|
||||
|
||||
if (command instanceof MultiLocalCommand) {
|
||||
handleMultiLocalCommand((MultiLocalCommand) command,
|
||||
if (command instanceof MultiLocalCommand multiLocalCommand) {
|
||||
handleMultiLocalCommand(multiLocalCommand,
|
||||
dataPath,
|
||||
serviceEnvironment,
|
||||
usernames,
|
||||
|
@ -185,8 +184,8 @@ public class App {
|
|||
throw new UserErrorException("Invalid username (phone number), make sure you include the country code.");
|
||||
}
|
||||
|
||||
if (command instanceof RegistrationCommand) {
|
||||
handleRegistrationCommand((RegistrationCommand) command, username, dataPath, serviceEnvironment);
|
||||
if (command instanceof RegistrationCommand registrationCommand) {
|
||||
handleRegistrationCommand(registrationCommand, username, dataPath, serviceEnvironment);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -344,11 +343,9 @@ public class App {
|
|||
private void handleCommand(
|
||||
Command command, Signal ts, DBusConnection dBusConn, OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
if (command instanceof ExtendedDbusCommand) {
|
||||
((ExtendedDbusCommand) command).handleCommand(ns, ts, dBusConn, outputWriter);
|
||||
} else if (command instanceof LocalCommand) {
|
||||
if (command instanceof LocalCommand localCommand) {
|
||||
try {
|
||||
((LocalCommand) command).handleCommand(ns, new DbusManagerImpl(ts, dBusConn), outputWriter);
|
||||
localCommand.handleCommand(ns, new DbusManagerImpl(ts, dBusConn), outputWriter);
|
||||
} catch (UnsupportedOperationException e) {
|
||||
throw new UserErrorException("Command is not yet implemented via dbus", e);
|
||||
} catch (DBusExecutionException e) {
|
||||
|
|
|
@ -113,8 +113,8 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
|
|||
var attachments = new ArrayList<String>();
|
||||
if (message.attachments().size() > 0) {
|
||||
for (var attachment : message.attachments()) {
|
||||
if (attachment.id().isPresent()) {
|
||||
attachments.add(m.getAttachmentFile(attachment.id().get()).getAbsolutePath());
|
||||
if (attachment.file().isPresent()) {
|
||||
attachments.add(attachment.file().get().getAbsolutePath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -161,7 +161,7 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
|
|||
"author",
|
||||
new Variant<>(quote.author().getLegacyIdentifier()),
|
||||
"text",
|
||||
new Variant<>(quote.text()));
|
||||
new Variant<>(quote.text().orElse("")));
|
||||
}
|
||||
|
||||
private Map<String, Variant<? extends Serializable>> getStickerMap(final MessageEnvelope.Data.Sticker sticker) {
|
||||
|
@ -184,9 +184,12 @@ public class DbusReceiveMessageHandler implements Manager.ReceiveMessageHandler
|
|||
) {
|
||||
final var map = new HashMap<String, Variant<?>>();
|
||||
if (a.id().isPresent()) {
|
||||
map.put("file", new Variant<>(m.getAttachmentFile(a.id().get()).getAbsolutePath()));
|
||||
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()));
|
||||
|
|
|
@ -543,8 +543,8 @@ public class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
|
|||
if (attachment.width().isPresent() || attachment.height().isPresent()) {
|
||||
writer.println("Dimensions: {}x{}", attachment.width().orElse(0), attachment.height().orElse(0));
|
||||
}
|
||||
if (attachment.id().isPresent()) {
|
||||
var file = m.getAttachmentFile(attachment.id().get());
|
||||
if (attachment.file().isPresent()) {
|
||||
var file = attachment.file().get();
|
||||
if (file.exists()) {
|
||||
writer.println("Stored plaintext in: {}", file);
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
package org.asamk.signal.commands;
|
||||
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.OutputWriter;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
||||
|
||||
public interface ExtendedDbusCommand extends CliCommand {
|
||||
|
||||
void handleCommand(
|
||||
Namespace ns, Signal signal, DBusConnection dbusconnection, final OutputWriter outputWriter
|
||||
) throws CommandException;
|
||||
}
|
|
@ -4,7 +4,6 @@ import net.sourceforge.argparse4j.impl.Arguments;
|
|||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.JsonReceiveMessageHandler;
|
||||
import org.asamk.signal.JsonWriter;
|
||||
import org.asamk.signal.OutputType;
|
||||
|
@ -13,24 +12,15 @@ import org.asamk.signal.PlainTextWriter;
|
|||
import org.asamk.signal.ReceiveMessageHandler;
|
||||
import org.asamk.signal.commands.exceptions.CommandException;
|
||||
import org.asamk.signal.commands.exceptions.IOErrorException;
|
||||
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;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Base64;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
|
||||
public class ReceiveCommand implements LocalCommand {
|
||||
|
||||
private final static Logger logger = LoggerFactory.getLogger(ReceiveCommand.class);
|
||||
|
||||
|
@ -56,106 +46,6 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
|
|||
return List.of(OutputType.PLAIN_TEXT, OutputType.JSON);
|
||||
}
|
||||
|
||||
public void handleCommand(
|
||||
final Namespace ns, final Signal signal, DBusConnection dbusconnection, final OutputWriter outputWriter
|
||||
) throws CommandException {
|
||||
try {
|
||||
if (outputWriter instanceof JsonWriter jsonWriter) {
|
||||
|
||||
dbusconnection.addSigHandler(Signal.MessageReceived.class, signal, messageReceived -> {
|
||||
var envelope = JsonMessageEnvelope.from(messageReceived);
|
||||
final var object = Map.of("envelope", envelope);
|
||||
jsonWriter.write(object);
|
||||
});
|
||||
|
||||
dbusconnection.addSigHandler(Signal.ReceiptReceived.class, signal, receiptReceived -> {
|
||||
var envelope = JsonMessageEnvelope.from(receiptReceived);
|
||||
final var object = Map.of("envelope", envelope);
|
||||
jsonWriter.write(object);
|
||||
});
|
||||
|
||||
dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, signal, syncReceived -> {
|
||||
var envelope = JsonMessageEnvelope.from(syncReceived);
|
||||
final var object = Map.of("envelope", envelope);
|
||||
jsonWriter.write(object);
|
||||
});
|
||||
} else {
|
||||
final var writer = (PlainTextWriter) outputWriter;
|
||||
|
||||
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());
|
||||
if (messageReceived.getGroupId().length > 0) {
|
||||
writer.println("Group info:");
|
||||
writer.indentedWriter()
|
||||
.println("Id: {}", Base64.getEncoder().encodeToString(messageReceived.getGroupId()));
|
||||
}
|
||||
final var extras = messageReceived.getExtras();
|
||||
printMessageExtras(writer, extras);
|
||||
writer.println();
|
||||
});
|
||||
|
||||
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.SyncMessageReceivedV2.class, signal, syncReceived -> {
|
||||
writer.println("Sync Envelope from: {} to: {}",
|
||||
syncReceived.getSource(),
|
||||
syncReceived.getDestination());
|
||||
writer.println("Timestamp: {}", DateUtils.formatTimestamp(syncReceived.getTimestamp()));
|
||||
writer.println("Body: {}", syncReceived.getMessage());
|
||||
if (syncReceived.getGroupId().length > 0) {
|
||||
writer.println("Group info:");
|
||||
writer.indentedWriter()
|
||||
.println("Id: {}", Base64.getEncoder().encodeToString(syncReceived.getGroupId()));
|
||||
}
|
||||
final var extras = syncReceived.getExtras();
|
||||
printMessageExtras(writer, extras);
|
||||
writer.println();
|
||||
});
|
||||
}
|
||||
} catch (DBusException e) {
|
||||
logger.error("Dbus client failed", e);
|
||||
throw new UnexpectedErrorException("Dbus client failed", e);
|
||||
}
|
||||
|
||||
double timeout = ns.getDouble("timeout");
|
||||
long timeoutMilliseconds = timeout < 0 ? 10000 : (long) (timeout * 1000);
|
||||
|
||||
while (true) {
|
||||
try {
|
||||
Thread.sleep(timeoutMilliseconds);
|
||||
} catch (InterruptedException ignored) {
|
||||
break;
|
||||
}
|
||||
if (timeout >= 0) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
|
|
@ -13,6 +13,7 @@ import org.asamk.signal.manager.api.Identity;
|
|||
import org.asamk.signal.manager.api.InactiveGroupLinkException;
|
||||
import org.asamk.signal.manager.api.InvalidDeviceLinkException;
|
||||
import org.asamk.signal.manager.api.Message;
|
||||
import org.asamk.signal.manager.api.MessageEnvelope;
|
||||
import org.asamk.signal.manager.api.Pair;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
import org.asamk.signal.manager.api.SendGroupMessageResults;
|
||||
|
@ -29,10 +30,13 @@ import org.asamk.signal.manager.groups.NotAGroupMemberException;
|
|||
import org.asamk.signal.manager.storage.recipients.Contact;
|
||||
import org.asamk.signal.manager.storage.recipients.Profile;
|
||||
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
||||
import org.freedesktop.dbus.DBusMap;
|
||||
import org.freedesktop.dbus.DBusPath;
|
||||
import org.freedesktop.dbus.connections.impl.DBusConnection;
|
||||
import org.freedesktop.dbus.exceptions.DBusException;
|
||||
import org.freedesktop.dbus.interfaces.DBusInterface;
|
||||
import org.freedesktop.dbus.interfaces.DBusSigHandler;
|
||||
import org.freedesktop.dbus.types.Variant;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
@ -40,6 +44,7 @@ import java.net.URI;
|
|||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
@ -59,6 +64,11 @@ public class DbusManagerImpl implements Manager {
|
|||
private final Signal signal;
|
||||
private final DBusConnection connection;
|
||||
|
||||
private final Set<ReceiveMessageHandler> messageHandlers = new HashSet<>();
|
||||
private DBusSigHandler<Signal.MessageReceivedV2> dbusMsgHandler;
|
||||
private DBusSigHandler<Signal.ReceiptReceivedV2> dbusRcptHandler;
|
||||
private DBusSigHandler<Signal.SyncMessageReceivedV2> dbusSyncHandler;
|
||||
|
||||
public DbusManagerImpl(final Signal signal, DBusConnection connection) {
|
||||
this.signal = signal;
|
||||
this.connection = connection;
|
||||
|
@ -133,7 +143,7 @@ public class DbusManagerImpl implements Manager {
|
|||
|
||||
@Override
|
||||
public void submitRateLimitRecaptchaChallenge(final String challenge, final String captcha) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
signal.submitRateLimitChallenge(challenge, captcha);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -308,7 +318,7 @@ public class DbusManagerImpl implements Manager {
|
|||
public void sendViewedReceipt(
|
||||
final RecipientIdentifier.Single sender, final List<Long> messageIds
|
||||
) throws IOException, UntrustedIdentityException {
|
||||
throw new UnsupportedOperationException();
|
||||
signal.sendViewedReceipt(sender.getIdentifier(), messageIds);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -414,39 +424,68 @@ public class DbusManagerImpl implements Manager {
|
|||
|
||||
@Override
|
||||
public void addReceiveHandler(final ReceiveMessageHandler handler) {
|
||||
throw new UnsupportedOperationException();
|
||||
synchronized (messageHandlers) {
|
||||
if (messageHandlers.size() == 0) {
|
||||
installMessageHandlers();
|
||||
}
|
||||
messageHandlers.add(handler);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeReceiveHandler(final ReceiveMessageHandler handler) {
|
||||
throw new UnsupportedOperationException();
|
||||
synchronized (messageHandlers) {
|
||||
messageHandlers.remove(handler);
|
||||
if (messageHandlers.size() == 0) {
|
||||
try {
|
||||
connection.removeSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
|
||||
connection.removeSigHandler(Signal.ReceiptReceivedV2.class, signal, this.dbusRcptHandler);
|
||||
connection.removeSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler);
|
||||
} catch (DBusException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isReceiving() {
|
||||
throw new UnsupportedOperationException();
|
||||
synchronized (messageHandlers) {
|
||||
return messageHandlers.size() > 0;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessages(final ReceiveMessageHandler handler) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
addReceiveHandler(handler);
|
||||
try {
|
||||
synchronized (this) {
|
||||
this.wait();
|
||||
}
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
removeReceiveHandler(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void receiveMessages(
|
||||
final long timeout, final TimeUnit unit, final ReceiveMessageHandler handler
|
||||
) throws IOException {
|
||||
throw new UnsupportedOperationException();
|
||||
addReceiveHandler(handler);
|
||||
try {
|
||||
Thread.sleep(unit.toMillis(timeout));
|
||||
} catch (InterruptedException ignored) {
|
||||
}
|
||||
removeReceiveHandler(handler);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setIgnoreAttachments(final boolean ignoreAttachments) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasCaughtUpWithOldMessages() {
|
||||
throw new UnsupportedOperationException();
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -454,11 +493,6 @@ public class DbusManagerImpl implements Manager {
|
|||
return signal.isContactBlocked(recipient.getIdentifier());
|
||||
}
|
||||
|
||||
@Override
|
||||
public File getAttachmentFile(final String attachmentId) {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendContacts() throws IOException {
|
||||
signal.sendContacts();
|
||||
|
@ -592,4 +626,168 @@ public class DbusManagerImpl implements Manager {
|
|||
throw new AssertionError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private void installMessageHandlers() {
|
||||
try {
|
||||
this.dbusMsgHandler = messageReceived -> {
|
||||
final var extras = messageReceived.getExtras();
|
||||
final var envelope = new MessageEnvelope(Optional.of(new RecipientAddress(null,
|
||||
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.of(messageReceived.getMessage()),
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
getAttachments(extras),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of(),
|
||||
List.of(),
|
||||
List.of())),
|
||||
Optional.empty(),
|
||||
Optional.empty());
|
||||
synchronized (messageHandlers) {
|
||||
for (final var messageHandler : messageHandlers) {
|
||||
messageHandler.handleMessage(envelope, null);
|
||||
}
|
||||
}
|
||||
};
|
||||
connection.addSigHandler(Signal.MessageReceivedV2.class, signal, this.dbusMsgHandler);
|
||||
|
||||
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(null,
|
||||
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());
|
||||
synchronized (messageHandlers) {
|
||||
for (final var messageHandler : messageHandlers) {
|
||||
messageHandler.handleMessage(envelope, null);
|
||||
}
|
||||
}
|
||||
};
|
||||
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(null,
|
||||
syncReceived.getSource())),
|
||||
0,
|
||||
syncReceived.getTimestamp(),
|
||||
0,
|
||||
0,
|
||||
false,
|
||||
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(null, syncReceived.getDestination())),
|
||||
Set.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.of(syncReceived.getMessage()),
|
||||
0,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
false,
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
getAttachments(extras),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
List.of(),
|
||||
List.of(),
|
||||
List.of()))),
|
||||
Optional.empty(),
|
||||
List.of(),
|
||||
List.of(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty(),
|
||||
Optional.empty())),
|
||||
Optional.empty());
|
||||
synchronized (messageHandlers) {
|
||||
for (final var messageHandler : messageHandlers) {
|
||||
messageHandler.handleMessage(envelope, null);
|
||||
}
|
||||
}
|
||||
};
|
||||
connection.addSigHandler(Signal.SyncMessageReceivedV2.class, signal, this.dbusSyncHandler);
|
||||
} catch (DBusException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
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"));
|
||||
}).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private <T> T getValue(
|
||||
final Map<String, Variant<?>> stringVariantMap, final String field
|
||||
) {
|
||||
return (T) stringVariantMap.get(field).getValue();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,14 +2,12 @@ package org.asamk.signal.json;
|
|||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.manager.UntrustedIdentityException;
|
||||
import org.asamk.signal.manager.api.InvalidNumberException;
|
||||
import org.asamk.signal.manager.api.MessageEnvelope;
|
||||
import org.asamk.signal.manager.api.RecipientIdentifier;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
public record JsonMessageEnvelope(
|
||||
|
@ -78,47 +76,4 @@ public record JsonMessageEnvelope(
|
|||
receiptMessage,
|
||||
typingMessage);
|
||||
}
|
||||
|
||||
public static JsonMessageEnvelope from(Signal.MessageReceived messageReceived) {
|
||||
return new JsonMessageEnvelope(messageReceived.getSource(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
messageReceived.getTimestamp(),
|
||||
JsonDataMessage.from(messageReceived),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
}
|
||||
|
||||
public static JsonMessageEnvelope from(Signal.ReceiptReceived receiptReceived) {
|
||||
return new JsonMessageEnvelope(receiptReceived.getSender(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
receiptReceived.getTimestamp(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
JsonReceiptMessage.deliveryReceipt(receiptReceived.getTimestamp(),
|
||||
List.of(receiptReceived.getTimestamp())),
|
||||
null);
|
||||
}
|
||||
|
||||
public static JsonMessageEnvelope from(Signal.SyncMessageReceived messageReceived) {
|
||||
return new JsonMessageEnvelope(messageReceived.getSource(),
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
null,
|
||||
messageReceived.getTimestamp(),
|
||||
null,
|
||||
JsonSyncMessage.from(messageReceived),
|
||||
null,
|
||||
null,
|
||||
null);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,8 +14,4 @@ record JsonReceiptMessage(long when, boolean isDelivery, boolean isRead, boolean
|
|||
final var timestamps = receiptMessage.timestamps();
|
||||
return new JsonReceiptMessage(when, isDelivery, isRead, isViewed, timestamps);
|
||||
}
|
||||
|
||||
static JsonReceiptMessage deliveryReceipt(final long when, final List<Long> timestamps) {
|
||||
return new JsonReceiptMessage(when, true, false, false, timestamps);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.asamk.signal.json;
|
|||
|
||||
import com.fasterxml.jackson.annotation.JsonUnwrapped;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.manager.api.MessageEnvelope;
|
||||
|
||||
import java.util.UUID;
|
||||
|
@ -26,11 +25,4 @@ record JsonSyncDataMessage(
|
|||
return new JsonSyncDataMessage(null, null, null, JsonDataMessage.from(transcriptMessage.message()));
|
||||
}
|
||||
}
|
||||
|
||||
static JsonSyncDataMessage from(Signal.SyncMessageReceived messageReceived) {
|
||||
return new JsonSyncDataMessage(messageReceived.getDestination(),
|
||||
null,
|
||||
null,
|
||||
JsonDataMessage.from(messageReceived));
|
||||
}
|
||||
}
|
||||
|
|
|
@ -2,7 +2,6 @@ package org.asamk.signal.json;
|
|||
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.asamk.signal.manager.api.MessageEnvelope;
|
||||
import org.asamk.signal.manager.groups.GroupId;
|
||||
import org.asamk.signal.manager.storage.recipients.RecipientAddress;
|
||||
|
@ -77,8 +76,4 @@ record JsonSyncMessage(
|
|||
}
|
||||
return new JsonSyncMessage(sentMessage, blockedNumbers, blockedGroupIds, readMessages, type);
|
||||
}
|
||||
|
||||
static JsonSyncMessage from(Signal.SyncMessageReceived messageReceived) {
|
||||
return new JsonSyncMessage(JsonSyncDataMessage.from(messageReceived), null, null, null, null);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue