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

Convert mod_proxy65 to exmpp.

SVN Revision: 1701
This commit is contained in:
Jean-Sébastien Pédron 2008-12-03 14:34:43 +00:00
parent f30d2b9f1c
commit b425093813
3 changed files with 68 additions and 55 deletions

View File

@ -1,3 +1,8 @@
2008-12-03 Jean-Sébastien Pédron <js.pedron@meetic-corp.com>
* src/mod_proxy65/mod_proxy65_stream.erl,
src/mod_proxy65/mod_proxy65_service.erl: Convert mod_proxy65 to exmpp.
2008-12-01 Jean-Sébastien Pédron <js.pedron@meetic-corp.com> 2008-12-01 Jean-Sébastien Pédron <js.pedron@meetic-corp.com>
Merge from trunk (r1649 to r1692). Merge from trunk (r1649 to r1692).

View File

@ -41,8 +41,9 @@
%% API. %% API.
-export([start_link/2, add_listener/2, delete_listener/1]). -export([start_link/2, add_listener/2, delete_listener/1]).
-include_lib("exmpp/include/exmpp.hrl").
-include("ejabberd.hrl"). -include("ejabberd.hrl").
-include("jlib.hrl").
-define(PROCNAME, ejabberd_mod_proxy65_service). -define(PROCNAME, ejabberd_mod_proxy65_service).
@ -73,14 +74,15 @@ terminate(_Reason, #state{myhost=MyHost}) ->
ejabberd_router:unregister_route(MyHost), ejabberd_router:unregister_route(MyHost),
ok. ok.
handle_info({route, From, To, {xmlelement, "iq", _, _} = Packet}, State) -> handle_info({route, From, To, Packet}, State) when ?IS_IQ(Packet) ->
IQ = jlib:iq_query_info(Packet), IQ_Rec = exmpp_iq:xmlel_to_iq(Packet),
case catch process_iq(From, IQ, State) of case catch process_iq(From, IQ_Rec, State) of
Result when is_record(Result, iq) -> Result when ?IS_IQ_RECORD(Result) ->
ejabberd_router:route(To, From, jlib:iq_to_xml(Result)); ejabberd_router:route(To, From,
exmpp_iq:iq_to_xmlel(Result, To, From));
{'EXIT', Reason} -> {'EXIT', Reason} ->
?ERROR_MSG("Error when processing IQ stanza: ~p", [Reason]), ?ERROR_MSG("Error when processing IQ stanza: ~p", [Reason]),
Err = jlib:make_error_reply(Packet, ?ERR_INTERNAL_SERVER_ERROR), Err = exmpp_iq:error(Packet, 'internal-server-error'),
ejabberd_router:route(To, From, Err); ejabberd_router:route(To, From, Err);
_ -> _ ->
ok ok
@ -119,67 +121,72 @@ delete_listener(Host) ->
%%%------------------------ %%%------------------------
%% disco#info request %% disco#info request
process_iq(_, #iq{type = get, xmlns = ?NS_DISCO_INFO, lang = Lang} = IQ, #state{name=Name}) -> process_iq(_, #iq{type = get, ns = ?NS_DISCO_INFO, lang = Lang} = IQ_Rec, #state{name=Name}) ->
IQ#iq{type = result, sub_el = Result = #xmlel{ns = ?NS_DISCO_INFO, name = 'query',
[{xmlelement, "query", [{"xmlns", ?NS_DISCO_INFO}], iq_disco_info(Lang, Name)}]}; children = iq_disco_info(Lang, Name)},
exmpp_iq:result(IQ_Rec, Result);
%% disco#items request %% disco#items request
process_iq(_, #iq{type = get, xmlns = ?NS_DISCO_ITEMS} = IQ, _) -> process_iq(_, #iq{type = get, ns = ?NS_DISCO_ITEMS} = IQ_Rec, _) ->
IQ#iq{type = result, sub_el = Result = #xmlel{ns = ?NS_DISCO_ITEMS, name = 'query',
[{xmlelement, "query", [{"xmlns", ?NS_DISCO_ITEMS}], []}]}; children = []},
exmpp_iq:result(IQ_Rec, Result);
%% vCard request %% vCard request
process_iq(_, #iq{type = get, xmlns = ?NS_VCARD, lang = Lang} = IQ, _) -> process_iq(_, #iq{type = get, ns = ?NS_VCARD, lang = Lang} = IQ_Rec, _) ->
IQ#iq{type = result, sub_el = Result = #xmlel{ns = ?NS_VCARD, name = 'vCard',
[{xmlelement, "vCard", [{"xmlns", ?NS_VCARD}], iq_vcard(Lang)}]}; children = iq_vcard(Lang)},
exmpp_iq:result(IQ_Rec, Result);
%% bytestreams info request %% bytestreams info request
process_iq(JID, #iq{type = get, sub_el = SubEl, xmlns = ?NS_BYTESTREAMS} = IQ, process_iq(JID, #iq{type = get, ns = ?NS_BYTESTREAMS} = IQ_Rec,
#state{acl = ACL, stream_addr = StreamAddr, serverhost = ServerHost}) -> #state{acl = ACL, stream_addr = StreamAddr, serverhost = ServerHost}) ->
case acl:match_rule(ServerHost, ACL, JID) of case acl:match_rule(ServerHost, ACL, JID) of
allow -> allow ->
StreamHostEl = [{xmlelement, "streamhost", StreamAddr, []}], StreamHostEl = [#xmlel{ns = ?NS_BYTESTREAMS, name = 'streamhost',
IQ#iq{type = result, sub_el = attrs = StreamAddr}],
[{xmlelement, "query", [{"xmlns", ?NS_BYTESTREAMS}], StreamHostEl}]}; Result = #xmlel{ns = ?NS_BYTESTREAMS, name = 'query',
children = StreamHostEl},
exmpp_iq:result(IQ_Rec, Result);
deny -> deny ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]} exmpp_iq:error(IQ_Rec, 'forbidden')
end; end;
%% bytestream activation request %% bytestream activation request
process_iq(InitiatorJID, #iq{type = set, sub_el = SubEl, xmlns = ?NS_BYTESTREAMS} = IQ, process_iq(InitiatorJID, #iq{type = set, payload = SubEl, ns = ?NS_BYTESTREAMS} = IQ_Rec,
#state{acl = ACL, serverhost = ServerHost}) -> #state{acl = ACL, serverhost = ServerHost}) ->
case acl:match_rule(ServerHost, ACL, InitiatorJID) of case acl:match_rule(ServerHost, ACL, InitiatorJID) of
allow -> allow ->
ActivateEl = xml:get_path_s(SubEl, [{elem, "activate"}]), ActivateEl = exmpp_xml:get_path(SubEl, [{element, 'activate'}]),
SID = xml:get_tag_attr_s("sid", SubEl), SID = exmpp_xml:get_attribute(SubEl, 'sid', ""),
case catch jlib:string_to_jid(xml:get_tag_cdata(ActivateEl)) of case catch exmpp_jid:list_to_jid(exmpp_xml:get_cdata_as_string(ActivateEl)) of
TargetJID when is_record(TargetJID, jid), SID /= "", TargetJID when ?IS_JID(TargetJID), SID /= "",
length(SID) =< 128, TargetJID /= InitiatorJID -> length(SID) =< 128, TargetJID /= InitiatorJID ->
Target = jlib:jid_to_string(jlib:jid_tolower(TargetJID)), Target = exmpp_jid:prepd_jid_to_list(TargetJID),
Initiator = jlib:jid_to_string(jlib:jid_tolower(InitiatorJID)), Initiator = exmpp_jid:prepd_jid_to_list(InitiatorJID),
SHA1 = sha:sha(SID ++ Initiator ++ Target), SHA1 = sha:sha(SID ++ Initiator ++ Target),
case mod_proxy65_sm:activate_stream(SHA1, InitiatorJID, TargetJID, ServerHost) of case mod_proxy65_sm:activate_stream(SHA1, InitiatorJID, TargetJID, ServerHost) of
ok -> ok ->
IQ#iq{type = result, sub_el = []}; exmpp_iq:result(IQ_Rec);
false -> false ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_ITEM_NOT_FOUND]}; exmpp_iq:error(IQ_Rec, 'item-not-found');
limit -> limit ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_RESOURCE_CONSTRAINT]}; exmpp_iq:error(IQ_Rec, 'resource-constraint');
conflict -> conflict ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_CONFLICT]}; exmpp_iq:error(IQ_Rec, 'conflict');
_ -> _ ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_INTERNAL_SERVER_ERROR]} exmpp_iq:error(IQ_Rec, 'internal-server-error')
end; end;
_ -> _ ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_BAD_REQUEST]} exmpp_iq:error(IQ_Rec, 'bad-request')
end; end;
deny -> deny ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_FORBIDDEN]} exmpp_iq:error(IQ_Rec, 'forbidden')
end; end;
%% Unknown "set" or "get" request %% Unknown "set" or "get" request
process_iq(_, #iq{type=Type, sub_el=SubEl} = IQ, _) when Type==get; Type==set -> process_iq(_, #iq{kind=request} = IQ_Rec, _) ->
IQ#iq{type = error, sub_el = [SubEl, ?ERR_SERVICE_UNAVAILABLE]}; exmpp_iq:error(IQ_Rec, 'service-unavailable');
%% IQ "result" or "error". %% IQ "result" or "error".
process_iq(_, _, _) -> process_iq(_, _, _) ->
@ -188,25 +195,26 @@ process_iq(_, _, _) ->
%%%------------------------- %%%-------------------------
%%% Auxiliary functions. %%% Auxiliary functions.
%%%------------------------- %%%-------------------------
-define(FEATURE(Feat), {xmlelement,"feature",[{"var", Feat}],[]}). -define(FEATURE(Feat), #xmlel{ns = ?NS_DISCO_INFO, name = 'feature',
attrs = [#xmlattr{name = 'var', value = Feat}]}).
iq_disco_info(Lang, Name) -> iq_disco_info(Lang, Name) ->
[{xmlelement, "identity", [#xmlel{ns = ?NS_DISCO_INFO, name = 'identity', attrs =
[{"category", "proxy"}, [#xmlattr{name = 'category', value = "proxy"},
{"type", "bytestreams"}, #xmlattr{name = 'type', value = "bytestreams"},
{"name", translate:translate(Lang, Name)}], []}, #xmlattr{name = 'name', value = translate:translate(Lang, Name)}]},
?FEATURE(?NS_DISCO_INFO), ?FEATURE(?NS_DISCO_INFO_s),
?FEATURE(?NS_VCARD), ?FEATURE(?NS_VCARD_s),
?FEATURE(?NS_BYTESTREAMS)]. ?FEATURE(?NS_BYTESTREAMS_s)].
iq_vcard(Lang) -> iq_vcard(Lang) ->
[{xmlelement, "FN", [], [#xmlel{ns = ?NS_VCARD, name = 'FN', children =
[{xmlcdata, "ejabberd/mod_proxy65"}]}, [#xmlcdata{cdata = <<"ejabberd/mod_proxy65">>}]},
{xmlelement, "URL", [], #xmlel{ns = ?NS_VCARD, name = 'URL', children =
[{xmlcdata, ?EJABBERD_URI}]}, [#xmlcdata{cdata = list_to_binary(?EJABBERD_URI)}]},
{xmlelement, "DESC", [], #xmlel{ns = ?NS_VCARD, name = 'DESC', children =
[{xmlcdata, translate:translate(Lang, "ejabberd SOCKS5 Bytestreams module") ++ [#xmlcdata{cdata = list_to_binary(translate:translate(Lang, "ejabberd SOCKS5 Bytestreams module") ++
"\nCopyright (c) 2003-2008 Alexey Shchepin"}]}]. "\nCopyright (c) 2003-2008 Alexey Shchepin")}]}].
parse_options(ServerHost, Opts) -> parse_options(ServerHost, Opts) ->
MyHost = gen_mod:get_opt_host(ServerHost, Opts, "proxy.@HOST@"), MyHost = gen_mod:get_opt_host(ServerHost, Opts, "proxy.@HOST@"),
@ -218,7 +226,7 @@ parse_options(ServerHost, Opts) ->
Addr -> Addr Addr -> Addr
end, end,
StrIP = inet_parse:ntoa(IP), StrIP = inet_parse:ntoa(IP),
StreamAddr = [{"jid", MyHost}, {"host", StrIP}, {"port", integer_to_list(Port)}], StreamAddr = [#xmlattr{name = 'jid', value = MyHost}, #xmlattr{name = 'host', value = StrIP}, #xmlattr{name = 'port', value = integer_to_list(Port)}],
{IP, #state{myhost = MyHost, {IP, #state{myhost = MyHost,
serverhost = ServerHost, serverhost = ServerHost,
name = Name, name = Name,

View File

@ -122,8 +122,8 @@ activate({P1, J1}, {P2, J2}) ->
{S1, S2} when is_port(S1), is_port(S2) -> {S1, S2} when is_port(S1), is_port(S2) ->
P1 ! {activate, P2, S2, J1, J2}, P1 ! {activate, P2, S2, J1, J2},
P2 ! {activate, P1, S1, J1, J2}, P2 ! {activate, P1, S1, J1, J2},
JID1 = jlib:jid_to_string(J1), JID1 = exmpp_jid:jid_to_list(J1),
JID2 = jlib:jid_to_string(J2), JID2 = exmpp_jid:jid_to_list(J2),
?INFO_MSG("(~w:~w) Activated bytestream for ~s -> ~s", [P1, P2, JID1, JID2]), ?INFO_MSG("(~w:~w) Activated bytestream for ~s -> ~s", [P1, P2, JID1, JID2]),
ok; ok;
_ -> _ ->