Refactored IRC command handling, while I was at it.
-{release, {"erlbot", "5"},
+{release, {"erlbot", "6"},
{erts, "5.7.4"},
[{kernel, "2.13.4"},
{stdlib, "1.16.4"},
{amqp_client, "0.0.0"},
{core, "2"},
{amqp, "1"},
- {irc, "4"}]
+ {irc, "5"}]
}.
{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, []}}
-{"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}
]}]
}.
--- /dev/null
+-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.".
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.