2016-04-10 21:04:47 -07:00
|
|
|
local luarun = {}
|
2015-12-13 09:25:49 -05:00
|
|
|
|
2016-06-07 00:31:34 -04:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-05-29 13:08:39 -04:00
|
|
|
local URL = require('socket.url')
|
2016-08-13 22:26:44 -04:00
|
|
|
local JSON, serpent
|
2015-12-13 09:25:49 -05:00
|
|
|
|
2016-05-26 20:28:44 -07:00
|
|
|
function luarun:init(config)
|
2016-08-13 22:46:18 -04: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-04-10 21:04:47 -07:00
|
|
|
end
|
|
|
|
|
2016-05-26 17:26:30 -07:00
|
|
|
function luarun:action(msg, config)
|
2016-04-10 21:04:47 -07:00
|
|
|
|
2016-08-13 22:46:18 -04:00
|
|
|
if msg.from.id ~= config.admin then
|
|
|
|
return true
|
|
|
|
end
|
2015-12-13 09:25:49 -05:00
|
|
|
|
2016-08-13 22:46:18 -04:00
|
|
|
local input = utilities.input(msg.text)
|
|
|
|
if not input then
|
2016-08-23 00:16:32 -04:00
|
|
|
utilities.send_reply(msg, 'Please enter a string to load.')
|
2016-08-13 22:46:18 -04:00
|
|
|
return
|
|
|
|
end
|
2015-12-13 09:25:49 -05:00
|
|
|
|
2016-08-13 22:46:18 -04:00
|
|
|
if msg.text_lower:match('^'..config.cmd_pat..'return') then
|
|
|
|
input = 'return ' .. input
|
|
|
|
end
|
2016-05-29 13:08:39 -04:00
|
|
|
|
2016-08-23 00:16:32 -04:00
|
|
|
local output, success =
|
2016-09-04 00:50:51 -04:00
|
|
|
load("local bot = require('otouto.bot')\n\z
|
2016-08-23 00:16:32 -04:00
|
|
|
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
|
|
|
|
return function (self, msg, config)\n" .. input .. "\nend")
|
|
|
|
|
|
|
|
local function err_msg(x)
|
|
|
|
return "Error:\n" .. tostring(x)
|
|
|
|
end
|
|
|
|
|
|
|
|
if output == nil then
|
|
|
|
output = success
|
|
|
|
else
|
|
|
|
success, output = xpcall(output(), err_msg, self, msg, config)
|
|
|
|
end
|
|
|
|
|
2016-08-13 22:46:18 -04: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
|
2016-08-23 00:16:32 -04:00
|
|
|
output = '<code>' .. utilities.html_escape(tostring(output)) .. '</code>'
|
2016-08-13 22:46:18 -04:00
|
|
|
end
|
2016-08-23 00:16:32 -04:00
|
|
|
utilities.send_message(msg.chat.id, output, true, msg.message_id, 'html')
|
2015-12-13 09:25:49 -05:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-10 21:04:47 -07:00
|
|
|
return luarun
|
2015-12-13 09:25:49 -05:00
|
|
|
|