2016-04-13 16:37:52 +02:00
|
|
|
%%%-------------------------------------------------------------------
|
2016-12-27 10:44:07 +01:00
|
|
|
%%% File : mod_vcard_mnesia.erl
|
|
|
|
%%% Author : Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-04-13 16:37:52 +02:00
|
|
|
%%% Created : 13 Apr 2016 by Evgeny Khramtsov <ekhramtsov@process-one.net>
|
2016-12-27 10:44:07 +01:00
|
|
|
%%%
|
|
|
|
%%%
|
2018-01-05 21:18:58 +01:00
|
|
|
%%% ejabberd, Copyright (C) 2002-2018 ProcessOne
|
2016-12-27 10:44:07 +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.
|
|
|
|
%%%
|
|
|
|
%%% 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.
|
|
|
|
%%%
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
-module(mod_vcard_mnesia).
|
|
|
|
|
|
|
|
-behaviour(mod_vcard).
|
|
|
|
|
|
|
|
%% API
|
2016-11-22 14:48:01 +01:00
|
|
|
-export([init/2, stop/1, import/3, get_vcard/2, set_vcard/4, search/4,
|
2016-07-29 16:39:13 +02:00
|
|
|
search_fields/1, search_reported/1, remove_user/2]).
|
2016-11-22 14:48:01 +01:00
|
|
|
-export([is_search_supported/1]).
|
2017-04-23 15:37:58 +02:00
|
|
|
-export([need_transform/1, transform/1]).
|
2018-01-23 08:54:52 +01:00
|
|
|
-export([mod_opt_type/1, mod_options/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").
|
2017-09-24 11:42:35 +02:00
|
|
|
-include("translate.hrl").
|
2016-04-13 16:37:52 +02:00
|
|
|
|
|
|
|
%%%===================================================================
|
|
|
|
%%% API
|
|
|
|
%%%===================================================================
|
|
|
|
init(_Host, _Opts) ->
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, vcard,
|
2016-04-13 16:37:52 +02:00
|
|
|
[{disc_only_copies, [node()]},
|
|
|
|
{attributes, record_info(fields, vcard)}]),
|
2016-11-30 11:09:17 +01:00
|
|
|
ejabberd_mnesia:create(?MODULE, vcard_search,
|
2016-04-13 16:37:52 +02:00
|
|
|
[{disc_copies, [node()]},
|
|
|
|
{attributes,
|
2017-01-17 15:05:14 +01:00
|
|
|
record_info(fields, vcard_search)},
|
|
|
|
{index, [ luser, lfn, lfamily,
|
|
|
|
lgiven, lmiddle, lnickname,
|
|
|
|
lbday, lctry, llocality,
|
|
|
|
lemail, lorgname, lorgunit
|
2017-04-23 15:37:58 +02:00
|
|
|
]}]).
|
2016-04-13 16:37:52 +02:00
|
|
|
|
2016-07-29 16:39:13 +02:00
|
|
|
stop(_Host) ->
|
|
|
|
ok.
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
is_search_supported(_ServerHost) ->
|
|
|
|
true.
|
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
get_vcard(LUser, LServer) ->
|
|
|
|
US = {LUser, LServer},
|
2017-05-18 20:24:47 +02:00
|
|
|
Rs = mnesia:dirty_read(vcard, US),
|
|
|
|
{ok, lists:map(fun (R) -> R#vcard.vcard end, Rs)}.
|
2016-04-13 16:37:52 +02:00
|
|
|
|
|
|
|
set_vcard(LUser, LServer, VCARD, VCardSearch) ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun () ->
|
|
|
|
mnesia:write(#vcard{us = US, vcard = VCARD}),
|
|
|
|
mnesia:write(VCardSearch)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
search(LServer, Data, AllowReturnAll, MaxMatch) ->
|
|
|
|
MatchSpec = make_matchspec(LServer, Data),
|
|
|
|
if (MatchSpec == #vcard_search{_ = '_'}) and
|
|
|
|
not AllowReturnAll ->
|
|
|
|
[];
|
|
|
|
true ->
|
|
|
|
case catch mnesia:dirty_select(vcard_search,
|
|
|
|
[{MatchSpec, [], ['$_']}]) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]), [];
|
|
|
|
Rs ->
|
2016-07-29 16:39:13 +02:00
|
|
|
Fields = lists:map(fun record_to_item/1, Rs),
|
2016-04-13 16:37:52 +02:00
|
|
|
case MaxMatch of
|
|
|
|
infinity ->
|
2016-07-29 16:39:13 +02:00
|
|
|
Fields;
|
2016-04-13 16:37:52 +02:00
|
|
|
Val ->
|
2016-07-29 16:39:13 +02:00
|
|
|
lists:sublist(Fields, Val)
|
2016-04-13 16:37:52 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2016-07-29 16:39:13 +02:00
|
|
|
search_fields(_LServer) ->
|
2017-09-24 11:42:35 +02:00
|
|
|
[{?T("User"), <<"user">>},
|
|
|
|
{?T("Full Name"), <<"fn">>},
|
|
|
|
{?T("Name"), <<"first">>},
|
|
|
|
{?T("Middle Name"), <<"middle">>},
|
|
|
|
{?T("Family Name"), <<"last">>},
|
|
|
|
{?T("Nickname"), <<"nick">>},
|
|
|
|
{?T("Birthday"), <<"bday">>},
|
|
|
|
{?T("Country"), <<"ctry">>},
|
|
|
|
{?T("City"), <<"locality">>},
|
|
|
|
{?T("Email"), <<"email">>},
|
|
|
|
{?T("Organization Name"), <<"orgname">>},
|
|
|
|
{?T("Organization Unit"), <<"orgunit">>}].
|
2016-07-29 16:39:13 +02:00
|
|
|
|
|
|
|
search_reported(_LServer) ->
|
2017-09-24 11:42:35 +02:00
|
|
|
[{?T("Jabber ID"), <<"jid">>},
|
|
|
|
{?T("Full Name"), <<"fn">>},
|
|
|
|
{?T("Name"), <<"first">>},
|
|
|
|
{?T("Middle Name"), <<"middle">>},
|
|
|
|
{?T("Family Name"), <<"last">>},
|
|
|
|
{?T("Nickname"), <<"nick">>},
|
|
|
|
{?T("Birthday"), <<"bday">>},
|
|
|
|
{?T("Country"), <<"ctry">>},
|
|
|
|
{?T("City"), <<"locality">>},
|
|
|
|
{?T("Email"), <<"email">>},
|
|
|
|
{?T("Organization Name"), <<"orgname">>},
|
|
|
|
{?T("Organization Unit"), <<"orgunit">>}].
|
2016-07-29 16:39:13 +02:00
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
remove_user(LUser, LServer) ->
|
|
|
|
US = {LUser, LServer},
|
|
|
|
F = fun () ->
|
|
|
|
mnesia:delete({vcard, US}),
|
|
|
|
mnesia:delete({vcard_search, US})
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, <<"vcard">>, [LUser, XML, _TimeStamp]) ->
|
|
|
|
#xmlel{} = El = fxml_stream:parse_element(XML),
|
|
|
|
VCard = #vcard{us = {LUser, LServer}, vcard = El},
|
2016-04-13 16:37:52 +02:00
|
|
|
mnesia:dirty_write(VCard);
|
2016-11-22 14:48:01 +01:00
|
|
|
import(LServer, <<"vcard_search">>,
|
|
|
|
[User, LUser, FN, LFN,
|
|
|
|
Family, LFamily, Given, LGiven,
|
|
|
|
Middle, LMiddle, Nickname, LNickname,
|
|
|
|
BDay, LBDay, CTRY, LCTRY, Locality, LLocality,
|
|
|
|
EMail, LEMail, OrgName, LOrgName, OrgUnit, LOrgUnit]) ->
|
|
|
|
mnesia:dirty_write(
|
|
|
|
#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}).
|
2016-04-13 16:37:52 +02:00
|
|
|
|
2017-04-23 15:37:58 +02:00
|
|
|
need_transform(#vcard{us = {U, S}}) when is_list(U) orelse is_list(S) ->
|
|
|
|
?INFO_MSG("Mnesia table 'vcard' will be converted to binary", []),
|
|
|
|
true;
|
|
|
|
need_transform(#vcard_search{us = {U, S}}) when is_list(U) orelse is_list(S) ->
|
|
|
|
?INFO_MSG("Mnesia table 'vcard_search' will be converted to binary", []),
|
|
|
|
true;
|
|
|
|
need_transform(_) ->
|
|
|
|
false.
|
|
|
|
|
|
|
|
transform(#vcard{us = {U, S}, vcard = El} = R) ->
|
|
|
|
R#vcard{us = {iolist_to_binary(U), iolist_to_binary(S)},
|
|
|
|
vcard = fxml:to_xmlel(El)};
|
|
|
|
transform(#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]).
|
|
|
|
|
2016-04-13 16:37:52 +02:00
|
|
|
%%%===================================================================
|
|
|
|
%%% Internal functions
|
|
|
|
%%%===================================================================
|
|
|
|
make_matchspec(LServer, Data) ->
|
|
|
|
GlobMatch = #vcard_search{_ = '_'},
|
|
|
|
Match = filter_fields(Data, GlobMatch, LServer),
|
|
|
|
Match.
|
|
|
|
|
|
|
|
filter_fields([], Match, _LServer) ->
|
|
|
|
Match;
|
|
|
|
filter_fields([{SVar, [Val]} | Ds], Match, LServer)
|
|
|
|
when is_binary(Val) and (Val /= <<"">>) ->
|
|
|
|
LVal = mod_vcard:string2lower(Val),
|
|
|
|
NewMatch = case SVar of
|
|
|
|
<<"user">> ->
|
|
|
|
case gen_mod:get_module_opt(LServer, ?MODULE,
|
2018-01-23 08:54:52 +01:00
|
|
|
search_all_hosts) of
|
2016-04-13 16:37:52 +02:00
|
|
|
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
|
|
|
|
end,
|
|
|
|
filter_fields(Ds, NewMatch, LServer);
|
|
|
|
filter_fields([_ | Ds], Match, LServer) ->
|
|
|
|
filter_fields(Ds, Match, LServer).
|
|
|
|
|
|
|
|
make_val(Val) ->
|
|
|
|
case str:suffix(<<"*">>, Val) of
|
|
|
|
true -> [str:substr(Val, 1, byte_size(Val) - 1)] ++ '_';
|
|
|
|
_ -> Val
|
|
|
|
end.
|
|
|
|
|
|
|
|
find_my_host(LServer) ->
|
|
|
|
Parts = str:tokens(LServer, <<".">>),
|
2018-06-14 13:00:47 +02:00
|
|
|
find_my_host(Parts, ejabberd_config:get_myhosts()).
|
2016-04-13 16:37:52 +02:00
|
|
|
|
2018-06-14 13:00:47 +02:00
|
|
|
find_my_host([], _Hosts) -> ejabberd_config:get_myname();
|
2016-04-13 16:37:52 +02:00
|
|
|
find_my_host([_ | Tail] = Parts, Hosts) ->
|
|
|
|
Domain = parts_to_string(Parts),
|
|
|
|
case lists:member(Domain, Hosts) of
|
|
|
|
true -> Domain;
|
|
|
|
false -> find_my_host(Tail, Hosts)
|
|
|
|
end.
|
|
|
|
|
|
|
|
parts_to_string(Parts) ->
|
|
|
|
str:strip(list_to_binary(
|
|
|
|
lists:map(fun (S) -> <<S/binary, $.>> end, Parts)),
|
|
|
|
right, $.).
|
2016-07-29 16:39:13 +02:00
|
|
|
|
|
|
|
-spec record_to_item(#vcard_search{}) -> [{binary(), binary()}].
|
|
|
|
record_to_item(R) ->
|
|
|
|
{User, Server} = R#vcard_search.user,
|
|
|
|
[{<<"jid">>, <<User/binary, "@", Server/binary>>},
|
|
|
|
{<<"fn">>, (R#vcard_search.fn)},
|
|
|
|
{<<"last">>, (R#vcard_search.family)},
|
|
|
|
{<<"first">>, (R#vcard_search.given)},
|
|
|
|
{<<"middle">>, (R#vcard_search.middle)},
|
|
|
|
{<<"nick">>, (R#vcard_search.nickname)},
|
|
|
|
{<<"bday">>, (R#vcard_search.bday)},
|
|
|
|
{<<"ctry">>, (R#vcard_search.ctry)},
|
|
|
|
{<<"locality">>, (R#vcard_search.locality)},
|
|
|
|
{<<"email">>, (R#vcard_search.email)},
|
|
|
|
{<<"orgname">>, (R#vcard_search.orgname)},
|
|
|
|
{<<"orgunit">>, (R#vcard_search.orgunit)}].
|
2018-01-23 08:54:52 +01:00
|
|
|
|
|
|
|
mod_opt_type(search_all_hosts) ->
|
|
|
|
fun (B) when is_boolean(B) -> B end.
|
|
|
|
|
|
|
|
mod_options(_) ->
|
|
|
|
[{search_all_hosts, true}].
|