2016-04-13 16:37:52 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
%%% @author Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%% @copyright (C) 2016, Evgeny Khramtsov
|
|
|
|
%%% @doc
|
|
|
|
%%%
|
|
|
|
%%% @end
|
|
|
|
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
|
|
|
%%%-------------------------------------------------------------------
|
|
|
|
-module(mod_vcard_sql).
|
|
|
|
|
|
|
|
-compile([{parse_transform, ejabberd_sql_pt}]).
|
|
|
|
|
|
|
|
-behaviour(mod_vcard).
|
|
|
|
|
|
|
|
%% API
|
2016-07-29 16:39:13 +02:00
|
|
|
-export([init/2, stop/1, get_vcard/2, set_vcard/4, search/4, remove_user/2,
|
2016-11-22 14:48:01 +01:00
|
|
|
search_fields/1, search_reported/1, import/3, export/1]).
|
|
|
|
-export([is_search_supported/1]).
|
2016-04-13 16:37:52 +02:00
|
|
|
|
2016-07-26 10:29:17 +02:00
|
|
|
-include("xmpp.hrl").
|
2016-04-13 16:37:52 +02:00
|
|
|
-include("mod_vcard.hrl").
|
|
|
|
-include("logger.hrl").
|
|
|
|
-include("ejabberd_sql_pt.hrl").
|
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
|
|
|
ok.
|
|
|
|
|
2016-07-29 16:39:13 +02:00
|
|
|
stop(_Host) ->
|
|
|
|
ok.
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
is_search_supported(_LServer) ->
|
|
|
|
true.
|
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
get_vcard(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch sql_queries:get_vcard(LServer, LUser) of
|
2016-04-13 16:37:52 +02:00
|
|
|
{selected, [{SVCARD}]} ->
|
|
|
|
case fxml_stream:parse_element(SVCARD) of
|
|
|
|
{error, _Reason} -> error;
|
|
|
|
VCARD -> [VCARD]
|
|
|
|
end;
|
|
|
|
{selected, []} -> [];
|
|
|
|
_ -> error
|
|
|
|
end.
|
|
|
|
|
|
|
|
set_vcard(LUser, LServer, VCARD,
|
|
|
|
#vcard_search{user = {User, _},
|
|
|
|
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}) ->
|
|
|
|
SVCARD = fxml:element_to_binary(VCARD),
|
2016-04-20 11:27:32 +02:00
|
|
|
sql_queries:set_vcard(LServer, LUser, BDay, CTRY,
|
2016-04-13 16:37:52 +02:00
|
|
|
EMail, FN, Family, Given, LBDay,
|
|
|
|
LCTRY, LEMail, LFN, LFamily,
|
|
|
|
LGiven, LLocality, LMiddle,
|
|
|
|
LNickname, LOrgName, LOrgUnit,
|
|
|
|
Locality, Middle, Nickname, OrgName,
|
|
|
|
OrgUnit, SVCARD, User).
|
|
|
|
|
|
|
|
search(LServer, Data, AllowReturnAll, MaxMatch) ->
|
|
|
|
MatchSpec = make_matchspec(LServer, Data),
|
|
|
|
if (MatchSpec == <<"">>) and not AllowReturnAll -> [];
|
|
|
|
true ->
|
|
|
|
Limit = case MaxMatch of
|
|
|
|
infinity ->
|
|
|
|
<<"">>;
|
|
|
|
Val ->
|
2016-09-24 22:34:28 +02:00
|
|
|
[<<" LIMIT ">>, integer_to_binary(Val)]
|
2016-04-13 16:37:52 +02:00
|
|
|
end,
|
2016-04-20 11:27:32 +02:00
|
|
|
case catch ejabberd_sql:sql_query(
|
2016-04-13 16:37:52 +02:00
|
|
|
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) ->
|
2016-07-29 16:39:13 +02:00
|
|
|
[row_to_item(LServer, R) || R <- Rs];
|
2016-04-13 16:37:52 +02:00
|
|
|
Error ->
|
|
|
|
?ERROR_MSG("~p", [Error]), []
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2016-07-29 16:39:13 +02:00
|
|
|
search_fields(_LServer) ->
|
|
|
|
[{<<"User">>, <<"user">>},
|
|
|
|
{<<"Full Name">>, <<"fn">>},
|
|
|
|
{<<"Name">>, <<"first">>},
|
|
|
|
{<<"Middle Name">>, <<"middle">>},
|
|
|
|
{<<"Family Name">>, <<"last">>},
|
|
|
|
{<<"Nickname">>, <<"nick">>},
|
|
|
|
{<<"Birthday">>, <<"bday">>},
|
|
|
|
{<<"Country">>, <<"ctry">>},
|
|
|
|
{<<"City">>, <<"locality">>},
|
|
|
|
{<<"Email">>, <<"email">>},
|
|
|
|
{<<"Organization Name">>, <<"orgname">>},
|
|
|
|
{<<"Organization Unit">>, <<"orgunit">>}].
|
|
|
|
|
|
|
|
search_reported(_LServer) ->
|
|
|
|
[{<<"Jabber ID">>, <<"jid">>},
|
|
|
|
{<<"Full Name">>, <<"fn">>},
|
|
|
|
{<<"Name">>, <<"first">>},
|
|
|
|
{<<"Middle Name">>, <<"middle">>},
|
|
|
|
{<<"Family Name">>, <<"last">>},
|
|
|
|
{<<"Nickname">>, <<"nick">>},
|
|
|
|
{<<"Birthday">>, <<"bday">>},
|
|
|
|
{<<"Country">>, <<"ctry">>},
|
|
|
|
{<<"City">>, <<"locality">>},
|
|
|
|
{<<"Email">>, <<"email">>},
|
|
|
|
{<<"Organization Name">>, <<"orgname">>},
|
|
|
|
{<<"Organization Unit">>, <<"orgunit">>}].
|
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
remove_user(LUser, LServer) ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_transaction(
|
2016-04-13 16:37:52 +02:00
|
|
|
LServer,
|
|
|
|
fun() ->
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2016-04-13 16:37:52 +02:00
|
|
|
?SQL("delete from vcard where username=%(LUser)s")),
|
2016-04-20 11:27:32 +02:00
|
|
|
ejabberd_sql:sql_query_t(
|
2016-04-13 16:37:52 +02:00
|
|
|
?SQL("delete from vcard_search where lusername=%(LUser)s"))
|
|
|
|
end).
|
|
|
|
|
|
|
|
export(_Server) ->
|
|
|
|
[{vcard,
|
|
|
|
fun(Host, #vcard{us = {LUser, LServer}, vcard = VCARD})
|
|
|
|
when LServer == Host ->
|
2016-05-04 20:01:05 +02:00
|
|
|
SVCARD = fxml:element_to_binary(VCARD),
|
|
|
|
[?SQL("delete from vcard where username=%(LUser)s;"),
|
|
|
|
?SQL("insert into vcard(username, vcard) values ("
|
|
|
|
"%(LUser)s, %(SVCARD)s);")];
|
2016-04-13 16:37:52 +02:00
|
|
|
(_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 ->
|
2016-05-04 20:01:05 +02:00
|
|
|
[?SQL("delete from vcard_search where lusername=%(LUser)s;"),
|
|
|
|
?SQL("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 ("
|
|
|
|
" %(LUser)s, %(User)s,"
|
|
|
|
" %(FN)s, %(LFN)s,"
|
|
|
|
" %(Family)s, %(LFamily)s,"
|
|
|
|
" %(Given)s, %(LGiven)s,"
|
|
|
|
" %(Middle)s, %(LMiddle)s,"
|
|
|
|
" %(Nickname)s, %(LNickname)s,"
|
|
|
|
" %(BDay)s, %(LBDay)s,"
|
|
|
|
" %(CTRY)s, %(LCTRY)s,"
|
|
|
|
" %(Locality)s, %(LLocality)s,"
|
|
|
|
" %(EMail)s, %(LEMail)s,"
|
|
|
|
" %(OrgName)s, %(LOrgName)s,"
|
|
|
|
" %(OrgUnit)s, %(LOrgUnit)s);")];
|
2016-04-13 16:37:52 +02:00
|
|
|
(_Host, _R) ->
|
|
|
|
[]
|
|
|
|
end}].
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(_, _, _) ->
|
|
|
|
ok.
|
2016-04-13 16:37:52 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
make_matchspec(LServer, Data) ->
|
|
|
|
filter_fields(Data, <<"">>, LServer).
|
|
|
|
|
|
|
|
filter_fields([], Match, _LServer) ->
|
|
|
|
case Match of
|
|
|
|
<<"">> -> <<"">>;
|
|
|
|
_ -> [<<" where ">>, Match]
|
|
|
|
end;
|
|
|
|
filter_fields([{SVar, [Val]} | Ds], Match, LServer)
|
|
|
|
when is_binary(Val) and (Val /= <<"">>) ->
|
|
|
|
LVal = mod_vcard:string2lower(Val),
|
|
|
|
NewMatch = case SVar of
|
|
|
|
<<"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
|
|
|
|
end,
|
|
|
|
filter_fields(Ds, NewMatch, LServer);
|
|
|
|
filter_fields([_ | Ds], Match, LServer) ->
|
|
|
|
filter_fields(Ds, Match, LServer).
|
|
|
|
|
|
|
|
make_val(Match, Field, Val) ->
|
|
|
|
Condition = case str:suffix(<<"*">>, Val) of
|
|
|
|
true ->
|
|
|
|
Val1 = str:substr(Val, 1, byte_size(Val) - 1),
|
2016-05-12 17:32:13 +02:00
|
|
|
SVal = <<(ejabberd_sql:escape(
|
|
|
|
ejabberd_sql:escape_like_arg_circumflex(
|
|
|
|
Val1)))/binary,
|
2016-04-13 16:37:52 +02:00
|
|
|
"%">>,
|
2016-05-12 17:32:13 +02:00
|
|
|
[Field, <<" LIKE '">>, SVal, <<"' ESCAPE '^'">>];
|
2016-04-13 16:37:52 +02:00
|
|
|
_ ->
|
2016-04-20 11:27:32 +02:00
|
|
|
SVal = ejabberd_sql:escape(Val),
|
2016-04-13 16:37:52 +02:00
|
|
|
[Field, <<" = '">>, SVal, <<"'">>]
|
|
|
|
end,
|
|
|
|
case Match of
|
|
|
|
<<"">> -> Condition;
|
|
|
|
_ -> [Match, <<" and ">>, Condition]
|
|
|
|
end.
|
2016-07-29 16:39:13 +02:00
|
|
|
|
|
|
|
row_to_item(LServer, [Username, FN, Family, Given, Middle, Nickname, BDay,
|
|
|
|
CTRY, Locality, EMail, OrgName, OrgUnit]) ->
|
|
|
|
[{<<"jid">>, <<Username/binary, $@, LServer/binary>>},
|
|
|
|
{<<"fn">>, FN},
|
|
|
|
{<<"last">>, Family},
|
|
|
|
{<<"first">>, Given},
|
|
|
|
{<<"middle">>, Middle},
|
|
|
|
{<<"nick">>, Nickname},
|
|
|
|
{<<"bday">>, BDay},
|
|
|
|
{<<"ctry">>, CTRY},
|
|
|
|
{<<"locality">>, Locality},
|
|
|
|
{<<"email">>, EMail},
|
|
|
|
{<<"orgname">>, OrgName},
|
|
|
|
{<<"orgunit">>, OrgUnit}].
|