Skip to content
This repository has been archived by the owner on Oct 14, 2018. It is now read-only.

Bot main script moved to a module, so it can be restarted #365

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
237 changes: 8 additions & 229 deletions bot/bot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,209 +8,14 @@ VERSION = '0.12.2'

-- 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)
if msg_valid(msg) then
msg = pre_process_msg(msg)
if msg then
match_plugins(msg)
mark_read(receiver, ok_cb, false)
end
end
return BOT.on_msg_receive(msg)
end

function ok_cb(extra, success, result)
end

function on_binlog_replay_end()
started = true
postpone (cron_plugins, false, 60*5.0)
-- See plugins/ping.lua as an example for cron

_config = load_config()

-- load plugins
plugins = {}
load_plugins()
end

function msg_valid(msg)
-- Dont process outgoing messages
if msg.out then
print('\27[36mNot valid: msg from us\27[39m')
return false
end

-- Before bot was started
if msg.date < now then
print('\27[36mNot valid: old msg\27[39m')
return false
end

if msg.unread == 0 then
print('\27[36mNot valid: readed\27[39m')
return false
end

if msg.service then
print('\27[36mNot valid: service\27[39m')
return false
end

if not msg.to.id then
print('\27[36mNot valid: To id not provided\27[39m')
return false
end

if not msg.from.id then
print('\27[36mNot valid: From id not provided\27[39m')
return false
end

if msg.from.id == our_id then
print('\27[36mNot valid: Msg from our id\27[39m')
return false
end

if msg.to.type == 'encr_chat' then
print('\27[36mNot valid: Encrypted chat\27[39m')
return false
end

return true
end

-- Apply plugin.pre_process function
function pre_process_msg(msg)
for name,plugin in pairs(plugins) do
if plugin.pre_process and msg then
msg = plugin.pre_process(msg)
end
end

return msg
end

-- Go over enabled plugins patterns.
function match_plugins(msg)
for name, plugin in pairs(plugins) do
match_plugin(plugin, name, 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..' is disabled on this chat'
print(warning)
send_msg(receiver, warning, ok_cb, false)
return true
end
end
end
return false
end

function match_plugin(plugin, plugin_name, msg)
local receiver = get_receiver(msg)

-- Go over patterns. If one matches is enought.
for k, pattern in pairs(plugin.patterns) do
local matches = match_pattern(pattern, msg.text)
if matches then
print("msg matches: ", pattern)

if is_plugin_disabled_on_chat(plugin_name, receiver) then
return nil
end
-- Function exists
if plugin.run then
-- If plugin is for privileged users only
if not warns_user_not_allowed(plugin, msg) then
local result = plugin.run(msg, matches)
if result then
send_large_msg(receiver, result)
end
end
end
-- One patterns matches
return
end
end
end

-- DEPRECATED, use send_large_msg(destination, text)
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 ('saved config into ./data/config.lua')
end

-- Returns the config from config.lua file.
-- If file doesnt exists, create it.
function load_config( )
local f = io.open('./data/config.lua', "r")
-- If config.lua doesnt exists
if not f then
print ("Created new config file: 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("Allowed user: " .. user)
end
return config
end

-- Create a basic config.json file and saves it.
function create_config( )
-- A simple config with basic plugins and ourserves as priviled user
config = {
enabled_plugins = {
"9gag",
"eur",
"echo",
"btc",
"get",
"giphy",
"google",
"gps",
"help",
"images",
"img_google",
"location",
"media",
"plugins",
"channels",
"set",
"stats",
"time",
"version",
"weather",
"xkcd",
"youtube" },
sudo_users = {our_id},
disabled_channels = {}
}
serialize_to_file(config, './data/config.lua')
print ('saved config into ./data/config.lua')
reload_bot()
end

function on_our_id (id)
Expand All @@ -232,40 +37,14 @@ end
function on_get_difference_end ()
end

-- Enable plugins in config.json
function load_plugins()
for k, v in pairs(_config.enabled_plugins) do
print("Loading plugin", v)

local ok, err = pcall(function()
local t = loadfile("plugins/"..v..'.lua')()
plugins[v] = t
end)

if not ok then
print('\27[31mError loading plugin '..v..'\27[39m')
print('\27[31m'..err..'\27[39m')
end

end
end

-- Call and postpone execution for cron plugins
function cron_plugins()

for name, plugin in pairs(plugins) do
-- Only plugins with cron function
if plugin.cron ~= nil then
plugin.cron()
end
end

-- Called again in 5 mins
postpone (cron_plugins, false, 5*60.0)
function reload_bot()
BOT = nil
print("Loading bot module")
BOT = loadfile("./bot/botmodule.lua")()
return BOT.start()
end

-- Start and load values
BOT = nil
our_id = 0
now = os.time()
math.randomseed(now)
started = false
Loading