2003-01-02 22:01:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_vcard.erl
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Author : Alexey Shchepin <alexey@process-one.net>
|
2012-04-27 11:52:05 +02:00
|
|
|
%%% Purpose : Vcard management
|
2007-12-24 13:58:05 +01:00
|
|
|
%%% Created : 2 Jan 2003 by Alexey Shchepin <alexey@process-one.net>
|
|
|
|
%%%
|
|
|
|
%%%
|
2016-01-13 12:29:14 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2016 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
|
|
|
%%%
|
2014-02-22 11:27:40 +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.,
|
|
|
|
%%% 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2007-12-24 13:58:05 +01:00
|
|
|
%%%
|
2003-01-02 22:01:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_vcard).
|
2013-03-14 10:33:02 +01:00
|
|
|
|
2016-02-26 13:45:28 +01:00
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
2007-12-24 13:58:05 +01:00
|
|
|
-author('alexey@process-one.net').
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2015-05-21 17:02:36 +02:00
|
|
|
-protocol({xep, 54, '1.2'}).
|
|
|
|
-protocol({xep, 55, '1.3'}).
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-export([start/2, init/3, stop/1, get_sm_features/5,
|
|
|
|
process_local_iq/3, process_sm_iq/3, reindex_vcards/0,
|
2015-06-01 14:38:27 +02:00
|
|
|
remove_user/2, export/1, import/1, import/3,
|
2016-01-28 12:23:51 +01:00
|
|
|
mod_opt_type/1, set_vcard/3]).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2016-02-26 13:45:28 +01:00
|
|
|
-include("ejabberd_sql_pt.hrl").
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
-define(JUD_MATCHES, 30).
|
2003-10-28 21:26:43 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
-record(vcard_search,
|
|
|
|
{us, user, luser, fn, lfn, family, lfamily, given,
|
|
|
|
lgiven, middle, lmiddle, nickname, lnickname, bday,
|
|
|
|
lbday, ctry, lctry, locality, llocality, email, lemail,
|
|
|
|
orgname, lorgname, orgunit, lorgunit}).
|
|
|
|
|
2012-11-13 13:56:27 +01:00
|
|
|
-record(vcard, {us = {<<"">>, <<"">>} :: {binary(), binary()} | binary(),
|
2013-03-14 10:33:02 +01:00
|
|
|
vcard = #xmlel{} :: xmlel()}).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-define(PROCNAME, ejabberd_mod_vcard).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2015-03-30 11:15:29 +02:00
|
|
|
case gen_mod:db_type(Host, Opts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
mnesia ->
|
|
|
|
mnesia:create_table(vcard,
|
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, vcard)}]),
|
|
|
|
mnesia:create_table(vcard_search,
|
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes,
|
|
|
|
record_info(fields, vcard_search)}]),
|
|
|
|
update_tables(),
|
|
|
|
mnesia:add_table_index(vcard_search, luser),
|
|
|
|
mnesia:add_table_index(vcard_search, lfn),
|
|
|
|
mnesia:add_table_index(vcard_search, lfamily),
|
|
|
|
mnesia:add_table_index(vcard_search, lgiven),
|
|
|
|
mnesia:add_table_index(vcard_search, lmiddle),
|
|
|
|
mnesia:add_table_index(vcard_search, lnickname),
|
|
|
|
mnesia:add_table_index(vcard_search, lbday),
|
|
|
|
mnesia:add_table_index(vcard_search, lctry),
|
|
|
|
mnesia:add_table_index(vcard_search, llocality),
|
|
|
|
mnesia:add_table_index(vcard_search, lemail),
|
|
|
|
mnesia:add_table_index(vcard_search, lorgname),
|
|
|
|
mnesia:add_table_index(vcard_search, lorgunit);
|
|
|
|
_ -> 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),
|
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, fun gen_iq_handler:check_type/1,
|
|
|
|
one_queue),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, Host,
|
|
|
|
?NS_VCARD, ?MODULE, process_local_iq, IQDisc),
|
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_VCARD, ?MODULE, process_sm_iq, IQDisc),
|
|
|
|
ejabberd_hooks:add(disco_sm_features, Host, ?MODULE,
|
|
|
|
get_sm_features, 50),
|
|
|
|
MyHost = gen_mod:get_opt_host(Host, Opts,
|
|
|
|
<<"vjud.@HOST@">>),
|
|
|
|
Search = gen_mod:get_opt(search, Opts,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
2015-11-13 19:30:11 +01:00
|
|
|
false),
|
2005-06-20 05:18:13 +02:00
|
|
|
register(gen_mod:get_module_proc(Host, ?PROCNAME),
|
2005-08-25 22:48:45 +02:00
|
|
|
spawn(?MODULE, init, [MyHost, Host, Search])).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2005-08-25 22:48:45 +02:00
|
|
|
init(Host, ServerHost, Search) ->
|
2004-07-10 00:34:26 +02:00
|
|
|
case Search of
|
2013-03-14 10:33:02 +01:00
|
|
|
false -> loop(Host, ServerHost);
|
|
|
|
_ ->
|
2016-03-13 09:38:40 +01:00
|
|
|
ejabberd_router:register_route(Host, ServerHost),
|
2013-03-14 10:33:02 +01:00
|
|
|
loop(Host, ServerHost)
|
2004-07-10 00:34:26 +02:00
|
|
|
end.
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2005-08-25 22:48:45 +02:00
|
|
|
loop(Host, ServerHost) ->
|
2003-01-02 22:01:12 +01:00
|
|
|
receive
|
2013-03-14 10:33:02 +01:00
|
|
|
{route, From, To, Packet} ->
|
|
|
|
case catch do_route(ServerHost, From, To, Packet) of
|
|
|
|
{'EXIT', Reason} -> ?ERROR_MSG("~p", [Reason]);
|
|
|
|
_ -> ok
|
|
|
|
end,
|
|
|
|
loop(Host, ServerHost);
|
|
|
|
stop -> ejabberd_router:unregister_route(Host), ok;
|
|
|
|
_ -> loop(Host, ServerHost)
|
2003-01-02 22:01:12 +01:00
|
|
|
end.
|
|
|
|
|
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_local, Host,
|
|
|
|
?NS_VCARD),
|
|
|
|
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host,
|
|
|
|
?NS_VCARD),
|
|
|
|
ejabberd_hooks:delete(disco_sm_features, Host, ?MODULE,
|
|
|
|
get_sm_features, 50),
|
2005-06-20 05:18:13 +02:00
|
|
|
Proc = gen_mod:get_module_proc(Host, ?PROCNAME),
|
|
|
|
Proc ! stop,
|
|
|
|
{wait, Proc}.
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
get_sm_features({error, _Error} = Acc, _From, _To,
|
|
|
|
_Node, _Lang) ->
|
2005-09-04 03:58:47 +02:00
|
|
|
Acc;
|
|
|
|
get_sm_features(Acc, _From, _To, Node, _Lang) ->
|
|
|
|
case Node of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> ->
|
|
|
|
case Acc of
|
|
|
|
{result, Features} ->
|
|
|
|
{result, [?NS_DISCO_INFO, ?NS_VCARD | Features]};
|
|
|
|
empty -> {result, [?NS_DISCO_INFO, ?NS_VCARD]}
|
|
|
|
end;
|
|
|
|
_ -> Acc
|
2003-01-02 22:01:12 +01:00
|
|
|
end.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_local_iq(_From, _To,
|
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]};
|
2013-03-14 10:33:02 +01:00
|
|
|
get ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"vCard">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"FN">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata, <<"ejabberd">>}]},
|
|
|
|
#xmlel{name = <<"URL">>, attrs = [],
|
|
|
|
children = [{xmlcdata, ?EJABBERD_URI}]},
|
|
|
|
#xmlel{name = <<"DESC">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"Erlang Jabber Server">>))/binary,
|
2016-01-13 12:29:14 +01:00
|
|
|
"\nCopyright (c) 2002-2016 ProcessOne">>}]},
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"BDAY">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata, <<"2002-11-16">>}]}]}]}
|
|
|
|
end.
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
process_sm_iq(From, To,
|
2016-03-31 10:00:29 +02:00
|
|
|
#iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
|
2003-01-02 22:01:12 +01:00
|
|
|
case Type of
|
2013-03-14 10:33:02 +01:00
|
|
|
set ->
|
|
|
|
#jid{user = User, lserver = LServer} = From,
|
|
|
|
case lists:member(LServer, ?MYHOSTS) of
|
|
|
|
true ->
|
|
|
|
set_vcard(User, LServer, SubEl),
|
|
|
|
IQ#iq{type = result, sub_el = []};
|
|
|
|
false ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"The query is only allowed from local users">>,
|
|
|
|
IQ#iq{type = error, sub_el = [SubEl, ?ERRT_NOT_ALLOWED(Lang, Txt)]}
|
2013-03-14 10:33:02 +01:00
|
|
|
end;
|
|
|
|
get ->
|
|
|
|
#jid{luser = LUser, lserver = LServer} = To,
|
|
|
|
case get_vcard(LUser, LServer) of
|
|
|
|
error ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Database failure">>,
|
2013-03-14 10:33:02 +01:00
|
|
|
IQ#iq{type = error,
|
2016-03-31 10:00:29 +02:00
|
|
|
sub_el = [SubEl, ?ERRT_INTERNAL_SERVER_ERROR(Lang, Txt)]};
|
2014-10-24 18:55:49 +02:00
|
|
|
[] ->
|
|
|
|
IQ#iq{type = result,
|
|
|
|
sub_el = [#xmlel{name = <<"vCard">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD}],
|
|
|
|
children = []}]};
|
2013-03-14 10:33:02 +01:00
|
|
|
Els -> IQ#iq{type = result, sub_el = Els}
|
|
|
|
end
|
2012-04-27 11:52:05 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
get_vcard(LUser, LServer) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
get_vcard(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
get_vcard(LUser, LServer, mnesia) ->
|
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () -> mnesia:read({vcard, US}) end,
|
2012-04-27 11:52:05 +02:00
|
|
|
case mnesia:transaction(F) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:map(fun (R) -> R#vcard.vcard end, Rs);
|
|
|
|
{aborted, _Reason} -> error
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
|
|
|
get_vcard(LUser, LServer, odbc) ->
|
2016-02-26 13:45:28 +01:00
|
|
|
case catch odbc_queries:get_vcard(LServer, LUser) of
|
|
|
|
{selected, [{SVCARD}]} ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml_stream:parse_element(SVCARD) of
|
2013-03-14 10:33:02 +01:00
|
|
|
{error, _Reason} -> error;
|
|
|
|
VCARD -> [VCARD]
|
|
|
|
end;
|
2016-02-26 13:45:28 +01:00
|
|
|
{selected, []} -> [];
|
2013-03-14 10:33:02 +01:00
|
|
|
_ -> error
|
2012-11-06 16:58:08 +01:00
|
|
|
end;
|
|
|
|
get_vcard(LUser, LServer, riak) ->
|
2014-07-14 05:49:02 +02:00
|
|
|
case ejabberd_riak:get(vcard, vcard_schema(), {LUser, LServer}) of
|
2012-11-13 13:56:27 +01:00
|
|
|
{ok, R} ->
|
|
|
|
[R#vcard.vcard];
|
2012-11-06 16:58:08 +01:00
|
|
|
{error, notfound} ->
|
|
|
|
[];
|
|
|
|
_ ->
|
|
|
|
error
|
2003-01-02 22:01:12 +01:00
|
|
|
end.
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
set_vcard(User, LServer, VCARD) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
FN = fxml:get_path_s(VCARD, [{elem, <<"FN">>}, cdata]),
|
|
|
|
Family = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"N">>}, {elem, <<"FAMILY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Given = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"N">>}, {elem, <<"GIVEN">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Middle = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"N">>}, {elem, <<"MIDDLE">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Nickname = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"NICKNAME">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
BDay = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"BDAY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
CTRY = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"ADR">>}, {elem, <<"CTRY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Locality = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"ADR">>}, {elem, <<"LOCALITY">>},
|
|
|
|
cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
EMail1 = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"EMAIL">>}, {elem, <<"USERID">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
EMail2 = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"EMAIL">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
OrgName = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"ORG">>}, {elem, <<"ORGNAME">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
OrgUnit = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"ORG">>}, {elem, <<"ORGUNIT">>}, cdata]),
|
2004-03-08 21:20:15 +01:00
|
|
|
EMail = case EMail1 of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> EMail2;
|
|
|
|
_ -> EMail1
|
2004-03-08 21:20:15 +01:00
|
|
|
end,
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
2013-03-14 10:33:02 +01:00
|
|
|
LFN = string2lower(FN),
|
|
|
|
LFamily = string2lower(Family),
|
|
|
|
LGiven = string2lower(Given),
|
|
|
|
LMiddle = string2lower(Middle),
|
2012-02-14 13:18:18 +01:00
|
|
|
LNickname = string2lower(Nickname),
|
2013-03-14 10:33:02 +01:00
|
|
|
LBDay = string2lower(BDay),
|
|
|
|
LCTRY = string2lower(CTRY),
|
2012-02-14 13:18:18 +01:00
|
|
|
LLocality = string2lower(Locality),
|
2013-03-14 10:33:02 +01:00
|
|
|
LEMail = string2lower(EMail),
|
|
|
|
LOrgName = string2lower(OrgName),
|
|
|
|
LOrgUnit = string2lower(OrgUnit),
|
|
|
|
if (LUser == error) ->
|
|
|
|
{error, badarg};
|
|
|
|
true ->
|
|
|
|
case gen_mod:db_type(LServer, ?MODULE) of
|
|
|
|
mnesia ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun () ->
|
|
|
|
mnesia:write(#vcard{us = US, vcard = VCARD}),
|
|
|
|
mnesia:write(#vcard_search{us = US,
|
|
|
|
user = {User, LServer},
|
|
|
|
luser = LUser, fn = FN,
|
|
|
|
lfn = LFN,
|
|
|
|
family = Family,
|
|
|
|
lfamily = LFamily,
|
|
|
|
given = Given,
|
|
|
|
lgiven = LGiven,
|
|
|
|
middle = Middle,
|
|
|
|
lmiddle = LMiddle,
|
|
|
|
nickname = Nickname,
|
|
|
|
lnickname = LNickname,
|
|
|
|
bday = BDay,
|
|
|
|
lbday = LBDay,
|
|
|
|
ctry = CTRY,
|
|
|
|
lctry = LCTRY,
|
|
|
|
locality = Locality,
|
|
|
|
llocality = LLocality,
|
|
|
|
email = EMail,
|
|
|
|
lemail = LEMail,
|
|
|
|
orgname = OrgName,
|
|
|
|
lorgname = LOrgName,
|
|
|
|
orgunit = OrgUnit,
|
|
|
|
lorgunit = LOrgUnit})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F);
|
2012-11-13 13:56:27 +01:00
|
|
|
riak ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
ejabberd_riak:put(#vcard{us = US, vcard = VCARD},
|
2014-07-14 05:49:02 +02:00
|
|
|
vcard_schema(),
|
2012-11-13 13:56:27 +01:00
|
|
|
[{'2i', [{<<"user">>, User},
|
|
|
|
{<<"luser">>, LUser},
|
|
|
|
{<<"fn">>, FN},
|
|
|
|
{<<"lfn">>, LFN},
|
|
|
|
{<<"family">>, Family},
|
|
|
|
{<<"lfamily">>, LFamily},
|
|
|
|
{<<"given">>, Given},
|
|
|
|
{<<"lgiven">>, LGiven},
|
|
|
|
{<<"middle">>, Middle},
|
|
|
|
{<<"lmiddle">>, LMiddle},
|
|
|
|
{<<"nickname">>, Nickname},
|
|
|
|
{<<"lnickname">>, LNickname},
|
|
|
|
{<<"bday">>, BDay},
|
|
|
|
{<<"lbday">>, LBDay},
|
|
|
|
{<<"ctry">>, CTRY},
|
|
|
|
{<<"lctry">>, LCTRY},
|
|
|
|
{<<"locality">>, Locality},
|
|
|
|
{<<"llocality">>, LLocality},
|
|
|
|
{<<"email">>, EMail},
|
|
|
|
{<<"lemail">>, LEMail},
|
|
|
|
{<<"orgname">>, OrgName},
|
|
|
|
{<<"lorgname">>, LOrgName},
|
|
|
|
{<<"orgunit">>, OrgUnit},
|
|
|
|
{<<"lorgunit">>, LOrgUnit}]}]);
|
2013-03-14 10:33:02 +01:00
|
|
|
odbc ->
|
2016-02-26 13:45:28 +01:00
|
|
|
SVCARD = fxml:element_to_binary(VCARD),
|
|
|
|
odbc_queries:set_vcard(LServer, LUser, BDay, CTRY,
|
|
|
|
EMail, FN, Family, Given, LBDay,
|
|
|
|
LCTRY, LEMail, LFN, LFamily,
|
|
|
|
LGiven, LLocality, LMiddle,
|
|
|
|
LNickname, LOrgName, LOrgUnit,
|
|
|
|
Locality, Middle, Nickname, OrgName,
|
|
|
|
OrgUnit, SVCARD, User)
|
2013-03-14 10:33:02 +01:00
|
|
|
end,
|
|
|
|
ejabberd_hooks:run(vcard_set, LServer,
|
|
|
|
[LUser, LServer, VCARD])
|
2003-10-20 20:23:30 +02:00
|
|
|
end.
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2012-02-14 13:18:18 +01:00
|
|
|
string2lower(String) ->
|
|
|
|
case stringprep:tolower(String) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Lower when is_binary(Lower) -> Lower;
|
|
|
|
error -> str:to_lower(String)
|
2012-02-14 13:18:18 +01:00
|
|
|
end.
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
-define(TLFIELD(Type, Label, Var),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"field">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"type">>, Type},
|
|
|
|
{<<"label">>, translate:translate(Lang, Label)},
|
|
|
|
{<<"var">>, Var}],
|
|
|
|
children = []}).
|
2003-01-03 20:36:59 +01:00
|
|
|
|
2004-02-26 23:00:04 +01:00
|
|
|
-define(FORM(JID),
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"instructions">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"You need an x:data capable client to "
|
|
|
|
"search">>)}]},
|
|
|
|
#xmlel{name = <<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>, ?NS_XDATA}, {<<"type">>, <<"form">>}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"title">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"Search users in ">>))/binary,
|
2015-11-24 16:44:13 +01:00
|
|
|
(jid:to_string(JID))/binary>>}]},
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"instructions">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"Fill in the form to search for any matching "
|
|
|
|
"Jabber User (Add * to the end of field "
|
|
|
|
"to match substring)">>)}]},
|
|
|
|
?TLFIELD(<<"text-single">>, <<"User">>, <<"user">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Full Name">>, <<"fn">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Name">>, <<"first">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Middle Name">>,
|
|
|
|
<<"middle">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Family Name">>,
|
|
|
|
<<"last">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Nickname">>, <<"nick">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Birthday">>, <<"bday">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Country">>, <<"ctry">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"City">>, <<"locality">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Email">>, <<"email">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Organization Name">>,
|
|
|
|
<<"orgname">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Organization Unit">>,
|
|
|
|
<<"orgunit">>)]}]).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2005-08-25 22:48:45 +02:00
|
|
|
do_route(ServerHost, From, To, Packet) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User, resource = Resource} = To,
|
2013-03-14 10:33:02 +01:00
|
|
|
if (User /= <<"">>) or (Resource /= <<"">>) ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
true ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
|
|
|
#iq{type = Type, xmlns = ?NS_SEARCH, lang = Lang,
|
|
|
|
sub_el = SubEl} ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
XDataEl = find_xdata_el(SubEl),
|
|
|
|
case XDataEl of
|
|
|
|
false ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Data form not found">>,
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERRT_BAD_REQUEST(Lang, Txt)),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
XData = jlib:parse_xdata_submit(XDataEl),
|
|
|
|
case XData of
|
|
|
|
invalid ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Incorrect data form">>,
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet, ?ERRT_BAD_REQUEST(Lang, Txt)),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_SEARCH}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name =
|
|
|
|
<<"x">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_XDATA},
|
|
|
|
{<<"type">>,
|
|
|
|
<<"result">>}],
|
|
|
|
children
|
|
|
|
=
|
|
|
|
search_result(Lang,
|
|
|
|
To,
|
|
|
|
ServerHost,
|
|
|
|
XData)}]}]},
|
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
jlib:iq_to_xml(ResIQ))
|
|
|
|
end
|
|
|
|
end;
|
|
|
|
get ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_SEARCH}],
|
|
|
|
children = ?FORM(To)}]},
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
|
|
|
#iq{type = Type, xmlns = ?NS_DISCO_INFO, lang = Lang} ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERRT_NOT_ALLOWED(Lang, Txt)),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
get ->
|
|
|
|
Info = ejabberd_hooks:run_fold(disco_info, ServerHost,
|
|
|
|
[],
|
|
|
|
[ServerHost, ?MODULE,
|
|
|
|
<<"">>, <<"">>]),
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_DISCO_INFO}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name =
|
|
|
|
<<"identity">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"category">>,
|
|
|
|
<<"directory">>},
|
|
|
|
{<<"type">>,
|
|
|
|
<<"user">>},
|
|
|
|
{<<"name">>,
|
|
|
|
translate:translate(Lang,
|
|
|
|
<<"vCard User Search">>)}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name =
|
|
|
|
<<"feature">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"var">>,
|
|
|
|
?NS_DISCO_INFO}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name =
|
|
|
|
<<"feature">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"var">>,
|
|
|
|
?NS_SEARCH}],
|
|
|
|
children = []},
|
|
|
|
#xmlel{name =
|
|
|
|
<<"feature">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"var">>,
|
|
|
|
?NS_VCARD}],
|
|
|
|
children = []}]
|
|
|
|
++ Info}]},
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
2016-03-31 10:00:29 +02:00
|
|
|
#iq{type = Type, lang = Lang, xmlns = ?NS_DISCO_ITEMS} ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2016-03-31 10:00:29 +02:00
|
|
|
Txt = <<"Value 'set' of 'type' attribute is not allowed">>,
|
|
|
|
Err = jlib:make_error_reply(Packet, ?ERRT_NOT_ALLOWED(Lang, Txt)),
|
2013-03-14 10:33:02 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
get ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"query">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"xmlns">>,
|
|
|
|
?NS_DISCO_ITEMS}],
|
|
|
|
children = []}]},
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
|
|
|
#iq{type = get, xmlns = ?NS_VCARD, lang = Lang} ->
|
|
|
|
ResIQ = IQ#iq{type = result,
|
|
|
|
sub_el =
|
|
|
|
[#xmlel{name = <<"vCard">>,
|
|
|
|
attrs = [{<<"xmlns">>, ?NS_VCARD}],
|
|
|
|
children = iq_get_vcard(Lang)}]},
|
|
|
|
ejabberd_router:route(To, From, jlib:iq_to_xml(ResIQ));
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
2003-01-02 22:01:12 +01:00
|
|
|
end.
|
|
|
|
|
2004-02-26 23:00:04 +01:00
|
|
|
iq_get_vcard(Lang) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"FN">>, attrs = [],
|
|
|
|
children = [{xmlcdata, <<"ejabberd/mod_vcard">>}]},
|
|
|
|
#xmlel{name = <<"URL">>, attrs = [],
|
|
|
|
children = [{xmlcdata, ?EJABBERD_URI}]},
|
|
|
|
#xmlel{name = <<"DESC">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"ejabberd vCard module">>))/binary,
|
2016-01-13 12:29:14 +01:00
|
|
|
"\nCopyright (c) 2003-2016 ProcessOne">>}]}].
|
2013-03-14 10:33:02 +01:00
|
|
|
|
|
|
|
find_xdata_el(#xmlel{children = SubEls}) ->
|
2003-01-03 20:36:59 +01:00
|
|
|
find_xdata_el1(SubEls).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
find_xdata_el1([]) -> false;
|
|
|
|
find_xdata_el1([#xmlel{name = Name, attrs = Attrs,
|
|
|
|
children = SubEls}
|
|
|
|
| Els]) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
case fxml:get_attr_s(<<"xmlns">>, Attrs) of
|
2013-03-14 10:33:02 +01:00
|
|
|
?NS_XDATA ->
|
|
|
|
#xmlel{name = Name, attrs = Attrs, children = SubEls};
|
|
|
|
_ -> find_xdata_el1(Els)
|
2003-01-03 20:36:59 +01:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
find_xdata_el1([_ | Els]) -> find_xdata_el1(Els).
|
2003-01-03 20:36:59 +01:00
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
-define(LFIELD(Label, Var),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"field">>,
|
|
|
|
attrs =
|
|
|
|
[{<<"label">>, translate:translate(Lang, Label)},
|
|
|
|
{<<"var">>, Var}],
|
|
|
|
children = []}).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2005-08-25 22:48:45 +02:00
|
|
|
search_result(Lang, JID, ServerHost, Data) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
[#xmlel{name = <<"title">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[{xmlcdata,
|
|
|
|
<<(translate:translate(Lang,
|
|
|
|
<<"Search Results for ">>))/binary,
|
2015-11-24 16:44:13 +01:00
|
|
|
(jid:to_string(JID))/binary>>}]},
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"reported">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[?TLFIELD(<<"text-single">>, <<"Jabber ID">>,
|
|
|
|
<<"jid">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Full Name">>, <<"fn">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Name">>, <<"first">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Middle Name">>,
|
|
|
|
<<"middle">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Family Name">>,
|
|
|
|
<<"last">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Nickname">>, <<"nick">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Birthday">>, <<"bday">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Country">>, <<"ctry">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"City">>, <<"locality">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Email">>, <<"email">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Organization Name">>,
|
|
|
|
<<"orgname">>),
|
|
|
|
?TLFIELD(<<"text-single">>, <<"Organization Unit">>,
|
|
|
|
<<"orgunit">>)]}]
|
|
|
|
++
|
|
|
|
lists:map(fun (R) -> record_to_item(ServerHost, R) end,
|
|
|
|
search(ServerHost, Data)).
|
2003-01-03 20:36:59 +01:00
|
|
|
|
|
|
|
-define(FIELD(Var, Val),
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"field">>, attrs = [{<<"var">>, Var}],
|
|
|
|
children =
|
|
|
|
[#xmlel{name = <<"value">>, attrs = [],
|
|
|
|
children = [{xmlcdata, Val}]}]}).
|
|
|
|
|
|
|
|
record_to_item(LServer,
|
|
|
|
[Username, FN, Family, Given, Middle, Nickname, BDay,
|
|
|
|
CTRY, Locality, EMail, OrgName, OrgUnit]) ->
|
|
|
|
#xmlel{name = <<"item">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[?FIELD(<<"jid">>,
|
|
|
|
<<Username/binary, "@", LServer/binary>>),
|
|
|
|
?FIELD(<<"fn">>, FN), ?FIELD(<<"last">>, Family),
|
|
|
|
?FIELD(<<"first">>, Given),
|
|
|
|
?FIELD(<<"middle">>, Middle),
|
|
|
|
?FIELD(<<"nick">>, Nickname), ?FIELD(<<"bday">>, BDay),
|
|
|
|
?FIELD(<<"ctry">>, CTRY),
|
|
|
|
?FIELD(<<"locality">>, Locality),
|
|
|
|
?FIELD(<<"email">>, EMail),
|
|
|
|
?FIELD(<<"orgname">>, OrgName),
|
|
|
|
?FIELD(<<"orgunit">>, OrgUnit)]};
|
2012-04-27 11:52:05 +02:00
|
|
|
record_to_item(_LServer, #vcard_search{} = R) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
{User, Server} = R#vcard_search.user,
|
2013-03-14 10:33:02 +01:00
|
|
|
#xmlel{name = <<"item">>, attrs = [],
|
|
|
|
children =
|
|
|
|
[?FIELD(<<"jid">>, <<User/binary, "@", Server/binary>>),
|
|
|
|
?FIELD(<<"fn">>, (R#vcard_search.fn)),
|
|
|
|
?FIELD(<<"last">>, (R#vcard_search.family)),
|
|
|
|
?FIELD(<<"first">>, (R#vcard_search.given)),
|
|
|
|
?FIELD(<<"middle">>, (R#vcard_search.middle)),
|
|
|
|
?FIELD(<<"nick">>, (R#vcard_search.nickname)),
|
|
|
|
?FIELD(<<"bday">>, (R#vcard_search.bday)),
|
|
|
|
?FIELD(<<"ctry">>, (R#vcard_search.ctry)),
|
|
|
|
?FIELD(<<"locality">>, (R#vcard_search.locality)),
|
|
|
|
?FIELD(<<"email">>, (R#vcard_search.email)),
|
|
|
|
?FIELD(<<"orgname">>, (R#vcard_search.orgname)),
|
|
|
|
?FIELD(<<"orgunit">>, (R#vcard_search.orgunit))]}.
|
2003-10-28 21:26:43 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
search(LServer, Data) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
DBType = gen_mod:db_type(LServer, ?MODULE),
|
2012-04-27 11:52:05 +02:00
|
|
|
MatchSpec = make_matchspec(LServer, Data, DBType),
|
2013-03-14 10:33:02 +01:00
|
|
|
AllowReturnAll = gen_mod:get_module_opt(LServer, ?MODULE, allow_return_all,
|
|
|
|
fun(B) when is_boolean(B) -> B end,
|
|
|
|
false),
|
2012-04-27 11:52:05 +02:00
|
|
|
search(LServer, MatchSpec, AllowReturnAll, DBType).
|
|
|
|
|
|
|
|
search(LServer, MatchSpec, AllowReturnAll, mnesia) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
if (MatchSpec == #vcard_search{_ = '_'}) and
|
|
|
|
not AllowReturnAll ->
|
|
|
|
[];
|
|
|
|
true ->
|
|
|
|
case catch mnesia:dirty_select(vcard_search,
|
|
|
|
[{MatchSpec, [], ['$_']}])
|
|
|
|
of
|
|
|
|
{'EXIT', Reason} -> ?ERROR_MSG("~p", [Reason]), [];
|
|
|
|
Rs ->
|
|
|
|
case gen_mod:get_module_opt(LServer, ?MODULE, matches,
|
|
|
|
fun(infinity) -> infinity;
|
|
|
|
(I) when is_integer(I),
|
|
|
|
I>0 ->
|
|
|
|
I
|
|
|
|
end, ?JUD_MATCHES) of
|
|
|
|
infinity ->
|
|
|
|
Rs;
|
|
|
|
Val ->
|
|
|
|
lists:sublist(Rs, Val)
|
|
|
|
end
|
|
|
|
end
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
|
|
|
search(LServer, MatchSpec, AllowReturnAll, odbc) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
if (MatchSpec == <<"">>) and not AllowReturnAll -> [];
|
|
|
|
true ->
|
|
|
|
Limit = case gen_mod:get_module_opt(LServer, ?MODULE, matches,
|
|
|
|
fun(infinity) -> infinity;
|
|
|
|
(I) when is_integer(I),
|
|
|
|
I>0 ->
|
|
|
|
I
|
|
|
|
end, ?JUD_MATCHES) of
|
|
|
|
infinity ->
|
|
|
|
<<"">>;
|
|
|
|
Val ->
|
|
|
|
[<<" LIMIT ">>,
|
|
|
|
jlib:integer_to_binary(Val)]
|
|
|
|
end,
|
|
|
|
case catch ejabberd_odbc:sql_query(LServer,
|
|
|
|
[<<"select username, fn, family, given, "
|
|
|
|
"middle, nickname, bday, ctry, "
|
|
|
|
"locality, email, orgname, orgunit "
|
|
|
|
"from vcard_search ">>,
|
|
|
|
MatchSpec, Limit, <<";">>])
|
|
|
|
of
|
|
|
|
{selected,
|
|
|
|
[<<"username">>, <<"fn">>, <<"family">>, <<"given">>,
|
|
|
|
<<"middle">>, <<"nickname">>, <<"bday">>, <<"ctry">>,
|
|
|
|
<<"locality">>, <<"email">>, <<"orgname">>,
|
|
|
|
<<"orgunit">>],
|
|
|
|
Rs}
|
|
|
|
when is_list(Rs) ->
|
|
|
|
Rs;
|
|
|
|
Error -> ?ERROR_MSG("~p", [Error]), []
|
|
|
|
end
|
2012-11-06 16:58:08 +01:00
|
|
|
end;
|
|
|
|
search(_LServer, _MatchSpec, _AllowReturnAll, riak) ->
|
|
|
|
[].
|
2003-10-28 21:26:43 +01:00
|
|
|
|
2012-04-27 11:52:05 +02:00
|
|
|
make_matchspec(LServer, Data, mnesia) ->
|
2003-01-08 21:42:03 +01:00
|
|
|
GlobMatch = #vcard_search{_ = '_'},
|
2012-04-27 11:52:05 +02:00
|
|
|
Match = filter_fields(Data, GlobMatch, LServer, mnesia),
|
|
|
|
Match;
|
|
|
|
make_matchspec(LServer, Data, odbc) ->
|
2012-11-06 16:58:08 +01:00
|
|
|
filter_fields(Data, <<"">>, LServer, odbc);
|
|
|
|
make_matchspec(_LServer, _Data, riak) ->
|
|
|
|
[].
|
2003-01-03 20:36:59 +01:00
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
filter_fields([], Match, _LServer, mnesia) -> Match;
|
2012-04-27 11:52:05 +02:00
|
|
|
filter_fields([], Match, _LServer, odbc) ->
|
|
|
|
case Match of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> <<"">>;
|
|
|
|
_ -> [<<" where ">>, Match]
|
2012-04-27 11:52:05 +02:00
|
|
|
end;
|
2013-03-14 10:33:02 +01:00
|
|
|
filter_fields([{SVar, [Val]} | Ds], Match, LServer,
|
|
|
|
mnesia)
|
|
|
|
when is_binary(Val) and (Val /= <<"">>) ->
|
2012-02-14 13:18:18 +01:00
|
|
|
LVal = string2lower(Val),
|
2003-01-03 20:36:59 +01:00
|
|
|
NewMatch = case SVar of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"user">> ->
|
|
|
|
case gen_mod:get_module_opt(LServer, ?MODULE,
|
|
|
|
search_all_hosts,
|
|
|
|
fun(B) when is_boolean(B) ->
|
|
|
|
B
|
|
|
|
end, true)
|
|
|
|
of
|
|
|
|
true -> Match#vcard_search{luser = make_val(LVal)};
|
|
|
|
false ->
|
|
|
|
Host = find_my_host(LServer),
|
|
|
|
Match#vcard_search{us = {make_val(LVal), Host}}
|
|
|
|
end;
|
|
|
|
<<"fn">> -> Match#vcard_search{lfn = make_val(LVal)};
|
|
|
|
<<"last">> ->
|
|
|
|
Match#vcard_search{lfamily = make_val(LVal)};
|
|
|
|
<<"first">> ->
|
|
|
|
Match#vcard_search{lgiven = make_val(LVal)};
|
|
|
|
<<"middle">> ->
|
|
|
|
Match#vcard_search{lmiddle = make_val(LVal)};
|
|
|
|
<<"nick">> ->
|
|
|
|
Match#vcard_search{lnickname = make_val(LVal)};
|
|
|
|
<<"bday">> ->
|
|
|
|
Match#vcard_search{lbday = make_val(LVal)};
|
|
|
|
<<"ctry">> ->
|
|
|
|
Match#vcard_search{lctry = make_val(LVal)};
|
|
|
|
<<"locality">> ->
|
|
|
|
Match#vcard_search{llocality = make_val(LVal)};
|
|
|
|
<<"email">> ->
|
|
|
|
Match#vcard_search{lemail = make_val(LVal)};
|
|
|
|
<<"orgname">> ->
|
|
|
|
Match#vcard_search{lorgname = make_val(LVal)};
|
|
|
|
<<"orgunit">> ->
|
|
|
|
Match#vcard_search{lorgunit = make_val(LVal)};
|
|
|
|
_ -> Match
|
2003-01-03 20:36:59 +01:00
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
filter_fields(Ds, NewMatch, LServer, mnesia);
|
2013-03-14 10:33:02 +01:00
|
|
|
filter_fields([{SVar, [Val]} | Ds], Match, LServer,
|
|
|
|
odbc)
|
|
|
|
when is_binary(Val) and (Val /= <<"">>) ->
|
2012-04-27 11:52:05 +02:00
|
|
|
LVal = string2lower(Val),
|
|
|
|
NewMatch = case SVar of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"user">> -> make_val(Match, <<"lusername">>, LVal);
|
|
|
|
<<"fn">> -> make_val(Match, <<"lfn">>, LVal);
|
|
|
|
<<"last">> -> make_val(Match, <<"lfamily">>, LVal);
|
|
|
|
<<"first">> -> make_val(Match, <<"lgiven">>, LVal);
|
|
|
|
<<"middle">> -> make_val(Match, <<"lmiddle">>, LVal);
|
|
|
|
<<"nick">> -> make_val(Match, <<"lnickname">>, LVal);
|
|
|
|
<<"bday">> -> make_val(Match, <<"lbday">>, LVal);
|
|
|
|
<<"ctry">> -> make_val(Match, <<"lctry">>, LVal);
|
|
|
|
<<"locality">> ->
|
|
|
|
make_val(Match, <<"llocality">>, LVal);
|
|
|
|
<<"email">> -> make_val(Match, <<"lemail">>, LVal);
|
|
|
|
<<"orgname">> -> make_val(Match, <<"lorgname">>, LVal);
|
|
|
|
<<"orgunit">> -> make_val(Match, <<"lorgunit">>, LVal);
|
|
|
|
_ -> Match
|
2012-04-27 11:52:05 +02:00
|
|
|
end,
|
|
|
|
filter_fields(Ds, NewMatch, LServer, odbc);
|
|
|
|
filter_fields([_ | Ds], Match, LServer, DBType) ->
|
|
|
|
filter_fields(Ds, Match, LServer, DBType).
|
|
|
|
|
|
|
|
make_val(Match, Field, Val) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Condition = case str:suffix(<<"*">>, Val) of
|
|
|
|
true ->
|
|
|
|
Val1 = str:substr(Val, 1, byte_size(Val) - 1),
|
|
|
|
SVal = <<(ejabberd_odbc:escape_like(Val1))/binary,
|
|
|
|
"%">>,
|
|
|
|
[Field, <<" LIKE '">>, SVal, <<"'">>];
|
|
|
|
_ ->
|
|
|
|
SVal = ejabberd_odbc:escape(Val),
|
|
|
|
[Field, <<" = '">>, SVal, <<"'">>]
|
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
case Match of
|
2013-03-14 10:33:02 +01:00
|
|
|
<<"">> -> Condition;
|
|
|
|
_ -> [Match, <<" and ">>, Condition]
|
2012-04-27 11:52:05 +02:00
|
|
|
end.
|
2003-01-09 20:59:16 +01:00
|
|
|
|
2004-09-10 22:57:00 +02:00
|
|
|
make_val(Val) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
case str:suffix(<<"*">>, Val) of
|
|
|
|
true -> [str:substr(Val, 1, byte_size(Val) - 1)] ++ '_';
|
|
|
|
_ -> Val
|
2004-09-10 22:57:00 +02:00
|
|
|
end.
|
2003-01-09 20:59:16 +01:00
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
find_my_host(LServer) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
Parts = str:tokens(LServer, <<".">>),
|
2005-04-17 20:08:34 +02:00
|
|
|
find_my_host(Parts, ?MYHOSTS).
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
find_my_host([], _Hosts) -> ?MYNAME;
|
2005-04-17 20:08:34 +02:00
|
|
|
find_my_host([_ | Tail] = Parts, Hosts) ->
|
|
|
|
Domain = parts_to_string(Parts),
|
|
|
|
case lists:member(Domain, Hosts) of
|
2013-03-14 10:33:02 +01:00
|
|
|
true -> Domain;
|
|
|
|
false -> find_my_host(Tail, Hosts)
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
parts_to_string(Parts) ->
|
2013-03-14 10:33:02 +01:00
|
|
|
str:strip(list_to_binary(
|
|
|
|
lists:map(fun (S) -> <<S/binary, $.>> end, Parts)),
|
|
|
|
right, $.).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2003-01-09 20:59:16 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
set_vcard_t(R, _) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = R#vcard.us,
|
2013-03-14 10:33:02 +01:00
|
|
|
User = US,
|
2003-01-09 20:59:16 +01:00
|
|
|
VCARD = R#vcard.vcard,
|
2016-02-03 19:03:17 +01:00
|
|
|
FN = fxml:get_path_s(VCARD, [{elem, <<"FN">>}, cdata]),
|
|
|
|
Family = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"N">>}, {elem, <<"FAMILY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Given = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"N">>}, {elem, <<"GIVEN">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Middle = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"N">>}, {elem, <<"MIDDLE">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Nickname = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"NICKNAME">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
BDay = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"BDAY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
CTRY = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"ADR">>}, {elem, <<"CTRY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Locality = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"ADR">>}, {elem, <<"LOCALITY">>},
|
|
|
|
cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
EMail = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"EMAIL">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
OrgName = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"ORG">>}, {elem, <<"ORGNAME">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
OrgUnit = fxml:get_path_s(VCARD,
|
2013-03-14 10:33:02 +01:00
|
|
|
[{elem, <<"ORG">>}, {elem, <<"ORGUNIT">>}, cdata]),
|
2005-04-17 20:08:34 +02:00
|
|
|
{LUser, _LServer} = US,
|
2013-03-14 10:33:02 +01:00
|
|
|
LFN = string2lower(FN),
|
|
|
|
LFamily = string2lower(Family),
|
|
|
|
LGiven = string2lower(Given),
|
|
|
|
LMiddle = string2lower(Middle),
|
2012-02-14 13:18:18 +01:00
|
|
|
LNickname = string2lower(Nickname),
|
2013-03-14 10:33:02 +01:00
|
|
|
LBDay = string2lower(BDay),
|
|
|
|
LCTRY = string2lower(CTRY),
|
2012-02-14 13:18:18 +01:00
|
|
|
LLocality = string2lower(Locality),
|
2013-03-14 10:33:02 +01:00
|
|
|
LEMail = string2lower(EMail),
|
|
|
|
LOrgName = string2lower(OrgName),
|
|
|
|
LOrgUnit = string2lower(OrgUnit),
|
|
|
|
mnesia:write(#vcard_search{us = US, user = User,
|
|
|
|
luser = LUser, fn = FN, lfn = LFN,
|
|
|
|
family = Family, lfamily = LFamily,
|
|
|
|
given = Given, lgiven = LGiven,
|
|
|
|
middle = Middle, lmiddle = LMiddle,
|
|
|
|
nickname = Nickname,
|
|
|
|
lnickname = LNickname, bday = BDay,
|
|
|
|
lbday = LBDay, ctry = CTRY, lctry = LCTRY,
|
|
|
|
locality = Locality,
|
|
|
|
llocality = LLocality, email = EMail,
|
|
|
|
lemail = LEMail, orgname = OrgName,
|
|
|
|
lorgname = LOrgName, orgunit = OrgUnit,
|
|
|
|
lorgunit = LOrgUnit}).
|
2003-01-09 20:59:16 +01:00
|
|
|
|
|
|
|
reindex_vcards() ->
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () -> mnesia:foldl(fun set_vcard_t/2, [], vcard)
|
2003-01-09 20:59:16 +01:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2005-04-17 20:08:34 +02:00
|
|
|
remove_user(User, Server) ->
|
2015-11-24 16:44:13 +01:00
|
|
|
LUser = jid:nodeprep(User),
|
|
|
|
LServer = jid:nameprep(Server),
|
2013-03-14 10:33:02 +01:00
|
|
|
remove_user(LUser, LServer,
|
|
|
|
gen_mod:db_type(LServer, ?MODULE)).
|
2012-04-27 11:52:05 +02:00
|
|
|
|
|
|
|
remove_user(LUser, LServer, mnesia) ->
|
2005-04-17 20:08:34 +02:00
|
|
|
US = {LUser, LServer},
|
2013-03-14 10:33:02 +01:00
|
|
|
F = fun () ->
|
2005-04-17 20:08:34 +02:00
|
|
|
mnesia:delete({vcard, US}),
|
|
|
|
mnesia:delete({vcard_search, US})
|
2003-01-26 21:16:53 +01:00
|
|
|
end,
|
2012-04-27 11:52:05 +02:00
|
|
|
mnesia:transaction(F);
|
|
|
|
remove_user(LUser, LServer, odbc) ->
|
2016-02-26 13:45:28 +01:00
|
|
|
ejabberd_odbc:sql_transaction(
|
|
|
|
LServer,
|
|
|
|
fun() ->
|
|
|
|
ejabberd_odbc:sql_query_t(
|
|
|
|
?SQL("delete from vcard where username=%(LUser)s")),
|
|
|
|
ejabberd_odbc:sql_query_t(
|
|
|
|
?SQL("delete from vcard_search where lusername=%(LUser)s"))
|
|
|
|
end);
|
2012-11-06 16:58:08 +01:00
|
|
|
remove_user(LUser, LServer, riak) ->
|
2012-11-13 13:56:27 +01:00
|
|
|
{atomic, ejabberd_riak:delete(vcard, {LUser, LServer})}.
|
2005-04-17 20:08:34 +02:00
|
|
|
|
|
|
|
update_tables() ->
|
|
|
|
update_vcard_table(),
|
|
|
|
update_vcard_search_table().
|
|
|
|
|
|
|
|
update_vcard_table() ->
|
|
|
|
Fields = record_info(fields, vcard),
|
|
|
|
case mnesia:table_info(vcard, attributes) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Fields ->
|
|
|
|
ejabberd_config:convert_table_to_binary(
|
|
|
|
vcard, Fields, set,
|
|
|
|
fun(#vcard{us = {U, _}}) -> U end,
|
|
|
|
fun(#vcard{us = {U, S}, vcard = El} = R) ->
|
|
|
|
R#vcard{us = {iolist_to_binary(U),
|
|
|
|
iolist_to_binary(S)},
|
2016-02-03 19:03:17 +01:00
|
|
|
vcard = fxml:to_xmlel(El)}
|
2013-03-14 10:33:02 +01:00
|
|
|
end);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating vcard table", []),
|
|
|
|
mnesia:transform_table(vcard, ignore, Fields)
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
|
|
|
update_vcard_search_table() ->
|
|
|
|
Fields = record_info(fields, vcard_search),
|
|
|
|
case mnesia:table_info(vcard_search, attributes) of
|
2013-03-14 10:33:02 +01:00
|
|
|
Fields ->
|
|
|
|
ejabberd_config:convert_table_to_binary(
|
|
|
|
vcard_search, Fields, set,
|
|
|
|
fun(#vcard_search{us = {U, _}}) -> U end,
|
|
|
|
fun(#vcard_search{} = VS) ->
|
|
|
|
[vcard_search | L] = tuple_to_list(VS),
|
|
|
|
NewL = lists:map(
|
|
|
|
fun({U, S}) ->
|
|
|
|
{iolist_to_binary(U),
|
|
|
|
iolist_to_binary(S)};
|
|
|
|
(Str) ->
|
|
|
|
iolist_to_binary(Str)
|
|
|
|
end, L),
|
|
|
|
list_to_tuple([vcard_search | NewL])
|
|
|
|
end);
|
|
|
|
_ ->
|
|
|
|
?INFO_MSG("Recreating vcard_search table", []),
|
|
|
|
mnesia:transform_table(vcard_search, ignore, Fields)
|
2005-04-17 20:08:34 +02:00
|
|
|
end.
|
|
|
|
|
2014-07-14 05:49:02 +02:00
|
|
|
vcard_schema() ->
|
|
|
|
{record_info(fields, vcard), #vcard{}}.
|
|
|
|
|
2013-03-14 10:33:02 +01:00
|
|
|
export(_Server) ->
|
|
|
|
[{vcard,
|
|
|
|
fun(Host, #vcard{us = {LUser, LServer}, vcard = VCARD})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(LUser),
|
|
|
|
SVCARD =
|
2016-02-03 19:03:17 +01:00
|
|
|
ejabberd_odbc:escape(fxml:element_to_binary(VCARD)),
|
2013-03-14 10:33:02 +01:00
|
|
|
[[<<"delete from vcard where username='">>, Username, <<"';">>],
|
|
|
|
[<<"insert into vcard(username, vcard) values ('">>,
|
|
|
|
Username, <<"', '">>, SVCARD, <<"');">>]];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end},
|
|
|
|
{vcard_search,
|
|
|
|
fun(Host, #vcard_search{user = {User, LServer}, luser = LUser,
|
|
|
|
fn = FN, lfn = LFN, family = Family,
|
|
|
|
lfamily = LFamily, given = Given,
|
|
|
|
lgiven = LGiven, middle = Middle,
|
|
|
|
lmiddle = LMiddle, nickname = Nickname,
|
|
|
|
lnickname = LNickname, bday = BDay,
|
|
|
|
lbday = LBDay, ctry = CTRY, lctry = LCTRY,
|
|
|
|
locality = Locality, llocality = LLocality,
|
|
|
|
email = EMail, lemail = LEMail,
|
|
|
|
orgname = OrgName, lorgname = LOrgName,
|
|
|
|
orgunit = OrgUnit, lorgunit = LOrgUnit})
|
|
|
|
when LServer == Host ->
|
|
|
|
Username = ejabberd_odbc:escape(User),
|
|
|
|
LUsername = ejabberd_odbc:escape(LUser),
|
|
|
|
SFN = ejabberd_odbc:escape(FN),
|
|
|
|
SLFN = ejabberd_odbc:escape(LFN),
|
|
|
|
SFamily = ejabberd_odbc:escape(Family),
|
|
|
|
SLFamily = ejabberd_odbc:escape(LFamily),
|
|
|
|
SGiven = ejabberd_odbc:escape(Given),
|
|
|
|
SLGiven = ejabberd_odbc:escape(LGiven),
|
|
|
|
SMiddle = ejabberd_odbc:escape(Middle),
|
|
|
|
SLMiddle = ejabberd_odbc:escape(LMiddle),
|
|
|
|
SNickname = ejabberd_odbc:escape(Nickname),
|
|
|
|
SLNickname = ejabberd_odbc:escape(LNickname),
|
|
|
|
SBDay = ejabberd_odbc:escape(BDay),
|
|
|
|
SLBDay = ejabberd_odbc:escape(LBDay),
|
|
|
|
SCTRY = ejabberd_odbc:escape(CTRY),
|
|
|
|
SLCTRY = ejabberd_odbc:escape(LCTRY),
|
|
|
|
SLocality = ejabberd_odbc:escape(Locality),
|
|
|
|
SLLocality = ejabberd_odbc:escape(LLocality),
|
|
|
|
SEMail = ejabberd_odbc:escape(EMail),
|
|
|
|
SLEMail = ejabberd_odbc:escape(LEMail),
|
|
|
|
SOrgName = ejabberd_odbc:escape(OrgName),
|
|
|
|
SLOrgName = ejabberd_odbc:escape(LOrgName),
|
|
|
|
SOrgUnit = ejabberd_odbc:escape(OrgUnit),
|
|
|
|
SLOrgUnit = ejabberd_odbc:escape(LOrgUnit),
|
|
|
|
[[<<"delete from vcard_search where lusername='">>,
|
|
|
|
LUsername, <<"';">>],
|
|
|
|
[<<"insert into vcard_search( username, "
|
|
|
|
"lusername, fn, lfn, family, lfamily, "
|
|
|
|
" given, lgiven, middle, lmiddle, "
|
|
|
|
"nickname, lnickname, bday, lbday, "
|
|
|
|
"ctry, lctry, locality, llocality, "
|
|
|
|
" email, lemail, orgname, lorgname, "
|
|
|
|
"orgunit, lorgunit)values (">>,
|
|
|
|
<<" '">>, Username, <<"', '">>, LUsername,
|
|
|
|
<<"', '">>, SFN, <<"', '">>, SLFN,
|
|
|
|
<<"', '">>, SFamily, <<"', '">>, SLFamily,
|
|
|
|
<<"', '">>, SGiven, <<"', '">>, SLGiven,
|
|
|
|
<<"', '">>, SMiddle, <<"', '">>, SLMiddle,
|
|
|
|
<<"', '">>, SNickname, <<"', '">>, SLNickname,
|
|
|
|
<<"', '">>, SBDay, <<"', '">>, SLBDay,
|
|
|
|
<<"', '">>, SCTRY, <<"', '">>, SLCTRY,
|
|
|
|
<<"', '">>, SLocality, <<"', '">>, SLLocality,
|
|
|
|
<<"', '">>, SEMail, <<"', '">>, SLEMail,
|
|
|
|
<<"', '">>, SOrgName, <<"', '">>, SLOrgName,
|
|
|
|
<<"', '">>, SOrgUnit, <<"', '">>, SLOrgUnit,
|
|
|
|
<<"');">>]];
|
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
2013-07-21 12:24:36 +02:00
|
|
|
|
|
|
|
import(LServer) ->
|
|
|
|
[{<<"select username, vcard from vcard;">>,
|
|
|
|
fun([LUser, SVCard]) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
#xmlel{} = VCARD = fxml_stream:parse_element(SVCard),
|
2013-07-21 12:24:36 +02:00
|
|
|
#vcard{us = {LUser, LServer}, vcard = VCARD}
|
|
|
|
end},
|
|
|
|
{<<"select username, lusername, fn, lfn, family, lfamily, "
|
|
|
|
"given, lgiven, middle, lmiddle, nickname, lnickname, "
|
|
|
|
"bday, lbday, ctry, lctry, locality, llocality, email, "
|
|
|
|
"lemail, orgname, lorgname, orgunit, lorgunit from vcard_search;">>,
|
|
|
|
fun([User, LUser, FN, LFN,
|
|
|
|
Family, LFamily, Given, LGiven,
|
|
|
|
Middle, LMiddle, Nickname, LNickname,
|
|
|
|
BDay, LBDay, CTRY, LCTRY, Locality, LLocality,
|
|
|
|
EMail, LEMail, OrgName, LOrgName, OrgUnit, LOrgUnit]) ->
|
|
|
|
#vcard_search{us = {LUser, LServer},
|
|
|
|
user = {User, LServer}, luser = LUser,
|
|
|
|
fn = FN, lfn = LFN, family = Family,
|
|
|
|
lfamily = LFamily, given = Given,
|
|
|
|
lgiven = LGiven, middle = Middle,
|
|
|
|
lmiddle = LMiddle, nickname = Nickname,
|
|
|
|
lnickname = LNickname, bday = BDay,
|
|
|
|
lbday = LBDay, ctry = CTRY, lctry = LCTRY,
|
|
|
|
locality = Locality, llocality = LLocality,
|
|
|
|
email = EMail, lemail = LEMail,
|
|
|
|
orgname = OrgName, lorgname = LOrgName,
|
|
|
|
orgunit = OrgUnit, lorgunit = LOrgUnit}
|
|
|
|
end}].
|
|
|
|
|
|
|
|
import(_LServer, mnesia, #vcard{} = VCard) ->
|
|
|
|
mnesia:dirty_write(VCard);
|
|
|
|
import(_LServer, mnesia, #vcard_search{} = S) ->
|
|
|
|
mnesia:dirty_write(S);
|
2013-07-21 14:53:44 +02:00
|
|
|
import(_LServer, riak, #vcard{us = {LUser, _}, vcard = El} = VCard) ->
|
2016-02-03 19:03:17 +01:00
|
|
|
FN = fxml:get_path_s(El, [{elem, <<"FN">>}, cdata]),
|
|
|
|
Family = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"N">>}, {elem, <<"FAMILY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Given = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"N">>}, {elem, <<"GIVEN">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Middle = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"N">>}, {elem, <<"MIDDLE">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Nickname = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"NICKNAME">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
BDay = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"BDAY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
CTRY = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"ADR">>}, {elem, <<"CTRY">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
Locality = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"ADR">>}, {elem, <<"LOCALITY">>},
|
|
|
|
cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
EMail1 = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"EMAIL">>}, {elem, <<"USERID">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
EMail2 = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"EMAIL">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
OrgName = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"ORG">>}, {elem, <<"ORGNAME">>}, cdata]),
|
2016-02-03 19:03:17 +01:00
|
|
|
OrgUnit = fxml:get_path_s(El,
|
2013-07-21 14:53:44 +02:00
|
|
|
[{elem, <<"ORG">>}, {elem, <<"ORGUNIT">>}, cdata]),
|
|
|
|
EMail = case EMail1 of
|
|
|
|
<<"">> -> EMail2;
|
|
|
|
_ -> EMail1
|
|
|
|
end,
|
|
|
|
LFN = string2lower(FN),
|
|
|
|
LFamily = string2lower(Family),
|
|
|
|
LGiven = string2lower(Given),
|
|
|
|
LMiddle = string2lower(Middle),
|
|
|
|
LNickname = string2lower(Nickname),
|
|
|
|
LBDay = string2lower(BDay),
|
|
|
|
LCTRY = string2lower(CTRY),
|
|
|
|
LLocality = string2lower(Locality),
|
|
|
|
LEMail = string2lower(EMail),
|
|
|
|
LOrgName = string2lower(OrgName),
|
|
|
|
LOrgUnit = string2lower(OrgUnit),
|
2014-07-14 05:49:02 +02:00
|
|
|
ejabberd_riak:put(VCard, vcard_schema(),
|
2013-07-21 14:53:44 +02:00
|
|
|
[{'2i', [{<<"user">>, LUser},
|
|
|
|
{<<"luser">>, LUser},
|
|
|
|
{<<"fn">>, FN},
|
|
|
|
{<<"lfn">>, LFN},
|
|
|
|
{<<"family">>, Family},
|
|
|
|
{<<"lfamily">>, LFamily},
|
|
|
|
{<<"given">>, Given},
|
|
|
|
{<<"lgiven">>, LGiven},
|
|
|
|
{<<"middle">>, Middle},
|
|
|
|
{<<"lmiddle">>, LMiddle},
|
|
|
|
{<<"nickname">>, Nickname},
|
|
|
|
{<<"lnickname">>, LNickname},
|
|
|
|
{<<"bday">>, BDay},
|
|
|
|
{<<"lbday">>, LBDay},
|
|
|
|
{<<"ctry">>, CTRY},
|
|
|
|
{<<"lctry">>, LCTRY},
|
|
|
|
{<<"locality">>, Locality},
|
|
|
|
{<<"llocality">>, LLocality},
|
|
|
|
{<<"email">>, EMail},
|
|
|
|
{<<"lemail">>, LEMail},
|
|
|
|
{<<"orgname">>, OrgName},
|
|
|
|
{<<"lorgname">>, LOrgName},
|
|
|
|
{<<"orgunit">>, OrgUnit},
|
|
|
|
{<<"lorgunit">>, LOrgUnit}]}]);
|
|
|
|
import(_LServer, riak, #vcard_search{}) ->
|
|
|
|
ok;
|
2013-07-21 12:24:36 +02:00
|
|
|
import(_, _, _) ->
|
|
|
|
pass.
|
2015-06-01 14:38:27 +02:00
|
|
|
|
|
|
|
mod_opt_type(allow_return_all) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(db_type) -> fun gen_mod:v_db/1;
|
|
|
|
mod_opt_type(host) -> fun iolist_to_binary/1;
|
|
|
|
mod_opt_type(iqdisc) -> fun gen_iq_handler:check_type/1;
|
|
|
|
mod_opt_type(matches) ->
|
|
|
|
fun (infinity) -> infinity;
|
|
|
|
(I) when is_integer(I), I > 0 -> I
|
|
|
|
end;
|
|
|
|
mod_opt_type(search) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(search_all_hosts) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
|
|
|
mod_opt_type(_) ->
|
|
|
|
[allow_return_all, db_type, host, iqdisc, matches,
|
|
|
|
search, search_all_hosts].
|