Duct tape solution to make daemon actually output JSON

This commit is contained in:
Riamse 2018-05-06 14:38:15 -04:00 committed by AsamK
parent afe18ea5ac
commit fbbd194e40

View file

@ -683,7 +683,7 @@ public class Main {
}
ignoreAttachments = ns.getBoolean("ignore_attachments");
try {
m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, new DbusReceiveMessageHandler(m, conn));
m.receiveMessages(1, TimeUnit.HOURS, false, ignoreAttachments, ns.getBoolean("json") ? new JsonDbusReceiveMessageHandler(m, conn) : new DbusReceiveMessageHandler(m, conn));
} catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage());
return 3;
@ -1296,6 +1296,59 @@ public class Main {
}
}
private static class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler {
final DBusConnection conn;
public JsonDbusReceiveMessageHandler(Manager m, DBusConnection conn) {
super(m);
this.conn = conn;
}
@Override
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
super.handleMessage(envelope, content, exception);
if (envelope.isReceipt()) {
try {
conn.sendSignal(new Signal.ReceiptReceived(
SIGNAL_OBJECTPATH,
envelope.getTimestamp(),
envelope.getSource()
));
} catch (DBusException e) {
e.printStackTrace();
}
} else if (content != null && content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get();
if (!message.isEndSession() &&
!(message.getGroupInfo().isPresent() &&
message.getGroupInfo().get().getType() != SignalServiceGroup.Type.DELIVER)) {
List<String> attachments = new ArrayList<>();
if (message.getAttachments().isPresent()) {
for (SignalServiceAttachment attachment : message.getAttachments().get()) {
if (attachment.isPointer()) {
attachments.add(m.getAttachmentFile(attachment.asPointer().getId()).getAbsolutePath());
}
}
}
try {
conn.sendSignal(new Signal.MessageReceived(
SIGNAL_OBJECTPATH,
message.getTimestamp(),
envelope.getSource(),
message.getGroupInfo().isPresent() ? message.getGroupInfo().get().getGroupId() : new byte[0],
message.getBody().isPresent() ? message.getBody().get() : "",
attachments));
} catch (DBusException e) {
e.printStackTrace();
}
}
}
}
}
private static String formatTimestamp(long timestamp) {
Date date = new Date(timestamp);
final DateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); // Quoted "Z" to indicate UTC, no timezone offset