forked from AsamK/signal-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
520 additions
and
440 deletions.
There are no files selected for viewing
25 changes: 25 additions & 0 deletions
25
src/main/java/org/asamk/signal/DbusReceiveMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package org.asamk.signal; | ||
|
||
import org.asamk.signal.manager.Manager; | ||
import org.freedesktop.dbus.DBusConnection; | ||
import org.whispersystems.signalservice.api.messages.SignalServiceContent; | ||
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; | ||
|
||
class DbusReceiveMessageHandler extends ReceiveMessageHandler { | ||
|
||
private final DBusConnection conn; | ||
private final String objectPath; | ||
|
||
DbusReceiveMessageHandler(Manager m, DBusConnection conn, final String objectPath) { | ||
super(m); | ||
this.conn = conn; | ||
this.objectPath = objectPath; | ||
} | ||
|
||
@Override | ||
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) { | ||
super.handleMessage(envelope, content, exception); | ||
|
||
JsonDbusReceiveMessageHandler.sendReceivedMessageToDbus(envelope, content, conn, objectPath, m); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/org/asamk/signal/GroupIdFormatException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.asamk.signal; | ||
|
||
import java.io.IOException; | ||
|
||
public class GroupIdFormatException extends Exception { | ||
|
||
public GroupIdFormatException(String groupId, IOException e) { | ||
super("Failed to decode groupId (must be base64) \"" + groupId + "\": " + e.getMessage()); | ||
} | ||
} |
71 changes: 71 additions & 0 deletions
71
src/main/java/org/asamk/signal/JsonDbusReceiveMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package org.asamk.signal; | ||
|
||
import org.asamk.Signal; | ||
import org.asamk.signal.manager.Manager; | ||
import org.freedesktop.dbus.DBusConnection; | ||
import org.freedesktop.dbus.exceptions.DBusException; | ||
import org.whispersystems.signalservice.api.messages.*; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
class JsonDbusReceiveMessageHandler extends JsonReceiveMessageHandler { | ||
|
||
private final DBusConnection conn; | ||
|
||
private final String objectPath; | ||
|
||
JsonDbusReceiveMessageHandler(Manager m, DBusConnection conn, final String objectPath) { | ||
super(m); | ||
this.conn = conn; | ||
this.objectPath = objectPath; | ||
} | ||
|
||
static void sendReceivedMessageToDbus(SignalServiceEnvelope envelope, SignalServiceContent content, DBusConnection conn, final String objectPath, Manager m) { | ||
if (envelope.isReceipt()) { | ||
try { | ||
conn.sendSignal(new Signal.ReceiptReceived( | ||
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( | ||
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(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) { | ||
super.handleMessage(envelope, content, exception); | ||
|
||
sendReceivedMessageToDbus(envelope, content, conn, objectPath, m); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
src/main/java/org/asamk/signal/JsonReceiveMessageHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.asamk.signal; | ||
|
||
import com.fasterxml.jackson.annotation.JsonAutoDetect; | ||
import com.fasterxml.jackson.annotation.PropertyAccessor; | ||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.databind.DeserializationFeature; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.fasterxml.jackson.databind.SerializationFeature; | ||
import com.fasterxml.jackson.databind.node.ObjectNode; | ||
import org.asamk.signal.manager.Manager; | ||
import org.whispersystems.signalservice.api.messages.SignalServiceContent; | ||
import org.whispersystems.signalservice.api.messages.SignalServiceEnvelope; | ||
|
||
import java.io.IOException; | ||
|
||
class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler { | ||
|
||
final Manager m; | ||
private final ObjectMapper jsonProcessor; | ||
|
||
JsonReceiveMessageHandler(Manager m) { | ||
this.m = m; | ||
this.jsonProcessor = new ObjectMapper(); | ||
jsonProcessor.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); // disable autodetect | ||
jsonProcessor.enable(SerializationFeature.WRITE_NULL_MAP_VALUES); | ||
jsonProcessor.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES); | ||
jsonProcessor.disable(JsonGenerator.Feature.AUTO_CLOSE_TARGET); | ||
} | ||
|
||
@Override | ||
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) { | ||
ObjectNode result = jsonProcessor.createObjectNode(); | ||
if (exception != null) { | ||
result.putPOJO("error", new JsonError(exception)); | ||
} | ||
if (envelope != null) { | ||
result.putPOJO("envelope", new JsonMessageEnvelope(envelope, content)); | ||
} | ||
try { | ||
jsonProcessor.writeValue(System.out, result); | ||
System.out.println(); | ||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Oops, something went wrong.