2003-01-16 21:24:53 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_private.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%% Purpose : Support for private storage.
|
|
|
|
%%% Created : 16 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2009-01-19 15:47:33 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2009 ProcessOne
|
2007-12-24 13:58:05 +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 13:58:05 +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-01-16 21:24:53 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_private).
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-export([start/2,
|
|
|
|
stop/1,
|
2004-07-11 22:51:54 +02:00
|
|
|
process_sm_iq/3,
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user/2]).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
-include_lib("exmpp/include/exmpp.hrl").
|
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
-include("ejabberd.hrl").
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
-record(private_storage, {usns, xml}).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2003-01-16 21:24:53 +01:00
|
|
|
mnesia:create_table(private_storage,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, private_storage)}]),
|
2005-04-17 20:08:34 +02:00
|
|
|
update_table(),
|
2009-01-03 16:15:38 +01:00
|
|
|
ejabberd_hooks:add(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, HostB, ?NS_PRIVATE,
|
2004-07-11 22:51:54 +02:00
|
|
|
?MODULE, process_sm_iq, IQDisc).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2009-01-03 16:15:38 +01:00
|
|
|
HostB = list_to_binary(Host),
|
|
|
|
ejabberd_hooks:delete(remove_user, HostB,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2009-01-10 17:10:12 +01:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, HostB, ?NS_PRIVATE).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
process_sm_iq(From, To, #iq{type = Type} = IQ_Rec) ->
|
|
|
|
case check_packet(From, To, IQ_Rec) of
|
|
|
|
ok ->
|
2003-01-16 21:24:53 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_set(From, To, IQ_Rec);
|
2003-01-16 21:24:53 +01:00
|
|
|
get ->
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_get(From, To, IQ_Rec)
|
2003-01-16 21:24:53 +01:00
|
|
|
end;
|
2008-10-07 14:20:09 +02:00
|
|
|
{error, Error} ->
|
|
|
|
exmpp_iq:error(IQ_Rec, Error)
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
process_iq_get(From, _To, #iq{payload = SubEl} = IQ_Rec) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
LUser = exmpp_jid:prep_node(From),
|
2009-06-01 18:37:15 +02:00
|
|
|
LServer = exmpp_jid:prep_domain(From),
|
2008-10-07 14:20:09 +02:00
|
|
|
case catch get_data(LUser,
|
|
|
|
LServer,
|
|
|
|
exmpp_xml:get_child_elements(SubEl)) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
{error, 'internal-server-error'};
|
|
|
|
Res ->
|
|
|
|
exmpp_iq:result(IQ_Rec, #xmlel{ns = ?NS_PRIVATE,
|
|
|
|
name = 'query',
|
|
|
|
children = Res})
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
process_iq_set(From, _To, #iq{payload = SubEl} = IQ_Rec) ->
|
2009-06-01 18:39:36 +02:00
|
|
|
LUser = exmpp_jid:prep_node(From),
|
2009-06-01 18:37:15 +02:00
|
|
|
LServer = exmpp_jid:prep_domain(From),
|
2008-10-07 14:20:09 +02:00
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
|
|
|
set_data(LUser, LServer, El)
|
|
|
|
end, exmpp_xml:get_child_elements(SubEl))
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
|
|
|
exmpp_iq:result(IQ_Rec).
|
|
|
|
|
|
|
|
|
|
|
|
check_packet(From, To, IQ_Rec) ->
|
|
|
|
check_packet(From, To, IQ_Rec, [ fun check_domain/3,
|
|
|
|
fun check_user/3,
|
|
|
|
fun check_ns/3]).
|
|
|
|
check_packet(_From, _To, _IQ_Rec, []) ->
|
|
|
|
ok;
|
|
|
|
check_packet(From, To, IQ_Rec, [F | R]) ->
|
|
|
|
case F(From, To, IQ_Rec) of
|
|
|
|
{error, _} = Error -> Error;
|
|
|
|
ok -> check_packet(From, To, IQ_Rec, R)
|
|
|
|
end.
|
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
check_domain(From, _To, _IQ_Rec) ->
|
2009-06-01 18:38:28 +02:00
|
|
|
LServer = exmpp_jid:prep_domain_as_list(From),
|
2008-10-07 14:20:09 +02:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true -> ok;
|
|
|
|
false -> {error, 'not-allowed'}
|
|
|
|
end.
|
|
|
|
|
|
|
|
% the iq can't be directed to another jid
|
|
|
|
check_user(From, To, _IQ_Rec) ->
|
2009-06-01 18:50:36 +02:00
|
|
|
case exmpp_jid:bare_compare(From, To) of
|
2008-10-07 14:20:09 +02:00
|
|
|
true -> ok;
|
|
|
|
false -> {error, 'forbidden'}
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
%there must be at least one child, and every child should have
|
|
|
|
%a namespace specified (reject if the namespace is jabber:iq:private,
|
|
|
|
%the same than the parent element).
|
|
|
|
check_ns(_From, _To, #iq{payload = SubEl}) ->
|
|
|
|
case exmpp_xml:get_child_elements(SubEl) of
|
|
|
|
[] ->
|
|
|
|
{error, 'not-acceptable'};
|
|
|
|
Children ->
|
|
|
|
case lists:any(fun(Child) ->
|
|
|
|
exmpp_xml:get_ns_as_atom(Child) =:= ?NS_PRIVATE
|
|
|
|
end, Children) of
|
|
|
|
true -> {error, 'not-acceptable'};
|
|
|
|
false -> ok
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
set_data(LUser, LServer, El) ->
|
|
|
|
XMLNS = exmpp_xml:get_ns_as_atom(El),
|
|
|
|
mnesia:write(#private_storage{usns = {LUser, LServer, XMLNS},
|
|
|
|
xml = El}).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(LUser, LServer, Els) ->
|
|
|
|
get_data(LUser, LServer, Els, []).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(_LUser, _LServer, [], Res) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
lists:reverse(Res);
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(LUser, LServer, [El | Els], Res) ->
|
2008-10-07 14:20:09 +02:00
|
|
|
XMLNS = exmpp_xml:get_ns_as_atom(El),
|
|
|
|
case mnesia:dirty_read(private_storage, {LUser, LServer, XMLNS}) of
|
|
|
|
[R] ->
|
|
|
|
get_data(LUser, LServer, Els,
|
|
|
|
[R#private_storage.xml | Res]);
|
|
|
|
[] ->
|
|
|
|
get_data(LUser, LServer, Els,
|
|
|
|
[El | Res])
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
2003-10-09 20:09:05 +02:00
|
|
|
|
2009-01-03 16:15:38 +01:00
|
|
|
remove_user(User, Server)
|
|
|
|
when is_binary(User), is_binary(Server) ->
|
2008-10-07 14:20:09 +02:00
|
|
|
try
|
|
|
|
LUser = exmpp_stringprep:nodeprep(User),
|
|
|
|
LServer = exmpp_stringprep:nameprep(Server),
|
|
|
|
F = fun() ->
|
|
|
|
Namespaces = mnesia:select(
|
|
|
|
private_storage,
|
|
|
|
[{#private_storage{usns = {LUser, LServer, '$1'},
|
|
|
|
_ = '_'},
|
|
|
|
[],
|
|
|
|
['$$']}]),
|
|
|
|
lists:foreach(
|
|
|
|
fun([Namespace]) ->
|
|
|
|
mnesia:delete({private_storage,
|
|
|
|
{LUser, LServer, Namespace}})
|
|
|
|
end, Namespaces)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
catch
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end.
|
2003-10-09 20:09:05 +02:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, private_storage),
|
|
|
|
case mnesia:table_info(private_storage, attributes) of
|
|
|
|
Fields ->
|
2008-10-07 14:20:09 +02:00
|
|
|
convert_to_exmpp();
|
2005-04-17 20:08:34 +02:00
|
|
|
[userns, xml] ->
|
|
|
|
?INFO_MSG("Converting private_storage table from "
|
|
|
|
"{user, default, lists} format", []),
|
|
|
|
Host = ?MYNAME,
|
|
|
|
{atomic, ok} = mnesia:create_table(
|
|
|
|
mod_private_tmp_table,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{type, bag},
|
|
|
|
{local_content, true},
|
|
|
|
{record_name, private_storage},
|
|
|
|
{attributes, record_info(fields, private_storage)}]),
|
|
|
|
mnesia:transform_table(private_storage, ignore, Fields),
|
|
|
|
F1 = fun() ->
|
|
|
|
mnesia:write_lock_table(mod_private_tmp_table),
|
|
|
|
mnesia:foldl(
|
2008-10-07 14:20:09 +02:00
|
|
|
fun(#private_storage{usns = {U, NS}, xml = El} = R, _) ->
|
|
|
|
NS1 = list_to_atom(NS),
|
|
|
|
El0 = exmpp_xml:xmlelement_to_xmlel(El,
|
|
|
|
[?NS_PRIVATE], [{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
El1 = exmpp_xml:remove_whitespaces_deeply(El0),
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_private_tmp_table,
|
2008-10-07 14:20:09 +02:00
|
|
|
R#private_storage{usns = {U, Host, NS1}, xml = El1})
|
2005-04-17 20:08:34 +02:00
|
|
|
end, ok, private_storage)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F1),
|
|
|
|
mnesia:clear_table(private_storage),
|
|
|
|
F2 = fun() ->
|
|
|
|
mnesia:write_lock_table(private_storage),
|
|
|
|
mnesia:foldl(
|
|
|
|
fun(R, _) ->
|
|
|
|
mnesia:dirty_write(R)
|
|
|
|
end, ok, mod_private_tmp_table)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F2),
|
|
|
|
mnesia:delete_table(mod_private_tmp_table);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating private_storage table", []),
|
|
|
|
mnesia:transform_table(private_storage, ignore, Fields)
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2008-10-07 14:20:09 +02:00
|
|
|
convert_to_exmpp() ->
|
|
|
|
Fun = fun() ->
|
|
|
|
case mnesia:first(private_storage) of
|
|
|
|
'$end_of_table' ->
|
|
|
|
none;
|
2009-01-21 14:31:14 +01:00
|
|
|
{U, _S, _NS} when is_binary(U) ->
|
|
|
|
none;
|
|
|
|
{U, _S, _NS} when is_list(U) ->
|
|
|
|
mnesia:foldl(fun convert_to_exmpp2/2,
|
|
|
|
done, private_storage, write)
|
2008-10-07 14:20:09 +02:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
mnesia:transaction(Fun).
|
|
|
|
|
|
|
|
convert_to_exmpp2(#private_storage{usns = {U, S, NS} = Key, xml = El} = R,
|
|
|
|
Acc) ->
|
|
|
|
mnesia:delete({private_storage, Key}),
|
2009-01-21 14:31:14 +01:00
|
|
|
U1 = list_to_binary(U),
|
|
|
|
S1 = list_to_binary(S),
|
2008-10-07 14:20:09 +02:00
|
|
|
NS1 = list_to_atom(NS),
|
2009-01-21 14:31:14 +01:00
|
|
|
El1 = exmpp_xml:xmlelement_to_xmlel(El,
|
2008-10-07 14:20:09 +02:00
|
|
|
[?NS_PRIVATE], [{?NS_XMPP, ?NS_XMPP_pfx}]),
|
|
|
|
New_R = R#private_storage{
|
2009-01-21 14:31:14 +01:00
|
|
|
usns = {U1, S1, NS1},
|
2008-10-07 14:20:09 +02:00
|
|
|
xml = El1},
|
|
|
|
mnesia:write(New_R),
|
|
|
|
Acc.
|