mirror of
https://github.com/AsamK/signal-cli
synced 2025-08-29 02:20:39 +00:00
Improve receiving messages
This commit is contained in:
parent
dbebd38391
commit
0a7a6a5c54
2 changed files with 70 additions and 40 deletions
|
@ -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());
|
||||||
|
|
||||||
|
if (envelope.isReceipt()) {
|
||||||
|
System.out.println("Got receipt.");
|
||||||
|
} else if (envelope.isWhisperMessage() | envelope.isPreKeyWhisperMessage()) {
|
||||||
|
TextSecureContent content = m.decryptMessage(envelope);
|
||||||
|
|
||||||
|
if (content == null) {
|
||||||
|
System.out.println("Failed to decrypt message.");
|
||||||
} else {
|
} else {
|
||||||
System.out.println("Received message: " + message.getBody().get());
|
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()) {
|
if (content.getSyncMessage().isPresent()) {
|
||||||
TextSecureSyncMessage syncMessage = content.getSyncMessage().get();
|
TextSecureSyncMessage syncMessage = content.getSyncMessage().get();
|
||||||
|
|
||||||
if (syncMessage == null) {
|
|
||||||
System.exit(0);
|
|
||||||
} else {
|
|
||||||
System.out.println("Received sync message");
|
System.out.println("Received sync message");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (IOException | InvalidVersionException e) {
|
} else {
|
||||||
System.out.println("Receive message: " + e.getMessage());
|
System.out.println("Unknown message received.");
|
||||||
|
}
|
||||||
|
System.out.println();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error while receiving message: " + e.getMessage());
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -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();
|
||||||
|
|
||||||
|
while (true) {
|
||||||
TextSecureEnvelope envelope;
|
TextSecureEnvelope envelope;
|
||||||
try {
|
try {
|
||||||
envelope = messagePipe.read(5, TimeUnit.SECONDS);
|
envelope = messagePipe.read(1, TimeUnit.MINUTES);
|
||||||
|
handler.handleMessage(envelope);
|
||||||
} catch (TimeoutException e) {
|
} catch (TimeoutException e) {
|
||||||
return null;
|
} catch (InvalidVersionException e) {
|
||||||
|
System.out.println("Ignoring error: " + e.getMessage());
|
||||||
}
|
}
|
||||||
TextSecureCipher cipher = new TextSecureCipher(new TextSecureAddress(username), axolotlStore);
|
save();
|
||||||
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();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue