From cac4ec8aca4ac49f1233d8dc16f0fa2fef531da8 Mon Sep 17 00:00:00 2001 From: Waqas Hussain Date: Wed, 10 Nov 2010 01:51:03 +0500 Subject: mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback. --- plugins/mod_component.lua | 49 +++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 41 insertions(+), 8 deletions(-) (limited to 'plugins') 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 -- cgit v1.2.3