This repository has been archived on 2021-04-24. You can view files and clone it, but cannot push or open issues or pull requests.
Mikubot/bot/bot.lua

226 lines
4.9 KiB
Lua
Raw Normal View History

require("./bot/utils")
2015-04-14 20:21:23 +02:00
VERSION = '2.5-reloaded'
-- This function is called when tg receive a msg
function on_msg_receive (msg)
local receiver = get_receiver(msg)
-- vardump(msg)
if msg_valid(msg) then
msg = pre_process_msg(msg)
match_plugins(msg)
mark_read(receiver, ok_cb, false)
end
end
function ok_cb(extra, success, result)
end
function on_binlog_replay_end()
started = 1
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("Not valid, msg from us")
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
print("Not valid, old msg")
return false
end
2015-04-12 23:21:37 +02:00
if msg.unread == 0 then
print("Not valid, readed")
return false
end
return true
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
if plugin.pre_process then
msg = plugin.pre_process(msg)
2014-12-26 12:13:27 +01:00
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
2014-12-26 12:13:27 +01:00
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
print(disabled_plugin)
if disabled_plugin == plugin_name and disabled then
2015-04-14 20:21:23 +02:00
local warning = ''
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)
2015-02-11 22:01:31 +01:00
-- 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( )
2015-01-06 21:08:18 +01:00
serialize_to_file(_config, './data/config.lua')
print ('saved config into ./data/config.lua')
end
2014-10-09 15:09:06 +02:00
-- Returns the config from config.lua file.
-- If file doesnt exists, create it.
function load_config( )
2015-01-06 21:08:18 +01:00
local f = io.open('./data/config.lua', "r")
-- If config.lua doesnt exists
if not f then
2015-01-06 21:08:18 +01:00
print ("Created new config file: data/config.lua")
create_config()
2015-01-06 21:08:18 +01:00
else
f:close()
end
2015-01-06 21:08:18 +01:00
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 = {
2015-01-12 20:29:18 +01:00
"9gag",
2015-02-15 12:00:33 +01:00
"eur",
2015-01-12 20:29:18 +01:00
"echo",
2015-02-15 12:00:33 +01:00
"btc",
"get",
2015-01-24 16:54:59 +01:00
"giphy",
"google",
2015-02-15 12:00:33 +01:00
"gps",
2015-01-12 20:50:22 +01:00
"help",
"images",
"img_google",
"location",
"media",
"plugins",
"channels",
2015-01-12 20:29:18 +01:00
"set",
"stats",
"time",
"version",
2015-01-24 16:54:59 +01:00
"weather",
2015-02-15 12:00:33 +01:00
"xkcd",
"youtube" },
sudo_users = {our_id},
disabled_channels = {}
}
2015-01-06 21:08:18 +01:00
serialize_to_file(config, './data/config.lua')
print ('saved config into ./data/config.lua')
end
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()
for k, v in pairs(_config.enabled_plugins) do
2014-12-24 01:38:41 +01:00
print("Loading plugin", v)
local t = loadfile("plugins/"..v..'.lua')()
2015-04-05 13:57:10 +02:00
plugins[v] = t
2014-12-16 15:05:42 +01:00
end
end
-- 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
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)