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

Convert to exmpp.

SVN Revision: 1574
This commit is contained in:
Jean-Sébastien Pédron 2008-09-23 13:11:05 +00:00
parent 44c77364ce
commit c74ab439ef
2 changed files with 87 additions and 105 deletions

View File

@ -13,6 +13,8 @@
(process_privacy_iq): Fix a bug where the #iq record was not converted (process_privacy_iq): Fix a bug where the #iq record was not converted
back to an #xmlel before calling ejabberd_router:route/3. back to an #xmlel before calling ejabberd_router:route/3.
* src/mod_register.erl: Convert to exmpp.
2008-09-22 Jean-Sébastien Pédron <js.pedron@meetic-corp.com> 2008-09-22 Jean-Sébastien Pédron <js.pedron@meetic-corp.com>
* src/mod_vcard.erl (get_sm_features): Remove unappropriate * src/mod_vcard.erl (get_sm_features): Remove unappropriate

View File

@ -35,14 +35,15 @@
unauthenticated_iq_register/4, unauthenticated_iq_register/4,
process_iq/3]). process_iq/3]).
-include_lib("exmpp/include/exmpp.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("jlib.hrl").
start(Host, Opts) -> start(Host, Opts) ->
IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue), IQDisc = gen_mod:get_opt(iqdisc, Opts, one_queue),
gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_REGISTER, gen_iq_handler:add_iq_handler(ejabberd_local, Host, ?NS_INBAND_REGISTER,
?MODULE, process_iq, IQDisc), ?MODULE, process_iq, IQDisc),
gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_REGISTER, gen_iq_handler:add_iq_handler(ejabberd_sm, Host, ?NS_INBAND_REGISTER,
?MODULE, process_iq, IQDisc), ?MODULE, process_iq, IQDisc),
ejabberd_hooks:add(c2s_stream_features, Host, ejabberd_hooks:add(c2s_stream_features, Host,
?MODULE, stream_feature_register, 50), ?MODULE, stream_feature_register, 50),
@ -60,61 +61,56 @@ stop(Host) ->
?MODULE, stream_feature_register, 50), ?MODULE, stream_feature_register, 50),
ejabberd_hooks:delete(c2s_unauthenticated_iq, Host, ejabberd_hooks:delete(c2s_unauthenticated_iq, Host,
?MODULE, unauthenticated_iq_register, 50), ?MODULE, unauthenticated_iq_register, 50),
gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_REGISTER), gen_iq_handler:remove_iq_handler(ejabberd_local, Host, ?NS_INBAND_REGISTER),
gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_REGISTER). gen_iq_handler:remove_iq_handler(ejabberd_sm, Host, ?NS_INBAND_REGISTER).
stream_feature_register(Acc) -> stream_feature_register(Acc) ->
[{xmlelement, "register", [#xmlel{ns = ?NS_INBAND_REGISTER_FEAT, name = 'register'} | Acc].
[{"xmlns", ?NS_FEATURE_IQREGISTER}], []} | Acc].
unauthenticated_iq_register(_Acc, unauthenticated_iq_register(_Acc,
Server, #iq{xmlns = ?NS_REGISTER} = IQ, IP) -> Server, #iq{ns = ?NS_INBAND_REGISTER} = IQ_Rec, IP) ->
Address = case IP of Address = case IP of
{A, _Port} -> A; {A, _Port} -> A;
_ -> undefined _ -> undefined
end, end,
ResIQ = process_iq(jlib:make_jid("", "", ""), ResIQ = process_iq(#jid{},
jlib:make_jid("", Server, ""), exmpp_jid:make_bare_jid(Server),
IQ, IQ_Rec,
Address), Address),
Res1 = jlib:replace_from_to(jlib:make_jid("", Server, ""), exmpp_iq:iq_to_xmlel(ResIQ, exmpp_jid:make_bare_jid(Server), undefined);
jlib:make_jid("", "", ""),
jlib:iq_to_xml(ResIQ)),
jlib:remove_attr("to", Res1);
unauthenticated_iq_register(Acc, _Server, _IQ, _IP) -> unauthenticated_iq_register(Acc, _Server, _IQ, _IP) ->
Acc. Acc.
process_iq(From, To, IQ) -> process_iq(From, To, IQ) ->
process_iq(From, To, IQ, jlib:jid_tolower(jlib:jid_remove_resource(From))). process_iq(From, To, IQ, jlib:short_prepd_bare_jid(From)).
process_iq(From, To, process_iq(From, To,
#iq{type = Type, lang = Lang, sub_el = SubEl, id = ID} = IQ, #iq{type = Type, lang = Lang, payload = SubEl} = IQ_Rec,
Source) -> Source) ->
case Type of case Type of
set -> set ->
UTag = xml:get_subtag(SubEl, "username"), UTag = exmpp_xml:get_element(SubEl, 'username'),
PTag = xml:get_subtag(SubEl, "password"), PTag = exmpp_xml:get_element(SubEl, 'password'),
RTag = xml:get_subtag(SubEl, "remove"), RTag = exmpp_xml:get_element(SubEl, 'remove'),
Server = To#jid.lserver, Server = To#jid.ldomain,
if if
(UTag /= false) and (RTag /= false) -> (UTag /= undefined) and (RTag /= undefined) ->
User = xml:get_tag_cdata(UTag), User = exmpp_xml:get_cdata_as_list(UTag),
case From of case From of
#jid{user = User, lserver = Server} -> #jid{node = User, ldomain = Server} ->
ejabberd_auth:remove_user(User, Server), ejabberd_auth:remove_user(User, Server),
IQ#iq{type = result, sub_el = [SubEl]}; exmpp_iq:result(IQ_Rec, SubEl);
_ -> _ ->
if if
PTag /= false -> PTag /= undefined ->
Password = xml:get_tag_cdata(PTag), Password = exmpp_xml:get_cdata_as_list(PTag),
case ejabberd_auth:remove_user(User, case ejabberd_auth:remove_user(User,
Server, Server,
Password) of Password) of
ok -> ok ->
IQ#iq{type = result, exmpp_iq:result(IQ_Rec, SubEl);
sub_el = [SubEl]};
%% TODO FIXME: This piece of %% TODO FIXME: This piece of
%% code does not work since %% code does not work since
%% the code have been changed %% the code have been changed
@ -122,102 +118,88 @@ process_iq(From, To,
%% modules. lists:foreach can %% modules. lists:foreach can
%% only return ok: %% only return ok:
not_allowed -> not_allowed ->
IQ#iq{type = error, exmpp_iq:error(IQ_Rec,
sub_el = 'not-allowed');
[SubEl, ?ERR_NOT_ALLOWED]};
not_exists -> not_exists ->
IQ#iq{type = error, exmpp_iq:error(IQ_Rec,
sub_el = 'item-not-found');
[SubEl, ?ERR_ITEM_NOT_FOUND]};
_ -> _ ->
IQ#iq{type = error, exmpp_iq:error(IQ_Rec,
sub_el = 'internal-server-error')
[SubEl,
?ERR_INTERNAL_SERVER_ERROR]}
end; end;
true -> true ->
IQ#iq{type = error, exmpp_iq:error(IQ_Rec, 'bad-request')
sub_el = [SubEl, ?ERR_BAD_REQUEST]}
end end
end; end;
(UTag == false) and (RTag /= false) -> (UTag == undefined) and (RTag /= undefined) ->
case From of case From of
#jid{user = User, #jid{node = User,
lserver = Server, ldomain = Server,
resource = Resource} -> resource = Resource} ->
ResIQ = #iq{type = result, xmlns = ?NS_REGISTER, ResIQ = exmpp_iq:result(IQ_Rec, SubEl),
id = ID,
sub_el = [SubEl]},
ejabberd_router:route( ejabberd_router:route(
jlib:make_jid(User, Server, Resource), exmpp_jid:make_jid(User, Server, Resource),
jlib:make_jid(User, Server, Resource), exmpp_jid:make_jid(User, Server, Resource),
jlib:iq_to_xml(ResIQ)), exmpp_iq:iq_to_xmlel(ResIQ)),
ejabberd_auth:remove_user(User, Server), ejabberd_auth:remove_user(User, Server),
ignore; ignore;
_ -> _ ->
IQ#iq{type = error, exmpp_iq:error(IQ_Rec, 'not-allowed')
sub_el = [SubEl, ?ERR_NOT_ALLOWED]}
end; end;
(UTag /= false) and (PTag /= false) -> (UTag /= undefined) and (PTag /= undefined) ->
User = xml:get_tag_cdata(UTag), User = exmpp_xml:get_cdata_as_list(UTag),
Password = xml:get_tag_cdata(PTag), Password = exmpp_xml:get_cdata_as_list(PTag),
case From of case From of
#jid{user = User, lserver = Server} -> #jid{node = User, ldomain = Server} ->
try_set_password(User, Server, Password, IQ, SubEl); try_set_password(User, Server, Password, IQ_Rec, SubEl);
_ -> _ ->
case try_register(User, Server, Password, case try_register(User, Server, Password,
Source, Lang) of Source, Lang) of
ok -> ok ->
IQ#iq{type = result, sub_el = [SubEl]}; exmpp_iq:result(IQ_Rec, SubEl);
{error, Error} -> {error, Error} ->
IQ#iq{type = error, exmpp_iq:error(IQ_Rec, Error)
sub_el = [SubEl, Error]}
end end
end; end;
true -> true ->
IQ#iq{type = error, exmpp_iq:error(IQ_Rec, 'bad-request')
sub_el = [SubEl, ?ERR_BAD_REQUEST]}
end; end;
get -> get ->
IQ#iq{type = result, Result = #xmlel{ns = ?NS_INBAND_REGISTER, name = 'query', children =
sub_el = [{xmlelement, [#xmlel{ns = ?NS_INBAND_REGISTER, name = 'instructions', children =
"query", [#xmlcdata{cdata = list_to_binary(translate:translate(Lang,
[{"xmlns", "jabber:iq:register"}], "Choose a username and password "
[{xmlelement, "instructions", [], "to register with this server"))}]},
[{xmlcdata, #xmlel{ns = ?NS_INBAND_REGISTER, name = 'username'},
translate:translate( #xmlel{ns = ?NS_INBAND_REGISTER, name = 'password'}]},
Lang, exmpp_iq:result(IQ_Rec, Result)
"Choose a username and password "
"to register with this server")}]},
{xmlelement, "username", [], []},
{xmlelement, "password", [], []}]}]}
end. end.
%% @doc Try to change password and return IQ response %% @doc Try to change password and return IQ response
try_set_password(User, Server, Password, IQ, SubEl) -> try_set_password(User, Server, Password, IQ_Rec, SubEl) ->
case ejabberd_auth:set_password(User, Server, Password) of case ejabberd_auth:set_password(User, Server, Password) of
ok -> ok ->
IQ#iq{type = result, sub_el = [SubEl]}; exmpp_iq:result(IQ_Rec, SubEl);
{error, empty_password} -> {error, empty_password} ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_BAD_REQUEST]}; exmpp_iq:error(IQ_Rec, 'bad-request');
{error, not_allowed} -> {error, not_allowed} ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_NOT_ALLOWED]}; exmpp_iq:error(IQ_Rec, 'not-allowed');
{error, invalid_jid} -> {error, invalid_jid} ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_ITEM_NOT_FOUND]}; exmpp_iq:error(IQ_Rec, 'item-not-found');
_ -> _ ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]} exmpp_iq:error(IQ_Rec, 'internal-server-error')
end. end.
try_register(User, Server, Password, Source, Lang) -> try_register(User, Server, Password, Source, Lang) ->
case jlib:is_nodename(User) of case exmpp_stringprep:is_node(User) of
false -> false ->
{error, ?ERR_BAD_REQUEST}; {error, 'bad-request'};
_ -> _ ->
JID = jlib:make_jid(User, Server, ""), JID = exmpp_jid:make_bare_jid(User, Server),
Access = gen_mod:get_module_opt(Server, ?MODULE, access, all), Access = gen_mod:get_module_opt(Server, ?MODULE, access, all),
case acl:match_rule(Server, Access, JID) of case acl:match_rule(Server, Access, JID) of
deny -> deny ->
{error, ?ERR_CONFLICT}; {error, 'conflict'};
allow -> allow ->
case check_timeout(Source) of case check_timeout(Source) of
true -> true ->
@ -232,60 +214,58 @@ try_register(User, Server, Password, Source, Lang) ->
remove_timeout(Source), remove_timeout(Source),
case Error of case Error of
{atomic, exists} -> {atomic, exists} ->
{error, ?ERR_CONFLICT}; {error, 'conflict'};
{error, invalid_jid} -> {error, invalid_jid} ->
{error, ?ERR_JID_MALFORMED}; {error, 'jid-malformed'};
{error, not_allowed} -> {error, not_allowed} ->
{error, ?ERR_NOT_ALLOWED}; {error, 'not-allowed'};
{error, _Reason} -> {error, _Reason} ->
{error, ?ERR_INTERNAL_SERVER_ERROR} {error, 'internal-server-error'}
end end
end; end;
false -> false ->
ErrText = "Users are not allowed to register " ErrText = "Users are not allowed to register "
"accounts so fast", "accounts so fast",
{error, ?ERRT_RESOURCE_CONSTRAINT(Lang, ErrText)} {error, exmpp_stanza:error(?NS_JABBER_CLIENT, 'resource-constraint', {Lang, ErrText})}
end end
end end
end. end.
send_welcome_message(JID) -> send_welcome_message(JID) ->
Host = JID#jid.lserver, Host = JID#jid.ldomain,
case gen_mod:get_module_opt(Host, ?MODULE, welcome_message, {"", ""}) of case gen_mod:get_module_opt(Host, ?MODULE, welcome_message, {"", ""}) of
{"", ""} -> {"", ""} ->
ok; ok;
{Subj, Body} -> {Subj, Body} ->
ejabberd_router:route( ejabberd_router:route(
jlib:make_jid("", Host, ""), exmpp_jid:make_bare_jid(Host),
JID, JID,
{xmlelement, "message", [{"type", "normal"}], exmpp_message:normal(Subj, Body));
[{xmlelement, "subject", [], [{xmlcdata, Subj}]},
{xmlelement, "body", [], [{xmlcdata, Body}]}]});
_ -> _ ->
ok ok
end. end.
send_registration_notifications(UJID) -> send_registration_notifications(UJID) ->
Host = UJID#jid.lserver, Host = UJID#jid.ldomain,
case gen_mod:get_module_opt(Host, ?MODULE, registration_watchers, []) of case gen_mod:get_module_opt(Host, ?MODULE, registration_watchers, []) of
[] -> ok; [] -> ok;
JIDs when is_list(JIDs) -> JIDs when is_list(JIDs) ->
Body = lists:flatten( Body = lists:flatten(
io_lib:format( io_lib:format(
"The user '~s' was just created on node ~w.", "The user '~s' was just created on node ~w.",
[jlib:jid_to_string(UJID), node()])), [exmpp_jid:jid_to_list(UJID), node()])),
lists:foreach( lists:foreach(
fun(S) -> fun(S) ->
case jlib:string_to_jid(S) of try
error -> ok; JID = exmpp_jid:list_to_jid(S),
JID -> ejabberd_router:route(
ejabberd_router:route( exmpp_jid:make_bare_jid(Host),
jlib:make_jid("", Host, ""), JID,
JID, exmpp_message:chat(Body))
{xmlelement, "message", [{"type", "chat"}], catch
[{xmlelement, "body", [], _ ->
[{xmlcdata, Body}]}]}) ok
end end
end, JIDs); end, JIDs);
_ -> _ ->