Comparison

mod_pubsub_subscription/mod_pubsub_subscription.lua @ 6119:6dca425eea15

mod_pubsub_subscription: Don't block pubsub unhandled or bare JID notifications The module was marking every successfully-parsed pubsub notification as handled, which could prevent local users from receiving pubsub notifications. Now it will only eat them if both: - The notification was sent to the host JID - The notification matched a node we consider ourselves subscribed to Notifications to local (i.e. non-host) JIDs will always be let through.
author Matthew Wild <mwild1@gmail.com>
date Sat, 28 Dec 2024 18:45:40 +0000
parent 6114:dc2cce03554d
comparison
equal deleted inserted replaced
6118:8338f42d2bc5 6119:6dca425eea15
50 end 50 end
51 51
52 for _, event_name in ipairs(valid_events) do 52 for _, event_name in ipairs(valid_events) do
53 module:hook("pubsub-event/host/"..event_name, function (event) 53 module:hook("pubsub-event/host/"..event_name, function (event)
54 for _, _, _, _, _, cb in active_subscriptions:iter(event.service, event.node, event.stanza.attr.to, nil, "on_"..event_name) do 54 for _, _, _, _, _, cb in active_subscriptions:iter(event.service, event.node, event.stanza.attr.to, nil, "on_"..event_name) do
55 event.handled = true;
55 pcall(cb, event); 56 pcall(cb, event);
56 end 57 end
57 end); 58 end);
58 59
59 module:hook("pubsub-event/bare/"..event_name, function (event) 60 module:hook("pubsub-event/bare/"..event_name, function (event)
60 for _, _, _, _, _, cb in active_subscriptions:iter(event.service, event.node, event.stanza.attr.to, nil, "on_"..event_name) do 61 for _, _, _, _, _, cb in active_subscriptions:iter(event.service, event.node, event.stanza.attr.to, nil, "on_"..event_name) do
62 event.handled = true;
61 pcall(cb, event); 63 pcall(cb, event);
62 end 64 end
63 end); 65 end);
64 end 66 end
65 67
130 132
131 module:handle_items("pubsub-subscription", subscription_added, subscription_removed, true); 133 module:handle_items("pubsub-subscription", subscription_added, subscription_removed, true);
132 134
133 function handle_message(context, event) 135 function handle_message(context, event)
134 local origin, stanza = event.origin, event.stanza; 136 local origin, stanza = event.origin, event.stanza;
135 local ret = nil; 137 local handled = nil;
136 local service = stanza.attr.from; 138 local service = stanza.attr.from;
137 module:log("debug", "Got message/%s: %s", context, stanza:top_tag()); 139 module:log("debug", "Got message/%s: %s", context, stanza:top_tag());
138 for event_container in stanza:childtags("event", xmlns_pubsub_event) do 140 for event_container in stanza:childtags("event", xmlns_pubsub_event) do
139 for pubsub_event in event_container:childtags() do 141 for pubsub_event in event_container:childtags() do
140 module:log("debug", "Got pubsub event %s", pubsub_event:top_tag()); 142 module:log("debug", "Got pubsub event %s", pubsub_event:top_tag());
141 local node = pubsub_event.attr.node; 143 local node = pubsub_event.attr.node;
142 module:fire_event("pubsub-event/" .. context .. "/"..pubsub_event.name, { 144 local event_data = {
143 stanza = stanza; 145 stanza = stanza;
144 origin = origin; 146 origin = origin;
145 event = pubsub_event; 147 event = pubsub_event;
146 service = service; 148 service = service;
147 node = node; 149 node = node;
148 }); 150 handled = false;
149 ret = true; 151 };
152 module:fire_event("pubsub-event/" .. context .. "/"..pubsub_event.name, event_data);
153 if not handled and event_data.handled then
154 handled = true;
155 end
150 end 156 end
151 end 157 end
152 return ret; 158 -- If not addressed to the host, let it fall through to normal handling
159 -- (it may be on its way to a local client), otherwise, we'll mark the
160 -- event as handled to suppress an error response if we handled it.
161 if context == "host" and handled then
162 return true;
163 end
153 end 164 end
154 165
155 module:hook("message/host", function(event) 166 module:hook("message/host", function(event)
156 return handle_message("host", event); 167 return handle_message("host", event);
157 end); 168 end);