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/miku/plugins/yourls.lua
2016-08-24 17:18:17 +02:00

54 lines
1.4 KiB
Lua

local yourls = {}
function yourls:init(config)
if not cred_data.yourls_site_url then
print('Fehlender Key: yourls_site_url.')
print('yourls.lua wird nicht aktiviert.')
return
elseif not cred_data.yourls_signature_token then
print('Fehlender Key: yourls_signature_token.')
print('yourls.lua wird nicht aktiviert.')
return
end
yourls.triggers = {
"^/yourls (https?://[%w-_%.%?%.:/%+=&]+)"
}
end
local SITE_URL = cred_data.yourls_site_url
local signature = cred_data.yourls_signature_token
local BASE_URL = SITE_URL..'/yourls-api.php'
function yourls:prot_url(url)
local url, h = string.gsub(url, "http://", "")
local url, hs = string.gsub(url, "https://", "")
local protocol = "http"
if hs == 1 then
protocol = "https"
end
return url, protocol
end
function yourls:create_yourls_link(long_url, protocol)
local url = BASE_URL..'?format=simple&signature='..signature..'&action=shorturl&url='..long_url
if protocol == "http" then
link,code = http.request(url)
else
link,code = https.request(url)
end
if code ~= 200 then
link = 'Ein Fehler ist aufgetreten. '..link
end
return link
end
function yourls:action(msg, config, matches)
local long_url = matches[1]
local baseurl, protocol = yourls:prot_url(SITE_URL)
utilities.send_reply(msg, yourls:create_yourls_link(long_url, protocol))
return
end
return yourls