Erster privater commit

This commit is contained in:
2015-11-12 17:42:03 +01:00
parent 71ddfb8502
commit ea622733f6
118 changed files with 5289 additions and 820 deletions

View File

@ -1,26 +1,25 @@
package.path = package.path .. ';.luarocks/share/lua/5.2/?.lua'
..';.luarocks/share/lua/5.2/?/init.lua'
package.cpath = package.cpath .. ';.luarocks/lib/lua/5.2/?.so'
package.path = './.luarocks/share/lua/5.2/?.lua;./.luarocks/share/lua/5.2/?/init.lua;./.luarocks/lib/lua/5.2/?.lua;./.luarocks/lib/lua/5.2/?/init.lua;' .. package.path
require("luarocks.loader")
require("./bot/utils")
VERSION = '2.5-reloaded'
VERSION = '20151003'
-- This function is called when tg receive a msg
function on_msg_receive (msg)
if not started then
return
end
local receiver = get_receiver(msg)
-- vardump(msg)
msg = pre_process_service_msg(msg)
if msg_valid(msg) then
msg = pre_process_msg(msg)
if msg then
match_plugins(msg)
--mark_read(receiver, ok_cb, false)
if msg then
match_plugins(msg)
-- mark_read(receiver, ok_cb, false)
end
end
end
@ -33,9 +32,10 @@ function on_binlog_replay_end()
postpone (cron_plugins, false, 60*5.0)
-- See plugins/isup.lua as an example for cron
_config = load_config()
-- load sudo_users and credentials
sudo_users = load_sudo_users()
cred_data = load_cred()
-- load plugins
plugins = {}
load_plugins()
@ -53,11 +53,6 @@ function msg_valid(msg)
print('\27[36mNicht gültig: alte Nachricht\27[39m')
return false
end
if msg.unread == 0 then
print('\27[36mNicht gültig: gelesen\27[39m')
return false
end
if not msg.to.id then
print('\27[36mNicht gültig: To id not provided\27[39m')
@ -68,6 +63,11 @@ function msg_valid(msg)
print('\27[36mNicht gültig: From id not provided\27[39m')
return false
end
if msg.unread == 0 then
print('\27[36mNicht gültig: gelesen\27[39m')
return false
end
if msg.from.id == our_id then
print('\27[36mNicht gültig: Nachricht von unserer ID\27[39m')
@ -83,11 +83,10 @@ function msg_valid(msg)
print('\27[36mNicht gültig: Telegram Nachricht\27[39m')
return false
end
return true
end
--
function pre_process_service_msg(msg)
if msg.service then
local action = msg.action or {type=""}
@ -111,7 +110,7 @@ function pre_process_msg(msg)
for name,plugin in pairs(plugins) do
if plugin.pre_process and msg then
-- print('Preprocess', name)
msg = plugin.pre_process(msg)
msg = plugin.pre_process(msg)
end
end
@ -125,22 +124,18 @@ function match_plugins(msg)
end
end
-- Check if plugin is on _config.disabled_plugin_on_chat table
local function is_plugin_disabled_on_chat(plugin_name, receiver)
local disabled_chats = _config.disabled_plugin_on_chat
-- Table exists and chat has disabled plugins
if disabled_chats and disabled_chats[receiver] then
-- Checks if plugin is disabled on this chat
for disabled_plugin,disabled in pairs(disabled_chats[receiver]) do
if disabled_plugin == plugin_name and disabled then
local warning = 'Plugin '..disabled_plugin..' ist in diesem Chat deaktiviert'
print(warning)
-- send_msg(receiver, warning, ok_cb, false)
return true
end
end
-- Check if plugin is deactivated in this chat
local function is_plugin_disabled_on_chat(plugin_name, msg)
local hash = get_redis_hash(msg, 'disabled_plugins')
local disabled = redis:hget(hash, plugin_name)
-- Plugin is disabled
if disabled == 'true' then
print('Plugin '..plugin_name..' ist in diesem Chat deaktiviert')
return true
else
return false
end
return false
end
function match_plugin(plugin, plugin_name, msg)
@ -151,20 +146,13 @@ function match_plugin(plugin, plugin_name, msg)
local matches = match_pattern(pattern, msg.text)
if matches then
print("Nachricht stimmt überein mit ", pattern)
-- Send typing
--if pattern ~= ".*" then send_typing(receiver, ok_cb, true) end
if is_plugin_disabled_on_chat(plugin_name, receiver) then
if is_plugin_disabled_on_chat(plugin_name, msg) then
return nil
end
-- Function exists
if plugin.run then
-- check if user has privileges
if can_use_bot(msg) then
print("Gesperrt")
-- local text = 'Du darfst den Bot nicht nutzen!'
-- send_msg(receiver, text, ok_cb, false)
else
if not plugin.notyping then send_typing(receiver, ok_cb, true) end
-- If plugin is for privileged users only
if not warns_user_not_allowed(plugin, msg) then
local result = plugin.run(msg, matches)
@ -173,7 +161,6 @@ function match_plugin(plugin, plugin_name, msg)
end
end
end
end
-- One patterns matches
return
end
@ -185,61 +172,37 @@ function _send_msg(destination, text)
send_large_msg(destination, text)
end
-- Save the content of _config to config.lua
function save_config( )
serialize_to_file(_config, './data/config.lua')
print ('Configuration in ./data/config.lua gespeichert')
-- Load superusers from redis
function load_sudo_users()
if redis:exists("telegram:sudo_users") == false then
-- If sudo_users set doesnt exists
print ("Created new sudo_users set: telegram:sudo_users")
create_sudo_users()
end
local sudo_users = redis:smembers("telegram:sudo_users")
for v,user in pairs(sudo_users) do
print("Superuser: " .. user)
end
return sudo_users
end
-- Returns the config from config.lua file.
-- If file doesn't exist, create it.
function load_config( )
local f = io.open('./data/config.lua', "r")
-- If config.lua doesn't exist
if not f then
print ("Neue Config-Datei erstellt: data/config.lua")
create_config()
else
f:close()
end
local config = loadfile ("./data/config.lua")()
for v,user in pairs(config.sudo_users) do
print("Erlaubter Benutzer: " .. user)
end
return config
end
function load_cred( )
local cf = io.open('./data/credentials.lua', "r")
-- If credentials.lua doesnt exists
if not cf then
print ("Neue Credentials-Datei erstellt: data/credentials.lua")
-- Load credentials from redis
function load_cred()
if redis:exists("telegram:credentials") == false then
-- If credentials hash doesnt exists
print ("Neuen Credentials-Hash erstellt: telegram:credentials")
create_cred()
else
cf:close()
end
local _file_cred = loadfile ("./data/credentials.lua")()
return _file_cred
return redis:hgetall("telegram:credentials")
end
-- Create a basic config.json file and saves it.
function create_config( )
-- A simple config with basic plugins and ourselves as privileged user
config = {
enabled_plugins = {
"help",
"plugins" },
sudo_users = {our_id},
disabled_channels = {}
}
serialize_to_file(config, './data/config.lua')
print ('Configuration in ./data/config.lua gespeichert')
end
function create_cred( )
-- create credentials hash with redis
function create_cred()
cred = {
bitly_access_token = "",
cloudconvert_apikey = "",
cloudinary_apikey = "",
cloudinary_api_secret = "",
cloudinary_public_id = "",
derpibooru_apikey = "",
fb_access_token = "",
flickr_apikey = "",
@ -247,15 +210,20 @@ function create_cred( )
ftp_username = "",
ftp_password = "",
gender_apikey = "",
golem_apikey = "",
google_apikey = "",
google_cse_id = "",
gitlab_private_token = "",
gitlab_project_id = "",
instagram_access_token = "",
lyricsnmusic_apikey = "",
mal_username = "",
mal_pw = "",
neutrino_userid = "",
neutrino_apikey = "",
owm_apikey = "",
page2images_restkey = "",
soundcloud_client_id = "",
superfeedr_authorization = "",
tw_consumer_key = "",
tw_consumer_secret = "",
tw_access_token = "",
@ -266,8 +234,28 @@ function create_cred( )
yourls_site_url = "",
yourls_signature_token = ""
}
serialize_to_file(cred, './data/credentials.lua')
print ('Credentials gespeichert in ./data/credentials.lua')
redis:hmset("telegram:credentials", cred)
print ('Credentials gespeichert in telegram:credentials')
end
function create_sudo_users()
redis:sadd("telegram:sudo_users", '0')
redis:sadd("telegram:sudo_users", '1')
redis:sadd("telegram:sudo_users", our_id)
print('Speichere Superuser in telegram:sudo_users')
print('Adde deine ID mit Redis: SADD telegram:sudo_users YOURID')
end
-- create plugin set if it doesn't exist
function create_plugin_set()
enabled_plugins = {
"plugins",
"manager"
}
print ('enabling a few plugins - saving to redis set telegram:enabled_plugins')
for _,plugin in pairs(enabled_plugins) do
redis:sadd("telegram:enabled_plugins", plugin)
end
end
function on_our_id (id)
@ -291,7 +279,11 @@ end
-- Enable plugins in config.json
function load_plugins()
for k, v in pairs(_config.enabled_plugins) do
enabled_plugins = redis:smembers('telegram:enabled_plugins')
if not enabled_plugins[1] then
create_plugin_set()
end
for k, v in pairs(enabled_plugins) do
print("Lade Plugin", v)
local ok, err = pcall(function()
@ -300,9 +292,10 @@ function load_plugins()
end)
if not ok then
print('\27[31mFehler beim laden des Plugins '..v..'\27[39m')
print('\27[31mFehler beim Laden vom Plugin '..v..'\27[39m')
print('\27[31m'..err..'\27[39m')
end
end
end
@ -324,4 +317,5 @@ end
our_id = 0
now = os.time()
math.randomseed(now)
started = false
started = false

View File

@ -1,14 +1,14 @@
URL = require "socket.url"
http = require "socket.http"
https = require "ssl.https"
http = require("socket.http")
https = require("ssl.https")
ltn12 = require "ltn12"
serpent = require "serpent"
feedparser = require "feedparser"
URL = require("socket.url")
feedparser = require ("feedparser")
json = (loadfile "./libs/JSON.lua")()
serpent = (loadfile "./libs/serpent.lua")()
mimetype = (loadfile "./libs/mimetype.lua")()
redis = (loadfile "./libs/redis.lua")()
http.TIMEOUT = 10
http.TIMEOUT = 5
function get_receiver(msg)
if msg.to.type == 'user' then
@ -19,7 +19,7 @@ function get_receiver(msg)
end
if msg.to.type == 'encr_chat' then
return msg.to.print_name
end
end
end
function is_chat_msg( msg )
@ -45,15 +45,9 @@ function string:split(sep)
return fields
end
-- DEPRECATED
function string.trim(s)
print("string.trim(s) is DEPRECATED use string:trim() instead")
return s:gsub("^%s*(.-)%s*$", "%1")
end
-- Removes spaces
function string:trim()
return self:gsub("^%s*(.-)%s*$", "%1")
function string.trim(s)
return s:gsub("^%s*(.-)%s*$", "%1")
end
function get_http_file_name(url, headers)
@ -63,17 +57,18 @@ function get_http_file_name(url, headers)
file_name = file_name or url:match("[^%w]+(%w+)[^%w]+$")
-- Random name, hope content-type works
file_name = file_name or str:random(5)
local content_type = headers["content-type"]
local content_type = headers["content-type"]
local extension = nil
if content_type then
extension = mimetype.get_mime_extension(content_type)
extension = mimetype.get_mime_extension(content_type)
end
if extension then
file_name = file_name.."."..extension
end
local disposition = headers["content-disposition"]
if disposition then
-- attachment; filename=CodeCogsEqn.png
@ -95,7 +90,7 @@ function download_to_file(url, file_name)
url = url,
sink = ltn12.sink.table(respbody),
redirect = true
}
}
-- nil, code, headers, status
local response = nil
@ -147,16 +142,15 @@ function run_command(str)
return result
end
-- User has privileges
function is_sudo(msg)
local var = false
-- Check users id in config
for v,user in pairs(_config.sudo_users) do
if user == msg.from.id then
var = true
end
end
return var
local var = false
-- Check if user id is in sudoers table
for v,user in pairs(sudo_users) do
if string.match(user, msg.from.id) then
var = true
end
end
return var
end
function can_use_bot(msg)
@ -172,11 +166,11 @@ end
-- Returns the name of the sender
function get_name(msg)
local name = msg.from.first_name
if name == nil then
name = msg.from.id
end
return name
local name = msg.from.first_name
if name == nil then
name = msg.from.id
end
return name
end
-- Returns at table of lua files inside plugins
@ -255,27 +249,33 @@ end
-- Download the image and send to receiver, it will be deleted.
-- cb_function and cb_extra are optionals callback
function send_photo_from_url(receiver, url, cb_function, cb_extra)
function send_photo_from_url(receiver, url, cb_function, cb_extra, sendNotErrMsg)
-- If callback not provided
cb_function = cb_function or ok_cb
cb_extra = cb_extra or false
local file_path = download_to_file(url, false)
if not file_path then -- Error
local text = 'Fehler beim laden des Bildes'
send_msg(receiver, text, cb_function, cb_extra)
if sendNotErrMsg then
return false
else
local text = 'Fehler beim Laden des Bildes'
send_msg(receiver, text, cb_function, cb_extra)
end
else
print("Datei Pfad: "..file_path)
_send_photo(receiver, file_path, cb_function, cb_extra)
return true
end
end
-- Same as send_photo_from_url but as callback function
function send_photo_from_url_callback(cb_extra, success, result)
function send_photo_from_url_callback(cb_extra, success, result, sendErrMsg)
local receiver = cb_extra.receiver
local url = cb_extra.url
local file_path = download_to_file(url, false)
if not file_path then -- Error
local text = 'Fehler beim laden des Bildes'
local text = 'Fehler beim Herunterladen des Bildes'
send_msg(receiver, text, ok_cb, false)
else
print("Datei Pfad: "..file_path)
@ -283,6 +283,21 @@ function send_photo_from_url_callback(cb_extra, success, result)
end
end
-- Same as above, but with send_as_document
function send_document_from_url_callback(cb_extra, success, result)
local receiver = cb_extra.receiver
local url = cb_extra.url
local file_path = download_to_file(url, false)
if not file_path then -- Error
local text = 'Fehler beim Herunterladen des Dokumentes'
send_msg(receiver, text, ok_cb, false)
else
print("File path: "..file_path)
_send_document(receiver, file_path, ok_cb, false)
end
end
-- Send multiple images asynchronous.
-- param urls must be a table.
function send_photos_from_url(receiver, urls)
@ -355,10 +370,24 @@ end
-- Download the image and send to receiver, it will be deleted.
-- cb_function and cb_extra are optionals callback
function send_document_from_url(receiver, url, cb_function, cb_extra)
function send_document_from_url(receiver, url, cb_function, cb_extra, sendNotErrMsg)
-- If callback not provided
cb_function = cb_function or ok_cb
cb_extra = cb_extra or false
local file_path = download_to_file(url, false)
print("Datei Pfad: "..file_path)
_send_document(receiver, file_path, cb_function, cb_extra)
if not file_path then -- Error
if sendNotErrMsg then
return false
else
local text = 'Fehler beim Herunterladen des Dokumentes'
send_msg(receiver, text, cb_function, cb_extra)
end
else
print("Datei Pfad: "..file_path)
_send_document(receiver, file_path, cb_function, cb_extra)
return true
end
end
-- Parameters in ?a=1&b=2 style
@ -409,48 +438,6 @@ function send_order_msg(destination, msgs)
send_order_msg_callback(cb_extra, true)
end
function send_order_msg_callback(cb_extra, success, result)
local destination = cb_extra.destination
local msgs = cb_extra.msgs
local file_path = cb_extra.file_path
if file_path ~= nil then
os.remove(file_path)
print("Deleted: " .. file_path)
end
if type(msgs) == 'string' then
send_large_msg(destination, msgs)
elseif type(msgs) ~= 'table' then
return
end
if #msgs < 1 then
return
end
local msg = table.remove(msgs, 1)
local new_cb_extra = {
destination = destination,
msgs = msgs
}
if type(msg) == 'string' then
send_msg(destination, msg, send_order_msg_callback, new_cb_extra)
elseif type(msg) == 'table' then
local typ = msg[1]
local nmsg = msg[2]
new_cb_extra.file_path = nmsg
if typ == 'document' then
send_document(destination, nmsg, send_order_msg_callback, new_cb_extra)
elseif typ == 'image' or typ == 'photo' then
send_photo(destination, nmsg, send_order_msg_callback, new_cb_extra)
elseif typ == 'audio' then
send_audio(destination, nmsg, send_order_msg_callback, new_cb_extra)
elseif typ == 'video' then
send_video(destination, nmsg, send_order_msg_callback, new_cb_extra)
else
send_file(destination, nmsg, send_order_msg_callback, new_cb_extra)
end
end
end
-- Same as send_large_msg_callback but friendly params
function send_large_msg(destination, text)
local cb_extra = {
destination = destination,
@ -486,17 +473,13 @@ function send_large_msg_callback(cb_extra, success, result)
end
-- Returns a table with matches or nil
function match_pattern(pattern, text, lower_case)
--function match_pattern(pattern, text, lower_case)
function match_pattern(pattern, text)
if text then
local matches = {}
if lower_case then
matches = { string.match(text:lower(), pattern) }
else
matches = { string.match(text, pattern) }
end
if next(matches) then
return matches
end
local matches = { string.match(text, pattern) }
if next(matches) then
return matches
end
end
-- nil
end
@ -521,7 +504,7 @@ function load_from_file(file, default_data)
return loadfile (file)()
end
function run_bash(str)
function run_bash(str)
local cmd = io.popen(str)
local result = cmd:read('*all')
cmd:close()
@ -536,11 +519,26 @@ function run_sh(msg)
return text
end
function round(num, idp)
if idp and idp>0 then
local mult = 10^idp
return math.floor(num * mult + 0.5) / mult
end
return math.floor(num + 0.5)
end
function unescape(str)
str = string.gsub( str, '&lt;', '<' )
str = string.gsub( str, '&gt;', '>' )
str = string.gsub( str, '&quot;', '"' )
str = string.gsub( str, '&apos;', "'" )
str = string.gsub( str, "&Auml;", "Ä")
str = string.gsub( str, "&auml;", "ä")
str = string.gsub( str, "&Ouml;", "Ö")
str = string.gsub( str, "&ouml;", "ö")
str = string.gsub( str, "Uuml;", "Ü")
str = string.gsub( str, "&uuml;", "ü")
str = string.gsub( str, "&szlig;", "ß")
str = string.gsub( str, '&#(%d+);', function(n) return string.char(n) end )
str = string.gsub( str, '&#x(%d+);', function(n) return string.char(tonumber(n,16)) end )
str = string.gsub( str, '&amp;', '&' ) -- Be sure to do this after all others
@ -563,4 +561,83 @@ function unescape_html(str)
return var
end)
return new
end
function post_petition(url, arguments)
local url, h = string.gsub(url, "http://", "")
local url, hs = string.gsub(url, "https://", "")
local post_prot = "http"
if hs == 1 then
post_prot = "https"
end
local response_body = {}
local request_constructor = {
url = post_prot..'://'..url,
method = "POST",
sink = ltn12.sink.table(response_body),
headers = {},
redirect = false
}
local source = arguments
if type(arguments) == "table" then
local source = helpers.url_encode_arguments(arguments)
end
request_constructor.headers["Content-Type"] = "application/x-www-form-urlencoded"
request_constructor.headers["Content-Length"] = tostring(#source)
request_constructor.source = ltn12.source.string(source)
if post_prot == "http" then
ok, response_code, response_headers, response_status_line = http.request(request_constructor)
else
ok, response_code, response_headers, response_status_line = https.request(request_constructor)
end
if not ok then
return nil
end
response_body = json:decode(table.concat(response_body))
return response_body
end
function get_redis_hash(msg, var)
if msg.to.type == 'chat' then
return 'chat:'..msg.to.id..':'..var
end
if msg.to.type == 'user' then
return 'user:'..msg.from.id..':'..var
end
end
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function comma_value(amount)
local formatted = amount
while true do
formatted, k = string.gsub(formatted, "^(-?%d+)(%d%d%d)", '%1.%2')
if (k==0) then
break
end
end
return formatted
end
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