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

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