Handle received sync messages

This commit is contained in:
AsamK 2016-04-07 22:09:36 +02:00
parent c2ae0c17c2
commit aa8a23aceb
2 changed files with 156 additions and 99 deletions

View file

@ -28,6 +28,8 @@ import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.messages.*;
import org.whispersystems.signalservice.api.messages.multidevice.ReadMessage;
import org.whispersystems.signalservice.api.messages.multidevice.SentTranscriptMessage;
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
@ -537,7 +539,51 @@ public class Main {
} else {
if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get();
handleSignalServiceDataMessage(message, group);
}
if (content.getSyncMessage().isPresent()) {
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
if (syncMessage.getContacts().isPresent()) {
System.out.println("Received sync contacts");
printAttachment(syncMessage.getContacts().get());
}
if (syncMessage.getGroups().isPresent()) {
System.out.println("Received sync groups");
printAttachment(syncMessage.getGroups().get());
}
if (syncMessage.getRead().isPresent()) {
System.out.println("Received sync read messages list");
for (ReadMessage rm : syncMessage.getRead().get()) {
System.out.println("From: " + rm.getSender() + " Message timestamp: " + rm.getTimestamp());
}
}
if (syncMessage.getRequest().isPresent()) {
System.out.println("Received sync request");
if (syncMessage.getRequest().get().isContactsRequest()) {
System.out.println(" - contacts request");
}
if (syncMessage.getRequest().get().isGroupsRequest()) {
System.out.println(" - groups request");
}
}
if (syncMessage.getSent().isPresent()) {
System.out.println("Received sync sent message");
final SentTranscriptMessage sentTranscriptMessage = syncMessage.getSent().get();
System.out.println("To: " + (sentTranscriptMessage.getDestination().isPresent() ? sentTranscriptMessage.getDestination().get() : "Unknown") + " , Message timestamp: " + sentTranscriptMessage.getTimestamp());
SignalServiceDataMessage message = sentTranscriptMessage.getMessage();
handleSignalServiceDataMessage(message, null);
}
}
}
} else {
System.out.println("Unknown message received.");
}
System.out.println();
}
// TODO remove group parameter
private void handleSignalServiceDataMessage(SignalServiceDataMessage message, GroupInfo group) {
System.out.println("Message timestamp: " + message.getTimestamp());
if (message.getBody().isPresent()) {
@ -576,16 +622,6 @@ public class Main {
}
}
}
if (content.getSyncMessage().isPresent()) {
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
System.out.println("Received sync message");
}
}
} else {
System.out.println("Unknown message received.");
}
System.out.println();
}
private void printAttachment(SignalServiceAttachment attachment) {
System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");

View file

@ -37,8 +37,10 @@ import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
import org.whispersystems.signalservice.api.SignalServiceMessageSender;
import org.whispersystems.signalservice.api.crypto.SignalServiceCipher;
import org.whispersystems.signalservice.api.crypto.*;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.messages.*;
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.TrustStore;
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
@ -453,24 +455,8 @@ class Manager implements Signal {
void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, GroupInfo group);
}
public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException {
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
SignalServiceMessagePipe messagePipe = null;
try {
messagePipe = messageReceiver.createMessagePipe();
while (true) {
SignalServiceEnvelope envelope;
SignalServiceContent content = null;
private GroupInfo handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, String destination) {
GroupInfo group = null;
try {
envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS);
if (!envelope.isReceipt()) {
content = decryptMessage(envelope);
if (content != null) {
if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get();
if (message.getGroupInfo().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupInfo().get();
switch (groupInfo.getType()) {
@ -513,14 +499,14 @@ class Manager implements Signal {
case QUIT:
try {
group = groupStore.getGroup(groupInfo.getGroupId());
group.members.remove(envelope.getSource());
group.members.remove(source);
} catch (GroupNotFoundException e) {
}
break;
}
}
if (message.isEndSession()) {
handleEndSession(envelope.getSource());
handleEndSession(isSync ? destination : source);
}
if (message.getAttachments().isPresent()) {
for (SignalServiceAttachment attachment : message.getAttachments().get()) {
@ -533,6 +519,41 @@ class Manager implements Signal {
}
}
}
return group;
}
public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException {
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
SignalServiceMessagePipe messagePipe = null;
try {
messagePipe = messageReceiver.createMessagePipe();
while (true) {
SignalServiceEnvelope envelope;
SignalServiceContent content = null;
GroupInfo group = null;
try {
envelope = messagePipe.read(timeoutSeconds, TimeUnit.SECONDS);
if (!envelope.isReceipt()) {
content = decryptMessage(envelope);
if (content != null) {
if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get();
group = handleSignalServiceDataMessage(message, false, envelope.getSource(), username);
}
if (content.getSyncMessage().isPresent()) {
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
if (syncMessage.getSent().isPresent()) {
SignalServiceDataMessage message = syncMessage.getSent().get().getMessage();
group = handleSignalServiceDataMessage(message, true, envelope.getSource(), syncMessage.getSent().get().getDestination().get());
}
if (syncMessage.getRequest().isPresent()) {
// TODO
}
if (syncMessage.getGroups().isPresent()) {
// TODO
}
}
}
}