Added a !random command, and cut a new release. v6
authorMatt Mullins <mmullins@mmlx.us>
Mon, 12 Dec 2011 07:09:53 +0000 (23:09 -0800)
committerMatt Mullins <mmullins@mmlx.us>
Mon, 12 Dec 2011 07:09:53 +0000 (23:09 -0800)
Refactored IRC command handling, while I was at it.

bot.rel
irc/irc.app
irc/irc.appup
irc/irc_command.erl [new file with mode: 0644]
irc/irc_conn.erl

diff --git a/bot.rel b/bot.rel
index ae65677..4b8eb7a 100644 (file)
--- a/bot.rel
+++ b/bot.rel
@@ -1,4 +1,4 @@
-{release, {"erlbot", "5"},
+{release, {"erlbot", "6"},
  {erts, "5.7.4"},
  [{kernel, "2.13.4"},
   {stdlib, "1.16.4"},
@@ -7,5 +7,5 @@
   {amqp_client, "0.0.0"},
   {core, "2"},
   {amqp, "1"},
-  {irc, "4"}]
+  {irc, "5"}]
 }.
index 4d1f083..402232c 100644 (file)
@@ -1,8 +1,8 @@
 {application, irc,
  [{description, "IRC protocol application"},
-  {vsn, "4"},
+  {vsn, "5"},
   {modules, [irc_util, irc_app, irc_sup, irc_net_sup, irc_conn,
-             irc_object_sup, irc_amqp_listener]},
+             irc_object_sup, irc_amqp_listener, irc_command]},
   {registered, [irc_sup]},
   {applications, [core, amqp]},
   {mod, {irc_app, []}}
index 9af27b3..ec6adfe 100644 (file)
@@ -1,15 +1,9 @@
-{"4",
- [{"3", [{add_module, irc_object_sup},
-         {add_module, irc_amqp_listener},
-         {update, irc_sup, supervisor},
-         {update, irc_net_sup, supervisor},
-         {update, irc_conn, {advanced, none}}
+{"5",
+ [{"4", [{add_module, irc_command},
+         {update, irc_conn}
         ]}],
 
- [{"3", [{update, irc_conn, {advanced, none}},
-         {delete_module, irc_object_sup},
-         {delete_module, irc_amqp_listener},
-         {update, irc_sup, supervisor},
-         {update, irc_net_sup, supervisor}
+ [{"4", [{update, irc_conn},
+         {delete_module, irc_command}
         ]}]
 }.
diff --git a/irc/irc_command.erl b/irc/irc_command.erl
new file mode 100644 (file)
index 0000000..1b39b19
--- /dev/null
@@ -0,0 +1,42 @@
+-module(irc_command).
+-vsn(1).
+
+-export([
+          do_privmsg_command/2
+        ]).
+
+-include("irc_util.hrl").
+
+do_privmsg_command(Text, From) ->
+    case Text of
+        "botsnack" ->
+            Choices = {"*chomp*", ":P", "yay", "meh", "\\o/"},
+            Index = random:uniform(size(Choices)),
+            NewCommand = #irc_command{command = "PRIVMSG",
+                                      middles = [From],
+                                      trailing = element(Index, Choices)},
+            irc_conn:send_command(self(), NewCommand);
+        "crash" ->
+            throw(crash);
+        "random" ++ Choices ->
+            Response = random_choice(Choices),
+            NewCommand = #irc_command{command = "PRIVMSG",
+                                      middles = [From],
+                                      trailing = Response},
+            irc_conn:send_command(self(), NewCommand);
+        _ -> ok
+    end.
+
+random_choice([SplitChar | ChoicesText]) ->
+    Choices = string:tokens(ChoicesText, [SplitChar]),
+    Count = length(Choices),
+    case Choices of
+        _ when Count > 0 ->
+            Index = random:uniform(Count),
+            lists:nth(Index, Choices);
+        _ ->
+            "You didn't give me any choices!"
+    end;
+
+random_choice([]) ->
+    "Usage: random!foo!bar!baz!... where ! is any character you want to use as a separator.".
index e3865c1..5548932 100644 (file)
@@ -115,13 +115,9 @@ do_line(Line, State) ->
 
 do_privmsg(_Command = #irc_command{middles = Middles, trailing = Text}) ->
     case Text of
-        "!botsnack" ++ _ ->
-            Choices = {"*chomp*", ":P", "yay", "meh", "\\o/"},
-            Index = random:uniform(size(Choices)),
-            NewCommand = #irc_command{command = "PRIVMSG",
-                                      middles = Middles,
-                                      trailing = element(Index, Choices)},
-            send_command(NewCommand);
+        "!" ++ CommandText ->
+            [From|_] = Middles,
+            irc_command:do_privmsg_command(CommandText, From);
         _ -> ok
     end.