Merge pull request #2 from PaulSD/master

Some more binary conversions in mod_muc_admin, mod_rest, and mod_shcommands
This commit is contained in:
badlop 2013-08-02 06:45:48 -07:00
commit 587f495af5
3 changed files with 59 additions and 59 deletions

View File

@ -170,9 +170,9 @@ muc_online_rooms(ServerHost) ->
fun({_, {Roomname, Host}, _}, Results) -> fun({_, {Roomname, Host}, _}, Results) ->
case MUCHost of case MUCHost of
global -> global ->
[str:join([Roomname, Host], "@") | Results]; [Roomname, <<"@">>, Host | Results];
Host -> Host ->
[str:join([Roomname, Host], "@") | Results]; [Roomname, <<"@">>, Host | Results];
_ -> _ ->
Results Results
end end
@ -258,7 +258,7 @@ get_sort_query(Q) ->
end. end.
get_sort_query2(Q) -> get_sort_query2(Q) ->
{value, {_, String}} = lists:keysearch("sort", 1, Q), {value, {_, String}} = lists:keysearch(<<"sort">>, 1, Q),
Integer = list_to_integer(String), Integer = list_to_integer(String),
case Integer >= 0 of case Integer >= 0 of
true -> {ok, {normal, Integer}}; true -> {ok, {normal, Integer}};
@ -631,7 +631,7 @@ find_serverhost(Host, ServerHosts) ->
act_on_room(destroy, {N, H, Pid}, SH) -> act_on_room(destroy, {N, H, Pid}, SH) ->
gen_fsm:send_all_state_event( gen_fsm:send_all_state_event(
Pid, {destroy, "Room destroyed by rooms_unused_destroy."}), Pid, {destroy, <<"Room destroyed by rooms_unused_destroy.">>}),
mod_muc:room_destroyed(H, N, Pid, SH), mod_muc:room_destroyed(H, N, Pid, SH),
mod_muc:forget_room(SH, H, N); mod_muc:forget_room(SH, H, N);
@ -697,18 +697,18 @@ get_users_to_invite(RoomJid, UsersString) ->
build_invitation(Password, Reason, RoomString) -> build_invitation(Password, Reason, RoomString) ->
PasswordAttrList = case Password of PasswordAttrList = case Password of
"none" -> []; "none" -> [];
_ -> [{"password", Password}] _ -> [{<<"password">>, Password}]
end, end,
ReasonAttrList = case Reason of ReasonAttrList = case Reason of
"none" -> []; "none" -> [];
_ -> [{"reason", Reason}] _ -> [{<<"reason">>, Reason}]
end, end,
XAttrs = [{"xmlns", ?NS_XCONFERENCE}, XAttrs = [{<<"xmlns">>, ?NS_XCONFERENCE},
{"jid", RoomString}] {<<"jid">>, RoomString}]
++ PasswordAttrList ++ PasswordAttrList
++ ReasonAttrList, ++ ReasonAttrList,
XEl = {xmlelement, "x", XAttrs, []}, XEl = {xmlelement, <<"x">>, XAttrs, []},
{xmlelement, "message", [], [XEl]}. {xmlelement, <<"message">>, [], [XEl]}.
send_direct_invitation(FromJid, UserJid, XmlEl) -> send_direct_invitation(FromJid, UserJid, XmlEl) ->
ejabberd_router:route(FromJid, UserJid, XmlEl). ejabberd_router:route(FromJid, UserJid, XmlEl).
@ -796,7 +796,7 @@ get_room_affiliations(Name, Service) ->
fun({{Uname, Domain, _Res}, {Aff, Reason}}) when is_atom(Aff)-> fun({{Uname, Domain, _Res}, {Aff, Reason}}) when is_atom(Aff)->
{Uname, Domain, Aff, Reason}; {Uname, Domain, Aff, Reason};
({{Uname, Domain, _Res}, Aff}) when is_atom(Aff)-> ({{Uname, Domain, _Res}, Aff}) when is_atom(Aff)->
{Uname, Domain, Aff, ""} {Uname, Domain, Aff, <<>>}
end, Affiliations); end, Affiliations);
[] -> [] ->
throw({error, "The room does not exist."}) throw({error, "The room does not exist."})

View File

@ -56,11 +56,11 @@ process([], #request{method = 'POST',
catch catch
error:{badmatch, _} = Error -> error:{badmatch, _} = Error ->
?DEBUG("Error when processing REST request: ~nData: ~p~nError: ~p", [Data, Error]), ?DEBUG("Error when processing REST request: ~nData: ~p~nError: ~p", [Data, Error]),
{406, [], "Error: REST request is rejected by service."} {406, [], <<"Error: REST request is rejected by service.">>}
end; end;
process(Path, Request) -> process(Path, Request) ->
?DEBUG("Got request to ~p: ~p", [Path, Request]), ?DEBUG("Got request to ~p: ~p", [Path, Request]),
{200, [], "Try POSTing a stanza."}. {200, [], <<"Try POSTing a stanza.">>}.
%% If the first character of Data is <, it is considered a stanza to deliver. %% If the first character of Data is <, it is considered a stanza to deliver.
@ -68,8 +68,8 @@ process(Path, Request) ->
maybe_post_request([$< | _ ] = Data, Host, ClientIp) -> maybe_post_request([$< | _ ] = Data, Host, ClientIp) ->
try try
Stanza = xml_stream:parse_element(Data), Stanza = xml_stream:parse_element(Data),
From = jlib:string_to_jid(xml:get_tag_attr_s("from", Stanza)), From = jlib:string_to_jid(xml:get_tag_attr_s(<<"from">>, Stanza)),
To = jlib:string_to_jid(xml:get_tag_attr_s("to", Stanza)), To = jlib:string_to_jid(xml:get_tag_attr_s(<<"to">>, Stanza)),
allowed = check_stanza(Stanza, From, To, Host), allowed = check_stanza(Stanza, From, To, Host),
?INFO_MSG("Got valid request from ~s~nwith IP ~p~nto ~s:~n~p", ?INFO_MSG("Got valid request from ~s~nwith IP ~p~nto ~s:~n~p",
[jlib:jid_to_string(From), [jlib:jid_to_string(From),
@ -129,8 +129,8 @@ check_member_option(Host, Element, Option) ->
post_request(Stanza, From, To) -> post_request(Stanza, From, To) ->
case ejabberd_router:route(From, To, Stanza) of case ejabberd_router:route(From, To, Stanza) of
ok -> {200, [], "Ok"}; ok -> {200, [], <<"Ok">>};
_ -> {500, [], "Error"} _ -> {500, [], <<"Error">>}
end. end.
%% Split a line into args. Args are splitted by blankspaces. Args can be enclosed in "". %% Split a line into args. Args are splitted by blankspaces. Args can be enclosed in "".
@ -142,7 +142,7 @@ post_request(Stanza, From, To) ->
%% 32 is the integer that represents the blankspace %% 32 is the integer that represents the blankspace
%% 34 is the integer that represents the double quotes: " %% 34 is the integer that represents the double quotes: "
%% 92 is the integer that represents the backslash: \ %% 92 is the integer that represents the backslash: \
split_line(Line) -> split(Line, "", []). split_line(Line) -> list_to_binary(split(binary_to_list(Line), "", [])).
split("", "", Args) -> lists:reverse(Args); split("", "", Args) -> lists:reverse(Args);
split("", Arg, Args) -> split("", "", [lists:reverse(Arg) | Args]); split("", Arg, Args) -> split("", "", [lists:reverse(Arg) | Args]);
split([32 | Line], "", Args) -> split(Line, [], Args); split([32 | Line], "", Args) -> split(Line, [], Args);

View File

@ -37,14 +37,14 @@ stop(_Host) ->
%%------------------- %%-------------------
web_menu_node(Acc, _Node, Lang) -> web_menu_node(Acc, _Node, Lang) ->
Acc ++ [{"shcommands", ?T("Shell Commands")}]. Acc ++ [{<<"shcommands">>, ?T(<<"Shell Commands">>)}].
%%------------------- %%-------------------
%% Web Admin Page %% Web Admin Page
%%------------------- %%-------------------
web_page_node(_, Node, ["shcommands"], Query, Lang) -> web_page_node(_, Node, [<<"shcommands">>], Query, Lang) ->
Res = [?XC("h1", "Shell Commands") | get_content(Node, Query, Lang)], Res = [?XC(<<"h1">>, <<"Shell Commands">>) | get_content(Node, Query, Lang)],
{stop, Res}; {stop, Res};
web_page_node(Acc, _, _, _, _) -> Acc. web_page_node(Acc, _, _, _, _) -> Acc.
@ -53,46 +53,46 @@ web_page_node(Acc, _, _, _, _) -> Acc.
%%------------------- %%-------------------
get_content(Node, Query, Lang) -> get_content(Node, Query, Lang) ->
Instruct = ?T("Type a command in a textbox and click Execute."), Instruct = ?T(<<"Type a command in a textbox and click Execute.">>),
{{CommandCtl, CommandErl, CommandShell}, Res} = case catch parse_and_execute(Query, Node) of {{CommandCtl, CommandErl, CommandShell}, Res} = case catch parse_and_execute(Query, Node) of
{'EXIT', _} -> {{"", "", ""}, Instruct}; {'EXIT', _} -> {{<<"">>, <<"">>, <<"">>}, Instruct};
Result_tuple -> Result_tuple Result_tuple -> Result_tuple
end, end,
TitleHTML = [ TitleHTML = [
?XC("p", Instruct++" "++?T("Use only commands which immediately return a result.")), ?XC(<<"p">>, ?T(<<"Type a command in a textbox and click Execute. Use only commands which immediately return a result.">>)),
?XC("p", ?T("WARNING: Use this only if you know what you are doing.")) ?XC(<<"p">>, ?T(<<"WARNING: Use this only if you know what you are doing.">>))
], ],
CommandHTML = CommandHTML =
[?XAE("form", [{"method", "post"}], [?XAE(<<"form">>, [{<<"method">>, <<"post">>}],
[?XAE("table", [], [?XAE(<<"table">>, [],
[?XE("tbody", [?XE(<<"tbody">>,
[?XE("tr", [?XE(<<"tr">>,
[?X("td"), [?X(<<"td">>),
?XCT("td", "ejabberd_ctl"), ?XCT(<<"td">>, <<"ejabberd_ctl">>),
?XE("td", [?INPUTS("text", "commandctl", CommandCtl, "70"), ?XE(<<"td">>, [?INPUTS(<<"text">>, <<"commandctl">>, CommandCtl, <<"70">>),
?INPUTT("submit", "executectl", "Execute")]) ?INPUTT(<<"submit">>, <<"executectl">>, <<"Execute">>)])
] ]
), ),
?XE("tr", ?XE(<<"tr">>,
[?X("td"), [?X(<<"td">>),
?XCT("td", "erlang shell"), ?XCT(<<"td">>, <<"erlang shell">>),
?XE("td", [?INPUTS("text", "commanderl", CommandErl, "70"), ?XE(<<"td">>, [?INPUTS(<<"text">>, <<"commanderl">>, CommandErl, <<"70">>),
?INPUTT("submit", "executeerl", "Execute")]) ?INPUTT(<<"submit">>, <<"executeerl">>, <<"Execute">>)])
] ]
), ),
?XE("tr", ?XE(<<"tr">>,
[?X("td"), [?X(<<"td">>),
?XCT("td", "system shell"), ?XCT(<<"td">>, <<"system shell">>),
?XE("td", [?INPUTS("text", "commandshe", CommandShell, "70"), ?XE(<<"td">>, [?INPUTS(<<"text">>, <<"commandshe">>, CommandShell, <<"70">>),
?INPUTT("submit", "executeshe", "Execute")]) ?INPUTT(<<"submit">>, <<"executeshe">>, <<"Execute">>)])
] ]
) )
] ]
)])] )])]
)], )],
ResHTML = ResHTML =
[?XAC("textarea", [{"wrap", "off"}, {"style", "font-family:monospace;"}, [?XAC(<<"textarea">>, [{<<"wrap">>, <<"off">>}, {<<"style">>, <<"font-family:monospace;">>},
{"name", "result"}, {"rows", "30"}, {"cols", "80"}], {<<"name">>, <<"result">>}, {<<"rows">>, <<"30">>}, {<<"cols">>, <<"80">>}],
Res) Res)
], ],
TitleHTML ++ CommandHTML ++ ResHTML. TitleHTML ++ CommandHTML ++ ResHTML.
@ -102,10 +102,10 @@ parse_and_execute(Query, Node) ->
fun(ExType) -> fun(ExType) ->
lists:keymember(ExType, 1, Query) lists:keymember(ExType, 1, Query)
end, end,
["executectl", "executeerl", "executeshe"]), [<<"executectl">>, <<"executeerl">>, <<"executeshe">>]),
Commands = {get_val("commandctl", Query), Commands = {get_val(<<"commandctl">>, Query),
get_val("commanderl", Query), get_val(<<"commanderl">>, Query),
get_val("commandshe", Query)}, get_val(<<"commandshe">>, Query)},
R = parse1_command(Exec, Commands, Node), R = parse1_command(Exec, Commands, Node),
{Commands, R}. {Commands, R}.
@ -113,28 +113,28 @@ get_val(Val, Query) ->
{value, {_, R}} = lists:keysearch(Val, 1, Query), {value, {_, R}} = lists:keysearch(Val, 1, Query),
R. R.
parse1_command("executectl", {Command, _, _}, Node) -> parse1_command(<<"executectl">>, {Command, _, _}, Node) ->
Command2 = string:tokens(Command, " "), Command2 = string:tokens(Command, <<" ">>),
{_E, Efile} = execute(ctl, Node, Command2), {_E, Efile} = execute(ctl, Node, Command2),
io_lib:format("ejabberdctl ~p ~s~n~s", [Node, Command, Efile]); io_lib:format("ejabberdctl ~p ~s~n~s", [Node, Command, Efile]);
parse1_command("executeerl", {_, Command, _}, Node) -> parse1_command(<<"executeerl">>, {_, Command, _}, Node) ->
{ok, A2, _} = erl_scan:string(Command), {ok, A2, _} = erl_scan:string(Command),
{ok, A3} = erl_parse:parse_exprs(A2), {ok, A3} = erl_parse:parse_exprs(A2),
{E, Efile} = execute(erl, Node, A3), {E, Efile} = execute(erl, Node, A3),
io_lib:format("(~p)1> ~s~s~n~p", [Node, Command, Efile, E]); io_lib:format("(~p)1> ~s~s~n~p", [Node, Command, Efile, E]);
parse1_command("executeshe", {_, _, Command}, Node) -> parse1_command(<<"executeshe">>, {_, _, Command}, Node) ->
E = rpc:call(Node, os, cmd, [Command]), E = rpc:call(Node, os, cmd, [Command]),
C1 = lists:map( C1 = lists:map(
fun(C) -> string:strip(os:cmd(C), right, $\n) end, fun(C) -> string:strip(os:cmd(C), right, $\n) end,
["whoami", "hostname -s", "pwd"]), [<<"whoami">>, <<"hostname -s">>, <<"pwd">>]),
io_lib:format("~s@~s:~s$ ~s~n~s", C1 ++ [Command, E]). io_lib:format("~s@~s:~s$ ~s~n~s", C1 ++ [Command, E]).
execute(Type, Node, C) -> execute(Type, Node, C) ->
GL = group_leader(), GL = group_leader(),
Filename = "temp" ++ io_lib:format("~p", [random:uniform()*10000]), Filename = <<"temp">> ++ io_lib:format("~p", [random:uniform()*10000]),
{ok, File} = file:open(Filename, [write]), {ok, File} = file:open(Filename, [write]),
group_leader(File, self()), group_leader(File, self()),
Res = case Type of Res = case Type of
@ -151,6 +151,6 @@ execute(Type, Node, C) ->
file:delete(Filename), file:delete(Filename),
E2 = case binary_to_list(B) of E2 = case binary_to_list(B) of
[] -> []; [] -> [];
List -> "\n"++List List -> <<"\n">>++List
end, end,
{E, E2}. {E, E2}.