Permalink
Browse files

First-run setup

  • Loading branch information...
1 parent af3103b commit ea7fc63c6174ec16c106f23878a46d3bbfef4958 @Shockah Shockah committed May 10, 2012
Showing with 64 additions and 16 deletions.
  1. +62 −2 src/pl/shockah/shocky/Data.java
  2. +2 −14 src/pl/shockah/shocky/Shocky.java
@@ -1,22 +1,51 @@
package pl.shockah.shocky;
+import java.io.Console;
import java.io.File;
import java.util.ArrayList;
import pl.shockah.Config;
import pl.shockah.FileLine;
+import pl.shockah.StringTools;
public class Data {
public static final Config config = new Config();
public static final ArrayList<String> controllers = new ArrayList<String>();
public static final ArrayList<String> channels = new ArrayList<String>();
public static final ArrayList<String> blacklistNicks = new ArrayList<String>();
- public static synchronized void blank() {}
+ public static synchronized void blank() {
+ Data.config.setNotExists("main-botname","Shocky");
+ Data.config.setNotExists("main-server","irc.esper.net");
+ Data.config.setNotExists("main-version","Shocky - PircBotX 1.6 - https://github.com/clone1018/Shocky - http://pircbotx.googlecode.com");
+ Data.config.setNotExists("main-verbose",false);
+ Data.config.setNotExists("main-maxchannels",10);
+ Data.config.setNotExists("main-nickservpass","");
+ Data.config.setNotExists("main-cmdchar","`~");
+ /*Data.config.setNotExists("main-sqlurl","http://localhost/shocky/sql.php");
+ Data.config.setNotExists("main-sqlhost","localhost");
+ Data.config.setNotExists("main-sqluser","");
+ Data.config.setNotExists("main-sqlpass","");
+ Data.config.setNotExists("main-sqldb","shocky");
+ Data.config.setNotExists("main-sqlprefix","");*/
+
+ Console c = System.console();
+ if (c == null) System.out.println("--- Not running in console, using default first-run settings ---"); else {
+ System.out.println("--- First-run setup ---\n(just press Enter for default value)\n");
+
+ firstRunSetupString(c,"main-server");
+ firstRunSetupBoolean(c,"main-verbose");
+ firstRunSetupString(c,"main-version");
+ firstRunSetupPassword(c,"main-nickservpass");
+ firstRunSetupInt(c,"main-maxchannels");
+ firstRunSetupString(c,"main-cmdchar");
+ System.out.println();
+ }
+ }
public static synchronized void load() {
File dir = new File("data"); dir.mkdir();
config.load(new File(dir,"config.cfg"));
- if (new File(dir,"controllers.cfg").exists()) {
+ if (new File(dir,"config.cfg").exists()) {
controllers.addAll(FileLine.read(new File(dir,"controllers.cfg")));
channels.addAll(FileLine.read(new File(dir,"channels.cfg")));
blacklistNicks.addAll(FileLine.read(new File(dir,"blacklistNicks.cfg")));
@@ -32,4 +61,35 @@ public static synchronized void save() {
for (Module module : Module.getModules(false)) module.onDataSave();
}
+
+ private static void firstRunSetupString(Console c, String key) {
+ String input = c.readLine(key+" (def: "+Data.config.getString(key)+"): ");
+ if (input != null && !input.isEmpty()) Data.config.set(key,input);
+ }
+ private static void firstRunSetupPassword(Console c, String key) {
+ String input = new String(c.readPassword(key+" (def: "+Data.config.getString(key)+"): "));
+ if (input != null && !input.isEmpty()) Data.config.set(key,input);
+ }
+ private static void firstRunSetupBoolean(Console c, String key) {
+ while (true) {
+ String input = c.readLine(key+" (def: "+Data.config.getString(key)+"): ");
+ if (input != null && !input.isEmpty()) {
+ if (input.toLowerCase().matches("^(t(rue)?)|(f(alse)?)$")) {
+ Data.config.set(key,input);
+ return;
+ }
+ } else return;
+ }
+ }
+ private static void firstRunSetupInt(Console c, String key) {
+ while (true) {
+ String input = c.readLine(key+" (def: "+Data.config.getString(key)+"): ");
+ if (input != null && !input.isEmpty()) {
+ if (StringTools.isNumber(input)) {
+ Data.config.set(key,input);
+ return;
+ }
+ } else return;
+ }
+ }
}
@@ -16,20 +16,6 @@
public static void main(String[] args) {
Data.load();
- Data.config.setNotExists("main-botname","Shocky");
- Data.config.setNotExists("main-server","irc.esper.net");
- Data.config.setNotExists("main-version","Shocky - PircBotX 1.6 - https://github.com/clone1018/Shocky - http://pircbotx.googlecode.com");
- Data.config.setNotExists("main-verbose",false);
- Data.config.setNotExists("main-maxchannels",10);
- Data.config.setNotExists("main-nickservpass","");
- Data.config.setNotExists("main-cmdchar","`~");
- Data.config.setNotExists("main-sqlurl","http://localhost/shocky/sql.php");
- Data.config.setNotExists("main-sqlhost","localhost");
- Data.config.setNotExists("main-sqluser","");
- Data.config.setNotExists("main-sqlpass","");
- Data.config.setNotExists("main-sqldb","shocky");
- Data.config.setNotExists("main-sqlprefix","");
-
multiBot = new MultiBotManager(Data.config.getString("main-botname"));
try {
multiBot.setName(Data.config.getString("main-botname"));
@@ -44,6 +30,8 @@ public static void main(String[] args) {
timed = new TimedActions();
Module.loadNewModules();
+ System.out.println("--- Shocky, the IRC bot, up and running! ---");
+ System.out.println("--- type \"help\" to list all available commands ---");
try {
MultiChannel.join(Data.channels.toArray(new String[0]));
} catch (Exception e) {e.printStackTrace();}

0 comments on commit ea7fc63

Please sign in to comment.