Software /
code /
prosody
Comparison
plugins/mod_console.lua @ 1342:947d94e3619f
mod_console: Redirect print() to console session when executing commands in global environment
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Fri, 12 Jun 2009 15:43:24 +0100 |
parent | 1341:53decd1ee351 |
child | 1433:e7bd00e70973 |
comparison
equal
deleted
inserted
replaced
1341:53decd1ee351 | 1342:947d94e3619f |
---|---|
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 _G = _G; | |
12 | |
11 local prosody = _G.prosody; | 13 local prosody = _G.prosody; |
12 local hosts = prosody.hosts; | 14 local hosts = prosody.hosts; |
13 local connlisteners_register = require "net.connlisteners".register; | 15 local connlisteners_register = require "net.connlisteners".register; |
14 | 16 |
15 local console_listener = { default_port = 5582; default_mode = "*l"; }; | 17 local console_listener = { default_port = 5582; default_mode = "*l"; }; |
18 local set, array = require "util.set", require "util.array"; | 20 local set, array = require "util.set", require "util.array"; |
19 | 21 |
20 local commands = {}; | 22 local commands = {}; |
21 local def_env = {}; | 23 local def_env = {}; |
22 local default_env_mt = { __index = def_env }; | 24 local default_env_mt = { __index = def_env }; |
25 | |
26 local function redirect_output(_G, session) | |
27 return setmetatable({ print = session.print }, { __index = function (t, k) return rawget(_G, k); end, __newindex = function (t, k, v) rawset(_G, k, v); end }); | |
28 end | |
23 | 29 |
24 console = {}; | 30 console = {}; |
25 | 31 |
26 function console:new_session(conn) | 32 function console:new_session(conn) |
27 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end; | 33 local w = function(s) conn.write(s:gsub("\n", "\r\n")); end; |
84 session.print("Sorry, I couldn't understand that... "..err); | 90 session.print("Sorry, I couldn't understand that... "..err); |
85 return; | 91 return; |
86 end | 92 end |
87 end | 93 end |
88 | 94 |
89 setfenv(chunk, (useglobalenv and _G) or session.env or nil); | 95 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil); |
90 | 96 |
91 local ranok, taskok, message = pcall(chunk); | 97 local ranok, taskok, message = pcall(chunk); |
92 | 98 |
93 if not ranok then | 99 if not ranok then |
94 session.print("Fatal error while running command, it did not complete"); | 100 session.print("Fatal error while running command, it did not complete"); |