- Portiere Credentials_Manager

- Portiere Location_Manager
- get_location() in utilites
- send_typing() in images und media
This commit is contained in:
Andreas Bielawski 2016-06-14 16:17:13 +02:00
parent 5df5d13e7a
commit 5362d40b3f
5 changed files with 208 additions and 2 deletions

125
otouto/plugins/creds.lua Normal file
View File

@ -0,0 +1,125 @@
local creds_manager = {}
local utilities = require('otouto.utilities')
local redis = (loadfile "./otouto/redis.lua")()
function creds_manager:init(config)
creds_manager.triggers = {
"^(/creds)$",
"^(/creds add) ([^%s]+) (.+)$",
"^(/creds del) (.+)$",
"^(/creds rename) ([^%s]+) (.+)$"
}
creds_manager.doc = [[*
]]..config.cmd_pat..[[creds*: Zeigt alle Logindaten und API-Keys
*]]..config.cmd_pat..[[creds* _add_ _<Variable>_ _<Schlüssel>_: Speichert Schlüssel mit dieser Variable ein
*]]..config.cmd_pat..[[creds* _del_ _<Variable>_: Löscht Schlüssel mit dieser Variable
*]]..config.cmd_pat..[[creds* _rename_ _<Variable>_ _<Neue Variable>_: Benennt Variable um, behält Schlüssel bei
]]
end
creds_manager.command = 'creds'
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 creds_manager:reload_creds()
cred_data = redis:hgetall(hash)
end
function creds_manager:list_creds()
creds_manager: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 creds_manager:add_creds(var, key)
print('Saving credential for '..var..' to redis hash '..hash)
redis:hset(hash, var, key)
reload_creds()
return 'Gespeichert!'
end
function creds_manager: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öscht!'
else
return 'Du hast keine Logininformationen für diese Variable eingespeichert.'
end
end
function creds_manager: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 creds_manager:action(msg, config, matches)
local receiver = msg.from.id
if receiver ~= config.admin then
utilities.send_reply(self, msg, config.errors.sudo)
return
end
if msg.chat.type ~= 'private' then
utilities.send_reply(self, msg, 'Dieses Plugin solltest du nur [privat](http://telegram.me/' .. self.info.username .. '?start=creds) nutzen!', true)
return
end
if matches[1] == "/creds" then
utilities.send_reply(self, msg, creds_manager:list_creds())
return
elseif matches[1] == "/creds add" then
local var = string.lower(string.sub(matches[2], 1, 50))
local key = string.sub(matches[3], 1, 1000)
utilities.send_reply(self, msg, creds_manager:add_creds(var, key))
return
elseif matches[1] == "/creds del" then
local var = string.lower(matches[2])
utilities.send_reply(self, msg, creds_manager:del_creds(var))
return
elseif matches[1] == "/creds rename" then
local var = string.lower(string.sub(matches[2], 1, 50))
local newvar = string.lower(string.sub(matches[3], 1, 1000))
utilities.send_reply(self, msg, creds_manager:rename_creds(var, newvar))
return
end
end
return creds_manager

View File

@ -7,6 +7,7 @@ images.triggers = {
}
function images:action(msg)
utilities.send_typing(self, msg.chat.id, 'upload_photo')
local url = matches[1]
local file = download_to_file(url)
utilities.send_photo(self, msg.chat.id, file, nil, msg.message_id)

View File

@ -0,0 +1,69 @@
local loc_manager = {}
local utilities = require('otouto.utilities')
local redis = (loadfile "./otouto/redis.lua")()
function loc_manager:init(config)
loc_manager.triggers = {
"^/location (set) (.*)$",
"^/location (del)$",
"^/location$"
}
loc_manager.doc = [[*
]]..config.cmd_pat..[[location*: Gibt deinen gesetzten Wohnort aus
*]]..config.cmd_pat..[[location* _set_ _<Ort>_: Setzt deinen Wohnort auf diesen Ort
*]]..config.cmd_pat..[[location* _del_: Löscht deinen angegebenen Wohnort
]]
end
loc_manager.command = 'location'
function loc_manager:set_location(user_id, location)
local hash = 'user:'..user_id
local set_location = get_location(user_id)
if set_location == location then
return 'Dieser Ort wurde bereits gesetzt.'
else
print('Setting location in redis hash '..hash..' to location')
redis:hset(hash, 'location', location)
return 'Dein Wohnort wurde auf *'..location..'* festgelegt.'
end
end
function loc_manager:del_location(user_id)
local hash = 'user:'..user_id
local set_location = get_location(user_id)
if not set_location then
return 'Du hast keinen Ort gesetzt'
else
print('Setting location in redis hash '..hash..' to false')
-- We set the location to false, because deleting the value blocks redis for a few milliseconds
redis:hset(hash, 'location', false)
return 'Dein Wohnort *'..set_location..'* wurde gelöscht!'
end
end
function loc_manager:action(msg, config, matches)
local user_id = msg.from.id
if matches[1] == 'set' then
utilities.send_reply(self, msg, loc_manager:set_location(user_id, matches[2]), true)
return
elseif matches[1] == 'del' then
utilities.send_reply(self, msg, loc_manager:del_location(user_id), true)
return
else
local set_location = get_location(user_id)
if not set_location then
utilities.send_reply(self, msg, '*Du hast keinen Ort gesetzt!*', true)
return
else
local coords = utilities.get_coords(set_location, config)
utilities.send_location(self, msg.chat.id, coords.lat, coords.lon, msg.message_id)
utilities.send_reply(self, msg, 'Gesetzter Wohnort: *'..set_location..'*', true)
return
end
end
end
return loc_manager

View File

@ -28,6 +28,7 @@ function media:action(msg)
local ext = matches[2]
local receiver = msg.chat.id
utilities.send_typing(self, receiver, 'upload_document')
local file = download_to_file(url)
local mime_type = mimetype.get_content_type_no_sub(ext)

View File

@ -349,9 +349,9 @@ end
-- Gets coordinates for a location. Used by gMaps.lua, time.lua, weather.lua.
function utilities.get_coords(input, config)
local url = 'http://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input)
local url = 'https://maps.googleapis.com/maps/api/geocode/json?address=' .. URL.escape(input)
local jstr, res = HTTP.request(url)
local jstr, res = HTTPS.request(url)
if res ~= 200 then
return config.errors.connection
end
@ -675,6 +675,16 @@ function string.ends(str, fin)
return fin=='' or string.sub(str,-string.len(fin)) == fin
end
function get_location(user_id)
local hash = 'user:'..user_id
local set_location = redis:hget(hash, 'location')
if set_location == 'false' or set_location == nil then
return false
else
return set_location
end
end
function cache_data(plugin, query, data, timeout, typ)
-- How to: cache_data(pluginname, query_name, data_to_cache, expire_in_seconds)
local hash = 'telegram:cache:'..plugin..':'..query