- Subparser parserLink = subparsers.addParser("link");
- parserLink.addArgument("-n", "--name")
- .help("Specify a name to describe this new device.");
-
- Subparser parserAddDevice = subparsers.addParser("addDevice");
- parserAddDevice.addArgument("--uri")
- .required(true)
- .help("Specify the uri contained in the QR code shown by the new device.");
-
- Subparser parserDevices = subparsers.addParser("listDevices");
-
- Subparser parserRemoveDevice = subparsers.addParser("removeDevice");
- parserRemoveDevice.addArgument("-d", "--deviceId")
- .type(int.class)
- .required(true)
- .help("Specify the device you want to remove. Use listDevices to see the deviceIds.");
-
- Subparser parserRegister = subparsers.addParser("register");
- parserRegister.addArgument("-v", "--voice")
- .help("The verification should be done over voice, not sms.")
- .action(Arguments.storeTrue());
-
- Subparser parserUnregister = subparsers.addParser("unregister");
- parserUnregister.help("Unregister the current device from the signal server.");
-
- 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")
- .help("Specify the recipient group ID.");
- parserSend.addArgument("recipient")
- .help("Specify the recipients' phone number.")
- .nargs("*");
- parserSend.addArgument("-m", "--message")
- .help("Specify the message, if missing standard input is used.");
- parserSend.addArgument("-a", "--attachment")
- .nargs("*")
- .help("Add file as attachment");
- parserSend.addArgument("-e", "--endsession")
- .help("Clear session state and send end session message.")
- .action(Arguments.storeTrue());
-
- Subparser parserLeaveGroup = subparsers.addParser("quitGroup");
- parserLeaveGroup.addArgument("-g", "--group")
- .required(true)
- .help("Specify the recipient group ID.");
-
- Subparser parserUpdateGroup = subparsers.addParser("updateGroup");
- parserUpdateGroup.addArgument("-g", "--group")
- .help("Specify the recipient group ID.");
- parserUpdateGroup.addArgument("-n", "--name")
- .help("Specify the new group name.");
- parserUpdateGroup.addArgument("-a", "--avatar")
- .help("Specify a new group avatar image file");
- parserUpdateGroup.addArgument("-m", "--member")
- .nargs("*")
- .help("Specify one or more members to add to the group");
-
- Subparser parserListGroups = subparsers.addParser("listGroups");
- parserListGroups.addArgument("-d", "--detailed").action(Arguments.storeTrue())
- .help("List members of each group");
- parserListGroups.help("List group name and ids");
-
- Subparser parserListIdentities = subparsers.addParser("listIdentities");
- parserListIdentities.addArgument("-n", "--number")
- .help("Only show identity keys for the given phone number.");
-
- Subparser parserTrust = subparsers.addParser("trust");
- parserTrust.addArgument("number")
- .help("Specify the phone number, for which to set the trust.")
- .required(true);
- MutuallyExclusiveGroup mutTrust = parserTrust.addMutuallyExclusiveGroup();
- mutTrust.addArgument("-a", "--trust-all-known-keys")
- .help("Trust all known keys of this user, only use this for testing.")
- .action(Arguments.storeTrue());
- mutTrust.addArgument("-v", "--verified-fingerprint")
- .help("Specify the fingerprint of the key, only use this option if you have verified the fingerprint.");
-
- Subparser parserReceive = subparsers.addParser("receive");
- parserReceive.addArgument("-t", "--timeout")
- .type(double.class)
- .help("Number of seconds to wait for new messages (negative values disable timeout)");
- 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")
- .action(Arguments.storeTrue())
- .help("Use DBus system bus instead of user bus.");
- 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);
- if ("link".equals(ns.getString("command"))) {
- if (ns.getString("username") != null) {
- parser.printUsage();
- System.err.println("You cannot specify a username (phone number) when linking");
- System.exit(2);
- }
- } else if (!ns.getBoolean("dbus") && !ns.getBoolean("dbus_system")) {
- if (ns.getString("username") == null) {
- parser.printUsage();
- System.err.println("You need to specify a username (phone number)");
- System.exit(2);
- }
- if (!PhoneNumberFormatter.isValidNumber(ns.getString("username"))) {
- System.err.println("Invalid username (phone number), make sure you include the country code.");
- System.exit(2);
- }
- }
- if (ns.getList("recipient") != null && !ns.getList("recipient").isEmpty() && ns.getString("group") != null) {
- System.err.println("You cannot specify recipients by phone number and groups a the same time");
- System.exit(2);
- }
- return ns;
- } catch (ArgumentParserException e) {
- parser.handleError(e);
- return null;
- }
- }
-
- private static void handleAssertionError(AssertionError e) {
- System.err.println("Failed to send/receive message (Assertion): " + e.getMessage());
- e.printStackTrace();
- System.err.println("If you use an Oracle JRE please check if you have unlimited strength crypto enabled, see README");
- }
-
- private static void handleEncapsulatedExceptions(EncapsulatedExceptions e) {
- System.err.println("Failed to send (some) messages:");
- for (NetworkFailureException n : e.getNetworkExceptions()) {
- System.err.println("Network failure for \"" + n.getE164number() + "\": " + n.getMessage());
- }
- for (UnregisteredUserException n : e.getUnregisteredUserExceptions()) {
- System.err.println("Unregistered user \"" + n.getE164Number() + "\": " + n.getMessage());
- }
- for (UntrustedIdentityException n : e.getUntrustedIdentityExceptions()) {
- System.err.println("Untrusted Identity for \"" + n.getE164Number() + "\": " + n.getMessage());
- }
- }
-
- private static void handleIOException(IOException e) {
- System.err.println("Failed to send message: " + e.getMessage());
- }
-
- private static class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
- final Manager m;
-
- public ReceiveMessageHandler(Manager m) {
- this.m = m;
- }
-
- @Override
- public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
- SignalServiceAddress source = envelope.getSourceAddress();
- ContactInfo sourceContact = m.getContact(source.getNumber());
- System.out.println(String.format("Envelope from: %s (device: %d)", (sourceContact == null ? "" : "“" + sourceContact.name + "” ") + source.getNumber(), envelope.getSourceDevice()));
- if (source.getRelay().isPresent()) {
- System.out.println("Relayed by: " + source.getRelay().get());
- }
- System.out.println("Timestamp: " + DateUtils.formatTimestamp(envelope.getTimestamp()));
- if (envelope.isUnidentifiedSender()) {
- System.out.println("Sent by unidentified/sealed sender");
- }
-
- if (envelope.isReceipt()) {
- System.out.println("Got receipt.");
- } else if (envelope.isSignalMessage() | envelope.isPreKeySignalMessage()) {
- if (exception != null) {
- if (exception instanceof org.whispersystems.libsignal.UntrustedIdentityException) {
- org.whispersystems.libsignal.UntrustedIdentityException e = (org.whispersystems.libsignal.UntrustedIdentityException) exception;
- System.out.println("The user’s key is untrusted, either the user has reinstalled Signal or a third party sent this message.");
- System.out.println("Use 'signal-cli -u " + m.getUsername() + " listIdentities -n " + e.getName() + "', verify the key and run 'signal-cli -u " + m.getUsername() + " trust -v \"FINGER_PRINT\" " + e.getName() + "' to mark it as trusted");
- System.out.println("If you don't care about security, use 'signal-cli -u " + m.getUsername() + " trust -a " + e.getName() + "' to trust it without verification");
- } else {
- System.out.println("Exception: " + exception.getMessage() + " (" + exception.getClass().getSimpleName() + ")");
- }
- }
- if (content == null) {
- System.out.println("Failed to decrypt message.");
- } else {
- if (content.getDataMessage().isPresent()) {
- SignalServiceDataMessage message = content.getDataMessage().get();
- handleSignalServiceDataMessage(message);
- }
- if (content.getSyncMessage().isPresent()) {
- System.out.println("Received a sync message");
- SignalServiceSyncMessage syncMessage = content.getSyncMessage().get();
-
- if (syncMessage.getContacts().isPresent()) {
- 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");
- printAttachment(syncMessage.getGroups().get());
- }
- if (syncMessage.getRead().isPresent()) {
- System.out.println("Received sync read messages list");
- for (ReadMessage rm : syncMessage.getRead().get()) {
- ContactInfo fromContact = m.getContact(rm.getSender());
- System.out.println("From: " + (fromContact == null ? "" : "“" + fromContact.name + "” ") + rm.getSender() + " Message timestamp: " + DateUtils.formatTimestamp(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();
- String to;
- if (sentTranscriptMessage.getDestination().isPresent()) {
- String dest = sentTranscriptMessage.getDestination().get();
- ContactInfo destContact = m.getContact(dest);
- to = (destContact == null ? "" : "“" + destContact.name + "” ") + dest;
- } else {
- to = "Unknown";
- }
- System.out.println("To: " + to + " , Message timestamp: " + DateUtils.formatTimestamp(sentTranscriptMessage.getTimestamp()));
- if (sentTranscriptMessage.getExpirationStartTimestamp() > 0) {
- System.out.println("Expiration started at: " + DateUtils.formatTimestamp(sentTranscriptMessage.getExpirationStartTimestamp()));
- }
- SignalServiceDataMessage message = sentTranscriptMessage.getMessage();
- handleSignalServiceDataMessage(message);
- }
- if (syncMessage.getBlockedList().isPresent()) {
- System.out.println("Received sync message with block list");
- System.out.println("Blocked numbers:");
- final BlockedListMessage blockedList = syncMessage.getBlockedList().get();
- for (String number : blockedList.getNumbers()) {
- 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 = Util.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: " + DateUtils.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(" " + DateUtils.formatTimestamp(timestamp));
- }
- }
- if (content.getTypingMessage().isPresent()) {
- System.out.println("Received a typing message");
- SignalServiceTypingMessage typingMessage = content.getTypingMessage().get();
- System.out.println(" - Action: " + typingMessage.getAction());
- System.out.println(" - Timestamp: " + DateUtils.formatTimestamp(typingMessage.getTimestamp()));
- if (typingMessage.getGroupId().isPresent()) {
- GroupInfo group = m.getGroup(typingMessage.getGroupId().get());
- if (group != null) {
- System.out.println(" Name: " + group.name);
- } else {
- System.out.println(" Name: <Unknown group>");
- }
- }
- }
- }
- } else {
- System.out.println("Unknown message received.");
- }
- System.out.println();
- }
-
- private void handleSignalServiceDataMessage(SignalServiceDataMessage message) {
- System.out.println("Message timestamp: " + DateUtils.formatTimestamp(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.getType() == SignalServiceGroup.Type.UPDATE && groupInfo.getName().isPresent()) {
- System.out.println(" Name: " + groupInfo.getName().get());
- } else {
- GroupInfo group = m.getGroup(groupInfo.getGroupId());
- 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.isExpirationUpdate()) {
- System.out.println("Is Expiration update: " + message.isExpirationUpdate());
- }
- if (message.getExpiresInSeconds() > 0) {
- System.out.println("Expires in: " + message.getExpiresInSeconds() + " seconds");
- }
- if (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:");
- if (attachment.getThumbnail() != null) {
- printAttachment(attachment.getThumbnail());
- }
- }
- }
- }
-
- if (message.getAttachments().isPresent()) {
- System.out.println("Attachments: ");
- for (SignalServiceAttachment attachment : message.getAttachments().get()) {
- printAttachment(attachment);
- }
- }
- }
-
- private void printAttachment(SignalServiceAttachment attachment) {
- System.out.println("- " + attachment.getContentType() + " (" + (attachment.isPointer() ? "Pointer" : "") + (attachment.isStream() ? "Stream" : "") + ")");
- if (attachment.isPointer()) {
- final SignalServiceAttachmentPointer pointer = attachment.asPointer();
- System.out.println(" Id: " + pointer.getId() + " Key length: " + pointer.getKey().length);
- 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);
- }
- }
- }
- }
-
- private static class DbusReceiveMessageHandler extends ReceiveMessageHandler {
- final DBusConnection conn;
-
- public DbusReceiveMessageHandler(Manager m, DBusConnection conn) {
- super(m);
- this.conn = conn;
- }
-
- @Override
- public void handleMessage(SignalServiceEnvelope envelope, SignalServiceContent content, Throwable exception) {
- super.handleMessage(envelope, content, exception);
-
- JsonDbusReceiveMessageHandler.sendReceivedMessageToDbus(envelope, content, conn, m);
- }
- }
-
- 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);