Browse Source

Rearrange thigns and start work on Google TV pairing.

Douglas William Thrift 13 years ago
parent
commit
95b4c483df

+ 77 - 136
src/net/douglasthrift/bigscreenbot/BigScreenBot.java

@@ -28,39 +28,19 @@ import java.io.FileNotFoundException;
 import java.io.FileReader;
 import java.io.IOException;
 
-import java.net.InetAddress;
-import java.net.UnknownHostException;
-
 import java.util.ArrayList;
-import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 import java.util.TreeMap;
 
 import java.util.regex.Pattern;
 
 import javax.net.ssl.SSLSocketFactory;
 
-import javax.jmdns.JmDNS;
-import javax.jmdns.ServiceEvent;
-import javax.jmdns.ServiceInfo;
-import javax.jmdns.ServiceListener;
-
-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 org.apache.oro.text.GlobCompiler;
-
 import org.jibble.pircbot.Colors;
 import org.jibble.pircbot.IrcException;
-import org.jibble.pircbot.PircBot;
-import org.jibble.pircbot.User;
 
-public class BigScreenBot extends PircBot
+public class BigScreenBot extends Bot
 {
     private static final String SETTINGS = "bigscreenbot.properties";
     private static final int CHANNEL = 0x1;
@@ -69,39 +49,6 @@ public class BigScreenBot extends PircBot
 
     private static enum Action { RECONNECT, RESTART, QUIT }
 
-    private static class Settings extends Properties
-    {
-        public boolean getBooleanProperty(String key, boolean defaultValue)
-        {
-            String value = getProperty(key);
-
-            if (value != null)
-                return Boolean.parseBoolean(value);
-            else
-                return defaultValue;
-        }
-
-        public int getIntegerProperty(String key, int defaultValue)
-        {
-            String value = getProperty(key);
-
-            if (value != null)
-                return Integer.parseInt(value);
-            else
-                return defaultValue;
-        }
-
-        public List<String> getListProperty(String key)
-        {
-            String value = getProperty(key);
-
-            if (value != null)
-                return Arrays.asList(value.split("\\s"));
-            else
-                return null;
-        }
-    }
-
     private abstract class Command
     {
         private boolean admin;
@@ -149,17 +96,53 @@ public class BigScreenBot extends PircBot
         }
     }
 
-    private Settings settings;
+    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 JmDNS mdns;
 
-    private BigScreenBot(Settings settings)
+    private BigScreenBot()
     {
         super();
 
-        this.settings = settings;
+        FileReader reader = null;
+
+        try
+        {
+            reader = new FileReader(SETTINGS);
+
+            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)
+        {
+            error(exception, 1);
+        }
 
         setAutoNickChange(true);
         setFinger("Big Screen Bot");
@@ -177,9 +160,9 @@ public class BigScreenBot extends PircBot
         setVerbose(settings.getBooleanProperty("verbose", false));
 
         for (String admin : settings.getListProperty("admins"))
-            admins.add(Pattern.compile(GlobCompiler.globToPerl5(admin.toCharArray(), GlobCompiler.DEFAULT_MASK), Pattern.CASE_INSENSITIVE));
+            admins.add(compileNickMask(admin));
 
-        commands.put("fling", new Command(false, BOTH, "url [device]", "fling url to a Google TV device")
+        commands.put("googletv", new Command(false, BOTH, "url [device]", "fling url to a Google TV device")
         {
             @Override
             public void execute(String channel, String sender, boolean admin, String argument)
@@ -228,18 +211,43 @@ public class BigScreenBot extends PircBot
         commands.put("pair", new Command(true, PRIVATE, "[device [code]]", "pair with a Google TV device")
         {
             @Override
-            public void execute(String channel, String sender, boolean admin, String argument)
+            public void execute(final String channel, final String sender, boolean admin, String argument)
             {
-                /*JmDNS dns = JmDNS.create(InetAddress.getByName(settings.getProperty("interface")));
+                String[] arguments = argument.split("\\s", 2);
 
-                for (ServiceInfo info : dns.list("_anymote._tcp.local."))
+                if (arguments.length == 1)
+                    if (arguments[0].isEmpty())
+                        new Thread()
+                        {
+                            @Override
+                            public void run()
+                            {
+                                List<String> devices;
+
+                                synchronized (remote)
+                                {
+                                    devices = remote.listDevices();
+                                }
+
+                                if (devices.isEmpty())
+                                {
+                                    sendMessage(channel, sender, "there are no devices to pair with");
+
+                                    return;
+                                }
+
+                                sendMessage(channel, sender, Colors.BOLD + "devices" + Colors.NORMAL);
+
+                                for (String device : devices)
+                                    sendMessage(channel, sender, device);
+                            }
+                        }.start();
+                    else
+                    {
+                    }
+                else
                 {
-                    System.out.println(info.getName());
-                    System.out.println(info.getInetAddresses()[0]);
-                    System.out.println(info.getPort());
                 }
-
-                dns.close();*/
             }
         });
         commands.put("quit", new Command(true, PRIVATE, "[message]", "quit and do not come back")
@@ -432,49 +440,9 @@ public class BigScreenBot extends PircBot
         commands.get("help").execute(channel, sender, admin, command);
     }
 
-    private boolean isNickInChannels(String nick)
-    {
-        for (String channel : getChannels())
-            for (User user : getUsers(channel))
-                if (user.equals(nick))
-                    return true;
-
-        return false;
-    }
-
     public static void main(String[] args)
     {
-        Settings settings = new Settings();
-        FileReader reader = null;
-
-        try
-        {
-            reader = new FileReader(SETTINGS);
-
-            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);
-                }
-        }
-
-        new BigScreenBot(settings);
+        new BigScreenBot();
     }
 
     private static void error(Exception exception)
@@ -488,33 +456,6 @@ public class BigScreenBot extends PircBot
 
         System.exit(code);
     }
-
-    private static boolean matchNickMasks(String sender, String login, String hostname, List<Pattern> masks)
-    {
-        String nick = sender + "!" + login + "@" + hostname;
-
-        for (Pattern mask : masks)
-            if (mask.matcher(nick).matches())
-                return true;
-
-        try
-        {
-            InetAddress address = InetAddress.getByName(hostname);
-            String name = address.getCanonicalHostName();
-
-            if (!name.equalsIgnoreCase(hostname))
-            {
-                nick = sender + "!" + login + "@" + name;
-
-                for (Pattern mask : masks)
-                    if (mask.matcher(nick).matches())
-                        return true;
-            }
-        }
-        catch (UnknownHostException exception) {}
-
-        return false;
-    }
 }
 
 // vim: expandtab

+ 84 - 0
src/net/douglasthrift/bigscreenbot/Bot.java

@@ -0,0 +1,84 @@
+// Bot
+//
+// Douglas Thrift
+//
+// Bot.java
+
+/*  Copyright 2011 Douglas Thrift
+ *
+ *  This file is part of Big Screen Bot.
+ *
+ *  Big Screen Bot is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Big Screen Bot is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Big Screen Bot.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.douglasthrift.bigscreenbot;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import java.util.List;
+
+import java.util.regex.Pattern;
+
+import org.apache.oro.text.GlobCompiler;
+
+import org.jibble.pircbot.PircBot;
+import org.jibble.pircbot.User;
+
+public abstract class Bot extends PircBot
+{
+    protected boolean isNickInChannels(String nick)
+    {
+        for (String channel : getChannels())
+            for (User user : getUsers(channel))
+                if (user.equals(nick))
+                    return true;
+
+        return false;
+    }
+
+    protected static Pattern compileNickMask(String mask)
+    {
+        return Pattern.compile(GlobCompiler.globToPerl5(mask.toCharArray(), GlobCompiler.DEFAULT_MASK), Pattern.CASE_INSENSITIVE);
+    }
+
+    protected static boolean matchNickMasks(String sender, String login, String hostname, List<Pattern> masks)
+    {
+        String nick = sender + "!" + login + "@" + hostname;
+
+        for (Pattern mask : masks)
+            if (mask.matcher(nick).matches())
+                return true;
+
+        try
+        {
+            InetAddress address = InetAddress.getByName(hostname);
+            String name = address.getCanonicalHostName();
+
+            if (!name.equalsIgnoreCase(hostname))
+            {
+                nick = sender + "!" + login + "@" + name;
+
+                for (Pattern mask : masks)
+                    if (mask.matcher(nick).matches())
+                        return true;
+            }
+        }
+        catch (UnknownHostException exception) {}
+
+        return false;
+    }
+}
+
+// vim: expandtab

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

@@ -0,0 +1,79 @@
+// Remote
+//
+// Douglas
+//
+// Remote.java
+
+/*  Copyright 2011 Douglas Thrift
+ *
+ *  This file is part of Big Screen Bot.
+ *
+ *  Big Screen Bot is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Big Screen Bot is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Big Screen Bot.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.douglasthrift.bigscreenbot;
+
+import java.io.IOException;
+
+import java.net.InetAddress;
+import java.net.UnknownHostException;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.jmdns.JmDNS;
+import javax.jmdns.ServiceInfo;
+
+import com.google.anymote.common.AnymoteFactory;
+import com.google.anymote.common.ErrorListener;
+
+import com.google.anymote.device.DeviceAdapter;
+import com.google.anymote.device.MessageReceiver;
+
+public class Remote
+{
+    private static final String TYPE = "_anymote._tcp.local.";
+    private Settings settings;
+    private JmDNS mdns;
+
+    public Remote(Settings settings) throws UnknownHostException, IOException
+    {
+        this.settings = settings;
+
+        String interfaze = settings.getProperty("interface");
+
+        if (interfaze != null)
+            mdns = JmDNS.create(InetAddress.getByName(interfaze));
+        else
+            mdns = JmDNS.create();
+    }
+
+    public List<String> listDevices()
+    {
+        List<String> devices = new ArrayList<String>();
+
+        for (ServiceInfo info : mdns.list(TYPE))
+            devices.add(info.getName());
+
+        return devices;
+    }
+
+    @Override
+    protected void finalize() throws IOException
+    {
+        mdns.close();
+    }
+}
+
+// vim: expandtab

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

@@ -0,0 +1,64 @@
+// Settings
+//
+// Douglas Thrift
+//
+// Settings.java
+
+/*  Copyright 2011 Douglas Thrift
+ *
+ *  This file is part of Big Screen Bot.
+ *
+ *  Big Screen Bot is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  Big Screen Bot is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with Big Screen Bot.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+package net.douglasthrift.bigscreenbot;
+
+import java.util.Arrays;
+import java.util.List;
+import java.util.Properties;
+
+public class Settings extends Properties
+{
+    public boolean getBooleanProperty(String key, boolean defaultValue)
+    {
+        String value = getProperty(key);
+
+        if (value != null)
+            return Boolean.parseBoolean(value);
+        else
+            return defaultValue;
+    }
+
+    public int getIntegerProperty(String key, int defaultValue)
+    {
+        String value = getProperty(key);
+
+        if (value != null)
+            return Integer.parseInt(value);
+        else
+            return defaultValue;
+    }
+
+    public List<String> getListProperty(String key)
+    {
+        String value = getProperty(key);
+
+        if (value != null)
+            return Arrays.asList(value.split("\\s"));
+        else
+            return null;
+    }
+}
+
+// vim: expandtab