Mercurial > prosody-hg
comparison plugins/mod_console.lua @ 1315:bfcd3f0a49df
mod_console: Much improved module load/unload/reload commands
| author | Matthew Wild <mwild1@gmail.com> |
|---|---|
| date | Fri, 05 Jun 2009 19:57:29 +0100 |
| parents | 9c53fb182044 |
| children | 28ae044f1aaf |
comparison
equal
deleted
inserted
replaced
| 1314:c18cf7ccc2de | 1315:bfcd3f0a49df |
|---|---|
| 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 module.host = "*"; | 9 module.host = "*"; |
| 10 | 10 |
| 11 local hosts = _G.hosts; | 11 local prosody = _G.prosody; |
| 12 local hosts = prosody.hosts; | |
| 12 local connlisteners_register = require "net.connlisteners".register; | 13 local connlisteners_register = require "net.connlisteners".register; |
| 13 | 14 |
| 14 local console_listener = { default_port = 5582; default_mode = "*l"; }; | 15 local console_listener = { default_port = 5582; default_mode = "*l"; }; |
| 16 | |
| 17 require "util.iterators"; | |
| 18 local set, array = require "util.set", require "util.array"; | |
| 15 | 19 |
| 16 local commands = {}; | 20 local commands = {}; |
| 17 local def_env = {}; | 21 local def_env = {}; |
| 18 local default_env_mt = { __index = def_env }; | 22 local default_env_mt = { __index = def_env }; |
| 19 | 23 |
| 139 dofile "prosody" | 143 dofile "prosody" |
| 140 return true, "Server reloaded"; | 144 return true, "Server reloaded"; |
| 141 end | 145 end |
| 142 | 146 |
| 143 def_env.module = {}; | 147 def_env.module = {}; |
| 144 function def_env.module:load(name, host, config) | 148 |
| 149 local function get_hosts_set(hosts) | |
| 150 if type(hosts) == "table" then | |
| 151 if hosts[1] then | |
| 152 return set.new(hosts); | |
| 153 elseif hosts._items then | |
| 154 return hosts; | |
| 155 end | |
| 156 elseif type(hosts) == "string" then | |
| 157 return set.new { hosts }; | |
| 158 elseif hosts == nil then | |
| 159 return set.new(array.collect(keys(prosody.hosts))) | |
| 160 / function (host) return prosody.hosts[host].type == "local"; end; | |
| 161 end | |
| 162 end | |
| 163 | |
| 164 function def_env.module:load(name, hosts, config) | |
| 145 local mm = require "modulemanager"; | 165 local mm = require "modulemanager"; |
| 146 local ok, err = mm.load(host or self.env.host, name, config); | 166 |
| 147 if not ok then | 167 hosts = get_hosts_set(hosts); |
| 148 return false, err or "Unknown error loading module"; | 168 |
| 149 end | 169 -- Load the module for each host |
| 150 return true, "Module loaded"; | 170 local ok, err, count = true, nil, 0; |
| 151 end | 171 for host in hosts do |
| 152 | 172 if (not mm.is_loaded(host, name)) then |
| 153 function def_env.module:unload(name, host) | 173 ok, err = mm.load(host, name, config); |
| 174 if not ok then | |
| 175 ok = false; | |
| 176 self.session.print(err or "Unknown error loading module"); | |
| 177 else | |
| 178 count = count + 1; | |
| 179 self.session.print("Loaded for "..host); | |
| 180 end | |
| 181 end | |
| 182 end | |
| 183 | |
| 184 return ok, (ok and "Module loaded onto "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); | |
| 185 end | |
| 186 | |
| 187 function def_env.module:unload(name, hosts) | |
| 154 local mm = require "modulemanager"; | 188 local mm = require "modulemanager"; |
| 155 local ok, err = mm.unload(host or self.env.host, name); | 189 |
| 156 if not ok then | 190 hosts = get_hosts_set(hosts); |
| 157 return false, err or "Unknown error unloading module"; | 191 |
| 158 end | 192 -- Unload the module for each host |
| 159 return true, "Module unloaded"; | 193 local ok, err, count = true, nil, 0; |
| 160 end | 194 for host in hosts do |
| 161 | 195 if mm.is_loaded(host, name) then |
| 162 function def_env.module:reload(name, host) | 196 ok, err = mm.unload(host, name); |
| 197 if not ok then | |
| 198 ok = false; | |
| 199 self.session.print(err or "Unknown error unloading module"); | |
| 200 else | |
| 201 count = count + 1; | |
| 202 self.session.print("Unloaded from "..host); | |
| 203 end | |
| 204 end | |
| 205 end | |
| 206 return ok, (ok and "Module unloaded from "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); | |
| 207 end | |
| 208 | |
| 209 function def_env.module:reload(name, hosts) | |
| 163 local mm = require "modulemanager"; | 210 local mm = require "modulemanager"; |
| 164 local ok, err = mm.reload(host or self.env.host, name); | 211 |
| 165 if not ok then | 212 hosts = get_hosts_set(hosts); |
| 166 return false, err or "Unknown error reloading module"; | 213 |
| 167 end | 214 -- Reload the module for each host |
| 168 return true, "Module reloaded"; | 215 local ok, err, count = true, nil, 0; |
| 216 for host in hosts do | |
| 217 if mm.is_loaded(host, name) then | |
| 218 ok, err = mm.reload(host, name); | |
| 219 if not ok then | |
| 220 ok = false; | |
| 221 self.session.print(err or "Unknown error reloading module"); | |
| 222 else | |
| 223 count = count + 1; | |
| 224 if ok == nil then | |
| 225 ok = true; | |
| 226 end | |
| 227 self.session.print("Reloaded on "..host); | |
| 228 end | |
| 229 end | |
| 230 end | |
| 231 return ok, (ok and "Module reloaded on "..count.." host"..(count ~= 1 and "s" or "")) or ("Last error: "..tostring(err)); | |
| 169 end | 232 end |
| 170 | 233 |
| 171 def_env.config = {}; | 234 def_env.config = {}; |
| 172 function def_env.config:load(filename, format) | 235 function def_env.config:load(filename, format) |
| 173 local config_load = require "core.configmanager".load; | 236 local config_load = require "core.configmanager".load; |
