-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoosePortalAdvanced.lua
1326 lines (1137 loc) · 49.5 KB
/
MoosePortalAdvanced.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
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
local ADDONNAME, WIA = ...
local corename = "MoosePortal"
-- luacheck: globals LibStub
local core = LibStub("AceAddon-3.0"):GetAddon(corename)
LibStub("AceAddon-3.0"):NewAddon(WIA, ADDONNAME, "AceConsole-3.0", "AceEvent-3.0", "AceTimer-3.0", "AceBucket-3.0")
local L = LibStub("AceLocale-3.0"):GetLocale(corename)
--local _G = _G
local GetGuildInfo, BNGetFriendInfo, BNGetFriendIndex, BNGetNumFriendGameAccounts, GetRealmName, BNGetFriendGameAccountInfo =
GetGuildInfo, BNGetFriendInfo, BNGetFriendIndex, BNGetNumFriendGameAccounts, GetRealmName, BNGetFriendGameAccountInfo
local BNET_CLIENT_WOW = BNET_CLIENT_WOW
local split, len, find, gsub, format =
strsplit, strlen, strfind, gsub, format
local type, select, pairs, wipe, tinsert, next, pcall, rawset, string, tonumber =
type, select, pairs, wipe, tinsert, next, pcall, rawset, string, tonumber
local lower = string.utf8lower or strlower
WIA.LIST_TYPES = {
BLOCK = 1, -- L["BLOCK"] = "Block"
ALLOW = 2, -- L["ALLOW"] = "Allow"
}
WIA.LIST_ENTRY_TYPES_REMOVE = 0 -- L["LIST_ENTRY_TYPES_REMOVE"] = "Remove Filter"
WIA.LIST_ENTRY_TYPES = {
PLAYER = 1, -- L["PLAYER"] = "Player"
PLAYER_REALM = 2, -- L["PLAYER_REALM"] = "Player-Realm"
GUILD = 3, -- L["GUILD"] = "Guild"
GUILD_REALM = 4, -- L["GUILD_REALM"] = "Guild-Realm"
REALM = 5, -- L["REALM"] = "Realm"
BNET_TAG = 6, -- L["BNET_TAG"] = "Battle.net Tag"
}
WIA.CHANNEL_TYPES = {
NORMAL = 1,
BNET = 2,
}
local CT_NORMAL = WIA.CHANNEL_TYPES.NORMAL
local CT_BNET = WIA.CHANNEL_TYPES.BNET
local getTable, delTable
do
local cache = setmetatable({},{__mode="k"})
function getTable()
local t = next(cache)
if t then
cache[t] = nil
return t
else
return {}
end
end
function delTable(t)
wipe(t)
cache[t] = true
end
end
local defaults = {
global = {
channels = {
CHAT_MSG_SAY = true, -- L["CHAT_MSG_SAY"] = "SAY"
CHAT_MSG_YELL = true, -- L["CHAT_MSG_YELL"] = "YELL"
CHAT_MSG_WHISPER = true, -- L["CHAT_MSG_WHISPER"] = "Whisper"
},
bnet_channels = {
CHAT_MSG_BN_CONVERSATION = true, -- L["CHAT_MSG_BN_CONVERSATION"] = "Chat"
CHAT_MSG_BN_INLINE_TOAST_BROADCAST = true, -- L["CHAT_MSG_BN_INLINE_TOAST_BROADCAST"] = "Status"
CHAT_MSG_BN_WHISPER = true, -- L["CHAT_MSG_BN_WHISPER"] = "Whisper"
},
cache = {
['**'] = { -- name-realm
name = false,
realm = false,
guild = false,
bnet_tag = false,
['*'] = false,
},
},
},
profile = {
messageInScheduleTime = 3,
checkPlayerScheduleTime = 0.1,
guildRosterUpdateDelay = 30,
hasGuildListEntrys = false,
hasBNetListEntrys = false,
keywords = {
['**'] = {-- id
active = true,
-- name = "", <-- Original keyword and exist check
keyword = "",
caseSensitive = false,
fullMatch = true,
plainMatch = true,
maxGroupSize = 40,
showInviteBlockMessage = false,
customBlockMessage = false,
hasGuildListEntrys = false,
hasBNetListEntrys = false,
listType = WIA.LIST_TYPES.BLOCK,
list = {
-- EntryName = WIA.LIST_ENTRY_TYPES.*
},
channels = {
-- CHAT_MSG_* = true/false
},
bnet_channels = {
-- CHAT_MSG_BN_* = true/false
},
},
},
},
}
function WIA:OnInitialize()
self.db = core:RegisterNamespace("MoosePortalAdvancedDB", defaults)
self:SetDBGlobal_cache()
self:RegisterChatCommand("wia", "CMD", false)
self:RegisterChatCommand("wiadvanced", "CMD", false)
self:RegisterChatCommand("MoosePortaladvanced", "CMD", false)
self:RegisterConfig()
end
function WIA:OnEnable()
joinedTable = getTable()
destinationFile = getTable()
self:RegisterEvent("GROUP_ROSTER_UPDATE", "GroupChanged")
self:RegisterEvent("RAID_ROSTER_UPDATE", "GroupChanged")
self:RegisterAllChannels()
end
function WIA:OnDisable()
--self:CheckGuildCacheStatus()
--self:CheckBattleNetCacheStatus()
self:UnregisterAllChannels()
end
do-- CMD
local InterfaceOptionsFrameAddOnsListScrollBar, InterfaceOptionsFrame_OpenToCategory =
InterfaceOptionsFrameAddOnsListScrollBar, InterfaceOptionsFrame_OpenToCategory
function WIA:CMD(input, editBox)
local a1, lastPos = self:GetArgs(input, 1, 1)
a1 = a1 and lower(a1) or nil
if a1 == "cache" then
local a2
a2, lastPos = self:GetArgs(input, 1, lastPos)
a2 = a2 and lower(a2) or nil
if a2 == "clean" then
self:CleanUpCache()
self:Print(L["Cache has been cleaned."])
elseif a2 == "reset" then
self:ResetCache()
self:Print(L["Cache has been reset."])
else
self:Printf(L["Usage: /wia %s"], "cache")
self:Printf(L["/wia cache clean||reset – Clean-up or reset cache"])
end
elseif a1 == "op" or a1 == "option" or a1 == "options" then
-- Load InterfaceOptionsFrameAddOns frame...
InterfaceOptionsFrame_OpenToCategory(L["Advanced"])
-- Let's scroll down, so your page can be open
local _, max = InterfaceOptionsFrameAddOnsListScrollBar:GetMinMaxValues()
InterfaceOptionsFrameAddOnsListScrollBar:SetValue(max)
InterfaceOptionsFrame_OpenToCategory(L["Advanced"])
else
self:Printf(L["Usage: /wia %s"], L["<command>"])
self:Printf(L["/wia cache clean||reset – Clean-up or reset cache"])
self:Printf(L["/wia op||option – Open MoosePortalAdvanced Options"])
end
end
end
do-- caching
local GetNumGuildMembers, GetGuildRosterInfo, BNGetNumFriends, BNGetFriendInfoByID =
GetNumGuildMembers, GetGuildRosterInfo, BNGetNumFriends, BNGetFriendInfoByID
local LET_GUILD = WIA.LIST_ENTRY_TYPES.GUILD
local LET_REALM = WIA.LIST_ENTRY_TYPES.REALM
function WIA:CheckAllRegisteredGuilds()
local isCached = getTable()
for id, data in pairs(self.db.profile.keywords) do
if data.hasGuildListEntrys then
for entryName, entryType in pairs(data.list) do
if entryType == LET_GUILD or entryType == LET_REALM then
if not isCached[entryName] then
isCached[entryName] = true
self:UpdatePlayerForGuild(entryName)
end
end
end
end
end
delTable(isCached)
end
local last_numGuildMembers, last_numOnline = 0,0
function WIA:GUILD_ROSTER_UPDATE()
local guildName = GetGuildInfo("player")
if type(guildName) ~= "string" or len(guildName) < 1 then return end
local numGuildMembers, numOnline = GetNumGuildMembers()
if last_numGuildMembers == numGuildMembers and last_numOnline == numOnline then return end
last_numGuildMembers = numGuildMembers
last_numOnline = numOnline
--[===[@debug@
self:Printf("Update guild cache from %s guild roster", guildName)
--@end-debug@]===]
local cache = self.db.global.cache
local count = 0
for guildIndex=1, numGuildMembers do
local fullName, rank, rankIndex, level, class, zone, note, officernote, online = GetGuildRosterInfo(guildIndex)
if online then
count = count + 1
local name_realm, name, realm = core:UniformPlayerName(fullName)
cache[name_realm].guild = guildName
-- not really needed
--self.db.global.cache[name_realm].realm = realm
--self.db.global.cache[name_realm].name = name
if count >= numOnline then
break
end
end
end
end
local function CheckData(friendIndex, toonIndex, bnet_tag)
local self = WIA
local _, toonName, client, realmName = BNGetFriendGameAccountInfo(friendIndex, toonIndex)
if client == BNET_CLIENT_WOW then
local name_realm = core:UniformPlayerName(toonName, realmName)
--[===[@debug@
self:Printf("Update: N-R: %s, TN: %s, RN: %s, TAG: %s", name_realm, toonName, realmName, bnet_tag)
--@end-debug@]===]
local cache_realm_name = self.db.global.cache[name_realm]
cache_realm_name.name = toonName
cache_realm_name.realm = realmName
cache_realm_name.bnet_tag = bnet_tag
if self.db.profile.hasGuildListEntrys then
self:UpdateGuildForPlayer(name_realm)
end
end
end
function WIA:CheckAllOnlineFriends()
local _, num = BNGetNumFriends()
--[===[@debug@
self:Printf("Check %d online Friends.", num)
--@end-debug@]===]
for friendIndex=1, num do
local presenceID = BNGetFriendInfo(friendIndex)
self:UpdatePlayersFromPresenceID(presenceID)
end
end
function WIA:UpdatePlayersFromPresenceID(presenceID)
local friendIndex = BNGetFriendIndex(presenceID)
local _, _, bnet_tag = BNGetFriendInfoByID(presenceID)
if friendIndex then
for toonIndex=1, BNGetNumFriendGameAccounts(friendIndex) do
CheckData(friendIndex, toonIndex, bnet_tag)
end
end
end
function WIA:UpdatePlayerFromToonID(toonID)
if not toonID then return end
local isBreak = false
--[===[@debug@
self:Printf("Search toonID: %s", toonID)
--@end-debug@]===]
local _, num = BNGetNumFriends()
for friendIndex=1, num do
for toonIndex=1, BNGetNumFriendGameAccounts(friendIndex) do
--local toonID2 = select(16, BNGetFriendGameAccountInfo(friendIndex, toonIndex) )
local _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, toonID2 = BNGetFriendGameAccountInfo(friendIndex, toonIndex)
if toonID2 == toonID then
--[===[@debug@
self:Print("Found toon")
--@end-debug@]===]
local _, _, bnet_tag = BNGetFriendInfo(friendIndex)
CheckData(friendIndex, toonIndex, bnet_tag)
isBreak = true
break
end
end
if isBreak then
break
end
end
end
function WIA:BN_FRIEND_TOON_ONLINE(event, toonID)
self:UpdatePlayerFromToonID(toonID)
end
end
do-- Channel Registering
local registeredChannels = {}
local registeredBNetChannels = {}
function WIA:RegisterAllChannels()
if not self:IsEnabled() then return end
for id, data in pairs(self.db.profile.keywords) do
if data.active then
for channel, active in pairs(data.channels) do
if active then
self:RegisterChannel(channel, CT_NORMAL, id)
end
end
for channel, active in pairs(data.bnet_channels) do
if active then
self:RegisterChannel(channel, CT_BNET, id)
end
end
end
end
end
function WIA:RegisterChannel(channel, channelType, id)
if not self:IsEnabled() then return end
self:RegisterEvent(channel, "MessageIn", channelType)
if channelType == CT_NORMAL then
registeredChannels[channel] = registeredChannels[channel] or {}
registeredChannels[channel][id] = true
elseif channelType == CT_BNET then
registeredBNetChannels[channel] = registeredBNetChannels[channel] or {}
registeredBNetChannels[channel][id] = true
else
if not next(registeredChannels[channel]) and next(registeredBNetChannels[channel]) then
self:UnregisterEvent(channel)
end
end
end
function WIA:UnregisterAllChannels()
for channel in pairs(registeredChannels) do
wipe(registeredChannels[channel])
self:UnregisterChannel(channel)
end
for channel in pairs(registeredBNetChannels) do
wipe(registeredBNetChannels)
self:UnregisterChannel(channel)
end
end
function WIA:UnregisterChannel(channel, id)
if id then
if type(registeredChannels[channel]) == "table" then
registeredChannels[channel][id] = nil
elseif type(registeredBNetChannels[channel]) == "table" then
registeredBNetChannels[channel][id] = nil
end
end
if type(registeredChannels[channel]) == "table" then
if not next(registeredChannels[channel]) then
self:UnregisterEvent(channel)
end
elseif type(registeredBNetChannels[channel]) == "table" then
if not next(registeredBNetChannels[channel]) then
self:UnregisterEvent(channel)
end
end
end
end
function WIA:GroupChanged()
for groupindex = 1,GetNumGroupMembers()-1 do
local unit = "party"..groupindex
local guid = UnitGUID(unit)
if not UnitIsUnit("player", unit) and joinedTable[guid] ~= nil then
print(joinedTable[guid])
joinedTable[guid] = nil
if destinationFile[guid] ~= nil then
FlashClientIcon()
PlaySoundFile(destinationFile[guid], "Dialog")
end
end
end
end
function WIA:PlayerFlagsChanged(unit)
playerAFK = UnitIsAFK("player")
end
--local keywordIDsCache = {}
function WIA:MessageIn(channelType, event, ...)
if not self:IsEnabled() then return end
local msg, name = ...
--local presenceID = select(13, ...)
local _, _, _, _, _, _, _, _, _, _, _, guid, presenceID = ...
-- if #keywordIDsCache > 0 then
-- --[===[@debug@
-- self:Print("Schedule MessageIn")
-- --@end-debug@]===]
-- self:ScheduleTimer("MessageIn", self.db.profile.messageInScheduleTime, channelType, event, ...)
-- return
-- end
local keywordIDs = getTable()
self:GetMatchingKeywordIDs(keywordIDs, msg, channelType, event)
local profile = self.db.profile
local checkPlayerScheduleTime = profile.checkPlayerScheduleTime
local keywords = profile.keywords
local overrideScheduleTime
local guildIsAlreadyChecked = false
for i=1, #keywordIDs do
local id = keywordIDs[i]
local data = keywords[id]
local joinedMessage = format("|cff0099CC------ %s ------|r |cffffff00%s|r |cff0099CC------|r |cffff6600%s|r |cff0099CC------|r", string.upper(data.keyword), Ambiguate(name, "all"), msg)
if data.keyword == "og" or data.keyword == "org" or data.keyword == "orc" then
destinationFile[guid] = "Interface\\CustomSounds\\orgrimmar.mp3"
elseif data.keyword == "uc" or data.keyword == "undercity" then
destinationFile[guid] = "Interface\\CustomSounds\\undercity.mp3"
elseif data.keyword == "tb" or data.keyword == "thunder" then
destinationFile[guid] = "Interface\\CustomSounds\\thunderbluff.mp3"
elseif data.keyword == "port" then
destinationFile[guid] = "Interface\\CustomSounds\\portal.mp3"
elseif data.keyword == "water" then
destinationFile[guid] = "Interface\\CustomSounds\\water.mp3"
end
if self:GetGroupSize() < data.maxGroupSize and select(2, GetPlayerInfoByGUID(guid)) ~= "MAGE" and not playerAFK then -- GetGroupSize returns without player
if next(data.list) then -- check if block/allow entry exits
if data.hasGuildListEntrys and not overrideScheduleTime and channelType == CT_NORMAL then
if not guildIsAlreadyChecked then
guildIsAlreadyChecked = true
self:UpdateGuildForPlayer(name)
end
if self:GetCacheValue( core:UniformPlayerName(name), "guild" ) == "" then
-- We have no guild data or player is not in a guild so give time to verify if in a guild or not
overrideScheduleTime = 5.5 > checkPlayerScheduleTime and 5.5 or checkPlayerScheduleTime
end
end
self:ScheduleTimer("CheckPlayer", overrideScheduleTime or checkPlayerScheduleTime, id, name, presenceID, channelType, guid, joinedMessage)
else
joinedTable[guid] = joinedMessage
local result, code
if channelType == CT_NORMAL then
result, code = core:InvitePlayer(name)
elseif channelType == CT_BNET then
result, code = core:InviteBNet(presenceID)
end
--[===[@debug@
self:Printf("Invite Sent: %s", result and "OK" or "Error: "..code )
--@end-debug@]===]
end
end
end
delTable(keywordIDs)
end
function WIA:GetMatchingKeywordIDs(resultTable, msg, channelType, channel)
--wipe(resultTable)
--tinsert(resultTable, false)
local channelTableName = channelType == CT_NORMAL and "channels" or channelType == CT_BNET and "bnet_channels"
for id, data in pairs(self.db.profile.keywords) do
local keyword = data.keyword
if data.active and channelTableName and data[channelTableName][channel] then
if not data.caseSensitive then
msg = lower(msg)
end
if data.fullMatch then
if keyword == msg then
tinsert(resultTable, id)
end
else
local plainMatch = data.plainMatch
if not plainMatch then
keyword = "%f[%a]"..keyword.."%f[%A]"
end
if find(msg, keyword, nil, plainMatch) then
tinsert(resultTable, id)
end
end
end
end
return #resultTable
end
do-- GetGroupSize
local GetNumGroupMembers = GetNumGroupMembers
local LE_PARTY_CATEGORY_HOME = LE_PARTY_CATEGORY_HOME
function WIA:GetGroupSize()
return GetNumGroupMembers(LE_PARTY_CATEGORY_HOME)
end
end
do
local cache
function WIA:SetDBGlobal_cache()
cache = self.db.global.cache
end
local value = setmetatable({}, {
__index = function (t, k)
local f = function(id)
return cache[id][k] or ""
end
rawset(t, k, f)
return f
end
})
local dash = "-"
local homeRealm = GetRealmName()
function value:name(id)
local name = split(dash, id)
return name or ""
end
function value:realm(id)
local name, realm = split(dash, id)
return realm or homeRealm
end
function value:guild(id)
WIA:UpdateGuildForPlayer(id)
return cache[id].guild or false
end
function value:bnet_tag(id)
local tag = cache[id].bnet_tag
return tag or ""
end
function WIA:UpdateCache(id, ...)
for i=1, select('#', ...) do
local valueType = select(i, ...)
cache[id][valueType] = value[valueType](value, id)
end
end
function WIA:GetCacheValue(id, valueType)
if cache[id][valueType] then
return cache[id][valueType]
else
self:UpdateCache(id, valueType)
return cache[id][valueType]
end
end
end
function WIA:CleanUpCache()
local guilds = getTable()
local guilds_realm = getTable()
local bnet_tags = getTable()
for id, data in pairs(self.db.profile.keywords) do
for entryName, entryType in pairs(data.list) do
if entryType == self.LIST_ENTRY_TYPES.GUILD then
guilds[entryName] = true
elseif entryType == self.LIST_ENTRY_TYPES.GUILD_REALM then
guilds_realm[entryName] = true
elseif entryType == self.LIST_ENTRY_TYPES.BNET_TAG then
bnet_tags[entryName] = true
end
end
end
for id, data in pairs(self.db.global.cache) do
local inUse = false
if bnet_tags[data.bnet_tag] then
inUse = true
elseif data.guild then
if guilds[data.guild] or guilds_realm[data.guild] then
inUse = true
else
local guild_realm, guild = core:UniformGuildName(data.guild, data.realm)
if guilds[guild] or guilds_realm[guild_realm] then
inUse = true
end
end
end
if not inUse then
self.db.global.cache[id] = nil
end
end
end
function WIA:ResetCache()
wipe(self.db.global.cache)
end
function WIA:CheckPlayer(keywordID, playerName, presenceID, channelType, guid, joinedMessage)
local keyword = self.db.profile.keywords[keywordID]
local isBlock = keyword.listType == self.LIST_TYPES.BLOCK
local isAllow = keyword.listType == self.LIST_TYPES.ALLOW
if channelType == CT_NORMAL then
if type(playerName) == "string" then
local id = core:UniformPlayerName(playerName)
if self:CheckList(keywordID, id ) then
joinedTable[guid] = joinedMessage
local result, code = core:InvitePlayer(playerName)
--[===[@debug@
self:Printf("Invite Sent: %s", result and "OK" or "Error: "..code )
--@end-debug@]===]
elseif keyword.showInviteBlockMessage then
self:Printf(L["%s was not invited %s"], playerName, keyword.customBlockMessage or isAllow and L["Is not allowed."] or isBlock and L["Is blocked."] or "")
end
end
elseif channelType == CT_BNET then
if presenceID then
local friendIndex = BNGetFriendIndex(presenceID)
local canInvite = false
--local inviteIsBlocked = false
local numToons = BNGetNumFriendGameAccounts(friendIndex)
for toonIndex=1, numToons do
local _, toonName, client, realmName = BNGetFriendGameAccountInfo(friendIndex, toonIndex)
if client == BNET_CLIENT_WOW then
local id = core:UniformPlayerName(toonName, realmName)
local result = self:CheckList(keywordID, id)
if result and isAllow then
canInvite = true
break
elseif result and isBlock then
canInvite = true
elseif not result and isBlock then
canInvite = false
break
end
end
end
if canInvite then
joinedTable[guid] = joinedMessage
local result, code = core:InviteBNet(presenceID, friendIndex)
--[===[@debug@
self:Printf("Invite Sent: %s", result and "OK" or "Error: "..code )
--@end-debug@]===]
elseif keyword.showInviteBlockMessage then
local _, name = BNGetFriendInfo(friendIndex)
self:Printf(L["%s was not invited: %s"], name, keyword.customBlockMessage or isAllow and L["Is not allowed."] or isBlock and L["Is blocked."] or "")
end
end
end
end
function WIA:CheckList(keywordID, id)
if not keywordID or not id then return false end
local data = self.db.profile.keywords[keywordID]
local isBlock = data.listType == self.LIST_TYPES.BLOCK
local isAllow = data.listType == self.LIST_TYPES.ALLOW
local canInvite = isBlock and true or false
--[===[@debug@
self:Printf("CheckList: [%s]%s -> %s ", keywordID, data.keyword, id)
--@end-debug@]===]
for entryName, entryType in pairs(data.list) do
if entryType == self.LIST_ENTRY_TYPES.PLAYER then
local _
local name = self:GetCacheValue(id, "name")
_, name = core:UniformPlayerName(name)
if name == entryName then
if isAllow then
canInvite = true
elseif isBlock then
canInvite = false
end
break
end
elseif entryType == self.LIST_ENTRY_TYPES.PLAYER_REALM then
local name_realm = core:UniformPlayerName(self:GetCacheValue(id, "name"), self:GetCacheValue(id, "realm") )
if name_realm == entryName then
if isAllow then
canInvite = true
elseif isBlock then
canInvite = false
end
break
end
elseif entryType == self.LIST_ENTRY_TYPES.GUILD then
local _
local guild = self:GetCacheValue(id, "guild")
_, guild = core:UniformGuildName(guild)
if guild == entryName then
if isAllow then
canInvite = true
elseif isBlock then
canInvite = false
end
break
end
elseif entryType == self.LIST_ENTRY_TYPES.GUILD_REALM then
local guild = self:GetCacheValue(id, "guild")
local realm = self:GetCacheValue(id, "realm")
local guild_realm = core:UniformGuildName(guild, realm)
if guild_realm == entryName then
if isAllow then
canInvite = true
elseif isBlock then
canInvite = false
end
break
end
elseif entryType == self.LIST_ENTRY_TYPES.REALM then
local realm = self:GetCacheValue(id, "realm")
if realm == entryName then
if isAllow then
canInvite = true
elseif isBlock then
canInvite = false
end
break
end
elseif entryType == self.LIST_ENTRY_TYPES.BNET_TAG then
local tag = self:GetCacheValue(id, "bnet_tag")
if tag == entryName then
if isAllow then
canInvite = true
elseif isBlock then
canInvite = false
end
break
end
end
end
return canInvite
end
function WIA:CheckHasListEntrys(key)
local valueChanged
local key_data = self.db.profile.keywords[key]
if key and key_data.name then
local hasGuild = false
local hasBNet = false
--[===[@debug@
self:Printf("CheckHasListEntrys(%s)", key)
--@end-debug@]===]
for entryName, entryType in pairs(key_data.list ) do
if entryType == self.LIST_ENTRY_TYPES.GUILD or entryType == self.LIST_ENTRY_TYPES.GUILD_REALM then
hasGuild = true
elseif entryType == self.LIST_ENTRY_TYPES.BNET_TAG then
hasBNet = true
end
if hasGuild and hasBNet then
break
end
end
valueChanged = key_data.hasGuildListEntrys ~= hasGuild or key_data.hasBNetListEntrys ~= hasBNet or false
key_data.hasGuildListEntrys = hasGuild
key_data.hasBNetListEntrys = hasBNet
end
-- nil ~= false -> true
if valueChanged ~= false then
local hasGuild = false
local hasBNet = false
--[===[@debug@
self:Print("CheckHasListEntrys - global")
--@end-debug@]===]
for keywordID, keyword in pairs(self.db.profile.keywords) do
if keyword.hasGuildListEntrys then
hasGuild = true
end
if keyword.hasBNetListEntrys then
hasBNet = true
end
if hasBNet and hasGuild then
break
end
end
self.db.profile.hasGuildListEntrys = hasGuild
self.db.profile.hasBNetListEntrys = hasBNet
end
end
local optionHandler = {}
function optionHandler:Set(info, value, ...)
local name = info[#info]
WIA.db.profile[name] = value
end
function optionHandler:Get(info, value, ...)
local name = info[#info]
return WIA.db.profile[name]
end
function optionHandler:AddKeyword(info, value)
local count = 0
local cleanValue = gsub(value, "[%c\127]", "")
local keywordID = cleanValue..count
local keywords = WIA.db.profile.keywords
while keywords[keywordID].name do
count = count + 1
keywordID = cleanValue..count
--[===[@debug@
WIA:Printf("Set keywordID to %s", keywordID)
--@end-debug@]===]
-- WARN: buffer-overflow check?...
end
keywords[keywordID].name = value
if not keywords[keywordID].caseSensitive then
keywords[keywordID].keyword = lower(value)
else
keywords[keywordID].keyword = value
end
end
function optionHandler:GetEmptyString(info)
return ""
end
function optionHandler:IsPlainMatchHidden(info, value)
local parent = info[#info-1]
return WIA.db.profile.keywords[parent].fullMatch
end
function optionHandler:IsCustomBlockMessageHidden(info, value)
local parent = info[#info-1]
return not WIA.db.profile.keywords[parent].showInviteBlockMessage
end
function optionHandler:KeywordSet(info, value)
local name = info[#info]
local parent = info.arg or info[#info-1]
WIA.db.profile.keywords[parent][name] = value
end
function optionHandler:KeywordGet(info, value)
local name = info[#info]
local parent = info.arg or info[#info-1]
return WIA.db.profile.keywords[parent][name]
end
function optionHandler:DeleteKeyword(info)
local key = info.args or info[#info-1]
WIA.db.profile.keywords[key] = nil
end
function optionHandler:SetChannel(info, name, value)
local parent = info[#info]
local key = info[#info-1]
if value then
local channelTyp = parent == "bnet_channels" and CT_BNET or CT_NORMAL
WIA:RegisterChannel(name, channelTyp, key)
else
WIA:UnregisterChannel(name, key)
end
WIA.db.profile.keywords[key][parent][name] = value
end
function optionHandler:GetChannel(info, name, value)
local parent = info[#info]
local key = info[#info-1]
return WIA.db.profile.keywords[key][parent][name]
end
do
local cacheChanged = {}
function optionHandler:ChangeMultiselectValues(info, value)
local name = info[#info]
local scope = info.arg or "global"
WIA.db[scope][name][value] = type(WIA.db[scope][name][value]) ~= "nil" and true or nil
cacheChanged[name] = true
end
local cache = {}
function optionHandler:MultiselectValues(info)
local name = info[#info]
local scope = info.arg or "global"
if not cache[name] or cacheChanged[name] then
cache[name] = wipe(cache[name] or {} )
for channel in pairs(WIA.db[scope][name]) do
cache[name][channel] = L[channel]
end
cacheChanged[name] = false
end
return cache[name]
end
end
function optionHandler:SetCaseSensitive(info, value, ...)
self:KeywordSet(info, value, ...)
local parent = info.arg or info[#info-1]
if value then
WIA.db.profile.keywords[parent].keyword = WIA.db.profile.keywords[parent].name
else
WIA.db.profile.keywords[parent].keyword = lower(WIA.db.profile.keywords[parent].name)
end
end
do
local UnitName, BNGetInfo =
UnitName, BNGetInfo
local cache = {}
local function getCacheIndex(info, startString, startCount)
local index = type(startString) == "string" and startString or ""
startCount = type(startCount) == "number" and startCount or #info
for i=startCount, 1, -1 do
index = index..info[i]
end
return index
end
function optionHandler:SetCache(info, value)
local index = getCacheIndex(info)
cache[index] = value
end
function optionHandler:GetCache(info)
local index = getCacheIndex(info)
if not cache[index] then
cache[index] = cache[index] or info.arg
end
return cache[index]
end
local name
local orgRealm
local guild
local bntag
local bntagIDLen
local nameRealmUsage
local guildRealmUsage
local bntagUsage
function optionHandler:populateIsValidEntryStrings()
name = UnitName("player")
orgRealm = gsub(GetRealmName(), "%s", "")
guild = GetGuildInfo("player")
guild = ( type(guild) == "string" and len(guild) > 1 ) and guild or L["Guild Name"]
bntag = select(2, BNGetInfo() )
bntag = ( type(bntag) == "string" and len(bntag) > 5 ) and bntag or name.."#1234"
--bntagIDLen = select(2, ("#"):split(bntag) ):len() or 4
bntagIDLen = len( select(2, split("#", bntag) ) ) or 4
nameRealmUsage = format(L["Usage: name-realm e.g: %s-%s"], name, orgRealm)
guildRealmUsage = format(L["Usage: guild-realm e.g: %s-%s"], guild, orgRealm)
bntagUsage = format(L["E.g: %s"], bntag)
end
function optionHandler:IsValidEntry(info, value)
local index = getCacheIndex(info, "listEntryType", #info-1)
local entryType = cache[index] or WIA.LIST_ENTRY_TYPES.PLAYER