Skip to content

Commit

Permalink
Add on_ping, on_kick, on_nick and on_raw callbacks
Browse files Browse the repository at this point in the history
on_ping happens when the server sends a ping request. on_kick happens
when someone is kicked from a channel the bot is in. on_nick happens
when someone changes his/her nickname in a channel and on_raw is sent if
none of the previous commands are picked up by the behaviour and gives
the bot a chance to respond to them.
  • Loading branch information
mazenharake committed Apr 15, 2011
1 parent d576fcb commit 996941d
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
20 changes: 18 additions & 2 deletions src/eirc_example_bot.erl
Expand Up @@ -32,7 +32,8 @@
-include("eirc.hrl").

-export([init/2, on_connect/1, on_text/4, on_notice/4, on_join/3, on_part/3,
on_ctcp/4, on_mode/5, on_topic/4, handle_call/3, terminate/2]).
on_ctcp/4, on_mode/5, on_topic/4, on_ping/1, on_nick/3, on_raw/3,
on_kick/5, handle_call/3, terminate/2]).

-compile(export_all).

Expand Down Expand Up @@ -60,7 +61,6 @@ init(Client, _Args) ->
on_connect(State) ->
io:format("Bot logged on...~n"),
BotNick = proplists:get_value(nick, eirc:state(State#botstate.cl)),

{ok, State#botstate{ nick = BotNick }}.

on_text(_, _, "!JOIN "++Channel, State) ->
Expand Down Expand Up @@ -106,6 +106,22 @@ on_topic(Nick, Channel, Topic, State) ->
io:format("TOPIC: (~p) ~p set topic to: ~p~n", [Channel, Nick, Topic]),
{ok, State}.

on_ping(State) ->
io:format("PING? PONG!~n"),
{ok, State}.

on_kick(User, Channel, TargetUser, Reason, State) ->
io:format("KICK: ~p kicked ~p from ~p, reason: ~p~n", [User, TargetUser, Channel, Reason]),
{ok, State}.

on_nick(OldNick, NewNick, State) ->
io:format("NICK: ~p is now known as ~p~n",[OldNick, NewNick]),
{ok, State}.

on_raw(Cmd, Args, State) ->
io:format("RAW: ~p; ~1000p~n",[Cmd, Args]),
{ok, State}.

handle_call({msg, _To}, _From, State) ->
{reply, ok, State};
handle_call(print_state, _From, State) ->
Expand Down
31 changes: 22 additions & 9 deletions src/gen_eircbot.erl
Expand Up @@ -37,6 +37,11 @@
-define(CTCP_TIME, "TIME "++eirc_lib:ctcp_time(calendar:local_time())).
-define(CTCP_PING(TS), "PING "++TS).

-define(MISSING_CALLBACKS, Cb == on_connect; Cb == on_text; Cb == on_notice;
Cb == on_join; Cb == on_part; Cb == on_mode; Cb == on_topic;
Cb == on_ping; Cb == on_kick; Cb == on_nick;
Cb == on_raw; Cb == handle_call; Cb == terminate).

%% =============================================================================
%% Application API
%% =============================================================================
Expand Down Expand Up @@ -116,9 +121,7 @@ safe_handle_ircmsg(IrcMsg, State) ->
[{_, on_ctcp, _}|_] ->
handle_default_ctcp(IrcMsg, State),
{ok, State#st.cbstate};
[{_, Cb, _}|_] when Cb == on_connect; Cb == on_text; Cb == on_notice;
Cb == on_join; Cb == on_part; Cb == on_mode;
Cb == on_topic; Cb == handle_call; Cb == terminate ->
[{_, Cb, _}|_] when ?MISSING_CALLBACKS ->
{ok, State#st.cbstate};
StackTrace ->
erlang:error(undef, StackTrace)
Expand Down Expand Up @@ -154,9 +157,18 @@ handle_ircmsg(#ircmsg{ cmd = "TOPIC" } = IrcMsg, State) ->
User = IrcMsg#ircmsg.nick,
[Channel|Topic] = IrcMsg#ircmsg.args,
(State#st.cb)(on_topic, [User, Channel, hd(Topic), State#st.cbstate]);
handle_ircmsg(#ircmsg{ cmd = "PING" }, State) ->
(State#st.cb)(on_ping, [State#st.cbstate]);
handle_ircmsg(#ircmsg{ cmd = "KICK" } = IrcMsg, State) ->
User = IrcMsg#ircmsg.nick,
[Channel, TargetUser, Reason|_] = IrcMsg#ircmsg.args,
(State#st.cb)(on_kick, [User, Channel, TargetUser, Reason, State#st.cbstate]);
handle_ircmsg(#ircmsg{ cmd = "NICK" } = IrcMsg, State) ->
Nick = IrcMsg#ircmsg.nick,
[NewNick|_] = IrcMsg#ircmsg.args,
(State#st.cb)(on_nick, [Nick, NewNick, State#st.cbstate]);
handle_ircmsg(IrcMsg, State) ->
io:format("IRCMSG: ~1000p~n", [IrcMsg]),
{ok, State#st.cbstate}.
(State#st.cb)(on_raw, [IrcMsg#ircmsg.cmd, tl(IrcMsg#ircmsg.args), State#st.cbstate]).

handle_default_ctcp(#ircmsg{ cmd = "VERSION" } = IrcMsg, State) ->
eirc_cl:msg(State#st.clpid, ctcp, IrcMsg#ircmsg.nick, ?CTCP_VERSION);
Expand Down Expand Up @@ -190,8 +202,9 @@ behaviour_info(callbacks) ->
{on_ctcp, 4},
{on_mode, 5},
{on_topic, 4},
{on_ping, 1},
{on_kick, 5},
{on_nick, 3},
{on_raw, 3},
{handle_call, 3},
{terminate, 2}];
behaviour_info(_) -> undefined.


{terminate, 2}].

0 comments on commit 996941d

Please sign in to comment.