Browse Source

Fling a URL if it is said to the bot's nick in a channel.

Douglas William Thrift 13 years ago
parent
commit
ad283bf285
1 changed files with 32 additions and 23 deletions
  1. 32 23
      src/net/douglasthrift/bigscreenbot/BigScreenBot.java

+ 32 - 23
src/net/douglasthrift/bigscreenbot/BigScreenBot.java

@@ -33,6 +33,7 @@ import java.net.URL;
 import java.security.GeneralSecurityException;
 
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
@@ -63,6 +64,7 @@ public class BigScreenBot extends Bot
     private static final int CHANNEL = 0x1;
     private static final int PRIVATE = 0x2;
     private static final int BOTH    = 0x3;
+    private static final Pattern SCHEMES = Pattern.compile("^https?$");
 
     private static enum Action { RECONNECT, RESTART, QUIT }
 
@@ -213,18 +215,9 @@ public class BigScreenBot extends Bot
                     return;
                 }
 
-                try
-                {
-                    if (!new URL(arguments[0]).getProtocol().matches("^https?$"))
-                    {
-                        invalidURL(channel, sender, arguments[0]);
-
-                        return;
-                    }
-                }
-                catch (MalformedURLException exception)
+                if (!isValidURL(arguments[0]))
                 {
-                    invalidURL(channel, sender, arguments[0]);
+                    sendMessage(channel, sender, String.format("invalid URL (\"%1$s\")", arguments[0]));
 
                     return;
                 }
@@ -248,11 +241,6 @@ public class BigScreenBot extends Bot
                     }
                 }.start();
             }
-
-            private void invalidURL(String channel, String sender, String url)
-            {
-                sendMessage(channel, sender, String.format("invalid URL (\"%1$s\")", url));
-            }
         });
         commands.put("help", new Command(false, PRIVATE, "[command]", "show this help message")
         {
@@ -652,16 +640,20 @@ public class BigScreenBot extends Bot
         if (arguments.length != 0)
         {
             if (channel == null)
-                try
+            {
+                if (isValidURL(arguments[0]))
                 {
-                    if (new URL(arguments[0]).getProtocol().matches("^https?$"))
-                    {
-                        commands.get("googletv").execute(channel, sender, admin, StringUtils.join(arguments, ' '));
+                    fling(channel, sender, admin, arguments);
 
-                        return;
-                    }
+                    return;
                 }
-                catch (MalformedURLException exception) {}
+            }
+            else if (arguments.length == 2 && Pattern.compile("^" + Pattern.quote(getNick()) + ":?$", Pattern.CASE_INSENSITIVE).matcher(arguments[0]).matches() && isValidURL(StringUtils.split(arguments[1], null, 2)[0]))
+            {
+                fling(channel, sender, admin, Arrays.copyOfRange(arguments, 1, arguments.length));
+
+                return;
+            }
 
             argument = arguments[0].toLowerCase();
 
@@ -730,11 +722,28 @@ public class BigScreenBot extends Bot
         sendMessage(channel, sender, "an error occurred: " + exception.getMessage());
     }
 
+    private void fling(String channel, String sender, boolean admin, String... arguments)
+    {
+        commands.get("googletv").execute(channel, sender, admin, StringUtils.join(arguments, ' '));
+    }
+
     private void help(String channel, String sender, boolean admin, String command)
     {
         commands.get("help").execute(channel, sender, admin, command);
     }
 
+    private boolean isValidURL(String url)
+    {
+        try
+        {
+            return SCHEMES.matcher(new URL(url).getProtocol()).matches();
+        }
+        catch (MalformedURLException exception)
+        {
+            return false;
+        }
+    }
+
     private void listBans(String channel, String sender)
     {
         synchronized (bans)