2015-11-12 17:42:03 +01:00
|
|
|
|
-- This file is part of the Telegram Bot "Brawlbot" (telegram.me/Brawlbot) by Andreas Bielawski (telegram.me/Brawl)
|
|
|
|
|
-- Released unter the MPLv2
|
|
|
|
|
|
|
|
|
|
local hash = "telegram:credentials"
|
|
|
|
|
|
|
|
|
|
-- See: http://www.lua.org/pil/19.3.html
|
|
|
|
|
function pairsByKeys (t, f)
|
|
|
|
|
local a = {}
|
|
|
|
|
for n in pairs(t) do table.insert(a, n) end
|
|
|
|
|
table.sort(a, f)
|
|
|
|
|
local i = 0 -- iterator variable
|
|
|
|
|
local iter = function () -- iterator function
|
|
|
|
|
i = i + 1
|
|
|
|
|
if a[i] == nil then
|
|
|
|
|
return nil
|
|
|
|
|
else
|
|
|
|
|
return a[i], t[a[i]]
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
return iter
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function reload_creds()
|
|
|
|
|
cred_data = redis:hgetall(hash)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function list_creds()
|
|
|
|
|
reload_creds()
|
|
|
|
|
if redis:exists("telegram:credentials") == true then
|
|
|
|
|
local text = ""
|
|
|
|
|
for var, key in pairsByKeys(cred_data) do
|
|
|
|
|
text = text..var..' = '..key..'\n'
|
|
|
|
|
end
|
|
|
|
|
return text
|
|
|
|
|
else
|
|
|
|
|
create_cred()
|
|
|
|
|
return "Es wurden noch keine Logininformationen gespeichert, lege Tabelle an...\nSpeichere Keys mit !creds add [Variable] [Key] ein!"
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function add_creds(var, key)
|
|
|
|
|
print('Saving credential for '..var..' to redis hash '..hash)
|
|
|
|
|
redis:hset(hash, var, key)
|
|
|
|
|
reload_creds()
|
|
|
|
|
return var..' = '..key..'\neingespeichert!'
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function del_creds(var)
|
|
|
|
|
if redis:hexists(hash, var) == true then
|
|
|
|
|
print('Deleting credential for '..var..' from redis hash '..hash)
|
|
|
|
|
redis:hdel(hash, var)
|
|
|
|
|
reload_creds()
|
|
|
|
|
return 'Key von "'..var..'" erfolgreich gel<65>scht!'
|
|
|
|
|
else
|
|
|
|
|
return 'Du hast keine Logininformationen f<>r diese Variable eingespeichert.'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function rename_creds(var, newvar)
|
|
|
|
|
if redis:hexists(hash, var) == true then
|
|
|
|
|
local key = redis:hget(hash, var)
|
|
|
|
|
if redis:hsetnx(hash, newvar, key) == true then
|
|
|
|
|
redis:hdel(hash, var)
|
|
|
|
|
reload_creds()
|
|
|
|
|
return '"'..var..'" erfolgreich zu "'..newvar..'" umbenannt.'
|
|
|
|
|
else
|
|
|
|
|
return "Variable konnte nicht umbenannt werden: Zielvariable existiert bereits."
|
|
|
|
|
end
|
|
|
|
|
else
|
|
|
|
|
return 'Die zu umbennende Variable existiert nicht.'
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
function run(msg, matches)
|
|
|
|
|
local receiver = get_receiver(msg)
|
|
|
|
|
|
|
|
|
|
if not is_sudo(msg) then
|
2016-06-23 13:55:39 +02:00
|
|
|
|
return 'Das darf nur mein Meister!'
|
2015-11-12 17:42:03 +01:00
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
if msg.to.type == 'chat' then
|
|
|
|
|
return 'Das Plugin solltest du nur per PN nutzen!'
|
|
|
|
|
end
|
|
|
|
|
|
2016-06-22 13:59:06 +02:00
|
|
|
|
if matches[1] == "#creds" then
|
2015-11-12 17:42:03 +01:00
|
|
|
|
return list_creds()
|
2016-06-22 13:59:06 +02:00
|
|
|
|
elseif matches[1] == "#creds add" then
|
2015-11-12 17:42:03 +01:00
|
|
|
|
local var = string.lower(string.sub(matches[2], 1, 50))
|
|
|
|
|
local key = string.sub(matches[3], 1, 1000)
|
|
|
|
|
return add_creds(var, key)
|
2016-06-22 13:59:06 +02:00
|
|
|
|
elseif matches[1] == "#creds del" then
|
2015-11-12 17:42:03 +01:00
|
|
|
|
local var = string.lower(matches[2])
|
|
|
|
|
return del_creds(var)
|
2016-06-22 13:59:06 +02:00
|
|
|
|
elseif matches[1] == "#creds rename" then
|
2015-11-12 17:42:03 +01:00
|
|
|
|
local var = string.lower(string.sub(matches[2], 1, 50))
|
|
|
|
|
local newvar = string.lower(string.sub(matches[3], 1, 1000))
|
|
|
|
|
return rename_creds(var, newvar)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
description = "Loginmanager f<>r Telegram (nur Superuser)",
|
|
|
|
|
usage = {
|
2016-06-22 13:59:06 +02:00
|
|
|
|
"#creds: Zeigt alle Logindaten und API-Keys",
|
|
|
|
|
"#creds add [Variable] [Key]: Speichert Key mit der Variable ein",
|
|
|
|
|
"#creds del [Variable]: L<>scht Key mit der Variable",
|
|
|
|
|
"#creds rename [Variable] [Neuer Name]: Benennt Variable um, beh<65>lt Key bei"
|
2015-11-12 17:42:03 +01:00
|
|
|
|
},
|
|
|
|
|
patterns = {
|
|
|
|
|
"^(/creds)$",
|
|
|
|
|
"^(/creds add) ([^%s]+) (.+)$",
|
|
|
|
|
"^(/creds del) (.+)$",
|
|
|
|
|
"^(/creds rename) ([^%s]+) (.+)$"
|
|
|
|
|
},
|
|
|
|
|
run = run,
|
|
|
|
|
privileged = true,
|
|
|
|
|
notyping = true
|
|
|
|
|
}
|