1 |
|
%%%------------------------------------------------------------------- |
2 |
|
%%% File : service_admin_extra_private.erl |
3 |
|
%%% Author : Badlop <badlop@process-one.net>, Piotr Nosek <piotr.nosek@erlang-solutions.com> |
4 |
|
%%% Purpose : Contributed administrative functions and commands |
5 |
|
%%% Created : 10 Aug 2008 by Badlop <badlop@process-one.net> |
6 |
|
%%% |
7 |
|
%%% |
8 |
|
%%% ejabberd, Copyright (C) 2002-2008 ProcessOne |
9 |
|
%%% |
10 |
|
%%% This program is free software; you can redistribute it and/or |
11 |
|
%%% modify it under the terms of the GNU General Public License as |
12 |
|
%%% published by the Free Software Foundation; either version 2 of the |
13 |
|
%%% License, or (at your option) any later version. |
14 |
|
%%% |
15 |
|
%%% This program is distributed in the hope that it will be useful, |
16 |
|
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of |
17 |
|
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
18 |
|
%%% General Public License for more details. |
19 |
|
%%% |
20 |
|
%%% You should have received a copy of the GNU General Public License |
21 |
|
%%% along with this program; if not, write to the Free Software |
22 |
|
%%% Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
23 |
|
%%% |
24 |
|
%%%------------------------------------------------------------------- |
25 |
|
|
26 |
|
-module(service_admin_extra_private). |
27 |
|
-author('badlop@process-one.net'). |
28 |
|
|
29 |
|
-export([ |
30 |
|
commands/0, |
31 |
|
private_get/4, |
32 |
|
private_set/3 |
33 |
|
]). |
34 |
|
|
35 |
|
-ignore_xref([ |
36 |
|
commands/0, private_get/4, private_set/3 |
37 |
|
]). |
38 |
|
|
39 |
|
-include("mongoose.hrl"). |
40 |
|
-include("ejabberd_commands.hrl"). |
41 |
|
-include("jlib.hrl"). |
42 |
|
-include_lib("exml/include/exml.hrl"). |
43 |
|
|
44 |
|
%%% |
45 |
|
%%% Register commands |
46 |
|
%%% |
47 |
|
|
48 |
|
-spec commands() -> [ejabberd_commands:cmd(), ...]. |
49 |
|
commands() -> |
50 |
152 |
[ |
51 |
|
#ejabberd_commands{name = private_get, tags = [private], |
52 |
|
desc = "Get some information from a user private storage", |
53 |
|
module = ?MODULE, function = private_get, |
54 |
|
args = [{user, binary}, {host, binary}, {element, binary}, {ns, binary}], |
55 |
|
result = {content, string}}, |
56 |
|
#ejabberd_commands{name = private_set, tags = [private], |
57 |
|
desc = "Set to the user private storage", |
58 |
|
module = ?MODULE, function = private_set, |
59 |
|
args = [{user, binary}, {host, binary}, {element, binary}], |
60 |
|
result = {res, restuple}} |
61 |
|
]. |
62 |
|
|
63 |
|
%%% |
64 |
|
%%% Private Storage |
65 |
|
%%% |
66 |
|
|
67 |
|
%% Example usage: |
68 |
|
%% $ mongooseimctl private_set badlop localhost "\<aa\ xmlns=\'bb\'\>Cluth\</aa\>" |
69 |
|
%% $ mongooseimctl private_get badlop localhost aa bb |
70 |
|
%% <aa xmlns='bb'>Cluth</aa> |
71 |
|
|
72 |
|
-spec private_get(jid:user(), jid:server(), binary(), binary()) -> |
73 |
|
{error, string()} | string(). |
74 |
|
private_get(Username, Host, Element, Ns) -> |
75 |
1 |
JID = jid:make(Username, Host, <<>>), |
76 |
1 |
case ejabberd_auth:does_user_exist(JID) of |
77 |
|
true -> |
78 |
1 |
do_private_get(JID, Element, Ns); |
79 |
|
false -> |
80 |
:-( |
{error, io_lib:format("User ~s@~s does not exist", [Username, Host])} |
81 |
|
end. |
82 |
|
|
83 |
|
do_private_get(JID, Element, Ns) -> |
84 |
1 |
From = To = JID, |
85 |
1 |
{ok, HostType} = mongoose_domain_api:get_domain_host_type(From#jid.lserver), |
86 |
1 |
IQ = {iq, <<"">>, get, ?NS_PRIVATE, <<"">>, |
87 |
|
#xmlel{ name = <<"query">>, |
88 |
|
attrs = [{<<"xmlns">>, ?NS_PRIVATE}], |
89 |
|
children = [#xmlel{ name = Element, attrs = [{<<"xmlns">>, Ns}]}] } }, |
90 |
1 |
Acc = mongoose_acc:new(#{ location => ?LOCATION, |
91 |
|
from_jid => From, |
92 |
|
to_jid => To, |
93 |
|
lserver => From#jid.lserver, |
94 |
|
host_type => HostType, |
95 |
|
element => jlib:iq_to_xml(IQ) }), |
96 |
1 |
{_, ResIq} = mod_private:process_iq(Acc, From, To, IQ, #{}), |
97 |
1 |
[#xmlel{ name = <<"query">>, |
98 |
|
attrs = [{<<"xmlns">>, ?NS_PRIVATE}], |
99 |
|
children = [SubEl] }] = ResIq#iq.sub_el, |
100 |
1 |
exml:to_binary(SubEl). |
101 |
|
|
102 |
|
-spec private_set(jid:user(), jid:server(), |
103 |
|
ElementString :: binary()) -> {Res, string()} when |
104 |
|
Res :: ok | user_does_not_exist | user_does_not_exist | not_loaded. |
105 |
|
private_set(Username, Host, ElementString) -> |
106 |
2 |
case exml:parse(ElementString) of |
107 |
|
{error, Error} -> |
108 |
:-( |
String = io_lib:format("Error found parsing the element:~n ~p~nError: ~p~n", |
109 |
|
[ElementString, Error]), |
110 |
:-( |
{parse_error, String}; |
111 |
|
{ok, Xml} -> |
112 |
2 |
private_set2(Username, Host, Xml) |
113 |
|
end. |
114 |
|
|
115 |
|
|
116 |
|
private_set2(Username, Host, Xml) -> |
117 |
2 |
JID = jid:make(Username, Host, <<>>), |
118 |
2 |
case ejabberd_auth:does_user_exist(JID) of |
119 |
|
true -> |
120 |
2 |
do_private_set2(JID, Xml); |
121 |
|
false -> |
122 |
:-( |
{user_does_not_exist, io_lib:format("User ~s does not exist", [jid:to_binary(JID)])} |
123 |
|
end. |
124 |
|
|
125 |
|
do_private_set2(#jid{lserver = Domain} = JID, Xml) -> |
126 |
2 |
{ok, HostType} = mongoose_domain_api:get_domain_host_type(Domain), |
127 |
2 |
case is_private_module_loaded(HostType) of |
128 |
|
true -> |
129 |
2 |
From = To = JID, |
130 |
2 |
IQ = {iq, <<"">>, set, ?NS_PRIVATE, <<"">>, |
131 |
|
#xmlel{ name = <<"query">>, |
132 |
|
attrs = [{<<"xmlns">>, ?NS_PRIVATE}], |
133 |
|
children = [Xml]}}, |
134 |
2 |
Acc = mongoose_acc:new(#{ location => ?LOCATION, |
135 |
|
from_jid => From, |
136 |
|
to_jid => To, |
137 |
|
lserver => Domain, |
138 |
|
host_type => HostType, |
139 |
|
element => jlib:iq_to_xml(IQ) }), |
140 |
2 |
mod_private:process_iq(Acc, From, To, IQ, #{}), |
141 |
2 |
{ok, ""}; |
142 |
|
false -> |
143 |
:-( |
{not_loaded, io_lib:format("Module mod_private is not loaded on domain ~s", [Domain])} |
144 |
|
end. |
145 |
|
|
146 |
|
-spec is_private_module_loaded(jid:server()) -> true | false. |
147 |
|
is_private_module_loaded(Server) -> |
148 |
2 |
lists:member(mod_private, gen_mod:loaded_modules(Server)). |