2016-04-11 06:04:47 +02:00
|
|
|
local luarun = {}
|
2015-12-13 15:25:49 +01:00
|
|
|
|
2016-06-07 06:31:34 +02:00
|
|
|
local utilities = require('otouto.utilities')
|
2016-05-29 19:08:39 +02:00
|
|
|
local URL = require('socket.url')
|
2016-08-14 04:26:44 +02:00
|
|
|
local JSON, serpent
|
2015-12-13 15:25:49 +01:00
|
|
|
|
2016-05-27 05:28:44 +02:00
|
|
|
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-04-11 06:04:47 +02:00
|
|
|
end
|
|
|
|
|
2016-05-27 02:26:30 +02:00
|
|
|
function luarun:action(msg, config)
|
2016-04-11 06:04:47 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
if msg.from.id ~= config.admin then
|
|
|
|
return true
|
|
|
|
end
|
2015-12-13 15:25:49 +01:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local input = utilities.input(msg.text)
|
|
|
|
if not input then
|
|
|
|
utilities.send_reply(self, msg, 'Please enter a string to load.')
|
|
|
|
return
|
|
|
|
end
|
2015-12-13 15:25:49 +01:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
if msg.text_lower:match('^'..config.cmd_pat..'return') then
|
|
|
|
input = 'return ' .. input
|
|
|
|
end
|
2016-05-29 19:08:39 +02:00
|
|
|
|
2016-08-14 04:46:18 +02:00
|
|
|
local output = loadstring( [[
|
|
|
|
local bot = require('otouto.bot')
|
|
|
|
local bindings = require('otouto.bindings')
|
|
|
|
local utilities = require('otouto.utilities')
|
|
|
|
local drua = require('otouto.drua-tg')
|
|
|
|
local JSON = require('dkjson')
|
|
|
|
local URL = require('socket.url')
|
|
|
|
local HTTP = require('socket.http')
|
|
|
|
local HTTPS = require('ssl.https')
|
|
|
|
return function (self, msg, config) ]] .. input .. [[ end
|
|
|
|
]] )()(self, msg, config)
|
|
|
|
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 = '```\n' .. tostring(output) .. '\n```'
|
|
|
|
end
|
|
|
|
utilities.send_message(self, msg.chat.id, output, true, msg.message_id, true)
|
2015-12-13 15:25:49 +01:00
|
|
|
|
|
|
|
end
|
|
|
|
|
2016-04-11 06:04:47 +02:00
|
|
|
return luarun
|
2015-12-13 15:25:49 +01:00
|
|
|
|