aboutsummaryrefslogtreecommitdiff
path: root/src/mod_configure2.erl
blob: 0852546d424d16ba5a9d35b5f69b7a4774c58bf9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
%%%----------------------------------------------------------------------
%%% File    : mod_configure2.erl
%%% Author  : Alexey Shchepin <alexey@sevcom.net>
%%% Purpose : Support for online configuration of ejabberd
%%% Created : 26 Oct 2003 by Alexey Shchepin <alexey@sevcom.net>
%%% Id      : $Id$
%%%----------------------------------------------------------------------

-module(mod_configure2).
-author('alexey@sevcom.net').
-vsn('$Revision$ ').

-behaviour(gen_mod).

-export([start/2,
	 stop/1,
	 process_local_iq/3]).

-include("ejabberd.hrl").
-include("jlib.hrl").

-define(NS_ECONFIGURE, "http://ejabberd.jabberstudio.org/protocol/configure").

start(Host, Opts) ->
    IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
    gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_ECONFIGURE,
				  ?MODULE, process_local_iq, IQDisc),
    ok.

stop(Host) ->
    gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_ECONFIGURE).


process_local_iq(From, To, #iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
    case acl:match_rule(To#jid.lserver, configure, From) of
	deny ->
	    IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
	allow ->
	    case Type of
		set ->
		    IQ#iq{type = error,
			  sub_el = [SubEl, ?ERR_FEATURE_NOT_IMPLEMENTED]};
		    %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;
		get ->
		    case process_get(SubEl) of
			{result, Res} ->
			    IQ#iq{type = result, sub_el = [Res]};
			{error, Error} ->
			    IQ#iq{type = error, sub_el = [SubEl, Error]}
		    end
	    end
    end.


process_get({xmlelement, "info", _Attrs, _SubEls}) ->
    S2SConns = ejabberd_s2s:dirty_get_connections(),
    TConns = lists:usort([element(2, C) || C <- S2SConns]),
    Attrs = [{"registered-users",
	      integer_to_list(mnesia:table_info(passwd, size))},
	     {"online-users",
	      integer_to_list(mnesia:table_info(presence, size))},
	     {"running-nodes",
	      integer_to_list(length(mnesia:system_info(running_db_nodes)))},
	     {"stopped-nodes",
	      integer_to_list(
		length(lists:usort(mnesia:system_info(db_nodes) ++
				   mnesia:system_info(extra_db_nodes)) --
		       mnesia:system_info(running_db_nodes)))},
	     {"outgoing-s2s-servers", integer_to_list(length(TConns))}],
    {result, {xmlelement, "info",
	      [{"xmlns", ?NS_ECONFIGURE} | Attrs], []}};
process_get({xmlelement, "welcome-message", Attrs, _SubEls}) ->
    {Subj, Body} = case ejabberd_config:get_local_option(welcome_message) of
		       {_Subj, _Body} = SB -> SB;
		       _ -> {"", ""}
		   end,
    {result, {xmlelement, "welcome-message", Attrs,
	      [{xmlelement, "subject", [], [{xmlcdata, Subj}]},
	       {xmlelement, "body", [], [{xmlcdata, Body}]}]}};
process_get({xmlelement, "registration-watchers", Attrs, _SubEls}) ->
    SubEls =
	case ejabberd_config:get_local_option(registration_watchers) of
	    JIDs when is_list(JIDs) ->
		lists:map(fun(JID) ->
				  {xmlelement, "jid", [], [{xmlcdata, JID}]}
			  end, JIDs);
	    _ ->
		[]
	end,
    {result, {xmlelement, "registration_watchers", Attrs, SubEls}};
process_get({xmlelement, "acls", Attrs, _SubEls}) ->
    Str = lists:flatten(io_lib:format("~p.", [ets:tab2list(acl)])),
    {result, {xmlelement, "acls", Attrs, [{xmlcdata, Str}]}};
process_get({xmlelement, "access", Attrs, _SubEls}) ->
    Str =
	lists:flatten(
	  io_lib:format(
	    "~p.",
	    [ets:select(config,
			[{{config, {access, '$1'}, '$2'},
			  [],
			  [{{access, '$1', '$2'}}]}])
	    ])),
    {result, {xmlelement, "access", Attrs, [{xmlcdata, Str}]}};
process_get({xmlelement, "last", Attrs, _SubEls}) ->
    case catch mnesia:dirty_select(
		 last_activity, [{{last_activity, '_', '$1', '_'}, [], ['$1']}]) of
	{'EXIT', _Reason} ->
	    {error, ?ERR_INTERNAL_SERVER_ERROR};
	Vals ->
	    {MegaSecs, Secs, _MicroSecs} = now(),
	    TimeStamp = MegaSecs * 1000000 + Secs,
	    Str = lists:flatten(
		    lists:append(
		      [[integer_to_list(TimeStamp - V), " "] || V <- Vals])),
	    {result, {xmlelement, "last", Attrs, [{xmlcdata, Str}]}}
    end;
%process_get({xmlelement, Name, Attrs, SubEls}) ->
%    {result, };
process_get(_) ->
    {error, ?ERR_BAD_REQUEST}.