Created
May 31, 2012 17:20
-
-
Save runejuhl/2844860 to your computer and use it in GitHub Desktop.
IRC bot
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% First try at making a small IRC bot. | |
%% Dedicated to ThordenM. | |
%% | |
%% Rune Juhl Jacobsen, 2012 | |
-module(irc). | |
-compile(export_all). | |
-define(NICK, "lolcat"). | |
-define(USERNAME, "lolcatzz"). | |
-define(REALNAME, "The real fat lolcat"). | |
-define(CHANNELS, ["#penguins"]). | |
-record(config, {socket, prefix=nil, options=nil}). | |
%% Set up TCP connection | |
connect(Address, Port) -> | |
{ok, Socket} = gen_tcp:connect(Address, Port, [binary, {active, true}]), | |
Config = #config{socket=Socket}, | |
setup(Config). | |
%% Initial setup. Receive welcome message, save prefix in Config | |
setup(Config) when Config#config.prefix == nil -> | |
Socket = Config#config.socket, | |
receive | |
{tcp, Socket, WMsg} -> | |
io:format("Received welcome msg: ~s~n", [WMsg]), | |
setup(Config#config{prefix=hd(re:split(WMsg, " "))}) | |
after 120000 -> | |
io:format("Didn't receive response in time, exiting."), | |
gen_tcp:close(Socket) | |
end; | |
%% Further setup. Send password, nick, username and realname. Recevive | |
%% MOTD. | |
setup(Config) when Config#config.options == nil -> | |
Socket = Config#config.socket, | |
send(Config, [<<"PASS *">>]), | |
send(Config, [<<"NICK ">>, <<?NICK>>]), | |
send(Config, ["USER ", ?USERNAME, " 8 * :", ?REALNAME]), | |
%% receive MOTD | |
receive | |
{tcp, Socket, Motd} -> | |
io:format( | |
"Received MOTD:~n~s~n", | |
[string:tokens(erlang:binary_to_list(Motd), "\r\n")]) | |
end, | |
%% join channels | |
join(Config), | |
handle(Config). | |
%% Handle responses from the IRC server. | |
handle(Config) -> | |
Socket = Config#config.socket, | |
receive | |
%% Ping messages | |
{tcp, Socket, <<"PING", _/binary>>} -> | |
send(Config, pong), | |
handle(Config); | |
%% Errors | |
{tcp, Socket, <<"ERROR :", Msg/binary>>} -> | |
io:format("Received error: ~p~n", [Msg]), | |
gen_tcp:close(Config); | |
%% Unknown responses | |
{tcp, Socket, Unknown} -> | |
io:format("~s~n", [Unknown]), | |
handle(Config) | |
%% Send ping after 2 minutes. (disabled!) | |
%% Enable for infinite ping-pong loop. | |
%% after 120000 -> | |
%% send(Config, ping), | |
%% handle(Config) | |
end. | |
%% Join channel | |
join(Config) -> | |
lists:map(fun(Channel) -> join(Config, Channel) end, ?CHANNELS). | |
join(Config, Channel) -> | |
send(Config, ["JOIN ", Channel]). | |
send(Config, pong) -> | |
send(Config, ["PONG ", Config#config.prefix]); | |
send(Config, ping) -> | |
send(Config, ["PING ", Config#config.prefix]); | |
send(Config, Msg) -> | |
gen_tcp:send(Config#config.socket, [Msg, "\r\n"]). | |
send(Config, Msg, prefix) -> | |
send(Config, [Config#config.prefix, " ",Msg]). |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
...a work in progress, I should note.