-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComms.lua
More file actions
1678 lines (1458 loc) · 52.1 KB
/
Comms.lua
File metadata and controls
1678 lines (1458 loc) · 52.1 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
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
--[[
QuestTogether Announcement Communication Layer
This file handles lightweight announcement events over a shared addon channel.
Local quest events are always published. Each receiving client applies its own
display preferences when deciding whether to render bubbles or print chat logs.
]]
local QuestTogether = _G.QuestTogether
local ANNOUNCEMENT_WIRE_VERSION = 3
local ANNOUNCEMENT_COMMAND = "ANN"
local PING_REQUEST_VERSION = 1
local PING_REQUEST_COMMAND = "PING"
local PING_RESPONSE_VERSION = 2
local PING_RESPONSE_COMMAND = "PONG"
local QUEST_COMPARE_REQUEST_VERSION = 1
local QUEST_COMPARE_REQUEST_COMMAND = "QCMP"
local QUEST_COMPARE_ENTRY_VERSION = 1
local QUEST_COMPARE_ENTRY_COMMAND = "QCQE"
local QUEST_COMPARE_DONE_VERSION = 1
local QUEST_COMPARE_DONE_COMMAND = "QCDN"
local ANNOUNCEMENT_MAX_TEXT_LENGTH = 220
local PING_REQUEST_TIMEOUT_SECONDS = 10
local QUEST_COMPARE_TIMEOUT_SECONDS = 10
local ANNOUNCEMENT_CHANNEL_FILTER_EVENTS = {
"CHAT_MSG_CHANNEL",
"CHAT_MSG_CHANNEL_NOTICE",
"CHAT_MSG_CHANNEL_NOTICE_USER",
}
local GROUP_ANNOUNCEMENT_DISTRIBUTIONS = {
PARTY = true,
RAID = true,
INSTANCE_CHAT = true,
}
local COMM_DUPLICATE_WINDOW_SECONDS = 0.75
local COMM_DUPLICATE_PRUNE_THRESHOLD = 200
local raw_issecretvalue = type(issecretvalue) == "function" and issecretvalue or nil
local function IsSecretValue(value)
if not raw_issecretvalue then
return false
end
return raw_issecretvalue(value) and true or false
end
local function SafeDebugString(value)
if QuestTogether and QuestTogether.SafeToString then
return QuestTogether:SafeToString(value, "<secret>")
end
if IsSecretValue(value) then
return "<secret>"
end
local valueType = type(value)
if valueType == "string" or valueType == "number" or valueType == "boolean" or valueType == "nil" then
return tostring(value)
end
local ok, stringValue = pcall(tostring, value)
if ok then
return stringValue
end
return "<secret>"
end
local function SafeAddonString(addon, value, fallback)
if addon and addon.SafeToString then
return addon:SafeToString(value, fallback or "")
end
if IsSecretValue(value) then
return fallback or ""
end
local valueType = type(value)
if valueType == "string" or valueType == "number" or valueType == "boolean" or valueType == "nil" then
return tostring(value)
end
local ok, textValue = pcall(tostring, value)
if ok then
return textValue
end
return fallback or ""
end
local function SafePrimitiveString(addon, value, fallback)
if addon and addon.IsSecretValue and addon:IsSecretValue(value) then
return fallback or ""
end
local valueType = type(value)
if valueType == "string" or valueType == "number" or valueType == "boolean" or valueType == "nil" then
return tostring(value)
end
return fallback or ""
end
local function SafeTrimAddonString(addon, value, fallback)
if addon and addon.SafeTrimString then
return addon:SafeTrimString(value, fallback or "")
end
return SafeAddonString(addon, value, fallback or "")
end
local function SafeChannelNumber(addon, value)
if addon and addon.SafeToNumber then
return addon:SafeToNumber(value)
end
if IsSecretValue(value) then
return nil
end
local numberValue = tonumber(value)
if type(numberValue) ~= "number" then
return nil
end
return numberValue
end
local function MatchesAnnouncementChannelName(addon, value)
if type(value) ~= "string" or value == "" then
return false
end
local channelName = SafeAddonString(addon, addon.announcementChannelName or "", "")
if channelName == "" then
return false
end
if value == channelName then
return true
end
local ok, found = pcall(string.find, value, channelName, 1, true)
return ok and found ~= nil
end
local function SplitByDelimiter(text, delimiter)
local pieces = {}
local safeText = SafeAddonString(QuestTogether, text, "")
local safeDelimiter = SafeAddonString(QuestTogether, delimiter, "")
if safeText == "" or safeDelimiter == "" then
return pieces
end
local startIndex = 1
while true do
local okFind, delimiterIndex = pcall(string.find, safeText, safeDelimiter, startIndex, true)
if not okFind then
return pieces
end
if not delimiterIndex then
local okTail, tailValue = pcall(string.sub, safeText, startIndex)
if okTail and type(tailValue) == "string" then
pieces[#pieces + 1] = tailValue
end
break
end
local okPart, partValue = pcall(string.sub, safeText, startIndex, delimiterIndex - 1)
if okPart and type(partValue) == "string" then
pieces[#pieces + 1] = partValue
end
startIndex = delimiterIndex + #safeDelimiter
end
return pieces
end
local function SafeNumber(addon, value)
if addon and addon.SafeToNumber then
return addon:SafeToNumber(value)
end
if IsSecretValue(value) then
return nil
end
local numberValue = tonumber(value)
if type(numberValue) ~= "number" then
return nil
end
return numberValue
end
local function NormalizeRealmName(addon, realmName)
local sourceRealm = realmName
if sourceRealm == nil or sourceRealm == "" then
sourceRealm = addon and addon.API and addon.API.GetRealmName and addon.API.GetRealmName() or ""
end
if addon and addon.SafeStripWhitespace then
return addon:SafeStripWhitespace(sourceRealm, "")
end
if IsSecretValue(sourceRealm) then
return ""
end
return string.gsub(tostring(sourceRealm), "%s+", "")
end
function QuestTogether:EscapePayload(value)
local text = SafeAddonString(self, value or "", "")
local ok, escaped = pcall(string.gsub, text, "([^%w%-_%.~])", function(character)
local okByte, byteValue = pcall(string.byte, character)
if not okByte or not byteValue then
return ""
end
return string.format("%%%02X", byteValue)
end)
if not ok then
return ""
end
return escaped
end
function QuestTogether:UnescapePayload(value)
local text = SafeAddonString(self, value or "", "")
local ok, unescaped = pcall(string.gsub, text, "%%(%x%x)", function(hex)
local safeHex = SafeAddonString(self, hex or "", "")
local okFirst, firstChar = pcall(string.sub, safeHex, 1, 1)
local okSecond, secondChar = pcall(string.sub, safeHex, 2, 2)
if not okFirst or not okSecond then
return ""
end
local firstNibble = nil
local secondNibble = nil
if firstChar ~= "" then
local firstByte = string.byte(string.upper(firstChar))
if firstByte >= string.byte("0") and firstByte <= string.byte("9") then
firstNibble = firstByte - string.byte("0")
elseif firstByte >= string.byte("A") and firstByte <= string.byte("F") then
firstNibble = firstByte - string.byte("A") + 10
end
end
if secondChar ~= "" then
local secondByte = string.byte(string.upper(secondChar))
if secondByte >= string.byte("0") and secondByte <= string.byte("9") then
secondNibble = secondByte - string.byte("0")
elseif secondByte >= string.byte("A") and secondByte <= string.byte("F") then
secondNibble = secondByte - string.byte("A") + 10
end
end
if firstNibble == nil or secondNibble == nil then
return ""
end
return string.char((firstNibble * 16) + secondNibble)
end)
if not ok then
return ""
end
return unescaped
end
function QuestTogether:SerializeWireMessage(command, payload)
return SafeAddonString(self, command or "", "") .. "|" .. SafeAddonString(self, payload or "", "")
end
function QuestTogether:DeserializeWireMessage(message)
local safeMessage = SafeAddonString(self, message, "")
if safeMessage == "" then
return nil, nil
end
local ok, command, payload = pcall(string.match, safeMessage, "^([^|]+)|?(.*)$")
if not ok then
return nil, nil
end
if not command or command == "" then
return nil, nil
end
return command, payload
end
function QuestTogether:SanitizeAnnouncementText(text)
local sanitized = SafeTrimAddonString(self, text or "", "")
if #sanitized > ANNOUNCEMENT_MAX_TEXT_LENGTH then
local okSub, shortened = pcall(string.sub, sanitized, 1, ANNOUNCEMENT_MAX_TEXT_LENGTH)
if okSub and type(shortened) == "string" then
sanitized = shortened
else
sanitized = ""
end
end
return sanitized
end
function QuestTogether:SanitizeAnnouncementExtraData(extraData)
local sanitized = {}
if type(extraData) ~= "table" or self:IsSecretValue(extraData) then
return sanitized
end
local iconAsset = SafePrimitiveString(self, extraData.iconAsset or "", "")
local iconKind = SafePrimitiveString(self, extraData.iconKind or "", "")
local emoteToken = SafePrimitiveString(self, extraData.emoteToken or "", "")
if iconAsset ~= "" then
sanitized.iconAsset = iconAsset
end
if iconKind ~= "" then
sanitized.iconKind = iconKind
end
if emoteToken ~= "" then
sanitized.emoteToken = emoteToken
end
return sanitized
end
function QuestTogether:SanitizeAnnouncementEventData(eventData)
if type(eventData) ~= "table" or self:IsSecretValue(eventData) then
return nil
end
local eventType = SafePrimitiveString(self, eventData.eventType or "", "")
local senderName = SafePrimitiveString(self, eventData.senderName or "", "")
local text = self:SanitizeAnnouncementText(eventData.text)
if eventType == "" or senderName == "" or text == "" then
return nil
end
local sanitizedExtraData = self:SanitizeAnnouncementExtraData(eventData)
local normalizedQuestId = self.NormalizeQuestID and self:NormalizeQuestID(eventData.questId) or nil
local numericCoordX = self.SafeToNumber and self:SafeToNumber(eventData.coordX) or nil
local numericCoordY = self.SafeToNumber and self:SafeToNumber(eventData.coordY) or nil
local normalizedWarMode = nil
if self.NormalizeAnnouncementWarModeValue then
normalizedWarMode = self:NormalizeAnnouncementWarModeValue(eventData.warMode)
end
return {
version = SafeNumber(self, eventData.version) or ANNOUNCEMENT_WIRE_VERSION,
eventType = SafePrimitiveString(self, eventType, ""),
senderGUID = SafePrimitiveString(self, eventData.senderGUID or "", ""),
classFile = SafePrimitiveString(self, eventData.classFile or "", ""),
senderName = senderName,
text = text,
questId = normalizedQuestId and SafePrimitiveString(self, normalizedQuestId, "") or "",
iconAsset = sanitizedExtraData.iconAsset or "",
iconKind = sanitizedExtraData.iconKind or "",
zoneName = SafePrimitiveString(self, eventData.zoneName or "", ""),
coordX = numericCoordX and string.format("%.1f", numericCoordX) or "",
coordY = numericCoordY and string.format("%.1f", numericCoordY) or "",
warMode = normalizedWarMode == nil and "" or (normalizedWarMode and "1" or "0"),
emoteToken = sanitizedExtraData.emoteToken or "",
}
end
function QuestTogether:EncodePingRequestPayload(requestData)
local fields = {
SafeAddonString(self, PING_REQUEST_VERSION),
self:EscapePayload(requestData.requestId or ""),
self:EscapePayload(requestData.requesterName or ""),
}
return table.concat(fields, ",")
end
function QuestTogether:DecodePingRequestPayload(payload)
if not payload or payload == "" then
return nil
end
local fields = SplitByDelimiter(payload, ",")
local version = SafeNumber(self, fields[1] or "")
if version ~= PING_REQUEST_VERSION then
return nil
end
local requestId = self:UnescapePayload(fields[2] or "")
local requesterName = self:UnescapePayload(fields[3] or "")
if requestId == "" then
return nil
end
return {
version = version,
requestId = requestId,
requesterName = requesterName,
}
end
function QuestTogether:EncodePingResponsePayload(responseData)
local fields = {
SafeAddonString(self, PING_RESPONSE_VERSION),
self:EscapePayload(responseData.requestId or ""),
self:EscapePayload(responseData.senderName or ""),
self:EscapePayload(responseData.realmName or ""),
self:EscapePayload(responseData.raceName or ""),
self:EscapePayload(responseData.classFile or ""),
self:EscapePayload(responseData.className or ""),
self:EscapePayload(responseData.level or ""),
self:EscapePayload(responseData.zoneName or ""),
self:EscapePayload(responseData.coordX or ""),
self:EscapePayload(responseData.coordY or ""),
self:EscapePayload(responseData.warMode or ""),
self:EscapePayload(responseData.mapID or ""),
self:EscapePayload(responseData.addonVersion or ""),
}
return table.concat(fields, ",")
end
function QuestTogether:DecodePingResponsePayload(payload)
if not payload or payload == "" then
return nil
end
local fields = SplitByDelimiter(payload, ",")
local version = SafeNumber(self, fields[1] or "")
if version ~= 1 and version ~= PING_RESPONSE_VERSION then
return nil
end
local requestId = self:UnescapePayload(fields[2] or "")
local senderName = self:UnescapePayload(fields[3] or "")
local realmName = self:UnescapePayload(fields[4] or "")
local raceName = self:UnescapePayload(fields[5] or "")
local classFile = self:UnescapePayload(fields[6] or "")
local className = self:UnescapePayload(fields[7] or "")
local level = self:UnescapePayload(fields[8] or "")
local zoneName = self:UnescapePayload(fields[9] or "")
local coordX = self:UnescapePayload(fields[10] or "")
local coordY = self:UnescapePayload(fields[11] or "")
local warMode = self:UnescapePayload(fields[12] or "")
local mapID = self:UnescapePayload(fields[13] or "")
local addonVersion = self:UnescapePayload(fields[14] or "")
if requestId == "" or senderName == "" then
return nil
end
return {
version = version,
requestId = requestId,
senderName = senderName,
realmName = realmName,
raceName = raceName,
classFile = classFile,
className = className,
level = level,
zoneName = zoneName,
coordX = coordX,
coordY = coordY,
warMode = warMode,
mapID = mapID,
addonVersion = addonVersion,
}
end
function QuestTogether:EncodeQuestCompareRequestPayload(requestData)
local fields = {
SafeAddonString(self, QUEST_COMPARE_REQUEST_VERSION),
self:EscapePayload(requestData.requestId or ""),
self:EscapePayload(requestData.requesterName or ""),
self:EscapePayload(requestData.targetName or ""),
}
return table.concat(fields, ",")
end
function QuestTogether:DecodeQuestCompareRequestPayload(payload)
if not payload or payload == "" then
return nil
end
local fields = SplitByDelimiter(payload, ",")
local version = SafeNumber(self, fields[1] or "")
if version ~= QUEST_COMPARE_REQUEST_VERSION then
return nil
end
local requestId = self:UnescapePayload(fields[2] or "")
local requesterName = self:UnescapePayload(fields[3] or "")
local targetName = self:UnescapePayload(fields[4] or "")
if requestId == "" or targetName == "" then
return nil
end
return {
version = version,
requestId = requestId,
requesterName = requesterName,
targetName = targetName,
}
end
function QuestTogether:EncodeQuestCompareEntryPayload(entryData)
local fields = {
SafeAddonString(self, QUEST_COMPARE_ENTRY_VERSION),
self:EscapePayload(entryData.requestId or ""),
self:EscapePayload(entryData.senderName or ""),
self:EscapePayload(entryData.classFile or ""),
self:EscapePayload(entryData.questId or ""),
self:EscapePayload(entryData.questTitle or ""),
self:EscapePayload(entryData.isComplete and "1" or "0"),
self:EscapePayload(entryData.isPushable and "1" or "0"),
}
return table.concat(fields, ",")
end
function QuestTogether:DecodeQuestCompareEntryPayload(payload)
if not payload or payload == "" then
return nil
end
local fields = SplitByDelimiter(payload, ",")
local version = SafeNumber(self, fields[1] or "")
if version ~= QUEST_COMPARE_ENTRY_VERSION then
return nil
end
local requestId = self:UnescapePayload(fields[2] or "")
local senderName = self:UnescapePayload(fields[3] or "")
local classFile = ""
local questId = ""
local questTitle = ""
local isComplete = false
local isPushable = false
if fields[8] ~= nil then
classFile = self:UnescapePayload(fields[4] or "")
questId = self:UnescapePayload(fields[5] or "")
questTitle = self:UnescapePayload(fields[6] or "")
isComplete = self:UnescapePayload(fields[7] or "") == "1"
isPushable = self:UnescapePayload(fields[8] or "") == "1"
else
questId = self:UnescapePayload(fields[4] or "")
questTitle = self:UnescapePayload(fields[5] or "")
isComplete = self:UnescapePayload(fields[6] or "") == "1"
isPushable = self:UnescapePayload(fields[7] or "") == "1"
end
if requestId == "" or senderName == "" or questId == "" then
return nil
end
return {
version = version,
requestId = requestId,
senderName = senderName,
classFile = classFile,
questId = questId,
questTitle = questTitle,
isComplete = isComplete,
isPushable = isPushable,
}
end
function QuestTogether:EncodeQuestCompareDonePayload(doneData)
local fields = {
SafeAddonString(self, QUEST_COMPARE_DONE_VERSION),
self:EscapePayload(doneData.requestId or ""),
self:EscapePayload(doneData.senderName or ""),
self:EscapePayload(doneData.classFile or ""),
self:EscapePayload(doneData.count or ""),
}
return table.concat(fields, ",")
end
function QuestTogether:DecodeQuestCompareDonePayload(payload)
if not payload or payload == "" then
return nil
end
local fields = SplitByDelimiter(payload, ",")
local version = SafeNumber(self, fields[1] or "")
if version ~= QUEST_COMPARE_DONE_VERSION then
return nil
end
local requestId = self:UnescapePayload(fields[2] or "")
local senderName = self:UnescapePayload(fields[3] or "")
local classFile = ""
local count = ""
if fields[5] ~= nil then
classFile = self:UnescapePayload(fields[4] or "")
count = self:UnescapePayload(fields[5] or "")
else
count = self:UnescapePayload(fields[4] or "")
end
if requestId == "" or senderName == "" then
return nil
end
return {
version = version,
requestId = requestId,
senderName = senderName,
classFile = classFile,
count = SafeNumber(self, count) or 0,
}
end
function QuestTogether:EncodeAnnouncementPayload(eventData)
local fields = {
SafeAddonString(self, ANNOUNCEMENT_WIRE_VERSION),
self:EscapePayload(eventData.eventType or ""),
self:EscapePayload(eventData.senderGUID or ""),
self:EscapePayload(eventData.classFile or ""),
self:EscapePayload(eventData.senderName or ""),
self:EscapePayload(eventData.text or ""),
self:EscapePayload(eventData.questId or ""),
self:EscapePayload(eventData.iconAsset or ""),
self:EscapePayload(eventData.iconKind or ""),
self:EscapePayload(eventData.zoneName or ""),
self:EscapePayload(eventData.coordX or ""),
self:EscapePayload(eventData.coordY or ""),
self:EscapePayload(eventData.warMode or ""),
self:EscapePayload(eventData.emoteToken or ""),
}
return table.concat(fields, ",")
end
function QuestTogether:DecodeAnnouncementPayload(payload)
if not payload or payload == "" then
return nil
end
local fields = SplitByDelimiter(payload, ",")
local version = SafeNumber(self, fields[1] or "")
if version ~= 1 and version ~= 2 and version ~= ANNOUNCEMENT_WIRE_VERSION then
return nil
end
local eventType = self:UnescapePayload(fields[2] or "")
local senderGUID = self:UnescapePayload(fields[3] or "")
local classFile = self:UnescapePayload(fields[4] or "")
local senderName = self:UnescapePayload(fields[5] or "")
local text = self:UnescapePayload(fields[6] or "")
local questId = self:UnescapePayload(fields[7] or "")
local iconAsset = self:UnescapePayload(fields[8] or "")
local iconKind = self:UnescapePayload(fields[9] or "")
local zoneName = self:UnescapePayload(fields[10] or "")
local coordX = self:UnescapePayload(fields[11] or "")
local coordY = self:UnescapePayload(fields[12] or "")
local warMode = self:UnescapePayload(fields[13] or "")
local emoteToken = self:UnescapePayload(fields[14] or "")
if eventType == "" or senderName == "" or text == "" then
return nil
end
return self:SanitizeAnnouncementEventData({
version = version,
eventType = eventType,
senderGUID = senderGUID,
classFile = classFile,
senderName = senderName,
text = text,
questId = questId,
iconAsset = iconAsset,
iconKind = iconKind,
zoneName = zoneName,
coordX = coordX,
coordY = coordY,
warMode = warMode,
emoteToken = emoteToken,
})
end
function QuestTogether:GetAnnouncementChannelLocalID()
if not self.API or not self.API.GetChannelName then
return nil
end
local localID = self.API.GetChannelName(self.announcementChannelName)
local numericLocalID = SafeChannelNumber(self, localID)
if numericLocalID and numericLocalID > 0 then
return numericLocalID
end
return nil
end
function QuestTogether:GetAnnouncementChannelTarget()
local localID = self:GetAnnouncementChannelLocalID() or self.announcementChannelLocalID
if type(localID) == "number" and localID > 0 then
return localID
end
return self.announcementChannelName
end
function QuestTogether:GetGroupAnnouncementDistribution()
if self.API and self.API.IsInInstanceGroup and self.API.IsInInstanceGroup() then
return "INSTANCE_CHAT"
end
if self.API and self.API.IsInRaid and self.API.IsInRaid() then
return "RAID"
end
if self.API and self.API.IsInParty and self.API.IsInParty() then
return "PARTY"
end
return nil
end
function QuestTogether:GetAnnouncementWireRoutes()
local groupedDistribution = self:GetGroupAnnouncementDistribution()
local routes = {}
if groupedDistribution then
routes[#routes + 1] = {
distribution = groupedDistribution,
target = nil,
requiresChannelJoin = false,
}
end
routes[#routes + 1] = {
distribution = "CHANNEL",
target = self:GetAnnouncementChannelTarget(),
requiresChannelJoin = true,
}
return routes
end
function QuestTogether:SendWireMessageToAnnouncementRoutes(wireMessage, debugContext)
if type(wireMessage) ~= "string" or wireMessage == "" then
return false
end
if not self.API or not self.API.SendAddonMessage then
return false
end
local contextLabel = SafeAddonString(self, debugContext or "wire message", "wire message")
local routes = self:GetAnnouncementWireRoutes()
local sentCount = 0
for _, route in ipairs(routes) do
if route.requiresChannelJoin and not self:EnsureAnnouncementChannelJoined() then
self:Debugf("comms", "Skipping %s route=%s because channel join failed", contextLabel, route.distribution or "")
else
self.API.SendAddonMessage(self.commPrefix, wireMessage, route.distribution, route.target)
sentCount = sentCount + 1
end
end
return sentCount > 0
end
function QuestTogether:ShouldSuppressDuplicateCommMessage(sender, message)
local nowSeconds = self.API and self.API.GetTime and self.API.GetTime() or 0
self.recentCommMessageSignatures = self.recentCommMessageSignatures or {}
local signatures = self.recentCommMessageSignatures
local signatureCount = 0
for _ in pairs(signatures) do
signatureCount = signatureCount + 1
end
if signatureCount > COMM_DUPLICATE_PRUNE_THRESHOLD then
for signature, seenAt in pairs(signatures) do
if (nowSeconds - (seenAt or 0)) > COMM_DUPLICATE_WINDOW_SECONDS then
signatures[signature] = nil
end
end
end
local signature = SafeAddonString(self, sender or "", "") .. "|" .. SafeAddonString(self, message or "", "")
local seenAt = signatures[signature]
if seenAt and (nowSeconds - seenAt) <= COMM_DUPLICATE_WINDOW_SECONDS then
return true
end
signatures[signature] = nowSeconds
return false
end
function QuestTogether:AnnouncementChannelChatFilter(_, _, ...)
for argumentIndex = 1, select("#", ...) do
if MatchesAnnouncementChannelName(self, select(argumentIndex, ...)) then
return true
end
end
return false
end
function QuestTogether:RegisterAnnouncementChannelChatFilters()
if self.announcementChannelChatFiltersRegistered then
return
end
if not self.API or not self.API.AddMessageEventFilter then
return
end
self.announcementChannelChatFilterFunc = self.announcementChannelChatFilterFunc
or function(...)
return QuestTogether:AnnouncementChannelChatFilter(...)
end
for _, eventName in ipairs(ANNOUNCEMENT_CHANNEL_FILTER_EVENTS) do
self.API.AddMessageEventFilter(eventName, self.announcementChannelChatFilterFunc)
end
self.announcementChannelChatFiltersRegistered = true
end
function QuestTogether:UnregisterAnnouncementChannelChatFilters()
if not self.announcementChannelChatFiltersRegistered then
return
end
if not self.API or not self.API.RemoveMessageEventFilter or not self.announcementChannelChatFilterFunc then
self.announcementChannelChatFiltersRegistered = nil
return
end
for _, eventName in ipairs(ANNOUNCEMENT_CHANNEL_FILTER_EVENTS) do
self.API.RemoveMessageEventFilter(eventName, self.announcementChannelChatFilterFunc)
end
self.announcementChannelChatFiltersRegistered = nil
end
function QuestTogether:HideAnnouncementChannelFromChatWindows()
if not self.API or not self.API.GetNumChatWindows or not self.API.GetChatFrameByID or not self.API.RemoveChatWindowChannel then
return
end
local maxWindows = SafeNumber(self, self.API.GetNumChatWindows()) or 0
for chatFrameID = 1, maxWindows do
local chatFrame = self.API.GetChatFrameByID(chatFrameID)
if chatFrame then
-- Some chat frames reject channel removal in edge states; keep cleanup best-effort.
pcall(self.API.RemoveChatWindowChannel, chatFrame, self.announcementChannelName)
end
end
end
function QuestTogether:EnsureAnnouncementChannelJoined()
if not self.isEnabled then
self:Debug("Skipping channel join because addon is disabled", "comms")
return false
end
local currentLocalID = self:GetAnnouncementChannelLocalID()
if currentLocalID then
self.announcementChannelLocalID = currentLocalID
if self.HideAnnouncementChannelFromChatWindows then
self:HideAnnouncementChannelFromChatWindows()
end
return true
end
if not self.API or not self.API.JoinPermanentChannel then
self:Debug("JoinPermanentChannel API unavailable", "comms")
return false
end
if self.RegisterAnnouncementChannelChatFilters then
self:RegisterAnnouncementChannelChatFilters()
end
local chatFrameId = (DEFAULT_CHAT_FRAME and DEFAULT_CHAT_FRAME.GetID and DEFAULT_CHAT_FRAME:GetID()) or 1
self:Debugf(
"comms",
"Joining announcement channel name=%s chatFrameId=%s",
SafeAddonString(self, self.announcementChannelName),
SafeAddonString(self, chatFrameId)
)
self.API.JoinPermanentChannel(self.announcementChannelName, nil, chatFrameId, 1)
currentLocalID = self:GetAnnouncementChannelLocalID()
if currentLocalID then
self.announcementChannelLocalID = currentLocalID
if self.HideAnnouncementChannelFromChatWindows then
self:HideAnnouncementChannelFromChatWindows()
end
self:Debugf("comms", "Joined announcement channel localID=%s", SafeAddonString(self, currentLocalID))
return true
end
self:Debug("Unable to join announcement channel " .. SafeAddonString(self, self.announcementChannelName), "comms")
return false
end
function QuestTogether:LeaveAnnouncementChannel()
if self.API and self.API.LeaveChannelByName then
self:Debugf("comms", "Leaving announcement channel name=%s", SafeAddonString(self, self.announcementChannelName))
-- Channel leave can fail if Blizzard already removed it; no need to hard fail disable.
pcall(self.API.LeaveChannelByName, self.announcementChannelName)
end
self.announcementChannelLocalID = nil
if self.UnregisterAnnouncementChannelChatFilters then
self:UnregisterAnnouncementChannelChatFilters()
end
end
function QuestTogether:GetPlayerPingMetadata()
local fullName = self:GetPlayerFullName() or self:GetPlayerName() or "Unknown"
local unitName, unitRealm = self.API.UnitFullName and self.API.UnitFullName("player")
local realmName = unitRealm or (self.API.GetRealmName and self.API.GetRealmName()) or ""
local className, classFile = self.API.UnitClass and self.API.UnitClass("player")
local raceName = nil
if self.API.UnitRace then
raceName = self.API.UnitRace("player")
end
local level = self.API.UnitLevel and self.API.UnitLevel("player") or ""
local locationInfo = self.GetPlayerAnnouncementLocationInfo and self:GetPlayerAnnouncementLocationInfo() or {}
local numericCoordX = locationInfo and self.SafeToNumber and self:SafeToNumber(locationInfo.coordX) or nil
local numericCoordY = locationInfo and self.SafeToNumber and self:SafeToNumber(locationInfo.coordY) or nil
return {
senderName = SafeAddonString(self, fullName or "", ""),
realmName = SafeAddonString(self, realmName or "", ""),
raceName = SafeAddonString(self, raceName or "", ""),
classFile = SafeAddonString(self, classFile or "", ""),
className = SafeAddonString(self, className or "", ""),
addonVersion = SafeAddonString(self, self:GetAddonVersion() or "", ""),
level = level and SafeAddonString(self, level, "") or "",
zoneName = locationInfo and SafeAddonString(self, locationInfo.zoneName or "", "") or "",
coordX = numericCoordX and string.format("%.1f", numericCoordX) or "",
coordY = numericCoordY and string.format("%.1f", numericCoordY) or "",
warMode = locationInfo and SafeAddonString(self, locationInfo.warMode and "1" or "0", "") or "",
mapID = locationInfo and SafeAddonString(self, locationInfo.mapID or "", "") or "",
}
end
function QuestTogether:BuildChannelRequestId(prefix)
local requestPrefix = SafeAddonString(self, prefix or "req", "req")
return string.format(
"%s-%s-%d-%d",
requestPrefix,
SafeAddonString(self, self:GetPlayerName() or "player", "player"),
math.floor((self.API.GetTime and self.API.GetTime() or 0) * 1000),
self.API.Random and self.API.Random(1000, 9999) or 1000
)
end
function QuestTogether:BuildLocalAnnouncementEvent(eventType, text, questId, extraData)
local senderName = self:GetPlayerFullName() or self:GetPlayerName()
local senderGUID = ""
if self.API.UnitGUID then
local okGuid, guidValue = pcall(self.API.UnitGUID, "player")
if okGuid and not self:IsSecretValue(guidValue) then
senderGUID = SafeAddonString(self, guidValue or "", "")
end
end
local sanitizedText = self:SanitizeAnnouncementText(text)
local iconAsset, iconKind = self:GetAnnouncementIconInfo(eventType, questId)
local sanitizedExtraData = self:SanitizeAnnouncementExtraData(extraData)
if sanitizedExtraData.iconAsset then
iconAsset = sanitizedExtraData.iconAsset
iconKind = sanitizedExtraData.iconKind or iconKind
end
local locationInfo = self.GetPlayerAnnouncementLocationInfo and self:GetPlayerAnnouncementLocationInfo() or nil
local numericCoordX = locationInfo and self.SafeToNumber and self:SafeToNumber(locationInfo.coordX) or nil
local numericCoordY = locationInfo and self.SafeToNumber and self:SafeToNumber(locationInfo.coordY) or nil
if sanitizedText == "" then
return nil
end
return self:SanitizeAnnouncementEventData({
version = ANNOUNCEMENT_WIRE_VERSION,
eventType = SafeAddonString(self, eventType or "", ""),
senderGUID = SafeAddonString(self, senderGUID or "", ""),
classFile = SafeAddonString(self, self:GetPlayerClassFile() or "", ""),
senderName = SafeAddonString(self, senderName or "", ""),
text = sanitizedText,
questId = questId and SafeAddonString(self, questId, "") or "",
iconAsset = SafeAddonString(self, iconAsset or "", ""),
iconKind = SafeAddonString(self, iconKind or "", ""),
zoneName = locationInfo and SafeAddonString(self, locationInfo.zoneName or "", "") or "",
coordX = numericCoordX and string.format("%.1f", numericCoordX) or "",
coordY = numericCoordY and string.format("%.1f", numericCoordY) or "",
warMode = locationInfo and SafeAddonString(self, locationInfo.warMode and "1" or "0", "") or "",
emoteToken = sanitizedExtraData.emoteToken or "",
})
end
function QuestTogether:BuildAnnouncementEventForUnit(unitToken, eventType, text)
if type(unitToken) ~= "string" or unitToken == "" then
return nil
end
local sanitizedText = self:SanitizeAnnouncementText(text)
if sanitizedText == "" then
return nil
end
local unitName, unitRealm = self.API.UnitFullName(unitToken)
if not unitName or unitName == "" then
return nil
end
local _, classFile = self.API.UnitClass(unitToken)
local senderGUID = ""
if self.API.UnitGUID then
local okGuid, guidValue = pcall(self.API.UnitGUID, unitToken)
if okGuid and not self:IsSecretValue(guidValue) then
senderGUID = SafeAddonString(self, guidValue or "", "")
end
end
local senderName = SafeAddonString(self, unitName, "") .. "-" .. SafeAddonString(self, NormalizeRealmName(self, unitRealm), "")
return self:SanitizeAnnouncementEventData({
version = ANNOUNCEMENT_WIRE_VERSION,
eventType = SafeAddonString(self, eventType or "", ""),