From 8b04aefd4571b21265aae89a265e938b99404e88 Mon Sep 17 00:00:00 2001 From: yago Date: Tue, 16 Dec 2014 21:50:19 +0100 Subject: [PATCH] Basic cron version --- bot/bot.lua | 30 +++++++++++++++++++++++------- plugins/ping.lua | 25 +++++++++++++++++-------- 2 files changed, 40 insertions(+), 15 deletions(-) diff --git a/bot/bot.lua b/bot/bot.lua index 5419b7d..cc53369 100644 --- a/bot/bot.lua +++ b/bot/bot.lua @@ -59,11 +59,13 @@ function do_action(msg) matches = { string.match(text, pattern) } if matches[1] then print(" matches",pattern) - result = desc.run(msg, matches) - print(" sending", result) - if (result) then - _send_msg(receiver, result) - return + if desc.run ~= nil then + result = desc.run(msg, matches) + print(" sending", result) + if (result) then + _send_msg(receiver, result) + return + end end end end @@ -182,6 +184,10 @@ end function on_binlog_replay_end () started = 1 + load_plugins() + -- Uncomment the line to enable cron plugins. + -- cron_plugins() + -- See plugins/ping.lua as an example for cron end -- Start and load values @@ -195,7 +201,6 @@ plugins = {} -- load all plugins in the plugins/ directory function load_plugins() - plugins = {} for k, v in pairs(scandir("plugins")) do -- Load only lua files if (v:match(".lua$")) then @@ -206,5 +211,16 @@ function load_plugins() end end -load_plugins() +-- Cron all the enabled plugins +function cron_plugins() + for name, desc in pairs(plugins) do + if desc.cron ~= nil then + print("croned!"..desc.description) + desc.cron() + end + end + + -- Called again in 5 mins + postpone (cron_plugins, false, 5*60.0) +end \ No newline at end of file diff --git a/plugins/ping.lua b/plugins/ping.lua index 88bc2c5..553d9e4 100644 --- a/plugins/ping.lua +++ b/plugins/ping.lua @@ -1,12 +1,21 @@ -function run(msg, matches) - local receiver = get_receiver(msg) - print('receiver: '..receiver) - return "pong" +socket = require("socket") + +function cron() + -- Checks a TCP connexion + -- Use yours desired web and id + local addr = "your.web.site" + local dest = "user#id"..our_id + if not socket.connect(addr, 80) then + local text = "ALERT: "..addr.." is offline" + print (text) + send_msg(dest, text, ok_cb, false) + end end return { - description = "bot sends pong", - usage = "!ping", - patterns = {"^!ping$"}, - run = run + description = "If domain is offline, send msg to peer", + usage = "", + patterns = {}, + run = nil, + cron = cron }