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
|
|
|
|
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,
|
2016-04-13 16:37:52 +02:00
|
|
|
process_local_iq/3, process_sm_iq/3, string2lower/1,
|
2016-07-06 13:58:48 +02:00
|
|
|
remove_user/2, export/1, import/1, import/3, depends/2,
|
2016-04-13 16:37:52 +02:00
|
|
|
mod_opt_type/1, set_vcard/3, make_vcard_search/4]).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2013-04-08 11:12:54 +02:00
|
|
|
-include("logger.hrl").
|
2013-03-14 10:33:02 +01:00
|
|
|
-include("jlib.hrl").
|
2016-04-13 16:37:52 +02:00
|
|
|
-include("mod_vcard.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
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
-define(PROCNAME, ejabberd_mod_vcard).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
-callback init(binary(), gen_mod:opts()) -> any().
|
|
|
|
-callback import(binary(), #vcard{} | #vcard_search{}) -> ok | pass.
|
|
|
|
-callback get_vcard(binary(), binary()) -> [xmlel()] | error.
|
|
|
|
-callback set_vcard(binary(), binary(),
|
|
|
|
xmlel(), #vcard_search{}) -> {atomic, any()}.
|
|
|
|
-callback search(binary(), [{binary(), [binary()]}], boolean(),
|
|
|
|
infinity | pos_integer()) -> [binary()].
|
|
|
|
-callback remove_user(binary(), binary()) -> {atomic, any()}.
|
|
|
|
|
2005-06-20 05:18:13 +02:00
|
|
|
start(Host, Opts) ->
|
2016-04-13 16:37:52 +02:00
|
|
|
Mod = gen_mod:db_mod(Host, Opts, ?MODULE),
|
|
|
|
Mod:init(Host, Opts),
|
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) ->
|
2016-04-13 16:37:52 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:get_vcard(LUser, LServer).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
make_vcard_search(User, LUser, 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,
|
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),
|
2016-04-13 16:37:52 +02:00
|
|
|
US = {LUser, LServer},
|
|
|
|
#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}.
|
|
|
|
|
|
|
|
set_vcard(User, LServer, VCARD) ->
|
|
|
|
case jid:nodeprep(User) of
|
|
|
|
error ->
|
|
|
|
{error, badarg};
|
|
|
|
LUser ->
|
|
|
|
VCardSearch = make_vcard_search(User, LUser, LServer, VCARD),
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:set_vcard(LUser, LServer, VCARD, VCardSearch),
|
|
|
|
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) ->
|
2016-04-13 16:37:52 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
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),
|
2016-04-13 16:37:52 +02:00
|
|
|
MaxMatch = gen_mod:get_module_opt(LServer, ?MODULE, matches,
|
|
|
|
fun(infinity) -> infinity;
|
|
|
|
(I) when is_integer(I),
|
|
|
|
I>0 ->
|
|
|
|
I
|
|
|
|
end, ?JUD_MATCHES),
|
|
|
|
Mod:search(LServer, Data, AllowReturnAll, MaxMatch).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2003-01-09 20:59:16 +01:00
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
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),
|
2016-04-13 16:37:52 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:remove_user(LUser, LServer).
|
2005-04-17 20:08:34 +02:00
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
export(LServer) ->
|
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:export(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
|
|
|
import(LServer) ->
|
2016-04-13 16:37:52 +02:00
|
|
|
Mod = gen_mod:db_mod(LServer, ?MODULE),
|
|
|
|
Mod:import(LServer).
|
2013-07-21 12:24:36 +02:00
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
import(LServer, DBType, VCard) ->
|
|
|
|
Mod = gen_mod:db_mod(DBType, ?MODULE),
|
|
|
|
Mod:import(LServer, VCard).
|
2015-06-01 14:38:27 +02:00
|
|
|
|
2016-07-06 13:58:48 +02:00
|
|
|
depends(_Host, _Opts) ->
|
|
|
|
[].
|
|
|
|
|
2015-06-01 14:38:27 +02:00
|
|
|
mod_opt_type(allow_return_all) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end;
|
2016-04-27 16:10:50 +02:00
|
|
|
mod_opt_type(db_type) -> fun(T) -> ejabberd_config:v_db(?MODULE, T) end;
|
2015-06-01 14:38:27 +02:00
|
|
|
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].
|