Improve receiving messages

This commit is contained in:
AsamK 2015-07-07 11:31:50 +02:00
parent dbebd38391
commit 0a7a6a5c54
2 changed files with 70 additions and 40 deletions

View file

@ -20,13 +20,9 @@ import net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.*; import net.sourceforge.argparse4j.inf.*;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.whispersystems.libaxolotl.InvalidVersionException;
import org.whispersystems.textsecure.api.TextSecureMessageSender; import org.whispersystems.textsecure.api.TextSecureMessageSender;
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException; import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
import org.whispersystems.textsecure.api.messages.TextSecureAttachment; import org.whispersystems.textsecure.api.messages.*;
import org.whispersystems.textsecure.api.messages.TextSecureAttachmentStream;
import org.whispersystems.textsecure.api.messages.TextSecureContent;
import org.whispersystems.textsecure.api.messages.TextSecureDataMessage;
import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage; import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
import org.whispersystems.textsecure.api.push.TextSecureAddress; import org.whispersystems.textsecure.api.push.TextSecureAddress;
@ -82,8 +78,8 @@ public class Main {
System.exit(1); System.exit(1);
} }
String username = ns.getString("username"); final String username = ns.getString("username");
Manager m = new Manager(username); final Manager m = new Manager(username);
if (m.userExists()) { if (m.userExists()) {
try { try {
m.load(); m.load();
@ -168,26 +164,47 @@ public class Main {
System.exit(1); System.exit(1);
} }
try { try {
TextSecureContent content = m.receiveMessage(); m.receiveMessages(new Manager.ReceiveMessageHandler() {
if (content.getDataMessage().isPresent()) { @Override
message = content.getDataMessage().get(); public void handleMessage(TextSecureEnvelope envelope) {
if (message == null) { System.out.println("Envelope from: " + envelope.getSource());
System.exit(0); System.out.println("Timestamp: " + envelope.getTimestamp());
} else {
System.out.println("Received message: " + message.getBody().get());
}
}
if (content.getSyncMessage().isPresent()) {
TextSecureSyncMessage syncMessage = content.getSyncMessage().get();
if (syncMessage == null) { if (envelope.isReceipt()) {
System.exit(0); System.out.println("Got receipt.");
} else { } else if (envelope.isWhisperMessage() | envelope.isPreKeyWhisperMessage()) {
System.out.println("Received sync message"); TextSecureContent content = m.decryptMessage(envelope);
if (content == null) {
System.out.println("Failed to decrypt message.");
} else {
if (content.getDataMessage().isPresent()) {
TextSecureDataMessage message = content.getDataMessage().get();
System.out.println("Body: " + message.getBody().get());
if (message.getAttachments().isPresent()) {
System.out.println("Attachments: ");
for (TextSecureAttachment attachment : message.getAttachments().get()) {
System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");
if (attachment.isPointer()) {
System.out.println(" Id: " + attachment.asPointer().getId() + " Key length: " + attachment.asPointer().getKey().length + (attachment.asPointer().getRelay().isPresent() ? " Relay: " + attachment.asPointer().getRelay().get() : ""));
}
}
}
}
if (content.getSyncMessage().isPresent()) {
TextSecureSyncMessage syncMessage = content.getSyncMessage().get();
System.out.println("Received sync message");
}
}
} else {
System.out.println("Unknown message received.");
}
System.out.println();
} }
} });
} catch (IOException | InvalidVersionException e) { } catch (IOException e) {
System.out.println("Receive message: " + e.getMessage()); System.out.println("Error while receiving message: " + e.getMessage());
} }
break; break;
} }

View file

@ -35,6 +35,8 @@ import org.whispersystems.textsecure.api.messages.TextSecureContent;
import org.whispersystems.textsecure.api.messages.TextSecureEnvelope; import org.whispersystems.textsecure.api.messages.TextSecureEnvelope;
import org.whispersystems.textsecure.api.push.TextSecureAddress; import org.whispersystems.textsecure.api.push.TextSecureAddress;
import org.whispersystems.textsecure.api.push.TrustStore; import org.whispersystems.textsecure.api.push.TrustStore;
import org.whispersystems.textsecure.api.util.InvalidNumberException;
import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
import java.io.*; import java.io.*;
import java.util.List; import java.util.List;
@ -166,28 +168,39 @@ public class Manager {
axolotlStore, Optional.<TextSecureMessageSender.EventListener>absent()); axolotlStore, Optional.<TextSecureMessageSender.EventListener>absent());
} }
public TextSecureContent receiveMessage() throws IOException, InvalidVersionException { public TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
TextSecureCipher cipher = new TextSecureCipher(new TextSecureAddress(username), axolotlStore);
try {
return cipher.decrypt(envelope);
} catch (Exception e) {
// TODO handle all exceptions
e.printStackTrace();
return null;
}
}
public interface ReceiveMessageHandler {
void handleMessage(TextSecureEnvelope envelope);
}
public void receiveMessages(ReceiveMessageHandler handler) throws IOException {
TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey); TextSecureMessageReceiver messageReceiver = new TextSecureMessageReceiver(URL, TRUST_STORE, username, password, signalingKey);
TextSecureMessagePipe messagePipe = null; TextSecureMessagePipe messagePipe = null;
try { try {
messagePipe = messageReceiver.createMessagePipe(); messagePipe = messageReceiver.createMessagePipe();
TextSecureEnvelope envelope; while (true) {
try { TextSecureEnvelope envelope;
envelope = messagePipe.read(5, TimeUnit.SECONDS); try {
} catch (TimeoutException e) { envelope = messagePipe.read(1, TimeUnit.MINUTES);
return null; handler.handleMessage(envelope);
} catch (TimeoutException e) {
} catch (InvalidVersionException e) {
System.out.println("Ignoring error: " + e.getMessage());
}
save();
} }
TextSecureCipher cipher = new TextSecureCipher(new TextSecureAddress(username), axolotlStore);
TextSecureContent message = null;
try {
message = cipher.decrypt(envelope);
} catch (Exception e) {
// TODO handle all exceptions
e.printStackTrace();
}
return message;
} finally { } finally {
if (messagePipe != null) if (messagePipe != null)
messagePipe.shutdown(); messagePipe.shutdown();