mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 18:40:39 +00:00
Output json when receiving messages from dbus and --json parameter is given
Fixes #138
This commit is contained in:
parent
e04c45766d
commit
ae41d0c502
15 changed files with 176 additions and 50 deletions
|
@ -9,9 +9,9 @@ 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.messages.multidevice.SignalServiceSyncMessage;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
|
||||
import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
@ -40,12 +40,25 @@ public class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
|
|||
e.printStackTrace();
|
||||
}
|
||||
} else if (content != null) {
|
||||
if (content.getDataMessage().isPresent()) {
|
||||
if (content.getReceiptMessage().isPresent()) {
|
||||
final SignalServiceReceiptMessage receiptMessage = content.getReceiptMessage().get();
|
||||
if (receiptMessage.isDeliveryReceipt()) {
|
||||
final String sender = !envelope.isUnidentifiedSender() && envelope.hasSource() ? envelope.getSourceE164().get() : content.getSender().getNumber().get();
|
||||
for (long timestamp : receiptMessage.getTimestamps()) {
|
||||
try {
|
||||
conn.sendSignal(new Signal.ReceiptReceived(
|
||||
objectPath,
|
||||
timestamp,
|
||||
sender
|
||||
));
|
||||
} catch (DBusException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (content.getDataMessage().isPresent()) {
|
||||
SignalServiceDataMessage message = content.getDataMessage().get();
|
||||
|
||||
if (message.getBody().isPresent())
|
||||
System.out.println(message.getBody().get());
|
||||
|
||||
if (!message.isEndSession() &&
|
||||
!(message.getGroupContext().isPresent() &&
|
||||
message.getGroupContext().get().getGroupV1Type() != SignalServiceGroup.Type.DELIVER)) {
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
package org.asamk.signal;
|
||||
|
||||
class JsonError {
|
||||
|
||||
String message;
|
||||
|
||||
JsonError(Throwable exception) {
|
||||
this.message = exception.getMessage();
|
||||
}
|
||||
}
|
|
@ -8,6 +8,8 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import org.asamk.signal.json.JsonError;
|
||||
import org.asamk.signal.json.JsonMessageEnvelope;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
|
||||
|
|
|
@ -1,5 +1,11 @@
|
|||
package org.asamk.signal.commands;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.JsonGenerator;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
|
||||
import net.sourceforge.argparse4j.impl.Arguments;
|
||||
import net.sourceforge.argparse4j.inf.Namespace;
|
||||
import net.sourceforge.argparse4j.inf.Subparser;
|
||||
|
@ -7,6 +13,7 @@ import net.sourceforge.argparse4j.inf.Subparser;
|
|||
import org.asamk.Signal;
|
||||
import org.asamk.signal.JsonReceiveMessageHandler;
|
||||
import org.asamk.signal.ReceiveMessageHandler;
|
||||
import org.asamk.signal.json.JsonMessageEnvelope;
|
||||
import org.asamk.signal.manager.Manager;
|
||||
import org.asamk.signal.util.DateUtils;
|
||||
import org.freedesktop.dbus.DBusConnection;
|
||||
|
@ -34,9 +41,27 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
|
|||
}
|
||||
|
||||
public int handleCommand(final Namespace ns, final Signal signal, DBusConnection dbusconnection) {
|
||||
if (dbusconnection != null) {
|
||||
final ObjectMapper jsonProcessor;
|
||||
if (ns.getBoolean("json")) {
|
||||
jsonProcessor = new ObjectMapper();
|
||||
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect
|
||||
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET);
|
||||
} else {
|
||||
jsonProcessor = null;
|
||||
}
|
||||
try {
|
||||
dbusconnection.addSigHandler(Signal.MessageReceived.class, messageReceived -> {
|
||||
if (jsonProcessor != null) {
|
||||
JsonMessageEnvelope envelope = new JsonMessageEnvelope(messageReceived);
|
||||
ObjectNode result = jsonProcessor.createObjectNode();
|
||||
result.putPOJO("envelope", envelope);
|
||||
try {
|
||||
jsonProcessor.writeValue(System.out, result);
|
||||
System.out.println();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
System.out.print(String.format("Envelope from: %s\nTimestamp: %s\nBody: %s\n",
|
||||
messageReceived.getSender(), DateUtils.formatTimestamp(messageReceived.getTimestamp()), messageReceived.getMessage()));
|
||||
if (messageReceived.getGroupId().length > 0) {
|
||||
|
@ -50,11 +75,39 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
|
|||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
});
|
||||
|
||||
dbusconnection.addSigHandler(Signal.ReceiptReceived.class,
|
||||
receiptReceived -> System.out.print(String.format("Receipt from: %s\nTimestamp: %s\n",
|
||||
receiptReceived.getSender(), DateUtils.formatTimestamp(receiptReceived.getTimestamp()))));
|
||||
receiptReceived -> {
|
||||
if (jsonProcessor != null) {
|
||||
JsonMessageEnvelope envelope = new JsonMessageEnvelope(receiptReceived);
|
||||
ObjectNode result = jsonProcessor.createObjectNode();
|
||||
result.putPOJO("envelope", envelope);
|
||||
try {
|
||||
jsonProcessor.writeValue(System.out, result);
|
||||
System.out.println();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
System.out.print(String.format("Receipt from: %s\nTimestamp: %s\n",
|
||||
receiptReceived.getSender(), DateUtils.formatTimestamp(receiptReceived.getTimestamp())));
|
||||
}
|
||||
});
|
||||
|
||||
dbusconnection.addSigHandler(Signal.SyncMessageReceived.class, syncReceived -> {
|
||||
if (jsonProcessor != null) {
|
||||
JsonMessageEnvelope envelope = new JsonMessageEnvelope(syncReceived);
|
||||
ObjectNode result = jsonProcessor.createObjectNode();
|
||||
result.putPOJO("envelope", envelope);
|
||||
try {
|
||||
jsonProcessor.writeValue(System.out, result);
|
||||
System.out.println();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
System.out.print(String.format("Sync Envelope from: %s to: %s\nTimestamp: %s\nBody: %s\n",
|
||||
syncReceived.getSource(), syncReceived.getDestination(), DateUtils.formatTimestamp(syncReceived.getTimestamp()), syncReceived.getMessage()));
|
||||
if (syncReceived.getGroupId().length > 0) {
|
||||
|
@ -68,6 +121,7 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
|
|||
}
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
});
|
||||
} catch (UnsatisfiedLinkError e) {
|
||||
System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
|
||||
|
@ -84,8 +138,6 @@ public class ReceiveCommand implements ExtendedDbusCommand, LocalCommand {
|
|||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int handleCommand(final Namespace ns, final Manager m) {
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachmentPointer;
|
||||
|
@ -24,4 +24,8 @@ class JsonAttachment {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
JsonAttachment(String filename) {
|
||||
this.filename = filename;
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.whispersystems.signalservice.api.messages.calls.AnswerMessage;
|
||||
import org.whispersystems.signalservice.api.messages.calls.BusyMessage;
|
|
@ -1,11 +1,13 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceAttachment;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceDataMessage;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
class JsonDataMessage {
|
||||
|
||||
|
@ -34,4 +36,24 @@ class JsonDataMessage {
|
|||
this.attachments = new ArrayList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public JsonDataMessage(Signal.MessageReceived messageReceived) {
|
||||
timestamp = messageReceived.getTimestamp();
|
||||
message = messageReceived.getMessage();
|
||||
groupInfo = new JsonGroupInfo(messageReceived.getGroupId());
|
||||
attachments = messageReceived.getAttachments()
|
||||
.stream()
|
||||
.map(JsonAttachment::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
public JsonDataMessage(Signal.SyncMessageReceived messageReceived) {
|
||||
timestamp = messageReceived.getTimestamp();
|
||||
message = messageReceived.getMessage();
|
||||
groupInfo = new JsonGroupInfo(messageReceived.getGroupId());
|
||||
attachments = messageReceived.getAttachments()
|
||||
.stream()
|
||||
.map(JsonAttachment::new)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
10
src/main/java/org/asamk/signal/json/JsonError.java
Normal file
10
src/main/java/org/asamk/signal/json/JsonError.java
Normal file
|
@ -0,0 +1,10 @@
|
|||
package org.asamk.signal.json;
|
||||
|
||||
public class JsonError {
|
||||
|
||||
String message;
|
||||
|
||||
public JsonError(Throwable exception) {
|
||||
this.message = exception.getMessage();
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceGroup;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
@ -27,4 +27,8 @@ class JsonGroupInfo {
|
|||
}
|
||||
this.type = groupInfo.getType().toString();
|
||||
}
|
||||
|
||||
JsonGroupInfo(byte[] groupId) {
|
||||
this.groupId = Base64.encodeBytes(groupId);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,11 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceContent;
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
||||
class JsonMessageEnvelope {
|
||||
public class JsonMessageEnvelope {
|
||||
|
||||
String source;
|
||||
int sourceDevice;
|
||||
|
@ -44,4 +45,22 @@ class JsonMessageEnvelope {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
public JsonMessageEnvelope(Signal.MessageReceived messageReceived) {
|
||||
source = messageReceived.getSender();
|
||||
timestamp = messageReceived.getTimestamp();
|
||||
dataMessage = new JsonDataMessage(messageReceived);
|
||||
}
|
||||
|
||||
public JsonMessageEnvelope(Signal.ReceiptReceived receiptReceived) {
|
||||
source = receiptReceived.getSender();
|
||||
timestamp = receiptReceived.getTimestamp();
|
||||
isReceipt = true;
|
||||
}
|
||||
|
||||
public JsonMessageEnvelope(Signal.SyncMessageReceived messageReceived) {
|
||||
source = messageReceived.getSource();
|
||||
timestamp = messageReceived.getTimestamp();
|
||||
syncMessage = new JsonSyncMessage(messageReceived);
|
||||
}
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.whispersystems.signalservice.api.messages.SignalServiceReceiptMessage;
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage;
|
||||
|
||||
class JsonSyncDataMessage extends JsonDataMessage {
|
||||
|
@ -12,4 +13,9 @@ class JsonSyncDataMessage extends JsonDataMessage {
|
|||
this.destination = transcriptMessage.getDestination().get().getNumber().get();
|
||||
}
|
||||
}
|
||||
|
||||
JsonSyncDataMessage(Signal.SyncMessageReceived messageReceived) {
|
||||
super(messageReceived);
|
||||
destination = messageReceived.getDestination();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,6 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.json;
|
||||
|
||||
import org.asamk.Signal;
|
||||
import org.whispersystems.signalservice.api.messages.multidevice.ReadMessage;
|
||||
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
|
||||
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
|
||||
|
@ -42,4 +43,8 @@ class JsonSyncMessage {
|
|||
this.type = JsonSyncMessageType.REQUEST_SYNC;
|
||||
}
|
||||
}
|
||||
|
||||
JsonSyncMessage(Signal.SyncMessageReceived messageReceived) {
|
||||
sentMessage = new JsonSyncDataMessage(messageReceived);
|
||||
}
|
||||
}
|
|
@ -1,10 +1,10 @@
|
|||
package org.asamk.signal;
|
||||
package org.asamk.signal.manager;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonProperty;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class JsonStickerPack {
|
||||
class JsonStickerPack {
|
||||
|
||||
@JsonProperty
|
||||
public String title;
|
|
@ -21,7 +21,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
|
|||
import org.asamk.Signal;
|
||||
import org.asamk.signal.AttachmentInvalidException;
|
||||
import org.asamk.signal.GroupNotFoundException;
|
||||
import org.asamk.signal.JsonStickerPack;
|
||||
import org.asamk.signal.NotAGroupMemberException;
|
||||
import org.asamk.signal.StickerPackInvalidException;
|
||||
import org.asamk.signal.TrustLevel;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue