X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/4377a2179b38360b00efc1c1db00a62f3f020db1..a44034a79e6a98e7d77bbde0af1483d8c30a22c6:/src/main/java/org/asamk/signal/Main.java diff --git a/src/main/java/org/asamk/signal/Main.java b/src/main/java/org/asamk/signal/Main.java index a9097fc9..3b2fbadb 100644 --- a/src/main/java/org/asamk/signal/Main.java +++ b/src/main/java/org/asamk/signal/Main.java @@ -16,6 +16,13 @@ */ 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.*; @@ -24,21 +31,24 @@ import org.asamk.Signal; 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; @@ -175,6 +185,39 @@ public class Main { 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.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"); @@ -189,7 +232,13 @@ public class Main { 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; @@ -390,6 +439,13 @@ public class Main { System.out.println(); } }); + dBusConn.addSigHandler(Signal.ReceiptReceived.class, new DBusSigHandler() { + @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; @@ -420,7 +476,8 @@ public class Main { } 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; @@ -705,7 +762,8 @@ public class Main { } 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); @@ -760,9 +818,17 @@ public class Main { 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") @@ -822,6 +888,9 @@ public class Main { 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") @@ -830,6 +899,9 @@ public class Main { parserDaemon.addArgument("--ignore-attachments") .help("Don’t download attachments of received messages.") .action(Arguments.storeTrue()); + parserDaemon.addArgument("--json") + .help("Output received messages in json format, one json object per line.") + .action(Arguments.storeTrue()); try { Namespace ns = parser.parseArgs(args); @@ -994,13 +1066,58 @@ public class Main { } if (syncMessage.getVerified().isPresent()) { System.out.println("Received sync message with verified identities:"); - final List verifiedList = syncMessage.getVerified().get(); - for (VerifiedMessage v : verifiedList) { - System.out.println(" - " + v.getDestination() + ": " + v.getVerified()); - String safetyNumber = formatSafetyNumber(m.computeSafetyNumber(v.getDestination(), v.getIdentityKey())); - System.out.println(" " + safetyNumber); + 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 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)); } } } @@ -1050,6 +1167,25 @@ public class Main { 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.getQuote().isPresent()) { + SignalServiceDataMessage.Quote quote = message.getQuote().get(); + System.out.println("Quote: (" + quote.getId() + ")"); + System.out.println(" Author: " + quote.getAuthor().getNumber()); + System.out.println(" Text: " + quote.getText()); + if (quote.getAttachments().size() > 0) { + System.out.println(" Attachments: "); + for (SignalServiceDataMessage.Quote.QuotedAttachment attachment : quote.getAttachments()) { + System.out.println(" Filename: " + attachment.getFileName()); + System.out.println(" Type: " + attachment.getContentType()); + System.out.println(" Thumbnail:"); + printAttachment(attachment.getThumbnail()); + } + } + } if (message.getAttachments().isPresent()) { System.out.println("Attachments: "); @@ -1067,6 +1203,7 @@ public class Main { System.out.println(" Filename: " + (pointer.getFileName().isPresent() ? pointer.getFileName().get() : "-")); System.out.println(" Size: " + (pointer.getSize().isPresent() ? pointer.getSize().get() + " bytes" : "") + (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); @@ -1087,7 +1224,17 @@ public class Main { 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() && @@ -1116,7 +1263,37 @@ public class Main { } } } + } + + 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) {