Skip to content

Instantly share code, notes, and snippets.

@dnet
Created February 9, 2012 12:30
Show Gist options
  • Save dnet/1779678 to your computer and use it in GitHub Desktop.
Save dnet/1779678 to your computer and use it in GitHub Desktop.
Erlang IRC bot unshortener module
% depends: https://gist.github.com/1779650
-module(unshortener).
-export([ircmain/1, ircproc/1, reload/2]).
recognize_url(Input) ->
{match, URLs} = re:run(Input,
"https?://(t.co|tinyurl.com|bit.ly)/[a-zA-Z0-9\-_]+",
[{capture, first, list}, global]),
"[unshortened] " ++ string:join(lists:map(fun unshort:unshort/1,
lists:map(fun lists:flatten/1, URLs)), " ").
ircmain(Contact) ->
Pid = spawn(?MODULE, ircproc, [Contact]),
Contact ! {subscribe, Pid},
Pid.
reload(Contact, Pid) ->
Pid ! reloaded,
ircproc(Contact).
ircproc(Contact) ->
receive
quit -> quit;
{incoming, Data} ->
S = binary_to_list(Data),
case string:str(S, "http") of
0 -> nop;
_ -> spawn(fun() ->
Contact ! {announce, recognize_url(S)} end)
end,
ircproc(Contact);
{ident, Pid} ->
Pid ! {ident, "unshortener"},
ircproc(Contact);
{reload, Pid} ->
?MODULE:reload(Contact, Pid);
_ -> ircproc(Contact)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment