*/
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 net.sourceforge.argparse4j.ArgumentParsers;
import net.sourceforge.argparse4j.impl.Arguments;
import net.sourceforge.argparse4j.inf.*;
import org.asamk.signal.storage.contacts.ContactInfo;
import org.asamk.signal.storage.groups.GroupInfo;
import org.asamk.signal.storage.protocol.JsonIdentityKeyStore;
-import org.asamk.signal.util.Base64;
import org.asamk.signal.util.Hex;
import org.freedesktop.dbus.DBusConnection;
import org.freedesktop.dbus.DBusSigHandler;
import org.freedesktop.dbus.exceptions.DBusException;
import org.freedesktop.dbus.exceptions.DBusExecutionException;
import org.whispersystems.libsignal.InvalidKeyException;
+import org.whispersystems.libsignal.util.guava.Optional;
import org.whispersystems.signalservice.api.crypto.UntrustedIdentityException;
import org.whispersystems.signalservice.api.messages.*;
+import org.whispersystems.signalservice.api.messages.calls.*;
import org.whispersystems.signalservice.api.messages.multidevice.*;
import org.whispersystems.signalservice.api.push.SignalServiceAddress;
import org.whispersystems.signalservice.api.push.exceptions.EncapsulatedExceptions;
import org.whispersystems.signalservice.api.push.exceptions.NetworkFailureException;
import org.whispersystems.signalservice.api.push.exceptions.UnregisteredUserException;
import org.whispersystems.signalservice.api.util.PhoneNumberFormatter;
+import org.whispersystems.signalservice.internal.push.LockedException;
+import org.whispersystems.signalservice.internal.util.Base64;
import java.io.File;
import java.io.IOException;
return 3;
}
break;
+ case "setPin":
+ if (dBusConn != null) {
+ System.err.println("setPin is not yet implemented via dbus");
+ return 1;
+ }
+ if (!m.isRegistered()) {
+ System.err.println("User is not registered.");
+ return 1;
+ }
+ try {
+ String registrationLockPin = ns.getString("registrationLockPin");
+ m.setRegistrationLockPin(Optional.of(registrationLockPin));
+ } catch (IOException e) {
+ System.err.println("Set pin error: " + e.getMessage());
+ return 3;
+ }
+ break;
+ case "removePin":
+ if (dBusConn != null) {
+ System.err.println("removePin is not yet implemented via dbus");
+ return 1;
+ }
+ if (!m.isRegistered()) {
+ System.err.println("User is not registered.");
+ return 1;
+ }
+ try {
+ m.setRegistrationLockPin(Optional.<String>absent());
+ } catch (IOException e) {
+ System.err.println("Remove pin error: " + e.getMessage());
+ return 3;
+ }
+ break;
case "verify":
if (dBusConn != null) {
System.err.println("verify is not yet implemented via dbus");
return 1;
}
try {
- m.verifyAccount(ns.getString("verificationCode"));
+ String verificationCode = ns.getString("verificationCode");
+ String pin = ns.getString("pin");
+ m.verifyAccount(verificationCode, pin);
+ } catch (LockedException e) {
+ System.err.println("Verification failed! This number is locked with a pin. Hours remaining until reset: " + (e.getTimeRemaining() / 1000 / 60 / 60));
+ System.err.println("Use '--pin PIN_CODE' to specify the registration lock PIN");
+ return 3;
} catch (IOException e) {
System.err.println("Verify error: " + e.getMessage());
return 3;
System.out.println();
}
});
+ dBusConn.addSigHandler(Signal.ReceiptReceived.class, new DBusSigHandler<Signal.ReceiptReceived>() {
+ @Override
+ public void handle(Signal.ReceiptReceived s) {
+ System.out.print(String.format("Receipt from: %s\nTimestamp: %s\n",
+ s.getSender(), formatTimestamp(s.getTimestamp())));
+ }
+ });
} catch (UnsatisfiedLinkError e) {
System.err.println("Missing native library dependency for dbus service: " + e.getMessage());
return 1;
}
boolean ignoreAttachments = ns.getBoolean("ignore_attachments");
try {
- m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, returnOnTimeout, ignoreAttachments, new ReceiveMessageHandler(m));
+ final Manager.ReceiveMessageHandler handler = ns.getBoolean("json") ? new JsonReceiveMessageHandler(m) : new ReceiveMessageHandler(m);
+ m.receiveMessages((long) (timeout * 1000), TimeUnit.MILLISECONDS, returnOnTimeout, ignoreAttachments, handler);
} catch (IOException e) {
System.err.println("Error while receiving messages: " + e.getMessage());
return 3;
}
private static Namespace parseArgs(String[] args) {
- ArgumentParser parser = ArgumentParsers.newArgumentParser("signal-cli")
+ ArgumentParser parser = ArgumentParsers.newFor("signal-cli")
+ .build()
.defaultHelp(true)
.description("Commandline interface for Signal.")
.version(Manager.PROJECT_NAME + " " + Manager.PROJECT_VERSION);
Subparser parserUpdateAccount = subparsers.addParser("updateAccount");
parserUpdateAccount.help("Update the account attributes on the signal server.");
+ Subparser parserSetPin = subparsers.addParser("setPin");
+ parserSetPin.addArgument("registrationLockPin")
+ .help("The registration lock PIN, that will be required for new registrations (resets after 7 days of inactivity)");
+
+ Subparser parserRemovePin = subparsers.addParser("removePin");
+
Subparser parserVerify = subparsers.addParser("verify");
parserVerify.addArgument("verificationCode")
.help("The verification code you received via sms or voice call.");
+ parserVerify.addArgument("-p", "--pin")
+ .help("The registration lock PIN, that was set by the user (Optional)");
Subparser parserSend = subparsers.addParser("send");
parserSend.addArgument("-g", "--group")
parserReceive.addArgument("--ignore-attachments")
.help("Don’t download attachments of received messages.")
.action(Arguments.storeTrue());
+ parserReceive.addArgument("--json")
+ .help("Output received messages in json format, one json object per line.")
+ .action(Arguments.storeTrue());
Subparser parserDaemon = subparsers.addParser("daemon");
parserDaemon.addArgument("--system")
SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
if (syncMessage.getContacts().isPresent()) {
- System.out.println("Received sync contacts");
- printAttachment(syncMessage.getContacts().get());
+ final ContactsMessage contactsMessage = syncMessage.getContacts().get();
+ if (contactsMessage.isComplete()) {
+ System.out.println("Received complete sync contacts");
+ } else {
+ System.out.println("Received sync contacts");
+ }
+ printAttachment(contactsMessage.getContactsStream());
}
if (syncMessage.getGroups().isPresent()) {
System.out.println("Received sync groups");
System.out.println(" - " + number);
}
}
+ if (syncMessage.getVerified().isPresent()) {
+ System.out.println("Received sync message with verified identities:");
+ final VerifiedMessage verifiedMessage = syncMessage.getVerified().get();
+ System.out.println(" - " + verifiedMessage.getDestination() + ": " + verifiedMessage.getVerified());
+ String safetyNumber = formatSafetyNumber(m.computeSafetyNumber(verifiedMessage.getDestination(), verifiedMessage.getIdentityKey()));
+ System.out.println(" " + safetyNumber);
+ }
+ if (syncMessage.getConfiguration().isPresent()) {
+ System.out.println("Received sync message with configuration:");
+ final ConfigurationMessage configurationMessage = syncMessage.getConfiguration().get();
+ if (configurationMessage.getReadReceipts().isPresent()) {
+ System.out.println(" - Read receipts: " + (configurationMessage.getReadReceipts().get() ? "enabled" : "disabled"));
+ }
+ }
+ }
+ if (content.getCallMessage().isPresent()) {
+ System.out.println("Received a call message");
+ SignalServiceCallMessage callMessage = content.getCallMessage().get();
+ if (callMessage.getAnswerMessage().isPresent()) {
+ AnswerMessage answerMessage = callMessage.getAnswerMessage().get();
+ System.out.println("Answer message: " + answerMessage.getId() + ": " + answerMessage.getDescription());
+ }
+ if (callMessage.getBusyMessage().isPresent()) {
+ BusyMessage busyMessage = callMessage.getBusyMessage().get();
+ System.out.println("Busy message: " + busyMessage.getId());
+ }
+ if (callMessage.getHangupMessage().isPresent()) {
+ HangupMessage hangupMessage = callMessage.getHangupMessage().get();
+ System.out.println("Hangup message: " + hangupMessage.getId());
+ }
+ if (callMessage.getIceUpdateMessages().isPresent()) {
+ List<IceUpdateMessage> iceUpdateMessages = callMessage.getIceUpdateMessages().get();
+ for (IceUpdateMessage iceUpdateMessage : iceUpdateMessages) {
+ System.out.println("Ice update message: " + iceUpdateMessage.getId() + ", sdp: " + iceUpdateMessage.getSdp());
+ }
+ }
+ if (callMessage.getOfferMessage().isPresent()) {
+ OfferMessage offerMessage = callMessage.getOfferMessage().get();
+ System.out.println("Offer message: " + offerMessage.getId() + ": " + offerMessage.getDescription());
+ }
+ }
+ if (content.getReceiptMessage().isPresent()) {
+ System.out.println("Received a receipt message");
+ SignalServiceReceiptMessage receiptMessage = content.getReceiptMessage().get();
+ System.out.println(" - When: " + formatTimestamp(receiptMessage.getWhen()));
+ if (receiptMessage.isDeliveryReceipt()) {
+ System.out.println(" - Is delivery receipt");
+ }
+ if (receiptMessage.isReadReceipt()) {
+ System.out.println(" - Is read receipt");
+ }
+ System.out.println(" - Timestamps:");
+ for (long timestamp : receiptMessage.getTimestamps()) {
+ System.out.println(" " + formatTimestamp(timestamp));
+ }
}
}
} else {
if (message.getExpiresInSeconds() > 0) {
System.out.println("Expires in: " + message.getExpiresInSeconds() + " seconds");
}
+ if (message.isProfileKeyUpdate() && message.getProfileKey().isPresent()) {
+ System.out.println("Profile key update, key length:" + message.getProfileKey().get().length);
+ }
if (message.getAttachments().isPresent()) {
System.out.println("Attachments: ");
System.out.println(" Id: " + pointer.getId() + " Key length: " + pointer.getKey().length + (pointer.getRelay().isPresent() ? " Relay: " + pointer.getRelay().get() : ""));
System.out.println(" Filename: " + (pointer.getFileName().isPresent() ? pointer.getFileName().get() : "-"));
System.out.println(" Size: " + (pointer.getSize().isPresent() ? pointer.getSize().get() + " bytes" : "<unavailable>") + (pointer.getPreview().isPresent() ? " (Preview is available: " + pointer.getPreview().get().length + " bytes)" : ""));
+ System.out.println(" Voice note: " + (pointer.getVoiceNote() ? "yes" : "no"));
+ System.out.println(" Dimensions: " + pointer.getWidth() + "x" + pointer.getHeight());
File file = m.getAttachmentFile(pointer.getId());
if (file.exists()) {
System.out.println(" Stored plaintext in: " + file);
public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
super.handleMessage(envelope, content, exception);
- if (!envelope.isReceipt() && content != null && content.getDataMessage().isPresent()) {
+ if (envelope.isReceipt()) {
+ try {
+ conn.sendSignal(new Signal.ReceiptReceived(
+ SIGNAL_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() &&
}
}
}
+ }
+
+ private static class JsonReceiveMessageHandler implements Manager.ReceiveMessageHandler {
+ final Manager m;
+ final ObjectMapper jsonProcessor;
+
+ public 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();
+ }
+ }
}
private static String formatTimestamp(long timestamp) {