-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPublic.lua
423 lines (362 loc) · 16.8 KB
/
Public.lua
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
--[[
© Justin Snelgrove
© Morgane Parize
Permission to use, copy, modify, and distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
]]
local Chomp = LibStub:GetLibrary("Chomp", true)
local Internal = Chomp and Chomp.Internal or nil
if not Chomp or not Internal or not Internal.LOADING then
return
end
local DEFAULT_PRIORITY = "NORMAL"
local PRIORITIES_HASH = { HIGH = true, MEDIUM = true, LOW = true }
local PRIORITY_TO_CTL = { LOW = "BULK", MEDIUM = "NORMAL", HIGH = "ALERT" }
function Chomp.SendAddonMessage(prefix, text, kind, target, priority, queue, callback, callbackArg)
if type(prefix) ~= "string" then
error("Chomp.SendAddonMessage: prefix: expected string, got " .. type(prefix), 2)
elseif type(text) ~= "string" then
error("Chomp.SendAddonMessage: text: expected string, got " .. type(text), 2)
elseif kind == "WHISPER" and type(target) ~= "string" then
error("Chomp.SendAddonMessage: target: expected string, got " .. type(target), 2)
elseif kind == "CHANNEL" and type(target) ~= "number" then
error("Chomp.SendAddonMessage: target: expected number, got " .. type(target), 2)
elseif priority and not PRIORITIES_HASH[priority] then
error("Chomp.SendAddonMessage: priority: expected \"HIGH\", \"MEDIUM\", \"LOW\", or nil, got " .. tostring(priority), 2)
elseif queue and type(queue) ~= "string" then
error("Chomp.SendAddonMessage: queue: expected string or nil, got " .. type(queue), 2)
elseif callback and type(callback) ~= "function" then
error("Chomp.SendAddonMessage: callback: expected function or nil, got " .. type(callback), 2)
end
local length = #text
if length > 255 then
error("Chomp.SendAddonMessage: text length cannot exceed 255 bytes", 2)
elseif #prefix > 16 then
error("Chomp.SendAddonMessage: prefix: length cannot exceed 16 bytes", 2)
end
if not kind then
kind = "PARTY"
else
kind = kind:upper()
end
if target and kind == "WHISPER" then
target = Ambiguate(target, "none")
end
ChatThrottleLib:SendAddonMessage(PRIORITY_TO_CTL[priority] or DEFAULT_PRIORITY, prefix, text, kind, target, queue, callback, callbackArg)
end
function Chomp.SendAddonMessageLogged(prefix, text, kind, target, priority, queue, callback, callbackArg)
if type(prefix) ~= "string" then
error("Chomp.SendAddonMessageLogged: prefix: expected string, got " .. type(prefix), 2)
elseif type(text) ~= "string" then
error("Chomp.SendAddonMessageLogged: text: expected string, got " .. type(text), 2)
elseif kind == "WHISPER" and type(target) ~= "string" then
error("Chomp.SendAddonMessageLogged: target: expected string, got " .. type(target), 2)
elseif kind == "CHANNEL" and type(target) ~= "number" then
error("Chomp.SendAddonMessageLogged: target: expected number, got " .. type(target), 2)
elseif priority and not PRIORITIES_HASH[priority] then
error("Chomp.SendAddonMessageLogged: priority: expected \"HIGH\", \"MEDIUM\", \"LOW\", or nil, got " .. tostring(priority), 2)
elseif queue and type(queue) ~= "string" then
error("Chomp.SendAddonMessageLogged: queue: expected string or nil, got " .. type(queue), 2)
elseif callback and type(callback) ~= "function" then
error("Chomp.SendAddonMessageLogged: callback: expected function or nil, got " .. type(callback), 2)
end
local length = #text
if length > 255 then
error("Chomp.SendAddonMessageLogged: text length cannot exceed 255 bytes", 2)
elseif #prefix > 16 then
error("Chomp.SendAddonMessageLogged: prefix: length cannot exceed 16 bytes", 2)
end
if not kind then
kind = "PARTY"
else
kind = kind:upper()
end
if target and kind == "WHISPER" then
target = Ambiguate(target, "none")
end
ChatThrottleLib:SendAddonMessageLogged(PRIORITY_TO_CTL[priority] or DEFAULT_PRIORITY, prefix, text, kind, target, queue, callback, callbackArg)
end
function Chomp.SendChatMessage(text, kind, language, target, priority, queue, callback, callbackArg)
if type(text) ~= "string" then
error("Chomp.SendChatMessage: text: expected string, got " .. type(text), 2)
elseif language and type(language) ~= "string" and type(language) ~= "number" then
error("Chomp.SendChatMessage: language: expected string or number, got " .. type(language), 2)
elseif kind == "WHISPER" and type(target) ~= "string" then
error("Chomp.SendChatMessage: target: expected string, got " .. type(target), 2)
elseif kind == "CHANNEL" and type(target) ~= "number" then
error("Chomp.SendChatMessage: target: expected number, got " .. type(target), 2)
elseif priority and not PRIORITIES_HASH[priority] then
error("Chomp.SendChatMessage: priority: expected \"HIGH\", \"MEDIUM\", \"LOW\", or nil, got " .. tostring(priority), 2)
elseif queue and type(queue) ~= "string" then
error("Chomp.SendChatMessage: queue: expected string or nil, got " .. type(queue), 2)
elseif callback and type(callback) ~= "function" then
error("Chomp.SendChatMessage: callback: expected function or nil, got " .. type(callback), 2)
end
local length = #text
if length > 255 then
error("Chomp.SendChatMessage: text length cannot exceed 255 bytes", 2)
end
if not kind then
kind = "SAY"
else
kind = kind:upper()
end
if target and kind == "WHISPER" then
target = Ambiguate(target, "none")
end
ChatThrottleLib:SendChatMessage(PRIORITY_TO_CTL[priority] or DEFAULT_PRIORITY, "Chomp", text, kind, language, target, queue, callback, callbackArg)
end
function Chomp.BNSendGameData(bnetIDGameAccount, prefix, text, priority, queue, callback, callbackArg)
if type(prefix) ~= "string" then
error("Chomp.BNSendGameData: prefix: expected string, got " .. type(text), 2)
elseif type(text) ~= "string" then
error("Chomp.BNSendGameData: text: expected string, got " .. type(text), 2)
elseif type(bnetIDGameAccount) ~= "number" then
error("Chomp.BNSendGameData: bnetIDGameAccount: expected number, got " .. type(bnetIDGameAccount), 2)
elseif priority and not PRIORITIES_HASH[priority] then
error("Chomp.BNSendGameData: priority: expected \"HIGH\", \"MEDIUM\", \"LOW\", or nil, got " .. tostring(priority), 2)
elseif queue and type(queue) ~= "string" then
error("Chomp.BNSendGameData: queue: expected string or nil, got " .. type(queue), 2)
elseif callback and type(callback) ~= "function" then
error("Chomp.BNSendGameData: callback: expected function or nil, got " .. type(callback), 2)
end
local length = #text
if length > 255 then
error("Chomp.BNSendGameData: text: length cannot exceed 255 bytes", 2)
elseif #prefix > 16 then
error("Chomp.BNSendGameData: prefix: length cannot exceed 16 bytes", 2)
end
local kind = "WHISPER"
ChatThrottleLib:BNSendGameData(PRIORITY_TO_CTL[priority] or DEFAULT_PRIORITY, prefix, text, kind, bnetIDGameAccount, queue, callback, callbackArg)
end
function Chomp.IsSending()
-- v26+: Removed with no replacement.
return false
end
local DEFAULT_SETTINGS = {
fullMsgOnly = true,
validTypes = {
["string"] = true,
},
}
function Chomp.RegisterAddonPrefix(prefix, callback, prefixSettings)
local prefixType = type(prefix)
if prefixType ~= "string" then
error("Chomp.RegisterAddonPrefix: prefix: expected string, got " .. prefixType, 2)
elseif prefixType == "string" and #prefix > 16 then
error("Chomp.RegisterAddonPrefix: prefix: length cannot exceed 16 bytes", 2)
elseif type(callback) ~= "function" then
error("Chomp.RegisterAddonPrefix: callback: expected function, got " .. type(callback), 2)
elseif prefixSettings and type(prefixSettings) ~= "table" then
error("Chomp.RegisterAddonPrefix: prefixSettings: expected table or nil, got " .. type(prefixSettings), 2)
end
if not prefixSettings then
prefixSettings = DEFAULT_SETTINGS
end
if prefixSettings.validTypes and type(prefixSettings.validTypes) ~= "table" then
error("Chomp.RegisterAddonPrefix: prefixSettings.validTypes: expected table or nil, got " .. type(prefixSettings.validTypes), 2)
elseif prefixSettings.rawCallback and type(prefixSettings.rawCallback) ~= "function" then
error("Chomp.RegisterAddonPrefix: prefixSettings.rawCallback: expected function or nil, got " .. type(prefixSettings.rawCallback), 2)
end
local prefixData = Internal.Prefixes[prefix]
if not prefixData then
prefixData = {
callback = callback,
rawCallback = prefixSettings.rawCallback,
fullMsgOnly = prefixSettings.fullMsgOnly,
broadcastPrefix = prefixSettings.broadcastPrefix,
}
local validTypes = prefixSettings.validTypes or DEFAULT_SETTINGS.validTypes
prefixData.validTypes = {}
for dataType, func in pairs(Internal.Serialize) do
if validTypes[dataType] then
prefixData.validTypes[dataType] = true
end
end
Internal.Prefixes[prefix] = prefixData
if not C_ChatInfo.IsAddonMessagePrefixRegistered(prefix) then
C_ChatInfo.RegisterAddonMessagePrefix(prefix)
end
else
error("Chomp.RegisterAddonPrefix: prefix handler already registered, Chomp currently supports only one handler per prefix")
end
end
function Chomp.IsAddonPrefixRegistered(prefix)
return Internal.Prefixes[prefix] ~= nil
end
local nextSessionID = math.random(0, 4095)
local function SplitAndSend(sendFunc, maxSize, bitField, prefix, text, ...)
local textLen = #text
-- Subtract Chomp metadata from maximum size.
maxSize = maxSize - 12
local totalOffset = 0
local msgID = 0
local totalMsg = math.ceil(textLen / maxSize)
local sessionID = nextSessionID
nextSessionID = (nextSessionID + 1) % 4096
local position = 1
while position <= textLen do
-- Only *need* to do a safe substring for encoded channels, but doing so
-- always shouldn't hurt.
local msgText, offset = Chomp.SafeSubString(text, position, position + maxSize - 1, textLen)
if offset > 0 then
-- Update total offset and total message number if needed.
totalOffset = totalOffset + offset
totalMsg = math.ceil((textLen + totalOffset) / maxSize)
end
msgID = msgID + 1
msgText = ("%03X%03X%03X%03X%s"):format(bitField, sessionID, msgID, totalMsg, msgText)
sendFunc(prefix, msgText, ...)
position = position + maxSize - offset
end
end
local function ToInGame(bitField, prefix, text, kind, target, priority, queue)
return SplitAndSend(Chomp.SendAddonMessage, 255, bitField, prefix, text, kind, target, priority, queue)
end
local function ToInGameLogged(bitField, prefix, text, kind, target, priority, queue)
return SplitAndSend(Chomp.SendAddonMessageLogged, 255, bitField, prefix, text, kind, target, priority, queue)
end
local function BNSendGameDataRearrange(prefix, text, bnetIDGameAccount, ...)
return Chomp.BNSendGameData(bnetIDGameAccount, prefix, text, ...)
end
local function ToBattleNet(bitField, prefix, text, kind, bnetIDGameAccount, priority, queue)
return SplitAndSend(BNSendGameDataRearrange, 255, bitField, prefix, text, bnetIDGameAccount, priority, queue)
end
local DEFAULT_OPTIONS = {}
function Chomp.SmartAddonMessage(prefix, data, kind, target, messageOptions)
local prefixData = Internal.Prefixes[prefix]
if not prefixData then
error("Chomp.SmartAddonMessage: prefix: prefix has not been registered with Chomp", 2)
elseif type(kind) ~= "string" then
error("Chomp.SmartAddonMessage: kind: expected string, got " .. type(kind), 2)
elseif kind == "WHISPER" and type(target) ~= "string" then
error("Chomp.SmartAddonMessage: target: expected string, got " .. type(target), 2)
elseif kind == "CHANNEL" and type(target) ~= "number" then
error("Chomp.SmartAddonMessage: target: expected number, got " .. type(target), 2)
elseif target and kind ~= "WHISPER" and kind ~= "CHANNEL" then
error("Chomp.SmartAddonMessage: target: expected nil, got " .. type(target), 2)
end
if not messageOptions then
messageOptions = DEFAULT_OPTIONS
end
local dataType = type(data)
if not prefixData.validTypes[dataType] then
error("Chomp.SmartAddonMessage: data: type not registered as valid: " .. dataType, 2)
elseif dataType ~= "string" and not messageOptions.serialize then
error("Chomp.SmartAddonMessage: data: no serialization requested, but serialization required for type: " .. dataType, 2)
elseif messageOptions.priority and not PRIORITIES_HASH[messageOptions.priority] then
error("Chomp.SmartAddonMessage: messageOptions.priority: expected \"HIGH\", \"MEDIUM\", or \"LOW\", got " .. tostring(messageOptions.priority), 2)
elseif messageOptions.queue and type(messageOptions.queue) ~= "string" then
error("Chomp.SmartAddonMessage: messageOptions.queue: expected string or nil, got " .. type(messageOptions.queue), 2)
end
local bitField = 0x000
-- v20+: Always set the CODECV2 bit. All clients on the network at this
-- point should support it. Setting this bit unconditionally will
-- eventually allow us to deprecate receipt of v1 codec data.
bitField = bit.bor(bitField, Internal.BITS.VERSION16, Internal.BITS.CODECV2)
if messageOptions.serialize then
bitField = bit.bor(bitField, Internal.BITS.SERIALIZE)
data = Chomp.Serialize(data)
end
if not messageOptions.binaryBlob then
local permitted, reason = Chomp.CheckLoggedContents(data)
if not permitted then
error(("Chomp.SmartAddonMessage: data: messageOptions.binaryBlob not specified, but disallowed sequences found, code: %s"):format(reason), 2)
end
end
if kind == "WHISPER" then
target = Chomp.NameMergedRealm(target)
end
if kind == "WHISPER" then
-- GetBattleNetAccountID() only returns an ID for crossfaction and
-- crossrealm targets.
local bnetIDGameAccount = Internal:GetBattleNetAccountID(target)
if bnetIDGameAccount then
ToBattleNet(bitField, prefix, Internal.EncodeQuotedPrintable(data, false), kind, bnetIDGameAccount, messageOptions.priority, messageOptions.queue)
return "BATTLENET"
end
end
if not messageOptions.binaryBlob then
ToInGameLogged(bitField, prefix, Internal.EncodeQuotedPrintable(data, true), kind, target, messageOptions.priority, messageOptions.queue)
return "LOGGED"
end
ToInGame(bitField, prefix, data, kind, target, messageOptions.priority, messageOptions.queue)
return "UNLOGGED"
end
function Chomp.CheckReportGUID(prefix, guid)
-- v26+: Removed with no replacement.
return false
end
function Chomp.ReportGUID(prefix, guid, customMessage)
-- v26+: Removed with no replacement.
return false, ""
end
Chomp.Event = {
OnMessageReceived = "OnMessageReceived",
OnError = "OnError",
}
function Chomp.RegisterCallback(event, func, owner)
if type(event) ~= "string" then
error("Chomp.RegisterCallback: 'event' must be a string")
elseif not Chomp.Event[event] then
error(string.format("Chomp.RegisterCallback: event %q does not exist", event))
elseif type(func) ~= "function" and type(func) ~= "table" then
error("Chomp.RegisterCallback: 'func' must be callable")
elseif type(owner) ~= "string" and type(owner) ~= "table" and type(owner) ~= "thread" then
error("Chomp.RegisterCallback: 'owner' must be string, table, or coroutine")
end
Internal.RegisterCallback(owner, event, function(_, ...) return func(owner, ...) end)
end
function Chomp.UnregisterCallback(event, owner)
if type(event) ~= "string" then
error("Chomp.UnregisterCallback: 'event' must be a string")
elseif not Chomp.Event[event] then
error(string.format("Chomp.UnregisterCallback: event %q does not exist", event))
elseif type(owner) ~= "string" and type(owner) ~= "table" and type(owner) ~= "thread" then
error("Chomp.UnregisterCallback: 'owner' must be string, table, or coroutine")
end
Internal.UnregisterCallback(owner, event)
end
function Chomp.UnregisterAllCallbacks(owner)
if type(owner) ~= "string" and type(owner) ~= "table" and type(owner) ~= "thread" then
error("Chomp.UnregisterAllCallbacks: 'owner' must be string, table, or coroutine")
end
Internal.UnregisterAllCallbacks(owner)
end
function Chomp.RegisterErrorCallback(callback)
-- v18+: RegisterErrorCallback is deprecated in favor of the generic
-- RegisterCallback system.
local event = "OnError"
local func = function(_, ...) return callback(...) end
local owner = tostring(callback)
Chomp.RegisterCallback(event, func, owner)
return true
end
function Chomp.UnregisterErrorCallback(callback)
-- v18+: UnregisterErrorCallback is deprecated in favor of the generic
-- UnregisterCallback system.
local event = "OnError"
local owner = tostring(callback)
Chomp.UnregisterCallback(event, owner)
return true
end
-- v18+: Deprecated alias for the old typo'd function name.
Chomp.UnegisterErrorCallback = Chomp.UnregisterErrorCallback
function Chomp.GetBPS()
return ChatThrottleLib.MAX_CPS, ChatThrottleLib.BURST
end
function Chomp.SetBPS(bps, burst)
ChatThrottleLib.MAX_CPS = bps
ChatThrottleLib.BURST = burst
end
function Chomp.GetVersion()
return Internal.VERSION
end