145 lines
4.7 KiB
Lua
145 lines
4.7 KiB
Lua
local _, shared = ...
|
|
---@cast shared HeimdallShared
|
|
local ModuleName = "AgentTracker"
|
|
|
|
---@class AgentTrackerData
|
|
---@field agents ReactiveValue<table<string, string>>
|
|
|
|
---@class HeimdallAgentTrackerConfig
|
|
---@field enabled boolean
|
|
---@field debug boolean
|
|
---@field channels string[]
|
|
|
|
---@class AgentTracker
|
|
shared.AgentTracker = {
|
|
---@param name string
|
|
---@return boolean
|
|
Track = function(name)
|
|
if not name then return false end
|
|
local exists = shared.AgentTracker.IsAgent(name)
|
|
if exists then return false end
|
|
shared.agentTracker.agents[name] = date("%Y-%m-%dT%H:%M:%S")
|
|
-- Heimdall_Data.config.agents[name] = date("%Y-%m-%dT%H:%M:%S")
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Tracking new agent: %s", ModuleName, name))
|
|
shared.dump(shared.agentTracker.agents)
|
|
end
|
|
return true
|
|
end,
|
|
---@param name string
|
|
---@return boolean
|
|
IsAgent = function(name)
|
|
if not name then return false end
|
|
return shared.agentTracker.agents[name] ~= nil
|
|
end,
|
|
---@param callback fun(agent: string)
|
|
OnChange = function(callback) shared.agentTracker.agents:onChange(callback) end,
|
|
---@param callback fun(agent: string)
|
|
ForEach = function(callback)
|
|
---@type table<string, string>
|
|
local agents = shared.agentTracker.agents:get()
|
|
for name, _ in pairs(agents) do
|
|
callback(name)
|
|
end
|
|
end,
|
|
---@return nil
|
|
Init = function()
|
|
shared.agentTracker = {
|
|
agents = ReactiveValue.new(Heimdall_Data.config.agents),
|
|
}
|
|
|
|
--/run Heimdall_Data.config.agents["Cyheuraeth"]=date("%Y-%m-%dT%H:%M:%S")
|
|
---@type table<string, boolean>
|
|
local channelRosterFrame = CreateFrame("Frame")
|
|
channelRosterFrame:RegisterEvent("CHANNEL_ROSTER_UPDATE")
|
|
channelRosterFrame:SetScript("OnEvent", function(self, event, index)
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Channel roster update received", ModuleName))
|
|
end
|
|
if not Heimdall_Data.config.agentTracker.enabled then
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Module disabled, ignoring roster update", ModuleName))
|
|
end
|
|
return
|
|
end
|
|
local name = GetChannelDisplayInfo(index)
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Processing channel update: %s (index: %d)", ModuleName, name or "nil", index))
|
|
end
|
|
if name ~= Heimdall_Data.config.agentTracker.masterChannel then
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Ignoring non-master channel: %s", ModuleName, name or "nil"))
|
|
end
|
|
return
|
|
end
|
|
local count = select(5, GetChannelDisplayInfo(index))
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Processing %d members in channel", ModuleName, count))
|
|
end
|
|
|
|
local newAgents = 0
|
|
for i = 1, count do
|
|
name = GetChannelRosterInfo(index, i)
|
|
shared.AgentTracker.Track(name)
|
|
end
|
|
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Roster update complete - Added %d new agents", ModuleName, newAgents))
|
|
end
|
|
end)
|
|
|
|
local agentTrackerChannelSniffer = CreateFrame("Frame")
|
|
agentTrackerChannelSniffer:RegisterEvent("CHAT_MSG_CHANNEL")
|
|
agentTrackerChannelSniffer:SetScript("OnEvent", function(self, event, msg, sender, ...)
|
|
-- if Heimdall_Data.config.agentTracker.debug then
|
|
-- print(string.format("[%s] Channel message received from: %s", ModuleName, sender))
|
|
-- end
|
|
if not Heimdall_Data.config.agentTracker.enabled then
|
|
-- if Heimdall_Data.config.agentTracker.debug then
|
|
-- print(string.format("[%s] Module disabled, ignoring channel message", ModuleName))
|
|
-- end
|
|
return
|
|
end
|
|
|
|
local channelId = select(6, ...)
|
|
local _, channelname = GetChannelName(channelId)
|
|
local ok = false
|
|
for _, channel in pairs(Heimdall_Data.config.agentTracker.channels) do
|
|
if channel == channelname then
|
|
ok = true
|
|
break
|
|
end
|
|
end
|
|
if not ok then
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Channel name does not match any of the channels", ModuleName))
|
|
end
|
|
return
|
|
end
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Processing message from master channel: %s", ModuleName, sender))
|
|
end
|
|
|
|
sender = string.match(sender, "^[^-]+")
|
|
local new = shared.AgentTracker.Track(sender)
|
|
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(
|
|
string.format(
|
|
"[%s] %s agent from message: %s",
|
|
ModuleName,
|
|
new and "Added new" or "Updated existing",
|
|
sender
|
|
)
|
|
)
|
|
end
|
|
end)
|
|
|
|
if Heimdall_Data.config.agentTracker.debug then
|
|
print(string.format("[%s] Module initialized", ModuleName))
|
|
shared.dump(shared.agentTracker.agents:get(), "Agents")
|
|
end
|
|
print(string.format("[%s] Module initialized", ModuleName))
|
|
end,
|
|
}
|