Software / code / prosody
Comparison
core/modulemanager.lua @ 5776:bd0ff8ae98a8
Remove all trailing whitespace
| author | Florian Zeitz <florob@babelmonkeys.de> |
|---|---|
| date | Fri, 09 Aug 2013 17:48:21 +0200 |
| parent | 5411:82b3ddba0ec7 |
| child | 6003:28a90f5fea46 |
comparison
equal
deleted
inserted
replaced
| 5775:a6c2b8933507 | 5776:bd0ff8ae98a8 |
|---|---|
| 1 -- Prosody IM | 1 -- Prosody IM |
| 2 -- Copyright (C) 2008-2010 Matthew Wild | 2 -- Copyright (C) 2008-2010 Matthew Wild |
| 3 -- Copyright (C) 2008-2010 Waqas Hussain | 3 -- Copyright (C) 2008-2010 Waqas Hussain |
| 4 -- | 4 -- |
| 5 -- This project is MIT/X11 licensed. Please see the | 5 -- This project is MIT/X11 licensed. Please see the |
| 6 -- COPYING file in the source package for more information. | 6 -- COPYING file in the source package for more information. |
| 7 -- | 7 -- |
| 8 | 8 |
| 9 local logger = require "util.logger"; | 9 local logger = require "util.logger"; |
| 43 local modulemap = { ["*"] = {} }; | 43 local modulemap = { ["*"] = {} }; |
| 44 | 44 |
| 45 -- Load modules when a host is activated | 45 -- Load modules when a host is activated |
| 46 function load_modules_for_host(host) | 46 function load_modules_for_host(host) |
| 47 local component = config.get(host, "component_module"); | 47 local component = config.get(host, "component_module"); |
| 48 | 48 |
| 49 local global_modules_enabled = config.get("*", "modules_enabled"); | 49 local global_modules_enabled = config.get("*", "modules_enabled"); |
| 50 local global_modules_disabled = config.get("*", "modules_disabled"); | 50 local global_modules_disabled = config.get("*", "modules_disabled"); |
| 51 local host_modules_enabled = config.get(host, "modules_enabled"); | 51 local host_modules_enabled = config.get(host, "modules_enabled"); |
| 52 local host_modules_disabled = config.get(host, "modules_disabled"); | 52 local host_modules_disabled = config.get(host, "modules_disabled"); |
| 53 | 53 |
| 54 if host_modules_enabled == global_modules_enabled then host_modules_enabled = nil; end | 54 if host_modules_enabled == global_modules_enabled then host_modules_enabled = nil; end |
| 55 if host_modules_disabled == global_modules_disabled then host_modules_disabled = nil; end | 55 if host_modules_disabled == global_modules_disabled then host_modules_disabled = nil; end |
| 56 | 56 |
| 57 local global_modules = set.new(autoload_modules) + set.new(global_modules_enabled) - set.new(global_modules_disabled); | 57 local global_modules = set.new(autoload_modules) + set.new(global_modules_enabled) - set.new(global_modules_disabled); |
| 58 if component then | 58 if component then |
| 59 global_modules = set.intersection(set.new(component_inheritable_modules), global_modules); | 59 global_modules = set.intersection(set.new(component_inheritable_modules), global_modules); |
| 60 end | 60 end |
| 61 local modules = (global_modules + set.new(host_modules_enabled)) - set.new(host_modules_disabled); | 61 local modules = (global_modules + set.new(host_modules_enabled)) - set.new(host_modules_disabled); |
| 62 | 62 |
| 63 -- COMPAT w/ pre 0.8 | 63 -- COMPAT w/ pre 0.8 |
| 64 if modules:contains("console") then | 64 if modules:contains("console") then |
| 65 log("error", "The mod_console plugin has been renamed to mod_admin_telnet. Please update your config."); | 65 log("error", "The mod_console plugin has been renamed to mod_admin_telnet. Please update your config."); |
| 66 modules:remove("console"); | 66 modules:remove("console"); |
| 67 modules:add("admin_telnet"); | 67 modules:add("admin_telnet"); |
| 68 end | 68 end |
| 69 | 69 |
| 70 if component then | 70 if component then |
| 71 load(host, component); | 71 load(host, component); |
| 72 end | 72 end |
| 73 for module in modules do | 73 for module in modules do |
| 74 load(host, module); | 74 load(host, module); |
| 82 --- Private helpers --- | 82 --- Private helpers --- |
| 83 | 83 |
| 84 local function do_unload_module(host, name) | 84 local function do_unload_module(host, name) |
| 85 local mod = get_module(host, name); | 85 local mod = get_module(host, name); |
| 86 if not mod then return nil, "module-not-loaded"; end | 86 if not mod then return nil, "module-not-loaded"; end |
| 87 | 87 |
| 88 if module_has_method(mod, "unload") then | 88 if module_has_method(mod, "unload") then |
| 89 local ok, err = call_module_method(mod, "unload"); | 89 local ok, err = call_module_method(mod, "unload"); |
| 90 if (not ok) and err then | 90 if (not ok) and err then |
| 91 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); | 91 log("warn", "Non-fatal error unloading module '%s' on '%s': %s", name, host, err); |
| 92 end | 92 end |
| 93 end | 93 end |
| 94 | 94 |
| 95 for object, event, handler in mod.module.event_handlers:iter(nil, nil, nil) do | 95 for object, event, handler in mod.module.event_handlers:iter(nil, nil, nil) do |
| 96 object.remove_handler(event, handler); | 96 object.remove_handler(event, handler); |
| 97 end | 97 end |
| 98 | 98 |
| 99 if mod.module.items then -- remove items | 99 if mod.module.items then -- remove items |
| 100 local events = (host == "*" and prosody.events) or hosts[host].events; | 100 local events = (host == "*" and prosody.events) or hosts[host].events; |
| 101 for key,t in pairs(mod.module.items) do | 101 for key,t in pairs(mod.module.items) do |
| 102 for i = #t,1,-1 do | 102 for i = #t,1,-1 do |
| 103 local value = t[i]; | 103 local value = t[i]; |
| 115 if not (host and module_name) then | 115 if not (host and module_name) then |
| 116 return nil, "insufficient-parameters"; | 116 return nil, "insufficient-parameters"; |
| 117 elseif not hosts[host] and host ~= "*"then | 117 elseif not hosts[host] and host ~= "*"then |
| 118 return nil, "unknown-host"; | 118 return nil, "unknown-host"; |
| 119 end | 119 end |
| 120 | 120 |
| 121 if not modulemap[host] then | 121 if not modulemap[host] then |
| 122 modulemap[host] = hosts[host].modules; | 122 modulemap[host] = hosts[host].modules; |
| 123 end | 123 end |
| 124 | 124 |
| 125 if modulemap[host][module_name] then | 125 if modulemap[host][module_name] then |
| 126 log("warn", "%s is already loaded for %s, so not loading again", module_name, host); | 126 log("warn", "%s is already loaded for %s, so not loading again", module_name, host); |
| 127 return nil, "module-already-loaded"; | 127 return nil, "module-already-loaded"; |
| 128 elseif modulemap["*"][module_name] then | 128 elseif modulemap["*"][module_name] then |
| 129 local mod = modulemap["*"][module_name]; | 129 local mod = modulemap["*"][module_name]; |
| 145 end | 145 end |
| 146 return host_module; | 146 return host_module; |
| 147 end | 147 end |
| 148 return nil, "global-module-already-loaded"; | 148 return nil, "global-module-already-loaded"; |
| 149 end | 149 end |
| 150 | 150 |
| 151 | 151 |
| 152 | 152 |
| 153 local _log = logger.init(host..":"..module_name); | 153 local _log = logger.init(host..":"..module_name); |
| 154 local api_instance = setmetatable({ name = module_name, host = host, | 154 local api_instance = setmetatable({ name = module_name, host = host, |
| 155 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable(), | 155 _log = _log, log = function (self, ...) return _log(...); end, event_handlers = new_multitable(), |
| 156 reloading = not not state, saved_state = state~=true and state or nil } | 156 reloading = not not state, saved_state = state~=true and state or nil } |
| 157 , { __index = api }); | 157 , { __index = api }); |
| 158 | 158 |
| 159 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); | 159 local pluginenv = setmetatable({ module = api_instance }, { __index = _G }); |
| 160 api_instance.environment = pluginenv; | 160 api_instance.environment = pluginenv; |
| 161 | 161 |
| 162 local mod, err = pluginloader.load_code(module_name, nil, pluginenv); | 162 local mod, err = pluginloader.load_code(module_name, nil, pluginenv); |
| 163 if not mod then | 163 if not mod then |
| 164 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil"); | 164 log("error", "Unable to load module '%s': %s", module_name or "nil", err or "nil"); |
| 165 return nil, err; | 165 return nil, err; |
| 166 end | 166 end |