Files
wow-Heimdall/Modules/Messenger.lua

177 lines
5.5 KiB
Lua

local _, shared = ...
---@cast shared HeimdallShared
local ModuleName = "Messenger"
---@class HeimdallMessengerConfig
---@field enabled boolean
---@field debug boolean
---@field interval number
---@class HeimdallMessengerData
---@field queue ReactiveValue<table<string, Message>>
---@field ticker Timer?
---@class Message
---@field message string
---@field channel string
---@field data string
---@class Messenger
shared.Messenger = {
---@param message Message
Enqueue = function(message) table.insert(shared.messenger.queue, message) end,
Init = function()
shared.messenger = {
queue = ReactiveValue.new({}),
}
local function FindOrJoinChannel(channelName, password)
local channelId = GetChannelName(channelName)
if channelId == 0 then
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Channel not found, joining: %s", ModuleName, channelName))
end
if password then
JoinPermanentChannel(channelName, password)
else
JoinPermanentChannel(channelName)
end
end
channelId = GetChannelName(channelName)
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Channel found with ID: %s (%s)", ModuleName, channelId, channelName))
end
return channelId
end
if not shared.messenger.ticker then
local function DoMessage()
-- if Heimdall_Data.config.messenger.debug then
-- print(
-- string.format(
-- "[%s] Processing message queue - Size: %d",
-- ModuleName,
-- #shared.messenger.queue:get()
-- )
-- )
-- end
if not Heimdall_Data.config.messenger.enabled then
-- if Heimdall_Data.config.messenger.debug then
-- print(string.format("[%s] Module disabled, skipping message processing", ModuleName))
-- end
return
end
---@type Message
local message = shared.messenger.queue[1]
if not message then
-- if Heimdall_Data.config.messenger.debug then
-- print(string.format("[%s] Message queue empty", ModuleName))
-- end
return
end
if Heimdall_Data.config.messenger.debug then shared.dump(message, "[%s] Processing message:") end
if not message.message or message.message == "" then
if Heimdall_Data.config.messenger.debug then
shared.dump(message, string.format("[%s] Invalid message: empty content", ModuleName))
end
return
end
if not message.channel or message.channel == "" then
if Heimdall_Data.config.messenger.debug then
shared.dump(message, string.format("[%s] Invalid message: no channel specified", ModuleName))
end
return
end
if string.find(message.channel, "^C") then
if Heimdall_Data.config.messenger.debug then
shared.dump(
message,
string.format("[%s] Converting channel type from C to CHANNEL", ModuleName)
)
end
message.channel = "CHANNEL"
elseif string.find(message.channel, "^W") then
if Heimdall_Data.config.messenger.debug then
shared.dump(
message,
string.format("[%s] Converting channel type from W to WHISPER", ModuleName)
)
end
message.channel = "WHISPER"
end
if message.channel == "CHANNEL" and message.data and string.match(message.data, "%D") then
if Heimdall_Data.config.messenger.debug then
shared.dump(message, string.format("[%s] Processing channel message:", ModuleName))
end
local channelId = GetChannelName(message.data)
if channelId == 0 then
if Heimdall_Data.config.messenger.debug then
shared.dump(message, string.format("[%s] Channel not found, joining:", ModuleName))
end
channelId = FindOrJoinChannel(message.data)
if Heimdall_Data.config.messenger.debug then
print(string.format("[%s] Channel join result - ID: %s", ModuleName, channelId))
end
end
message.data = tostring(channelId)
end
table.remove(shared.messenger.queue, 1)
if not message.message or message.message == "" then
if Heimdall_Data.config.messenger.debug then
shared.dump(message, string.format("[%s] Skipping empty message", ModuleName))
end
return
end
if not message.channel or message.channel == "" then
if Heimdall_Data.config.messenger.debug then
shared.dump(message, string.format("[%s] Skipping message with no channel", ModuleName))
end
return
end
if not message.data or message.data == "" then
if Heimdall_Data.config.messenger.debug then
shared.dump(message, string.format("[%s] Skipping message with no data", ModuleName))
end
return
end
if Heimdall_Data.config.messenger.debug then
shared.dump(message, string.format("[%s] Sending message:", ModuleName))
end
if string.len(message.message) > 255 then
shared.dump(message, string.format("[%s] Message too long!!!!: %s", ModuleName, message.message))
return
end
SendChatMessage(message.message, message.channel, nil, message.data)
end
local function Tick()
-- if Heimdall_Data.config.messenger.debug then
-- print(string.format("[%s] Tick - Queue size: %d", ModuleName, #shared.messenger.queue:get()))
-- end
DoMessage()
shared.messenger.ticker = C_Timer.NewTimer(Heimdall_Data.config.messenger.interval, Tick, 1)
end
Tick()
end
if Heimdall_Data.config.messenger.debug then
print(
string.format(
"[%s] Module initialized with interval: %s",
ModuleName,
Heimdall_Data.config.messenger.interval
)
)
end
print(string.format("[%s] Module initialized", ModuleName))
end,
}