Skip to content

Instantly share code, notes, and snippets.

@rsampaio
Created February 13, 2012 23:07
Show Gist options
  • Save rsampaio/1821328 to your computer and use it in GitHub Desktop.
Save rsampaio/1821328 to your computer and use it in GitHub Desktop.
erbot
#!/usr/bin/env escript
-module(erbot).
-export([start/0]).
-define(_SERVER, "irc.oftc.net").
-define(_NICK, "4funBot").
-define(_CHANNEL, "#sysadms").
start() ->
{ok, Socket} = gen_tcp:connect(?_SERVER, 5222, [binary, {packet, 0}]),
gen_tcp:send(Socket, lists:concat(["NICK ", ?_NICK, "\r\n"])),
gen_tcp:send(Socket, lists:concat(["USER ", ?_NICK, " 192.168.1.226 ", ?_SERVER, " :Erlang Bot\r\n"])),
gen_tcp:send(Socket, lists:concat(["JOIN ", ?_CHANNEL, " \r\n"])),
receive_data(Socket, []).
%io:format("~p~n", [Pid]).
receive_data(Socket, DataSoFar) ->
receive
{tcp, Socket, Bin} ->
Lines = string:tokens(binary_to_list(Bin), "\r\n"),
lists:foreach(fun(L) -> parse_lines(Socket, L) end, Lines),
receive_data(Socket, [Bin | DataSoFar]);
{tcp_closed, Socket} ->
binary_to_list(DataSoFar)
end.
parse_lines(Socket, Line) ->
case re:run(Line, ":(!.*)", [global, {capture, [1], list}]) of
{match, Captured} ->
io:format("~p~n", Captured),
[[Cmd]] = Captured,
spawn(fun() -> parse_cmd(Socket, Cmd) end);
nomatch ->
ok
end.
parse_cmd(Socket, Cmd) ->
case Cmd of
"!oi" ->
gen_tcp:send(Socket, lists:concat(["PRIVMSG ", ?_CHANNEL, " :E e aee vagabundo\r\n"]));
nomatch ->
ok
end.
main(_) ->
start().
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment