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>
|
|
|
|
%%%
|
|
|
|
%%%
|
2013-01-24 15:25:13 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2013 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).
|
2013-03-14 10:33:02 +01: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).
|
|
|
|
|
2013-07-21 12:24:36 +02:00
|
|
|
-export([start/2, stop/1, process_sm_iq/3, import/3,
|
|
|
|
remove_user/2, get_data/2, export/1, import/1]).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2013-03-14 10:33:02 +01: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),
|
2013-03-14 10:33:02 +01: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) ->
|
2013-03-14 10:33:02 +01: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
|
2013-03-14 10:33:02 +01: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,
|
2013-03-14 10:33:02 +01: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) ->
|
2013-03-14 10:33:02 +01: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},
|
2013-03-14 10:33:02 +01: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
|
2013-03-14 10:33:02 +01: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);
|
|
|
|
mnesia -> mnesia:transaction(F)
|
|
|
|
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},
|
2013-03-14 10:33:02 +01: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
|
2013-03-14 10:33:02 +01: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) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
IQ#iq{type = error,
|
|
|
|
sub_el = [IQ#iq.sub_el, ?ERR_FORBIDDEN]}.
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
filter_xmlels(Xmlels) -> filter_xmlels(Xmlels, []).
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2013-03-14 10:33:02 +01: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) ->
|
2013-03-14 10:33:02 +01: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),
|
2013-03-14 10:33:02 +01:00
|
|
|
SData = ejabberd_odbc:escape(xml:element_to_binary(El)),
|
|
|
|
odbc_queries:set_private_data(LServer, Username, LXMLNS,
|
|
|
|
SData).
|
2011-12-30 15:08:24 +01:00
|
|
|
|
|
|
|
get_data(LUser, LServer, Data) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
get_data(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE), Data, []).
|
2011-12-30 15:08:24 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_data(_LUser, _LServer, _DBType, [],
|
|
|
|
Storage_Xmlels) ->
|
2011-12-30 15:08:24 +01:00
|
|
|
lists:reverse(Storage_Xmlels);
|
2013-03-14 10:33:02 +01: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;
|
2013-03-14 10:33:02 +01: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),
|
2013-03-14 10:33:02 +01: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;
|
|
|
|
%% MREMOND: I wonder when the query could return a vcard ?
|
|
|
|
{selected, [<<"vcard">>], []} ->
|
|
|
|
get_data(LUser, LServer, odbc, Els, [El | Res]);
|
|
|
|
_ -> get_data(LUser, LServer, odbc, Els, [El | Res])
|
|
|
|
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);
|
|
|
|
_ ->
|
|
|
|
[]
|
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),
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_user(LUser, LServer,
|
|
|
|
gen_mod:db_type(Server, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
remove_user(LUser, LServer, mnesia) ->
|
2013-03-14 10:33:02 +01: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),
|
2013-03-14 10:33:02 +01:00
|
|
|
odbc_queries:del_user_private_storage(LServer,
|
|
|
|
Username).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
update_table() ->
|
|
|
|
Fields = record_info(fields, private_storage),
|
|
|
|
case mnesia:table_info(private_storage, attributes) of
|
2013-03-14 10:33:02 +01: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.
|
|
|
|
|
2013-03-14 10:33:02 +01: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}].
|
2013-07-21 12:24:36 +02:00
|
|
|
|
|
|
|
import(LServer) ->
|
|
|
|
[{<<"select username, namespace, data from private_storage;">>,
|
|
|
|
fun([LUser, XMLNS, XML]) ->
|
|
|
|
El = #xmlel{} = xml_stream:parse_element(XML),
|
|
|
|
#private_storage{usns = {LUser, LServer, XMLNS},
|
|
|
|
xml = El}
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(_LServer, mnesia, #private_storage{} = PS) ->
|
|
|
|
mnesia:dirty_write(PS);
|
|
|
|
import(_, _, _) ->
|
|
|
|
pass.
|