core/modulemanager.lua
author Matthew Wild <mwild1@gmail.com>
Sun, 02 Nov 2008 01:28:27 +0000
changeset 197 19c57a24afa1
parent 196 ebe23269b377
child 198 e4755408d40b
permissions -rw-r--r--
Fix for previous commit
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
local log = require "util.logger".init("modulemanager")
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
local loadfile, pcall = loadfile, pcall;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
local setmetatable, setfenv, getfenv = setmetatable, setfenv, getfenv;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
local pairs, ipairs = pairs, ipairs;
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
     7
local t_insert = table.insert;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
local type = type;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
local tostring, print = tostring, print;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
local _G = _G;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
module "modulemanager"
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
local handler_info = {};
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
local handlers = {};
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
					
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
local modulehelpers = setmetatable({}, { __index = _G });
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
196
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    21
local function _add_iq_handler(module, origin_type, xmlns, handler)
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
	handlers[origin_type] = handlers[origin_type] or {};
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
	handlers[origin_type].iq = handlers[origin_type].iq or {};
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
	if not handlers[origin_type].iq[xmlns] then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
		handlers[origin_type].iq[xmlns]= handler;
196
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    26
		handler_info[handler] = module;
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    27
		log("debug", "mod_%s now handles tag 'iq' with query namespace '%s'", module.name, xmlns);
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	else
196
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    29
		log("warning", "mod_%s wants to handle tag 'iq' with query namespace '%s' but mod_%s already handles that", module.name, xmlns, handler_info[handlers[origin_type].iq[xmlns]].module.name);
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
	end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
196
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    33
function modulehelpers.add_iq_handler(origin_type, xmlns, handler)
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    34
	if not (origin_type and handler and xmlns) then return false; end
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    35
	if type(origin_type) == "table" then
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    36
		for _, origin_type in ipairs(origin_type) do
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    37
			_add_iq_handler(getfenv(2), origin_type, xmlns, handler);
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    38
		end
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    39
		return;
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    40
	end
197
19c57a24afa1 Fix for previous commit
Matthew Wild <mwild1@gmail.com>
parents: 196
diff changeset
    41
	_add_iq_handler(getfenv(2).module, origin_type, xmlns, handler);
196
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    42
end
ebe23269b377 Fix for add_iq_handler to allow multiple origin types too
Matthew Wild <mwild1@gmail.com>
parents: 191
diff changeset
    43
191
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    44
local function _add_handler(module, origin_type, tag, xmlns, handler)
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    45
	handlers[origin_type] = handlers[origin_type] or {};
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    46
	if not handlers[origin_type][tag] then
47
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
    47
		handlers[origin_type][tag] = handlers[origin_type][tag] or {};
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
    48
		handlers[origin_type][tag][xmlns]= handler;
191
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    49
		handler_info[handler] = module;
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    50
		log("debug", "mod_%s now handles tag '%s'", module.name, tag);
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    51
	elseif handler_info[handlers[origin_type][tag]] then
191
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    52
		log("warning", "mod_%s wants to handle tag '%s' but mod_%s already handles that", module.name, tag, handler_info[handlers[origin_type][tag]].module.name);
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    53
	end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
end
47
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
    55
191
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    56
function modulehelpers.add_handler(origin_type, tag, xmlns, handler)
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    57
	if not (origin_type and tag and xmlns and handler) then return false; end
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    58
	if type(origin_type) == "table" then
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    59
		for _, origin_type in ipairs(origin_type) do
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    60
			_add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    61
		end
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    62
		return;
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    63
	end
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    64
	_add_handler(getfenv(2).module, origin_type, tag, xmlns, handler);
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    65
end
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    66
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
function loadall()
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    68
	load("saslauth");
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
	load("legacyauth");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
	load("roster");
63
4c27740fdeff mod_InBandRegistration -> mod_register
Matthew Wild <mwild1@gmail.com>
parents: 60
diff changeset
    71
	load("register");
65
9c471840acb9 TLS: Handshake works, no data after that
Matthew Wild <mwild1@gmail.com>
parents: 63
diff changeset
    72
	load("tls");
86
a2085854c72c Added: vCard plugin: mod_vcard
Waqas Hussain <waqas20@gmail.com>
parents: 65
diff changeset
    73
	load("vcard");
185
a67c88ce1c6a Added support for XEP-0049: Private XML Storage (mod_private)
Waqas Hussain <waqas20@gmail.com>
parents: 86
diff changeset
    74
	load("private");
188
0d9f03009b8a mod_version is here
Matthew Wild <mwild1@gmail.com>
parents: 185
diff changeset
    75
	load("version");
191
e64c8a44060f Fix s2s once and for all
Matthew Wild <mwild1@gmail.com>
parents: 188
diff changeset
    76
	load("dialback");
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
function load(name)
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
	local mod, err = loadfile("plugins/mod_"..name..".lua");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
	if not mod then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
		log("error", "Unable to load module '%s': %s", name or "nil", err or "nil");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
		return;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
	end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
	
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
	local pluginenv = setmetatable({ module = { name = name } }, { __index = modulehelpers });
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
	
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
	setfenv(mod, pluginenv);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
	local success, ret = pcall(mod);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
	if not success then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
		log("error", "Error initialising module '%s': %s", name or "nil", ret or "nil");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
		return;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
	end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
function handle_stanza(origin, stanza)
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    97
	local name, xmlns, origin_type = stanza.name, stanza.attr.xmlns, origin.type;
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
	
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
    99
	if name == "iq" and xmlns == "jabber:client" and handlers[origin_type] then
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
		log("debug", "Stanza is an <iq/>");
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
		local child = stanza.tags[1];
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
		if child then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
			local xmlns = child.attr.xmlns;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
			log("debug", "Stanza has xmlns: %s", xmlns);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
			local handler = handlers[origin_type][name][xmlns];
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
			if  handler then
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
				log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
				return handler(origin, stanza) or true;
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
			end
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
		end
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
   112
	elseif handlers[origin_type] then
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
   113
		local handler = handlers[origin_type][name];
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
   114
		if  handler then
47
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
   115
			handler = handler[xmlns];
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
   116
			if handler then
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
   117
				log("debug", "Passing stanza to mod_%s", handler_info[handler].name);
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
   118
				return handler(origin, stanza) or true;
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
   119
			end
38
Matthew Wild <mwild1@gmail.com>
parents: 30
diff changeset
   120
		end
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
	end
47
33ed4c6ac249 Fix stanza handlers to use xmlns also for matching
Matthew Wild <mwild1@gmail.com>
parents: 42
diff changeset
   122
	log("debug", "Stanza unhandled by any modules, xmlns: %s", stanza.attr.xmlns);
30
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
	return false; -- we didn't handle it
bcf539295f2d Huge commit to:
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
end
39
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   125
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   126
do
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   127
	local event_handlers = {};
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   128
	
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   129
	function modulehelpers.add_event_hook(name, handler)
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   130
		if not event_handlers[name] then
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   131
			event_handlers[name] = {};
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   132
		end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   133
		t_insert(event_handlers[name] , handler);
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   134
	end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   135
	
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   136
	function fire_event(name, ...)
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   137
		local event_handlers = event_handlers[name];
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   138
		if event_handlers then
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   139
			for name, handler in ipairs(event_handlers) do
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   140
				handler(...);
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   141
			end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   142
		end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   143
	end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   144
end
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   145
89877d61ac51 Add support for arbitrary events and event hooks
Matthew Wild <mwild1@gmail.com>
parents: 38
diff changeset
   146
return _M;