Software / code / prosody
Comparison
plugins/mod_component.lua @ 3581:3f3f8227ba76
mod_component: Updated to use events for hooking stanzas instead of the component stanza handler, and the on_destroy callback.
| author | Waqas Hussain <waqas20@gmail.com> |
|---|---|
| date | Wed, 10 Nov 2010 01:51:03 +0500 |
| parent | 3579:9720fa5e0991 |
| child | 3587:d94aacb2771a |
comparison
equal
deleted
inserted
replaced
| 3580:39547152bb72 | 3581:3f3f8227ba76 |
|---|---|
| 20 local sha1 = require "util.hashes".sha1; | 20 local sha1 = require "util.hashes".sha1; |
| 21 local st = require "util.stanza"; | 21 local st = require "util.stanza"; |
| 22 | 22 |
| 23 local log = module._log; | 23 local log = module._log; |
| 24 | 24 |
| 25 local main_session, send; | |
| 26 | |
| 27 local function on_destroy(session, err) | |
| 28 if main_session == session then | |
| 29 main_session = nil; | |
| 30 send = nil; | |
| 31 session.on_destroy = nil; | |
| 32 hosts[session.host].connected = nil; | |
| 33 end | |
| 34 end | |
| 35 | |
| 36 local function handle_stanza(event) | |
| 37 local stanza = event.stanza; | |
| 38 if send then | |
| 39 stanza.attr.xmlns = nil; | |
| 40 send(stanza); | |
| 41 else | |
| 42 log("warn", "Stanza being handled by default component; bouncing error for: %s", stanza:top_tag()); | |
| 43 if stanza.attr.type ~= "error" and stanza.attr.type ~= "result" then | |
| 44 event.origin.send(st.error_reply(stanza, "wait", "service-unavailable", "Component unavailable")); | |
| 45 end | |
| 46 end | |
| 47 end | |
| 48 | |
| 49 module:hook("iq/bare", handle_stanza); | |
| 50 module:hook("message/bare", handle_stanza); | |
| 51 module:hook("presence/bare", handle_stanza); | |
| 52 module:hook("iq/full", handle_stanza); | |
| 53 module:hook("message/full", handle_stanza); | |
| 54 module:hook("presence/full", handle_stanza); | |
| 55 module:hook("iq/host", handle_stanza); | |
| 56 module:hook("message/host", handle_stanza); | |
| 57 module:hook("presence/host", handle_stanza); | |
| 58 | |
| 59 cm_register_component(module.host, function() end); | |
| 60 | |
| 25 --- Handle authentication attempts by components | 61 --- Handle authentication attempts by components |
| 26 function handle_component_auth(event) | 62 function handle_component_auth(event) |
| 27 local session, stanza = event.origin, event.stanza; | 63 local session, stanza = event.origin, event.stanza; |
| 28 | 64 |
| 29 if session.type ~= "component" then return; end | 65 if session.type ~= "component" then return; end |
| 66 if main_session == session then return; end | |
| 30 | 67 |
| 31 log("info", "Handling component auth"); | 68 log("info", "Handling component auth"); |
| 32 if (not session.host) or #stanza.tags > 0 then | 69 if (not session.host) or #stanza.tags > 0 then |
| 33 (session.log or log)("warn", "Component handshake invalid"); | 70 (session.log or log)("warn", "Component handshake invalid"); |
| 34 session:close("not-authorized"); | 71 session:close("not-authorized"); |
| 55 log("info", "Component authenticated: %s", session.host); | 92 log("info", "Component authenticated: %s", session.host); |
| 56 | 93 |
| 57 session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false; | 94 session.component_validate_from = module:get_option_boolean("validate_from_addresses") ~= false; |
| 58 | 95 |
| 59 -- If component not already created for this host, create one now | 96 -- If component not already created for this host, create one now |
| 60 if not hosts[session.host].connected then | 97 if not main_session then |
| 61 local send = session.send; | 98 send = session.send; |
| 62 session.component_session = cm_register_component(session.host, function (_, data) | 99 main_session = session; |
| 63 if data.attr and data.attr.xmlns == "jabber:client" then | 100 session.on_destroy = on_destroy; |
| 64 data.attr.xmlns = nil; | |
| 65 end | |
| 66 return send(data); | |
| 67 end); | |
| 68 hosts[session.host].connected = true; | 101 hosts[session.host].connected = true; |
| 69 log("info", "Component successfully registered"); | 102 log("info", "Component successfully registered"); |
| 70 else | 103 else |
| 71 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)"); | 104 log("error", "Multiple components bound to the same address, first one wins (TODO: Implement stanza distribution)"); |
| 72 session:close{ condition = "conflict", text = "Component already connected" }; | 105 session:close{ condition = "conflict", text = "Component already connected" }; |