diff options
author | Badlop <badlop@process-one.net> | 2013-03-14 13:33:02 +0400 |
---|---|---|
committer | Badlop <badlop@process-one.net> | 2013-03-14 13:33:02 +0400 |
commit | 9deb294328bb3f9eb6bd2c0e7cd500732e9b5830 (patch) | |
tree | 7e1066c130250627ee0abab44a135f583a28d07f /src/mod_echo.erl | |
parent | 9c41abde101395111efcda16aa2fd9625f4c6207 (diff) |
Accumulated patch to binarize and indent code
Diffstat (limited to 'src/mod_echo.erl')
-rw-r--r-- | src/mod_echo.erl | 103 |
1 files changed, 49 insertions, 54 deletions
diff --git a/src/mod_echo.erl b/src/mod_echo.erl index 9c7259481..15df69244 100644 --- a/src/mod_echo.erl +++ b/src/mod_echo.erl @@ -25,22 +25,26 @@ %%%---------------------------------------------------------------------- -module(mod_echo). + -author('alexey@process-one.net'). -behaviour(gen_server). + -behaviour(gen_mod). %% API --export([start_link/2, start/2, stop/1, do_client_version/3]). +-export([start_link/2, start/2, stop/1, + do_client_version/3]). %% gen_server callbacks --export([init/1, handle_call/3, handle_cast/2, handle_info/2, - terminate/2, code_change/3]). +-export([init/1, handle_call/3, handle_cast/2, + handle_info/2, terminate/2, code_change/3]). -include("ejabberd.hrl"). + -include("jlib.hrl"). --record(state, {host}). +-record(state, {host = <<"">> :: binary()}). -define(PROCNAME, ejabberd_mod_echo). @@ -53,17 +57,13 @@ %%-------------------------------------------------------------------- start_link(Host, Opts) -> Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - gen_server:start_link({local, Proc}, ?MODULE, [Host, Opts], []). + gen_server:start_link({local, Proc}, ?MODULE, + [Host, Opts], []). start(Host, Opts) -> Proc = gen_mod:get_module_proc(Host, ?PROCNAME), - ChildSpec = - {Proc, - {?MODULE, start_link, [Host, Opts]}, - temporary, - 1000, - worker, - [?MODULE]}, + ChildSpec = {Proc, {?MODULE, start_link, [Host, Opts]}, + temporary, 1000, worker, [?MODULE]}, supervisor:start_child(ejabberd_sup, ChildSpec). stop(Host) -> @@ -72,7 +72,6 @@ stop(Host) -> supervisor:terminate_child(ejabberd_sup, Proc), supervisor:delete_child(ejabberd_sup, Proc). - %%==================================================================== %% gen_server callbacks %%==================================================================== @@ -85,7 +84,8 @@ stop(Host) -> %% Description: Initiates the server %%-------------------------------------------------------------------- init([Host, Opts]) -> - MyHost = gen_mod:get_opt_host(Host, Opts, "echo.@HOST@"), + MyHost = gen_mod:get_opt_host(Host, Opts, + <<"echo.@HOST@">>), ejabberd_router:register_route(MyHost), {ok, #state{host = MyHost}}. @@ -107,8 +107,7 @@ handle_call(stop, _From, State) -> %% {stop, Reason, State} %% Description: Handling cast messages %%-------------------------------------------------------------------- -handle_cast(_Msg, State) -> - {noreply, State}. +handle_cast(_Msg, State) -> {noreply, State}. %%-------------------------------------------------------------------- %% Function: handle_info(Info, State) -> {noreply, State} | @@ -117,15 +116,15 @@ handle_cast(_Msg, State) -> %% Description: Handling all non call/cast messages %%-------------------------------------------------------------------- handle_info({route, From, To, Packet}, State) -> - Packet2 = case From#jid.user of - "" -> jlib:make_error_reply(Packet, ?ERR_BAD_REQUEST); + Packet2 = case From#jid.user of + <<"">> -> + jlib:make_error_reply(Packet, ?ERR_BAD_REQUEST); _ -> Packet - end, - do_client_version(disabled, To, From), % Put 'enabled' to enable it + end, + do_client_version(disabled, To, From), ejabberd_router:route(To, From, Packet2), {noreply, State}; -handle_info(_Info, State) -> - {noreply, State}. +handle_info(_Info, State) -> {noreply, State}. %%-------------------------------------------------------------------- %% Function: terminate(Reason, State) -> void() @@ -135,15 +134,13 @@ handle_info(_Info, State) -> %% The return value is ignored. %%-------------------------------------------------------------------- terminate(_Reason, State) -> - ejabberd_router:unregister_route(State#state.host), - ok. + ejabberd_router:unregister_route(State#state.host), ok. %%-------------------------------------------------------------------- %% Func: code_change(OldVsn, State, Extra) -> {ok, NewState} %% Description: Convert process state when code is changed %%-------------------------------------------------------------------- -code_change(_OldVsn, State, _Extra) -> - {ok, State}. +code_change(_OldVsn, State, _Extra) -> {ok, State}. %%-------------------------------------------------------------------- %% Example of routing XMPP packets using Erlang's message passing @@ -168,36 +165,34 @@ code_change(_OldVsn, State, _Extra) -> %% using exactly the same JID. We add a (mostly) random resource to %% try to guarantee that the received response matches the request sent. %% Finally, the received response is printed in the ejabberd log file. -do_client_version(disabled, _From, _To) -> - ok; +do_client_version(disabled, _From, _To) -> ok; do_client_version(enabled, From, To) -> ToS = jlib:jid_to_string(To), - %% It is important to identify this process and packet - Random_resource = integer_to_list(random:uniform(100000)), + Random_resource = + iolist_to_binary(integer_to_list(random:uniform(100000))), From2 = From#jid{resource = Random_resource, lresource = Random_resource}, - - %% Build an iq:query request - Packet = {xmlelement, "iq", - [{"to", ToS}, {"type", "get"}], - [{xmlelement, "query", [{"xmlns", ?NS_VERSION}], []}]}, - - %% Send the request - ejabberd_router:route(From2, To, Packet), - - %% Wait to receive the response - %% It is very important to only accept a packet which is the - %% response to the request that he sent - Els = receive {route, To, From2, IQ} -> - {xmlelement, "query", _, List} = xml:get_subtag(IQ, "query"), - List - after 5000 -> % Timeout in miliseconds: 5 seconds - [] + Packet = #xmlel{name = <<"iq">>, + attrs = [{<<"to">>, ToS}, {<<"type">>, <<"get">>}], + children = + [#xmlel{name = <<"query">>, + attrs = [{<<"xmlns">>, ?NS_VERSION}], + children = []}]}, + ejabberd_router:route(From2, To, Packet), + Els = receive + {route, To, From2, IQ} -> + #xmlel{name = <<"query">>, children = List} = + xml:get_subtag(IQ, <<"query">>), + List + after 5000 -> % Timeout in miliseconds: 5 seconds + [] end, - Values = [{Name, Value} || {xmlelement,Name,[],[{xmlcdata,Value}]} <- Els], - - %% Print in log - Values_string1 = [io_lib:format("~n~s: ~p", [N, V]) || {N, V} <- Values], - Values_string2 = lists:concat(Values_string1), - ?INFO_MSG("Information of the client: ~s~s", [ToS, Values_string2]). - + Values = [{Name, Value} + || #xmlel{name = Name, attrs = [], + children = [{xmlcdata, Value}]} + <- Els], + Values_string1 = [io_lib:format("~n~s: ~p", [N, V]) + || {N, V} <- Values], + Values_string2 = iolist_to_binary(Values_string1), + ?INFO_MSG("Information of the client: ~s~s", + [ToS, Values_string2]). |