2018-09-14 18:34:10 +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
|
|
|
|
|
|
|
|
|
|
|
|
local irc = nil
|
|
|
|
|
|
|
|
local CheckNotifications = function()
|
|
|
|
local Redis = GetRedisClient()
|
|
|
|
if Redis == nil then
|
|
|
|
return
|
|
|
|
end
|
|
|
|
while true do
|
|
|
|
local Data = Redis:rpoplpush('notifications:queue', 'notifications:processing')
|
|
|
|
if Data == nil then
|
|
|
|
Redis:quit()
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local Notification = json.decode.decode(Data)
|
|
|
|
local Line = ""
|
2020-03-20 14:53:42 +00:00
|
|
|
print("Sending notification: " .. Line)
|
2018-09-14 18:34:10 +00:00
|
|
|
if Notification['Notice'] ~= nil then
|
2020-03-20 14:53:42 +00:00
|
|
|
irc:Notice(Notification['Receiver'], Notification['Message'])
|
2018-09-14 18:34:10 +00:00
|
|
|
else
|
2020-03-20 14:53:42 +00:00
|
|
|
irc:Say(Notification['Receiver'], Notification['Message'])
|
2018-09-14 18:34:10 +00:00
|
|
|
end
|
|
|
|
Redis:lrem('notifications:processing', -1, Data)
|
|
|
|
end
|
|
|
|
Redis:quit()
|
|
|
|
end
|
|
|
|
|
|
|
|
local LastGlobalChecked = 0
|
|
|
|
plugin.AddHook('irc.Message', 'notificationgrabirc', function(Username, Channel, Message)
|
|
|
|
irc = Channel._irc
|
|
|
|
end)
|
|
|
|
|
|
|
|
plugin.AddHook('ReactorTick', 'notifications', function()
|
|
|
|
if irc == nil then
|
|
|
|
return
|
|
|
|
end
|
2020-03-20 14:53:42 +00:00
|
|
|
if LastGlobalChecked + 1 < os.time() then
|
2018-09-14 18:34:10 +00:00
|
|
|
LastGlobalChecked = os.time()
|
|
|
|
pcall(function()
|
|
|
|
CheckNotifications(irc)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|
|
|
|
plugin.AddCommand('redis', -1, function(Username, Channel, String)
|
|
|
|
local Redis = GetRedisClient()
|
|
|
|
if Redis == nil then
|
|
|
|
Channel:Say("Couldn't connect to Redis, lol.")
|
|
|
|
return
|
|
|
|
end
|
|
|
|
local Result = Redis:raw_cmd(String)
|
|
|
|
if Result == nil then
|
|
|
|
Channel:Say("OK, nil result.")
|
|
|
|
elseif type(Result) == "table" then
|
|
|
|
Channel:Say(table.concat(Result, ", "))
|
|
|
|
else
|
|
|
|
Channel:Say(tostring(Result))
|
|
|
|
end
|
|
|
|
Redis:quit()
|
|
|
|
end, "Runs stuff in Redis", 100)
|