75 lines
1.8 KiB
Lua
75 lines
1.8 KiB
Lua
|
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 = ""
|
||
|
if Notification['Notice'] ~= nil then
|
||
|
Line = "NOTICE " .. Notification['Receiver'] .. " :" .. Notification['Message']
|
||
|
else
|
||
|
Line = "PRIVMSG " .. Notification['Receiver'] .. " :" .. Notification['Message']
|
||
|
end
|
||
|
print("Sending notification: " .. Line)
|
||
|
irc:_Send(Line)
|
||
|
|
||
|
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
|
||
|
if LastGlobalChecked + 5 < os.time() then
|
||
|
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)
|