Add Lua 5.2 compatibility.

This commit is contained in:
topkecleon 2016-09-06 20:34:57 -04:00
parent fa3e6d449f
commit 1fd125aa2c
6 changed files with 51 additions and 34 deletions

View File

@ -20,7 +20,7 @@ otouto (including all plugins and documentation) is free software; you are free
## Setup ## Setup
To get your bot running as soon as possible, see [Quick start](#quick-start). To get your bot running as soon as possible, see [Quick start](#quick-start).
otouto uses Lua 5.3 and the following Lua libraries: luasocket, luasec, multipart-post, dkjson, and lpeg. It is recommended you install these with Luarocks. This can be done easily on Ubuntu 16.04 and later with the `install-dependencies.sh` script. otouto uses Lua (5.3 is recommended) and the following Lua libraries: luasocket, luasec, multipart-post, dkjson, and lpeg. If you are using Lua 5.2, luautf8 is also required. It is recommended you install these with Luarocks. This can be done easily on Ubuntu with the `install-dependencies.sh` script.
To get started, clone the repository and set the following values in `config.lua`: To get started, clone the repository and set the following values in `config.lua`:
@ -29,7 +29,7 @@ To get started, clone the repository and set the following values in `config.lua
Some plugins are not enabled by default. If you wish to enable them, add their names (sans file extension) to the `plugins` table in the configuration file. Some plugins are not enabled by default. If you wish to enable them, add their names (sans file extension) to the `plugins` table in the configuration file.
When you are ready to start the bot, run the `launch.sh` script. This script will automatically restart the bot five seconds after being stopped. If this behavior is undesired, start the bot manually with `lua5.3 main.lua`. When you are ready to start the bot, run the `launch.sh` script. This script will automatically restart the bot five seconds after being stopped. If this behavior is undesired, start the bot manually with `lua main.lua`.
To stop the bot, send "/halt" through Telegram. You can exit with Ctrl-C (or two Ctrl-C if using `launch.sh`), but this is not recommended as it risks data loss. To stop the bot, send "/halt" through Telegram. You can exit with Ctrl-C (or two Ctrl-C if using `launch.sh`), but this is not recommended as it risks data loss.
@ -38,11 +38,11 @@ Note that certain plugins, such as `translate.lua` and `greetings.lua`, will req
### Quick start ### Quick start
1. Clone the repository. 1. Clone the repository.
`git clone http://otou.to/code otouto` `git clone http://otou.to/code otouto`
2. Install dependencies: Lua 5.3, and the following Lua libs: luasocket, luasec, multipart-post, dkjson, and lpeg.† 2. Install dependencies: Lua and the following Lua libs: luasocket, luasec, multipart-post, dkjson, and lpeg.†
3. Add your bot token and Telegram ID to `config.lua`. 3. Add your bot token and Telegram ID to `config.lua`.
4. Start the bot with `./launch.sh`. 4. Start the bot with `./launch.sh`.
**†** On Ubuntu 16.04, this can be done easily with the `install-dependencies.sh` script. **†** On Ubuntu, this can be done easily with the `install-dependencies.sh` script.
## Configuration ## Configuration
otouto is configured in the `config.lua` file. It is the single point of configuration for the bot, and contains any necessary user-specific variables, such as API keys, custom error messages, and enabled plugins. otouto is configured in the `config.lua` file. It is the single point of configuration for the bot, and contains any necessary user-specific variables, such as API keys, custom error messages, and enabled plugins.

View File

@ -1,31 +1,35 @@
# This script will attempt to install Lua 5.3, Luarocks (pointed at 5.3), and # Install Lua, Luarocks, and otouto dependencies. Works in Ubuntu, maybe Debian.
# the rocks necssary to run otouto. This script targets Ubuntu 16.04; it will # Installs Lua 5.3 if Ubuntu 16.04. Otherwise, 5.2.
# probably not work on earlier versions of Ubuntu.
#!/bin/sh #!/bin/sh
echo "This script is intended for Ubuntu 16.04 and later. It will not work in" if [ $(lsb_release -r | cut -f 2) == "16.04" ]; then
echo "14.04 or earlier." luaver="5.3"
rocklist="luasocket luasec multipart-post lpeg dkjson"
else
luaver="5.2"
rocklist="luasocket luasec multipart-post lpeg dkjson serpent"
fi
echo "This script is intended for Ubuntu. It may work in Debian."
echo "This script will request root privileges to install the following packages:" echo "This script will request root privileges to install the following packages:"
echo "lua5.3 liblua5.3-dev git libssl-dev fortune-mod fortunes unzip" echo "lua$luaver liblua$luaver-dev git libssl-dev fortune-mod fortunes unzip make"
echo "It will also request root privileges to install Luarocks to to /usr/local/" echo "It will also request root privileges to install Luarocks to to /usr/local/"
echo "along with the following rocks:" echo "along with the following rocks:"
echo "luasocket luasec multipart-post lpeg dkjson" echo $rocklist
echo "Press enter to continue. Use Ctrl-C to exit." echo "Press enter to continue. Use Ctrl-C to exit."
read read
sudo apt-get update sudo apt-get update
sudo apt-get install -y lua5.3 liblua5.3-dev git libssl-dev fortune-mod fortunes unzip sudo apt-get install -y lua$luaver liblua$luaver-dev git libssl-dev fortune-mod fortunes unzip make
git clone http://github.com/keplerproject/luarocks git clone http://github.com/keplerproject/luarocks
cd luarocks cd luarocks
./configure --lua-version=5.3 --versioned-rocks-dir --lua-suffix=5.3 ./configure --lua-version=$luaver --versioned-rocks-dir --lua-suffix=$luaver
make build make build
sudo make install sudo make install
sudo luarocks-5.3 install luasocket for rock in $rocklist; do
sudo luarocks-5.3 install luasec sudo luarocks-$luaver install $rock
sudo luarocks-5.3 install multipart-post done
sudo luarocks-5.3 install lpeg
sudo luarocks-5.3 install dkjson
sudo -k sudo -k
cd .. cd ..

View File

@ -1,5 +1,8 @@
# Launch otouto, after checking for Lua 5.3. # Run otouto in Lua 5.3, if available.
# Restart otouto five seconds after halted. # (Specifying lua5.3 because "lua" is not linked to it in Ubuntu 16.04.)
# Otherwise, use any generic installed Lua.
# If none, give an error and a friendly suggestion.
# If Lua was found, restart otouto five seconds after halting each time.
#!/bin/sh #!/bin/sh
@ -10,7 +13,13 @@ if type lua5.3 >/dev/null 2>/dev/null; then
echo "otouto has stopped. ^C to exit." echo "otouto has stopped. ^C to exit."
sleep 5s sleep 5s
done done
elif type lua >/dev/null 2>/dev/null; then
while true; do
lua main.lua
echo "otouto has stopped. ^C to exit."
sleep 5s
done
else else
echo "Lua 5.3 was not found." echo "Lua not found."
echo "If you're on Ubuntu 16.04+, try running ./install-dependencies.sh." echo "If you're on Ubuntu, try running ./install-dependencies.sh."
fi fi

View File

@ -17,6 +17,12 @@ function luarun:init(config)
return JSON.encode(t, {indent=true}) return JSON.encode(t, {indent=true})
end end
end end
-- 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 end
function luarun:action(msg, config) function luarun:action(msg, config)
@ -35,8 +41,8 @@ function luarun:action(msg, config)
input = 'return ' .. input input = 'return ' .. input
end end
local output, success = local output, success = luarun.loadstring(
load("local bot = require('otouto.bot')\n\z "local bot = require('otouto.bot')\n\z
local bindings = require('otouto.bindings')\n\z local bindings = require('otouto.bindings')\n\z
local utilities = require('otouto.utilities')\n\z local utilities = require('otouto.utilities')\n\z
local drua = require('otouto.drua-tg')\n\z local drua = require('otouto.drua-tg')\n\z
@ -44,16 +50,13 @@ function luarun:action(msg, config)
local URL = require('socket.url')\n\z local URL = require('socket.url')\n\z
local HTTP = require('socket.http')\n\z local HTTP = require('socket.http')\n\z
local HTTPS = require('ssl.https')\n\z local HTTPS = require('ssl.https')\n\z
return function (self, msg, config)\n" .. input .. "\nend") return function (self, msg, config)\n" .. input .. "\nend"
)
local function err_msg(x)
return "Error:\n" .. tostring(x)
end
if output == nil then if output == nil then
output = success output = success
else else
success, output = xpcall(output(), err_msg, self, msg, config) success, output = xpcall(output(), luarun.err_msg, self, msg, config)
end end
if output == nil then if output == nil then

View File

@ -26,6 +26,9 @@ local HTTPS = require('ssl.https')
local URL = require('socket.url') local URL = require('socket.url')
local JSON = require('dkjson') local JSON = require('dkjson')
local bindings = require('otouto.bindings') local bindings = require('otouto.bindings')
-- Lua 5.2 compatibility.
-- If no built-in utf8 is available, load the library.
local utf8 = utf8 or require('lua-utf8')
-- For the sake of ease to new contributors and familiarity to old contributors, -- For the sake of ease to new contributors and familiarity to old contributors,
-- we'll provide a couple of aliases to real bindings here. -- we'll provide a couple of aliases to real bindings here.
@ -324,8 +327,7 @@ end
-- Converts a gross string back into proper UTF-8. -- Converts a gross string back into proper UTF-8.
-- Useful for fixing improper encoding caused by bad JSON escaping. -- Useful for fixing improper encoding caused by bad JSON escaping.
function utilities.fix_utf8(str) function utilities.fix_utf8(str)
return string.char(utf8.codepoint(str, 1, -1)) return string.char(utf8.codepoint(str, 1, -1))
end end
return utilities return utilities

View File

@ -2,8 +2,7 @@
#!/bin/sh #!/bin/sh
echo "This script is intended for Ubuntu 16.04 and later. It will probably work on" echo "This script is intended for Ubuntu. It has been tested on 16.04 and 14.04."
echo "14.04 or 12.04 as well as Debian, but this is not guaranteed."
echo "This script will request root privileges to install the following packages:" echo "This script will request root privileges to install the following packages:"
echo "git libreadline-dev libssl-dev libevent-dev make" echo "git libreadline-dev libssl-dev libevent-dev make"
echo "Press enter to continue. Use Ctrl-C to exit." echo "Press enter to continue. Use Ctrl-C to exit."