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.freedesktop.dbus.exceptions.DBusExecutionException;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException; import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.messages.*; 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.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions; import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
@ -537,48 +539,41 @@ public class Main {
} else { } else {
if (content.getDataMessage().isPresent()) { if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get(); SignalServiceDataMessage message = content.getDataMessage().get();
handleSignalServiceDataMessage(message, group);
System.out.println("Message timestamp: " + message.getTimestamp());
if (message.getBody().isPresent()) {
System.out.println("Body: " + message.getBody().get());
}
if (message.getGroupInfo().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupInfo().get();
System.out.println("Group info:");
System.out.println(" Id: " + Base64.encodeBytes(groupInfo.getGroupId()));
if (groupInfo.getName().isPresent()) {
System.out.println(" Name: " + groupInfo.getName().get());
} else if (group != null) {
System.out.println(" Name: " + group.name);
} else {
System.out.println(" Name: <Unknown group>");
}
System.out.println(" Type: " + groupInfo.getType());
if (groupInfo.getMembers().isPresent()) {
for (String member : groupInfo.getMembers().get()) {
System.out.println(" Member: " + member);
}
}
if (groupInfo.getAvatar().isPresent()) {
System.out.println(" Avatar:");
printAttachment(groupInfo.getAvatar().get());
}
}
if (message.isEndSession()) {
System.out.println("Is end session");
}
if (message.getAttachments().isPresent()) {
System.out.println("Attachments: ");
for (SignalServiceAttachment attachment : message.getAttachments().get()) {
printAttachment(attachment);
}
}
} }
if (content.getSyncMessage().isPresent()) { if (content.getSyncMessage().isPresent()) {
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get(); SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
System.out.println("Received sync message");
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 { } else {
@ -587,6 +582,47 @@ public class Main {
System.out.println(); 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()) {
System.out.println("Body: " + message.getBody().get());
}
if (message.getGroupInfo().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupInfo().get();
System.out.println("Group info:");
System.out.println(" Id: " + Base64.encodeBytes(groupInfo.getGroupId()));
if (groupInfo.getName().isPresent()) {
System.out.println(" Name: " + groupInfo.getName().get());
} else if (group != null) {
System.out.println(" Name: " + group.name);
} else {
System.out.println(" Name: <Unknown group>");
}
System.out.println(" Type: " + groupInfo.getType());
if (groupInfo.getMembers().isPresent()) {
for (String member : groupInfo.getMembers().get()) {
System.out.println(" Member: " + member);
}
}
if (groupInfo.getAvatar().isPresent()) {
System.out.println(" Avatar:");
printAttachment(groupInfo.getAvatar().get());
}
}
if (message.isEndSession()) {
System.out.println("Is end session");
}
if (message.getAttachments().isPresent()) {
System.out.println("Attachments: ");
for (SignalServiceAttachment attachment : message.getAttachments().get()) {
printAttachment(attachment);
}
}
}
private void printAttachment(SignalServiceAttachment attachment) { private void printAttachment(SignalServiceAttachment attachment) {
System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")"); System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");
if (attachment.isPointer()) { if (attachment.isPointer()) {

View file

@ -37,8 +37,10 @@ import org.whispersystems.signalservice.api.SignalServiceAccountManager;
import org.whispersystems.signalservice.api.SignalServiceMessagePipe; import org.whispersystems.signalservice.api.SignalServiceMessagePipe;
import org.whispersystems.signalservice.api.SignalServiceMessageReceiver; import org.whispersystems.signalservice.api.SignalServiceMessageReceiver;
import org.whispersystems.signalservice.api.SignalServiceMessageSender; 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.*;
import org.whispersystems.signalservice.api.messages.multidevice.SignalServiceSyncMessage;
import org.whispersystems.signalservice.api.push.SignalServiceAddress; import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.TrustStore; import org.whispersystems.signalservice.api.push.TrustStore;
import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException; import org.whispersystems.signalservice.api.push.exceptions.AuthorizationFailedException;
@ -453,6 +455,73 @@ class Manager implements Signal {
void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, GroupInfo group); void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent decryptedContent, GroupInfo group);
} }
private GroupInfo handleSignalServiceDataMessage(SignalServiceDataMessage message, boolean isSync, String source, String destination) {
GroupInfo group = null;
if (message.getGroupInfo().isPresent()) {
SignalServiceGroup groupInfo = message.getGroupInfo().get();
switch (groupInfo.getType()) {
case UPDATE:
try {
group = groupStore.getGroup(groupInfo.getGroupId());
} catch (GroupNotFoundException e) {
group = new GroupInfo(groupInfo.getGroupId());
}
if (groupInfo.getAvatar().isPresent()) {
SignalServiceAttachment avatar = groupInfo.getAvatar().get();
if (avatar.isPointer()) {
long avatarId = avatar.asPointer().getId();
try {
retrieveAttachment(avatar.asPointer());
group.avatarId = avatarId;
} catch (IOException | InvalidMessageException e) {
System.err.println("Failed to retrieve group avatar (" + avatarId + "): " + e.getMessage());
}
}
}
if (groupInfo.getName().isPresent()) {
group.name = groupInfo.getName().get();
}
if (groupInfo.getMembers().isPresent()) {
group.members.addAll(groupInfo.getMembers().get());
}
groupStore.updateGroup(group);
break;
case DELIVER:
try {
group = groupStore.getGroup(groupInfo.getGroupId());
} catch (GroupNotFoundException e) {
}
break;
case QUIT:
try {
group = groupStore.getGroup(groupInfo.getGroupId());
group.members.remove(source);
} catch (GroupNotFoundException e) {
}
break;
}
}
if (message.isEndSession()) {
handleEndSession(isSync ? destination : source);
}
if (message.getAttachments().isPresent()) {
for (SignalServiceAttachment attachment : message.getAttachments().get()) {
if (attachment.isPointer()) {
try {
retrieveAttachment(attachment.asPointer());
} catch (IOException | InvalidMessageException e) {
System.err.println("Failed to retrieve attachment (" + attachment.asPointer().getId() + "): " + e.getMessage());
}
}
}
}
return group;
}
public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException { public void receiveMessages(int timeoutSeconds, boolean returnOnTimeout, ReceiveMessageHandler handler) throws IOException {
final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT); final SignalServiceMessageReceiver messageReceiver = new SignalServiceMessageReceiver(URL, TRUST_STORE, username, password, signalingKey, USER_AGENT);
SignalServiceMessagePipe messagePipe = null; SignalServiceMessagePipe messagePipe = null;
@ -471,67 +540,19 @@ class Manager implements Signal {
if (content != null) { if (content != null) {
if (content.getDataMessage().isPresent()) { if (content.getDataMessage().isPresent()) {
SignalServiceDataMessage message = content.getDataMessage().get(); SignalServiceDataMessage message = content.getDataMessage().get();
if (message.getGroupInfo().isPresent()) { group = handleSignalServiceDataMessage(message, false, envelope.getSource(), username);
SignalServiceGroup groupInfo = message.getGroupInfo().get(); }
switch (groupInfo.getType()) { if (content.getSyncMessage().isPresent()) {
case UPDATE: SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
try { if (syncMessage.getSent().isPresent()) {
group = groupStore.getGroup(groupInfo.getGroupId()); SignalServiceDataMessage message = syncMessage.getSent().get().getMessage();
} catch (GroupNotFoundException e) { group = handleSignalServiceDataMessage(message, true, envelope.getSource(), syncMessage.getSent().get().getDestination().get());
group = new GroupInfo(groupInfo.getGroupId());
}
if (groupInfo.getAvatar().isPresent()) {
SignalServiceAttachment avatar = groupInfo.getAvatar().get();
if (avatar.isPointer()) {
long avatarId = avatar.asPointer().getId();
try {
retrieveAttachment(avatar.asPointer());
group.avatarId = avatarId;
} catch (IOException | InvalidMessageException e) {
System.err.println("Failed to retrieve group avatar (" + avatarId + "): " + e.getMessage());
}
}
}
if (groupInfo.getName().isPresent()) {
group.name = groupInfo.getName().get();
}
if (groupInfo.getMembers().isPresent()) {
group.members.addAll(groupInfo.getMembers().get());
}
groupStore.updateGroup(group);
break;
case DELIVER:
try {
group = groupStore.getGroup(groupInfo.getGroupId());
} catch (GroupNotFoundException e) {
}
break;
case QUIT:
try {
group = groupStore.getGroup(groupInfo.getGroupId());
group.members.remove(envelope.getSource());
} catch (GroupNotFoundException e) {
}
break;
}
} }
if (message.isEndSession()) { if (syncMessage.getRequest().isPresent()) {
handleEndSession(envelope.getSource()); // TODO
} }
if (message.getAttachments().isPresent()) { if (syncMessage.getGroups().isPresent()) {
for (SignalServiceAttachment attachment : message.getAttachments().get()) { // TODO
if (attachment.isPointer()) {
try {
retrieveAttachment(attachment.asPointer());
} catch (IOException | InvalidMessageException e) {
System.err.println("Failed to retrieve attachment (" + attachment.asPointer().getId() + "): " + e.getMessage());
}
}
}
} }
} }
} }