add extended stanza addressing 'replyto' on PEP (EJAB-1198) (thanks karim)

This commit is contained in:
Christophe Romain 2010-03-05 17:52:41 +01:00
parent 6e878d17e6
commit 6ee8ec49a5
4 changed files with 143 additions and 124 deletions

View File

@ -84,6 +84,7 @@
-define(NS_CAPS, "http://jabber.org/protocol/caps").
-define(NS_SHIM, "http://jabber.org/protocol/shim").
-define(NS_ADDRESS, "http://jabber.org/protocol/address").
%% CAPTCHA related NSes.
-define(NS_OOB, "jabber:x:oob").

View File

@ -2900,8 +2900,7 @@ event_stanza_withmoreels(Els, MoreEls) ->
%%%%%% broadcast functions
broadcast_publish_item(Host, Node, NodeId, Type, NodeOptions, Removed, ItemId, _From, Payload) ->
%broadcast(Host, Node, NodeId, NodeOptions, none, true, "items", ItemEls)
broadcast_publish_item(Host, Node, NodeId, Type, NodeOptions, Removed, ItemId, From, Payload) ->
case get_collection_subscriptions(Host, Node) of
SubsByDepth when is_list(SubsByDepth) ->
Content = case get_option(NodeOptions, deliver_payloads) of
@ -2911,7 +2910,7 @@ broadcast_publish_item(Host, Node, NodeId, Type, NodeOptions, Removed, ItemId, _
Stanza = event_stanza(
[{xmlelement, "items", nodeAttr(Node),
[{xmlelement, "item", itemAttr(ItemId), Content}]}]),
broadcast_stanza(Host, Node, NodeId, Type,
broadcast_stanza(Host, From, Node, NodeId, Type,
NodeOptions, SubsByDepth, items, Stanza, true),
case Removed of
[] ->
@ -2939,7 +2938,6 @@ broadcast_retract_items(Host, Node, NodeId, Type, NodeOptions, ItemIds) ->
broadcast_retract_items(_Host, _Node, _NodeId, _Type, _NodeOptions, [], _ForceNotify) ->
{result, false};
broadcast_retract_items(Host, Node, NodeId, Type, NodeOptions, ItemIds, ForceNotify) ->
%broadcast(Host, Node, NodeId, NodeOptions, notify_retract, ForceNotify, "retract", RetractEls)
case (get_option(NodeOptions, notify_retract) or ForceNotify) of
true ->
case get_collection_subscriptions(Host, Node) of
@ -2958,7 +2956,6 @@ broadcast_retract_items(Host, Node, NodeId, Type, NodeOptions, ItemIds, ForceNot
end.
broadcast_purge_node(Host, Node, NodeId, Type, NodeOptions) ->
%broadcast(Host, Node, NodeId, NodeOptions, notify_retract, false, "purge", [])
case get_option(NodeOptions, notify_retract) of
true ->
case get_collection_subscriptions(Host, Node) of
@ -2977,7 +2974,6 @@ broadcast_purge_node(Host, Node, NodeId, Type, NodeOptions) ->
end.
broadcast_removed_node(Host, Node, NodeId, Type, NodeOptions, SubsByDepth) ->
%broadcast(Host, Node, NodeId, NodeOptions, notify_delete, false, "delete", [])
case get_option(NodeOptions, notify_delete) of
true ->
case SubsByDepth of
@ -2996,7 +2992,6 @@ broadcast_removed_node(Host, Node, NodeId, Type, NodeOptions, SubsByDepth) ->
end.
broadcast_config_notification(Host, Node, NodeId, Type, NodeOptions, Lang) ->
%broadcast(Host, Node, NodeId, NodeOptions, notify_config, false, "items", ConfigEls)
case get_option(NodeOptions, notify_config) of
true ->
case get_collection_subscriptions(Host, Node) of
@ -3068,60 +3063,61 @@ broadcast_stanza(Host, _Node, _NodeId, _Type, NodeOptions, SubsByDepth, NotifyTy
[LJID]
end,
%% Determine if the stanza should have SHIM ('SubID' and 'name') headers
StanzaToSend = case SHIM of
true ->
Headers = lists:append(collection_shim(NodeName), subid_shim(SubIDs)),
add_headers(Stanza, Headers);
false ->
Stanza
end,
StanzaToSend = case {SHIM, SubIDs} of
{false, _} ->
Stanza;
%% If there's only one SubID, don't add it
{true, [_]} ->
add_shim_headers(Stanza, collection_shim(NodeName));
{true, SubIDs} ->
add_shim_headers(Stanza, lists:append(collection_shim(NodeName), subid_shim(SubIDs)))
end,
lists:foreach(fun(To) ->
ejabberd_router:route(From, jlib:make_jid(To), StanzaToSend)
end, LJIDs)
end, SubIDsByJID),
ejabberd_router:route(From, jlib:make_jid(To), StanzaToSend)
end, LJIDs)
end, SubIDsByJID).
broadcast_stanza({LUser, LServer, LResource}, Publisher, Node, NodeId, Type, NodeOptions, SubsByDepth, NotifyType, BaseStanza, SHIM) ->
broadcast_stanza({LUser, LServer, LResource}, Node, NodeId, Type, NodeOptions, SubsByDepth, NotifyType, BaseStanza, SHIM),
%% Handles implicit presence subscriptions
case Host of
{LUser, LServer, LResource} ->
SenderResource = case LResource of
[] ->
case user_resources(LUser, LServer) of
[Resource|_] -> Resource;
_ -> ""
end;
SenderResource = case LResource of
[] ->
case user_resources(LUser, LServer) of
[Resource|_] -> Resource;
_ -> ""
end;
_ ->
LResource
end,
case ejabberd_sm:get_session_pid(LUser, LServer, SenderResource) of
C2SPid when is_pid(C2SPid) ->
Stanza = case get_option(NodeOptions, notification_type, headline) of
normal -> BaseStanza;
MsgType -> add_message_type(BaseStanza, atom_to_list(MsgType))
end,
%% set the from address on the notification to the bare JID of the account owner
%% Also, add "replyto" if entity has presence subscription to the account owner
%% See XEP-0163 1.1 section 4.3.1
Sender = jlib:make_jid(LUser, LServer, ""),
ReplyTo = jlib:jid_to_string(Publisher),
StanzaToSend = add_extended_headers(Stanza, extended_headers([ReplyTo])),
case catch ejabberd_c2s:get_subscribed(C2SPid) of
Contacts when is_list(Contacts) ->
lists:foreach(fun({U, S, _}) ->
spawn(fun() ->
lists:foreach(fun(To) ->
ejabberd_router:route(Sender, jlib:make_jid(To), StanzaToSend)
end, [{U, S, R} || R <- user_resources(U, S)])
end)
end, Contacts);
_ ->
LResource
end,
case ejabberd_sm:get_session_pid(LUser, LServer, SenderResource) of
C2SPid when is_pid(C2SPid) ->
%% set the from address on the notification to the bare JID of the account owner
%% Also, add "replyto" if entity has presence subscription to the account owner
%% See XEP-0163 1.1 section 4.3.1
Sender = jlib:make_jid(LUser, LServer, ""),
%%ReplyTo = jlib:make_jid(LUser, LServer, SenderResource), % This has to be used
case catch ejabberd_c2s:get_subscribed(C2SPid) of
Contacts when is_list(Contacts) ->
lists:foreach(fun({U, S, _}) ->
spawn(fun() ->
LJIDs = lists:foldl(fun(R, Acc) ->
LJID = {U, S, R},
[LJID | Acc]
end, [], user_resources(U, S)),
lists:foreach(fun(To) ->
ejabberd_router:route(Sender, jlib:make_jid(To), Stanza)
end, LJIDs)
end)
end, Contacts);
_ ->
ok
end,
ok;
_ ->
?DEBUG("~p@~p has no session; can't deliver ~p to contacts", [LUser, LServer, Stanza]),
ok
end;
_ ->
ok
end.
?DEBUG("~p@~p has no session; can't deliver ~p to contacts", [LUser, LServer, BaseStanza])
end;
broadcast_stanza(_Host, _Publisher, _Node, _NodeId, _Type, _NodeOptions, _SubsByDepth, _NotifyType, _BaseStanza, _SHIM) ->
ok.
subscribed_nodes_by_jid(NotifyType, SubsByDepth) ->
NodesToDeliver = fun(Depth, Node, Subs, Acc) ->
@ -3730,8 +3726,14 @@ add_message_type(XmlEl, _Type) ->
%% "[SHIM Headers] SHOULD be included after the event notification information
%% (i.e., as the last child of the <message/> stanza)".
add_headers({xmlelement, Name, Attrs, Els}, HeaderEls) ->
HeaderEl = {xmlelement, "headers", [{"xmlns", ?NS_SHIM}], HeaderEls},
add_shim_headers(Stanza, HeaderEls) ->
add_headers(Stanza, "headers", ?NS_SHIM, HeaderEls).
add_extended_headers(Stanza, HeaderEls) ->
add_headers(Stanza, "addresses", ?NS_ADDRESS, HeaderEls).
add_headers({xmlelement, Name, Attrs, Els}, HeaderName, HeaderNS, HeaderEls) ->
HeaderEl = {xmlelement, HeaderName, [{"xmlns", HeaderNS}], HeaderEls},
{xmlelement, Name, Attrs, lists:append(Els, [HeaderEl])}.
%% Removed multiple <header name=Collection>Foo</header/> elements
@ -3755,6 +3757,13 @@ subid_shim(SubIDs) ->
[{xmlelement, "header", [{"name", "SubID"}],
[{xmlcdata, SubID}]} || SubID <- SubIDs].
%% The argument is a list of Jids because this function could be used
%% with the 'pubsub#replyto' (type=jid-multi) node configuration.
extended_headers(Jids) ->
[{xmlelement, "address", [{"type", "replyto"}, {"jid", Jid}], []} || Jid <- Jids].
feature_check_packet(allow, _User, Server, Pres, {#jid{lserver = LServer}, _To, {xmlelement, "message", _, _} = El}, in) ->
Host = host(Server),
case LServer of

View File

@ -2709,8 +2709,7 @@ event_stanza_withmoreels(Els, MoreEls) ->
%%%%%% broadcast functions
broadcast_publish_item(Host, Node, NodeId, Type, NodeOptions, Removed, ItemId, _From, Payload) ->
%broadcast(Host, Node, NodeId, NodeOptions, none, true, "items", ItemEls)
broadcast_publish_item(Host, Node, NodeId, Type, NodeOptions, Removed, ItemId, From, Payload) ->
case get_collection_subscriptions(Host, Node) of
SubsByDepth when is_list(SubsByDepth) ->
Content = case get_option(NodeOptions, deliver_payloads) of
@ -2720,7 +2719,7 @@ broadcast_publish_item(Host, Node, NodeId, Type, NodeOptions, Removed, ItemId, _
Stanza = event_stanza(
[{xmlelement, "items", nodeAttr(Node),
[{xmlelement, "item", itemAttr(ItemId), Content}]}]),
broadcast_stanza(Host, Node, NodeId, Type,
broadcast_stanza(Host, From, Node, NodeId, Type,
NodeOptions, SubsByDepth, items, Stanza, true),
case Removed of
[] ->
@ -2748,7 +2747,6 @@ broadcast_retract_items(Host, Node, NodeId, Type, NodeOptions, ItemIds) ->
broadcast_retract_items(_Host, _Node, _NodeId, _Type, _NodeOptions, [], _ForceNotify) ->
{result, false};
broadcast_retract_items(Host, Node, NodeId, Type, NodeOptions, ItemIds, ForceNotify) ->
%broadcast(Host, Node, NodeId, NodeOptions, notify_retract, ForceNotify, "retract", RetractEls)
case (get_option(NodeOptions, notify_retract) or ForceNotify) of
true ->
case get_collection_subscriptions(Host, Node) of
@ -2767,7 +2765,6 @@ broadcast_retract_items(Host, Node, NodeId, Type, NodeOptions, ItemIds, ForceNot
end.
broadcast_purge_node(Host, Node, NodeId, Type, NodeOptions) ->
%broadcast(Host, Node, NodeId, NodeOptions, notify_retract, false, "purge", [])
case get_option(NodeOptions, notify_retract) of
true ->
case get_collection_subscriptions(Host, Node) of
@ -2786,7 +2783,6 @@ broadcast_purge_node(Host, Node, NodeId, Type, NodeOptions) ->
end.
broadcast_removed_node(Host, Node, NodeId, Type, NodeOptions, SubsByDepth) ->
%broadcast(Host, Node, NodeId, NodeOptions, notify_delete, false, "delete", [])
case get_option(NodeOptions, notify_delete) of
true ->
case SubsByDepth of
@ -2805,7 +2801,6 @@ broadcast_removed_node(Host, Node, NodeId, Type, NodeOptions, SubsByDepth) ->
end.
broadcast_config_notification(Host, Node, NodeId, Type, NodeOptions, Lang) ->
%broadcast(Host, Node, NodeId, NodeOptions, notify_config, false, "items", ConfigEls)
case get_option(NodeOptions, notify_config) of
true ->
case get_collection_subscriptions(Host, Node) of
@ -2877,60 +2872,61 @@ broadcast_stanza(Host, _Node, _NodeId, _Type, NodeOptions, SubsByDepth, NotifyTy
[LJID]
end,
%% Determine if the stanza should have SHIM ('SubID' and 'name') headers
StanzaToSend = case SHIM of
true ->
Headers = lists:append(collection_shim(NodeName), subid_shim(SubIDs)),
add_headers(Stanza, Headers);
false ->
Stanza
end,
StanzaToSend = case {SHIM, SubIDs} of
{false, _} ->
Stanza;
%% If there's only one SubID, don't add it
{true, [_]} ->
add_shim_headers(Stanza, collection_shim(NodeName));
{true, SubIDs} ->
add_shim_headers(Stanza, lists:append(collection_shim(NodeName), subid_shim(SubIDs)))
end,
lists:foreach(fun(To) ->
ejabberd_router:route(From, jlib:make_jid(To), StanzaToSend)
end, LJIDs)
end, SubIDsByJID),
ejabberd_router:route(From, jlib:make_jid(To), StanzaToSend)
end, LJIDs)
end, SubIDsByJID).
broadcast_stanza({LUser, LServer, LResource}, Publisher, Node, NodeId, Type, NodeOptions, SubsByDepth, NotifyType, BaseStanza, SHIM) ->
broadcast_stanza({LUser, LServer, LResource}, Node, NodeId, Type, NodeOptions, SubsByDepth, NotifyType, BaseStanza, SHIM),
%% Handles implicit presence subscriptions
case Host of
{LUser, LServer, LResource} ->
SenderResource = case LResource of
[] ->
case user_resources(LUser, LServer) of
[Resource|_] -> Resource;
_ -> ""
end;
SenderResource = case LResource of
[] ->
case user_resources(LUser, LServer) of
[Resource|_] -> Resource;
_ -> ""
end;
_ ->
LResource
end,
case ejabberd_sm:get_session_pid(LUser, LServer, SenderResource) of
C2SPid when is_pid(C2SPid) ->
Stanza = case get_option(NodeOptions, notification_type, headline) of
normal -> BaseStanza;
MsgType -> add_message_type(BaseStanza, atom_to_list(MsgType))
end,
%% set the from address on the notification to the bare JID of the account owner
%% Also, add "replyto" if entity has presence subscription to the account owner
%% See XEP-0163 1.1 section 4.3.1
Sender = jlib:make_jid(LUser, LServer, ""),
ReplyTo = jlib:jid_to_string(Publisher),
StanzaToSend = add_extended_headers(Stanza, extended_headers([ReplyTo])),
case catch ejabberd_c2s:get_subscribed(C2SPid) of
Contacts when is_list(Contacts) ->
lists:foreach(fun({U, S, _}) ->
spawn(fun() ->
lists:foreach(fun(To) ->
ejabberd_router:route(Sender, jlib:make_jid(To), StanzaToSend)
end, [{U, S, R} || R <- user_resources(U, S)])
end)
end, Contacts);
_ ->
LResource
end,
case ejabberd_sm:get_session_pid(LUser, LServer, SenderResource) of
C2SPid when is_pid(C2SPid) ->
%% set the from address on the notification to the bare JID of the account owner
%% Also, add "replyto" if entity has presence subscription to the account owner
%% See XEP-0163 1.1 section 4.3.1
Sender = jlib:make_jid(LUser, LServer, ""),
%%ReplyTo = jlib:make_jid(LUser, LServer, SenderResource), % This has to be used
case catch ejabberd_c2s:get_subscribed(C2SPid) of
Contacts when is_list(Contacts) ->
lists:foreach(fun({U, S, _}) ->
spawn(fun() ->
LJIDs = lists:foldl(fun(R, Acc) ->
LJID = {U, S, R},
[LJID | Acc]
end, [], user_resources(U, S)),
lists:foreach(fun(To) ->
ejabberd_router:route(Sender, jlib:make_jid(To), Stanza)
end, LJIDs)
end)
end, Contacts);
_ ->
ok
end,
ok;
_ ->
?DEBUG("~p@~p has no session; can't deliver ~p to contacts", [LUser, LServer, Stanza]),
ok
end;
_ ->
ok
end.
?DEBUG("~p@~p has no session; can't deliver ~p to contacts", [LUser, LServer, BaseStanza])
end;
broadcast_stanza(_Host, _Publisher, _Node, _NodeId, _Type, _NodeOptions, _SubsByDepth, _NotifyType, _BaseStanza, _SHIM) ->
ok.
subscribed_nodes_by_jid(NotifyType, SubsByDepth) ->
NodesToDeliver = fun(Depth, Node, Subs, Acc) ->
@ -3595,8 +3591,14 @@ add_message_type(XmlEl, _Type) ->
%% "[SHIM Headers] SHOULD be included after the event notification information
%% (i.e., as the last child of the <message/> stanza)".
add_headers({xmlelement, Name, Attrs, Els}, HeaderEls) ->
HeaderEl = {xmlelement, "headers", [{"xmlns", ?NS_SHIM}], HeaderEls},
add_shim_headers(Stanza, HeaderEls) ->
add_headers(Stanza, "headers", ?NS_SHIM, HeaderEls).
add_extended_headers(Stanza, HeaderEls) ->
add_headers(Stanza, "addresses", ?NS_ADDRESS, HeaderEls).
add_headers({xmlelement, Name, Attrs, Els}, HeaderName, HeaderNS, HeaderEls) ->
HeaderEl = {xmlelement, HeaderName, [{"xmlns", HeaderNS}], HeaderEls},
{xmlelement, Name, Attrs, lists:append(Els, [HeaderEl])}.
%% Removed multiple <header name=Collection>Foo</header/> elements
@ -3620,6 +3622,13 @@ subid_shim(SubIDs) ->
[{xmlelement, "header", [{"name", "SubID"}],
[{xmlcdata, SubID}]} || SubID <- SubIDs].
%% The argument is a list of Jids because this function could be used
%% with the 'pubsub#replyto' (type=jid-multi) node configuration.
extended_headers(Jids) ->
[{xmlelement, "address", [{"type", "replyto"}, {"jid", Jid}], []} || Jid <- Jids].
feature_check_packet(allow, _User, Server, Pres, {#jid{lserver = LServer}, _To, {xmlelement, "message", _, _} = El}, in) ->
Host = host(Server),
case LServer of

View File

@ -1,5 +1,5 @@
--- mod_pubsub.erl 2010-03-05 11:51:24.000000000 +0100
+++ mod_pubsub_odbc.erl 2010-03-05 11:52:09.000000000 +0100
--- mod_pubsub.erl 2010-03-05 17:51:32.000000000 +0100
+++ mod_pubsub_odbc.erl 2010-03-05 17:51:57.000000000 +0100
@@ -42,7 +42,7 @@
%%% 6.2.3.1, 6.2.3.5, and 6.3. For information on subscription leases see
%%% XEP-0060 section 12.18.
@ -637,7 +637,7 @@
true ->
Result = lists:foldl(fun({JID, Subscription, SubId}, Acc) ->
@@ -3026,7 +2835,7 @@
@@ -3021,7 +2830,7 @@
{Depth, [{N, get_node_subs(N)} || N <- Nodes]}
end, tree_call(Host, get_parentnodes_tree, [Host, Node, service_jid(Host)]))}
end,
@ -646,7 +646,7 @@
{result, CollSubs} -> CollSubs;
_ -> []
end.
@@ -3040,9 +2849,9 @@
@@ -3035,9 +2844,9 @@
get_options_for_subs(NodeID, Subs) ->
lists:foldl(fun({JID, subscribed, SubID}, Acc) ->
@ -658,7 +658,7 @@
_ -> Acc
end;
(_, Acc) ->
@@ -3233,6 +3042,30 @@
@@ -3229,6 +3038,30 @@
Result
end.
@ -689,7 +689,7 @@
%% @spec (Host, Options) -> MaxItems
%% Host = host()
%% Options = [Option]
@@ -3628,7 +3461,13 @@
@@ -3624,7 +3457,13 @@
tree_action(Host, Function, Args) ->
?DEBUG("tree_action ~p ~p ~p",[Host,Function,Args]),
Fun = fun() -> tree_call(Host, Function, Args) end,
@ -704,7 +704,7 @@
%% @doc <p>node plugin call.</p>
node_call(Type, Function, Args) ->
@@ -3648,13 +3487,13 @@
@@ -3644,13 +3483,13 @@
node_action(Host, Type, Function, Args) ->
?DEBUG("node_action ~p ~p ~p ~p",[Host,Type,Function,Args]),
@ -720,7 +720,7 @@
case tree_call(Host, get_node, [Host, Node]) of
N when is_record(N, pubsub_node) ->
case Action(N) of
@@ -3667,8 +3506,14 @@
@@ -3663,8 +3502,14 @@
end
end, Trans).
@ -737,7 +737,7 @@
{result, Result} -> {result, Result};
{error, Error} -> {error, Error};
{atomic, {result, Result}} -> {result, Result};
@@ -3676,6 +3521,15 @@
@@ -3672,6 +3517,15 @@
{aborted, Reason} ->
?ERROR_MSG("transaction return internal error: ~p~n", [{aborted, Reason}]),
{error, ?ERR_INTERNAL_SERVER_ERROR};
@ -753,7 +753,7 @@
{'EXIT', Reason} ->
?ERROR_MSG("transaction return internal error: ~p~n", [{'EXIT', Reason}]),
{error, ?ERR_INTERNAL_SERVER_ERROR};
@@ -3684,6 +3538,17 @@
@@ -3680,6 +3534,17 @@
{error, ?ERR_INTERNAL_SERVER_ERROR}
end.