Move stream opening handling from xmlhandlers to sessionmanager
[prosody.git] / core / modulemanager.lua
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 t_insert = table.insert;
8 local type = type;
9
10 local tostring, print = tostring, print;
11
12 local _G = _G;
13
14 module "modulemanager"
15
16 local handler_info = {};
17 local handlers = {};
18                                         
19 local modulehelpers = setmetatable({}, { __index = _G });
20
21 function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
22         handlers[origin_type] = handlers[origin_type] or {};
23         handlers[origin_type].iq = handlers[origin_type].iq or {};
24         if not handlers[origin_type].iq[xmlns] then
25                 handlers[origin_type].iq[xmlns]= handler;
26                 handler_info[handler] = getfenv(2).module;
27                 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", getfenv(2).module.name, xmlns);
28         else
29                 log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", getfenv(2).module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name);
30         end
31 end
32
33 function modulehelpers.add_handler(origin_type, tag, handler)
34         handlers[origin_type] = handlers[origin_type] or {};
35         if not handlers[origin_type][tag] then
36                 handlers[origin_type][tag]= handler;
37                 handler_info[handler] = getfenv(2).module;
38                 log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag);
39         elseif handler_info[handlers[origin_type][tag]] then
40                 log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", getfenv(2).module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
41         end
42 end
43                                         
44 function loadall()
45         load("saslauth");
46         load("legacyauth");
47         load("roster");
48 end
49
50 function load(name)
51         local mod, err = loadfile("plugins/mod_"..name..".lua");
52         if not mod then
53                 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
54                 return;
55         end
56         
57         local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
58         
59         setfenv(mod, pluginenv);
60         local success, ret = pcall(mod);
61         if not success then
62                 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
63                 return;
64         end
65 end
66
67 function handle_stanza(origin, stanza)
68         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
69         
70         if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
71                 log("debug", "Stanza is an <iq/>");
72                 local child = stanza.tags[1];
73                 if child then
74                         local xmlns = child.attr.xmlns;
75                         log("debug", "Stanza has xmlns: %s", xmlns);
76                         local handler = handlers[origin_type][name][xmlns];
77                         if  handler then
78                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
79                                 return handler(origin, stanza) or true;
80                         end
81
82                 end
83                 --FIXME: All iq's must be replied to, here we should return service-unavailable I think
84         elseif handlers[origin_type] then
85                 local handler = handlers[origin_type][name];
86                 if  handler then
87                         log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
88                         return handler(origin, stanza) or true;
89                 end
90         end
91         log("debug", "Stanza unhandled by any modules");
92         return false; -- we didn't handle it
93 end
94
95 do
96         local event_handlers = {};
97         
98         function modulehelpers.add_event_hook(name, handler)
99                 if not event_handlers[name] then
100                         event_handlers[name] = {};
101                 end
102                 t_insert(event_handlers[name] , handler);
103         end
104         
105         function fire_event(name, ...)
106                 local event_handlers = event_handlers[name];
107                 if event_handlers then
108                         for name, handler in ipairs(event_handlers) do
109                                 handler(...);
110                         end
111                 end
112         end
113 end
114
115 return _M;