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:38:21 +00:00
|
|
|
local function CheckMate(IRC, Force)
|
|
|
|
Force = Force or false
|
2015-09-27 16:29:14 +00:00
|
|
|
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")
|
2019-02-13 15:34:35 +00:00
|
|
|
local Channel = Redis:hget("paczkomate:objects:" .. ID, "channel")
|
|
|
|
if Channel == nil then
|
|
|
|
Channel = "#hackerspace-pl-members"
|
|
|
|
end
|
|
|
|
local User = Redis:hget("paczkomate:objects:" .. ID, "user")
|
2015-09-27 16:38:21 +00:00
|
|
|
if Now > LastChecked + 600 or Force then
|
2015-09-27 16:36:41 +00:00
|
|
|
Redis:hset("paczkomate:objects:" .. ID, "lastchecked", Now)
|
|
|
|
local NewStatus = GetPackStatus(ID)
|
2019-02-13 15:34:35 +00:00
|
|
|
if NewStatus ~= nil and 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)
|
2019-02-13 15:34:35 +00:00
|
|
|
if User ~= nil then
|
|
|
|
Message = User .. ": " .. Message
|
|
|
|
end
|
|
|
|
IRC:Say(Channel, Message)
|
|
|
|
end
|
|
|
|
if NewStatus == "Delivered" then
|
|
|
|
local Message = string.format("Paczkomate %s done, removing.", ID)
|
|
|
|
if User ~= nil then
|
|
|
|
Message = User .. ": " .. Message
|
|
|
|
end
|
|
|
|
IRC:Say(Channel, Message)
|
|
|
|
Redis:srem("paczkomate:ids", ID)
|
|
|
|
Redis:del("paczkomate:objects:" .. ID)
|
2015-09-27 16:29:14 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-09-14 18:34:10 +00:00
|
|
|
Redis:quit()
|
2015-09-27 16:29:14 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
plugin.AddCommand('add-paczkomate', 1, function(Username, Channel, ID)
|
2019-02-13 15:34:35 +00:00
|
|
|
local Status = GetPackStatus(ID)
|
|
|
|
if Status == nil then
|
|
|
|
Channel:Say(Username .. ": i am not aware of such rubber")
|
|
|
|
return
|
|
|
|
end
|
2015-09-27 16:29:14 +00:00
|
|
|
local Redis = GetRedisClient()
|
|
|
|
Redis:transaction(function(t)
|
|
|
|
t:sadd('paczkomate:ids', ID)
|
2019-02-13 15:34:35 +00:00
|
|
|
t:hset("paczkomate:objects:" .. ID, "channel", Channel.Name)
|
|
|
|
t:hset("paczkomate:objects:" .. ID, "user", Username)
|
2015-09-27 16:29:14 +00:00
|
|
|
t:hset("paczkomate:objects:" .. ID, "lastchecked", 0)
|
|
|
|
t:hset("paczkomate:objects:" .. ID, "status", "none")
|
|
|
|
end)
|
2018-09-14 18:34:10 +00:00
|
|
|
Redis:quit()
|
2015-09-27 16:29:14 +00:00
|
|
|
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
|
2018-09-14 18:34:10 +00:00
|
|
|
Redis:quit()
|
2015-09-27 16:29:14 +00:00
|
|
|
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
|
2015-09-27 16:38:21 +00:00
|
|
|
CheckMate(Channel._irc, true)
|
2015-09-27 16:36:41 +00:00
|
|
|
Channel:Say("Done.")
|
|
|
|
end)
|