Extract code from main() to functions
This commit is contained in:
parent
1c4578ceac
commit
6c02004326
@ -20,7 +20,6 @@ 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.textsecure.api.TextSecureMessageSender;
|
|
||||||
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
|
import org.whispersystems.textsecure.api.crypto.UntrustedIdentityException;
|
||||||
import org.whispersystems.textsecure.api.messages.*;
|
import org.whispersystems.textsecure.api.messages.*;
|
||||||
import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
|
import org.whispersystems.textsecure.api.messages.multidevice.TextSecureSyncMessage;
|
||||||
@ -46,39 +45,8 @@ public class Main {
|
|||||||
// Workaround for BKS truststore
|
// Workaround for BKS truststore
|
||||||
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
|
Security.insertProviderAt(new org.spongycastle.jce.provider.BouncyCastleProvider(), 1);
|
||||||
|
|
||||||
ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
|
Namespace ns = parseArgs(args);
|
||||||
.defaultHelp(true)
|
if (ns == null) {
|
||||||
.description("Commandline interface for TextSecure.");
|
|
||||||
Subparsers subparsers = parser.addSubparsers()
|
|
||||||
.title("subcommands")
|
|
||||||
.dest("command")
|
|
||||||
.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 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)
|
|
||||||
.help("Specify your phone number, that will be used for verification.");
|
|
||||||
Namespace ns = null;
|
|
||||||
try {
|
|
||||||
ns = parser.parseArgs(args);
|
|
||||||
} catch (ArgumentParserException e) {
|
|
||||||
parser.handleError(e);
|
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -88,10 +56,11 @@ public class Main {
|
|||||||
try {
|
try {
|
||||||
m.load();
|
m.load();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
System.out.println("Loading file error: " + e.getMessage());
|
System.out.println("Error loading state file \"" + m.getFileName() + "\": " + e.getMessage());
|
||||||
System.exit(2);
|
System.exit(2);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
switch (ns.getString("command")) {
|
switch (ns.getString("command")) {
|
||||||
case "register":
|
case "register":
|
||||||
if (!m.userHasKeys()) {
|
if (!m.userHasKeys()) {
|
||||||
@ -125,7 +94,6 @@ public class Main {
|
|||||||
System.out.println("User is not registered.");
|
System.out.println("User is not registered.");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
TextSecureMessageSender messageSender = m.getMessageSender();
|
|
||||||
String messageText = ns.getString("message");
|
String messageText = ns.getString("message");
|
||||||
if (messageText == null) {
|
if (messageText == null) {
|
||||||
try {
|
try {
|
||||||
@ -135,10 +103,11 @@ public class Main {
|
|||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
|
|
||||||
final List<String> attachments = ns.<String>getList("attachment");
|
final List<String> attachments = ns.<String>getList("attachment");
|
||||||
|
List<TextSecureAttachment> textSecureAttachments = null;
|
||||||
if (attachments != null) {
|
if (attachments != null) {
|
||||||
List<TextSecureAttachment> textSecureAttachments = new ArrayList<TextSecureAttachment>(attachments.size());
|
textSecureAttachments = new ArrayList<TextSecureAttachment>(attachments.size());
|
||||||
for (String attachment : attachments) {
|
for (String attachment : attachments) {
|
||||||
try {
|
try {
|
||||||
File attachmentFile = new File(attachment);
|
File attachmentFile = new File(attachment);
|
||||||
@ -148,23 +117,92 @@ public class Main {
|
|||||||
textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null));
|
textSecureAttachments.add(new TextSecureAttachmentStream(attachmentStream, mime, attachmentSize, null));
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
|
System.out.println("Failed to add attachment \"" + attachment + "\": " + e.getMessage());
|
||||||
|
System.out.println("Aborting sending.");
|
||||||
System.exit(1);
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
messageBuilder.withAttachments(textSecureAttachments);
|
|
||||||
}
|
}
|
||||||
TextSecureDataMessage message = messageBuilder.build();
|
|
||||||
|
|
||||||
List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
|
List<TextSecureAddress> recipients = new ArrayList<>(ns.<String>getList("recipient").size());
|
||||||
for (String recipient : ns.<String>getList("recipient")) {
|
for (String recipient : ns.<String>getList("recipient")) {
|
||||||
try {
|
try {
|
||||||
recipients.add(m.getPushAddress(recipient));
|
recipients.add(m.getPushAddress(recipient));
|
||||||
} catch (InvalidNumberException e) {
|
} catch (InvalidNumberException e) {
|
||||||
System.out.println("Failed to send message to \"" + recipient + "\": " + e.getMessage());
|
System.out.println("Failed to add recipient \"" + recipient + "\": " + e.getMessage());
|
||||||
|
System.out.println("Aborting sending.");
|
||||||
|
System.exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
sendMessage(m, messageText, textSecureAttachments, recipients);
|
||||||
|
break;
|
||||||
|
case "receive":
|
||||||
|
if (!m.isRegistered()) {
|
||||||
|
System.out.println("User is not registered.");
|
||||||
|
System.exit(1);
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
messageSender.sendMessage(recipients, message);
|
m.receiveMessages(5, true, new ReceiveMessageHandler(m));
|
||||||
|
} catch (IOException e) {
|
||||||
|
System.out.println("Error while receiving message: " + e.getMessage());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
m.save();
|
||||||
|
System.exit(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Namespace parseArgs(String[] args) {
|
||||||
|
ArgumentParser parser = ArgumentParsers.newArgumentParser("textsecure-cli")
|
||||||
|
.defaultHelp(true)
|
||||||
|
.description("Commandline interface for TextSecure.");
|
||||||
|
Subparsers subparsers = parser.addSubparsers()
|
||||||
|
.title("subcommands")
|
||||||
|
.dest("command")
|
||||||
|
.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 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)
|
||||||
|
.help("Specify your phone number, that will be used for verification.");
|
||||||
|
|
||||||
|
try {
|
||||||
|
return parser.parseArgs(args);
|
||||||
|
} catch (ArgumentParserException e) {
|
||||||
|
parser.handleError(e);
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void sendMessage(Manager m, String messageText, List<TextSecureAttachment> textSecureAttachments,
|
||||||
|
List<TextSecureAddress> recipients) {
|
||||||
|
final TextSecureDataMessage.Builder messageBuilder = TextSecureDataMessage.newBuilder().withBody(messageText);
|
||||||
|
if (textSecureAttachments != null) {
|
||||||
|
messageBuilder.withAttachments(textSecureAttachments);
|
||||||
|
}
|
||||||
|
TextSecureDataMessage message = messageBuilder.build();
|
||||||
|
|
||||||
|
try {
|
||||||
|
m.sendMessage(recipients, message);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
System.out.println("Failed to send message: " + e.getMessage());
|
System.out.println("Failed to send message: " + e.getMessage());
|
||||||
} catch (EncapsulatedExceptions e) {
|
} catch (EncapsulatedExceptions e) {
|
||||||
@ -179,14 +217,15 @@ public class Main {
|
|||||||
System.out.println("Untrusted Identity for \"" + n.getE164Number() + "\": " + n.getMessage());
|
System.out.println("Untrusted Identity for \"" + n.getE164Number() + "\": " + n.getMessage());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
|
||||||
case "receive":
|
|
||||||
if (!m.isRegistered()) {
|
|
||||||
System.out.println("User is not registered.");
|
|
||||||
System.exit(1);
|
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
m.receiveMessages(5, true, new Manager.ReceiveMessageHandler() {
|
private static class ReceiveMessageHandler implements Manager.ReceiveMessageHandler {
|
||||||
|
Manager m;
|
||||||
|
|
||||||
|
public ReceiveMessageHandler(Manager m) {
|
||||||
|
this.m = m;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void handleMessage(TextSecureEnvelope envelope) {
|
public void handleMessage(TextSecureEnvelope envelope) {
|
||||||
System.out.println("Envelope from: " + envelope.getSource());
|
System.out.println("Envelope from: " + envelope.getSource());
|
||||||
@ -226,13 +265,5 @@ public class Main {
|
|||||||
}
|
}
|
||||||
System.out.println();
|
System.out.println();
|
||||||
}
|
}
|
||||||
});
|
|
||||||
} catch (IOException e) {
|
|
||||||
System.out.println("Error while receiving message: " + e.getMessage());
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
m.save();
|
|
||||||
System.exit(0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -35,9 +35,11 @@ import org.whispersystems.textsecure.api.TextSecureMessageReceiver;
|
|||||||
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
import org.whispersystems.textsecure.api.TextSecureMessageSender;
|
||||||
import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
|
import org.whispersystems.textsecure.api.crypto.TextSecureCipher;
|
||||||
import org.whispersystems.textsecure.api.messages.TextSecureContent;
|
import org.whispersystems.textsecure.api.messages.TextSecureContent;
|
||||||
|
import org.whispersystems.textsecure.api.messages.TextSecureDataMessage;
|
||||||
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.push.exceptions.EncapsulatedExceptions;
|
||||||
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
import org.whispersystems.textsecure.api.util.InvalidNumberException;
|
||||||
import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
|
import org.whispersystems.textsecure.api.util.PhoneNumberFormatter;
|
||||||
|
|
||||||
@ -68,7 +70,7 @@ public class Manager {
|
|||||||
this.username = username;
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getFileName() {
|
public String getFileName() {
|
||||||
String path = settingsPath + "/data";
|
String path = settingsPath + "/data";
|
||||||
new File(path).mkdirs();
|
new File(path).mkdirs();
|
||||||
return path + "/" + username;
|
return path + "/" + username;
|
||||||
@ -218,9 +220,11 @@ public class Manager {
|
|||||||
accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
|
accountManager.setPreKeys(axolotlStore.getIdentityKeyPair().getPublicKey(), lastResortKey, signedPreKeyRecord, oneTimePreKeys);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextSecureMessageSender getMessageSender() {
|
public void sendMessage(List<TextSecureAddress> recipients, TextSecureDataMessage message)
|
||||||
return new TextSecureMessageSender(URL, TRUST_STORE, username, password,
|
throws IOException, EncapsulatedExceptions {
|
||||||
|
TextSecureMessageSender messageSender = new TextSecureMessageSender(URL, TRUST_STORE, username, password,
|
||||||
axolotlStore, Optional.<TextSecureMessageSender.EventListener>absent());
|
axolotlStore, Optional.<TextSecureMessageSender.EventListener>absent());
|
||||||
|
messageSender.sendMessage(recipients, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
public TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
|
public TextSecureContent decryptMessage(TextSecureEnvelope envelope) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user