Comparison

plugins/mod_console.lua @ 1317:f6e56a555c37

mod_console: Allow running code in the global environment by prefixing with '>'
author Matthew Wild <mwild1@gmail.com>
date Fri, 05 Jun 2009 20:18:55 +0100
parent 1316:28ae044f1aaf
child 1322:33d103b0283f
comparison
equal deleted inserted replaced
1316:28ae044f1aaf 1317:f6e56a555c37
54 printbanner(session); 54 printbanner(session);
55 end 55 end
56 if data then 56 if data then
57 -- Handle data 57 -- Handle data
58 (function(session, data) 58 (function(session, data)
59 local useglobalenv;
60
59 if data:match("[!.]$") then 61 if data:match("[!.]$") then
60 local command = data:lower(); 62 local command = data:lower();
61 command = data:match("^%w+") or data:match("%p"); 63 command = data:match("^%w+") or data:match("%p");
62 if commands[command] then 64 if commands[command] then
63 commands[command](session, data); 65 commands[command](session, data);
64 return; 66 return;
65 end 67 end
68 end
69
70 if data:match("^>") then
71 data = data:gsub("^>", "");
72 useglobalenv = true;
66 end 73 end
67 74
68 session.env._ = data; 75 session.env._ = data;
69 76
70 local chunk, err = loadstring("return "..data); 77 local chunk, err = loadstring("return "..data);
77 session.print("Sorry, I couldn't understand that... "..err); 84 session.print("Sorry, I couldn't understand that... "..err);
78 return; 85 return;
79 end 86 end
80 end 87 end
81 88
82 setfenv(chunk, session.env); 89 setfenv(chunk, (useglobalenv and _G) or session.env or nil);
90
83 local ranok, taskok, message = pcall(chunk); 91 local ranok, taskok, message = pcall(chunk);
84 92
85 if not ranok then 93 if not ranok then
86 session.print("Fatal error while running command, it did not complete"); 94 session.print("Fatal error while running command, it did not complete");
87 session.print("Error: "..taskok); 95 session.print("Error: "..taskok);