-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathMessageBox.lua
More file actions
352 lines (295 loc) · 13.5 KB
/
MessageBox.lua
File metadata and controls
352 lines (295 loc) · 13.5 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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
-- MessageBox.lua
-- Main events, Slash commands, Init hooks
function MessageBox:OnLoad()
MessageBox.eventFrame:RegisterEvent("CHAT_MSG_WHISPER")
MessageBox.eventFrame:RegisterEvent("CHAT_MSG_WHISPER_INFORM")
MessageBox.eventFrame:RegisterEvent("PLAYER_LOGIN")
MessageBox.eventFrame:RegisterEvent("FRIENDLIST_UPDATE")
MessageBox.eventFrame:RegisterEvent("CHAT_MSG_SYSTEM")
MessageBox.eventFrame:RegisterEvent("WHO_LIST_UPDATE")
MessageBox.eventFrame:RegisterEvent("CHAT_MSG_AFK")
MessageBox.eventFrame:RegisterEvent("CHAT_MSG_DND")
SLASH_MESSAGEBOX1 = "/messagebox"
SLASH_MESSAGEBOX2 = "/mb"
SLASH_MESSAGEBOX3 = "/mbox"
SlashCmdList["MESSAGEBOX"] = function(msg)
if msg then
local cmd = string.lower(msg)
if cmd == "queue" then
MessageBox:PrintWhoQueue()
return
elseif cmd == "clear" then
MessageBox:ClearWhoQueue()
return
elseif string.find(cmd, "^stress") then
local _, _, arg1, arg2 = string.find(msg, "^%S+%s+(%d+)%s+(%d+)")
local numContacts = tonumber(arg1) or 1000
local numMessages = tonumber(arg2) or 1000
MessageBox:RunStressTest(numContacts, numMessages)
return
end
end
MessageBox:ToggleFrame()
end
MessageBox:SetupHooks()
MessageBox.whoElapsed = 0
MessageBox.eventFrame:SetScript("OnUpdate", function()
MessageBox.whoElapsed = MessageBox.whoElapsed + arg1
if MessageBox.whoElapsed < 1 then return end
MessageBox.whoElapsed = 0
MessageBox:ProcessWhoQueue()
-- Periodic crash save flush
if MessageBox.hasNampower then
MessageBox.flushElapsed = (MessageBox.flushElapsed or 0) + 1
if MessageBox.flushElapsed >= MessageBox.FLUSH_INTERVAL then
MessageBox.flushElapsed = 0
if MessageBox.crashSaveDirty then
MessageBox:FlushToDisk()
end
end
end
end)
DEFAULT_CHAT_FRAME:AddMessage("|cff3cb7f0Message|rBox: loaded! Use /messagebox, /mb, or /mbox to open.")
end
function MessageBox:OnEvent(event)
if event == "PLAYER_LOGIN" then
if not MessageBoxDB then
MessageBoxDB = {}
end
MessageBox.conversations = MessageBoxDB
if not MessageBoxSettings then
MessageBoxSettings = {}
for key, value in pairs(MessageBox.defaultSettings) do
MessageBoxSettings[key] = value
end
else
for key, value in pairs(MessageBox.defaultSettings) do
if MessageBoxSettings[key] == nil then
MessageBoxSettings[key] = value
end
end
end
MessageBox.settings = MessageBoxSettings
MessageBox.unreadCounts = MessageBox.settings.unreadCounts
-- Detect Nampower file I/O and attempt crash recovery
MessageBox.hasNampower = (WriteCustomFile ~= nil and ReadCustomFile ~= nil and CustomFileExists ~= nil)
if MessageBox.hasNampower then
MessageBox:AttemptCrashRecovery()
end
-- Register PLAYER_LOGOUT to mark a clean exit
MessageBox.eventFrame:RegisterEvent("PLAYER_LOGOUT")
-- Restore persistent GM flags into playerCache
if MessageBox.settings.gmList then
for name, _ in pairs(MessageBox.settings.gmList) do
if not MessageBox.playerCache[name] then
MessageBox.playerCache[name] = {}
end
MessageBox.playerCache[name].isGM = true
end
end
-- Restore persistent class/level data into playerCache
if MessageBox.settings.classCache then
for name, info in pairs(MessageBox.settings.classCache) do
if not MessageBox.playerCache[name] then
MessageBox.playerCache[name] = {}
end
if info.class then
MessageBox.playerCache[name].class = info.class
MessageBox.playerCache[name].classUpper = info.classUpper
end
if info.level then
MessageBox.playerCache[name].level = info.level
end
end
end
MessageBox.searchQuery = ""
MessageBox:CreateMinimapButton()
elseif event == "CHAT_MSG_WHISPER" then
local message = arg1
local sender = arg2
-- arg6 == "GM" means the sender is a Game Master
if arg6 == "GM" then
if not MessageBox.playerCache[sender] then
MessageBox.playerCache[sender] = {}
end
MessageBox.playerCache[sender].isGM = true
-- Persist GM status
if MessageBox.settings.gmList then
MessageBox.settings.gmList[sender] = true
end
else
MessageBox:AddToWhoQueue(sender)
end
MessageBox:AddMessage(sender, message, false)
elseif event == "CHAT_MSG_WHISPER_INFORM" then
local message = arg1
local recipient = arg2
-- arg6 == "GM" means the recipient is a Game Master
if arg6 == "GM" then
if not MessageBox.playerCache[recipient] then
MessageBox.playerCache[recipient] = {}
end
MessageBox.playerCache[recipient].isGM = true
-- Persist GM status so it survives reload/relog
if MessageBox.settings.gmList then
MessageBox.settings.gmList[recipient] = true
end
else
MessageBox:AddToWhoQueue(recipient)
end
local convo = MessageBox.conversations[recipient]
if convo and convo.messages then
local count = MessageBox:GetCount(convo)
if count > 0 then
-- Strip color codes for comparison in case the server
-- returns a modified version (TurtleWoW GM whispers)
local storedMsg = string.gsub(convo.messages[count], "|c%x%x%x%x%x%x%x%x", "")
storedMsg = string.gsub(storedMsg, "|r", "")
local incomingMsg = string.gsub(message, "|c%x%x%x%x%x%x%x%x", "")
incomingMsg = string.gsub(incomingMsg, "|r", "")
if convo.outgoing[count] and storedMsg == incomingMsg then
return
end
end
end
MessageBox:AddMessage(recipient, message, true)
elseif event == "CHAT_MSG_SYSTEM" then
local sysMessage = arg1
local _, _, playerName = string.find(sysMessage, "No player named '(.+)' is currently playing.")
if playerName then
local convo = MessageBox.conversations[playerName]
if convo and convo.messages and MessageBox:GetCount(convo) > 0 then
local count = MessageBox:GetCount(convo)
if convo.outgoing[count] then
MessageBox:RemoveLastMessage(playerName)
MessageBox:AddSystemMessage(playerName, playerName .. " is offline.", true)
end
end
end
elseif event == "FRIENDLIST_UPDATE" then
if MessageBox.frame and MessageBox.frame:IsVisible() then
MessageBox:MarkContactListDirty()
end
elseif event == "WHO_LIST_UPDATE" then
MessageBox:HandleWhoResult()
elseif event == "CHAT_MSG_AFK" then
local message = arg1
local sender = arg2
if sender and MessageBox.conversations[sender] then
if not MessageBox.playerCache[sender] then MessageBox.playerCache[sender] = {} end
MessageBox.playerCache[sender].status = "<AFK>"
MessageBox:AddSystemMessage(sender, sender .. " is AFK: " .. (message or "Away from Keyboard"))
if MessageBox.frame and MessageBox.frame:IsVisible() and MessageBox.selectedContact == sender then
MessageBox:UpdateChatHeader()
end
end
elseif event == "CHAT_MSG_DND" then
local message = arg1
local sender = arg2
if sender and MessageBox.conversations[sender] then
if not MessageBox.playerCache[sender] then MessageBox.playerCache[sender] = {} end
MessageBox.playerCache[sender].status = "<DND>"
MessageBox:AddSystemMessage(sender, sender .. " is DND: " .. (message or "Do not Disturb"))
if MessageBox.frame and MessageBox.frame:IsVisible() and MessageBox.selectedContact == sender then
MessageBox:UpdateChatHeader()
end
end
elseif event == "PLAYER_LOGOUT" then
MessageBox:FlushToDisk()
MessageBox:WriteCrashSaveClean()
end
end
-- Debug
function MessageBox:RunStressTest(numContacts, numMessages)
numContacts = numContacts or 1000
numMessages = numMessages or 1000
DEFAULT_CHAT_FRAME:AddMessage("|cff3cb7f0Message|rBox: Generating " .. numContacts .. " dummies with " .. numMessages .. " messages each. Screen may freeze momentarily...")
local baseTime = time() - (numMessages * 60)
for k = 1, numContacts do
local name = "Dummy" .. k
MessageBox.conversations[name] = MessageBox:NewConversation()
local c = MessageBox.conversations[name]
for i = 1, numMessages do
local isOut = (math.mod(i, 2) == 0)
local txt = "Stress test message #" .. i .. " - This is a somewhat long line of text to ensure wrapping works correctly in the scrolling frame."
table.insert(c.messages, txt)
table.insert(c.times, baseTime + (i*60))
table.insert(c.outgoing, isOut)
table.insert(c.system, false)
end
c.count = numMessages
if math.mod(k, 10) == 0 then
MessageBox.unreadCounts[name] = 5
end
end
MessageBox.conversationOrderDirty = true
MessageBox:UpdateContactList()
MessageBox:UpdateMinimapBadge()
DEFAULT_CHAT_FRAME:AddMessage("|cff3cb7f0Message|rBox: Stress test data populated. Use the Delete All button to clear them later.")
end
local original_ContainerFrameItemButton_OnClick = ContainerFrameItemButton_OnClick
function ContainerFrameItemButton_OnClick(button, ignoreModifiers)
if button == "LeftButton" and IsShiftKeyDown() and not ignoreModifiers then
if MessageBox.whisperInput and MessageBox.whisperInput:IsVisible() then
local useMessageBox = false
if MessageBox.isInputFocused then
useMessageBox = true
elseif MessageBox.lastFocusTime and (GetTime() - MessageBox.lastFocusTime) < 3.0 then
useMessageBox = true
elseif not ChatFrameEditBox:IsVisible() then
useMessageBox = true
end
if useMessageBox then
local link = GetContainerItemLink(this:GetParent():GetID(), this:GetID())
if link then
MessageBox.whisperInput:Insert(link)
MessageBox.whisperInput:SetFocus()
return
end
end
end
end
return original_ContainerFrameItemButton_OnClick(button, ignoreModifiers)
end
local original_PaperDollItemSlotButton_OnClick = PaperDollItemSlotButton_OnClick
function PaperDollItemSlotButton_OnClick(button, ignoreModifiers)
if button == "LeftButton" and IsShiftKeyDown() and not ignoreModifiers then
if MessageBox.whisperInput and MessageBox.whisperInput:IsVisible() then
local useMessageBox = false
if MessageBox.isInputFocused then
useMessageBox = true
elseif MessageBox.lastFocusTime and (GetTime() - MessageBox.lastFocusTime) < 3.0 then
useMessageBox = true
elseif not ChatFrameEditBox:IsVisible() then
useMessageBox = true
end
if useMessageBox then
local link = GetInventoryItemLink("player", this:GetID())
if link then
MessageBox.whisperInput:Insert(link)
MessageBox.whisperInput:SetFocus()
return
end
end
end
end
return original_PaperDollItemSlotButton_OnClick(button, ignoreModifiers)
end
if not MessageBox.original_HandleModifiedItemClick then
MessageBox.original_HandleModifiedItemClick = HandleModifiedItemClick
end
function HandleModifiedItemClick(link)
if MessageBox.whisperInput and MessageBox.whisperInput:IsVisible() then
if MessageBox.isInputFocused or (MessageBox.lastFocusTime and (GetTime() - MessageBox.lastFocusTime) < 3.0) then
MessageBox.whisperInput:Insert(link)
MessageBox.whisperInput:SetFocus()
return
end
end
return MessageBox.original_HandleModifiedItemClick(link)
end
MessageBox.eventFrame = CreateFrame("Frame", "MessageBoxEventFrame")
MessageBox.eventFrame:SetScript("OnEvent", function()
MessageBox:OnEvent(event)
end)
MessageBox:OnLoad()