-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInviteTools.lua
More file actions
2159 lines (1937 loc) · 78.8 KB
/
Copy pathInviteTools.lua
File metadata and controls
2159 lines (1937 loc) · 78.8 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
---------------------------------------------------------------------------
-- PugzRaidTools - Invite & Loot Tools
-- Keyword invites, realm-aware blocks, auto-promote, loot prompts, and
-- disband/reinvite snapshots.
---------------------------------------------------------------------------
local _, PRT = ...
PRT.INVITE_LOOT_METHODS = {
{ value = "freeforall", text = "Free For All", id = 0 },
{ value = "roundrobin", text = "Round Robin", id = 1 },
{ value = "master", text = "Master Loot", id = 2 },
{ value = "group", text = "Group Loot", id = 3 },
{ value = "needbeforegreed", text = "Need Before Greed", id = 4 },
}
local function GetQualityText(quality, label, fallbackHex)
local colorHex = fallbackHex
if GetItemQualityColor then
local _, _, _, apiHex = GetItemQualityColor(quality)
if apiHex and apiHex ~= "" then
colorHex = apiHex:gsub("^|c", ""):gsub("|r$", "")
end
end
return "|c" .. colorHex .. label .. "|r"
end
PRT.INVITE_LOOT_THRESHOLDS = {
{
value = 1,
text = GetQualityText(1, "Common (White)", "ffffffff"),
plainText = "Common (White)",
},
{
value = 2,
text = GetQualityText(2, "Uncommon (Green)", "ff1eff00"),
plainText = "Uncommon (Green)",
},
{
value = 3,
text = GetQualityText(3, "Rare (Blue)", "ff0070dd"),
plainText = "Rare (Blue)",
},
{
value = 4,
text = GetQualityText(4, "Epic (Purple)", "ffa335ee"),
plainText = "Epic (Purple)",
},
}
PRT.INVITE_LOOT_ZONES = {
{ key = "naxxramas", name = "Naxxramas", instanceId = 533 },
{ key = "aq40", name = "Ahn'Qiraj", instanceId = 531 },
{ key = "bwl", name = "Blackwing Lair", instanceId = 469 },
{ key = "moltenCore", name = "Molten Core", instanceId = 409 },
{ key = "zulgurub", name = "Zul'Gurub", instanceId = 309 },
{ key = "aq20", name = "Ruins of Ahn'Qiraj", instanceId = 509 },
{ key = "blastedLands", name = "Blasted Lands", uiMapId = 1419 },
{ key = "azshara", name = "Azshara", uiMapId = 1447 },
{ key = "ashenvale", name = "Ashenvale", uiMapId = 1440 },
{ key = "hinterlands", name = "The Hinterlands", uiMapId = 1425 },
{ key = "duskwood", name = "Duskwood", uiMapId = 1431 },
{ key = "feralas", name = "Feralas", uiMapId = 1444 },
}
local lootMethodByValue = {}
local lootMethodById = {}
for _, method in ipairs(PRT.INVITE_LOOT_METHODS) do
lootMethodByValue[method.value] = method
lootMethodById[method.id] = method
end
local lootThresholdByValue = {}
for _, threshold in ipairs(PRT.INVITE_LOOT_THRESHOLDS) do
lootThresholdByValue[threshold.value] = threshold
end
local lootZoneByInstanceId = {}
local lootZoneByMapId = {}
local lootZoneByName = {}
for _, zone in ipairs(PRT.INVITE_LOOT_ZONES) do
if zone.instanceId then lootZoneByInstanceId[zone.instanceId] = zone end
if zone.uiMapId then lootZoneByMapId[zone.uiMapId] = zone end
lootZoneByName[string.lower(zone.name)] = zone
end
local function GetConfig()
return PRT:GetDB().inviteTools
end
local RefreshPanel
local function GetInviteBanStore()
local store = GetConfig()
local legacy = type(store.autoInvite) == "table"
and type(store.autoInvite.bannedPlayers) == "table"
and store.autoInvite.bannedPlayers
or nil
if type(store.bannedPlayers) ~= "table" then store.bannedPlayers = {} end
if legacy and legacy ~= store.bannedPlayers then
for identityKey, entry in pairs(legacy) do
if store.bannedPlayers[identityKey] == nil then
store.bannedPlayers[identityKey] = entry
end
end
end
if type(store.autoInvite) == "table" then
store.autoInvite.bannedPlayers = nil
end
return store.bannedPlayers
end
local function DeepCopy(value, seen)
if type(value) ~= "table" then return value end
seen = seen or {}
if seen[value] then return seen[value] end
local copy = {}
seen[value] = copy
for key, child in pairs(value) do
copy[DeepCopy(key, seen)] = DeepCopy(child, seen)
end
return copy
end
local function Bool(value, fallback)
if value == nil then return fallback and true or false end
return value and true or false
end
local function NormalizeCustomZones(zones)
local normalized = {}
local seen = {}
for _, zone in ipairs(type(zones) == "table" and zones or {}) do
local name
local uiMapId
if type(zone) == "table" then
name = PRT.Trim(tostring(zone.name or ""))
uiMapId = tonumber(zone.uiMapId)
else
name = PRT.Trim(tostring(zone or ""))
end
local key = string.lower(name)
if name ~= "" and not seen[key] then
seen[key] = true
normalized[#normalized + 1] = {
name = name,
uiMapId = uiMapId,
}
end
end
return normalized
end
local function NormalizeInviteToolsPreset(preset)
preset = type(preset) == "table" and preset or {}
preset.name = PRT.Trim(tostring(preset.name or ""))
if preset.name == "" then preset.name = "Preset" end
local autoInvite = type(preset.autoInvite) == "table" and preset.autoInvite or {}
autoInvite.enabled = Bool(autoInvite.enabled, false)
autoInvite.keywords = type(autoInvite.keywords) == "table" and autoInvite.keywords or { "inv" }
autoInvite.guildOnly = Bool(autoInvite.guildOnly, false)
autoInvite.autoAcceptTrusted = Bool(autoInvite.autoAcceptTrusted, false)
autoInvite.raidInvites = type(autoInvite.raidInvites) == "table" and autoInvite.raidInvites or {}
autoInvite.raidInvites.enabled = Bool(autoInvite.raidInvites.enabled, false)
autoInvite.bannedPlayers = nil
preset.autoInvite = autoInvite
local autoPromote = type(preset.autoPromote) == "table" and preset.autoPromote or {}
autoPromote.enabled = Bool(autoPromote.enabled, false)
autoPromote.names = tostring(autoPromote.names or "")
autoPromote.guildRankThreshold = tonumber(autoPromote.guildRankThreshold) or 0
preset.autoPromote = autoPromote
local loot = type(preset.loot) == "table" and preset.loot or {}
loot.enabled = Bool(loot.enabled, false)
loot.method = lootMethodByValue[loot.method] and loot.method or "group"
loot.assignMasterLooter = Bool(loot.assignMasterLooter, false)
loot.masterLooter = PRT.Trim(tostring(loot.masterLooter or ""))
loot.threshold = tonumber(loot.threshold) or 1
if not lootThresholdByValue[loot.threshold] then loot.threshold = 1 end
loot.onlyInRaid = Bool(loot.onlyInRaid, true)
loot.zones = type(loot.zones) == "table" and loot.zones or {}
for _, zone in ipairs(PRT.INVITE_LOOT_ZONES) do
loot.zones[zone.key] = Bool(loot.zones[zone.key], false)
end
loot.customZones = NormalizeCustomZones(loot.customZones)
preset.loot = loot
local lootToChat = type(preset.lootToChat) == "table" and preset.lootToChat or {}
lootToChat.enabled = Bool(lootToChat.enabled, false)
lootToChat.includeItemLevel = Bool(lootToChat.includeItemLevel, false)
preset.lootToChat = lootToChat
return preset
end
local function SnapshotInviteToolsPreset(name, source)
source = source or GetConfig()
return NormalizeInviteToolsPreset({
name = name,
autoInvite = DeepCopy(source.autoInvite),
autoPromote = DeepCopy(source.autoPromote),
loot = DeepCopy(source.loot),
lootToChat = DeepCopy(source.lootToChat),
})
end
local function FindInviteToolsPreset(store, name)
for _, preset in ipairs(store and store.presets or {}) do
if preset.name == name then return preset end
end
end
local function MakeUniqueInviteToolsPresetName(store, requested)
local base = PRT.Trim(tostring(requested or ""))
if base == "" then base = "Imported Preset" end
if not FindInviteToolsPreset(store, base) then return base end
local suffix = 2
while FindInviteToolsPreset(store, base .. " (" .. suffix .. ")") do
suffix = suffix + 1
end
return base .. " (" .. suffix .. ")"
end
function PRT:EnsureInviteToolsPresetDefaults()
local store = GetConfig()
store.enabled = Bool(store.enabled, true)
store.presets = type(store.presets) == "table" and store.presets or {}
store.activePreset = tostring(store.activePreset or "")
GetInviteBanStore()
for index, preset in ipairs(store.presets) do
store.presets[index] = NormalizeInviteToolsPreset(preset)
end
if #store.presets == 0 then
store.presets[1] = SnapshotInviteToolsPreset("Default", store)
end
if not FindInviteToolsPreset(store, store.activePreset) then
store.activePreset = store.presets[1].name
end
end
function PRT:GetInviteToolsPreset(name)
return FindInviteToolsPreset(GetConfig(), name)
end
function PRT:GetActiveInviteToolsPreset()
local store = GetConfig()
return FindInviteToolsPreset(store, store.activePreset)
end
function PRT:CreateInviteToolsPreset(name)
self:EnsureInviteToolsPresetDefaults()
local store = GetConfig()
local cleanName = PRT.Trim(tostring(name or ""))
if cleanName == "" then return nil, "Enter a preset name." end
if FindInviteToolsPreset(store, cleanName) then
return nil, "An Invite & Loot preset with that name already exists."
end
local preset = SnapshotInviteToolsPreset(cleanName, store)
store.presets[#store.presets + 1] = preset
return preset
end
function PRT:RenameInviteToolsPreset(oldName, newName)
self:EnsureInviteToolsPresetDefaults()
local store = GetConfig()
local preset = FindInviteToolsPreset(store, oldName)
local cleanName = PRT.Trim(tostring(newName or ""))
if not preset then return false, "That Invite & Loot preset no longer exists." end
if cleanName == "" then return false, "Enter a preset name." end
local existing = FindInviteToolsPreset(store, cleanName)
if existing and existing ~= preset then
return false, "An Invite & Loot preset with that name already exists."
end
preset.name = cleanName
if store.activePreset == oldName then store.activePreset = cleanName end
if self.RenamePRTProfilePresetReference then
self:RenamePRTProfilePresetReference("inviteTools", oldName, cleanName)
end
return true
end
function PRT:DeleteInviteToolsPreset(name)
self:EnsureInviteToolsPresetDefaults()
local store = GetConfig()
if #store.presets <= 1 then
return false, "At least one Invite & Loot preset must remain."
end
local removedIndex
for index, preset in ipairs(store.presets) do
if preset.name == name then
removedIndex = index
break
end
end
if not removedIndex then return false, "That Invite & Loot preset no longer exists." end
table.remove(store.presets, removedIndex)
local replacement = store.presets[math.min(removedIndex, #store.presets)] or store.presets[1]
if self.RemovePRTProfilePresetReference then
self:RemovePRTProfilePresetReference("inviteTools", name, replacement and replacement.name or "")
end
if store.activePreset == name and replacement then
self:ActivateInviteToolsPreset(replacement.name)
end
return true
end
function PRT:ActivateInviteToolsPreset(name, refreshUI, runAutomation)
self:EnsureInviteToolsPresetDefaults()
local store = GetConfig()
local preset = FindInviteToolsPreset(store, name)
if not preset then return false end
store.activePreset = preset.name
store.autoInvite = preset.autoInvite
store.autoPromote = preset.autoPromote
store.loot = preset.loot
store.lootToChat = preset.lootToChat
if self.UpdateInviteToolsListeners then self:UpdateInviteToolsListeners() end
if runAutomation ~= false then
if store.enabled and store.autoPromote.enabled and self.RequestAutoPromote then
self:RequestAutoPromote()
end
if store.enabled and store.loot.enabled and self.ResetInviteLootPromptState then
self:ResetInviteLootPromptState()
end
end
if refreshUI ~= false then
if self.inviteToolsPanel and self.inviteToolsPanel.Refresh then
self.inviteToolsPanel:Refresh()
end
if self.profilesPanel and self.profilesPanel.RefreshProfilesView then
self.profilesPanel:RefreshProfilesView()
end
end
return true
end
local function EncodeInviteField(value)
return tostring(value or ""):gsub("([^%w%-%._ ])", function(char)
return ("%%%02X"):format(string.byte(char))
end)
end
local function DecodeInviteField(value)
return tostring(value or ""):gsub("%%(%x%x)", function(hex)
return string.char(tonumber(hex, 16))
end)
end
local function ParseBool(value)
if value == "true" then return true end
if value == "false" then return false end
end
function PRT:ExportInviteToolsPreset(preset)
preset = NormalizeInviteToolsPreset(DeepCopy(preset or self:GetActiveInviteToolsPreset()))
if not preset then return "" end
local lines = {
"[InviteLootPreset: " .. EncodeInviteField(preset.name) .. "]",
"formatVersion=1",
"autoInviteEnabled=" .. tostring(preset.autoInvite.enabled),
"guildOnly=" .. tostring(preset.autoInvite.guildOnly),
"autoAcceptTrusted=" .. tostring(preset.autoInvite.autoAcceptTrusted),
"raidInvitesEnabled=" .. tostring(preset.autoInvite.raidInvites.enabled),
}
for _, keyword in ipairs(preset.autoInvite.keywords) do
lines[#lines + 1] = "keyword=" .. EncodeInviteField(keyword)
end
lines[#lines + 1] = "autoPromoteEnabled=" .. tostring(preset.autoPromote.enabled)
lines[#lines + 1] = "autoPromoteNames=" .. EncodeInviteField(preset.autoPromote.names)
lines[#lines + 1] = "guildRankThreshold=" .. tostring(preset.autoPromote.guildRankThreshold)
lines[#lines + 1] = "lootPromptEnabled=" .. tostring(preset.loot.enabled)
lines[#lines + 1] = "lootMethod=" .. tostring(preset.loot.method)
lines[#lines + 1] = "assignMasterLooter=" .. tostring(preset.loot.assignMasterLooter)
lines[#lines + 1] = "masterLooter=" .. EncodeInviteField(preset.loot.masterLooter)
lines[#lines + 1] = "lootThreshold=" .. tostring(preset.loot.threshold)
lines[#lines + 1] = "onlyInRaid=" .. tostring(preset.loot.onlyInRaid)
for _, zone in ipairs(PRT.INVITE_LOOT_ZONES) do
lines[#lines + 1] = "zone." .. zone.key .. "=" .. tostring(preset.loot.zones[zone.key] and true or false)
end
for _, zone in ipairs(preset.loot.customZones) do
lines[#lines + 1] = "customZone=" .. EncodeInviteField(zone.name)
.. "|" .. tostring(zone.uiMapId or "")
end
lines[#lines + 1] = "lootToChatEnabled=" .. tostring(preset.lootToChat.enabled)
lines[#lines + 1] = "includeItemLevel=" .. tostring(preset.lootToChat.includeItemLevel)
return table.concat(lines, "\n")
end
function PRT:ParseInviteToolsPresetString(raw)
raw = tostring(raw or ""):gsub("\r\n", "\n"):gsub("\r", "\n")
local encodedName = raw:match("^%s*%[InviteLootPreset:%s*(.-)%]%s*\n")
if not encodedName then return nil, "No [InviteLootPreset: Name] header was found." end
local defaults = PRT.DEFAULTS and PRT.DEFAULTS.inviteTools or {}
local preset = SnapshotInviteToolsPreset(DecodeInviteField(encodedName), defaults)
preset.autoInvite.keywords = {}
preset.loot.customZones = {}
for line in raw:gmatch("[^\n]+") do
local key, value = line:match("^([^=]+)=(.*)$")
if key == "autoInviteEnabled" then preset.autoInvite.enabled = ParseBool(value)
elseif key == "guildOnly" then preset.autoInvite.guildOnly = ParseBool(value)
elseif key == "autoAcceptTrusted" then preset.autoInvite.autoAcceptTrusted = ParseBool(value)
elseif key == "raidInvitesEnabled" then preset.autoInvite.raidInvites.enabled = ParseBool(value)
elseif key == "keyword" then preset.autoInvite.keywords[#preset.autoInvite.keywords + 1] = DecodeInviteField(value)
elseif key == "autoPromoteEnabled" then preset.autoPromote.enabled = ParseBool(value)
elseif key == "autoPromoteNames" then preset.autoPromote.names = DecodeInviteField(value)
elseif key == "guildRankThreshold" then preset.autoPromote.guildRankThreshold = tonumber(value) or 0
elseif key == "lootPromptEnabled" then preset.loot.enabled = ParseBool(value)
elseif key == "lootMethod" then preset.loot.method = value
elseif key == "assignMasterLooter" then preset.loot.assignMasterLooter = ParseBool(value)
elseif key == "masterLooter" then preset.loot.masterLooter = DecodeInviteField(value)
elseif key == "lootThreshold" then preset.loot.threshold = tonumber(value) or 1
elseif key == "onlyInRaid" then preset.loot.onlyInRaid = ParseBool(value)
elseif key and key:match("^zone%.") then
preset.loot.zones[key:sub(6)] = ParseBool(value)
elseif key == "customZone" then
local nameValue, mapValue = value:match("^(.-)|(%d*)$")
preset.loot.customZones[#preset.loot.customZones + 1] = {
name = DecodeInviteField(nameValue or value),
uiMapId = tonumber(mapValue),
}
elseif key == "lootToChatEnabled" then preset.lootToChat.enabled = ParseBool(value)
elseif key == "includeItemLevel" then preset.lootToChat.includeItemLevel = ParseBool(value)
end
end
return NormalizeInviteToolsPreset(preset)
end
function PRT:ImportInviteToolsPreset(raw)
local preset, err = self:ParseInviteToolsPresetString(raw)
if not preset then return nil, err end
self:EnsureInviteToolsPresetDefaults()
local store = GetConfig()
preset.name = MakeUniqueInviteToolsPresetName(store, preset.name)
store.presets[#store.presets + 1] = preset
self:ActivateInviteToolsPreset(preset.name)
return preset
end
function PRT:InitInviteToolsPresets()
self:EnsureInviteToolsPresetDefaults()
self:ActivateInviteToolsPreset(GetConfig().activePreset, false, false)
end
local function Now()
if GetTime then return GetTime() end
if time then return time() end
return 0
end
local function IsSecret(value)
return issecretvalue and issecretvalue(value)
end
local function IsGrouped()
if IsInGroup then return IsInGroup() end
return (GetNumGroupMembers and GetNumGroupMembers() or 0) > 0
end
local function IsGroupLeader()
return UnitIsGroupLeader and UnitIsGroupLeader("player")
end
local function CanInvite()
if not IsGrouped() then return true end
return IsGroupLeader() or (UnitIsGroupAssistant and UnitIsGroupAssistant("player"))
end
local function InviteUnitCompat(name)
if not name or name == "" then return false end
if C_PartyInfo and C_PartyInfo.InviteUnit then
C_PartyInfo.InviteUnit(name)
return true
elseif InviteUnit then
InviteUnit(name)
return true
end
return false
end
local function ConvertToRaidCompat()
if C_PartyInfo and C_PartyInfo.ConvertToRaid then
C_PartyInfo.ConvertToRaid()
return true
elseif ConvertToRaid then
ConvertToRaid()
return true
end
return false
end
local function UninviteUnitCompat(name)
if C_PartyInfo and C_PartyInfo.UninviteUnit then
C_PartyInfo.UninviteUnit(name)
return true
elseif UninviteUnit then
UninviteUnit(name)
return true
end
return false
end
local function SendChatMessageCompat(message, chatType, target)
if C_ChatInfo and C_ChatInfo.SendChatMessage then
C_ChatInfo.SendChatMessage(message, chatType, nil, target)
return true
elseif SendChatMessage then
SendChatMessage(message, chatType, nil, target)
return true
end
return false
end
RefreshPanel = function()
if PRT.inviteToolsPanel and PRT.inviteToolsPanel.Refresh then
PRT.inviteToolsPanel:Refresh()
end
if PRT.RefreshInviteBanListPopup then
PRT:RefreshInviteBanListPopup()
end
end
---------------------------------------------------------------------------
-- Auto invite keywords and realm-aware block list
---------------------------------------------------------------------------
local function CanonicalKeyword(keyword)
return string.lower(PRT.Trim(keyword or ""))
end
function PRT:AddInviteKeyword(keyword)
keyword = CanonicalKeyword(keyword)
if keyword == "" then
return false, "Enter a keyword first."
end
local keywords = GetConfig().autoInvite.keywords
for _, existing in ipairs(keywords) do
if CanonicalKeyword(existing) == keyword then
return false, "That keyword already exists."
end
end
keywords[#keywords + 1] = keyword
RefreshPanel()
return true
end
function PRT:RemoveInviteKeyword(index)
local keywords = GetConfig().autoInvite.keywords
index = tonumber(index)
if not index or not keywords[index] then return false end
table.remove(keywords, index)
RefreshPanel()
return true
end
function PRT:GetInviteBanEntries()
local entries = {}
local blocked = GetInviteBanStore()
for identityKey, value in pairs(blocked) do
local name
local realm
if type(value) == "table" then
name = value.name
realm = value.realm
end
if not name or name == "" then
name, realm = identityKey:match("^([^@]+)@(.*)$")
end
if name and name ~= "" then
entries[#entries + 1] = {
identityKey = identityKey,
name = name,
realm = realm or "",
displayName = self:MakeCharacterFullName(name, realm or "", true),
}
end
end
table.sort(entries, function(a, b)
return string.lower(a.displayName) < string.lower(b.displayName)
end)
return entries
end
function PRT:AddInviteBan(fullName)
local name, realm = self:SplitNameRealm(fullName, true)
if name == "" then
PRT.Print("Usage: /prt ban PlayerName or PlayerName-Realm")
return false
end
local identityKey = self:MakePlayerIdentityKey(name, realm)
local blocked = GetInviteBanStore()
local displayName = self:MakeCharacterFullName(name, realm, true)
if blocked[identityKey] then
PRT.Print(displayName .. " is already blocked from keyword invites.")
return false
end
blocked[identityKey] = { name = name, realm = realm }
PRT.Print(displayName .. " blocked from keyword invites.")
RefreshPanel()
return true
end
function PRT:RemoveInviteBan(fullName)
local identityKey = self:GetPlayerIdentityKey(fullName)
local blocked = GetInviteBanStore()
if identityKey == "" or not blocked[identityKey] then
PRT.Print((fullName or "Player") .. " is not on the invite block list.")
return false
end
local value = blocked[identityKey]
local displayName = fullName
if type(value) == "table" then
displayName = self:MakeCharacterFullName(value.name, value.realm, true)
end
blocked[identityKey] = nil
PRT.Print(displayName .. " removed from the invite block list.")
RefreshPanel()
return true
end
function PRT:RemoveInviteBanByKey(identityKey)
local blocked = GetInviteBanStore()
local value = blocked[identityKey]
if not value then return false end
blocked[identityKey] = nil
local displayName = identityKey
if type(value) == "table" then
displayName = self:MakeCharacterFullName(value.name, value.realm, true)
end
PRT.Print(displayName .. " removed from the invite block list.")
RefreshPanel()
return true
end
function PRT:IsInviteBanned(fullName)
local identityKey = self:GetPlayerIdentityKey(fullName)
return identityKey ~= "" and GetInviteBanStore()[identityKey] ~= nil
end
function PRT:PrintInviteBanList()
local entries = self:GetInviteBanEntries()
if #entries == 0 then
PRT.Print("Invite block list is empty.")
return
end
PRT.Print(("Invite block list (%d):"):format(#entries))
for _, entry in ipairs(entries) do
print(" " .. entry.displayName)
end
end
local function BuildKeywordLookup()
local lookup = {}
for _, keyword in ipairs(GetConfig().autoInvite.keywords or {}) do
keyword = CanonicalKeyword(keyword)
if keyword ~= "" then lookup[keyword] = true end
end
return lookup
end
local function RequestGuildRosterRefresh()
if C_GuildInfo and C_GuildInfo.GuildRoster then
C_GuildInfo.GuildRoster()
elseif GuildRoster then
GuildRoster()
end
end
function PRT:IsInviteGuildMember(fullName, guid)
if not IsInGuild or not IsInGuild() then return false end
local name = self:SplitNameRealm(fullName, false)
if UnitIsInMyGuild and (UnitIsInMyGuild(fullName) or UnitIsInMyGuild(name)) then
return true
end
local count = GetNumGuildMembers and GetNumGuildMembers() or 0
if count == 0 then
RequestGuildRosterRefresh()
return nil
end
local targetKey = self:GetPlayerIdentityKey(fullName)
for index = 1, count do
local guildName, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, guildGuid =
GetGuildRosterInfo(index)
if guildName then
if guid and guildGuid and guid == guildGuid then return true end
if self:GetPlayerIdentityKey(guildName) == targetKey then return true end
end
end
return false
end
local function InviteIdentityMatches(fullName, guid, candidateName, candidateRealm, candidateGuid)
if guid and candidateGuid and guid == candidateGuid then return true end
if not candidateName or candidateName == "" then return false end
local targetName = PRT:SplitNameRealm(fullName, false)
local candidateBase, embeddedRealm = PRT:SplitNameRealm(candidateName, false)
candidateRealm = embeddedRealm ~= "" and embeddedRealm or candidateRealm or ""
if candidateRealm ~= "" then
return PRT:GetPlayerIdentityKey(fullName)
== PRT:MakePlayerIdentityKey(candidateBase, candidateRealm)
end
return string.lower(targetName or "") == string.lower(candidateBase or "")
end
function PRT:IsInviteFriend(fullName, guid)
if C_FriendList then
if C_FriendList.ShowFriends then C_FriendList.ShowFriends() end
local count = C_FriendList.GetNumFriends and C_FriendList.GetNumFriends() or 0
for index = 1, count do
local info = C_FriendList.GetFriendInfoByIndex
and C_FriendList.GetFriendInfoByIndex(index)
if info and InviteIdentityMatches(fullName, guid, info.name, nil, info.guid) then
return true
end
end
end
local bnetCount = BNGetNumFriends and BNGetNumFriends() or 0
for friendIndex = 1, bnetCount do
local checkedGameAccounts = false
if C_BattleNet and C_BattleNet.GetFriendNumGameAccounts
and C_BattleNet.GetFriendGameAccountInfo then
local gameCount = C_BattleNet.GetFriendNumGameAccounts(friendIndex) or 0
for accountIndex = 1, gameCount do
checkedGameAccounts = true
local info = C_BattleNet.GetFriendGameAccountInfo(friendIndex, accountIndex)
if info and info.clientProgram == BNET_CLIENT_WOW
and InviteIdentityMatches(fullName, guid, info.characterName,
info.realmName, info.playerGuid) then
return true
end
end
elseif BNGetNumFriendGameAccounts and BNGetFriendGameAccountInfo then
local gameCount = BNGetNumFriendGameAccounts(friendIndex) or 0
for accountIndex = 1, gameCount do
checkedGameAccounts = true
local _, characterName, client, realmName, _, _, _, _, _, _, _, _, _, _, _,
_, playerGuid = BNGetFriendGameAccountInfo(friendIndex, accountIndex)
if client == BNET_CLIENT_WOW
and InviteIdentityMatches(fullName, guid, characterName, realmName, playerGuid) then
return true
end
end
end
if not checkedGameAccounts and BNGetFriendInfo then
local activeCharacter = select(5, BNGetFriendInfo(friendIndex))
if InviteIdentityMatches(fullName, guid, activeCharacter) then return true end
end
end
return false
end
local function HideAcceptedInvitePopup(accepted)
local function HideIfInvite(frame)
if not frame then return end
local visible = frame.IsShown and frame:IsShown()
if visible and (frame.which == "PARTY_INVITE" or frame.which == "PARTY_INVITE_XREALM") then
if accepted ~= false then frame.inviteAccepted = 1 end
if StaticPopup_Hide then StaticPopup_Hide(frame.which) end
end
end
if StaticPopup_ForEachShownDialog then
StaticPopup_ForEachShownDialog(HideIfInvite)
else
for index = 1, 4 do HideIfInvite(_G["StaticPopup" .. index]) end
end
end
function PRT:HandleTrustedInviteRequest(inviterName, inviterGuid, retried)
local store = GetConfig()
local cfg = store.autoInvite
if store.enabled == false or not cfg.autoAcceptTrusted
or IsSecret(inviterName) or IsSecret(inviterGuid) then return false end
local isFriend = self:IsInviteFriend(inviterName, inviterGuid)
local isGuildMember = self:IsInviteGuildMember(inviterName, inviterGuid)
if isFriend or isGuildMember then
if AcceptGroup then
AcceptGroup()
HideAcceptedInvitePopup()
return true
end
return false
end
if isGuildMember == nil and not retried then
C_Timer.After(1, function()
PRT:HandleTrustedInviteRequest(inviterName, inviterGuid, true)
end)
end
return false
end
---------------------------------------------------------------------------
-- Queued party-to-raid invites
---------------------------------------------------------------------------
local PARTY_INVITE_RESERVATION_SECONDS = 20
function PRT:GetRaidInvitesEnabled()
local raidInvites = GetConfig().autoInvite.raidInvites
return raidInvites and raidInvites.enabled or false
end
function PRT:ClearRaidInviteQueue()
self._raidInviteQueueState = nil
self._raidInviteQueueSerial = (self._raidInviteQueueSerial or 0) + 1
end
function PRT:SetRaidInvitesEnabled(enabled, announce)
local cfg = GetConfig().autoInvite
cfg.raidInvites = cfg.raidInvites or {}
cfg.raidInvites.enabled = enabled and true or false
if not cfg.raidInvites.enabled then self:ClearRaidInviteQueue() end
self:UpdateInviteToolsListeners()
RefreshPanel()
if announce then
PRT.Print("Raid Invites " .. (cfg.raidInvites.enabled and "enabled." or "disabled."))
end
end
local function GetCurrentInviteGroupLookup()
local lookup = {}
if PRT.GetCurrentGroupMemberEntries then
for _, member in ipairs(PRT:GetCurrentGroupMemberEntries(true)) do
lookup[member.identityKey] = true
end
else
lookup[PRT:GetUnitIdentityKey("player")] = true
end
return lookup
end
function PRT:ScheduleRaidInviteQueue(delay)
local state = self._raidInviteQueueState
if not state then return end
delay = delay or 1
local dueAt = Now() + delay
if state.processScheduled and state.processDueAt and state.processDueAt <= dueAt then
return
end
state.processScheduled = true
state.processDueAt = dueAt
state.processScheduleSerial = (state.processScheduleSerial or 0) + 1
local processScheduleSerial = state.processScheduleSerial
local serial = self._raidInviteQueueSerial
C_Timer.After(delay, function()
local currentState = PRT._raidInviteQueueState
if not currentState or PRT._raidInviteQueueSerial ~= serial then return end
if currentState.processScheduleSerial ~= processScheduleSerial then return end
currentState.processScheduled = false
currentState.processDueAt = nil
PRT:ProcessRaidInviteQueue()
end)
end
function PRT:HandleRaidConvertPopup()
local state = self._raidInviteQueueState
if not state or #state.queue == 0 or not self:GetRaidInvitesEnabled()
or (IsInRaid and IsInRaid()) or not IsGrouped() or not IsGroupLeader() then
return false
end
local handled = false
local function HandleIfTrackedConvert(frame)
if handled or not frame or frame.which ~= "CONVERT_TO_RAID" then return end
if frame.IsShown and not frame:IsShown() then return end
if IsSecret(frame.data) then return end
local identityKey = PRT:GetPlayerIdentityKey(frame.data)
if identityKey == ""
or (not state.queued[identityKey] and not state.pending[identityKey]) then
return
end
if ConvertToRaidCompat() then
handled = true
state.converting = true
state.conversionRequestedAt = Now()
if StaticPopup_Hide then
StaticPopup_Hide("CONVERT_TO_RAID")
elseif frame.Hide then
frame:Hide()
end
end
end
if StaticPopup_ForEachShownDialog then
StaticPopup_ForEachShownDialog(HandleIfTrackedConvert)
else
for index = 1, 4 do
HandleIfTrackedConvert(_G["StaticPopup" .. index])
end
end
if handled then self:ScheduleRaidInviteQueue(0.1) end
return handled
end
function PRT:ProcessRaidInviteQueue()
local state = self._raidInviteQueueState
local store = GetConfig()
local cfg = store.autoInvite
if not state then return end
if store.enabled == false or not cfg.enabled or not self:GetRaidInvitesEnabled() then
self:ClearRaidInviteQueue()
return
end
if not CanInvite() then return end
if self:HandleRaidConvertPopup() then return end
local current = GetCurrentInviteGroupLookup()
local now = Now()
for identityKey in pairs(state.pending) do
if current[identityKey] then state.pending[identityKey] = nil end
end
local liveQueue = {}
for _, entry in ipairs(state.queue) do
if not current[entry.identityKey] and not state.pending[entry.identityKey] then
liveQueue[#liveQueue + 1] = entry
else
state.queued[entry.identityKey] = nil
end
end
state.queue = liveQueue
if IsInRaid and IsInRaid() then
local targets = {}
local seen = {}
for _, entry in ipairs(state.queue) do
if not current[entry.identityKey] and not seen[entry.identityKey] then
seen[entry.identityKey] = true
targets[#targets + 1] = entry
end
end
for identityKey, pending in pairs(state.pending) do
if not current[identityKey] and not seen[identityKey] then
seen[identityKey] = true
targets[#targets + 1] = pending.entry
end
end
local capacity = math.max(0, 40 - (GetNumGroupMembers and GetNumGroupMembers() or 0))
self:ClearRaidInviteQueue()
for index = 1, math.min(capacity, #targets) do
local inviteName = targets[index].inviteName
C_Timer.After((index - 1) * 0.1, function()
InviteUnitCompat(inviteName)
end)
end
return
end
local groupCount = GetNumGroupMembers and GetNumGroupMembers() or 0
-- A queued request means the four initial party invite slots are already
-- reserved. Convert as soon as one invite has been accepted and a real
-- party exists; waiting for all five members only delays the queued invite.
if groupCount >= 2 and #state.queue > 0 then
local conversionAge = now - (state.conversionRequestedAt or 0)
if IsGroupLeader() and (not state.converting or conversionAge >= 1) then
state.converting = true
state.conversionRequestedAt = now
if ConvertToRaidCompat() then
self:ScheduleRaidInviteQueue(0.1)
else
state.converting = false
state.conversionRequestedAt = nil
end
end
return
end
state.converting = false
if #state.queue > 0 then
for identityKey, pending in pairs(state.pending) do
if now - pending.sentAt >= PARTY_INVITE_RESERVATION_SECONDS then
state.pending[identityKey] = nil
if (pending.entry.attempts or 0) < 2 and not current[identityKey]
and not state.queued[identityKey] then