Code-Cleanup für Utilities, fix #12

This commit is contained in:
Andreas Bielawski 2016-09-08 01:35:10 +02:00
parent c37ad5a156
commit 8b7c55088a
21 changed files with 85 additions and 170 deletions

View File

@ -43,7 +43,7 @@ function app_store:send_appstore_data(data)
else
game_center = ''
end
local category_count = tablelength(data.genres)
local category_count = #data.genres
if category_count == 1 then
category = '\nKategorie: '..data.genres[1]
else

View File

@ -124,10 +124,10 @@ function bitly_create:action(msg, config, matches)
end
if matches[2] == nil then
long_url = url_encode(matches[1])
long_url = URL.encode(matches[1])
domain = 'bit.ly'
else
long_url = url_encode(matches[2])
long_url = URL.encode(matches[2])
domain = matches[1]
end
utilities.send_reply(msg, bitly_create:create_bitlink(long_url, domain, bitly_access_token))

View File

@ -47,7 +47,7 @@ function channels:pre_process(msg, config)
end
function channels:action(msg, config, matches)
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end

View File

@ -11,7 +11,7 @@ end
function control:action(msg, config)
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
return
end

View File

@ -89,7 +89,7 @@ end
function creds_manager:action(msg, config, matches)
local receiver = msg.from.id
if receiver ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end

View File

@ -16,9 +16,9 @@ function dhl:sendungsstatus(id)
local res,code = https.request(url)
if code ~= 200 then return "Fehler beim Abrufen von mobil.dhl.de" end
local status = string.match(res, "<div id%=\"detailShortStatus\">(.-)</div>")
local status = all_trim(status)
local status = utilities.trim(status)
local zeit = string.match(res, "<div id%=\"detailStatusDateTime\">(.-)</div>")
local zeit = all_trim(zeit)
local zeit = utilities.trim(zeit)
if not zeit or zeit == '<br />' then
return status
end

View File

@ -23,6 +23,19 @@ end
gImages.command = 'img <Suchbegriff>'
function gImages:is_blacklisted(msg)
_blacklist = redis:smembers("telegram:img_blacklist")
local var = false
for v,word in pairs(_blacklist) do
if string.find(string.lower(msg), string.lower(word)) then
print("Wort steht auf der Blacklist!")
var = true
break
end
end
return var
end
-- Yes, the callback is copied from below, but I can't think of another method :\
function gImages:callback(callback, msg, self, config, input)
if not msg then return end
@ -150,7 +163,7 @@ function gImages:action(msg, config, matches)
end
print ('Checking if search contains blacklisted word: '..input)
if is_blacklisted(input) then
if gImages:is_blacklisted(input) then
utilities.send_reply(msg, 'Vergiss es! ._.')
return
end

View File

@ -79,7 +79,7 @@ function games:send_game_data(game_id, self, msg)
if xml.find(result, 'Genres') then
local genres = xml.find(result, 'Genres')
local genre_count = tablelength(genres)-1
local genre_count = #genres-1
if genre_count == 1 then
genre = '\nGenre: '..genres[1][1]
else

View File

@ -180,7 +180,7 @@ function gh_feed:action(msg, config, matches)
-- For channels
if matches[1] == 'sub' and matches[2] and matches[3] then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -194,7 +194,7 @@ function gh_feed:action(msg, config, matches)
utilities.send_reply(msg, output, true)
return
elseif matches[1] == 'del' and matches[2] and matches[3] then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -227,7 +227,7 @@ function gh_feed:action(msg, config, matches)
end
if matches[1] == 'sub' and matches[2] then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -235,7 +235,7 @@ function gh_feed:action(msg, config, matches)
utilities.send_reply(msg, output, true)
return
elseif matches[1] == 'del' and matches[2] then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -247,7 +247,7 @@ function gh_feed:action(msg, config, matches)
utilities.send_reply(msg, list_subs, true, keyboard)
return
elseif matches[1] == 'sync' then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end

View File

@ -66,27 +66,27 @@ function id:action(msg, config, matches)
end
local chat_id = msg.chat.id
local user = 'Du bist @%s, auch bekannt als *%s* `[%s]`'
local user = 'Du bist @%s, auch bekannt als <b>%s</b> <code>[%s]</code>'
if msg.from.username then
user = user:format(utilities.markdown_escape(msg.from.username), msg.from.name, msg.from.id)
user = user:format(utilities.html_escape(msg.from.username), msg.from.name, msg.from.id)
else
user = 'Du bist *%s* `[%s]`,'
user = 'Du bist <b>%s</b> <code>[%s]</code>,'
user = user:format(msg.from.name, msg.from.id)
end
local group = '@%s, auch bekannt als *%s* `[%s]`.'
local group = '@%s, auch bekannt als <b>%s</b> <code>[%s]</code>.'
if msg.chat.type == 'private' then
group = group:format(utilities.markdown_escape(self.info.username), self.info.first_name, self.info.id)
group = group:format(utilities.html_escape(self.info.username), self.info.first_name, self.info.id)
elseif msg.chat.username then
group = group:format(utilities.markdown_escape(msg.chat.username), msg.chat.title, chat_id)
group = group:format(utilities.html_escape(msg.chat.username), msg.chat.title, chat_id)
else
group = '*%s* `[%s]`.'
group = '<b>%s</b> <code>[%s]</code>.'
group = group:format(msg.chat.title, chat_id)
end
local output = user .. ', und du bist in der Gruppe ' .. group
utilities.send_message(msg.chat.id, output, true, msg.message_id, true)
utilities.send_message(msg.chat.id, output, true, msg.message_id, 'HTML')
elseif matches[1] == "chat" then
if msg.chat.type ~= 'group' and msg.chat.type ~= 'supergroup' then
utilities.send_reply(msg, 'Das hier ist keine Gruppe!')
@ -118,21 +118,21 @@ function id:action(msg, config, matches)
local result = id:get_member_count(msg, chat_id)
local member_count = result.result
if member_count == 1 then
member_count = 'ist *1 Mitglied'
member_count = 'ist <b>1 Mitglied'
else
member_count = 'sind *'..member_count..' Mitglieder'
member_count = 'sind <b>'..member_count..' Mitglieder'
end
local text = 'IDs für *'..chat_name..'* `['..chat_id..']`\nHier '..member_count..':*\n---------\n'
local text = 'IDs für <b>'..chat_name..'</b> <code>['..chat_id..']</code>\nHier '..member_count..':</b>\n---------\n'
for k,user in pairs(users_info) do
if table.contains(admins, tostring(user.id)) then
text = text..'*'..user.name..'* `['..user.id..']` _Administrator_\n'
text = text..'<b>'..user.name..'</b> <code>['..user.id..']</code> <i>Administrator</i>\n'
elseif tostring(creator_id) == user.id then
text = text..'*'..user.name..'* `['..user.id..']` _Gründer_\n'
text = text..'<b>'..user.name..'</b> <code>['..user.id..']</code> <i>Gründer</i>\n'
else
text = text..'*'..user.name..'* `['..user.id..']`\n'
text = text..'<b>'..user.name..'</b> <code>['..user.id..']</code>\n'
end
end
utilities.send_reply(msg, text..'_(Bots sind nicht gelistet)_', true)
utilities.send_reply(msg, text..'<i>(Bots sind nicht gelistet)</i>', 'HTML')
end
end

View File

@ -49,7 +49,7 @@ function imgblacklist:remove_blacklist(word)
end
function imgblacklist:action(msg, config, matches)
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end

View File

@ -27,7 +27,7 @@ end
function luarun:action(msg, config)
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
return true
end

View File

@ -16,7 +16,7 @@ lyrics.command = 'lyrics <Lied>'
function lyrics:getLyrics(text)
local apikey = cred_data.lyricsnmusic_apikey
local q = url_encode(text)
local q = URL.encode(text)
local b = http.request("http://api.lyricsnmusic.com/songs?api_key="..apikey.."&q=" .. q)
response = json.decode(b)
local reply = ""

View File

@ -24,6 +24,18 @@ end
plugin_manager.command = 'plugins <nur für Superuser>'
-- Returns at table of lua files inside plugins
function plugin_manager:plugins_names()
local files = {}
for k, v in pairs(scandir("otouto/plugins")) do
-- Ends with .lua
if (v:match(".lua$")) then
files[#files+1] = v
end
end
return files
end
-- Returns the key (index) in the config.enabled_plugins table
function plugin_manager:plugin_enabled(name, chat)
for k,v in pairs(enabled_plugins) do
@ -37,7 +49,7 @@ end
-- Returns true if file exists in plugins folder
function plugin_manager:plugin_exists(name)
for k,v in pairs(plugins_names()) do
for k,v in pairs(plugin_manager:plugins_names()) do
if name..'.lua' == v then
return true
end
@ -47,7 +59,7 @@ end
function plugin_manager:list_plugins()
local text = ''
for k, v in pairs(plugins_names()) do
for k, v in pairs(plugin_manager:plugins_names()) do
-- ✔ enabled, ❌ disabled
local status = ''
-- Check if is enabled
@ -161,7 +173,7 @@ function plugin_manager:reenable_plugin_on_chat(msg, plugin)
end
function plugin_manager:action(msg, config, matches)
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end

View File

@ -21,10 +21,6 @@ end
quotes.command = 'quote'
function quotes:save_quote(msg)
if msg.text:sub(11):isempty() then
return "Benutzung: /addquote [Zitat]"
end
local quote = msg.text:sub(11)
local hash = get_redis_hash(msg, 'quotes')
print('Saving quote to redis set '..hash)
@ -33,10 +29,6 @@ function quotes:save_quote(msg)
end
function quotes:delete_quote(msg)
if msg.text:sub(11):isempty() then
return "Benutzung: /delquote [Zitat]"
end
local quote = msg.text:sub(11)
local hash = get_redis_hash(msg, 'quotes')
print('Deleting quote from redis set '..hash)

View File

@ -211,7 +211,7 @@ function rss:action(msg, config, matches)
-- For channels
if matches[1] == 'sub' and matches[2] and matches[3] then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -225,7 +225,7 @@ function rss:action(msg, config, matches)
utilities.send_reply(msg, output, 'HTML')
return
elseif matches[1] == 'del' and matches[2] and matches[3] then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -258,7 +258,7 @@ function rss:action(msg, config, matches)
end
if matches[1] == 'sub' and matches[2] then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -266,7 +266,7 @@ function rss:action(msg, config, matches)
utilities.send_reply(msg, output, 'HTML')
return
elseif matches[1] == 'del' and matches[2] then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -278,7 +278,7 @@ function rss:action(msg, config, matches)
utilities.send_reply(msg, list_subs, 'HTML', keyboard)
return
elseif matches[1] == 'sync' then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end

View File

@ -8,7 +8,7 @@ function site_header:init(config)
end
function site_header:action(msg, config, matches)
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
end

View File

@ -136,7 +136,7 @@ function stats:action(msg, config, matches)
end
if matches[2] == "chat" then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
else

View File

@ -64,7 +64,7 @@ function tagesschau_eil:action(msg, config)
local output = tagesschau_eil:deabonnieren(id)
utilities.send_reply(msg, output, true)
elseif input:match('(sync)$') then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end

View File

@ -248,7 +248,7 @@ end
function twitter_send:action(msg, config, matches)
if matches[1] == "twwhitelist add" then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
else
@ -265,7 +265,7 @@ function twitter_send:action(msg, config, matches)
end
if matches[1] == "twwhitelist del" then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
else
@ -288,7 +288,7 @@ function twitter_send:action(msg, config, matches)
-- Thanks to the great doc at https://github.com/ignacio/LuaOAuth#a-more-involved-example
if not oauth_token and not oauth_token_secret then
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
else
@ -312,7 +312,7 @@ function twitter_send:action(msg, config, matches)
if matches[1] == 'auth' and matches[2] then
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end
@ -327,7 +327,7 @@ function twitter_send:action(msg, config, matches)
if matches[1] == 'unauth' then
if msg.chat.type == 'group' or msg.chat.type == 'supergroup' then
if msg.from.id ~= config.admin then
if not is_sudo(msg, config) then
utilities.send_reply(msg, config.errors.sudo)
return
end

View File

@ -342,38 +342,21 @@ function utilities.input_from_msg(msg)
return utilities.input(msg.text) or (msg.reply_to_message and #msg.reply_to_message.text > 0 and msg.reply_to_message.text) or false
end
-- Calculates the length of the given string as UTF-8 characters
function utilities.utf8_len(s)
local chars = 0
for i = 1, string.len(s) do
local b = string.byte(s, i)
if b < 128 or b >= 192 then
chars = chars + 1
end
end
return chars
end
-- Trims whitespace from a string.
function utilities.trim(str)
local s = str:gsub('^%s*(.-)%s*$', '%1')
return s
end
-- Returns true if the string is empty
-- Returns true if the string is blank/empty
function string:isempty()
return self == nil or self == ''
end
-- Returns true if the string is blank
function string:isblank()
self = self:trim()
return self:isempty()
return self == nil or self == ''
end
function get_name(msg)
local name = msg.from.first_name
if name == nil then
if not name then
name = msg.from.id
end
return name
@ -391,10 +374,6 @@ function convert_timestamp(timestamp, date_format)
return os.date(date_format, timestamp)
end
function string.starts(String, Start)
return Start == string.sub(String,1,string.len(Start))
end
-- Saves file to $HOME/tmp/. If file_name isn't provided,
-- will get the text after the last "/" for filename
-- and content-type for extension
@ -497,19 +476,6 @@ function utilities.build_name(first, last)
end
end
function utilities:resolve_username(input)
input = input:gsub('^@', '')
for _, user in pairs(self.database.users) do
if user.username and user.username:lower() == input:lower() then
local t = {}
for key, val in pairs(user) do
t[key] = val
end
return t
end
end
end
function utilities:handle_exception(err, message, log_chat)
local output = string.format(
'[%s]\n%s: %s\n%s\n',
@ -527,19 +493,12 @@ function utilities:handle_exception(err, message, log_chat)
end
-- MOVED TO DOWNLOAD_TO_FILE
function utilities.download_file(url, filename)
return download_to_file(url, filename)
end
function utilities.md_escape(text)
return text:gsub('_', '\\_')
:gsub('%[', '\\['):gsub('%]', '\\]')
:gsub('%*', '\\*'):gsub('`', '\\`')
end
utilities.markdown_escape = utilities.md_escape
function utilities.html_escape(text)
return text:gsub('&', '&amp;'):gsub('<', '&lt;'):gsub('>', '&gt;')
end
@ -565,13 +524,6 @@ function utilities.triggers(username, cmd_pat, trigger_table)
return self
end
function utilities.with_http_timeout(timeout, fun)
local original = http.TIMEOUT
http.TIMEOUT = timeout
fun()
http.TIMEOUT = original
end
function utilities.enrich_user(user)
user.id_str = tostring(user.id)
user.name = utilities.build_name(user.first_name, user.last_name)
@ -594,11 +546,11 @@ function utilities.enrich_message(msg)
if msg.forward_from then
msg.forward_from = utilities.enrich_user(msg.forward_from)
end
if msg.new_chat_participant then
msg.new_chat_participant = utilities.enrich_user(msg.new_chat_participant)
if msg.new_chat_member then
msg.new_chat_member = utilities.enrich_user(msg.new_chat_member)
end
if msg.left_chat_participant then
msg.left_chat_participant = utilities.enrich_user(msg.left_chat_participant)
if msg.left_chat_member then
msg.left_chat_member = utilities.enrich_user(msg.left_chat_member)
end
return msg
end
@ -632,29 +584,6 @@ function scandir(directory)
return t
end
-- Returns at table of lua files inside plugins
function plugins_names()
local files = {}
for k, v in pairs(scandir("otouto/plugins")) do
-- Ends with .lua
if (v:match(".lua$")) then
files[#files+1] = v
end
end
return files
end
-- Function name explains what it does.
function file_exists(name)
local f = io.open(name,"r")
if f ~= nil then
io.close(f)
return true
else
return false
end
end
-- Returns a table with matches or nil
function match_pattern(pattern, text)
if text then
@ -793,17 +722,6 @@ function get_redis_hash(msg, var)
end
end
-- remove whitespace
function all_trim(s)
return s:match( "^%s*(.-)%s*$" )
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function round(num, idp)
if idp and idp>0 then
local mult = 10^idp
@ -823,6 +741,9 @@ function comma_value(amount)
return formatted
end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
function string.ends(str, fin)
return fin=='' or string.sub(str,-string.len(fin)) == fin
@ -1034,19 +955,6 @@ function makeHumanTime(totalseconds)
end
end
function is_blacklisted(msg)
_blacklist = redis:smembers("telegram:img_blacklist")
local var = false
for v,word in pairs(_blacklist) do
if string.find(string.lower(msg), string.lower(word)) then
print("Wort steht auf der Blacklist!")
var = true
break
end
end
return var
end
function unescape(str)
str = string.gsub( str, '&lt;', '<' )
str = string.gsub( str, '&gt;', '>' )
@ -1065,16 +973,6 @@ function unescape(str)
return str
end
function url_encode(str)
if (str) then
str = string.gsub (str, "\n", "\r\n")
str = string.gsub (str, "([^%w %-%_%.%~])",
function (c) return string.format ("%%%02X", string.byte(c)) end)
str = string.gsub (str, " ", "+")
end
return str
end
function table.contains(table, element)
for _, value in pairs(table) do
if value == element then
@ -1103,4 +1001,4 @@ function utilities.fix_utf8(str)
end
return utilities
return utilities