-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatFilter.lua
More file actions
264 lines (239 loc) · 12.2 KB
/
Copy pathChatFilter.lua
File metadata and controls
264 lines (239 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
-- ============================================================
-- ChatFilter.lua — hides CleanBot's own whisper/command chatter from the chat
-- window to cut spam. Display-only: ChatFrame message filters intercept the chat-
-- frame display pipeline, NOT addon RegisterEvent handlers, so Bridge.lua keeps
-- parsing every line — we only stop the user-facing echo of the commands we send
-- and the replies we consume.
--
-- Gated on NS.hideBotChatter (Settings → "Hide Bot Chatter", default on; toggle off
-- to see raw traffic for testing). Read live on each call, so no /reload is needed.
-- ============================================================
local NS = CleanBotNS
local function enabled()
return NS.hideBotChatter ~= false
end
-- A whisper is suppressible when the other party is a bot we manage OR a current
-- group member. The group-member case covers the no-bridge discovery handshake, where
-- a member isn't in CleanBot_PartyBots yet but is being probed (the "co ?" we send and
-- the "Strategies:" reply). CB_FindPartyUnit walks party AND raid.
---@param name string|nil Whisper sender (incoming) or recipient (outgoing).
---@return boolean
local function suppressibleParty(name)
if not name or name == "" then return false end
if CleanBot_PartyBots[strlower(name)] then return true end
return (NS.CB_FindPartyUnit and NS.CB_FindPartyUnit(name) ~= nil) or false
end
-- Filters run once PER CHAT WINDOW displaying the event, so a filter that consumes a
-- tag must hand every window the same verdict for the same line — otherwise window 1
-- eats the tag and window 2 shows the echo. Each consuming filter remembers its last
-- (line key, frame time) decision and replays it for repeat invocations in the same
-- frame (all windows process a line at one GetTime()).
---@param cache table Per-filter { key=, at=, verdict= } memo.
---@param key string Identity of the line being filtered.
---@param decide fun():boolean Evaluates (and consumes) on the first invocation only.
---@return boolean
local function replayable(cache, key, decide)
local now = GetTime()
if cache.key == key and cache.at == now then return cache.verdict end
cache.key, cache.at, cache.verdict = key, now, decide()
return cache.verdict
end
-- Outgoing: hide only the command whispers CleanBot itself sent, so a command you type by hand
-- to a bot stays visible. Bridge.lua tags each addon-sent whisper in NS.selfWhispers (keyed by
-- recipient+text); we consume one matching tag per INFORM. No tag → it's a manual whisper, show
-- it. Tags older than SELF_WHISPER_TTL are purged: a failed send fires no INFORM, so its tag
-- must not linger and wrongly hide a later identical manual command.
local SELF_WHISPER_TTL = 3
local informSeen = {}
local function filterWhisperInform(_, _, msg, recipient)
if not enabled() or not suppressibleParty(recipient) then return false end
local key = strlower(recipient or "") .. "\0" .. (msg or "")
return replayable(informSeen, key, function()
local store = NS.selfWhispers
local list = store and store[key]
if not list then return false end
local now = GetTime()
while list[1] and (now - list[1]) > SELF_WHISPER_TTL do
table.remove(list, 1) -- drop stale tags (sends that never produced an INFORM)
end
if list[1] then
table.remove(list, 1) -- consume this addon-sent whisper
return true
end
return false
end)
end
-- Outgoing broadcast: hide the player's own party/raid echo of a command CleanBot broadcast (the
-- action bar buttons + Manage tab go through CB_SendGroupCommand, which tags each send in
-- NS.selfGroupMessages). Keyed by text, consumed one-per-echo with a short TTL so a failed send can't
-- leave a tag that hides a later identical line; the sender check keeps another member's identical
-- message visible. The overhear listener still sees the line (filters touch display only, not events).
local SELF_GROUP_TTL = 3
local groupSeen = {}
local function filterGroup(_, _, msg, sender)
if not enabled() then return false end
if not (NS.CB_IsSelfSender and NS.CB_IsSelfSender(sender, UnitName("player"))) then return false end
return replayable(groupSeen, (msg or "") .. "\0" .. (sender or ""), function()
local list = NS.selfGroupMessages and NS.selfGroupMessages[msg or ""]
if not list then return false end
local now = GetTime()
while list[1] and (now - list[1]) > SELF_GROUP_TTL do
table.remove(list, 1)
end
if list[1] then
table.remove(list, 1)
return true
end
return false
end)
end
NS.INVENTORY_BURST_SILENCE = 2.0
NS.botInInventoryBurst = NS.botInInventoryBurst or {}
--- Checks if a whisper line matches the structure of mod-playerbots inventory dump
--- ("=== Inventory ===", category headers "--- other ---", item links "|Hitem:", or "Discount up to:").
---@param msg string|nil
---@return boolean
local function isInventoryBurstLine(msg)
if not msg or msg == "" then return false end
if msg:match("^===%s*[iI]nventory%s*===") then return true end
if msg:match("^%s*%-%-%- .+ %-%-%-%s*$") then return true end
if msg:find("|Hitem:", 1, true) then return true end
if msg:match("^Discount up to:") then return true end
return false
end
--- Arms an extended reply window (2.0s) for an expected inventory dump from a bot (e.g. on TRADE_SHOW).
--- Avoids degradation if standard CB_MarkExpectReply (0.5s) was previously called.
---@param botName string
NS.CB_ArmInventoryBurst = function(botName)
if not enabled() or not botName or botName == "" then return end
local key = strlower(botName)
NS.botReplyWindow = NS.botReplyWindow or {}
NS.botInInventoryBurst = NS.botInInventoryBurst or {}
local now = GetTime()
local burstDeadline = now + (NS.INVENTORY_BURST_SILENCE or 2.0)
NS.botReplyWindow[key] = math.max(NS.botReplyWindow[key] or 0, burstDeadline)
NS.botInInventoryBurst[key] = true
end
-- Incoming: hide a bot's reply while its command-reply window is open (see CB_MarkExpectReply
-- in Bridge.lua) or during an inventory dump burst (TradeStatusAction::BeginTrade in mod-playerbots).
-- The window opens when we whisper a command or trade with a bot, and is slid forward on each
-- matching line, closing once the bot goes quiet. Unsolicited bot greetings (a bot's readiness
-- whisper, with no command before it) are intentionally NOT hidden.
local whisperSeen = {}
local function filterWhisper(_, _, msg, sender)
if not enabled() or not suppressibleParty(sender) then return false end
local replayKey = (msg or "") .. "\0" .. strlower(sender or "")
return replayable(whisperSeen, replayKey, function()
local key = strlower(sender)
local now = GetTime()
-- Inventory dump header
if msg and msg:match("^===%s*[iI]nventory%s*===") then
NS.botReplyWindow = NS.botReplyWindow or {}
NS.botInInventoryBurst = NS.botInInventoryBurst or {}
NS.botInInventoryBurst[key] = true
NS.botReplyWindow[key] = now + (NS.INVENTORY_BURST_SILENCE or 2.0)
return true
end
local deadline = NS.botReplyWindow and NS.botReplyWindow[key]
if deadline and now < deadline then
if NS.botInInventoryBurst and NS.botInInventoryBurst[key] then
if isInventoryBurstLine(msg) then
if msg:match("^Discount up to:") then
-- End of burst in random bots
NS.botInInventoryBurst[key] = nil
NS.botReplyWindow[key] = nil
else
NS.botReplyWindow[key] = now + (NS.INVENTORY_BURST_SILENCE or 2.0)
end
return true
end
return false
end
NS.botReplyWindow[key] = now + NS.WHISPER_SILENCE
return true
end
if NS.botInInventoryBurst and NS.botInInventoryBurst[key] then
NS.botInInventoryBurst[key] = nil
end
return false
end)
end
-- System: hide the server output CleanBot triggers and already parses — the self-bot
-- "player botAI" toggle line, the "Linked accounts:" dump, and the per-name bot-add
-- results. collectingLinked is tracked locally (not off Bridge's flag) so it stays
-- consistent regardless of filter-vs-handler ordering for the same line.
local collectingLinked = false
-- Result words that mark a line as ".playerbots bot add/addaccount/login/remove" output.
local BOT_CMD_RESULTS = {
"player already logged in",
"player is offline",
"not your bot",
" - ok",
"logged in",
}
local function filterSystem(_, _, msg)
if not enabled() or not msg then return false end
local lower = strlower(msg)
if lower:find("player botai", 1, true) then return true end
if lower:find("linked accounts", 1, true) then
collectingLinked = true
return true
end
if collectingLinked then
if msg:match("^%s*%-%s*%S+%s*$") then return true end -- "- NAME" row
collectingLinked = false -- non-row line ends the list
end
-- Per-name bot command results: "<cmd>: <Name> - <result>".
if msg:match("^%a+:%s+%S+%s+%-%s+") then
for _, word in ipairs(BOT_CMD_RESULTS) do
if lower:find(word, 1, true) then return true end
end
end
return false
end
-- World chat bubbles render as WorldFrame children, NOT through the chat-frame pipeline, so the
-- message filters above can't touch them. For our own broadcast commands we scan WorldFrame for the
-- bubble whose text matches and Hide() it every frame — only ours, leaving other members' bubbles
-- alone. A one-time Hide doesn't stick: the client owns the bubble's fade in/out alpha and re-shows
-- it, so we must keep re-hiding for the bubble's whole lifetime (a short single hide only swallows the
-- fade-in, then it pops back at full alpha). We match by TEXT and do NOT gate on IsShown, so (a) we
-- keep recognizing our own bubble after we've hidden it, and (b) once the client reuses that frame for
-- a different message its text no longer matches, so we never hide someone else's bubble. The window
-- comfortably covers a short command's bubble lifetime; cleared on expiry. Gated on Hide Bot Chatter.
local BUBBLE_HIDE_WINDOW = 6
local bubbleScanner
local pendingBubbles = {} -- [exact text] = expiry GetTime()
local function scanAndHideBubbles()
local now = GetTime()
for text, expiry in pairs(pendingBubbles) do
if now > expiry then pendingBubbles[text] = nil end
end
if not next(pendingBubbles) then bubbleScanner:SetScript("OnUpdate", nil) return end
for _, child in ipairs({ WorldFrame:GetChildren() }) do
if not (child.GetName and child:GetName()) then
for _, region in ipairs({ child:GetRegions() }) do
if region.GetObjectType and region:GetObjectType() == "FontString" then
local t = region:GetText()
if t and pendingBubbles[t] then child:Hide() end
end
end
end
end
end
--- Suppresses the world chat bubble for one of our own broadcast commands (no-op when chatter-hiding
--- is off). Call right after broadcasting the command text.
---@param text string Exact message text we just sent to party/raid.
NS.CB_HideOwnBubble = function(text)
if not enabled() or not text or text == "" then return end
pendingBubbles[text] = GetTime() + BUBBLE_HIDE_WINDOW
bubbleScanner = bubbleScanner or CreateFrame("Frame", "CleanBotBubbleScanner")
bubbleScanner:SetScript("OnUpdate", scanAndHideBubbles)
end
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER_INFORM", filterWhisperInform)
ChatFrame_AddMessageEventFilter("CHAT_MSG_WHISPER", filterWhisper)
ChatFrame_AddMessageEventFilter("CHAT_MSG_SYSTEM", filterSystem)
-- Our own broadcast echo arrives as PARTY/RAID (or the *_LEADER variant when we lead the group).
ChatFrame_AddMessageEventFilter("CHAT_MSG_PARTY", filterGroup)
ChatFrame_AddMessageEventFilter("CHAT_MSG_PARTY_LEADER", filterGroup)
ChatFrame_AddMessageEventFilter("CHAT_MSG_RAID", filterGroup)
ChatFrame_AddMessageEventFilter("CHAT_MSG_RAID_LEADER", filterGroup)