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, Force) Force = Force or false 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") 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") if Now > LastChecked + 600 or Force then Redis:hset("paczkomate:objects:" .. ID, "lastchecked", Now) local NewStatus = GetPackStatus(ID) if NewStatus ~= nil and NewStatus ~= LastStatus then Redis:hset("paczkomate:objects:" .. ID, "status", NewStatus) local Message = string.format("Paczkomate %s status changed (%s -> %s)", ID, LastStatus, NewStatus) 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) end end end Redis:quit() end plugin.AddCommand('add-paczkomate', 1, function(Username, Channel, ID) local Status = GetPackStatus(ID) if Status == nil then Channel:Say(Username .. ": i am not aware of such rubber") return end local Redis = GetRedisClient() Redis:transaction(function(t) t:sadd('paczkomate:ids', ID) t:hset("paczkomate:objects:" .. ID, "channel", Channel.Name) t:hset("paczkomate:objects:" .. ID, "user", Username) t:hset("paczkomate:objects:" .. ID, "lastchecked", 0) t:hset("paczkomate:objects:" .. ID, "status", "none") end) Redis:quit() 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 Redis:quit() 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, true) Channel:Say("Done.") end)