2015-06-08 01:52:08 +02:00
|
|
|
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'
|
2015-05-28 16:47:30 +02:00
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
require("./bot/utils")
|
|
|
|
|
2015-04-14 20:21:23 +02:00
|
|
|
VERSION = '2.5-reloaded'
|
2014-12-14 20:52:48 +01:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
-- This function is called when tg receive a msg
|
2014-12-14 20:52:48 +01:00
|
|
|
function on_msg_receive (msg)
|
2015-04-19 20:12:54 +02:00
|
|
|
if not started then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2015-04-12 19:16:06 +02:00
|
|
|
local receiver = get_receiver(msg)
|
2015-06-08 01:52:08 +02:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
-- vardump(msg)
|
2015-05-28 16:47:30 +02:00
|
|
|
msg = pre_process_service_msg(msg)
|
2015-03-14 17:21:20 +01:00
|
|
|
if msg_valid(msg) then
|
|
|
|
msg = pre_process_msg(msg)
|
2015-05-28 16:47:30 +02:00
|
|
|
if msg then
|
|
|
|
match_plugins(msg)
|
|
|
|
--mark_read(receiver, ok_cb, false)
|
|
|
|
end
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function ok_cb(extra, success, result)
|
|
|
|
end
|
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
function on_binlog_replay_end()
|
2015-04-19 20:12:54 +02:00
|
|
|
started = true
|
2015-01-12 20:38:36 +01:00
|
|
|
postpone (cron_plugins, false, 60*5.0)
|
2015-05-28 16:47:30 +02:00
|
|
|
-- See plugins/isup.lua as an example for cron
|
2014-12-31 17:14:48 +01:00
|
|
|
|
|
|
|
_config = load_config()
|
2015-04-19 21:17:53 +02:00
|
|
|
cred_data = load_cred()
|
2014-12-31 17:14:48 +01:00
|
|
|
|
|
|
|
-- load plugins
|
|
|
|
plugins = {}
|
|
|
|
load_plugins()
|
|
|
|
end
|
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
function msg_valid(msg)
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Don't process outgoing messages
|
2014-12-14 20:52:48 +01:00
|
|
|
if msg.out then
|
2015-05-12 20:15:15 +02:00
|
|
|
print('\27[36mNicht gültig: Nachricht von mir\27[39m')
|
2014-12-14 20:52:48 +01:00
|
|
|
return false
|
|
|
|
end
|
2015-04-12 23:21:37 +02:00
|
|
|
|
|
|
|
-- Before bot was started
|
2015-03-10 21:06:15 +01:00
|
|
|
if msg.date < now then
|
2015-05-12 20:15:15 +02:00
|
|
|
print('\27[36mNicht gültig: alte Nachricht\27[39m')
|
2014-12-14 20:52:48 +01:00
|
|
|
return false
|
|
|
|
end
|
2015-04-12 23:21:37 +02:00
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
if msg.unread == 0 then
|
2015-05-12 20:15:15 +02:00
|
|
|
print('\27[36mNicht gültig: gelesen\27[39m')
|
2015-04-19 20:12:54 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2015-04-26 19:16:44 +02:00
|
|
|
if not msg.to.id then
|
2015-05-12 20:15:15 +02:00
|
|
|
print('\27[36mNicht gültig: To id not provided\27[39m')
|
2015-04-26 19:16:44 +02:00
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if not msg.from.id then
|
2015-05-12 20:15:15 +02:00
|
|
|
print('\27[36mNicht gültig: From id not provided\27[39m')
|
2014-12-14 20:52:48 +01:00
|
|
|
return false
|
|
|
|
end
|
2015-04-12 22:08:56 +02:00
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
if msg.from.id == our_id then
|
|
|
|
print('\27[36mNicht gültig: Nachricht von unserer ID\27[39m')
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if msg.to.type == 'encr_chat' then
|
|
|
|
print('\27[36mNicht gültig: Encrypted Chat\27[39m')
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
if msg.from.id == 777000 then
|
|
|
|
print('\27[36mNicht gültig: Telegram Nachricht\27[39m')
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
return true
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
--
|
|
|
|
function pre_process_service_msg(msg)
|
|
|
|
if msg.service then
|
|
|
|
local action = msg.action or {type=""}
|
|
|
|
-- Double / to discriminate of normal actions
|
|
|
|
msg.text = "//tgservice " .. action.type
|
|
|
|
|
|
|
|
-- wipe the data to allow the bot to read service messages
|
|
|
|
if msg.out then
|
|
|
|
msg.out = false
|
|
|
|
end
|
|
|
|
if msg.from.id == our_id then
|
|
|
|
msg.from.id = 0
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return msg
|
|
|
|
end
|
|
|
|
|
|
|
|
|
2015-04-12 20:12:41 +02:00
|
|
|
-- Apply plugin.pre_process function
|
|
|
|
function pre_process_msg(msg)
|
|
|
|
for name,plugin in pairs(plugins) do
|
2015-05-28 16:47:30 +02:00
|
|
|
if plugin.pre_process and msg then
|
2015-06-02 21:52:42 +02:00
|
|
|
-- print('Preprocess', name)
|
2015-04-12 20:12:41 +02:00
|
|
|
msg = plugin.pre_process(msg)
|
2014-12-26 12:13:27 +01:00
|
|
|
end
|
|
|
|
end
|
2015-03-14 17:21:20 +01:00
|
|
|
|
|
|
|
return msg
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Go over enabled plugins patterns.
|
|
|
|
function match_plugins(msg)
|
|
|
|
for name, plugin in pairs(plugins) do
|
2015-04-12 21:55:01 +02:00
|
|
|
match_plugin(plugin, name, msg)
|
2015-03-14 17:21:20 +01:00
|
|
|
end
|
2014-12-26 12:13:27 +01:00
|
|
|
end
|
|
|
|
|
2015-04-12 21:55:01 +02:00
|
|
|
-- 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
|
2015-06-02 21:52:42 +02:00
|
|
|
local warning = 'Plugin '..disabled_plugin..' ist in diesem Chat deaktiviert'
|
2015-04-12 21:55:01 +02:00
|
|
|
print(warning)
|
2015-06-02 21:52:42 +02:00
|
|
|
-- send_msg(receiver, warning, ok_cb, false)
|
2015-04-12 21:55:01 +02:00
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
function match_plugin(plugin, plugin_name, msg)
|
2014-12-14 20:52:48 +01:00
|
|
|
local receiver = get_receiver(msg)
|
2015-02-11 22:01:31 +01:00
|
|
|
|
2015-05-28 16:47:30 +02:00
|
|
|
-- Go over patterns. If one matches it's enough.
|
2015-03-14 17:21:20 +01:00
|
|
|
for k, pattern in pairs(plugin.patterns) do
|
2015-05-15 15:37:59 +02:00
|
|
|
local matches = match_pattern(pattern, msg.text)
|
2015-04-12 19:16:06 +02:00
|
|
|
if matches then
|
2015-05-12 20:15:15 +02:00
|
|
|
print("Nachricht stimmt überein mit ", pattern)
|
2015-05-03 13:51:33 +02:00
|
|
|
|
|
|
|
if is_plugin_disabled_on_chat(plugin_name, receiver) then
|
2015-04-12 21:55:01 +02:00
|
|
|
return nil
|
2015-05-03 13:51:33 +02:00
|
|
|
end
|
2015-03-14 17:21:20 +01:00
|
|
|
-- Function exists
|
2015-04-12 19:16:06 +02:00
|
|
|
if plugin.run then
|
2015-05-03 13:51:33 +02:00
|
|
|
-- 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 plugin is for privileged users only
|
2015-04-12 19:16:06 +02:00
|
|
|
if not warns_user_not_allowed(plugin, msg) then
|
|
|
|
local result = plugin.run(msg, matches)
|
|
|
|
if result then
|
2015-05-03 13:51:33 +02:00
|
|
|
send_large_msg(receiver, result)
|
2015-03-14 17:21:20 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2015-05-03 13:51:33 +02:00
|
|
|
end
|
2015-06-02 21:52:42 +02:00
|
|
|
-- One patterns matches
|
2015-03-14 17:21:20 +01:00
|
|
|
return
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-04-12 19:16:06 +02:00
|
|
|
-- DEPRECATED, use send_large_msg(destination, text)
|
|
|
|
function _send_msg(destination, text)
|
|
|
|
send_large_msg(destination, text)
|
2015-03-14 17:21:20 +01:00
|
|
|
end
|
|
|
|
|
2014-12-31 17:14:48 +01:00
|
|
|
-- Save the content of _config to config.lua
|
|
|
|
function save_config( )
|
2015-01-06 21:08:18 +01:00
|
|
|
serialize_to_file(_config, './data/config.lua')
|
2015-05-12 20:15:15 +02:00
|
|
|
print ('Configuration in ./data/config.lua gespeichert')
|
2014-12-31 17:14:48 +01:00
|
|
|
end
|
2014-10-09 15:09:06 +02:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
-- Returns the config from config.lua file.
|
2015-05-28 16:47:30 +02:00
|
|
|
-- If file doesn't exist, create it.
|
2014-12-31 17:14:48 +01:00
|
|
|
function load_config( )
|
2015-01-06 21:08:18 +01:00
|
|
|
local f = io.open('./data/config.lua', "r")
|
2015-06-02 21:52:42 +02:00
|
|
|
-- If config.lua doesn't exist
|
2014-12-31 17:14:48 +01:00
|
|
|
if not f then
|
2015-05-12 20:15:15 +02:00
|
|
|
print ("Neue Config-Datei erstellt: data/config.lua")
|
2014-12-31 17:14:48 +01:00
|
|
|
create_config()
|
2015-01-06 21:08:18 +01:00
|
|
|
else
|
|
|
|
f:close()
|
2014-12-31 17:14:48 +01:00
|
|
|
end
|
2015-01-06 21:08:18 +01:00
|
|
|
local config = loadfile ("./data/config.lua")()
|
2014-12-31 17:14:48 +01:00
|
|
|
for v,user in pairs(config.sudo_users) do
|
2015-05-12 20:15:15 +02:00
|
|
|
print("Erlaubter Benutzer: " .. user)
|
2014-12-31 17:14:48 +01:00
|
|
|
end
|
|
|
|
return config
|
|
|
|
end
|
|
|
|
|
2015-04-19 21:17:53 +02:00
|
|
|
function load_cred( )
|
|
|
|
local cf = io.open('./data/credentials.lua', "r")
|
|
|
|
-- If credentials.lua doesnt exists
|
|
|
|
if not cf then
|
2015-05-12 20:15:15 +02:00
|
|
|
print ("Neue Credentials-Datei erstellt: data/credentials.lua")
|
2015-04-19 21:17:53 +02:00
|
|
|
create_cred()
|
|
|
|
else
|
|
|
|
cf:close()
|
|
|
|
end
|
|
|
|
local _file_cred = loadfile ("./data/credentials.lua")()
|
|
|
|
return _file_cred
|
|
|
|
end
|
|
|
|
|
2014-12-31 17:14:48 +01:00
|
|
|
-- Create a basic config.json file and saves it.
|
|
|
|
function create_config( )
|
2015-05-28 16:47:30 +02:00
|
|
|
-- A simple config with basic plugins and ourselves as privileged user
|
2014-12-31 17:14:48 +01:00
|
|
|
config = {
|
|
|
|
enabled_plugins = {
|
2015-01-12 20:50:22 +01:00
|
|
|
"help",
|
2015-05-12 20:15:15 +02:00
|
|
|
"plugins" },
|
2015-02-27 14:08:05 +01:00
|
|
|
sudo_users = {our_id},
|
|
|
|
disabled_channels = {}
|
2014-12-31 17:14:48 +01:00
|
|
|
}
|
2015-01-06 21:08:18 +01:00
|
|
|
serialize_to_file(config, './data/config.lua')
|
2015-05-12 20:15:15 +02:00
|
|
|
print ('Configuration in ./data/config.lua gespeichert')
|
2014-12-14 20:52:48 +01:00
|
|
|
end
|
|
|
|
|
2015-04-19 21:17:53 +02:00
|
|
|
function create_cred( )
|
|
|
|
cred = {
|
|
|
|
bitly_access_token = "",
|
2015-04-22 16:47:41 +02:00
|
|
|
cloudconvert_apikey = "",
|
2015-04-23 17:30:43 +02:00
|
|
|
derpibooru_apikey = "",
|
2015-04-19 21:17:53 +02:00
|
|
|
fb_access_token = "",
|
|
|
|
gender_apikey = "",
|
2015-05-09 23:57:19 +02:00
|
|
|
google_apikey = "",
|
|
|
|
google_cse_id = "",
|
2015-04-19 21:17:53 +02:00
|
|
|
instagram_access_token = "",
|
|
|
|
lyricsnmusic_apikey = "",
|
|
|
|
neutrino_userid = "",
|
|
|
|
neutrino_apikey = "",
|
|
|
|
page2images_restkey = "",
|
|
|
|
soundcloud_client_id = "",
|
2015-05-10 13:09:06 +02:00
|
|
|
superfeedr_authorization = "",
|
2015-04-19 21:17:53 +02:00
|
|
|
tw_consumer_key = "",
|
|
|
|
tw_consumer_secret = "",
|
|
|
|
tw_access_token = "",
|
2015-05-09 23:57:19 +02:00
|
|
|
tw_access_token_secret = "",
|
|
|
|
x_mashape_key = "",
|
|
|
|
yandex_translate_apikey = "",
|
|
|
|
yandex_rich_content_apikey = ""
|
2015-04-19 21:17:53 +02:00
|
|
|
}
|
|
|
|
serialize_to_file(cred, './data/credentials.lua')
|
2015-05-12 20:15:15 +02:00
|
|
|
print ('Credentials gespeichert in ./data/credentials.lua')
|
2015-04-19 21:17:53 +02:00
|
|
|
end
|
|
|
|
|
2014-12-14 20:52:48 +01:00
|
|
|
function on_our_id (id)
|
|
|
|
our_id = id
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_user_update (user, what)
|
|
|
|
--vardump (user)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_chat_update (chat, what)
|
|
|
|
--vardump (chat)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_secret_chat_update (schat, what)
|
|
|
|
--vardump (schat)
|
|
|
|
end
|
|
|
|
|
|
|
|
function on_get_difference_end ()
|
|
|
|
end
|
|
|
|
|
2014-12-24 01:38:41 +01:00
|
|
|
-- Enable plugins in config.json
|
2014-12-16 15:05:42 +01:00
|
|
|
function load_plugins()
|
2014-12-31 17:14:48 +01:00
|
|
|
for k, v in pairs(_config.enabled_plugins) do
|
2015-05-12 20:15:15 +02:00
|
|
|
print("Lade Plugin", v)
|
2015-05-14 21:37:51 +02:00
|
|
|
|
|
|
|
local ok, err = pcall(function()
|
|
|
|
local t = loadfile("plugins/"..v..'.lua')()
|
|
|
|
plugins[v] = t
|
|
|
|
end)
|
|
|
|
|
|
|
|
if not ok then
|
|
|
|
print('\27[31mFehler beim laden des Plugins '..v..'\27[39m')
|
|
|
|
print('\27[31m'..err..'\27[39m')
|
|
|
|
end
|
2014-12-16 15:05:42 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
-- Call and postpone execution for cron plugins
|
2014-12-16 21:50:19 +01:00
|
|
|
function cron_plugins()
|
2014-12-16 15:05:42 +01:00
|
|
|
|
2015-03-14 17:21:20 +01:00
|
|
|
for name, plugin in pairs(plugins) do
|
|
|
|
-- Only plugins with cron function
|
|
|
|
if plugin.cron ~= nil then
|
|
|
|
plugin.cron()
|
2014-12-16 21:50:19 +01:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Called again in 5 mins
|
|
|
|
postpone (cron_plugins, false, 5*60.0)
|
2014-12-22 22:05:46 +01:00
|
|
|
end
|
|
|
|
|
|
|
|
-- Start and load values
|
|
|
|
our_id = 0
|
2015-01-24 17:18:13 +01:00
|
|
|
now = os.time()
|
2015-03-10 21:06:15 +01:00
|
|
|
math.randomseed(now)
|
2015-04-19 20:12:54 +02:00
|
|
|
started = false
|