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 local function GetPackStatus(ID) local URL = "http://ppsa.app.q3k.org/api/1/parcel/" .. ID .. ".json" local Body, Code, Headers = http.request(URL) if Code ~= 200 then return "Invalid", true end local Data = json.decode.decode(Body) if Data.data.last ~= nil then return Data.data.last.name, Data.data.last.ending end return "Unknown", true end local function CheckPPSA(IRC, Force) Force = Force or false local Now = os.time() local Redis = GetRedisClient() if Redis == nil then return end for _, ID in pairs(Redis:smembers("poczta:ids")) do local LastChecked = tonumber(Redis:hget("poczta:objects:" .. ID, "lastchecked")) local LastStatus = Redis:hget("poczta:objects:" .. ID, "status") if Now > LastChecked + 600 or Force then Redis:hset("poczta:objects:" .. ID, "lastchecked", Now) local NewStatus, Ending = GetPackStatus(ID) if NewStatus ~= LastStatus then Redis:hset("poczta:objects:" .. ID, "status", NewStatus) local Message = string.format("PPSA %s status changed (%s -> %s)", ID, LastStatus, NewStatus) IRC:Say("#hackerspace-pl", Message) end if Ending then local Message = string.format("PPSA %s done, removing.", ID) IRC:Say("#hackerspace-pl", Message) Redis:srem("poczta:ids", ID) Redis:del("poczta:objects:" .. ID) end end end Redis:quit() end plugin.AddCommand('poczta', 1, function(Username, Channel, ID) local Status, Last = GetPackStatus(ID) Channel:Say(Username .. ": " .. Status) end) plugin.AddCommand('add-poczta', 1, function(Username, Channel, ID) local Redis = GetRedisClient() Redis:transaction(function(t) t:sadd('poczta:ids', ID) t:hset("poczta:objects:" .. ID, "lastchecked", 0) t:hset("poczta:objects:" .. ID, "status", "none") end) CheckPPSA(Channel._irc) Redis:quit() end, "Add a PPSA 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() CheckPPSA(Channel._irc) end) end end) plugin.AddCommand('list-poczta', 0, function(Username, Channel) local Redis = GetRedisClient() for _, ID in pairs(Redis:smembers("poczta:ids")) do local LastStatus = Redis:hget("poczta:objects:" .. ID, "status") Channel:Say(string.format(" - %s, status: %s", ID, LastStatus)) end Redis:quit() end) plugin.AddCommand('check-poczta', 0, function(Username, Channel) CheckPPSA(Channel._irc, true) Channel:Say("Done.") end)