Browse Source

Checkpoint!

Douglas William Thrift 13 years ago
parent
commit
701db21913

+ 1 - 1
bigscreenbot

@@ -29,7 +29,7 @@ cp=(bin lib/*jar)
 cp=`IFS=':'; echo "${cp[*]}"`
 
 while [[ $code -eq 2 ]]; do
-	java -cp $cp net.douglasthrift.bigscreenbot.BigScreenBot
+	java -cp $cp net.douglasthrift.bigscreenbot.BigScreenBot "$@"
 
 	code=$?
 done

BIN
lib/commons-cli-1.2.jar


+ 74 - 48
src/net/douglasthrift/bigscreenbot/BigScreenBot.java

@@ -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'));
     }
 }
 

+ 27 - 0
src/net/douglasthrift/bigscreenbot/Remote.java

@@ -29,18 +29,27 @@ import java.io.IOException;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
 
+import java.security.KeyManagementException;
+import java.security.NoSuchAlgorithmException;
+
 import java.util.ArrayList;
 import java.util.List;
 
 import javax.jmdns.JmDNS;
 import javax.jmdns.ServiceInfo;
 
+import javax.net.ssl.SSLSocketFactory;
+import javax.net.ssl.KeyManager;
+import javax.net.ssl.KeyManagerFactory;
+
 import com.google.anymote.common.AnymoteFactory;
 import com.google.anymote.common.ErrorListener;
 
 import com.google.anymote.device.DeviceAdapter;
 import com.google.anymote.device.MessageReceiver;
 
+import com.google.polo.ssl.DummySSLSocketFactory;
+
 public class Remote
 {
     private static final String TYPE = "_anymote._tcp.local.";
@@ -69,11 +78,29 @@ public class Remote
         return devices;
     }
 
+    public void beginPairDevice(String device) throws NoSuchAlgorithmException, KeyManagementException
+    {
+        ServiceInfo info = mdns.getServiceInfo(TYPE, device);
+        InetAddress address = info.getInetAddresses()[0];
+        int port = info.getPort();
+
+        SSLSocketFactory factory = DummySSLSocketFactory.fromKeyManagers(getKeyManagers());
+    }
+
     @Override
     protected void finalize() throws IOException
     {
         mdns.close();
     }
+
+    private KeyManager[] getKeyManagers() throws NoSuchAlgorithmException
+    {
+        KeyManagerFactory factory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
+
+        factory.init(store, "".toCharArray());
+
+        return factory.getKeyManagers();
+    }
 }
 
 // vim: expandtab

+ 23 - 0
src/net/douglasthrift/bigscreenbot/Settings.java

@@ -24,12 +24,18 @@
 
 package net.douglasthrift.bigscreenbot;
 
+import java.io.FileNotFoundException;
+import java.io.FileReader;
+import java.io.IOException;
+
 import java.util.Arrays;
 import java.util.List;
 import java.util.Properties;
 
 public class Settings extends Properties
 {
+    private static final String SETTINGS = "bigscreenbot.properties";
+
     public boolean getBooleanProperty(String key, boolean defaultValue)
     {
         String value = getProperty(key);
@@ -59,6 +65,23 @@ public class Settings extends Properties
         else
             return null;
     }
+
+    public void load() throws FileNotFoundException, IOException
+    {
+        FileReader reader = null;
+
+        try
+        {
+            reader = new FileReader(SETTINGS);
+
+            load(reader);
+        }
+        finally
+        {
+            if (reader != null)
+                reader.close();
+        }
+    }
 }
 
 // vim: expandtab