X-Git-Url: https://git.nmode.ca/signal-cli/blobdiff_plain/73e8c4cabfe55dd4dbea501334c6925d2844a3fb..704f2d76ba3c24dceecabd3fd473d973c29f7699:/src/main/java/cli/Main.java diff --git a/src/main/java/cli/Main.java b/src/main/java/cli/Main.java index 28c64e8c..4cdbb806 100644 --- a/src/main/java/cli/Main.java +++ b/src/main/java/cli/Main.java @@ -1,34 +1,44 @@ /** * Copyright (C) 2015 AsamK - *

+ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - *

+ * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. - *

+ * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ package cli; import net.sourceforge.argparse4j.ArgumentParsers; +import net.sourceforge.argparse4j.impl.Arguments; import net.sourceforge.argparse4j.inf.*; import org.apache.commons.io.IOUtils; -import org.whispersystems.libaxolotl.InvalidVersionException; import org.whispersystems.textsecure.api.TextSecureMessageSender; import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException; -import org.whispersystems.textsecure.api.messages.TextSecureContent; -import org.whispersystems.textsecure.api.messages.TextSecureDataMessage; +import org.whispersystems.textsecure.api.messages.*; import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage; import org.whispersystems.textsecure.api.push.TextSecureAddress; +import org.whispersystems.textsecure.api.push.exceptions.EncapsulatedExceptions; +import org.whispersystems.textsecure.api.push.exceptions.NetworkFailureException; +import org.whispersystems.textsecure.api.push.exceptions.UnregisteredUserException; +import org.whispersystems.textsecure.api.util.InvalidNumberException; +import java.io.File; +import java.io.FileInputStream; import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; import java.security.Security; +import java.util.ArrayList; +import java.util.List; public class Main { @@ -45,15 +55,21 @@ public class Main { .description("valid subcommands") .help("additional help"); Subparser parserRegister = subparsers.addParser("register"); + parserRegister.addArgument("-v", "--voice") + .help("The verification should be done over voice, not sms.") + .action(Arguments.storeTrue()); Subparser parserVerify = subparsers.addParser("verify"); parserVerify.addArgument("verificationCode") - .help("The verification code you received via sms."); + .help("The verification code you received via sms or voice call."); Subparser parserSend = subparsers.addParser("send"); 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"); Subparser parserReceive = subparsers.addParser("receive"); parser.addArgument("-u", "--username") .required(true) @@ -66,8 +82,8 @@ public class Main { System.exit(1); } - String username = ns.getString("username"); - Manager m = new Manager(username); + final String username = ns.getString("username"); + final Manager m = new Manager(username); if (m.userExists()) { try { m.load(); @@ -82,7 +98,7 @@ public class Main { m.createNewIdentity(); } try { - m.register(); + m.register(ns.getBoolean("voice")); } catch (IOException e) { System.out.println("Request verify error: " + e.getMessage()); System.exit(3); @@ -119,12 +135,48 @@ public class Main { System.exit(1); } } - TextSecureDataMessage message = TextSecureDataMessage.newBuilder().withBody(messageText).build(); + final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText); + final List attachments = ns.getList("attachment"); + if (attachments != null) { + List textSecureAttachments = new ArrayList(attachments.size()); + for (String attachment : attachments) { + try { + File attachmentFile = new File(attachment); + InputStream attachmentStream = new FileInputStream(attachmentFile); + final long attachmentSize = attachmentFile.length(); + String mime = Files.probeContentType(Paths.get(attachment)); + textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null)); + } catch (IOException e) { + System.out.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage()); + System.exit(1); + } + } + messageBuilder.withAttachments(textSecureAttachments); + } + TextSecureDataMessage message = messageBuilder.build(); + + List recipients = new ArrayList<>(ns.getList("recipient").size()); for (String recipient : ns.getList("recipient")) { try { - messageSender.sendMessage(new TextSecureAddress(recipient), message); - } catch (UntrustedIdentityException | IOException e) { - System.out.println("Send message: " + e.getMessage()); + recipients.add(m.getPushAddress(recipient)); + } catch (InvalidNumberException e) { + System.out.println("Failed to send message to \"" + recipient + "\": " + e.getMessage()); + } + } + try { + messageSender.sendMessage(recipients, message); + } catch (IOException e) { + System.out.println("Failed to send message: " + e.getMessage()); + } catch (EncapsulatedExceptions e) { + System.out.println("Failed to send (some) messages:"); + for (NetworkFailureException n : e.getNetworkExceptions()) { + System.out.println("Network failure for \"" + n.getE164number() + "\": " + n.getMessage()); + } + for (UnregisteredUserException n : e.getUnregisteredUserExceptions()) { + System.out.println("Unregistered user \"" + n.getE164Number() + "\": " + n.getMessage()); + } + for (UntrustedIdentityException n : e.getUntrustedIdentityExceptions()) { + System.out.println("Untrusted Identity for \"" + n.getE164Number() + "\": " + n.getMessage()); } } break; @@ -134,29 +186,53 @@ public class Main { System.exit(1); } try { - TextSecureContent content = m.receiveMessage(); - if (content.getDataMessage().isPresent()) { - message = content.getDataMessage().get(); - if (message == null) { - System.exit(0); - } else { - System.out.println("Received message: " + message.getBody().get()); - } - } - if (content.getSyncMessage().isPresent()) { - TextSecureSyncMessage syncMessage = content.getSyncMessage().get(); + m.receiveMessages(5, true, new Manager.ReceiveMessageHandler() { + @Override + public void handleMessage(TextSecureEnvelope envelope) { + System.out.println("Envelope from: " + envelope.getSource()); + System.out.println("Timestamp: " + envelope.getTimestamp()); - if (syncMessage == null) { - System.exit(0); - } else { - System.out.println("Received sync message"); + 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 { + if (content.getDataMessage().isPresent()) { + TextSecureDataMessage message = content.getDataMessage().get(); + System.out.println("Body: " + message.getBody().get()); + + if (message.isEndSession()) { + m.handleEndSession(envelope.getSource()); + } else 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) { - System.out.println("Receive message: " + e.getMessage()); + }); + } catch (IOException e) { + System.out.println("Error while receiving message: " + e.getMessage()); } break; } m.save(); + System.exit(0); } }