diff --git a/README.md b/README.md
index 9958d3f..290ab54 100644
--- a/README.md
+++ b/README.md
@@ -28,6 +28,7 @@ Ubuntu und Debian liefern Luarocks nur für Lua 5.1 aus. Um Luarocks für Lua 5.
### Setup
Du benötigst **Lua 5.2+**, eine aktive **Redis-Instanz** und die folgenden **LuaRocks-Module**:
+* luautf8
* luasocket
* luasec
* multipart-post
@@ -184,4 +185,4 @@ Das ist die Datenbank-Struktur:
`database.userdata` speichert Daten von verschiedenen Plugins, hierzu wird aber für Brawlbot-Plugins Redis verwendet.
-`database.version` speichert die Bot-Version
\ No newline at end of file
+`database.version` speichert die Bot-Version.
\ No newline at end of file
diff --git a/otouto/plugins/xkcd.lua b/otouto/plugins/xkcd.lua
index 7f86824..1148062 100644
--- a/otouto/plugins/xkcd.lua
+++ b/otouto/plugins/xkcd.lua
@@ -1,33 +1,54 @@
local xkcd = {}
xkcd.command = 'xkcd [i]'
+xkcd.base_url = 'https://xkcd.com/info.0.json'
+xkcd.strip_url = 'https://xkcd.com/%s/info.0.json'
function xkcd:init(config)
- xkcd.triggers = {
+ xkcd.triggers = {
+ "xkcd.com/(%d+)",
"^/xkcd (%d+)",
- "xkcd.com/(%d+)"
+ "^/xkcd (r)",
+ "^/xkcd"
}
- xkcd.doc = [[*
-]]..config.cmd_pat..[[xkcd* _[i]_: Gibt diesen XKCD-Comic aus]]
-end
-
-function xkcd:get_xkcd(id)
- local res,code = https.request("https://xkcd.com/"..id.."/info.0.json")
- if code ~= 200 then return nil end
- local data = json.decode(res)
- local link_image = data.img
- if link_image:sub(0,2) == '//' then
- link_image = link_image:sub(3,-1)
+ xkcd.doc = [[*
+]]..config.cmd_pat..[[xkcd* _[i]_: Gibt den aktuellen XKCD-Comic aus, oder die Nummer, wenn eine gegeben ist. Wenn "r" übergeben wird, wird ein zufälliger Comic zurückgegeben.]]
+ local jstr = https.request(xkcd.base_url)
+ if jstr then
+ local data = json.decode(jstr)
+ if data then
+ xkcd.latest = data.num
+ end
end
- return link_image, data.title, data.alt
+ xkcd.latest = xkcd.latest or 1700
end
function xkcd:action(msg, config, matches)
- local url, title, alt = xkcd:get_xkcd(matches[1])
- if not url then utilities.send_reply(msg, config.errors.connection) return end
- utilities.send_typing(msg.chat.id, 'upload_photo')
- local file = download_to_file(url)
- utilities.send_photo(msg.chat.id, file, title..'\n'..alt, msg.message_id)
+ if matches[1] == 'r' then
+ input = math.random(xkcd.latest)
+ elseif tonumber(matches[1]) then
+ input = tonumber(matches[1])
+ else
+ input = xkcd.latest
+ end
+
+ local url = xkcd.strip_url:format(input)
+ local jstr, code = https.request(url)
+ if code == 404 then
+ utilities.send_reply(msg, config.errors.results)
+ elseif code ~= 200 then
+ utilities.send_reply(msg, config.errors.connection)
+ else
+ local data = json.decode(jstr)
+ local output = string.format(
+ '%s (%s)\n%s',
+ utilities.html_escape(utilities.fix_utf8(data.safe_title)),
+ utilities.html_escape(data.img),
+ data.num,
+ utilities.html_escape(utilities.fix_utf8(data.alt))
+ )
+ utilities.send_message(msg.chat.id, output, false, nil, 'html')
+ end
end
-return xkcd
+return xkcd
\ No newline at end of file
diff --git a/otouto/utilities.lua b/otouto/utilities.lua
index a496d62..5028453 100644
--- a/otouto/utilities.lua
+++ b/otouto/utilities.lua
@@ -20,6 +20,7 @@
local utilities = {}
+utf8 = require 'lua-utf8'
ltn12 = require('ltn12')
http = require('socket.http')
https = require('ssl.https')
@@ -1097,4 +1098,10 @@ function is_channel_disabled(msg)
return disabled
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))
+end
+
return utilities
\ No newline at end of file