25
1
mirror of https://github.com/processone/ejabberd.git synced 2024-11-22 16:20:52 +01:00

Convert to exmpp.

SVN Revision: 1568
This commit is contained in:
Jean-Sébastien Pédron 2008-09-22 11:18:08 +00:00
parent 35a7342416
commit 5803c51633
3 changed files with 474 additions and 518 deletions

View File

@ -6,6 +6,8 @@
atom_to_list/1.
(filter_fields): A call to the stringprep module was left.
* src/mod_vcard_ldap.erl, src/mod_vcard_odbc.erl: Convert to exmpp.
2008-09-18 Jean-Sébastien Pédron <js.pedron@meetic-corp.com>
* src/mod_roster_odbc.erl: Convert to exmpp.

View File

@ -49,9 +49,10 @@
route/4
]).
-include_lib("exmpp/include/exmpp.hrl").
-include("ejabberd.hrl").
-include("eldap/eldap.hrl").
-include("jlib.hrl").
-define(PROCNAME, ejabberd_mod_vcard_ldap).
@ -193,7 +194,8 @@ handle_info({route, From, To, Packet}, State) ->
Pid when is_pid(Pid) ->
ok;
_ ->
Err = jlib:make_error_reply(Packet, ?ERR_INTERNAL_SERVER_ERROR),
Err = exmpp_stanza:reply_with_error(Packet,
'internal-server-error'),
ejabberd_router:route(To, From, Err)
end,
{noreply, State};
@ -208,53 +210,45 @@ get_sm_features(Acc, _From, _To, Node, _Lang) ->
[] ->
case Acc of
{result, Features} ->
{result, [?NS_VCARD | Features]};
{result, [?NS_VCARD_s | Features]};
empty ->
{result, [?NS_VCARD]}
{result, [?NS_VCARD_s]}
end;
_ ->
Acc
end.
process_local_iq(_From, _To, #iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
case Type of
set ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
get ->
IQ#iq{type = result,
sub_el = [{xmlelement, "vCard",
[{"xmlns", ?NS_VCARD}],
[{xmlelement, "FN", [],
[{xmlcdata, "ejabberd"}]},
{xmlelement, "URL", [],
[{xmlcdata, ?EJABBERD_URI}]},
{xmlelement, "DESC", [],
[{xmlcdata,
translate:translate(
Lang,
"Erlang Jabber Server") ++
"\nCopyright (c) 2002-2008 ProcessOne"}]},
{xmlelement, "BDAY", [],
[{xmlcdata, "2002-11-16"}]}
]}]}
end.
process_local_iq(_From, _To, #iq{type = get, lang = Lang} = IQ_Rec) ->
Result = #xmlel{ns = ?NS_VCARD, name = 'vCard', children = [
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'FN'},
"ejabberd"),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'URL'},
?EJABBERD_URI),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'DESC'},
translate:translate(Lang, "Erlang Jabber Server") ++
"\nCopyright (c) 2002-2008 ProcessOne"),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'BDAY'},
"2002-11-16")
]},
exmpp_iq:result(IQ_Rec, Result);
process_local_iq(_From, _To, #iq{type = set} = IQ_Rec) ->
exmpp_iq:error(IQ_Rec, 'not-allowed').
process_sm_iq(_From, #jid{lserver=LServer} = To, #iq{sub_el = SubEl} = IQ) ->
case catch process_vcard_ldap(To, IQ, LServer) of
process_sm_iq(_From, #jid{ldomain=LServer} = To, #iq{} = IQ_Rec) ->
case catch process_vcard_ldap(To, IQ_Rec, LServer) of
{'EXIT', _} ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]};
exmpp_iq:error(IQ_Rec, 'internal-server-error');
Other ->
Other
end.
process_vcard_ldap(To, IQ, Server) ->
process_vcard_ldap(To, IQ_Rec, Server) ->
{ok, State} = eldap_utils:get_state(Server, ?PROCNAME),
#iq{type = Type, sub_el = SubEl} = IQ,
case Type of
case IQ_Rec#iq.type of
set ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
exmpp_iq:error(IQ_Rec, 'not-allowed');
get ->
#jid{luser = LUser} = To,
#jid{lnode = LUser} = To,
LServer = State#state.serverhost,
case ejabberd_auth:is_user_exists(LUser, LServer) of
true ->
@ -262,12 +256,12 @@ process_vcard_ldap(To, IQ, Server) ->
case find_ldap_user(LUser, State) of
#eldap_entry{attributes = Attributes} ->
Vcard = ldap_attributes_to_vcard(Attributes, VCardMap, {LUser, LServer}),
IQ#iq{type = result, sub_el = Vcard};
exmpp_iq:result(IQ_Rec, Vcard);
_ ->
IQ#iq{type = result, sub_el = []}
exmpp_iq:result(IQ_Rec)
end;
_ ->
IQ#iq{type = result, sub_el = []}
exmpp_iq:result(IQ_Rec)
end
end.
@ -300,247 +294,237 @@ find_ldap_user(User, State) ->
ldap_attributes_to_vcard(Attributes, VCardMap, UD) ->
Attrs = lists:map(
fun({VCardName, _, _}) ->
{stringprep:tolower(VCardName),
{exmpp_stringprep:to_lower(VCardName),
map_vcard_attr(VCardName, Attributes, VCardMap, UD)}
end, VCardMap),
Elts = [ldap_attribute_to_vcard(vCard, Attr) || Attr <- Attrs],
NElts = [ldap_attribute_to_vcard(vCardN, Attr) || Attr <- Attrs],
OElts = [ldap_attribute_to_vcard(vCardO, Attr) || Attr <- Attrs],
AElts = [ldap_attribute_to_vcard(vCardA, Attr) || Attr <- Attrs],
[{xmlelement, "vCard", [{"xmlns", ?NS_VCARD}],
[#xmlel{ns = ?NS_VCARD, name = 'vCard', children =
lists:append([X || X <- Elts, X /= none],
[{xmlelement,"N",[], [X || X <- NElts, X /= none]},
{xmlelement,"ORG",[], [X || X <- OElts, X /= none]},
{xmlelement,"ADR",[], [X || X <- AElts, X /= none]}])
[#xmlel{ns = ?NS_VCARD, name = 'N', children = [X || X <- NElts, X /= none]},
#xmlel{ns = ?NS_VCARD, name = 'ORG', children = [X || X <- OElts, X /= none]},
#xmlel{ns = ?NS_VCARD, name = 'ADR', children = [X || X <- AElts, X /= none]}])
}].
ldap_attribute_to_vcard(vCard, {"fn", Value}) ->
{xmlelement,"FN",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'FN'}, Value);
ldap_attribute_to_vcard(vCard, {"nickname", Value}) ->
{xmlelement,"NICKNAME",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'NICKNAME'}, Value);
ldap_attribute_to_vcard(vCard, {"title", Value}) ->
{xmlelement,"TITLE",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'TITLE'}, Value);
ldap_attribute_to_vcard(vCard, {"bday", Value}) ->
{xmlelement,"BDAY",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'BDAY'}, Value);
ldap_attribute_to_vcard(vCard, {"url", Value}) ->
{xmlelement,"URL",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'URL'}, Value);
ldap_attribute_to_vcard(vCard, {"desc", Value}) ->
{xmlelement,"DESC",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'DESC'}, Value);
ldap_attribute_to_vcard(vCard, {"role", Value}) ->
{xmlelement,"ROLE",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'ROLE'}, Value);
ldap_attribute_to_vcard(vCard, {"tel", Value}) ->
{xmlelement,"TEL",[],[{xmlelement,"VOICE",[],[]},
{xmlelement,"WORK",[],[]},
{xmlelement,"NUMBER",[],[{xmlcdata,Value}]}]};
#xmlel{ns = ?NS_VCARD, name = 'TEL', children = [
#xmlel{ns = ?NS_VCARD, name = 'VOICE'},
#xmlel{ns = ?NS_VCARD, name = 'WORK'},
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'NUMBER'}, Value)]};
ldap_attribute_to_vcard(vCard, {"email", Value}) ->
{xmlelement,"EMAIL",[],[{xmlelement,"INTERNET",[],[]},
{xmlelement,"PREF",[],[]},
{xmlelement,"USERID",[],[{xmlcdata,Value}]}]};
#xmlel{ns = ?NS_VCARD, name = 'EMAIL', children = [
#xmlel{ns = ?NS_VCARD, name = 'INTERNET'},
#xmlel{ns = ?NS_VCARD, name = 'PREF'},
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'USERID'}, Value)]};
ldap_attribute_to_vcard(vCard, {"photo", Value}) ->
{xmlelement,"PHOTO",[],[
{xmlelement,"BINVAL",[],[{xmlcdata, jlib:encode_base64(Value)}]}]};
#xmlel{ns = ?NS_VCARD, name = 'PHOTO', children = [
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'BINVAL'},
jlib:encode_base64(Value))]};
ldap_attribute_to_vcard(vCardN, {"family", Value}) ->
{xmlelement,"FAMILY",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'FAMILY'}, Value);
ldap_attribute_to_vcard(vCardN, {"given", Value}) ->
{xmlelement,"GIVEN",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'GIVEN'}, Value);
ldap_attribute_to_vcard(vCardN, {"middle", Value}) ->
{xmlelement,"MIDDLE",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'MIDDLE'}, Value);
ldap_attribute_to_vcard(vCardO, {"orgname", Value}) ->
{xmlelement,"ORGNAME",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'ORGNAME'}, Value);
ldap_attribute_to_vcard(vCardO, {"orgunit", Value}) ->
{xmlelement,"ORGUNIT",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'ORGUNIT'}, Value);
ldap_attribute_to_vcard(vCardA, {"locality", Value}) ->
{xmlelement,"LOCALITY",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'LOCALITY'}, Value);
ldap_attribute_to_vcard(vCardA, {"street", Value}) ->
{xmlelement,"STREET",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'STREET'}, Value);
ldap_attribute_to_vcard(vCardA, {"ctry", Value}) ->
{xmlelement,"CTRY",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'CTRY'}, Value);
ldap_attribute_to_vcard(vCardA, {"region", Value}) ->
{xmlelement,"REGION",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'REGION'}, Value);
ldap_attribute_to_vcard(vCardA, {"pcode", Value}) ->
{xmlelement,"PCODE",[],[{xmlcdata,Value}]};
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'PCODE'}, Value);
ldap_attribute_to_vcard(_, _) ->
none.
-define(TLFIELD(Type, Label, Var),
{xmlelement, "field", [{"type", Type},
{"label", translate:translate(Lang, Label)},
{"var", Var}], []}).
#xmlel{ns = ?NS_VCARD, name = 'field', attrs = [
#xmlattr{name = 'type', value = Type},
#xmlattr{name = 'label', value = translate:translate(Lang, Label)},
#xmlattr{name = 'var', value = Var}]}).
-define(FORM(JID, SearchFields),
[{xmlelement, "instructions", [],
[{xmlcdata, translate:translate(Lang, "You need an x:data capable client to search")}]},
{xmlelement, "x", [{"xmlns", ?NS_XDATA}, {"type", "form"}],
[{xmlelement, "title", [],
[{xmlcdata, translate:translate(Lang, "Search users in ") ++
jlib:jid_to_string(JID)}]},
{xmlelement, "instructions", [],
[{xmlcdata, translate:translate(Lang, "Fill in fields to search "
"for any matching Jabber User")}]}
[#xmlel{ns = ?NS_SEARCH, name = 'instructions', children =
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang, "You need an x:data capable client to search"))}]},
#xmlel{ns = ?NS_DATA_FORMS, name = 'x', attrs =
[#xmlattr{name = 'type', value = "form"}], children =
[#xmlel{ns = ?NS_DATA_FORMS, name = 'title', children =
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang, "Search users in ") ++
exmpp_jid:jid_to_list(JID))}]},
#xmlel{ns = ?NS_SEARCH, name = 'instructions', children =
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang, "Fill in fields to search "
"for any matching Jabber User"))}]}
] ++ lists:map(fun({X,Y}) -> ?TLFIELD("text-single", X, Y) end, SearchFields)}]).
do_route(State, From, To, Packet) ->
spawn(?MODULE, route, [State, From, To, Packet]).
route(State, From, To, Packet) ->
#jid{user = User, resource = Resource} = To,
#jid{node = User, resource = Resource} = To,
if
(User /= "") or (Resource /= "") ->
Err = jlib:make_error_reply(Packet, ?ERR_SERVICE_UNAVAILABLE),
(User /= undefined) or (Resource /= undefined) ->
Err = exmpp_stanza:reply_with_error(Packet, '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),
try
Request = exmpp_iq:get_request(Packet),
Type = exmpp_iq:get_type(Packet),
Lang = exmpp_stanza:get_lang(Packet),
case {Type, Request#xmlel.ns} of
{set, ?NS_SEARCH} ->
XDataEl = find_xdata_el(Request),
case XDataEl of
false ->
Err = jlib:make_error_reply(
Packet, ?ERR_BAD_REQUEST),
Err = exmpp_iq:error(Packet, 'bad-request'),
ejabberd_router:route(To, From, Err);
_ ->
XData = jlib:parse_xdata_submit(XDataEl),
case XData of
invalid ->
Err = jlib:make_error_reply(
Packet,
?ERR_BAD_REQUEST),
Err = exmpp_iq:error(Packet,
'bad-request'),
ejabberd_router:route(To, From,
Err);
_ ->
ResIQ =
IQ#iq{
type = result,
sub_el =
[{xmlelement,
"query",
[{"xmlns", ?NS_SEARCH}],
[{xmlelement, "x",
[{"xmlns", ?NS_XDATA},
{"type", "result"}],
search_result(Lang, To, State, XData)
}]}]},
Result = #xmlel{
ns = ?NS_SEARCH,
name = 'query',
children = [
#xmlel{
ns = ?NS_DATA_FORMS,
name = 'x',
attrs = [#xmlattr{name = 'type',
value = "result"}],
children = search_result(Lang, To, State, XData)}]},
ResIQ = exmpp_iq:result(Packet,
Result),
ejabberd_router:route(
To, From, jlib:iq_to_xml(ResIQ))
To, From, ResIQ)
end
end;
get ->
{get, ?NS_SEARCH} ->
SearchFields = State#state.search_fields,
ResIQ = IQ#iq{type = result,
sub_el = [{xmlelement,
"query",
[{"xmlns", ?NS_SEARCH}],
?FORM(To, SearchFields)
}]},
Result = #xmlel{ns = ?NS_SEARCH, name = 'query',
children = ?FORM(To, SearchFields)},
ResIQ = exmpp_iq:result(Packet, Result),
ejabberd_router:route(To,
From,
jlib:iq_to_xml(ResIQ))
end;
#iq{type = Type, xmlns = ?NS_DISCO_INFO, lang = Lang} ->
case Type of
set ->
Err = jlib:make_error_reply(
Packet, ?ERR_NOT_ALLOWED),
ResIQ);
{set, ?NS_DISCO_INFO} ->
Err = exmpp_iq:error(Packet, 'not-allowed'),
ejabberd_router:route(To, From, Err);
get ->
ResIQ =
IQ#iq{type = result,
sub_el = [{xmlelement,
"query",
[{"xmlns", ?NS_DISCO_INFO}],
[{xmlelement, "identity",
[{"category", "directory"},
{"type", "user"},
{"name",
translate:translate(Lang, "vCard User Search")}],
[]},
{xmlelement, "feature",
[{"var", ?NS_SEARCH}], []},
{xmlelement, "feature",
[{"var", ?NS_VCARD}], []}
]
}]},
{get, ?NS_DISCO_INFO} ->
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
children = [
#xmlel{ns = ?NS_DISCO_INFO, name = 'identity',
attrs = [
#xmlattr{name = 'category',
value = "directory"},
#xmlattr{name = 'type',
value = "user"},
#xmlattr{name = 'name',
value = translate:translate(Lang,
"vCard User Search")}]},
#xmlel{ns = ?NS_DISCO_INFO, name = 'feature',
attrs = [
#xmlattr{name = 'var',
value = ?NS_SEARCH_s}]},
#xmlel{ns = ?NS_DISCO_INFO, name = 'feature',
attrs = [
#xmlattr{name = 'var',
value = ?NS_VCARD_s}]}
]},
ResIQ = exmpp_iq:result(Packet, Result),
ejabberd_router:route(To,
From,
jlib:iq_to_xml(ResIQ))
end;
#iq{type = Type, xmlns = ?NS_DISCO_ITEMS} ->
case Type of
set ->
Err = jlib:make_error_reply(
Packet, ?ERR_NOT_ALLOWED),
ResIQ);
{set, ?NS_DISCO_ITEMS} ->
Err = exmpp_iq:error(Packet, 'not-allowed'),
ejabberd_router:route(To, From, Err);
get ->
ResIQ =
IQ#iq{type = result,
sub_el = [{xmlelement,
"query",
[{"xmlns", ?NS_DISCO_ITEMS}],
[]}]},
{get, ?NS_DISCO_ITEMS} ->
Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query'},
ResIQ = exmpp_iq:result(Packet, Result),
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 = [{xmlelement,
"vCard",
[{"xmlns", ?NS_VCARD}],
iq_get_vcard(Lang)}]},
ResIQ);
{get, ?NS_VCARD} ->
Result = #xmlel{ns = ?NS_VCARD, name = 'vCard',
children = iq_get_vcard(Lang)},
ResIQ = exmpp_iq:result(Packet, Result),
ejabberd_router:route(To,
From,
jlib:iq_to_xml(ResIQ));
ResIQ);
_ ->
Err = jlib:make_error_reply(Packet,
?ERR_SERVICE_UNAVAILABLE),
Err = exmpp_iq:error(Packet, 'service-unavailable'),
ejabberd_router:route(To, From, Err)
end
catch
_ ->
Err1 = exmpp_iq:error(Packet, 'service-unavailable'),
ejabberd_router:route(To, From, Err1)
end
end.
iq_get_vcard(Lang) ->
[{xmlelement, "FN", [],
[{xmlcdata, "ejabberd/mod_vcard"}]},
{xmlelement, "URL", [],
[{xmlcdata, ?EJABBERD_URI}]},
{xmlelement, "DESC", [],
[{xmlcdata, translate:translate(
Lang,
"ejabberd vCard module") ++
"\nCopyright (c) 2003-2008 ProcessOne"}]}].
-define(LFIELD(Label, Var),
{xmlelement, "field", [{"label", translate:translate(Lang, Label)},
{"var", Var}], []}).
[
#xmlel{ns = ?NS_SEARCH, name = 'FN', children = [
#xmlcdata{cdata = <<"ejabberd/mod_vcard">>}]},
#xmlel{ns = ?NS_SEARCH, name = 'URL', children = [
#xmlcdata{cdata = list_to_binary(?EJABBERD_URI)}]},
#xmlel{ns = ?NS_SEARCH, name ='DESC', children = [
#xmlcdata{cdata = list_to_binary(
translate:translate(Lang, "ejabberd vCard module") ++
"\nCopyright (c) 2003-2008 ProcessOne")}]}
].
search_result(Lang, JID, State, Data) ->
SearchReported = State#state.search_reported,
Header = [{xmlelement, "title", [],
[{xmlcdata, translate:translate(Lang, "Search Results for ") ++
jlib:jid_to_string(JID)}]},
{xmlelement, "reported", [],
Header = [#xmlel{ns = ?NS_DATA_FORMS, name = 'title', children =
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang, "Search Results for ") ++
exmpp_jid:jid_to_list(JID))}]},
#xmlel{ns = ?NS_DATA_FORMS, name = 'reported', children =
[?TLFIELD("text-single", "Jabber ID", "jid")] ++
lists:map(
fun({Name, Value}) -> ?TLFIELD("text-single", Name, Value) end,
@ -554,9 +538,10 @@ search_result(Lang, JID, State, Data) ->
end.
-define(FIELD(Var, Val),
{xmlelement, "field", [{"var", Var}],
[{xmlelement, "value", [],
[{xmlcdata, Val}]}]}).
#xmlel{ns = ?NS_DATA_FORMS, name = 'field', attrs =
[#xmlattr{name = 'var', value = Var}], children =
[#xmlel{ns = ?NS_DATA_FORMS, name = 'value', children =
[#xmlcdata{cdata = list_to_binary(Val)}]}]}).
search(State, Data) ->
Base = State#state.base,
@ -605,7 +590,7 @@ search_items(Entries, State) ->
end, SearchReported),
Result = [?FIELD("jid", Username ++ "@" ++ LServer)] ++
[?FIELD(Name, Value) || {Name, Value} <- RFields],
[{xmlelement, "item", [], Result}];
[#xmlel{ns = ?NS_DATA_FORMS, name = 'item', children = Result}];
_ ->
[]
end;
@ -645,13 +630,8 @@ find_xdata_el({xmlelement, _Name, _Attrs, 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([#xmlel{ns = ?NS_DATA_FORMS} = El | _Els]) ->
El;
find_xdata_el1([_ | Els]) ->
find_xdata_el1(Els).

View File

@ -36,8 +36,9 @@
%reindex_vcards/0,
remove_user/2]).
-include_lib("exmpp/include/exmpp.hrl").
-include("ejabberd.hrl").
-include("jlib.hrl").
-define(JUD_MATCHES, 30).
@ -102,85 +103,85 @@ get_sm_features(Acc, _From, _To, Node, _Lang) ->
[] ->
case Acc of
{result, Features} ->
{result, [?NS_VCARD | Features]};
{result, [?NS_VCARD_s | Features]};
empty ->
{result, [?NS_VCARD]}
{result, [?NS_VCARD_s]}
end;
_ ->
Acc
end.
process_local_iq(_From, _To, #iq{type = Type, lang = Lang, sub_el = SubEl} = IQ) ->
case Type of
set ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]};
get ->
IQ#iq{type = result,
sub_el = [{xmlelement, "vCard",
[{"xmlns", ?NS_VCARD}],
[{xmlelement, "FN", [],
[{xmlcdata, "ejabberd"}]},
{xmlelement, "URL", [],
[{xmlcdata, ?EJABBERD_URI}]},
{xmlelement, "DESC", [],
[{xmlcdata,
translate:translate(
Lang,
"Erlang Jabber Server") ++
"\nCopyright (c) 2002-2008 ProcessOne"}]},
{xmlelement, "BDAY", [],
[{xmlcdata, "2002-11-16"}]}
]}]}
end.
process_local_iq(_From, _To, #iq{type = get, lang = Lang} = IQ_Rec) ->
Result = #xmlel{ns = ?NS_VCARD, name = 'vCard', children = [
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'FN'},
"ejabberd"),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'URL'},
?EJABBERD_URI),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'DESC'},
translate:translate(Lang, "Erlang Jabber Server") ++
"\nCopyright (c) 2002-2008 ProcessOne"),
exmpp_xml:set_cdata(#xmlel{ns = ?NS_VCARD, name = 'BDAY'},
"2002-11-16")
]},
exmpp_iq:result(IQ_Rec, Result);
process_local_iq(_From, _To, #iq{type = set} = IQ_Rec) ->
exmpp_iq:error(IQ_Rec, 'not-allowed').
process_sm_iq(From, To, #iq{type = Type, sub_el = SubEl} = IQ) ->
case Type of
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 ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]}
end;
get ->
#jid{luser = LUser, lserver = LServer} = To,
process_sm_iq(_From, To, #iq{type = get} = IQ_Rec) ->
#jid{lnode = LUser, ldomain = LServer} = To,
Username = ejabberd_odbc:escape(LUser),
case catch ejabberd_odbc:sql_query(
LServer,
["select vcard from vcard "
"where username='", Username, "';"]) of
{selected, ["vcard"], [{SVCARD}]} ->
case xml_stream:parse_element(SVCARD) of
case exmpp_xml:parse_document(SVCARD) of
{error, _Reason} ->
IQ#iq{type = error,
sub_el = [SubEl, ?ERR_SERVICE_UNAVAILABLE]};
exmpp_iq:error(IQ_Rec, 'service-unavailable');
VCARD ->
IQ#iq{type = result, sub_el = [VCARD]}
exmpp_iq:result(IQ_Rec, VCARD)
end;
{selected, ["vcard"], []} ->
IQ#iq{type = result, sub_el = []};
exmpp_iq:result(IQ_Rec);
_ ->
IQ#iq{type = error,
sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]}
end
exmpp_iq:error(IQ_Rec, 'internal-server-error')
end;
process_sm_iq(From, _To, #iq{type = set, payload = Request} = IQ_Rec) ->
#jid{node = User, ldomain = LServer} = From,
case lists:member(LServer, ?MYHOSTS) of
true ->
set_vcard(User, LServer, Request),
exmpp_iq:result(IQ_Rec);
false ->
exmpp_iq:error(IQ_Rec, 'not-allowed')
end.
set_vcard(User, LServer, 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]),
EMail1 = xml:get_path_s(VCARD, [{elem, "EMAIL"}, {elem, "USERID"},cdata]),
EMail2 = 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]),
FN = exmpp_xml:get_path(VCARD,
[{element, 'FN'}, cdata_as_list]),
Family = exmpp_xml:get_path(VCARD,
[{element, 'N'}, {element, 'FAMILY'}, cdata_as_list]),
Given = exmpp_xml:get_path(VCARD,
[{element, 'N'}, {element, 'GIVEN'}, cdata_as_list]),
Middle = exmpp_xml:get_path(VCARD,
[{element, 'N'}, {element, 'MIDDLE'}, cdata_as_list]),
Nickname = exmpp_xml:get_path(VCARD,
[{element, 'NICKNAME'}, cdata_as_list]),
BDay = exmpp_xml:get_path(VCARD,
[{element, 'BDAY'}, cdata_as_list]),
CTRY = exmpp_xml:get_path(VCARD,
[{element, 'ADR'}, {element, 'CTRY'}, cdata_as_list]),
Locality = exmpp_xml:get_path(VCARD,
[{element, 'ADR'}, {element, 'LOCALITY'}, cdata_as_list]),
EMail1 = exmpp_xml:get_path(VCARD,
[{element, 'EMAIL'}, {element, 'USERID'}, cdata_as_list]),
EMail2 = exmpp_xml:get_path(VCARD,
[{element, 'EMAIL'}, cdata_as_list]),
OrgName = exmpp_xml:get_path(VCARD,
[{element, 'ORG'}, {element, 'ORGNAME'}, cdata_as_list]),
OrgUnit = exmpp_xml:get_path(VCARD,
[{element, 'ORG'}, {element, 'ORGUNIT'}, cdata_as_list]),
EMail = case EMail1 of
"" ->
EMail2;
@ -188,38 +189,23 @@ set_vcard(User, LServer, VCARD) ->
EMail1
end,
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),
try
LUser = exmpp_stringprep:nodeprep(User),
LFN = exmpp_stringprep:to_lower(FN),
LFamily = exmpp_stringprep:to_lower(Family),
LGiven = exmpp_stringprep:to_lower(Given),
LMiddle = exmpp_stringprep:to_lower(Middle),
LNickname = exmpp_stringprep:to_lower(Nickname),
LBDay = exmpp_stringprep:to_lower(BDay),
LCTRY = exmpp_stringprep:to_lower(CTRY),
LLocality = exmpp_stringprep:to_lower(Locality),
LEMail = exmpp_stringprep:to_lower(EMail),
LOrgName = exmpp_stringprep:to_lower(OrgName),
LOrgUnit = exmpp_stringprep:to_lower(OrgUnit),
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 ->
Username = ejabberd_odbc:escape(User),
LUsername = ejabberd_odbc:escape(LUser),
SVCARD = ejabberd_odbc:escape(
lists:flatten(xml:element_to_string(VCARD))),
SVCARD = ejabberd_odbc:escape(exmpp_xml:document_to_list(VCARD)),
SFN = ejabberd_odbc:escape(FN),
SLFN = ejabberd_odbc:escape(LFN),
@ -268,26 +254,31 @@ set_vcard(User, LServer, VCARD) ->
" '", SEMail, "', '", SLEMail, "',"
" '", SOrgName, "', '", SLOrgName, "',"
" '", SOrgUnit, "', '", SLOrgUnit, "');"]])
catch
_ ->
{error, badarg}
end.
-define(TLFIELD(Type, Label, Var),
{xmlelement, "field", [{"type", Type},
{"label", translate:translate(Lang, Label)},
{"var", Var}], []}).
#xmlel{ns = ?NS_VCARD, name = 'field', attrs = [
#xmlattr{name = 'type', value = Type},
#xmlattr{name = 'label', value = translate:translate(Lang, Label)},
#xmlattr{name = 'var', value = Var}]}).
-define(FORM(JID),
[{xmlelement, "instructions", [],
[{xmlcdata, translate:translate(Lang, "You need an x:data capable client to search")}]},
{xmlelement, "x", [{"xmlns", ?NS_XDATA}, {"type", "form"}],
[{xmlelement, "title", [],
[{xmlcdata, translate:translate(Lang, "Search users in ") ++
jlib:jid_to_string(JID)}]},
{xmlelement, "instructions", [],
[{xmlcdata, translate:translate(Lang, "Fill in the form to search "
[#xmlel{ns = ?NS_SEARCH, name = 'instructions', children =
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang, "You need an x:data capable client to search"))}]},
#xmlel{ns = ?NS_DATA_FORMS, name = 'x', attrs =
[#xmlattr{name = 'type', value = "form"}], children =
[#xmlel{ns = ?NS_DATA_FORMS, name = 'title', children =
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang, "Search users in ") ++ exmpp_jid:jid_to_list(JID))}]},
#xmlel{ns = ?NS_SEARCH, name = 'instructions', children =
[#xmlcdata{cdata = list_to_binary(translate:translate(Lang,
"Fill in the form to search "
"for any matching Jabber User "
"(Add * to the end of field to "
"match substring)")}]},
"match substring)"))}]},
?TLFIELD("text-single", "User", "user"),
?TLFIELD("text-single", "Full Name", "fn"),
?TLFIELD("text-single", "Name", "first"),
@ -303,157 +294,139 @@ set_vcard(User, LServer, VCARD) ->
]}]).
do_route(ServerHost, From, To, Packet) ->
#jid{user = User, resource = Resource} = To,
#jid{node = User, resource = Resource} = To,
if
(User /= "") or (Resource /= "") ->
Err = jlib:make_error_reply(Packet, ?ERR_SERVICE_UNAVAILABLE),
(User /= undefined) or (Resource /= undefined) ->
Err = exmpp_stanza:reply_with_error(Packet, '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),
try
Request = exmpp_iq:get_request(Packet),
Type = exmpp_iq:get_type(Packet),
Lang = exmpp_stanza:get_lang(Packet),
case {Type, Request#xmlel.ns} of
{set, ?NS_SEARCH} ->
XDataEl = find_xdata_el(Request),
case XDataEl of
false ->
Err = jlib:make_error_reply(
Packet, ?ERR_BAD_REQUEST),
Err = exmpp_iq:error(Packet, 'bad-request'),
ejabberd_router:route(To, From, Err);
_ ->
XData = jlib:parse_xdata_submit(XDataEl),
case XData of
invalid ->
Err = jlib:make_error_reply(
Packet,
?ERR_BAD_REQUEST),
Err = exmpp_iq:error(Packet,
'bad-request'),
ejabberd_router:route(To, From,
Err);
_ ->
ResIQ =
IQ#iq{
type = result,
sub_el =
[{xmlelement,
"query",
[{"xmlns", ?NS_SEARCH}],
[{xmlelement, "x",
[{"xmlns", ?NS_XDATA},
{"type", "result"}],
search_result(Lang, To, ServerHost, XData)
}]}]},
Result = #xmlel{
ns = ?NS_SEARCH,
name = 'query',
children = [
#xmlel{
ns = ?NS_DATA_FORMS,
name = 'x',
attrs = [#xmlattr{name = 'type',
value = "result"}],
children = search_result(Lang,
To, ServerHost, XData)}]},
ResIQ = exmpp_iq:result(Packet,
Result),
ejabberd_router:route(
To, From, jlib:iq_to_xml(ResIQ))
end
end;
get ->
ResIQ = IQ#iq{type = result,
sub_el = [{xmlelement,
"query",
[{"xmlns", ?NS_SEARCH}],
?FORM(To)
}]},
{get, ?NS_SEARCH} ->
Result = #xmlel{ns = ?NS_SEARCH, name = 'query',
children = ?FORM(To)},
ResIQ = exmpp_iq:result(Packet, Result),
ejabberd_router:route(To,
From,
jlib:iq_to_xml(ResIQ))
end;
#iq{type = Type, xmlns = ?NS_DISCO_INFO, lang = Lang} ->
case Type of
set ->
Err = jlib:make_error_reply(
Packet, ?ERR_NOT_ALLOWED),
ResIQ);
{set, ?NS_DISCO_INFO} ->
Err = exmpp_iq:error(Packet, 'not-allowed'),
ejabberd_router:route(To, From, Err);
get ->
ResIQ =
IQ#iq{type = result,
sub_el = [{xmlelement,
"query",
[{"xmlns", ?NS_DISCO_INFO}],
[{xmlelement, "identity",
[{"category", "directory"},
{"type", "user"},
{"name",
translate:translate(Lang, "vCard User Search")}],
[]},
{xmlelement, "feature",
[{"var", ?NS_SEARCH}], []},
{xmlelement, "feature",
[{"var", ?NS_VCARD}], []}
]
}]},
{get, ?NS_DISCO_INFO} ->
Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
children = [
#xmlel{ns = ?NS_DISCO_INFO, name = 'identity',
attrs = [
#xmlattr{name = 'category',
value = "directory"},
#xmlattr{name = 'type',
value = "user"},
#xmlattr{name = 'name',
value = translate:translate(Lang,
"vCard User Search")}]},
#xmlel{ns = ?NS_DISCO_INFO, name = 'feature',
attrs = [
#xmlattr{name = 'var',
value = ?NS_SEARCH_s}]},
#xmlel{ns = ?NS_DISCO_INFO, name = 'feature',
attrs = [
#xmlattr{name = 'var',
value = ?NS_VCARD_s}]}
]},
ResIQ = exmpp_iq:result(Packet, Result),
ejabberd_router:route(To,
From,
jlib:iq_to_xml(ResIQ))
end;
#iq{type = Type, xmlns = ?NS_DISCO_ITEMS} ->
case Type of
set ->
Err = jlib:make_error_reply(
Packet, ?ERR_NOT_ALLOWED),
ResIQ);
{set, ?NS_DISCO_ITEMS} ->
Err = exmpp_iq:error(Packet, 'not-allowed'),
ejabberd_router:route(To, From, Err);
get ->
ResIQ =
IQ#iq{type = result,
sub_el = [{xmlelement,
"query",
[{"xmlns", ?NS_DISCO_ITEMS}],
[]}]},
{get, ?NS_DISCO_ITEMS} ->
Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query'},
ResIQ = exmpp_iq:result(Packet, Result),
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 = [{xmlelement,
"vCard",
[{"xmlns", ?NS_VCARD}],
iq_get_vcard(Lang)}]},
ResIQ);
{get, ?NS_VCARD} ->
Result = #xmlel{ns = ?NS_VCARD, name = 'vCard',
children = iq_get_vcard(Lang)},
ResIQ = exmpp_iq:result(Packet, Result),
ejabberd_router:route(To,
From,
jlib:iq_to_xml(ResIQ));
ResIQ);
_ ->
Err = jlib:make_error_reply(Packet,
?ERR_SERVICE_UNAVAILABLE),
Err = exmpp_iq:error(Packet, 'service-unavailable'),
ejabberd_router:route(To, From, Err)
end
catch
_ ->
Err1 = exmpp_iq:error(Packet, 'service-unavailable'),
ejabberd_router:route(To, From, Err1)
end
end.
iq_get_vcard(Lang) ->
[{xmlelement, "FN", [],
[{xmlcdata, "ejabberd/mod_vcard"}]},
{xmlelement, "URL", [],
[{xmlcdata, ?EJABBERD_URI}]},
{xmlelement, "DESC", [],
[{xmlcdata, translate:translate(
Lang,
"ejabberd vCard module") ++
"\nCopyright (c) 2003-2008 ProcessOne"}]}].
[
#xmlel{ns = ?NS_SEARCH, name = 'FN', children = [
#xmlcdata{cdata = <<"ejabberd/mod_vcard">>}]},
#xmlel{ns = ?NS_SEARCH, name = 'URL', children = [
#xmlcdata{cdata = list_to_binary(?EJABBERD_URI)}]},
#xmlel{ns = ?NS_SEARCH, name ='DESC', children = [
#xmlcdata{cdata = list_to_binary(
translate:translate(Lang, "ejabberd vCard module") ++
"\nCopyright (c) 2003-2008 ProcessOne")}]}
].
find_xdata_el({xmlelement, _Name, _Attrs, SubEls}) ->
find_xdata_el(#xmlel{children = SubEls}) ->
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([#xmlel{ns = ?NS_DATA_FORMS} = El | _Els]) ->
El;
find_xdata_el1([_ | Els]) ->
find_xdata_el1(Els).
-define(LFIELD(Label, Var),
{xmlelement, "field", [{"label", translate:translate(Lang, Label)},
{"var", Var}], []}).
search_result(Lang, JID, ServerHost, Data) ->
[{xmlelement, "title", [],
[{xmlcdata, translate:translate(Lang, "Search Results for ") ++
jlib:jid_to_string(JID)}]},
{xmlelement, "reported", [],
[#xmlel{ns = ?NS_DATA_FORMS, name = 'title', children =
[#xmlcdata{cdata = list_to_binary(
translate:translate(Lang, "Search Results for ") ++
exmpp_jid:jid_to_list(JID))}]},
#xmlel{ns = ?NS_DATA_FORMS, name = 'reported', children =
[?TLFIELD("text-single", "Jabber ID", "jid"),
?TLFIELD("text-single", "Full Name", "fn"),
?TLFIELD("text-single", "Name", "first"),
@ -470,15 +443,16 @@ search_result(Lang, JID, ServerHost, Data) ->
search(ServerHost, Data)).
-define(FIELD(Var, Val),
{xmlelement, "field", [{"var", Var}],
[{xmlelement, "value", [],
[{xmlcdata, Val}]}]}).
#xmlel{ns = ?NS_DATA_FORMS, name = 'field', attrs =
[#xmlattr{name = 'var', value = Var}], children =
[#xmlel{ns = ?NS_DATA_FORMS, name = 'value', children =
[#xmlcdata{cdata = list_to_binary(Val)}]}]}).
record_to_item(LServer, {Username, FN, Family, Given, Middle,
Nickname, BDay, CTRY, Locality,
EMail, OrgName, OrgUnit}) ->
{xmlelement, "item", [],
#xmlel{ns = ?NS_DATA_FORMS, name = 'item', children =
[
?FIELD("jid", Username ++ "@" ++ LServer),
?FIELD("fn", FN),
@ -546,7 +520,7 @@ filter_fields([], Match, _LServer) ->
end;
filter_fields([{SVar, [Val]} | Ds], Match, LServer)
when is_list(Val) and (Val /= "") ->
LVal = stringprep:tolower(Val),
LVal = exmpp_stringprep:to_lower(Val),
NewMatch = case SVar of
"user" -> make_val(Match, "lusername", LVal);
"fn" -> make_val(Match, "lfn", LVal);
@ -659,8 +633,8 @@ make_val(Match, Field, Val) ->
remove_user(User, Server) ->
LUser = jlib:nodeprep(User),
LServer = jlib:nameprep(Server),
LUser = exmpp_stringprep:nodeprep(User),
LServer = exmpp_stringprep:nameprep(Server),
Username = ejabberd_odbc:escape(LUser),
ejabberd_odbc:sql_transaction(
LServer,