2003-01-02 22:01:12 +01:00
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
%%% File : mod_vcard.erl
|
|
|
|
%%% Author : Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Purpose :
|
|
|
|
%%% Created : 2 Jan 2003 by Alexey Shchepin <alexey@sevcom.net>
|
|
|
|
%%% Id : $Id$
|
|
|
|
%%%----------------------------------------------------------------------
|
|
|
|
|
|
|
|
-module(mod_vcard).
|
|
|
|
-author('alexey@sevcom.net').
|
|
|
|
-vsn('$Revision$ ').
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
-behaviour(gen_mod).
|
|
|
|
|
|
|
|
-export([start/1, init/1,
|
2003-01-02 22:01:12 +01:00
|
|
|
process_local_iq/3,
|
2003-01-09 20:59:16 +01:00
|
|
|
process_sm_iq/3,
|
2003-01-26 21:16:53 +01:00
|
|
|
reindex_vcards/0,
|
|
|
|
remove_user/1]).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
|
|
|
-include("ejabberd.hrl").
|
2003-03-09 21:46:47 +01:00
|
|
|
-include("jlib.hrl").
|
2003-01-02 22:01:12 +01:00
|
|
|
|
|
|
|
|
2003-10-28 21:26:43 +01:00
|
|
|
%-define(JUD_ALLOW_RETURN_ALL, true)
|
|
|
|
|
2003-01-09 20:59:16 +01:00
|
|
|
-record(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
|
|
|
|
}).
|
2003-01-02 22:01:12 +01:00
|
|
|
-record(vcard, {user, vcard}).
|
|
|
|
|
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
start(Opts) ->
|
2003-01-02 22:01:12 +01:00
|
|
|
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)}]),
|
2003-01-09 20:59:16 +01:00
|
|
|
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),
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
|
2003-01-22 21:40:40 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_local, ?NS_VCARD,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_local_iq, IQDisc),
|
2003-01-22 21:40:40 +01:00
|
|
|
gen_iq_handler:add_iq_handler(ejabberd_sm, ?NS_VCARD,
|
2003-01-24 21:18:33 +01:00
|
|
|
?MODULE, process_sm_iq, IQDisc),
|
|
|
|
Host = gen_mod:get_opt(host, Opts, "vjud." ++ ?MYNAME),
|
|
|
|
spawn(?MODULE, init, [Host]).
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2003-01-16 21:24:53 +01:00
|
|
|
|
2003-01-24 21:18:33 +01:00
|
|
|
init(Host) ->
|
2003-12-14 21:51:01 +01:00
|
|
|
ejabberd_router:register_route(Host),
|
2003-01-02 22:01:12 +01:00
|
|
|
loop().
|
|
|
|
|
|
|
|
loop() ->
|
|
|
|
receive
|
|
|
|
{route, From, To, Packet} ->
|
2003-10-07 22:31:44 +02:00
|
|
|
case catch do_route(From, To, Packet) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]);
|
|
|
|
_ ->
|
|
|
|
ok
|
|
|
|
end,
|
2003-01-02 22:01:12 +01:00
|
|
|
loop();
|
|
|
|
_ ->
|
|
|
|
loop()
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
2003-10-20 20:23:30 +02:00
|
|
|
process_local_iq(_From, _To, {iq, ID, Type, XMLNS, SubEl}) ->
|
2003-01-02 22:01:12 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2003-07-21 22:01:22 +02:00
|
|
|
{iq, ID, error, XMLNS, [SubEl, ?ERR_NOT_ALLOWED]};
|
2003-01-02 22:01:12 +01:00
|
|
|
get ->
|
|
|
|
{iq, ID, result, XMLNS,
|
|
|
|
[{xmlelement, "vCard",
|
|
|
|
[{"xmlns", ?NS_VCARD}],
|
|
|
|
[{xmlelement, "FN", [],
|
|
|
|
[{xmlcdata, "ejabberd"}]},
|
|
|
|
{xmlelement, "URL", [],
|
|
|
|
[{xmlcdata,
|
2003-03-23 21:08:44 +01:00
|
|
|
"http://ejabberd.jabberstudio.org/"}]},
|
2003-01-02 22:01:12 +01:00
|
|
|
{xmlelement, "DESC", [],
|
|
|
|
[{xmlcdata, "Erlang Jabber Server\n"
|
|
|
|
"Copyright (c) 2002, 2003 Alexey Shchepin"}]},
|
|
|
|
{xmlelement, "BDAY", [],
|
2003-01-11 21:25:11 +01:00
|
|
|
[{xmlcdata, "2002-11-16"}]}
|
2003-01-02 22:01:12 +01:00
|
|
|
]}]}
|
|
|
|
end.
|
|
|
|
|
|
|
|
|
|
|
|
process_sm_iq(From, To, {iq, ID, Type, XMLNS, SubEl}) ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User, lserver = LServer, luser = LUser} = From,
|
2003-07-21 22:01:22 +02:00
|
|
|
case ?MYNAME of
|
|
|
|
LServer ->
|
|
|
|
set_vcard(User, SubEl),
|
|
|
|
{iq, ID, result, XMLNS, []};
|
|
|
|
_ ->
|
|
|
|
{iq, ID, error, XMLNS,
|
|
|
|
[SubEl, ?ERR_NOT_ALLOWED]}
|
|
|
|
end;
|
2003-01-02 22:01:12 +01:00
|
|
|
get ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{luser = LUser} = To,
|
2003-01-02 22:01:12 +01:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:read({vcard, LUser})
|
|
|
|
end,
|
|
|
|
Els = case mnesia:transaction(F) of
|
|
|
|
{atomic, Rs} ->
|
|
|
|
lists:map(fun(R) ->
|
|
|
|
R#vcard.vcard
|
|
|
|
end, Rs);
|
2003-10-20 20:23:30 +02:00
|
|
|
{aborted, _Reason} ->
|
2003-01-02 22:01:12 +01:00
|
|
|
[]
|
|
|
|
end,
|
|
|
|
{iq, ID, result, XMLNS, Els}
|
|
|
|
end.
|
|
|
|
|
2003-01-09 20:59:16 +01:00
|
|
|
set_vcard(User, VCARD) ->
|
2003-01-02 22:01:12 +01:00
|
|
|
FN = xml:get_path_s(VCARD, [{elem, "FN"}, cdata]),
|
|
|
|
Family = xml:get_path_s(VCARD, [{elem, "N"}, {elem, "FAMILY"}, cdata]),
|
|
|
|
Given = xml:get_path_s(VCARD, [{elem, "N"}, {elem, "GIVEN"}, cdata]),
|
|
|
|
Middle = xml:get_path_s(VCARD, [{elem, "N"}, {elem, "MIDDLE"}, cdata]),
|
|
|
|
Nickname = xml:get_path_s(VCARD, [{elem, "NICKNAME"}, cdata]),
|
|
|
|
BDay = xml:get_path_s(VCARD, [{elem, "BDAY"}, cdata]),
|
2003-01-05 21:24:59 +01:00
|
|
|
CTRY = xml:get_path_s(VCARD, [{elem, "ADR"}, {elem, "CTRY"}, cdata]),
|
|
|
|
Locality = xml:get_path_s(VCARD, [{elem, "ADR"}, {elem, "LOCALITY"},cdata]),
|
2003-01-02 22:01:12 +01:00
|
|
|
EMail = xml:get_path_s(VCARD, [{elem, "EMAIL"}, cdata]),
|
|
|
|
OrgName = xml:get_path_s(VCARD, [{elem, "ORG"}, {elem, "ORGNAME"}, cdata]),
|
|
|
|
OrgUnit = xml:get_path_s(VCARD, [{elem, "ORG"}, {elem, "ORGUNIT"}, cdata]),
|
2003-01-03 20:36:59 +01:00
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LFN = stringprep:tolower(FN),
|
|
|
|
LFamily = stringprep:tolower(Family),
|
|
|
|
LGiven = stringprep:tolower(Given),
|
|
|
|
LMiddle = stringprep:tolower(Middle),
|
|
|
|
LNickname = stringprep:tolower(Nickname),
|
|
|
|
LBDay = stringprep:tolower(BDay),
|
|
|
|
LCTRY = stringprep:tolower(CTRY),
|
|
|
|
LLocality = stringprep:tolower(Locality),
|
|
|
|
LEMail = stringprep:tolower(EMail),
|
|
|
|
LOrgName = stringprep:tolower(OrgName),
|
|
|
|
LOrgUnit = stringprep:tolower(OrgUnit),
|
2003-01-03 20:36:59 +01:00
|
|
|
|
2003-10-20 20:23:30 +02:00
|
|
|
if
|
|
|
|
(LUser == error) or
|
|
|
|
(LFN == error) or
|
|
|
|
(LFamily == error) or
|
|
|
|
(LGiven == error) or
|
|
|
|
(LMiddle == error) or
|
|
|
|
(LNickname == error) or
|
|
|
|
(LBDay == error) or
|
|
|
|
(LCTRY == error) or
|
|
|
|
(LLocality == error) or
|
|
|
|
(LEMail == error) or
|
|
|
|
(LOrgName == error) or
|
|
|
|
(LOrgUnit == error) ->
|
|
|
|
{error, badarg};
|
|
|
|
true ->
|
|
|
|
F = fun() ->
|
2003-01-02 22:01:12 +01:00
|
|
|
mnesia:write(#vcard{user = LUser, vcard = VCARD}),
|
2003-01-09 20:59:16 +01:00
|
|
|
mnesia:write(
|
|
|
|
#vcard_search{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-10-20 20:23:30 +02:00
|
|
|
end,
|
|
|
|
mnesia:transaction(F)
|
|
|
|
end.
|
2003-01-02 22:01:12 +01:00
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
-define(TLFIELD(Type, Label, Var),
|
|
|
|
{xmlelement, "field", [{"type", Type},
|
2003-01-06 20:57:05 +01:00
|
|
|
{"label", translate:translate(Lang, Label)},
|
2003-01-05 21:24:59 +01:00
|
|
|
{"var", Var}], []}).
|
|
|
|
|
2003-01-03 20:36:59 +01:00
|
|
|
|
2003-01-02 22:01:12 +01:00
|
|
|
-define(FORM,
|
|
|
|
[{xmlelement, "instructions", [],
|
2003-01-06 20:57:05 +01:00
|
|
|
[{xmlcdata, translate:translate(Lang, "You need a x:data capable client to search")}]},
|
2003-01-02 22:01:12 +01:00
|
|
|
{xmlelement, "x", [{"xmlns", ?NS_XDATA}, {"type", "form"}],
|
2003-01-06 20:57:05 +01:00
|
|
|
[{xmlelement, "title", [],
|
|
|
|
[{xmlcdata, translate:translate(Lang, "Users Search")}]},
|
2003-01-02 22:01:12 +01:00
|
|
|
{xmlelement, "instructions", [],
|
2003-01-06 20:57:05 +01:00
|
|
|
[{xmlcdata, translate:translate(Lang, "Fill in fields to search "
|
|
|
|
"for any matching Jabber User")}]},
|
2003-01-05 21:24:59 +01:00
|
|
|
?TLFIELD("text-single", "User", "user"),
|
|
|
|
?TLFIELD("text-single", "Full Name", "fn"),
|
|
|
|
?TLFIELD("text-single", "Name", "given"),
|
|
|
|
?TLFIELD("text-single", "Middle Name", "middle"),
|
|
|
|
?TLFIELD("text-single", "Family Name", "family"),
|
|
|
|
?TLFIELD("text-single", "Nickname", "nickname"),
|
|
|
|
?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
|
|
|
]}]).
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do_route(From, To, Packet) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
#jid{user = User, resource = Resource} = To,
|
2003-01-02 22:01:12 +01:00
|
|
|
if
|
|
|
|
(User /= "") or (Resource /= "") ->
|
2003-05-09 21:32:59 +02:00
|
|
|
Err = jlib:make_error_reply(Packet, ?ERR_SERVICE_UNAVAILABLE),
|
2003-01-02 22:01:12 +01:00
|
|
|
ejabberd_router ! {route, To, From, Err};
|
|
|
|
true ->
|
|
|
|
IQ = jlib:iq_query_info(Packet),
|
|
|
|
case IQ of
|
|
|
|
{iq, ID, Type, ?NS_SEARCH, SubEl} ->
|
2003-01-06 20:57:05 +01:00
|
|
|
Lang = xml:get_tag_attr_s("xml:lang", SubEl),
|
2003-01-02 22:01:12 +01:00
|
|
|
case Type of
|
|
|
|
set ->
|
2003-01-03 20:36:59 +01:00
|
|
|
XDataEl = find_xdata_el(SubEl),
|
|
|
|
case XDataEl of
|
|
|
|
false ->
|
|
|
|
Err = jlib:make_error_reply(
|
2003-05-09 21:32:59 +02:00
|
|
|
Packet, ?ERR_BAD_REQUEST),
|
2003-01-03 20:36:59 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
_ ->
|
|
|
|
XData = jlib:parse_xdata_submit(XDataEl),
|
|
|
|
case XData of
|
|
|
|
invalid ->
|
|
|
|
Err = jlib:make_error_reply(
|
|
|
|
Packet,
|
2003-05-09 21:32:59 +02:00
|
|
|
?ERR_BAD_REQUEST),
|
2003-01-03 20:36:59 +01:00
|
|
|
ejabberd_router:route(To, From,
|
|
|
|
Err);
|
|
|
|
_ ->
|
|
|
|
ResIQ =
|
|
|
|
{iq, ID, result, ?NS_SEARCH,
|
|
|
|
[{xmlelement,
|
|
|
|
"query",
|
|
|
|
[{"xmlns", ?NS_SEARCH}],
|
|
|
|
[{xmlelement, "x",
|
|
|
|
[{"xmlns", ?NS_XDATA},
|
|
|
|
{"type", "result"}],
|
2003-01-06 20:57:05 +01:00
|
|
|
search_result(Lang, XData)
|
2003-01-03 20:36:59 +01:00
|
|
|
}]}]},
|
|
|
|
ejabberd_router:route(
|
|
|
|
To, From, jlib:iq_to_xml(ResIQ))
|
|
|
|
end
|
|
|
|
end;
|
2003-01-02 22:01:12 +01:00
|
|
|
get ->
|
|
|
|
ResIQ = {iq, ID, result, ?NS_SEARCH,
|
|
|
|
[{xmlelement,
|
|
|
|
"query",
|
|
|
|
[{"xmlns", ?NS_SEARCH}],
|
|
|
|
?FORM
|
|
|
|
}]},
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
|
|
|
{iq, ID, Type, ?NS_DISCO_INFO, SubEl} ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
Err = jlib:make_error_reply(
|
2003-05-09 21:32:59 +02:00
|
|
|
Packet, ?ERR_NOT_ALLOWED),
|
2003-01-02 22:01:12 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
get ->
|
|
|
|
ResIQ = {iq, ID, result, ?NS_DISCO_INFO,
|
|
|
|
[{xmlelement,
|
|
|
|
"query",
|
|
|
|
[{"xmlns", ?NS_DISCO_INFO}],
|
|
|
|
[{xmlelement, "identity",
|
|
|
|
[{"category", "directory"},
|
|
|
|
{"type", "user"},
|
2003-01-03 20:36:59 +01:00
|
|
|
{"name", "vCard User Search"}], []},
|
2003-01-02 22:01:12 +01:00
|
|
|
{xmlelement, "feature",
|
|
|
|
[{"var", ?NS_SEARCH}], []}
|
|
|
|
]
|
|
|
|
}]},
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
|
|
|
{iq, ID, Type, ?NS_DISCO_ITEMS, SubEl} ->
|
|
|
|
case Type of
|
|
|
|
set ->
|
|
|
|
Err = jlib:make_error_reply(
|
2003-05-09 21:32:59 +02:00
|
|
|
Packet, ?ERR_NOT_ALLOWED),
|
2003-01-02 22:01:12 +01:00
|
|
|
ejabberd_router:route(To, From, Err);
|
|
|
|
get ->
|
|
|
|
ResIQ = {iq, ID, result, ?NS_DISCO_INFO,
|
|
|
|
[{xmlelement,
|
|
|
|
"query",
|
|
|
|
[{"xmlns", ?NS_DISCO_INFO}], []}]},
|
|
|
|
ejabberd_router:route(To,
|
|
|
|
From,
|
|
|
|
jlib:iq_to_xml(ResIQ))
|
|
|
|
end;
|
|
|
|
_ ->
|
|
|
|
Err = jlib:make_error_reply(Packet,
|
2003-05-09 21:32:59 +02:00
|
|
|
?ERR_SERVICE_UNAVAILABLE),
|
2003-01-02 22:01:12 +01:00
|
|
|
ejabberd_router:route(To, From, Err)
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
2003-10-20 20:23:30 +02:00
|
|
|
find_xdata_el({xmlelement, _Name, _Attrs, SubEls}) ->
|
2003-01-03 20:36:59 +01:00
|
|
|
find_xdata_el1(SubEls).
|
|
|
|
|
|
|
|
find_xdata_el1([]) ->
|
|
|
|
false;
|
|
|
|
find_xdata_el1([{xmlelement, Name, Attrs, SubEls} | Els]) ->
|
|
|
|
case xml:get_attr_s("xmlns", Attrs) of
|
|
|
|
?NS_XDATA ->
|
|
|
|
{xmlelement, Name, Attrs, SubEls};
|
|
|
|
_ ->
|
|
|
|
find_xdata_el1(Els)
|
|
|
|
end;
|
|
|
|
find_xdata_el1([_ | Els]) ->
|
|
|
|
find_xdata_el1(Els).
|
|
|
|
|
2003-01-05 21:24:59 +01:00
|
|
|
-define(LFIELD(Label, Var),
|
2003-01-06 20:57:05 +01:00
|
|
|
{xmlelement, "field", [{"label", translate:translate(Lang, Label)},
|
|
|
|
{"var", Var}], []}).
|
2003-01-05 21:24:59 +01:00
|
|
|
|
2003-01-06 20:57:05 +01:00
|
|
|
search_result(Lang, Data) ->
|
|
|
|
[{xmlelement, "title", [],
|
|
|
|
[{xmlcdata, translate:translate(Lang, "Users Search Results")}]},
|
2003-01-03 20:36:59 +01:00
|
|
|
{xmlelement, "reported", [],
|
2003-01-05 21:24:59 +01:00
|
|
|
[?LFIELD("JID", "jid"),
|
|
|
|
?LFIELD("Full Name", "fn"),
|
|
|
|
?LFIELD("Name", "given"),
|
|
|
|
?LFIELD("Middle Name", "middle"),
|
|
|
|
?LFIELD("Family Name", "family"),
|
|
|
|
?LFIELD("Nickname", "nickname"),
|
|
|
|
?LFIELD("Birthday", "bday"),
|
|
|
|
?LFIELD("Country", "ctry"),
|
|
|
|
?LFIELD("City", "locality"),
|
|
|
|
?LFIELD("email", "email"),
|
|
|
|
?LFIELD("Organization Name", "orgname"),
|
|
|
|
?LFIELD("Organization Unit", "orgunit")
|
2003-01-03 20:36:59 +01:00
|
|
|
]}] ++ lists:map(fun record_to_item/1, search(Data)).
|
|
|
|
|
|
|
|
-define(FIELD(Var, Val),
|
|
|
|
{xmlelement, "field", [{"var", Var}],
|
|
|
|
[{xmlelement, "value", [],
|
|
|
|
[{xmlcdata, Val}]}]}).
|
|
|
|
|
|
|
|
record_to_item(R) ->
|
|
|
|
{xmlelement, "item", [],
|
|
|
|
[
|
|
|
|
?FIELD("jid", R#vcard_search.user ++ "@" ++ ?MYNAME),
|
|
|
|
?FIELD("fn", R#vcard_search.fn),
|
|
|
|
?FIELD("family", R#vcard_search.family),
|
|
|
|
?FIELD("given", R#vcard_search.given),
|
|
|
|
?FIELD("middle", R#vcard_search.middle),
|
|
|
|
?FIELD("nickname", 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
|
|
|
-ifdef(JUD_ALLOW_RETURN_ALL).
|
2003-01-03 20:36:59 +01:00
|
|
|
|
|
|
|
search(Data) ->
|
|
|
|
MatchSpec = make_matchspec(Data),
|
2003-01-15 20:54:44 +01:00
|
|
|
case catch mnesia:dirty_select(vcard_search, [{MatchSpec, [], ['$_']}]) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]),
|
|
|
|
[];
|
|
|
|
Rs ->
|
|
|
|
Rs
|
2003-01-03 20:36:59 +01:00
|
|
|
end.
|
|
|
|
|
2003-10-28 21:26:43 +01:00
|
|
|
-else.
|
|
|
|
|
|
|
|
search(Data) ->
|
|
|
|
MatchSpec = make_matchspec(Data),
|
|
|
|
if
|
|
|
|
MatchSpec == #vcard_search{_ = '_'} ->
|
|
|
|
[];
|
|
|
|
true ->
|
|
|
|
case catch mnesia:dirty_select(vcard_search,
|
|
|
|
[{MatchSpec, [], ['$_']}]) of
|
|
|
|
{'EXIT', Reason} ->
|
|
|
|
?ERROR_MSG("~p", [Reason]),
|
|
|
|
[];
|
|
|
|
Rs ->
|
|
|
|
Rs
|
|
|
|
end
|
|
|
|
end.
|
|
|
|
|
|
|
|
-endif.
|
|
|
|
|
|
|
|
|
2003-01-15 20:54:44 +01:00
|
|
|
% TODO: remove
|
|
|
|
% F = fun() ->
|
|
|
|
% mnesia:select(vcard_search, [{MatchSpec, [], ['$_']}])
|
|
|
|
% end,
|
|
|
|
% case mnesia:transaction(F) of
|
|
|
|
% {atomic, Rs} ->
|
|
|
|
% Rs;
|
|
|
|
% _ ->
|
|
|
|
% []
|
|
|
|
% end.
|
|
|
|
|
2003-01-03 20:36:59 +01:00
|
|
|
|
|
|
|
make_matchspec(Data) ->
|
2003-01-08 21:42:03 +01:00
|
|
|
GlobMatch = #vcard_search{_ = '_'},
|
2003-01-03 20:36:59 +01:00
|
|
|
Match = filter_fields(Data, GlobMatch),
|
|
|
|
Match.
|
|
|
|
|
|
|
|
filter_fields([], Match) ->
|
|
|
|
Match;
|
|
|
|
filter_fields([{SVar, [Val]} | Ds], Match)
|
|
|
|
when is_list(Val) and (Val /= "") ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LVal = stringprep:tolower(Val),
|
2003-01-03 20:36:59 +01:00
|
|
|
NewMatch = case SVar of
|
2003-01-09 20:59:16 +01:00
|
|
|
"user" -> Match#vcard_search{luser = LVal};
|
|
|
|
"fn" -> Match#vcard_search{lfn = LVal};
|
|
|
|
"family" -> Match#vcard_search{lfamily = LVal};
|
|
|
|
"given" -> Match#vcard_search{lgiven = LVal};
|
|
|
|
"middle" -> Match#vcard_search{lmiddle = LVal};
|
|
|
|
"nickname" -> Match#vcard_search{lnickname = LVal};
|
|
|
|
"bday" -> Match#vcard_search{lbday = LVal};
|
|
|
|
"ctry" -> Match#vcard_search{lctry = LVal};
|
|
|
|
"locality" -> Match#vcard_search{llocality = LVal};
|
|
|
|
"email" -> Match#vcard_search{lemail = LVal};
|
|
|
|
"orgname" -> Match#vcard_search{lorgname = LVal};
|
|
|
|
"orgunit" -> Match#vcard_search{lorgunit = LVal};
|
2003-01-03 20:36:59 +01:00
|
|
|
_ -> Match
|
|
|
|
end,
|
|
|
|
filter_fields(Ds, NewMatch);
|
|
|
|
filter_fields([_ | Ds], Match) ->
|
|
|
|
filter_fields(Ds, Match).
|
2003-01-09 20:59:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
|
|
|
|
|
|
|
set_vcard_t(R, _) ->
|
|
|
|
User = R#vcard.user,
|
|
|
|
VCARD = R#vcard.vcard,
|
|
|
|
|
|
|
|
FN = xml:get_path_s(VCARD, [{elem, "FN"}, cdata]),
|
|
|
|
Family = xml:get_path_s(VCARD, [{elem, "N"}, {elem, "FAMILY"}, cdata]),
|
|
|
|
Given = xml:get_path_s(VCARD, [{elem, "N"}, {elem, "GIVEN"}, cdata]),
|
|
|
|
Middle = xml:get_path_s(VCARD, [{elem, "N"}, {elem, "MIDDLE"}, cdata]),
|
|
|
|
Nickname = xml:get_path_s(VCARD, [{elem, "NICKNAME"}, cdata]),
|
|
|
|
BDay = xml:get_path_s(VCARD, [{elem, "BDAY"}, cdata]),
|
|
|
|
CTRY = xml:get_path_s(VCARD, [{elem, "ADR"}, {elem, "CTRY"}, cdata]),
|
|
|
|
Locality = xml:get_path_s(VCARD, [{elem, "ADR"}, {elem, "LOCALITY"},cdata]),
|
|
|
|
EMail = xml:get_path_s(VCARD, [{elem, "EMAIL"}, cdata]),
|
|
|
|
OrgName = xml:get_path_s(VCARD, [{elem, "ORG"}, {elem, "ORGNAME"}, cdata]),
|
|
|
|
OrgUnit = xml:get_path_s(VCARD, [{elem, "ORG"}, {elem, "ORGUNIT"}, cdata]),
|
|
|
|
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
|
|
|
LFN = stringprep:tolower(FN),
|
|
|
|
LFamily = stringprep:tolower(Family),
|
|
|
|
LGiven = stringprep:tolower(Given),
|
|
|
|
LMiddle = stringprep:tolower(Middle),
|
|
|
|
LNickname = stringprep:tolower(Nickname),
|
|
|
|
LBDay = stringprep:tolower(BDay),
|
|
|
|
LCTRY = stringprep:tolower(CTRY),
|
|
|
|
LLocality = stringprep:tolower(Locality),
|
|
|
|
LEMail = stringprep:tolower(EMail),
|
|
|
|
LOrgName = stringprep:tolower(OrgName),
|
|
|
|
LOrgUnit = stringprep:tolower(OrgUnit),
|
2003-01-09 20:59:16 +01:00
|
|
|
|
2003-10-20 20:23:30 +02:00
|
|
|
if
|
|
|
|
(LUser == error) or
|
|
|
|
(LFN == error) or
|
|
|
|
(LFamily == error) or
|
|
|
|
(LGiven == error) or
|
|
|
|
(LMiddle == error) or
|
|
|
|
(LNickname == error) or
|
|
|
|
(LBDay == error) or
|
|
|
|
(LCTRY == error) or
|
|
|
|
(LLocality == error) or
|
|
|
|
(LEMail == error) or
|
|
|
|
(LOrgName == error) or
|
|
|
|
(LOrgUnit == error) ->
|
|
|
|
{error, badarg};
|
|
|
|
true ->
|
|
|
|
mnesia:write(
|
|
|
|
#vcard_search{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
|
|
|
|
})
|
|
|
|
end.
|
2003-01-09 20:59:16 +01:00
|
|
|
|
|
|
|
|
|
|
|
reindex_vcards() ->
|
|
|
|
F = fun() ->
|
|
|
|
mnesia:foldl(fun set_vcard_t/2, [], vcard)
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|
|
|
|
|
2003-01-26 21:16:53 +01:00
|
|
|
remove_user(User) ->
|
2003-10-07 22:31:44 +02:00
|
|
|
LUser = jlib:nodeprep(User),
|
2003-01-26 21:16:53 +01:00
|
|
|
F = fun() ->
|
|
|
|
mnesia:delete({vcard, LUser}),
|
|
|
|
lists:foreach(fun(R) ->
|
|
|
|
mnesia:delete_object(R)
|
|
|
|
end,
|
|
|
|
mnesia:index_read(vcard_search,
|
|
|
|
LUser,
|
|
|
|
#vcard_search.luser))
|
|
|
|
end,
|
|
|
|
mnesia:transaction(F).
|
|
|
|
|