Software / code / prosody
Comparison
core/modulemanager.lua @ 30:bcf539295f2d
Huge commit to:
* Break stanza routing (to be restored in a future commit)
* Remove the old stanza_dispatcher code, which was never going to be maintainable nor extendable :)
* Bring us plugins, starting with mod_legacyauth and mod_roster
* Sessions are now created/destroyed using a standard sessionmanager interface
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Tue, 30 Sep 2008 19:52:00 +0100 |
| child | 38:3fdfd6e0cb4e |
comparison
equal
deleted
inserted
replaced
| 29:b847875801e5 | 30:bcf539295f2d |
|---|---|
| 1 | |
| 2 local log = require "util.logger".init("modulemanager") | |
| 3 | |
| 4 local loadfile, pcall = loadfile, pcall; | |
| 5 local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv; | |
| 6 local pairs, ipairs = pairs, ipairs; | |
| 7 local type = type; | |
| 8 | |
| 9 local tostring, print = tostring, print; | |
| 10 | |
| 11 local _G = _G; | |
| 12 | |
| 13 module "modulemanager" | |
| 14 | |
| 15 local handler_info = {}; | |
| 16 local handlers = {}; | |
| 17 | |
| 18 local modulehelpers = setmetatable({}, { __index = _G }); | |
| 19 | |
| 20 function modulehelpers.add_iq_handler(origin_type, xmlns, handler) | |
| 21 handlers[origin_type] = handlers[origin_type] or {}; | |
| 22 handlers[origin_type].iq = handlers[origin_type].iq or {}; | |
| 23 if not handlers[origin_type].iq[xmlns] then | |
| 24 handlers[origin_type].iq[xmlns]= handler; | |
| 25 handler_info[handler] = getfenv(2).module; | |
| 26 log("debug", "mod_%s now handles iq,%s", getfenv(2).module.name, xmlns); | |
| 27 else | |
| 28 log("warning", "mod_%s wants to handle iq,%s but mod_%s already handles that", getfenv(2).module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name); | |
| 29 end | |
| 30 end | |
| 31 | |
| 32 function modulehelpers.add_presence_handler(origin_type, handler) | |
| 33 end | |
| 34 | |
| 35 function modulehelpers.add_message_handler(origin_type, handler) | |
| 36 end | |
| 37 | |
| 38 function loadall() | |
| 39 load("legacyauth"); | |
| 40 load("roster"); | |
| 41 end | |
| 42 | |
| 43 function load(name) | |
| 44 local mod, err = loadfile("plugins/mod_"..name..".lua"); | |
| 45 if not mod then | |
| 46 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil"); | |
| 47 return; | |
| 48 end | |
| 49 | |
| 50 local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers }); | |
| 51 | |
| 52 setfenv(mod, pluginenv); | |
| 53 local success, ret = pcall(mod); | |
| 54 if not success then | |
| 55 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil"); | |
| 56 return; | |
| 57 end | |
| 58 end | |
| 59 | |
| 60 function handle_stanza(origin, stanza) | |
| 61 local name, origin_type = stanza.name, origin.type; | |
| 62 | |
| 63 if name == "iq" then | |
| 64 log("debug", "Stanza is an <iq/>"); | |
| 65 local child = stanza.tags[1]; | |
| 66 if child then | |
| 67 local xmlns = child.attr.xmlns; | |
| 68 log("debug", "Stanza has xmlns: %s", xmlns); | |
| 69 local handler = handlers[origin_type][name][xmlns]; | |
| 70 if handler then | |
| 71 log("debug", "Passing stanza to mod_%s", handler_info[handler].name); | |
| 72 return handler(origin, stanza) or true; | |
| 73 end | |
| 74 | |
| 75 end | |
| 76 end | |
| 77 log("debug", "Stanza unhandled by any modules"); | |
| 78 return false; -- we didn't handle it | |
| 79 end |