Software /
code /
prosody
Comparison
plugins/mod_console.lua @ 1502:0f895c06e03f
mod_console: Check for commands when not executing in the global environment
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Thu, 09 Jul 2009 04:34:55 +0100 |
parent | 1496:4fa337035f46 |
child | 1503:5970e06d9335 |
comparison
equal
deleted
inserted
replaced
1501:330b8437ac35 | 1502:0f895c06e03f |
---|---|
63 if data then | 63 if data then |
64 -- Handle data | 64 -- Handle data |
65 (function(session, data) | 65 (function(session, data) |
66 local useglobalenv; | 66 local useglobalenv; |
67 | 67 |
68 if data:match("[!.]$") then | 68 if data:match("^>") then |
69 data = data:gsub("^>", ""); | |
70 useglobalenv = true; | |
71 else | |
69 local command = data:lower(); | 72 local command = data:lower(); |
70 command = data:match("^%w+") or data:match("%p"); | 73 command = data:match("^%w+") or data:match("%p"); |
71 if commands[command] then | 74 if commands[command] then |
72 commands[command](session, data); | 75 commands[command](session, data); |
73 return; | 76 return; |
74 end | 77 end |
75 end | 78 end |
76 | 79 |
77 if data:match("^>") then | |
78 data = data:gsub("^>", ""); | |
79 useglobalenv = true; | |
80 end | |
81 | |
82 session.env._ = data; | 80 session.env._ = data; |
83 | 81 |
84 local chunk, err = loadstring("return "..data); | 82 local chunk, err = loadstring("return "..data); |
85 if not chunk then | 83 if not chunk then |
86 chunk, err = loadstring(data); | 84 chunk, err = loadstring(data); |
94 end | 92 end |
95 | 93 |
96 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil); | 94 setfenv(chunk, (useglobalenv and redirect_output(_G, session)) or session.env or nil); |
97 | 95 |
98 local ranok, taskok, message = pcall(chunk); | 96 local ranok, taskok, message = pcall(chunk); |
97 | |
98 if not (ranok or message or useglobalenv) and commands[data:lower()] then | |
99 commands[data:lower()](session, data); | |
100 return; | |
101 end | |
99 | 102 |
100 if not ranok then | 103 if not ranok then |
101 session.print("Fatal error while running command, it did not complete"); | 104 session.print("Fatal error while running command, it did not complete"); |
102 session.print("Error: "..taskok); | 105 session.print("Error: "..taskok); |
103 return; | 106 return; |