2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : ejabberd_service.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2008-08-09 20:08:00 +02:00
|
|
|
%%% Purpose : External component management (XEP-0114)
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 6 Dec 2002 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2015-01-08 17:34:43 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2015 ProcessOne
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
|
|
|
%%% This program is free software; you can redistribute it and/or
|
|
|
|
%%% modify it under the terms of the GNU General Public License as
|
|
|
|
%%% published by the Free Software Foundation; either version 2 of the
|
|
|
|
%%% License, or (at your option) any later version.
|
|
|
|
%%%
|
|
|
|
%%% This program is distributed in the hope that it will be useful,
|
|
|
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
%%% General Public License for more details.
|
2009-01-12 15:44:42 +01:00
|
|
|
%%%
|
2014-02-22 11:27:40 +01:00
|
|
|
%%% You should have received a copy of the GNU General Public License along
|
|
|
|
%%% with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 12:41:41 +01:00
|
|
|
%%%
|
2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(ejabberd_service).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
-behaviour(ejabberd_config).
|
|
|
|
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 114, '1.6'}).
|
|
|
|
|
2009-10-07 15:41:36 +02:00
|
|
|
-define(GEN_FSM, p1_fsm).
|
|
|
|
|
|
|
|
-behaviour(?GEN_FSM).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
%% External exports
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, start_link/2, send_text/2,
|
2013-08-12 14:25:05 +02:00
|
|
|
send_element/2, socket_type/0, transform_listen_option/2]).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([init/1, wait_for_stream/2,
|
|
|
|
wait_for_handshake/2, stream_established/2,
|
|
|
|
handle_event/3, handle_sync_event/4, code_change/4,
|
2015-06-03 15:05:17 +02:00
|
|
|
handle_info/3, terminate/3, print_state/1, opt_type/1]).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-05-09 21:32:59 +02:00
|
|
|
-include("jlib.hrl").
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(state,
|
|
|
|
{socket :: ejabberd_socket:socket_state(),
|
|
|
|
sockmod = ejabberd_socket :: ejabberd_socket | ejabberd_frontend_socket,
|
|
|
|
streamid = <<"">> :: binary(),
|
|
|
|
hosts = [] :: [binary()],
|
|
|
|
password = <<"">> :: binary(),
|
|
|
|
access :: atom(),
|
|
|
|
check_from = true :: boolean()}).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2007-06-29 19:27:42 +02:00
|
|
|
%-define(DBGFSM, true).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
-ifdef(DBGFSM).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
-define(FSMOPTS, [{debug, [trace]}]).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
-else.
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
-define(FSMOPTS, []).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
-endif.
|
|
|
|
|
|
|
|
-define(STREAM_HEADER,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"<?xml version='1.0'?><stream:stream "
|
|
|
|
"xmlns:stream='http://etherx.jabber.org/stream"
|
|
|
|
"s' xmlns='jabber:component:accept' id='~s' "
|
|
|
|
"from='~s'>">>).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-define(STREAM_TRAILER, <<"</stream:stream>">>).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
-define(INVALID_HEADER_ERR,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"<stream:stream xmlns:stream='http://etherx.ja"
|
|
|
|
"bber.org/streams'><stream:error>Invalid "
|
|
|
|
"Stream Header</stream:error></stream:stream>">>).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
-define(INVALID_HANDSHAKE_ERR,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"<stream:error><not-authorized xmlns='urn:ietf"
|
|
|
|
":params:xml:ns:xmpp-streams'/><text "
|
|
|
|
"xmlns='urn:ietf:params:xml:ns:xmpp-streams' "
|
|
|
|
"xml:lang='en'>Invalid Handshake</text></strea"
|
|
|
|
"m:error></stream:stream>">>).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2003-05-29 21:07:41 +02:00
|
|
|
-define(INVALID_XML_ERR,
|
2013-03-14 10:33:02 +01:00
|
|
|
xml:element_to_binary(?SERR_XML_NOT_WELL_FORMED)).
|
|
|
|
|
2003-05-29 21:07:41 +02:00
|
|
|
-define(INVALID_NS_ERR,
|
2013-03-14 10:33:02 +01:00
|
|
|
xml:element_to_binary(?SERR_INVALID_NAMESPACE)).
|
2003-05-29 21:07:41 +02:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% API
|
|
|
|
%%%----------------------------------------------------------------------
|
2003-07-20 22:35:35 +02:00
|
|
|
start(SockData, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
supervisor:start_child(ejabberd_service_sup,
|
|
|
|
[SockData, Opts]).
|
2003-07-20 22:35:35 +02:00
|
|
|
|
2003-07-14 20:06:03 +02:00
|
|
|
start_link(SockData, Opts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
(?GEN_FSM):start_link(ejabberd_service,
|
|
|
|
[SockData, Opts], fsm_limit_opts(Opts) ++ (?FSMOPTS)).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
socket_type() -> xml_stream.
|
2006-09-25 05:51:11 +02:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Callback functions from gen_fsm
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: init/1
|
|
|
|
%% Returns: {ok, StateName, StateData} |
|
|
|
|
%% {ok, StateName, StateData, Timeout} |
|
|
|
|
%% ignore |
|
2007-03-13 16:01:27 +01:00
|
|
|
%% {stop, StopReason}
|
2003-01-07 20:10:35 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2006-10-01 03:53:37 +02:00
|
|
|
init([{SockMod, Socket}, Opts]) ->
|
2007-03-13 17:01:39 +01:00
|
|
|
?INFO_MSG("(~w) External service connected", [Socket]),
|
2004-01-11 21:42:57 +01:00
|
|
|
Access = case lists:keysearch(access, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, {_, A}} -> A;
|
|
|
|
_ -> all
|
2004-01-11 21:42:57 +01:00
|
|
|
end,
|
2013-08-12 14:25:05 +02:00
|
|
|
%% This should be improved probably
|
|
|
|
{Hosts, HostOpts} = case lists:keyfind(hosts, 1, Opts) of
|
|
|
|
{_, HOpts} ->
|
|
|
|
{[H || {H, _} <- HOpts],
|
|
|
|
lists:flatten(
|
|
|
|
[O || {_, O} <- HOpts])};
|
|
|
|
_ ->
|
|
|
|
{[], []}
|
|
|
|
end,
|
|
|
|
Password = gen_mod:get_opt(password, HostOpts,
|
|
|
|
fun iolist_to_binary/1,
|
|
|
|
p1_sha:sha(crypto:rand_bytes(20))),
|
2007-03-13 17:01:39 +01:00
|
|
|
Shaper = case lists:keysearch(shaper_rule, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, {_, S}} -> S;
|
|
|
|
_ -> none
|
2007-06-28 16:32:48 +02:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
CheckFrom = case lists:keysearch(service_check_from, 1,
|
|
|
|
Opts)
|
|
|
|
of
|
|
|
|
{value, {_, CF}} -> CF;
|
|
|
|
_ -> true
|
|
|
|
end,
|
2007-03-13 17:01:39 +01:00
|
|
|
SockMod:change_shaper(Socket, Shaper),
|
2013-03-14 10:33:02 +01:00
|
|
|
{ok, wait_for_stream,
|
|
|
|
#state{socket = Socket, sockmod = SockMod,
|
|
|
|
streamid = new_id(), hosts = Hosts, password = Password,
|
|
|
|
access = Access, check_from = CheckFrom}}.
|
2003-01-07 20:10:35 +01:00
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/2
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2007-03-13 16:01:27 +01:00
|
|
|
%% {stop, Reason, NewStateData}
|
2003-01-07 20:10:35 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
wait_for_stream({xmlstreamstart, _Name, Attrs},
|
|
|
|
StateData) ->
|
|
|
|
case xml:get_attr_s(<<"xmlns">>, Attrs) of
|
|
|
|
<<"jabber:component:accept">> ->
|
|
|
|
To = xml:get_attr_s(<<"to">>, Attrs),
|
|
|
|
Header = io_lib:format(?STREAM_HEADER,
|
|
|
|
[StateData#state.streamid, xml:crypt(To)]),
|
|
|
|
send_text(StateData, Header),
|
|
|
|
{next_state, wait_for_handshake, StateData};
|
|
|
|
_ ->
|
|
|
|
send_text(StateData, ?INVALID_HEADER_ERR),
|
|
|
|
{stop, normal, StateData}
|
2003-01-07 20:10:35 +01:00
|
|
|
end;
|
2003-05-29 21:07:41 +02:00
|
|
|
wait_for_stream({xmlstreamerror, _}, StateData) ->
|
|
|
|
Header = io_lib:format(?STREAM_HEADER,
|
2013-03-14 10:33:02 +01:00
|
|
|
[<<"none">>, ?MYNAME]),
|
2003-10-18 21:15:12 +02:00
|
|
|
send_text(StateData,
|
2013-03-14 10:33:02 +01:00
|
|
|
<<(list_to_binary(Header))/binary, (?INVALID_XML_ERR)/binary,
|
|
|
|
(?STREAM_TRAILER)/binary>>),
|
2003-05-29 21:07:41 +02:00
|
|
|
{stop, normal, StateData};
|
2003-01-07 20:10:35 +01:00
|
|
|
wait_for_stream(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
wait_for_handshake({xmlstreamelement, El}, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = Name, children = Els} = El,
|
2003-01-07 20:10:35 +01:00
|
|
|
case {Name, xml:get_cdata(Els)} of
|
2013-03-14 10:33:02 +01:00
|
|
|
{<<"handshake">>, Digest} ->
|
2013-06-20 10:40:44 +02:00
|
|
|
case p1_sha:sha(<<(StateData#state.streamid)/binary,
|
2013-03-14 10:33:02 +01:00
|
|
|
(StateData#state.password)/binary>>)
|
|
|
|
of
|
|
|
|
Digest ->
|
|
|
|
send_text(StateData, <<"<handshake/>">>),
|
|
|
|
lists:foreach(fun (H) ->
|
|
|
|
ejabberd_router:register_route(H),
|
|
|
|
?INFO_MSG("Route registered for service ~p~n",
|
|
|
|
[H])
|
|
|
|
end,
|
|
|
|
StateData#state.hosts),
|
|
|
|
{next_state, stream_established, StateData};
|
|
|
|
_ ->
|
|
|
|
send_text(StateData, ?INVALID_HANDSHAKE_ERR),
|
|
|
|
{stop, normal, StateData}
|
|
|
|
end;
|
|
|
|
_ -> {next_state, wait_for_handshake, StateData}
|
2003-01-07 20:10:35 +01:00
|
|
|
end;
|
2003-10-18 21:15:12 +02:00
|
|
|
wait_for_handshake({xmlstreamend, _Name}, StateData) ->
|
2003-01-07 20:10:35 +01:00
|
|
|
{stop, normal, StateData};
|
2003-05-29 21:07:41 +02:00
|
|
|
wait_for_handshake({xmlstreamerror, _}, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
send_text(StateData,
|
|
|
|
<<(?INVALID_XML_ERR)/binary,
|
|
|
|
(?STREAM_TRAILER)/binary>>),
|
2003-05-29 21:07:41 +02:00
|
|
|
{stop, normal, StateData};
|
2003-01-07 20:10:35 +01:00
|
|
|
wait_for_handshake(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
stream_established({xmlstreamelement, El}, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
NewEl = jlib:remove_attr(<<"xmlns">>, El),
|
|
|
|
#xmlel{name = Name, attrs = Attrs} = NewEl,
|
|
|
|
From = xml:get_attr_s(<<"from">>, Attrs),
|
2007-06-28 16:32:48 +02:00
|
|
|
FromJID = case StateData#state.check_from of
|
2013-03-14 10:33:02 +01:00
|
|
|
%% If the admin does not want to check the from field
|
|
|
|
%% when accept packets from any address.
|
|
|
|
%% In this case, the component can send packet of
|
|
|
|
%% behalf of the server users.
|
|
|
|
false -> jlib:string_to_jid(From);
|
|
|
|
%% The default is the standard behaviour in XEP-0114
|
|
|
|
_ ->
|
|
|
|
FromJID1 = jlib:string_to_jid(From),
|
|
|
|
case FromJID1 of
|
|
|
|
#jid{lserver = Server} ->
|
|
|
|
case lists:member(Server, StateData#state.hosts) of
|
|
|
|
true -> FromJID1;
|
|
|
|
false -> error
|
|
|
|
end;
|
|
|
|
_ -> error
|
|
|
|
end
|
2003-01-07 20:10:35 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
To = xml:get_attr_s(<<"to">>, Attrs),
|
2003-01-07 20:10:35 +01:00
|
|
|
ToJID = case To of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> error;
|
|
|
|
_ -> jlib:string_to_jid(To)
|
2003-01-07 20:10:35 +01:00
|
|
|
end,
|
2013-03-14 10:33:02 +01:00
|
|
|
if ((Name == <<"iq">>) or (Name == <<"message">>) or
|
|
|
|
(Name == <<"presence">>))
|
|
|
|
and (ToJID /= error)
|
|
|
|
and (FromJID /= error) ->
|
|
|
|
ejabberd_router:route(FromJID, ToJID, NewEl);
|
2003-01-07 20:10:35 +01:00
|
|
|
true ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Err = jlib:make_error_reply(NewEl, ?ERR_BAD_REQUEST),
|
|
|
|
send_element(StateData, Err),
|
|
|
|
error
|
2003-01-07 20:10:35 +01:00
|
|
|
end,
|
|
|
|
{next_state, stream_established, StateData};
|
2003-10-18 21:15:12 +02:00
|
|
|
stream_established({xmlstreamend, _Name}, StateData) ->
|
2003-01-07 20:10:35 +01:00
|
|
|
{stop, normal, StateData};
|
2003-05-29 21:07:41 +02:00
|
|
|
stream_established({xmlstreamerror, _}, StateData) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
send_text(StateData,
|
|
|
|
<<(?INVALID_XML_ERR)/binary,
|
|
|
|
(?STREAM_TRAILER)/binary>>),
|
2003-05-29 21:07:41 +02:00
|
|
|
{stop, normal, StateData};
|
2003-01-07 20:10:35 +01:00
|
|
|
stream_established(closed, StateData) ->
|
|
|
|
{stop, normal, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: StateName/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2007-03-13 16:01:27 +01:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2003-01-07 20:10:35 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%state_name(Event, From, StateData) ->
|
|
|
|
% Reply = ok,
|
|
|
|
% {reply, Reply, state_name, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_event/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2007-03-13 16:01:27 +01:00
|
|
|
%% {stop, Reason, NewStateData}
|
2003-01-07 20:10:35 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2003-10-18 21:15:12 +02:00
|
|
|
handle_event(_Event, StateName, StateData) ->
|
2003-01-07 20:10:35 +01:00
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_sync_event/4
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData} |
|
|
|
|
%% {reply, Reply, NextStateName, NextStateData, Timeout} |
|
|
|
|
%% {stop, Reason, NewStateData} |
|
2007-03-13 16:01:27 +01:00
|
|
|
%% {stop, Reason, Reply, NewStateData}
|
2003-01-07 20:10:35 +01:00
|
|
|
%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_sync_event(_Event, _From, StateName,
|
|
|
|
StateData) ->
|
|
|
|
Reply = ok, {reply, Reply, StateName, StateData}.
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2003-10-18 21:15:12 +02:00
|
|
|
code_change(_OldVsn, StateName, StateData, _Extra) ->
|
2003-01-07 20:10:35 +01:00
|
|
|
{ok, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: handle_info/3
|
|
|
|
%% Returns: {next_state, NextStateName, NextStateData} |
|
|
|
|
%% {next_state, NextStateName, NextStateData, Timeout} |
|
2007-03-13 16:01:27 +01:00
|
|
|
%% {stop, Reason, NewStateData}
|
2003-01-07 20:10:35 +01:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
handle_info({send_text, Text}, StateName, StateData) ->
|
2003-10-18 21:15:12 +02:00
|
|
|
send_text(StateData, Text),
|
2003-01-07 20:10:35 +01:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
handle_info({send_element, El}, StateName, StateData) ->
|
2003-10-18 21:15:12 +02:00
|
|
|
send_element(StateData, El),
|
2003-01-07 20:10:35 +01:00
|
|
|
{next_state, StateName, StateData};
|
2013-03-14 10:33:02 +01:00
|
|
|
handle_info({route, From, To, Packet}, StateName,
|
|
|
|
StateData) ->
|
|
|
|
case acl:match_rule(global, StateData#state.access,
|
|
|
|
From)
|
|
|
|
of
|
|
|
|
allow ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = Els} =
|
|
|
|
Packet,
|
|
|
|
Attrs2 =
|
|
|
|
jlib:replace_from_to_attrs(jlib:jid_to_string(From),
|
|
|
|
jlib:jid_to_string(To), Attrs),
|
|
|
|
Text = xml:element_to_binary(#xmlel{name = Name,
|
|
|
|
attrs = Attrs2, children = Els}),
|
|
|
|
send_text(StateData, Text);
|
|
|
|
deny ->
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_NOT_ALLOWED),
|
|
|
|
ejabberd_router:route_error(To, From, Err, Packet)
|
2004-01-11 21:42:57 +01:00
|
|
|
end,
|
2010-09-17 16:42:35 +02:00
|
|
|
{next_state, StateName, StateData};
|
|
|
|
handle_info(Info, StateName, StateData) ->
|
|
|
|
?ERROR_MSG("Unexpected info: ~p", [Info]),
|
2003-01-07 20:10:35 +01:00
|
|
|
{next_state, StateName, StateData}.
|
|
|
|
|
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: terminate/3
|
|
|
|
%% Purpose: Shutdown the fsm
|
|
|
|
%% Returns: any
|
|
|
|
%%----------------------------------------------------------------------
|
2005-12-10 19:42:08 +01:00
|
|
|
terminate(Reason, StateName, StateData) ->
|
|
|
|
?INFO_MSG("terminated: ~p", [Reason]),
|
2003-01-07 20:10:35 +01:00
|
|
|
case StateName of
|
2013-03-14 10:33:02 +01:00
|
|
|
stream_established ->
|
|
|
|
lists:foreach(fun (H) ->
|
|
|
|
ejabberd_router:unregister_route(H)
|
|
|
|
end,
|
|
|
|
StateData#state.hosts);
|
|
|
|
_ -> ok
|
2003-01-07 20:10:35 +01:00
|
|
|
end,
|
2006-10-01 03:53:37 +02:00
|
|
|
(StateData#state.sockmod):close(StateData#state.socket),
|
2003-01-07 20:10:35 +01:00
|
|
|
ok.
|
|
|
|
|
2010-04-09 19:25:00 +02:00
|
|
|
%%----------------------------------------------------------------------
|
|
|
|
%% Func: print_state/1
|
|
|
|
%% Purpose: Prepare the state to be printed on error log
|
|
|
|
%% Returns: State to print
|
|
|
|
%%----------------------------------------------------------------------
|
2013-03-14 10:33:02 +01:00
|
|
|
print_state(State) -> State.
|
2010-04-09 19:25:00 +02:00
|
|
|
|
2003-01-07 20:10:35 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% Internal functions
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2003-10-18 21:15:12 +02:00
|
|
|
send_text(StateData, Text) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
(StateData#state.sockmod):send(StateData#state.socket,
|
|
|
|
Text).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2003-10-18 21:15:12 +02:00
|
|
|
send_element(StateData, El) ->
|
2010-07-01 12:54:01 +02:00
|
|
|
send_text(StateData, xml:element_to_binary(El)).
|
2003-01-07 20:10:35 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
new_id() -> randoms:get_string().
|
2009-10-07 16:24:09 +02:00
|
|
|
|
2013-08-12 14:25:05 +02:00
|
|
|
transform_listen_option({hosts, Hosts, O}, Opts) ->
|
|
|
|
case lists:keyfind(hosts, 1, Opts) of
|
|
|
|
{_, PrevHostOpts} ->
|
|
|
|
NewHostOpts =
|
|
|
|
lists:foldl(
|
|
|
|
fun(H, Acc) ->
|
|
|
|
dict:append_list(H, O, Acc)
|
|
|
|
end, dict:from_list(PrevHostOpts), Hosts),
|
|
|
|
[{hosts, dict:to_list(NewHostOpts)}|
|
|
|
|
lists:keydelete(hosts, 1, Opts)];
|
|
|
|
_ ->
|
|
|
|
[{hosts, [{H, O} || H <- Hosts]}|Opts]
|
|
|
|
end;
|
|
|
|
transform_listen_option({host, Host, Os}, Opts) ->
|
|
|
|
transform_listen_option({hosts, [Host], Os}, Opts);
|
|
|
|
transform_listen_option(Opt, Opts) ->
|
|
|
|
[Opt|Opts].
|
|
|
|
|
2009-10-07 16:24:09 +02:00
|
|
|
fsm_limit_opts(Opts) ->
|
|
|
|
case lists:keysearch(max_fsm_queue, 1, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{value, {_, N}} when is_integer(N) ->
|
|
|
|
[{max_queue, N}];
|
|
|
|
_ ->
|
2013-08-12 14:25:05 +02:00
|
|
|
case ejabberd_config:get_option(
|
2013-03-14 10:33:02 +01:00
|
|
|
max_fsm_queue,
|
|
|
|
fun(I) when is_integer(I), I > 0 -> I end) of
|
|
|
|
undefined -> [];
|
|
|
|
N -> [{max_queue, N}]
|
|
|
|
end
|
2009-10-07 16:24:09 +02:00
|
|
|
end.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
opt_type(max_fsm_queue) ->
|
|
|
|
fun (I) when is_integer(I), I > 0 -> I end;
|
|
|
|
opt_type(_) -> [max_fsm_queue].
|