2003-11-02 18:16:28 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_configure2.erl
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2003-11-02 18:16:28 +01:00
|
|
|
%%% Purpose : Support for online configuration of ejabberd
|
2007-12-24 12:41:41 +01:00
|
|
|
%%% Created : 26 Oct 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2011-02-14 13:47:22 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2011 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-19 15:47:33 +01:00
|
|
|
%%%
|
2007-12-24 12:41:41 +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., 59 Temple Place, Suite 330, Boston, MA
|
|
|
|
%%% 02111-1307 USA
|
|
|
|
%%%
|
2003-11-02 18:16:28 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_configure2).
|
2007-12-24 12:41:41 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-11-02 18:16:28 +01:00
|
|
|
|
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2003-11-02 18:16:28 +01:00
|
|
|
process_local_iq/3]).
|
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-11-02 18:16:28 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
-define(NS_ECONFIGURE, 'http://ejabberd.jabberstudio.org/protocol/configure').
|
|
|
|
-define(NS_ECONFIGURE_s, "http://ejabberd.jabberstudio.org/protocol/configure").
|
2003-11-02 18:16:28 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2003-11-02 18:16:28 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, list_to_binary(Host), ?NS_ECONFIGURE,
|
2003-11-02 18:16:28 +01:00
|
|
|
?MODULE, process_local_iq, IQDisc),
|
2008-09-29 15:19:11 +02:00
|
|
|
% Add nss/names/attrs used by this module to the known lists of Exmpp.
|
2009-01-21 14:34:26 +01:00
|
|
|
exmpp_xml:add_known_nss(xmpp, [?NS_ECONFIGURE]),
|
|
|
|
exmpp_xml:add_known_elems(xmpp, [
|
2008-09-29 15:19:11 +02:00
|
|
|
'access',
|
|
|
|
'acls',
|
|
|
|
'body',
|
|
|
|
'info',
|
|
|
|
'jid',
|
|
|
|
'last',
|
|
|
|
'registration-watchers',
|
|
|
|
'subject',
|
|
|
|
'welcome-message'
|
|
|
|
]),
|
2003-11-02 18:16:28 +01:00
|
|
|
ok.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, list_to_binary(Host), ?NS_ECONFIGURE).
|
2003-11-02 18:16:28 +01:00
|
|
|
|
|
|
|
|
2008-08-14 15:36:11 +02:00
|
|
|
process_local_iq(From, To, #iq{type = Type, payload = Request} = IQ_Rec) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
case acl:match_rule(exmpp_jid:prep_domain_as_list(To), configure, From) of
|
2003-11-02 18:16:28 +01:00
|
|
|
deny ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'not-allowed');
|
2003-11-02 18:16:28 +01:00
|
|
|
allow ->
|
2008-08-14 15:36:11 +02:00
|
|
|
case Type of
|
2003-11-02 18:16:28 +01:00
|
|
|
set ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, 'feature-not-implemented');
|
2007-12-07 01:09:48 +01:00
|
|
|
%%case xml:get_tag_attr_s("type", SubEl) of
|
|
|
|
%% "cancel" ->
|
|
|
|
%% IQ#iq{type = result,
|
|
|
|
%% sub_el = [{xmlelement, "query",
|
|
|
|
%% [{"xmlns", XMLNS}], []}]};
|
|
|
|
%% "submit" ->
|
|
|
|
%% XData = jlib:parse_xdata_submit(SubEl),
|
|
|
|
%% case XData of
|
|
|
|
%% invalid ->
|
|
|
|
%% IQ#iq{type = error,
|
|
|
|
%% sub_el = [SubEl, ?ERR_BAD_REQUEST]};
|
|
|
|
%% _ ->
|
|
|
|
%% Node =
|
|
|
|
%% string:tokens(
|
|
|
|
%% xml:get_tag_attr_s("node", SubEl),
|
|
|
|
%% "/"),
|
|
|
|
%% case set_form(Node, Lang, XData) of
|
|
|
|
%% {result, Res} ->
|
|
|
|
%% IQ#iq{type = result,
|
|
|
|
%% sub_el = [{xmlelement, "query",
|
|
|
|
%% [{"xmlns", XMLNS}],
|
|
|
|
%% Res
|
|
|
|
%% }]};
|
|
|
|
%% {error, Error} ->
|
|
|
|
%% IQ#iq{type = error,
|
|
|
|
%% sub_el = [SubEl, Error]}
|
|
|
|
%% end
|
|
|
|
%% end;
|
|
|
|
%% _ ->
|
|
|
|
%% IQ#iq{type = error,
|
|
|
|
%% sub_el = [SubEl, ?ERR_NOT_ALLOWED]}
|
|
|
|
%%end;
|
2003-11-02 18:16:28 +01:00
|
|
|
get ->
|
2008-08-14 15:36:11 +02:00
|
|
|
case process_get(Request) of
|
2003-11-02 18:16:28 +01:00
|
|
|
{result, Res} ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:result(IQ_Rec, Res);
|
2003-11-02 18:16:28 +01:00
|
|
|
{error, Error} ->
|
2008-08-14 15:36:11 +02:00
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
2003-11-02 18:16:28 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2008-08-06 15:44:58 +02:00
|
|
|
process_get(#xmlel{ns = ?NS_ECONFIGURE, name = 'info'}) ->
|
2003-11-06 21:35:10 +01:00
|
|
|
S2SConns = ejabberd_s2s:dirty_get_connections(),
|
|
|
|
TConns = lists:usort([element(2, C) || C <- S2SConns]),
|
2010-11-29 20:44:31 +01:00
|
|
|
Attrs = [?XMLATTR(<<"registered-users">>, mnesia:table_info(passwd, size)),
|
|
|
|
?XMLATTR(<<"online-users">>, mnesia:table_info(presence, size)),
|
|
|
|
?XMLATTR(<<"running-nodes">>,
|
2009-01-21 14:34:26 +01:00
|
|
|
length(mnesia:system_info(running_db_nodes))),
|
2010-11-29 20:44:31 +01:00
|
|
|
?XMLATTR(<<"stopped-nodes">>,
|
2009-01-21 14:34:26 +01:00
|
|
|
length(lists:usort(mnesia:system_info(db_nodes) ++
|
|
|
|
mnesia:system_info(extra_db_nodes)) --
|
|
|
|
mnesia:system_info(running_db_nodes))),
|
2010-11-29 20:44:31 +01:00
|
|
|
?XMLATTR(<<"outgoing-s2s-servers">>,
|
2009-01-21 14:34:26 +01:00
|
|
|
length(TConns))],
|
2008-08-06 15:44:58 +02:00
|
|
|
{result, #xmlel{ns = ?NS_ECONFIGURE, name = 'info', attrs = Attrs}};
|
|
|
|
process_get(#xmlel{ns = ?NS_ECONFIGURE, name = 'welcome-message', attrs = Attrs}) ->
|
2003-11-02 18:16:28 +01:00
|
|
|
{Subj, Body} = case ejabberd_config:get_local_option(welcome_message) of
|
|
|
|
{_Subj, _Body} = SB -> SB;
|
|
|
|
_ -> {"", ""}
|
|
|
|
end,
|
2008-08-06 15:44:58 +02:00
|
|
|
{result, #xmlel{ns = ?NS_ECONFIGURE, name = 'welcome-message',
|
|
|
|
attrs = Attrs, children =
|
|
|
|
[#xmlel{ns = ?NS_ECONFIGURE, name = 'subject', children = [#xmlcdata{cdata = list_to_binary(Subj)}]},
|
|
|
|
#xmlel{ns = ?NS_ECONFIGURE, name = 'body', children = [#xmlcdata{cdata = list_to_binary(Body)}]}]}};
|
|
|
|
process_get(#xmlel{ns = ?NS_ECONFIGURE, name = 'registration-watchers', attrs = Attrs}) ->
|
2003-11-02 18:16:28 +01:00
|
|
|
SubEls =
|
|
|
|
case ejabberd_config:get_local_option(registration_watchers) of
|
|
|
|
JIDs when is_list(JIDs) ->
|
|
|
|
lists:map(fun(JID) ->
|
2008-08-06 15:44:58 +02:00
|
|
|
#xmlel{ns = ?NS_ECONFIGURE, name = 'jid', children = [#xmlcdata{cdata = list_to_binary(JID)}]}
|
2003-11-02 18:16:28 +01:00
|
|
|
end, JIDs);
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end,
|
2008-08-06 15:44:58 +02:00
|
|
|
{result, #xmlel{ns = ?NS_ECONFIGURE, name = 'registration_watchers', attrs = Attrs, children = SubEls}};
|
|
|
|
process_get(#xmlel{ns = ?NS_ECONFIGURE, name = 'acls', attrs = Attrs}) ->
|
2003-11-02 18:16:28 +01:00
|
|
|
Str = lists:flatten(io_lib:format("~p.", [ets:tab2list(acl)])),
|
2008-08-06 15:44:58 +02:00
|
|
|
{result, #xmlel{ns = ?NS_ECONFIGURE, name = 'acls', attrs = Attrs, children = [#xmlcdata{cdata = list_to_binary(Str)}]}};
|
|
|
|
process_get(#xmlel{ns = ?NS_ECONFIGURE, name = 'access', attrs = Attrs}) ->
|
2003-11-02 18:16:28 +01:00
|
|
|
Str =
|
|
|
|
lists:flatten(
|
|
|
|
io_lib:format(
|
|
|
|
"~p.",
|
|
|
|
[ets:select(config,
|
|
|
|
[{{config, {access, '$1'}, '$2'},
|
|
|
|
[],
|
|
|
|
[{{access, '$1', '$2'}}]}])
|
|
|
|
])),
|
2008-08-06 15:44:58 +02:00
|
|
|
{result, #xmlel{ns = ?NS_ECONFIGURE, name = 'access', attrs = Attrs, children = [#xmlcdata{cdata = list_to_binary(Str)}]}};
|
|
|
|
process_get(#xmlel{ns = ?NS_ECONFIGURE, name = 'last', attrs = Attrs}) ->
|
2003-11-02 18:16:28 +01:00
|
|
|
case catch mnesia:dirty_select(
|
2004-07-25 23:27:56 +02:00
|
|
|
last_activity, [{{last_activity, '_', '$1', '_'}, [], ['$1']}]) of
|
2003-11-02 18:16:28 +01:00
|
|
|
{'EXIT', _Reason} ->
|
2008-08-06 15:44:58 +02:00
|
|
|
{error, 'internal-server-error'};
|
2003-11-02 18:16:28 +01:00
|
|
|
Vals ->
|
|
|
|
{MegaSecs, Secs, _MicroSecs} = now(),
|
|
|
|
TimeStamp = MegaSecs * 1000000 + Secs,
|
|
|
|
Str = lists:flatten(
|
|
|
|
lists:append(
|
|
|
|
[[integer_to_list(TimeStamp - V), " "] || V <- Vals])),
|
2008-08-06 15:44:58 +02:00
|
|
|
{result, #xmlel{ns = ?NS_ECONFIGURE, name = 'last', attrs = Attrs, children = [#xmlcdata{cdata = list_to_binary(Str)}]}}
|
2003-11-02 18:16:28 +01:00
|
|
|
end;
|
2007-12-07 01:09:48 +01:00
|
|
|
%%process_get({xmlelement, Name, Attrs, SubEls}) ->
|
|
|
|
%% {result, };
|
2003-11-02 18:16:28 +01:00
|
|
|
process_get(_) ->
|
2008-08-06 15:44:58 +02:00
|
|
|
{error, 'bad-request'}.
|
2003-11-02 18:16:28 +01:00
|
|
|
|