|
@@ -24,10 +24,10 @@
|
|
|
|
|
|
package net.douglasthrift.bigscreenbot;
|
|
|
|
|
|
-import java.io.FileNotFoundException;
|
|
|
-import java.io.FileReader;
|
|
|
import java.io.IOException;
|
|
|
|
|
|
+import java.security.GeneralSecurityException;
|
|
|
+
|
|
|
import java.util.ArrayList;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
@@ -37,12 +37,17 @@ import java.util.regex.Pattern;
|
|
|
|
|
|
import javax.net.ssl.SSLSocketFactory;
|
|
|
|
|
|
+import org.apache.commons.cli.CommandLine;
|
|
|
+import org.apache.commons.cli.GnuParser;
|
|
|
+import org.apache.commons.cli.HelpFormatter;
|
|
|
+import org.apache.commons.cli.Options;
|
|
|
+import org.apache.commons.cli.ParseException;
|
|
|
+
|
|
|
import org.jibble.pircbot.Colors;
|
|
|
import org.jibble.pircbot.IrcException;
|
|
|
|
|
|
public class BigScreenBot extends Bot
|
|
|
{
|
|
|
- private static final String SETTINGS = "bigscreenbot.properties";
|
|
|
private static final int CHANNEL = 0x1;
|
|
|
private static final int PRIVATE = 0x2;
|
|
|
private static final int BOTH = 0x3;
|
|
@@ -96,47 +101,23 @@ public class BigScreenBot extends Bot
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private boolean verbose;
|
|
|
private Settings settings = new Settings();
|
|
|
private Remote remote;
|
|
|
private List<Pattern> admins = new ArrayList<Pattern>();
|
|
|
private Map<String, Command> commands = new TreeMap<String, Command>();
|
|
|
private Action action = Action.RECONNECT;
|
|
|
|
|
|
- private BigScreenBot()
|
|
|
+ private BigScreenBot(boolean verbose)
|
|
|
{
|
|
|
super();
|
|
|
|
|
|
- FileReader reader = null;
|
|
|
+ this.verbose = verbose;
|
|
|
|
|
|
try
|
|
|
{
|
|
|
- reader = new FileReader(SETTINGS);
|
|
|
+ settings.load();
|
|
|
|
|
|
- settings.load(reader);
|
|
|
- }
|
|
|
- catch (FileNotFoundException exception)
|
|
|
- {
|
|
|
- error(exception, 1);
|
|
|
- }
|
|
|
- catch (IOException exception)
|
|
|
- {
|
|
|
- error(exception, 1);
|
|
|
- }
|
|
|
- finally
|
|
|
- {
|
|
|
- if (reader != null)
|
|
|
- try
|
|
|
- {
|
|
|
- reader.close();
|
|
|
- }
|
|
|
- catch (IOException exception)
|
|
|
- {
|
|
|
- error(exception, 1);
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- try
|
|
|
- {
|
|
|
remote = new Remote(settings);
|
|
|
}
|
|
|
catch (IOException exception)
|
|
@@ -157,7 +138,7 @@ public class BigScreenBot extends Bot
|
|
|
|
|
|
setLogin(System.getProperty("user.name"));
|
|
|
setName(settings.getProperty("nick", "bigscreenbot"));
|
|
|
- setVerbose(settings.getBooleanProperty("verbose", false));
|
|
|
+ setVerbose(verbose);
|
|
|
|
|
|
for (String admin : settings.getListProperty("admins"))
|
|
|
admins.add(compileNickMask(admin));
|
|
@@ -213,7 +194,7 @@ public class BigScreenBot extends Bot
|
|
|
@Override
|
|
|
public void execute(final String channel, final String sender, boolean admin, String argument)
|
|
|
{
|
|
|
- String[] arguments = argument.split("\\s", 2);
|
|
|
+ final String[] arguments = argument.split("\\s", 2);
|
|
|
|
|
|
if (arguments.length == 1)
|
|
|
if (arguments[0].isEmpty())
|
|
@@ -243,8 +224,24 @@ public class BigScreenBot extends Bot
|
|
|
}
|
|
|
}.start();
|
|
|
else
|
|
|
- {
|
|
|
- }
|
|
|
+ new Thread()
|
|
|
+ {
|
|
|
+ @Override
|
|
|
+ public void run()
|
|
|
+ {
|
|
|
+ synchronized (remote)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ remote.beginPairDevice(arguments[0]);
|
|
|
+ }
|
|
|
+ catch (GeneralSecurityException exception)
|
|
|
+ {
|
|
|
+ error(channel, sender, exception);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }.start();
|
|
|
else
|
|
|
{
|
|
|
}
|
|
@@ -260,10 +257,7 @@ public class BigScreenBot extends Bot
|
|
|
action = Action.QUIT;
|
|
|
}
|
|
|
|
|
|
- if (!argument.isEmpty())
|
|
|
- quitServer(argument);
|
|
|
- else
|
|
|
- quitServer();
|
|
|
+ quitServer(!argument.isEmpty() ? argument : "oh no!");
|
|
|
}
|
|
|
});
|
|
|
commands.put("restart", new Command(true, PRIVATE, "", "quit and join running more up to date code")
|
|
@@ -435,26 +429,58 @@ public class BigScreenBot extends Bot
|
|
|
command.execute(channel, sender, admin, arguments.length == 2 ? arguments[1] : "");
|
|
|
}
|
|
|
|
|
|
- private void help(String channel, String sender, boolean admin, String command)
|
|
|
+ private void error(Exception exception)
|
|
|
{
|
|
|
- commands.get("help").execute(channel, sender, admin, command);
|
|
|
+ if (verbose)
|
|
|
+ exception.printStackTrace();
|
|
|
+ else
|
|
|
+ System.err.println("bigscreenbot: " + exception.getMessage());
|
|
|
}
|
|
|
|
|
|
- public static void main(String[] args)
|
|
|
+ private void error(Exception exception, int code)
|
|
|
{
|
|
|
- new BigScreenBot();
|
|
|
+ error(exception);
|
|
|
+
|
|
|
+ System.exit(code);
|
|
|
}
|
|
|
|
|
|
- private static void error(Exception exception)
|
|
|
+ private void error(String channel, String sender, Exception exception)
|
|
|
{
|
|
|
- System.err.println("bigscreenbot: " + exception.getMessage());
|
|
|
+ error(exception);
|
|
|
+ sendMessage(channel, sender, "an error occurred");
|
|
|
}
|
|
|
|
|
|
- private static void error(Exception exception, int code)
|
|
|
+ private void help(String channel, String sender, boolean admin, String command)
|
|
|
{
|
|
|
- error(exception);
|
|
|
+ commands.get("help").execute(channel, sender, admin, command);
|
|
|
+ }
|
|
|
|
|
|
- System.exit(code);
|
|
|
+ public static void main(String[] args)
|
|
|
+ {
|
|
|
+ Options options = new Options();
|
|
|
+
|
|
|
+ options.addOption("h", "help", false, "show this help message and exit");
|
|
|
+ options.addOption("v", "verbose", false, "");
|
|
|
+
|
|
|
+ CommandLine line = null;
|
|
|
+
|
|
|
+ try
|
|
|
+ {
|
|
|
+ line = new GnuParser().parse(options, args);
|
|
|
+ }
|
|
|
+ catch (ParseException exception)
|
|
|
+ {
|
|
|
+ System.err.println("bigscreenbot: " + exception.getMessage());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (line.hasOption('h'))
|
|
|
+ {
|
|
|
+ new HelpFormatter().printHelp("bigscreenbot", options, true);
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ new BigScreenBot(line.hasOption('v'));
|
|
|
}
|
|
|
}
|
|
|
|