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("") if Start == nil then return nil end local End, _ = XML:find("", Start) if End == nil then return nil end return XML:sub(Start+1, End-1) end 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 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") if Now > LastChecked + 600 then Redis:hset("paczkomate:objects:" .. ID, "lastchecked", Now) local NewStatus = GetPackStatus(ID) if NewStatus ~= LastStatus then Redis:hset("paczkomate:objects:" .. ID, "status", NewStatus) local Message = string.format("Paczkomate %s status changed (%s -> %s)", ID, LastStatus, NewStatus) IRC:Say("#hackerspace-pl-members", Message) 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) plugin.AddCommand('list-paczkomate', 0, function(Username, Channel) 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) 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)