Skip to content

Commit

Permalink
removed flattenings and list to binary conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
dnet committed Sep 23, 2013
1 parent 78f3064 commit 7d7efb1
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 30 deletions.
33 changes: 17 additions & 16 deletions admin.erl
Expand Up @@ -84,7 +84,7 @@ admin(Prefix) ->
process_privmsg("-quit", _Remainder, _ReplyTo, Prefix, _Contact) ->
case admin(Prefix) of
true ->
{quit, "QUIT :Goodbye from " ++ ?NICK};
{quit, ["QUIT :Goodbye from ", ?NICK]};
false ->
noreply
end;
Expand Down Expand Up @@ -120,25 +120,25 @@ process_privmsg("-load", [Module | _], ReplyTo, Prefix, _Contact) ->
case admin(Prefix) of
true ->
Atom = list_to_atom(Module),
{ok, "PRIVMSG " ++ ReplyTo ++ " :" ++
{ok, ["PRIVMSG ", ReplyTo, " :",
case code:load_file(Atom) of
{module, Atom} -> "module loaded ok";
{error, not_purged} ->
case code:soft_purge(Atom) of
true -> "module loaded ok (old version got purged)";
false -> "old version is still in use, use rmmod"
end;
{error, What} -> "could not load module: " ++ atom_to_list(What)
end
{error, What} -> ["could not load module: ", atom_to_list(What)]
end]
};
false ->
noreply
end;
process_privmsg("-help", _Remainder, ReplyTo, Prefix, _Contact) ->
case admin(Prefix) of
true ->
{ok, "PRIVMSG " ++ ReplyTo ++
" :Available commands are: quit, lsmod, rmmod, insmod, load, reload, help"};
{ok, ["PRIVMSG ", ReplyTo,
" :Available commands are: quit, lsmod, rmmod, insmod, load, reload, help"]};
false ->
noreply
end;
Expand All @@ -152,12 +152,13 @@ lsmod(To, Contact) ->
fun(Pid, {N, Msg}) ->
Pid ! {ident, self()},
Ident = receive {ident, I} -> I after 200 -> "(timeout)" end,
{N + 1, Msg ++ "\r\nPRIVMSG " ++ To ++ " :" ++
integer_to_list(N) ++ ". " ++ pid_to_list(Pid) ++ " " ++ Ident}
end, {1, "PRIVMSG " ++ To ++ " :Loaded modules:"}, L),
{N + 1, io_lib:format(
"~s\r\nPRIVMSG ~s :~w. ~w ~s",
[Msg, To, N, Pid, Ident])}
end, {1, ["PRIVMSG ", To, " :Loaded modules:"]}, L),
Contact ! {raw, T}
after 1500 ->
Contact ! {raw, "PRIVMSG " ++ To ++ " :(timeout)"}
Contact ! {raw, ["PRIVMSG ", To, " :(timeout)"]}
end.

rmmod(To, [FirstParam | _], Contact) ->
Expand All @@ -168,18 +169,18 @@ rmmod(To, [FirstParam | _], Contact) ->
ModPid = lists:nth(N, L),
Contact ! {killmod, ModPid, self()},
Resp = receive {killmod, Msg} -> Msg after 500 -> "(timeout)" end,
Contact ! {raw, "PRIVMSG " ++ To ++ " :" ++ Resp}
Contact ! {raw, ["PRIVMSG ", To, " :", Resp]}
after 1500 ->
Contact ! {raw, "PRIVMSG " ++ To ++ " :(timeout)"}
Contact ! {raw, ["PRIVMSG ", To, " :(timeout)"]}
end.

insmod(To, [ModName | Params], Contact) ->
Contact ! {insmod, ModName, Params, self()},
receive
{insmod, Resp} ->
Contact ! {raw, "PRIVMSG " ++ To ++ " :" ++ Resp}
Contact ! {raw, ["PRIVMSG ", To, " :", Resp]}
after 1500 ->
Contact ! {raw, "PRIVMSG " ++ To ++ " :(timeout)"}
Contact ! {raw, ["PRIVMSG ", To, " :(timeout)"]}
end.

reload(To, [FirstParam | _], Contact) ->
Expand All @@ -189,9 +190,9 @@ reload(To, [FirstParam | _], Contact) ->
{mods, L} ->
lists:nth(N, L) ! {reload, self()},
Resp = receive reloaded -> "module reloaded successfully" after 2500 -> "(timeout)" end,
Contact ! {raw, "PRIVMSG " ++ To ++ " :" ++ Resp}
Contact ! {raw, ["PRIVMSG ", To, " :", Resp]}
after 1500 ->
Contact ! {raw, "PRIVMSG " ++ To ++ " :(timeout)"}
Contact ! {raw, ["PRIVMSG ", To, " :(timeout)"]}
end.

strip_crlf(Str) ->
Expand Down
17 changes: 9 additions & 8 deletions ircbot.erl
Expand Up @@ -52,10 +52,10 @@ master(State = #ms{socket = Socket}) ->
io:format("Socket error [~w]: ~s~n", [Socket, Reason]),
master(State);
{announce, Text} ->
send(Socket, "PRIVMSG " ++ State#ms.channel ++ " :" ++ Text),
send(Socket, ["PRIVMSG ", State#ms.channel, " :", Text]),
master(State);
{topic, Text} ->
send(Socket, "TOPIC " ++ State#ms.channel ++ " :" ++ Text),
send(Socket, ["TOPIC ", State#ms.channel, " :", Text]),
master(State);
{raw, Text} ->
send(Socket, Text),
Expand All @@ -69,7 +69,7 @@ master(State = #ms{socket = Socket}) ->
master(case lists:member(ModPid, State#ms.modpids) of
true ->
ModPid ! quit,
RespPid ! {killmod, "removed module " ++ pid_to_list(ModPid)},
RespPid ! {killmod, ["removed module ", pid_to_list(ModPid)]},
State#ms{
rawsubscribers = lists:delete(ModPid, State#ms.rawsubscribers),
modpids = lists:delete(ModPid, State#ms.modpids)
Expand All @@ -83,7 +83,8 @@ master(State = #ms{socket = Socket}) ->
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)},
Pid ! {insmod, io_lib:format(
"inserted module ~s as PID ~w", [ModName, ModPid])},
master(State#ms{modpids = [ModPid | State#ms.modpids]});
false ->
Pid ! {insmod, "invalid module or parameter count"},
Expand All @@ -107,12 +108,12 @@ master(State = #ms{socket = Socket}) ->

send(Socket, Text) ->
io:format("~p~n", [Text]),
gen_tcp:send(Socket, list_to_binary(Text ++ "\r\n")).
gen_tcp:send(Socket, [Text, "\r\n"]).

send_init(Socket, Channel) ->
send(Socket, "USER " ++ ?NICK ++ " dummy-host dummy-server :" ++ ?FULL_NAME),
send(Socket, "NICK " ++ ?NICK ++ ""),
send(Socket, "JOIN " ++ Channel).
send(Socket, ["USER ", ?NICK, " dummy-host dummy-server :", ?FULL_NAME]),
send(Socket, ["NICK ", ?NICK]),
send(Socket, ["JOIN ", Channel]).

quit(Socket, QuitCommand) ->
send(Socket, QuitCommand),
Expand Down
2 changes: 1 addition & 1 deletion ping.erl
Expand Up @@ -47,7 +47,7 @@ inner_ircproc(Contact, ServerName, Reconnect) ->
Contact ! {reconnect, "QUIT :Reconnecting"},
inner_ircproc(Contact, ServerName);
_ ->
Contact ! {raw, "PING " ++ ServerName},
Contact ! {raw, ["PING ", ServerName]},
inner_ircproc(Contact, ServerName, true)
end
end.
2 changes: 1 addition & 1 deletion pong.erl
Expand Up @@ -14,7 +14,7 @@ ircproc(Contact) ->
receive
quit -> quit;
{incoming, <<"PING :", Data/binary>>} ->
Contact ! {raw, "PONG :" ++ binary_to_list(Data)},
Contact ! {raw, ["PONG :", Data]},
ircproc(Contact);
{ident, Pid} ->
Pid ! {ident, "pong"},
Expand Down
8 changes: 4 additions & 4 deletions version.erl
Expand Up @@ -15,13 +15,13 @@ ircproc(Contact) ->
quit -> quit;
{incoming, <<":", Data/binary>>} ->
case string:tokens(binary_to_list(Data), " ") of
[From, "PRIVMSG", _, [58, 1, 86, 69, 82, 83, 73, 79, 78, 1 | _]] ->
[From, "PRIVMSG", _, [58, 1, $V, $E, $R, $S, $I, $O, $N, 1 | _]] ->
[Nick | _] = string:tokens(From, "!"),
Contact ! {raw, lists:flatten(io_lib:format(
"NOTICE ~s :\x01VERSION jimm-erlang-bot running " ++
Contact ! {raw, io_lib:format(
"NOTICE ~s :\x01VERSION jimm-erlang-bot running "
"on Erlang emulator ~s OTP release ~s\x01",
[Nick, erlang:system_info(version),
erlang:system_info(otp_release)]))};
erlang:system_info(otp_release)])};
_ -> nop
end,
ircproc(Contact);
Expand Down

0 comments on commit 7d7efb1

Please sign in to comment.