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

62 lines
1.5 KiB
Lua
Raw Normal View History

local luarun = {}
function luarun:init(config)
luarun.triggers = utilities.triggers(self.info.username, config.cmd_pat):t('lua', true):t('return', true).table
2016-08-14 16:30:06 +02:00
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
end
2016-05-27 02:26:30 +02:00
function luarun:action(msg, config)
2016-08-14 16:30:06 +02:00
if not is_sudo(msg, config) then
return true
end
local input = utilities.input(msg.text)
if not input then
utilities.send_reply(self, msg, 'Please enter a string to load.')
return
end
if msg.text_lower:match('^'..config.cmd_pat..'return') then
input = 'return ' .. input
end
local output = loadstring( [[
2016-06-07 06:31:34 +02:00
local bot = require('otouto.bot')
local bindings = require('otouto.bindings')
local utilities = require('otouto.utilities')
2016-08-14 16:30:06 +02:00
local drua = require('otouto.drua-tg')
local JSON = require('dkjson')
local URL = require('socket.url')
2016-08-14 16:30:06 +02:00
local HTTP = require('socket.http')
local HTTPS = require('ssl.https')
return function (self, msg, config) ]] .. input .. [[ end
]] )()(self, msg, config)
if output == nil then
2015-12-14 20:48:17 +01:00
output = 'Done!'
else
if type(output) == 'table' then
2016-08-14 16:30:06 +02:00
local s = luarun.serialize(output)
if URL.escape(s):len() < 4000 then
output = s
end
end
output = '```\n' .. tostring(output) .. '\n```'
2015-12-14 20:48:17 +01:00
end
utilities.send_message(self, msg.chat.id, output, true, msg.message_id, true)
end
return luarun