2003-01-16 21:24:53 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_private.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 16 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_private).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
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
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-16 21:24:53 +01:00
|
|
|
|
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) ->
|
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(),
|
2005-06-20 05:18:13 +02:00
|
|
|
ejabberd_hooks:add(remove_user, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?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) ->
|
|
|
|
ejabberd_hooks:delete(remove_user, Host,
|
2004-12-19 21:47:35 +01:00
|
|
|
?MODULE, remove_user, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_PRIVATE).
|
2003-01-29 18:12:23 +01:00
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2004-07-11 22:51:54 +02:00
|
|
|
process_sm_iq(From, _To, #iq{type = Type, sub_el = SubEl} = IQ) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
2005-04-17 20:08:34 +02:00
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true ->
|
2003-01-16 21:24:53 +01:00
|
|
|
{xmlelement, Name, Attrs, Els} = SubEl,
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
set_data(LUser, LServer, El)
|
2003-01-16 21:24:53 +01:00
|
|
|
end, Els)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F),
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, Name, Attrs, []}]};
|
2003-01-16 21:24:53 +01:00
|
|
|
get ->
|
2005-04-17 20:08:34 +02:00
|
|
|
case catch get_data(LUser, LServer, Els) of
|
2003-12-17 21:13:21 +01:00
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [SubEl,
|
|
|
|
?ERR_INTERNAL_SERVER_ERROR]};
|
2003-01-16 21:24:53 +01:00
|
|
|
Res ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [{xmlelement, Name, Attrs, Res}]}
|
2003-01-16 21:24:53 +01:00
|
|
|
end
|
|
|
|
end;
|
2005-04-17 20:08:34 +02:00
|
|
|
false ->
|
2003-12-17 21:13:21 +01:00
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]}
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
set_data(LUser, LServer, El) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
case El of
|
2003-12-17 21:13:21 +01:00
|
|
|
{xmlelement, _Name, Attrs, _Els} ->
|
2003-01-16 21:24:53 +01:00
|
|
|
XMLNS = xml:get_attr_s("xmlns", Attrs),
|
|
|
|
case XMLNS of
|
|
|
|
"" ->
|
|
|
|
ignore;
|
|
|
|
_ ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:write(
|
|
|
|
#private_storage{usns = {LUser, LServer, XMLNS},
|
|
|
|
xml = El})
|
2003-01-16 21:24:53 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ignore
|
|
|
|
end.
|
|
|
|
|
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) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
case El of
|
2003-12-17 21:13:21 +01:00
|
|
|
{xmlelement, _Name, Attrs, _} ->
|
2003-01-16 21:24:53 +01:00
|
|
|
XMLNS = xml:get_attr_s("xmlns", Attrs),
|
2005-04-17 20:08:34 +02:00
|
|
|
case mnesia:dirty_read(private_storage, {LUser, LServer, XMLNS}) of
|
2003-01-16 21:24:53 +01:00
|
|
|
[R] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(LUser, LServer, Els,
|
|
|
|
[R#private_storage.xml | Res]);
|
2003-01-16 21:24:53 +01:00
|
|
|
[] ->
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(LUser, LServer, Els,
|
|
|
|
[El | Res])
|
2003-01-16 21:24:53 +01:00
|
|
|
end;
|
|
|
|
_ ->
|
2005-04-17 20:08:34 +02:00
|
|
|
get_data(LUser, LServer, Els, Res)
|
2003-01-16 21:24:53 +01:00
|
|
|
end.
|
2003-10-09 20:09:05 +02:00
|
|
|
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
% TODO: use mnesia:select
|
|
|
|
remove_user(User, Server) ->
|
2003-10-09 20:09:05 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2005-04-17 20:08:34 +02:00
|
|
|
LServer = jlib:nameprep(Server),
|
2003-10-09 20:09:05 +02:00
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
2005-04-17 20:08:34 +02:00
|
|
|
fun({U, S, _} = Key) ->
|
2003-10-09 20:09:05 +02:00
|
|
|
if
|
2005-04-17 20:08:34 +02:00
|
|
|
(U == LUser) and (S == LServer) ->
|
2003-10-09 20:09:05 +02:00
|
|
|
mnesia:delete({private_storage, Key});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, mnesia:all_keys(private_storage))
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
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 ->
|
|
|
|
ok;
|
|
|
|
[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(
|
|
|
|
fun(#private_storage{usns = {U, NS}} = R, _) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
mod_private_tmp_table,
|
|
|
|
R#private_storage{usns = {U, Host, NS}})
|
|
|
|
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.
|
|
|
|
|
|
|
|
|