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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2012-02-23 16:52:34 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2012 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-12 15:44:42 +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).
|
2012-09-11 15:45:59 +02:00
|
|
|
|
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).
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-export([start/2, stop/1, process_sm_iq/3,
|
|
|
|
remove_user/2, get_data/2, export/1]).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2012-09-11 15:45:59 +02:00
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
-record(private_storage,
|
|
|
|
{usns = {<<"">>, <<"">>, <<"">>} :: {binary(), binary(), binary() |
|
|
|
|
'$1' | '_'},
|
|
|
|
xml = #xmlel{} :: xmlel() | '_' | '$1'}).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2011-12-30 15:08:24 +01:00
|
|
|
-define(Xmlel_Query(Attrs, Children),
|
2012-09-11 15:45:59 +02:00
|
|
|
#xmlel{name = <<"query">>, attrs = Attrs,
|
|
|
|
children = Children}).
|
2011-12-30 15:08:24 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
2012-04-27 11:52:05 +02:00
|
|
|
case gen_mod:db_type(Opts) of
|
2012-09-11 15:45:59 +02:00
|
|
|
mnesia ->
|
|
|
|
mnesia:create_table(private_storage,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{attributes,
|
|
|
|
record_info(fields, private_storage)}]),
|
|
|
|
update_table();
|
|
|
|
_ -> ok
|
2012-04-27 11:52:05 +02:00
|
|
|
end,
|
2012-09-11 15:45:59 +02:00
|
|
|
ejabberd_hooks:add(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_PRIVATE, ?MODULE, process_sm_iq, IQDisc).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
stop(Host) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
ejabberd_hooks:delete(remove_user, Host, ?MODULE,
|
|
|
|
remove_user, 50),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_PRIVATE).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2011-12-30 15:08:24 +01:00
|
|
|
process_sm_iq(#jid{luser = LUser, lserver = LServer},
|
2012-09-11 15:45:59 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer}, IQ)
|
|
|
|
when IQ#iq.type == set ->
|
2011-12-30 15:08:24 +01:00
|
|
|
case IQ#iq.sub_el of
|
2012-09-11 15:45:59 +02:00
|
|
|
#xmlel{name = <<"query">>, children = Xmlels} ->
|
|
|
|
case filter_xmlels(Xmlels) of
|
|
|
|
[] ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [IQ#iq.sub_el, ?ERR_NOT_ACCEPTABLE]};
|
|
|
|
Data ->
|
|
|
|
DBType = gen_mod:db_type(LServer, ?MODULE),
|
|
|
|
F = fun () ->
|
|
|
|
lists:foreach(fun (Datum) ->
|
|
|
|
set_data(LUser, LServer,
|
|
|
|
Datum, DBType)
|
|
|
|
end,
|
|
|
|
Data)
|
|
|
|
end,
|
|
|
|
case DBType of
|
|
|
|
odbc -> ejabberd_odbc:sql_transaction(LServer, F);
|
2012-11-06 16:58:08 +01:00
|
|
|
mnesia -> mnesia:transaction(F);
|
|
|
|
riak -> F()
|
2012-09-11 15:45:59 +02:00
|
|
|
end,
|
|
|
|
IQ#iq{type = result, sub_el = []}
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [IQ#iq.sub_el, ?ERR_NOT_ACCEPTABLE]}
|
2011-12-30 15:08:24 +01:00
|
|
|
end;
|
|
|
|
%%
|
|
|
|
process_sm_iq(#jid{luser = LUser, lserver = LServer},
|
2012-09-11 15:45:59 +02:00
|
|
|
#jid{luser = LUser, lserver = LServer}, IQ)
|
|
|
|
when IQ#iq.type == get ->
|
2011-12-30 15:08:24 +01:00
|
|
|
case IQ#iq.sub_el of
|
2012-09-11 15:45:59 +02:00
|
|
|
#xmlel{name = <<"query">>, attrs = Attrs,
|
|
|
|
children = Xmlels} ->
|
|
|
|
case filter_xmlels(Xmlels) of
|
|
|
|
[] ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [IQ#iq.sub_el, ?ERR_BAD_FORMAT]};
|
|
|
|
Data ->
|
|
|
|
case catch get_data(LUser, LServer, Data) of
|
|
|
|
{'EXIT', _Reason} ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el =
|
|
|
|
[IQ#iq.sub_el, ?ERR_INTERNAL_SERVER_ERROR]};
|
|
|
|
Storage_Xmlels ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [?Xmlel_Query(Attrs, Storage_Xmlels)]}
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [IQ#iq.sub_el, ?ERR_BAD_FORMAT]}
|
2011-12-30 15:08:24 +01:00
|
|
|
end;
|
|
|
|
%%
|
|
|
|
process_sm_iq(_From, _To, IQ) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [IQ#iq.sub_el, ?ERR_FORBIDDEN]}.
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
filter_xmlels(Xmlels) -> filter_xmlels(Xmlels, []).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
filter_xmlels([], Data) -> lists:reverse(Data);
|
|
|
|
filter_xmlels([#xmlel{attrs = Attrs} = Xmlel | Xmlels],
|
|
|
|
Data) ->
|
|
|
|
case xml:get_attr_s(<<"xmlns">>, Attrs) of
|
|
|
|
<<"">> -> [];
|
|
|
|
XmlNS -> filter_xmlels(Xmlels, [{XmlNS, Xmlel} | Data])
|
2011-12-30 15:08:24 +01:00
|
|
|
end;
|
|
|
|
filter_xmlels([_ | Xmlels], Data) ->
|
|
|
|
filter_xmlels(Xmlels, Data).
|
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
set_data(LUser, LServer, {XmlNS, Xmlel}, mnesia) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
mnesia:write(#private_storage{usns =
|
|
|
|
{LUser, LServer, XmlNS},
|
|
|
|
xml = Xmlel});
|
2012-04-27 11:52:05 +02:00
|
|
|
set_data(LUser, LServer, {XMLNS, El}, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LXMLNS = ejabberd_odbc:escape(XMLNS),
|
2012-09-11 15:45:59 +02:00
|
|
|
SData = ejabberd_odbc:escape(xml:element_to_binary(El)),
|
|
|
|
odbc_queries:set_private_data(LServer, Username, LXMLNS,
|
2012-11-06 16:58:08 +01:00
|
|
|
SData);
|
|
|
|
set_data(LUser, LServer, {XMLNS, El}, riak) ->
|
2012-11-13 13:56:27 +01:00
|
|
|
ejabberd_riak:put(#private_storage{usns = {LUser, LServer, XMLNS},
|
|
|
|
xml = El},
|
|
|
|
[{'2i', [{<<"us">>, {LUser, LServer}}]}]).
|
2011-12-30 15:08:24 +01:00
|
|
|
|
|
|
|
get_data(LUser, LServer, Data) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
get_data(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE), Data, []).
|
2011-12-30 15:08:24 +01:00
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
get_data(_LUser, _LServer, _DBType, [],
|
|
|
|
Storage_Xmlels) ->
|
2011-12-30 15:08:24 +01:00
|
|
|
lists:reverse(Storage_Xmlels);
|
2012-09-11 15:45:59 +02:00
|
|
|
get_data(LUser, LServer, mnesia,
|
|
|
|
[{XmlNS, Xmlel} | Data], Storage_Xmlels) ->
|
|
|
|
case mnesia:dirty_read(private_storage,
|
|
|
|
{LUser, LServer, XmlNS})
|
|
|
|
of
|
|
|
|
[#private_storage{xml = Storage_Xmlel}] ->
|
|
|
|
get_data(LUser, LServer, mnesia, Data,
|
|
|
|
[Storage_Xmlel | Storage_Xmlels]);
|
|
|
|
_ ->
|
|
|
|
get_data(LUser, LServer, mnesia, Data,
|
|
|
|
[Xmlel | Storage_Xmlels])
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
2012-09-11 15:45:59 +02:00
|
|
|
get_data(LUser, LServer, odbc, [{XMLNS, El} | Els],
|
|
|
|
Res) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LXMLNS = ejabberd_odbc:escape(XMLNS),
|
2012-09-11 15:45:59 +02:00
|
|
|
case catch odbc_queries:get_private_data(LServer,
|
|
|
|
Username, LXMLNS)
|
|
|
|
of
|
|
|
|
{selected, [<<"data">>], [[SData]]} ->
|
|
|
|
case xml_stream:parse_element(SData) of
|
|
|
|
Data when is_record(Data, xmlel) ->
|
|
|
|
get_data(LUser, LServer, odbc, Els, [Data | Res])
|
|
|
|
end;
|
|
|
|
_ -> get_data(LUser, LServer, odbc, Els, [El | Res])
|
2012-11-06 16:58:08 +01:00
|
|
|
end;
|
|
|
|
get_data(LUser, LServer, riak, [{XMLNS, El} | Els],
|
|
|
|
Res) ->
|
2012-11-13 13:56:27 +01:00
|
|
|
case ejabberd_riak:get(private_storage, {LUser, LServer, XMLNS}) of
|
|
|
|
{ok, #private_storage{xml = NewEl}} ->
|
|
|
|
get_data(LUser, LServer, riak, Els, [NewEl|Res]);
|
|
|
|
_ ->
|
|
|
|
get_data(LUser, LServer, riak, Els, [El|Res])
|
2012-09-11 15:45:59 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_data(LUser, LServer) ->
|
|
|
|
get_all_data(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
|
|
|
|
|
|
|
get_all_data(LUser, LServer, mnesia) ->
|
|
|
|
lists:flatten(
|
|
|
|
mnesia:dirty_select(private_storage,
|
|
|
|
[{#private_storage{usns = {LUser, LServer, '_'},
|
|
|
|
xml = '$1'},
|
|
|
|
[], ['$1']}]));
|
|
|
|
get_all_data(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
case catch odbc_queries:get_private_data(LServer, Username) of
|
|
|
|
{selected, [<<"namespace">>, <<"data">>], Res} ->
|
|
|
|
lists:flatmap(
|
|
|
|
fun([_, SData]) ->
|
|
|
|
case xml_stream:parse_element(SData) of
|
|
|
|
#xmlel{} = El ->
|
|
|
|
[El];
|
|
|
|
_ ->
|
|
|
|
[]
|
|
|
|
end
|
|
|
|
end, Res);
|
|
|
|
_ ->
|
|
|
|
[]
|
2012-11-06 16:58:08 +01:00
|
|
|
end;
|
|
|
|
get_all_data(LUser, LServer, riak) ->
|
|
|
|
case ejabberd_riak:get_by_index(
|
2012-11-13 13:56:27 +01:00
|
|
|
private_storage, <<"us">>, {LUser, LServer}) of
|
2012-11-06 16:58:08 +01:00
|
|
|
{ok, Res} ->
|
2012-11-13 13:56:27 +01:00
|
|
|
[El || #private_storage{xml = El} <- Res];
|
2012-11-06 16:58:08 +01:00
|
|
|
_ ->
|
|
|
|
[]
|
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
|
|
|
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),
|
2012-09-11 15:45:59 +02:00
|
|
|
remove_user(LUser, LServer,
|
|
|
|
gen_mod:db_type(Server, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
remove_user(LUser, LServer, mnesia) ->
|
2012-09-11 15:45:59 +02:00
|
|
|
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,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
|
|
|
remove_user(LUser, LServer, odbc) ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
2012-09-11 15:45:59 +02:00
|
|
|
odbc_queries:del_user_private_storage(LServer,
|
2012-11-06 16:58:08 +01:00
|
|
|
Username);
|
|
|
|
remove_user(LUser, LServer, riak) ->
|
2012-11-13 13:56:27 +01:00
|
|
|
{atomic, ejabberd_riak:delete_by_index(private_storage,
|
|
|
|
<<"us">>, {LUser, LServer})}.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, private_storage),
|
|
|
|
case mnesia:table_info(private_storage, attributes) of
|
2012-09-11 15:45:59 +02:00
|
|
|
Fields ->
|
|
|
|
ejabberd_config:convert_table_to_binary(
|
|
|
|
private_storage, Fields, set,
|
|
|
|
fun(#private_storage{usns = {U, _, _}}) -> U end,
|
|
|
|
fun(#private_storage{usns = {U, S, NS}, xml = El} = R) ->
|
|
|
|
R#private_storage{usns = {iolist_to_binary(U),
|
|
|
|
iolist_to_binary(S),
|
|
|
|
iolist_to_binary(NS)},
|
|
|
|
xml = xml:to_xmlel(El)}
|
|
|
|
end);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating private_storage table", []),
|
|
|
|
mnesia:transform_table(private_storage, ignore, Fields)
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2012-09-11 15:45:59 +02:00
|
|
|
export(_Server) ->
|
|
|
|
[{private_storage,
|
|
|
|
fun(Host, #private_storage{usns = {LUser, LServer, XMLNS},
|
|
|
|
xml = Data})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
LXMLNS = ejabberd_odbc:escape(XMLNS),
|
|
|
|
SData =
|
|
|
|
ejabberd_odbc:escape(xml:element_to_binary(Data)),
|
|
|
|
odbc_queries:set_private_data_sql(Username, LXMLNS,
|
|
|
|
SData);
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|