Skip to content

Instantly share code, notes, and snippets.

@jlouis
Created January 22, 2011 20:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jlouis/791442 to your computer and use it in GitHub Desktop.
Save jlouis/791442 to your computer and use it in GitHub Desktop.
master(State = #ms{socket = Socket}) ->
receive
%% Loop cases
{tcp_error, Socket, Reason} ->
io:format("Socket error [~w]: ~s~n", [Socket, Reason]),
master(State);
{announce, Text} ->
send(Socket, "PRIVMSG " ++ State#ms.channel ++ " :" ++ Text),
send(File, State#ms.channel ++ " :" ++ Text),
master(State);
{topic, Text} ->
send(Socket, "TOPIC " ++ State#ms.channel ++ " :" ++ Text),
master(State);
{raw, Text} ->
send(Socket, Text),
master(State);
{subscribe, Pid} ->
master(State#ms{rawsubscribers = [Pid | State#ms.rawsubscribers]});
{getmods, Pid} ->
Pid ! {mods, State#ms.modpids},
master(State);
{killmod, ModPid, RespPid} ->
master(case lists:member(ModPid, State#ms.modpids) of
true ->
ModPid ! quit,
RespPid ! {killmod, "removed module " ++ pid_to_list(ModPid)},
State#ms{
rawsubscribers = lists:delete(ModPid, State#ms.rawsubscribers),
modpids = lists:delete(ModPid, State#ms.modpids)
};
false ->
RespPid ! {killmod, "invalid module"},
State
end);
{insmod, ModName, Params, Pid} ->
ModAtom = list_to_atom(ModName),
case erlang:function_exported(ModAtom, ircmain, length(Params) + 1) of
true ->
ModPid = apply(ModAtom, ircmain, [self() | Params]),
Pid ! {insmod, "inserted module " ++ ModName ++ " as PID " ++ pid_to_list(ModPid)},
master(State#ms{modpids = [ModPid | State#ms.modpids]});
false ->
Pid ! {insmod, "invalid module or parameter count"},
master(State)
end;
{tcp, Socket, Data} ->
lists:foreach(fun(P) -> P ! {incoming, Data} end, State#ms.rawsubscribers),
master(State);
{tcp_closed, Socket} ->
io:format("Socket ~w closed [~w]~n", [Socket, self()]),
reconnect_master(State);
{reconnect, QuitCommand} ->
quit(Socket, QuitCommand),
reconnect_master(State);
% Quit cases
{quit, QuitCommand} ->
lists:foreach(fun(P) -> P ! quit end, State#ms.modpids),
quit(Socket, QuitCommand),
ok
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment