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/plugins/credentials_manager.lua

118 lines
3.2 KiB
Lua
Raw Normal View History

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
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
if matches[1] == "#creds" then
2015-11-12 17:42:03 +01:00
return list_creds()
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)
elseif matches[1] == "#creds del" then
2015-11-12 17:42:03 +01:00
local var = string.lower(matches[2])
return del_creds(var)
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 = {
"#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
}