Code cleanup
This commit is contained in:
@ -17,31 +17,33 @@
|
||||
package ch.dissem.bitmessage.demo;
|
||||
|
||||
import ch.dissem.bitmessage.BitmessageContext;
|
||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||
import ch.dissem.bitmessage.entity.Plaintext;
|
||||
import ch.dissem.bitmessage.entity.payload.Pubkey;
|
||||
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
|
||||
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
||||
import ch.dissem.bitmessage.repository.*;
|
||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.InetAddress;
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
import static ch.dissem.bitmessage.demo.CommandLine.COMMAND_BACK;
|
||||
import static ch.dissem.bitmessage.demo.CommandLine.ERROR_UNKNOWN_COMMAND;
|
||||
|
||||
/**
|
||||
* A simple command line Bitmessage application
|
||||
*/
|
||||
public class Application {
|
||||
private final static Logger LOG = LoggerFactory.getLogger(Application.class);
|
||||
private final Scanner scanner;
|
||||
private final CommandLine commandLine;
|
||||
|
||||
private BitmessageContext ctx;
|
||||
|
||||
public Application(String syncServer, int syncPort) {
|
||||
public Application(InetAddress syncServer, int syncPort) {
|
||||
JdbcConfig jdbcConfig = new JdbcConfig();
|
||||
ctx = new BitmessageContext.Builder()
|
||||
.addressRepo(new JdbcAddressRepository(jdbcConfig))
|
||||
@ -68,7 +70,7 @@ public class Application {
|
||||
ctx.startup();
|
||||
}
|
||||
|
||||
scanner = new Scanner(System.in);
|
||||
commandLine = new CommandLine();
|
||||
|
||||
String command;
|
||||
do {
|
||||
@ -84,7 +86,7 @@ public class Application {
|
||||
System.out.println("?) info");
|
||||
System.out.println("e) exit");
|
||||
|
||||
command = nextCommand();
|
||||
command = commandLine.nextCommand();
|
||||
try {
|
||||
switch (command) {
|
||||
case "i": {
|
||||
@ -106,10 +108,12 @@ public class Application {
|
||||
case "e":
|
||||
break;
|
||||
case "y":
|
||||
ctx.synchronize(InetAddress.getByName(syncServer), syncPort, 120, true);
|
||||
if (syncServer != null) {
|
||||
ctx.synchronize(syncServer, syncPort, 120, true);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
System.out.println("Unknown command. Please try again.");
|
||||
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.debug(e.getMessage());
|
||||
@ -125,32 +129,16 @@ public class Application {
|
||||
System.out.println(ctx.status());
|
||||
}
|
||||
|
||||
private String nextCommand() {
|
||||
return scanner.nextLine().trim().toLowerCase();
|
||||
}
|
||||
|
||||
private void identities() {
|
||||
String command;
|
||||
List<BitmessageAddress> identities = ctx.addresses().getIdentities();
|
||||
do {
|
||||
System.out.println();
|
||||
int i = 0;
|
||||
for (BitmessageAddress identity : identities) {
|
||||
i++;
|
||||
System.out.print(i + ") ");
|
||||
if (identity.getAlias() != null) {
|
||||
System.out.println(identity.getAlias() + " (" + identity.getAddress() + ")");
|
||||
} else {
|
||||
System.out.println(identity.getAddress());
|
||||
}
|
||||
}
|
||||
if (i == 0) {
|
||||
System.out.println("You have no identities yet.");
|
||||
}
|
||||
commandLine.listAddresses(identities, "identities");
|
||||
System.out.println("a) create identity");
|
||||
System.out.println("b) back");
|
||||
System.out.println(COMMAND_BACK);
|
||||
|
||||
command = nextCommand();
|
||||
command = commandLine.nextCommand();
|
||||
switch (command) {
|
||||
case "a":
|
||||
addIdentity();
|
||||
@ -163,7 +151,7 @@ public class Application {
|
||||
int index = Integer.parseInt(command) - 1;
|
||||
address(identities.get(index));
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Unknown command. Please try again.");
|
||||
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||
}
|
||||
}
|
||||
} while (!"b".equals(command));
|
||||
@ -171,9 +159,9 @@ public class Application {
|
||||
|
||||
private void addIdentity() {
|
||||
System.out.println();
|
||||
BitmessageAddress identity = ctx.createIdentity(yesNo("would you like a shorter address? This will take some time to calculate."), Pubkey.Feature.DOES_ACK);
|
||||
BitmessageAddress identity = ctx.createIdentity(commandLine.yesNo("would you like a shorter address? This will take some time to calculate."), Pubkey.Feature.DOES_ACK);
|
||||
System.out.println("Please enter an alias for this identity, or an empty string for none");
|
||||
String alias = scanner.nextLine().trim();
|
||||
String alias = commandLine.nextLineTrimmed();
|
||||
if (alias.length() > 0) {
|
||||
identity.setAlias(alias);
|
||||
}
|
||||
@ -185,24 +173,12 @@ public class Application {
|
||||
List<BitmessageAddress> contacts = ctx.addresses().getContacts();
|
||||
do {
|
||||
System.out.println();
|
||||
int i = 0;
|
||||
for (BitmessageAddress contact : contacts) {
|
||||
i++;
|
||||
System.out.print(i + ") ");
|
||||
if (contact.getAlias() != null) {
|
||||
System.out.println(contact.getAlias() + " (" + contact.getAddress() + ")");
|
||||
} else {
|
||||
System.out.println(contact.getAddress());
|
||||
}
|
||||
}
|
||||
if (i == 0) {
|
||||
System.out.println("You have no contacts yet.");
|
||||
}
|
||||
commandLine.listAddresses(contacts, "contacts");
|
||||
System.out.println();
|
||||
System.out.println("a) add contact");
|
||||
System.out.println("b) back");
|
||||
System.out.println(COMMAND_BACK);
|
||||
|
||||
command = nextCommand();
|
||||
command = commandLine.nextCommand();
|
||||
switch (command) {
|
||||
case "a":
|
||||
addContact(false);
|
||||
@ -215,7 +191,7 @@ public class Application {
|
||||
int index = Integer.parseInt(command) - 1;
|
||||
address(contacts.get(index));
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Unknown command. Please try again.");
|
||||
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||
}
|
||||
}
|
||||
} while (!"b".equals(command));
|
||||
@ -225,9 +201,9 @@ public class Application {
|
||||
System.out.println();
|
||||
System.out.println("Please enter the Bitmessage address you want to add");
|
||||
try {
|
||||
BitmessageAddress address = new BitmessageAddress(scanner.nextLine().trim());
|
||||
BitmessageAddress address = new BitmessageAddress(commandLine.nextLineTrimmed());
|
||||
System.out.println("Please enter an alias for this address, or an empty string for none");
|
||||
String alias = scanner.nextLine().trim();
|
||||
String alias = commandLine.nextLineTrimmed();
|
||||
if (alias.length() > 0) {
|
||||
address.setAlias(alias);
|
||||
}
|
||||
@ -245,24 +221,12 @@ public class Application {
|
||||
List<BitmessageAddress> subscriptions = ctx.addresses().getSubscriptions();
|
||||
do {
|
||||
System.out.println();
|
||||
int i = 0;
|
||||
for (BitmessageAddress contact : subscriptions) {
|
||||
i++;
|
||||
System.out.print(i + ") ");
|
||||
if (contact.getAlias() != null) {
|
||||
System.out.println(contact.getAlias() + " (" + contact.getAddress() + ")");
|
||||
} else {
|
||||
System.out.println(contact.getAddress());
|
||||
}
|
||||
}
|
||||
if (i == 0) {
|
||||
System.out.println("You have no subscriptions yet.");
|
||||
}
|
||||
commandLine.listAddresses(subscriptions, "subscriptions");
|
||||
System.out.println();
|
||||
System.out.println("a) add subscription");
|
||||
System.out.println("b) back");
|
||||
System.out.println(COMMAND_BACK);
|
||||
|
||||
command = nextCommand();
|
||||
command = commandLine.nextCommand();
|
||||
switch (command) {
|
||||
case "a":
|
||||
addContact(true);
|
||||
@ -275,7 +239,7 @@ public class Application {
|
||||
int index = Integer.parseInt(command) - 1;
|
||||
address(subscriptions.get(index));
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Unknown command. Please try again.");
|
||||
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||
}
|
||||
}
|
||||
} while (!"b".equals(command));
|
||||
@ -313,9 +277,9 @@ public class Application {
|
||||
System.out.println();
|
||||
System.out.println("c) compose message");
|
||||
System.out.println("s) compose broadcast");
|
||||
System.out.println("b) back");
|
||||
System.out.println(COMMAND_BACK);
|
||||
|
||||
command = scanner.nextLine().trim();
|
||||
command = commandLine.nextCommand();
|
||||
switch (command) {
|
||||
case "c":
|
||||
compose(false);
|
||||
@ -330,7 +294,7 @@ public class Application {
|
||||
int index = Integer.parseInt(command) - 1;
|
||||
show(messages.get(index));
|
||||
} catch (NumberFormatException | IndexOutOfBoundsException e) {
|
||||
System.out.println("Unknown command. Please try again.");
|
||||
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||
}
|
||||
}
|
||||
} while (!"b".equalsIgnoreCase(command));
|
||||
@ -350,8 +314,8 @@ public class Application {
|
||||
do {
|
||||
System.out.println("r) reply");
|
||||
System.out.println("d) delete");
|
||||
System.out.println("b) back");
|
||||
command = nextCommand();
|
||||
System.out.println(COMMAND_BACK);
|
||||
command = commandLine.nextCommand();
|
||||
switch (command) {
|
||||
case "r":
|
||||
compose(message.getTo(), message.getFrom(), "RE: " + message.getSubject());
|
||||
@ -361,18 +325,18 @@ public class Application {
|
||||
case "b":
|
||||
return;
|
||||
default:
|
||||
System.out.println("Unknown command. Please try again.");
|
||||
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||
}
|
||||
} while (!"b".equalsIgnoreCase(command));
|
||||
}
|
||||
|
||||
private void compose(boolean broadcast) {
|
||||
System.out.println();
|
||||
BitmessageAddress from = selectAddress(true);
|
||||
BitmessageAddress from = selectIdentity();
|
||||
if (from == null) {
|
||||
return;
|
||||
}
|
||||
BitmessageAddress to = (broadcast ? null : selectAddress(false));
|
||||
BitmessageAddress to = (broadcast ? null : selectContact());
|
||||
if (!broadcast && to == null) {
|
||||
return;
|
||||
}
|
||||
@ -380,58 +344,22 @@ public class Application {
|
||||
compose(from, to, null);
|
||||
}
|
||||
|
||||
private BitmessageAddress selectAddress(boolean id) {
|
||||
List<BitmessageAddress> addresses = (id ? ctx.addresses().getIdentities() : ctx.addresses().getContacts());
|
||||
private BitmessageAddress selectIdentity() {
|
||||
List<BitmessageAddress> addresses = ctx.addresses().getIdentities();
|
||||
while (addresses.size() == 0) {
|
||||
if (id) {
|
||||
addIdentity();
|
||||
addresses = ctx.addresses().getIdentities();
|
||||
} else {
|
||||
addContact(false);
|
||||
addresses = ctx.addresses().getContacts();
|
||||
}
|
||||
addIdentity();
|
||||
addresses = ctx.addresses().getIdentities();
|
||||
}
|
||||
if (addresses.size() == 1) {
|
||||
return addresses.get(0);
|
||||
return commandLine.selectAddress(addresses, "From:");
|
||||
}
|
||||
|
||||
private BitmessageAddress selectContact() {
|
||||
List<BitmessageAddress> addresses = ctx.addresses().getContacts();
|
||||
while (addresses.size() == 0) {
|
||||
addContact(false);
|
||||
addresses = ctx.addresses().getContacts();
|
||||
}
|
||||
|
||||
String command;
|
||||
do {
|
||||
System.out.println();
|
||||
if (id) {
|
||||
System.out.println("From:");
|
||||
} else {
|
||||
System.out.println("To:");
|
||||
}
|
||||
|
||||
int i = 0;
|
||||
for (BitmessageAddress identity : addresses) {
|
||||
i++;
|
||||
System.out.print(i + ") ");
|
||||
if (identity.getAlias() != null) {
|
||||
System.out.println(identity.getAlias() + " (" + identity.getAddress() + ")");
|
||||
} else {
|
||||
System.out.println(identity.getAddress());
|
||||
}
|
||||
}
|
||||
System.out.println("b) back");
|
||||
|
||||
command = nextCommand();
|
||||
switch (command) {
|
||||
case "b":
|
||||
return null;
|
||||
default:
|
||||
try {
|
||||
int index = Integer.parseInt(command) - 1;
|
||||
if (addresses.get(index) != null) {
|
||||
return addresses.get(index);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println("Unknown command. Please try again.");
|
||||
}
|
||||
}
|
||||
} while (!"b".equals(command));
|
||||
return null;
|
||||
return commandLine.selectAddress(addresses, "To:");
|
||||
}
|
||||
|
||||
private void compose(BitmessageAddress from, BitmessageAddress to, String subject) {
|
||||
@ -445,29 +373,19 @@ public class Application {
|
||||
System.out.println("Subject: " + subject);
|
||||
} else {
|
||||
System.out.print("Subject: ");
|
||||
subject = scanner.nextLine().trim();
|
||||
subject = commandLine.nextLineTrimmed();
|
||||
}
|
||||
System.out.println("Message:");
|
||||
StringBuilder message = new StringBuilder();
|
||||
String line;
|
||||
do {
|
||||
line = scanner.nextLine();
|
||||
line = commandLine.nextLine();
|
||||
message.append(line).append('\n');
|
||||
} while (line.length() > 0 || !yesNo("Send message?"));
|
||||
} while (line.length() > 0 || !commandLine.yesNo("Send message?"));
|
||||
if (broadcast) {
|
||||
ctx.broadcast(from, subject, message.toString());
|
||||
} else {
|
||||
ctx.send(from, to, subject, message.toString());
|
||||
}
|
||||
}
|
||||
|
||||
private boolean yesNo(String question) {
|
||||
String answer;
|
||||
do {
|
||||
System.out.println(question + " (y/n)");
|
||||
answer = scanner.nextLine();
|
||||
if ("y".equalsIgnoreCase(answer)) return true;
|
||||
if ("n".equalsIgnoreCase(answer)) return false;
|
||||
} while (true);
|
||||
}
|
||||
}
|
||||
|
102
demo/src/main/java/ch/dissem/bitmessage/demo/CommandLine.java
Normal file
102
demo/src/main/java/ch/dissem/bitmessage/demo/CommandLine.java
Normal file
@ -0,0 +1,102 @@
|
||||
/*
|
||||
* Copyright 2016 Christian Basler
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package ch.dissem.bitmessage.demo;
|
||||
|
||||
import ch.dissem.bitmessage.entity.BitmessageAddress;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Scanner;
|
||||
|
||||
|
||||
/**
|
||||
* @author Christian Basler
|
||||
*/
|
||||
public class CommandLine {
|
||||
public static final String COMMAND_BACK = "b) back";
|
||||
public static final String ERROR_UNKNOWN_COMMAND = "Unknown command. Please try again.";
|
||||
|
||||
private Scanner scanner = new Scanner(System.in);
|
||||
|
||||
public String nextCommand() {
|
||||
return scanner.nextLine().trim().toLowerCase();
|
||||
}
|
||||
|
||||
public String nextLine() {
|
||||
return scanner.nextLine();
|
||||
}
|
||||
|
||||
public String nextLineTrimmed() {
|
||||
return scanner.nextLine();
|
||||
}
|
||||
|
||||
public boolean yesNo(String question) {
|
||||
String answer;
|
||||
do {
|
||||
System.out.println(question + " (y/n)");
|
||||
answer = scanner.nextLine();
|
||||
if ("y".equalsIgnoreCase(answer)) return true;
|
||||
if ("n".equalsIgnoreCase(answer)) return false;
|
||||
} while (true);
|
||||
}
|
||||
|
||||
public BitmessageAddress selectAddress(List<BitmessageAddress> addresses, String label) {
|
||||
if (addresses.size() == 1) {
|
||||
return addresses.get(0);
|
||||
}
|
||||
|
||||
String command;
|
||||
do {
|
||||
System.out.println();
|
||||
System.out.println(label);
|
||||
|
||||
listAddresses(addresses, "contacts");
|
||||
System.out.println(COMMAND_BACK);
|
||||
|
||||
command = nextCommand();
|
||||
switch (command) {
|
||||
case "b":
|
||||
return null;
|
||||
default:
|
||||
try {
|
||||
int index = Integer.parseInt(command) - 1;
|
||||
if (addresses.get(index) != null) {
|
||||
return addresses.get(index);
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
System.out.println(ERROR_UNKNOWN_COMMAND);
|
||||
}
|
||||
}
|
||||
} while (!"b".equals(command));
|
||||
return null;
|
||||
}
|
||||
|
||||
public void listAddresses(List<BitmessageAddress> addresses, String kind) {
|
||||
int i = 0;
|
||||
for (BitmessageAddress address : addresses) {
|
||||
i++;
|
||||
System.out.print(i + ") ");
|
||||
if (address.getAlias() == null) {
|
||||
System.out.println(address.getAddress());
|
||||
} else {
|
||||
System.out.println(address.getAlias() + " (" + address.getAddress() + ")");
|
||||
}
|
||||
}
|
||||
if (i == 0) {
|
||||
System.out.println("You have no " + kind + " yet.");
|
||||
}
|
||||
}
|
||||
}
|
@ -17,10 +17,10 @@
|
||||
package ch.dissem.bitmessage.demo;
|
||||
|
||||
import ch.dissem.bitmessage.BitmessageContext;
|
||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
||||
import ch.dissem.bitmessage.networking.DefaultNetworkHandler;
|
||||
import ch.dissem.bitmessage.ports.MemoryNodeRegistry;
|
||||
import ch.dissem.bitmessage.repository.*;
|
||||
import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography;
|
||||
import ch.dissem.bitmessage.wif.WifExporter;
|
||||
import ch.dissem.bitmessage.wif.WifImporter;
|
||||
import org.kohsuke.args4j.CmdLineException;
|
||||
@ -29,6 +29,7 @@ import org.kohsuke.args4j.Option;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.InetAddress;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args) throws IOException {
|
||||
@ -64,7 +65,8 @@ public class Main {
|
||||
new WifImporter(ctx, options.importWIF).importAll();
|
||||
}
|
||||
} else {
|
||||
new Application(options.syncServer, options.syncPort);
|
||||
InetAddress syncServer = options.syncServer == null ? null : InetAddress.getByName(options.syncServer);
|
||||
new Application(syncServer, options.syncPort);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user