2015-09-27 16:29:14 +00:00
|
|
|
local function GetRedisClient()
|
|
|
|
local Host = plugin.ConfigGet('server')
|
|
|
|
local Port = plugin.ConfigGet('port')
|
|
|
|
local Pass = plugin.ConfigGet('pass')
|
|
|
|
local Redis = redis.connect(Host, Port)
|
|
|
|
if Redis == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
Redis:auth(Pass)
|
|
|
|
return Redis
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- forgive me father for i have sinned
|
|
|
|
local function ParsePackXML(XML)
|
|
|
|
local _, Start = XML:find("<status>")
|
|
|
|
if Start == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
local End, _ = XML:find("</status>", Start)
|
|
|
|
if End == nil then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return XML:sub(Start+1, End-1)
|
|
|
|
end
|
|
|
|
|
2015-09-27 16:36:41 +00:00
|
|
|
local function GetPackStatus(ID)
|
|
|
|
local URL = "http://api.paczkomaty.pl/?do=getpackstatus&packcode=" .. ID
|
|
|
|
local Body, Code, Headers = http.request(URL)
|
|
|
|
if Code ~= 200 then
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return ParsePackXML(Body)
|
|
|
|
end
|
|
|
|
|
2015-09-27 16:29:14 +00:00
|
|
|
local function CheckMate(IRC)
|
|
|
|
local Now = os.time()
|
|
|
|
local Redis = GetRedisClient()
|
|
|
|
if Redis == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
for _, ID in pairs(Redis:smembers("paczkomate:ids")) do
|
|
|
|
local LastChecked = tonumber(Redis:hget("paczkomate:objects:" .. ID, "lastchecked"))
|
|
|
|
local LastStatus = Redis:hget("paczkomate:objects:" .. ID, "status")
|
2015-09-27 16:36:41 +00:00
|
|
|
if Now > LastChecked + 600 then
|
|
|
|
Redis:hset("paczkomate:objects:" .. ID, "lastchecked", Now)
|
|
|
|
local NewStatus = GetPackStatus(ID)
|
|
|
|
if NewStatus ~= LastStatus then
|
2015-09-27 16:29:14 +00:00
|
|
|
Redis:hset("paczkomate:objects:" .. ID, "status", NewStatus)
|
2015-09-27 16:36:41 +00:00
|
|
|
local Message = string.format("Paczkomate %s status changed (%s -> %s)", ID, LastStatus, NewStatus)
|
|
|
|
IRC:Say("#hackerspace-pl-members", Message)
|
2015-09-27 16:29:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
plugin.AddCommand('add-paczkomate', 1, function(Username, Channel, ID)
|
|
|
|
local Redis = GetRedisClient()
|
|
|
|
Redis:transaction(function(t)
|
|
|
|
t:sadd('paczkomate:ids', ID)
|
|
|
|
t:hset("paczkomate:objects:" .. ID, "lastchecked", 0)
|
|
|
|
t:hset("paczkomate:objects:" .. ID, "status", "none")
|
|
|
|
end)
|
|
|
|
CheckMate(Channel._irc)
|
|
|
|
end, "Add a InPost tracking number", 10)
|
|
|
|
|
|
|
|
local LastGlobalChecked = 0
|
|
|
|
plugin.AddHook('irc.Message', 'testmate', function(Username, Channel, Message)
|
|
|
|
if LastGlobalChecked + 10 < os.time() then
|
|
|
|
LastGlobalChecked = os.time()
|
|
|
|
pcall(function()
|
|
|
|
CheckMate(Channel._irc)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
2015-09-27 16:36:41 +00:00
|
|
|
plugin.AddCommand('list-paczkomate', 0, function(Username, Channel)
|
2015-09-27 16:29:14 +00:00
|
|
|
if Channel.Name ~= "#hackerspace-pl-members" then
|
|
|
|
Channel:Say("#hackerspace-pl-members only, dear.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local Redis = GetRedisClient()
|
|
|
|
for _, ID in pairs(Redis:smembers("paczkomate:ids")) do
|
|
|
|
local LastStatus = Redis:hget("paczkomate:objects:" .. ID, "status")
|
|
|
|
Channel:Say(string.format(" - %s, status: %s", ID, LastStatus))
|
|
|
|
end
|
|
|
|
end)
|
2015-09-27 16:36:41 +00:00
|
|
|
|
|
|
|
plugin.AddCommand('check-paczkomate', 0, function(Username, Channel)
|
|
|
|
if Channel.Name ~= "#hackerspace-pl-members" then
|
|
|
|
Channel:Say("#hackerspace-pl-members only, dear.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
CheckMate(Channel._irc)
|
|
|
|
Channel:Say("Done.")
|
|
|
|
end)
|