Seen plugin

This commit is contained in:
Jinks 2015-10-24 03:47:44 +02:00
parent 1c1bae63d8
commit ebd4a30c70
4 changed files with 99 additions and 18 deletions

View file

@ -1,10 +1,14 @@
require "luarocks.loader"
local irc = require 'irc'
require('socket.http')
sqlite3 = require "lsqlite3"
plugin = {}
callback = {}
on_join = {}
on_part = {}
DB = sqlite3.open("bot.db")
require('config')
@ -29,6 +33,23 @@ irc.register_callback("channel_msg", function(channel, from, message)
end
end)
irc.register_callback("join", function(channel, from)
for k,v in pairs(on_join) do
if type(v) == "function" then
v(channel.name, from)
end
end
end)
irc.register_callback("part", function(channel, from, message)
for k,v in pairs(on_part) do
if type(v) == "function" then
v(channel.name, from)
end
end
end)
-- irc.register_callback("private_msg", function(from, message)
-- local is_cmd, cmd, arg = message:match("^(!)(%w+) (.*)$")
-- if is_cmd and plugin[cmd] then
@ -44,5 +65,6 @@ end)
irc.register_callback("nick_change", function(from, old_nick)
end)
--]]
irc.connect{network = network, port = port, nick = nick, username = username, realname = realname, pass = password}

29
misc.lua Normal file
View file

@ -0,0 +1,29 @@
local function pluralize (val, str)
if val > 1 or val == 0 then
return val.." "..str.."s"
else
return val.." "..str
end
end
function elapsed (datestring)
local pattern = "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)Z"
local year,month,day,hour,min,sec = datestring:match(pattern)
local start = os.time({year = year, month = month, day = day, hour = hour, min = min, sec = sec, isdst = false })
local utcnow = os.time(os.date("!*t"))
local diff = utcnow-start
local days = math.floor(diff/86400)
local hours = math.floor(diff/3600 - days*24)
local mins = math.floor(diff/60 - hours*60 - days*24)
local rval = ""
if days > 0 then
rval = pluralize(days, "day").." "
end
if hours > 0 then
rval = rval..pluralize(hours,"hour").." and "..pluralize(mins,"minute")
else
rval = rval..pluralize(mins,"minute")
end
return rval
end

46
plugin/seen.lua Normal file
View file

@ -0,0 +1,46 @@
DB:exec([[CREATE TABLE IF NOT EXISTS seen (
nick VARCHAR(100) PRIMARY KEY,
channel VARCHAR(100) NOT NULL,
action VARCHAR(20) NOT NULL,
time INTEGER NOT NULL)]])
local function update (channel, from, action)
local stmt = DB:prepare("INSERT OR REPLACE INTO seen VALUES (?, ?, ?, ?)")
stmt:bind_values(from, channel, action, os.time())
stmt:step()
stmt:finalize()
end
on_join.seen = function(target, from)
update(target, from, "joining")
end
on_part.seen = function (target, from, msg)
update(target, from, "leaving")
end
callback.seen = function (target, from, msg)
update(target, from, "talking")
end
plugin.seen = function (target, from, arg)
if not arg or arg == "" then
irc.say(target, "Give me a name plz!")
return
end
local stmt = DB:prepare("SELECT * FROM seen WHERE NICK = ?")
stmt:bind_values(arg:lower())
local status = stmt:step()
local result = nil
if status == sqlite3.ROW then result = stmt:get_named_values() end
stmt:finalize()
if status and result then
local c = " "
if result.action == "talking" then
c = " in "
end
irc.say(target, result.nick.." was last seen "..result.action..c..result.channel.." "..elapsed(os.date("!%Y-%m-%dT%H:%M:%SZ",result.time)).." ago.")
else
irc.say(target, "I cannot find anything about "..arg)
end
end

View file

@ -3,6 +3,7 @@
local json = require("json")
local https = require("ssl.https")
local ltn12 = require("ltn12")
local msc = require("misc")
local function fetch_data (endpoint)
local response = {}
@ -12,23 +13,6 @@ local function fetch_data (endpoint)
return json.decode(table.concat(response))
end
local function elapsed (datestring)
local pattern = "(%d+)-(%d+)-(%d+)T(%d+):(%d+):(%d+)Z"
local year,month,day,hour,min,sec = datestring:match(pattern)
local start = os.time({year = year, month = month, day = day, hour = hour, min = min, sec = sec, isdst = false })
local utcnow = os.time(os.date("!*t"))
local diff = utcnow-start
local hours = math.floor(diff/3600)
local mins = math.floor(diff/60 - hours*60)
h = hours > 1 and " hours" or " hour"
m = mins > 1 and " minutes" or " minute"
if hours > 0 then
return hours..h.." and "..mins..m
else
return mins..m
end
end
plugin.uptime = function (target, from, arg)
local channel = string.sub(target, 2)
if arg and arg ~= "" then