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