177 lines
5.3 KiB
Lua
177 lines
5.3 KiB
Lua
local _, shared = ...
|
|
---@cast shared HeimdallShared
|
|
local ModuleName = "Messenger"
|
|
|
|
---@diagnostic disable-next-line: missing-fields
|
|
shared.Messenger = {}
|
|
function shared.Messenger.Init()
|
|
---@class Message
|
|
---@field message string
|
|
---@field channel string
|
|
---@field data string
|
|
|
|
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
|
|
|
|
---@diagnostic disable-next-line: missing-fields
|
|
if not shared.messenger then shared.messenger = {} end
|
|
if not shared.messenger.queue then shared.messenger.queue = {} 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))
|
|
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
|
|
print(
|
|
string.format(
|
|
"[%s] Processing message - Channel: %s, Data: %s",
|
|
ModuleName,
|
|
message.channel or "nil",
|
|
message.data or "nil"
|
|
)
|
|
)
|
|
print(string.format("[%s] Message content: %s", ModuleName, message.message or "nil"))
|
|
end
|
|
|
|
if not message.message or message.message == "" then
|
|
if Heimdall_Data.config.messenger.debug then
|
|
print(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
|
|
print(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
|
|
print(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
|
|
print(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
|
|
print(
|
|
string.format(
|
|
"[%s] Processing channel message: '%s' to '%s'",
|
|
ModuleName,
|
|
message.message,
|
|
message.data
|
|
)
|
|
)
|
|
end
|
|
local channelId = GetChannelName(message.data)
|
|
if channelId == 0 then
|
|
if Heimdall_Data.config.messenger.debug then
|
|
print(string.format("[%s] Channel not found, attempting to join: %s", ModuleName, message.data))
|
|
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
|
|
print(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
|
|
print(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
|
|
print(string.format("[%s] Skipping message with no data", ModuleName))
|
|
end
|
|
return
|
|
end
|
|
|
|
if Heimdall_Data.config.messenger.debug then
|
|
print(
|
|
string.format(
|
|
"[%s] Sending message: '%s' to %s:%s",
|
|
ModuleName,
|
|
message.message,
|
|
message.channel,
|
|
message.data
|
|
)
|
|
)
|
|
end
|
|
if string.len(message.message) > 255 then
|
|
print(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))
|
|
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("[Heimdall] Messenger loaded")
|
|
end
|