From 8f5684ba9021ae775da4662da180c753a2139917 Mon Sep 17 00:00:00 2001 From: Matt Mullins Date: Sun, 11 Dec 2011 23:09:53 -0800 Subject: [PATCH] Added a !random command, and cut a new release. Refactored IRC command handling, while I was at it. --- bot.rel | 4 ++-- irc/irc.app | 4 ++-- irc/irc.appup | 16 +++++----------- irc/irc_command.erl | 42 ++++++++++++++++++++++++++++++++++++++++++ irc/irc_conn.erl | 10 +++------- 5 files changed, 54 insertions(+), 22 deletions(-) create mode 100644 irc/irc_command.erl diff --git a/bot.rel b/bot.rel index ae65677..4b8eb7a 100644 --- 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"}] }. diff --git a/irc/irc.app b/irc/irc.app index 4d1f083..402232c 100644 --- a/irc/irc.app +++ b/irc/irc.app @@ -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, []}} diff --git a/irc/irc.appup b/irc/irc.appup index 9af27b3..ec6adfe 100644 --- a/irc/irc.appup +++ b/irc/irc.appup @@ -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 index 0000000..1b39b19 --- /dev/null +++ b/irc/irc_command.erl @@ -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.". diff --git a/irc/irc_conn.erl b/irc/irc_conn.erl index e3865c1..5548932 100644 --- a/irc/irc_conn.erl +++ b/irc/irc_conn.erl @@ -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. -- 2.11.0