diff --git a/README.md b/README.md index 29af4a6..5622e43 100644 --- a/README.md +++ b/README.md @@ -20,7 +20,7 @@ otouto (including all plugins and documentation) is free software; you are free ## Setup 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`: @@ -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. -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. @@ -38,11 +38,11 @@ Note that certain plugins, such as `translate.lua` and `greetings.lua`, will req ### Quick start 1. Clone the repository. `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`. 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 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. diff --git a/install-dependencies.sh b/install-dependencies.sh index 4c6924d..623fe40 100755 --- a/install-dependencies.sh +++ b/install-dependencies.sh @@ -1,31 +1,35 @@ -# This script will attempt to install Lua 5.3, Luarocks (pointed at 5.3), and -# the rocks necssary to run otouto. This script targets Ubuntu 16.04; it will -# probably not work on earlier versions of Ubuntu. +# Install Lua, Luarocks, and otouto dependencies. Works in Ubuntu, maybe Debian. +# Installs Lua 5.3 if Ubuntu 16.04. Otherwise, 5.2. #!/bin/sh -echo "This script is intended for Ubuntu 16.04 and later. It will not work in" -echo "14.04 or earlier." +if [ $(lsb_release -r | cut -f 2) == "16.04" ]; then + 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 "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 "along with the following rocks:" -echo "luasocket luasec multipart-post lpeg dkjson" +echo $rocklist echo "Press enter to continue. Use Ctrl-C to exit." read 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 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 sudo make install -sudo luarocks-5.3 install luasocket -sudo luarocks-5.3 install luasec -sudo luarocks-5.3 install multipart-post -sudo luarocks-5.3 install lpeg -sudo luarocks-5.3 install dkjson +for rock in $rocklist; do + sudo luarocks-$luaver install $rock +done sudo -k cd .. diff --git a/launch.sh b/launch.sh index 17e973e..6ba0265 100755 --- a/launch.sh +++ b/launch.sh @@ -1,5 +1,8 @@ -# Launch otouto, after checking for Lua 5.3. -# Restart otouto five seconds after halted. +# Run otouto in Lua 5.3, if available. +# (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 @@ -10,7 +13,13 @@ if type lua5.3 >/dev/null 2>/dev/null; then echo "otouto has stopped. ^C to exit." sleep 5s 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 - echo "Lua 5.3 was not found." - echo "If you're on Ubuntu 16.04+, try running ./install-dependencies.sh." + echo "Lua not found." + echo "If you're on Ubuntu, try running ./install-dependencies.sh." fi diff --git a/otouto/plugins/luarun.lua b/otouto/plugins/luarun.lua index 1e5e230..6c66bd2 100644 --- a/otouto/plugins/luarun.lua +++ b/otouto/plugins/luarun.lua @@ -17,6 +17,12 @@ function luarun:init(config) return JSON.encode(t, {indent=true}) 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 function luarun:action(msg, config) @@ -35,8 +41,8 @@ function luarun:action(msg, config) input = 'return ' .. input end - local output, success = - load("local bot = require('otouto.bot')\n\z + 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 @@ -44,16 +50,13 @@ function luarun:action(msg, config) 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 + return function (self, msg, config)\n" .. input .. "\nend" + ) if output == nil then output = success else - success, output = xpcall(output(), err_msg, self, msg, config) + success, output = xpcall(output(), luarun.err_msg, self, msg, config) end if output == nil then diff --git a/otouto/utilities.lua b/otouto/utilities.lua index 8461ffc..7a44f28 100644 --- a/otouto/utilities.lua +++ b/otouto/utilities.lua @@ -26,6 +26,9 @@ local HTTPS = require('ssl.https') local URL = require('socket.url') local JSON = require('dkjson') 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, -- 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. -- Useful for fixing improper encoding caused by bad JSON escaping. function utilities.fix_utf8(str) - return string.char(utf8.codepoint(str, 1, -1)) + return string.char(utf8.codepoint(str, 1, -1)) end - return utilities diff --git a/tg-install.sh b/tg-install.sh index 1b6ea6c..15baf11 100755 --- a/tg-install.sh +++ b/tg-install.sh @@ -2,8 +2,7 @@ #!/bin/sh -echo "This script is intended for Ubuntu 16.04 and later. It will probably work on" -echo "14.04 or 12.04 as well as Debian, but this is not guaranteed." +echo "This script is intended for Ubuntu. It has been tested on 16.04 and 14.04." echo "This script will request root privileges to install the following packages:" echo "git libreadline-dev libssl-dev libevent-dev make" echo "Press enter to continue. Use Ctrl-C to exit."