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).
|
|
|
|
|
2003-01-22 21:40:40 +01:00
|
|
|
-export([start/1,
|
2003-01-29 18:12:23 +01:00
|
|
|
stop/0,
|
2003-10-09 20:09:05 +02:00
|
|
|
process_local_iq/3,
|
|
|
|
remove_user/1]).
|
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
|
|
|
|
|
|
|
-record(private_storage, {userns, xml}).
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
start(Opts) ->
|
|
|
|
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)}]),
|
2003-07-20 22:35:35 +02:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, ?NS_PRIVATE,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_local_iq, IQDisc).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2003-01-29 18:12:23 +01:00
|
|
|
stop() ->
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_local, ?NS_PRIVATE).
|
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
process_local_iq(From, _To, #iq{type = Type, sub_el = SubEl} = IQ) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer} = From,
|
2003-01-16 21:24:53 +01:00
|
|
|
case ?MYNAME of
|
2003-12-17 21:13:21 +01:00
|
|
|
LServer ->
|
2003-01-16 21:24:53 +01:00
|
|
|
{xmlelement, Name, Attrs, Els} = SubEl,
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun(El) ->
|
|
|
|
set_data(LUser, El)
|
|
|
|
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 ->
|
|
|
|
case catch get_data(LUser, 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;
|
|
|
|
_ ->
|
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.
|
|
|
|
|
|
|
|
set_data(LUser, El) ->
|
|
|
|
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;
|
|
|
|
_ ->
|
|
|
|
mnesia:write(#private_storage{userns = {LUser, XMLNS},
|
|
|
|
xml = El})
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
ignore
|
|
|
|
end.
|
|
|
|
|
|
|
|
get_data(LUser, Els) ->
|
|
|
|
get_data(LUser, Els, []).
|
|
|
|
|
2003-12-17 21:13:21 +01:00
|
|
|
get_data(_LUser, [], Res) ->
|
2003-01-16 21:24:53 +01:00
|
|
|
lists:reverse(Res);
|
|
|
|
get_data(LUser, [El | Els], Res) ->
|
|
|
|
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),
|
|
|
|
case mnesia:dirty_read(private_storage, {LUser, XMLNS}) of
|
|
|
|
[R] ->
|
|
|
|
get_data(LUser, Els, [R#private_storage.xml | Res]);
|
|
|
|
[] ->
|
|
|
|
get_data(LUser, Els, [El | Res])
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
get_data(LUser, Els, Res)
|
|
|
|
end.
|
2003-10-09 20:09:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
remove_user(User) ->
|
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
F = fun() ->
|
|
|
|
lists:foreach(
|
|
|
|
fun({U, _} = Key) ->
|
|
|
|
if
|
|
|
|
U == LUser ->
|
|
|
|
mnesia:delete({private_storage, Key});
|
|
|
|
true ->
|
|
|
|
ok
|
|
|
|
end
|
|
|
|
end, mnesia:all_keys(private_storage))
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|