mun/plugins/repl.lua

62 lines
1.6 KiB
Lua

local function DeepCopy(t)
local Copied = {}
local Result = {}
local function Internal(Out, In)
for K, V in pairs(In) do
local Type = type(V)
if Type == "string" or Type == "function" or Type == "number" then
Out[K] = V
elseif Type == "table" then
if Copied[V] ~= nil then
Out[K] = Copied[V]
else
Copied[V] = {}
Internal(Copied[V], V)
Out[K] = Copied[V]
end
end
end
return Out
end
Internal(Result, t)
return Result
end
plugin.AddCommand('eval', -1, function(User, Channel, String)
local Function, Message = loadstring(String)
if not Function then
Channel:Say("Parse error: " .. Message)
return
end
local Env = DeepCopy(_G)
Env.plugin = nil
Env.loadstring = nil
Env.pcall = nil
Env.setfenv = nil
Env._G = Env
Env.DBI = nil
Env.https = nil
Env.json = nil
Env.print = function(...)
local Args = {...}
local Output = table.concat(Args, "\t")
if #Output > 256 then
Output = Output:sub(1,256) .. " [...] (truncated)"
end
Channel:Say("stdout: " .. Output)
end
setfenv(Function, Env)
local Result, Message = pcall(Function)
Message = tostring(Message)
if #Message > 256 then
Message = Message:sub(1,256) .. " [...] (truncated)"
end
if Result then
Channel:Say("OK -> " .. Message)
else
Channel:Say("Error -> " .. Message)
end
end, "Runs a Lua command in a sandbox.", 10)