aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorWaqas Hussain <waqas20@gmail.com>2010-11-10 01:51:03 +0500
committerWaqas Hussain <waqas20@gmail.com>2010-11-10 01:51:03 +0500
commitcac4ec8aca4ac49f1233d8dc16f0fa2fef531da8 (patch)
tree47b3a493ed290603062932a50f74db7b4335ff2a /plugins
parent87615e95fc4f18cefa4e9f31255db3140340ec77 (diff)
downloadprosody-cac4ec8aca4ac49f1233d8dc16f0fa2fef531da8.tar.gz
prosody-cac4ec8aca4ac49f1233d8dc16f0fa2fef531da8.zip
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
Diffstat (limited to 'plugins')
-rw-r--r--plugins/mod_component.lua49
1 files changed, 41 insertions, 8 deletions
diff --git a/plugins/mod_component.lua b/plugins/mod_component.lua
index 996a983e..3caee1ea 100644
--- a/plugins/mod_component.lua
+++ b/plugins/mod_component.lua
@@ -22,11 +22,48 @@ local st = require "util.stanza";
local log = module._log;
+local main_session, send;
+
+local function on_destroy(session, err)
+ if main_session == session then
+ main_session = nil;
+ send = nil;
+ session.on_destroy = nil;
+ hosts[session.host].connected = nil;
+ end
+end
+
+local function handle_stanza(event)
+ local stanza = event.stanza;
+ if send then
+ stanza.attr.xmlns = nil;
+ send(stanza);
+ else
+ log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag());
+ if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then
+ event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable"));
+ end
+ end
+end
+
+module:hook("iq/bare", handle_stanza);
+module:hook("message/bare", handle_stanza);
+module:hook("presence/bare", handle_stanza);
+module:hook("iq/full", handle_stanza);
+module:hook("message/full", handle_stanza);
+module:hook("presence/full", handle_stanza);
+module:hook("iq/host", handle_stanza);
+module:hook("message/host", handle_stanza);
+module:hook("presence/host", handle_stanza);
+
+cm_register_component(module.host, function() end);
+
--- Handle authentication attempts by components
function handle_component_auth(event)
local session, stanza = event.origin, event.stanza;
if session.type ~= "component" then return; end
+ if main_session == session then return; end
log("info", "Handling component auth");
if (not session.host) or #stanza.tags > 0 then
@@ -57,14 +94,10 @@ function handle_component_auth(event)
session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false;
-- If component not already created for this host, create one now
- if not hosts[session.host].connected then
- local send = session.send;
- session.component_session = cm_register_component(session.host, function (_, data)
- if data.attr and data.attr.xmlns == "jabber:client" then
- data.attr.xmlns = nil;
- end
- return send(data);
- end);
+ if not main_session then
+ send = session.send;
+ main_session = session;
+ session.on_destroy = on_destroy;
hosts[session.host].connected = true;
log("info", "Component successfully registered");
else