This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot-2/otouto/plugins/luarun.lua

79 lines
2.2 KiB
Lua
Raw Normal View History

local luarun = {}
2016-06-07 06:31:34 +02:00
local utilities = require('otouto.utilities')
local URL = require('socket.url')
2016-08-14 04:26:44 +02:00
local JSON, serpent
function luarun:init(config)
2016-08-14 04:46:18 +02:00
luarun.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('lua', true):t('return', true).table
if config.luarun_serpent then
serpent = require('serpent')
luarun.serialize = function(t)
return serpent.block(t, {comment=false})
end
else
JSON = require('dkjson')
luarun.serialize = function(t)
return JSON.encode(t, {indent=true})
end
end
2016-09-07 02:34:57 +02:00
-- Lua 5.2 compatibility.
-- "loadstring" was renamed "load" in 5.3.
luarun.loadstring = load or loadstring
luarun.err_msg = function(x)
return 'Error:\n' .. tostring(x)
end
end
2016-05-27 02:26:30 +02:00
function luarun:action(msg, config)
2016-08-14 04:46:18 +02:00
if msg.from.id ~= config.admin then
return true
end
2016-08-14 04:46:18 +02:00
local input = utilities.input(msg.text)
if not input then
utilities.send_reply(msg, 'Please enter a string to load.')
2016-08-14 04:46:18 +02:00
return
end
2016-08-14 04:46:18 +02:00
if msg.text_lower:match('^'..config.cmd_pat..'return') then
input = 'return ' .. input
end
2016-09-07 02:34:57 +02:00
local output, success = luarun.loadstring(
"local bot = require('otouto.bot')\n\z
local bindings = require('otouto.bindings')\n\z
local utilities = require('otouto.utilities')\n\z
local drua = require('otouto.drua-tg')\n\z
local JSON = require('dkjson')\n\z
local URL = require('socket.url')\n\z
local HTTP = require('socket.http')\n\z
local HTTPS = require('ssl.https')\n\z
2016-09-07 02:34:57 +02:00
return function (self, msg, config)\n" .. input .. "\nend"
)
if output == nil then
output = success
else
2016-09-07 02:34:57 +02:00
success, output = xpcall(output(), luarun.err_msg, self, msg, config)
end
2016-08-14 04:46:18 +02:00
if output == nil then
output = 'Done!'
else
if type(output) == 'table' then
local s = luarun.serialize(output)
if URL.escape(s):len() < 4000 then
output = s
end
end
output = '<code>' .. utilities.html_escape(tostring(output)) .. '</code>'
2016-08-14 04:46:18 +02:00
end
utilities.send_message(msg.chat.id, output, true, msg.message_id, 'html')
end
return luarun