Was a bit hasty to remove send_s2s() from stanza_router. We still use it, and there...
[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         if not (origin_type and handler and xmlns) then return false; end
23         handlers[origin_type] = handlers[origin_type] or {};
24         handlers[origin_type].iq = handlers[origin_type].iq or {};
25         if not handlers[origin_type].iq[xmlns] then
26                 handlers[origin_type].iq[xmlns]= handler;
27                 handler_info[handler] = getfenv(2).module;
28                 log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", getfenv(2).module.name, xmlns);
29         else
30                 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);
31         end
32 end
33
34 function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
35         if not (origin_type and tag and xmlns and handler) then return false; end
36         handlers[origin_type] = handlers[origin_type] or {};
37         if not handlers[origin_type][tag] then
38                 handlers[origin_type][tag] = handlers[origin_type][tag] or {};
39                 handlers[origin_type][tag][xmlns]= handler;
40                 handler_info[handler] = getfenv(2).module;
41                 log("debug", "mod_%s now handles tag '%s'", getfenv(2).module.name, tag);
42         elseif handler_info[handlers[origin_type][tag]] then
43                 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);
44         end
45 end
46
47 function loadall()
48         load("saslauth");
49         load("legacyauth");
50         load("roster");
51         load("register");
52         load("tls");
53         load("vcard");
54         load("private");
55 end
56
57 function load(name)
58         local mod, err = loadfile("plugins/mod_"..name..".lua");
59         if not mod then
60                 log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
61                 return;
62         end
63         
64         local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
65         
66         setfenv(mod, pluginenv);
67         local success, ret = pcall(mod);
68         if not success then
69                 log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
70                 return;
71         end
72 end
73
74 function handle_stanza(origin, stanza)
75         local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
76         
77         if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
78                 log("debug", "Stanza is an <iq/>");
79                 local child = stanza.tags[1];
80                 if child then
81                         local xmlns = child.attr.xmlns;
82                         log("debug", "Stanza has xmlns: %s", xmlns);
83                         local handler = handlers[origin_type][name][xmlns];
84                         if  handler then
85                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
86                                 return handler(origin, stanza) or true;
87                         end
88
89                 end
90         elseif handlers[origin_type] then
91                 local handler = handlers[origin_type][name];
92                 if  handler then
93                         handler = handler[xmlns];
94                         if handler then
95                                 log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
96                                 return handler(origin, stanza) or true;
97                         end
98                 end
99         end
100         log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
101         return false; -- we didn't handle it
102 end
103
104 do
105         local event_handlers = {};
106         
107         function modulehelpers.add_event_hook(name, handler)
108                 if not event_handlers[name] then
109                         event_handlers[name] = {};
110                 end
111                 t_insert(event_handlers[name] , handler);
112         end
113         
114         function fire_event(name, ...)
115                 local event_handlers = event_handlers[name];
116                 if event_handlers then
117                         for name, handler in ipairs(event_handlers) do
118                                 handler(...);
119                         end
120                 end
121         end
122 end
123
124 return _M;