-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigPanel.lua
More file actions
2597 lines (2334 loc) · 85.7 KB
/
Copy pathConfigPanel.lua
File metadata and controls
2597 lines (2334 loc) · 85.7 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
local _, NS = ...
local ConfigPanel = {}
-- BSP-008: i18n hook. Identity today; future Locale ticket retrofits L.
local function L(s) return s end
-- BSP-009: GameTooltip helper for widget hover help. Mirrors HistoryPanel's
-- AttachTooltip; duplicated locally because the two files are independent
-- and a single shared module would require .toc load-order plumbing for
-- minor gain. `widget.frame or widget` is a historical AceGUI compatibility
-- fallback retained so any future widget wrapper that exposes `.frame` still
-- works without a refactor. EnableMouse is asserted because BackdropTemplate
-- hosts default mouse-disabled.
local function AttachTooltip(widget, title, body, hint)
if not widget then return end
local host = widget.frame or widget
if not host.HookScript then return end
if host.EnableMouse then host:EnableMouse(true) end
host:HookScript("OnEnter", function(self)
if not GameTooltip then return end
GameTooltip:SetOwner(self, "ANCHOR_RIGHT")
if title then GameTooltip:AddLine(L(title)) end
if body then GameTooltip:AddLine(L(body), 1.00, 1.00, 1.00, true) end
if hint then GameTooltip:AddLine(L(hint), 0.70, 0.70, 0.70, true) end
GameTooltip:Show()
end)
host:HookScript("OnLeave", function()
if GameTooltip then GameTooltip:Hide() end
end)
end
local DEFAULT_WIDTH = 700
local DEFAULT_HEIGHT = 500
local MIN_WIDTH = 600
local MIN_HEIGHT = 400
local NAV_WIDTH = 128
local CONTENT_PAD = 14
local ROW_HEIGHT = 32
local PAGE_ROWS = 8
local SECTIONS = {
"Detection",
"Categories",
"Surfaces",
"Allowlist",
"Blocked",
"History",
"UI",
"Dev",
}
local CATEGORY_KEYS = { "RMT", "Boosting", "Casino", "Phishing", "Commercial", "Anti" }
local SURFACE_KEYS = { "chat", "whisper", "bn-whisper" }
local SURFACE_LABELS = {
chat = "Chat",
whisper = "Whisper",
["bn-whisper"] = "Bnet whisper",
}
local DEFAULT_SETTINGS = {
threshold = 4,
enabledCategories = {
RMT = true,
Boosting = true,
Casino = true,
Phishing = true,
Commercial = true,
Anti = true,
},
mixedScriptEnabled = true,
mixedScriptWeight = 1,
antiSignalCap = -5,
filterBubbles = false,
historyMaxEntries = 300,
historyGlobalMaxEntries = 1000,
showMinimapButton = true,
devMode = false,
}
local frame
local content
local navButtons = {}
local activeSection = "Detection"
local sizeDirty
local embeddedMode
local initialized
local popupsRegistered
-- BSP-022: aceWidgets ringbuffer removed in Commit 3 (Slider/CheckBox went
-- native then). MultiLineEditBox dialog went native in Commit 4. ConfigPanel
-- no longer touches AceGUI at all; embed removal lands in Commit 5.
local nativeChildren = {}
local sectionStatus = {}
local removedAllowlistEntry
local pendingImport
local pendingHistoryMax
local pendingHistoryGlobalMax
local dialogFrame
local listState = {
allowlistSearch = "",
allowlistPage = 1,
allowlistAddText = "",
blockedSearch = "",
blockedPage = 1,
}
local function Print(message)
message = "|cff33ff99Sift|r " .. tostring(message)
if DEFAULT_CHAT_FRAME and DEFAULT_CHAT_FRAME.AddMessage then
DEFAULT_CHAT_FRAME:AddMessage(message)
else
print(message)
end
end
local function Now()
if type(GetServerTime) == "function" then
return GetServerTime()
end
return time()
end
local function GetSettings()
if NS.DB and NS.DB.GetSettings then
return NS.DB.GetSettings()
end
return nil
end
local function GetGlobal()
if NS.DB and NS.DB.GetGlobal then
return NS.DB.GetGlobal()
end
return nil
end
local function GetChar()
if NS.DB and NS.DB.GetChar then
return NS.DB.GetChar()
end
local db = NS.DB and NS.DB.db
return db and db.char
end
local function GetHistoryStats()
if NS.History and NS.History.GetStats then
return NS.History.GetStats()
end
local entries = NS.History and NS.History.GetAll and NS.History.GetAll() or {}
return {
lifetime = {
detections = #entries,
blocked = #entries,
restored = 0,
},
retained = {
detections = #entries,
blocked = #entries,
restored = 0,
},
}
end
local function CopyTable(tbl)
local out = {}
if type(tbl) == "table" then
for key, value in pairs(tbl) do
out[key] = value
end
end
return out
end
local function SettingValue(key)
local settings = GetSettings()
if settings and settings[key] ~= nil then
return settings[key]
end
return DEFAULT_SETTINGS[key]
end
local function SetSetting(key, value)
if NS.DB and NS.DB.SetSetting then
return NS.DB.SetSetting(key, value) ~= nil
end
local settings = GetSettings()
if settings then
settings[key] = value
return true
end
return false
end
local function SetFilterBubblesEnabled(value)
value = value == true
SetSetting("filterBubbles", value)
if not value and NS.BubbleSuppressor and NS.BubbleSuppressor.MaybeRestore then
NS.BubbleSuppressor.MaybeRestore()
end
end
local function ResetSettings()
if NS.DB and NS.DB.ResetSettings then
NS.DB.ResetSettings()
return true
end
local settings = GetSettings()
if not settings then
return false
end
for key in pairs(settings) do
settings[key] = nil
end
for key, value in pairs(DEFAULT_SETTINGS) do
if type(value) == "table" then
settings[key] = CopyTable(value)
else
settings[key] = value
end
end
return true
end
local function ClampNumber(value, minValue, maxValue, fallback)
value = tonumber(value) or fallback
if value < minValue then value = minValue end
if value > maxValue then value = maxValue end
return value
end
local function ClearTable(tbl)
if type(wipe) == "function" then
wipe(tbl)
return
end
for key in pairs(tbl) do
tbl[key] = nil
end
end
local function GetCharStore()
local char = GetChar()
if not char then
return nil
end
char.configPanel = char.configPanel or {}
return char.configPanel
end
local function SavePosition()
if not frame then
return
end
local store = GetCharStore()
if not store then
return
end
store.x = frame:GetLeft()
store.y = frame:GetTop()
end
local function SaveSize()
if not frame then
return
end
local store = GetCharStore()
if not store then
return
end
store.width = frame:GetWidth()
store.height = frame:GetHeight()
sizeDirty = false
end
local function ApplyStoredGeometry()
local store = GetCharStore() or {}
local width = ClampNumber(store.width, MIN_WIDTH, 2000, DEFAULT_WIDTH)
local height = ClampNumber(store.height, MIN_HEIGHT, 1600, DEFAULT_HEIGHT)
frame:SetSize(width, height)
frame:ClearAllPoints()
if store.x and store.y then
frame:SetPoint("TOPLEFT", UIParent, "BOTTOMLEFT", store.x, store.y)
else
frame:SetPoint("CENTER", UIParent, "CENTER", 0, 0)
end
end
local function TrackNative(child)
nativeChildren[#nativeChildren + 1] = child
return child
end
local function ReleaseNativeChild(child)
if not child then return end
if child.GetRegions then
for i = 1, select("#", child:GetRegions()) do
local region = select(i, child:GetRegions())
if region and region.Hide then
region:Hide()
end
if region and region.ClearAllPoints then
region:ClearAllPoints()
end
end
end
if child.GetChildren then
for i = 1, select("#", child:GetChildren()) do
ReleaseNativeChild(select(i, child:GetChildren()))
end
end
if child.Hide then child:Hide() end
if child.ClearAllPoints then child:ClearAllPoints() end
end
local function ReleaseContent()
for _, child in ipairs(nativeChildren) do
ReleaseNativeChild(child)
end
nativeChildren = {}
end
local function AddText(text, template, x, y, width)
local fs = TrackNative(content:CreateFontString(nil, "OVERLAY", template or "GameFontNormal"))
fs:SetPoint("TOPLEFT", content, "TOPLEFT", x or CONTENT_PAD, y)
if width then
fs:SetWidth(width)
else
fs:SetPoint("RIGHT", content, "RIGHT", -CONTENT_PAD, 0)
end
fs:SetJustifyH("LEFT")
fs:SetText(text or "")
fs:Show()
return fs
end
local function AddSectionTitle(title, subtitle)
AddText(title, "GameFontNormalLarge", CONTENT_PAD, -12)
if subtitle and subtitle ~= "" then
local body = AddText(subtitle, "GameFontHighlightSmall", CONTENT_PAD, -36)
body:SetWordWrap(true)
return -70
end
return -48
end
local function AddStatus(y, message, good)
if not message or message == "" then
return y
end
local text = good and "|cff5ad080" or "|cffffd100"
AddText(text .. message .. "|r", "GameFontHighlightSmall", CONTENT_PAD, y)
return y - 22
end
local function AddNativeButton(label, x, y, width, onClick, tooltipBody)
local button = TrackNative(CreateFrame("Button", nil, content, "UIPanelButtonTemplate"))
button:SetSize(width or 120, 24)
button:SetPoint("TOPLEFT", content, "TOPLEFT", x, y)
button:SetText(label)
button:SetScript("OnClick", onClick)
if tooltipBody then AttachTooltip(button, label, tooltipBody) end
button:Show()
return button
end
local function AddEditBox(x, y, width, initialText, tooltipTitle, tooltipBody)
local editBox = TrackNative(CreateFrame("EditBox", nil, content, "InputBoxTemplate"))
editBox:SetSize(width or 180, 24)
editBox:SetPoint("TOPLEFT", content, "TOPLEFT", x, y)
editBox:SetAutoFocus(false)
editBox:SetText(initialText or "")
if tooltipTitle then AttachTooltip(editBox, tooltipTitle, tooltipBody) end
editBox:Show()
return editBox
end
local function AddDisabledRow(label, value, y)
local row = TrackNative(CreateFrame("Frame", nil, content, "BackdropTemplate"))
row:SetHeight(30)
row:SetPoint("TOPLEFT", content, "TOPLEFT", CONTENT_PAD, y)
row:SetPoint("RIGHT", content, "RIGHT", -CONTENT_PAD, 0)
if row.SetBackdrop then
row:SetBackdrop({ bgFile = "Interface\\Buttons\\WHITE8X8" })
row:SetBackdropColor(0.12, 0.12, 0.14, 0.55)
end
row:Show()
local left = TrackNative(row:CreateFontString(nil, "OVERLAY", "GameFontDisable"))
left:SetPoint("LEFT", row, "LEFT", 8, 0)
left:SetJustifyH("LEFT")
left:SetText(label)
left:Show()
local right = TrackNative(row:CreateFontString(nil, "OVERLAY", "GameFontDisableSmall"))
right:SetPoint("RIGHT", row, "RIGHT", -8, 0)
right:SetJustifyH("RIGHT")
right:SetText(value)
right:Show()
return y - 36
end
-- BSP-022 Commit 2: native sliders, dual-path.
-- Retail uses MinimalSliderWithSteppersTemplate (MWS) — the modern Settings-UI
-- slider with stepper buttons on each end, matching the look of Edit Mode and
-- Retail's Settings panels. Classic-family falls back to OptionsSliderTemplate
-- (MCP-confirmed present in DeprecatedTemplates.xml on all 3 flavors, used by
-- Classic's own Interface Options screens today). Both paths return a slider
-- object exposing the same AceGUI-compatible facade so the call sites are
-- identical regardless of flavor:
-- SetLabel(text), SetSliderValues(min, max, step),
-- SetValue(value), SetCallback("OnValueChanged", fn).
--
-- MWS-specific API reference (verified in Blizzard_SharedXML\Shared\Slider\
-- MinimalSlider.lua): Init(value, minValue, maxValue, steps, formatters) takes
-- `steps` as a count, not a step size — we compute steps = (max-min)/step.
-- The formatter table is keyed by MinimalSliderWithSteppersMixin.Label.{Top,
-- Min, Max} enum values; we use Top for "Label: value", Min/Max for range
-- bounds. The OnValueChanged event is registered through CallbackRegistryMixin
-- (RegisterCallback), not the native OnValueChanged script.
local nextSliderId = 0
local function MakeMWSSlider(x, y, width, label, minValue, maxValue, step)
local mws = TrackNative(CreateFrame("Frame", nil, content, "MinimalSliderWithSteppersTemplate"))
mws:SetSize(width or 280, 40)
mws:SetPoint("TOPLEFT", content, "TOPLEFT", x or CONTENT_PAD, y)
local labelText = label or ""
local function buildFormatters(minV, maxV)
return {
[MinimalSliderWithSteppersMixin.Label.Top] = function(v)
return string.format("%s: %d", labelText, math.floor((tonumber(v) or 0) + 0.5))
end,
[MinimalSliderWithSteppersMixin.Label.Min] = CreateMinimalSliderFormatter(
MinimalSliderWithSteppersMixin.Label.Min, tostring(minV)),
[MinimalSliderWithSteppersMixin.Label.Max] = CreateMinimalSliderFormatter(
MinimalSliderWithSteppersMixin.Label.Max, tostring(maxV)),
}
end
local function reInit(value, minV, maxV, stepV)
local span = (maxV - minV)
local stepSize = stepV or 1
local steps = math.max(1, math.floor(span / stepSize + 0.5))
mws:Init(value, minV, maxV, steps, buildFormatters(minV, maxV))
end
-- Initial setup: caller's subsequent SetValue replaces the initial value.
reInit(minValue, minValue, maxValue, step)
local valueCb
function mws:SetLabel(text)
labelText = text or ""
-- Refresh the Top label so the new name appears immediately.
self:FormatValue(self.Slider:GetValue())
end
function mws:SetSliderValues(minV, maxV, stepV)
local v = self.Slider:GetValue()
if v < minV then v = minV end
if v > maxV then v = maxV end
reInit(v, minV, maxV, stepV)
end
-- mws:SetValue already exists from MinimalSliderWithSteppersMixin (line 169
-- of MinimalSlider.lua) — proxies to self.Slider:SetValue.
function mws:SetCallback(event, fn)
if event ~= "OnValueChanged" then return end
if valueCb then
-- Already registered the dispatch closure; just swap the user fn.
valueCb = fn
return
end
valueCb = fn
self:RegisterCallback(MinimalSliderWithSteppersMixin.Event.OnValueChanged,
function(_, value)
if valueCb then valueCb(self, "OnValueChanged", value) end
end, self)
end
mws:Show()
return mws
end
local function MakeOptionsSlider(x, y, width, label, minValue, maxValue, step)
nextSliderId = nextSliderId + 1
local name = "SiftConfigSlider" .. nextSliderId
local slider = TrackNative(CreateFrame("Slider", name, content, "OptionsSliderTemplate"))
slider:SetSize(width or 280, 17)
slider:SetPoint("TOPLEFT", content, "TOPLEFT", x or CONTENT_PAD, y)
slider:SetMinMaxValues(minValue, maxValue)
slider:SetValueStep(step or 1)
if slider.SetObeyStepOnDrag then
slider:SetObeyStepOnDrag(true)
end
-- OptionsSliderTemplate auto-creates these via $parent name resolution.
local textFS = _G[name .. "Text"]
local lowFS = _G[name .. "Low"]
local highFS = _G[name .. "High"]
if textFS then textFS:SetText(label or "") end
if lowFS then lowFS:SetText(tostring(minValue)) end
if highFS then highFS:SetText(tostring(maxValue)) end
-- Current-value readout below the slider; preserves the AceGUI visual
-- where the current value sat near the slider.
local valueFS = slider:CreateFontString(nil, "OVERLAY", "GameFontHighlightSmall")
valueFS:SetPoint("TOP", slider, "BOTTOM", 0, -2)
local callbacks = {}
function slider:SetLabel(text)
if textFS then textFS:SetText(text or "") end
end
function slider:SetSliderValues(minV, maxV, stepV)
slider:SetMinMaxValues(minV, maxV)
slider:SetValueStep(stepV or 1)
if lowFS then lowFS:SetText(tostring(minV)) end
if highFS then highFS:SetText(tostring(maxV)) end
end
-- Wrap native SetValue so the value readout updates even on initial set
-- (which happens before the OnValueChanged script binds the callback).
local nativeSetValue = slider.SetValue
function slider:SetValue(value)
nativeSetValue(slider, value)
valueFS:SetText(tostring(math.floor((tonumber(value) or 0) + 0.5)))
end
function slider:SetCallback(event, fn)
callbacks[event] = fn
end
slider:SetScript("OnValueChanged", function(self, value)
value = tonumber(value)
if value == nil then return end
valueFS:SetText(tostring(math.floor(value + 0.5)))
local fn = callbacks.OnValueChanged
if fn then fn(self, "OnValueChanged", value) end
end)
slider:Show()
return slider
end
local function MakeNativeSlider(x, y, width, label, minValue, maxValue, step)
if NS.Compat and NS.Compat.isClassicFamily then
return MakeOptionsSlider(x, y, width, label, minValue, maxValue, step)
end
-- Retail path: MWS. If the template is somehow missing at runtime, fall
-- back gracefully so the panel still renders.
if type(MinimalSliderWithSteppersMixin) ~= "table" then
return MakeOptionsSlider(x, y, width, label, minValue, maxValue, step)
end
return MakeMWSSlider(x, y, width, label, minValue, maxValue, step)
end
local function AddSlider(label, key, minValue, maxValue, step, y, tooltipBody)
local slider = MakeNativeSlider(CONTENT_PAD, y, 330, label, minValue, maxValue, step)
slider:SetValue(tonumber(SettingValue(key)) or tonumber(DEFAULT_SETTINGS[key]) or minValue)
slider:SetCallback("OnValueChanged", function(_, _, value)
value = ClampNumber(value, minValue, maxValue, DEFAULT_SETTINGS[key] or minValue)
if step >= 1 then
value = math.floor(value + 0.5)
end
SetSetting(key, value)
end)
if tooltipBody then AttachTooltip(slider, label, tooltipBody) end
return y - 48
end
-- BSP-022 Commit 3: native checkboxes via UICheckButtonTemplate.
-- Universal template (works on all 3 flavors), so no flavor branch.
-- The label is a FontString anchored to the right of the checkbox.
-- The onChange callback receives a boolean (the new checked state).
local function MakeNativeCheckbox(x, y, label, initialChecked, onChange, tooltipBody)
local cb = TrackNative(CreateFrame("CheckButton", nil, content, "UICheckButtonTemplate"))
cb:SetSize(24, 24)
cb:SetPoint("TOPLEFT", content, "TOPLEFT", x or CONTENT_PAD, y)
cb:SetChecked(initialChecked and true or false)
local labelFS = cb:CreateFontString(nil, "OVERLAY", "GameFontNormal")
labelFS:SetPoint("LEFT", cb, "RIGHT", 4, 0)
labelFS:SetJustifyH("LEFT")
labelFS:SetText(label or "")
cb:SetScript("OnClick", function(self)
local value = self:GetChecked() and true or false
if onChange then onChange(value) end
end)
if tooltipBody then AttachTooltip(cb, label, tooltipBody) end
cb:Show()
return cb
end
local function AddCheckbox(label, key, y, onChanged, tooltipBody)
MakeNativeCheckbox(CONTENT_PAD, y, label, SettingValue(key) == true, function(value)
if onChanged then
onChanged(value)
else
SetSetting(key, value)
end
end, tooltipBody)
return y - 32
end
local function SectionExists(section)
for _, name in ipairs(SECTIONS) do
if name == section then
return true
end
end
return false
end
local function SetNavHighlight()
for name, button in pairs(navButtons) do
if name == activeSection then
button:LockHighlight()
else
button:UnlockHighlight()
end
end
end
local function RelativeTime(ts)
ts = tonumber(ts)
if not ts then
return "-"
end
local delta = Now() - ts
if delta < 0 then return "0s" end
if delta < 60 then return tostring(delta) .. "s" end
if delta < 3600 then return tostring(math.floor(delta / 60)) .. "m" end
if delta < 86400 then return tostring(math.floor(delta / 3600)) .. "h" end
if delta < 90 * 86400 then return tostring(math.floor(delta / 86400)) .. "d" end
return date("%Y-%m-%d", ts)
end
local function SenderLabel(entry)
if type(entry) ~= "table" then
return "?"
end
local name = entry.name or "?"
if entry.realm and entry.realm ~= "" then
return name .. "-" .. entry.realm
end
return name
end
local function Lower(value)
return string.lower(tostring(value or ""))
end
local function MatchesSearch(entry, guid, search)
search = Lower(search)
if search == "" then
return true
end
return string.find(Lower(guid), search, 1, true)
or string.find(Lower(SenderLabel(entry)), search, 1, true)
or string.find(Lower(entry and entry.source), search, 1, true)
end
local function SortedAllowlist()
local allowlist = NS.Trust and NS.Trust.GetAllowlist and NS.Trust.GetAllowlist() or {}
local out = {}
for guid, entry in pairs(allowlist) do
if type(guid) == "string" and type(entry) == "table"
and MatchesSearch(entry, guid, listState.allowlistSearch) then
out[#out + 1] = { guid = guid, entry = entry }
end
end
table.sort(out, function(a, b)
return Lower(SenderLabel(a.entry)) < Lower(SenderLabel(b.entry))
end)
return out
end
local function GetBlockedActors()
local global = GetGlobal()
if not global then
return {}
end
global.blockedActors = global.blockedActors or {}
return global.blockedActors
end
local function BlockedEntryLabel(key, entry)
if type(entry) == "table" then
local name = entry.name or entry.sender or entry.player
local realm = entry.realm
if name and realm and realm ~= "" then
return tostring(name) .. "-" .. tostring(realm)
end
if name then
return tostring(name)
end
end
return tostring(key)
end
local function BlockedEntryCount(entry)
if type(entry) == "table" then
return tonumber(entry.count or entry.blockCount or entry.blocks or entry.total) or 1
end
return 1
end
local function BlockedEntryLastSeen(entry)
if type(entry) == "table" then
return entry.lastBlockedAt or entry.lastSeenAt or entry.updatedAt or entry.ts
end
return nil
end
local function SortedBlockedActors()
local blocked = GetBlockedActors()
local search = Lower(listState.blockedSearch)
local out = {}
for key, entry in pairs(blocked) do
local label = BlockedEntryLabel(key, entry)
if search == "" or string.find(Lower(key), search, 1, true)
or string.find(Lower(label), search, 1, true) then
out[#out + 1] = { key = key, entry = entry, label = label }
end
end
table.sort(out, function(a, b)
local ats = tonumber(BlockedEntryLastSeen(a.entry)) or 0
local bts = tonumber(BlockedEntryLastSeen(b.entry)) or 0
if ats ~= bts then
return ats > bts
end
return Lower(a.label) < Lower(b.label)
end)
return out
end
local function MaxPage(total)
local maxPage = math.ceil(total / PAGE_ROWS)
if maxPage < 1 then
maxPage = 1
end
return maxPage
end
local function FindHistorySender(text)
text = tostring(text or "")
local name, realm = string.match(text, "^%s*([^%-]+)%-(.-)%s*$")
if not name or name == "" or not realm or realm == "" then
return nil, "Enter a sender as Name-Realm."
end
local entries = NS.History and NS.History.GetAll and NS.History.GetAll() or {}
local wantedName = Lower(name)
local wantedRealm = Lower(realm)
for _, entry in ipairs(entries) do
if entry.guid and entry.guid ~= ""
and Lower(entry.name) == wantedName
and Lower(entry.realm) == wantedRealm then
return entry
end
end
return nil, "Sift can only manually allow players already present in History."
end
local function AddAllowlistFromText(text)
local entry, err = FindHistorySender(text)
if not entry then
sectionStatus.Allowlist = err
return false
end
if not NS.Trust or not NS.Trust.AddAllowlist then
sectionStatus.Allowlist = "Allowlist API is unavailable."
return false
end
if NS.Trust.AddAllowlist(entry.guid, entry.name, entry.realm, "manual") then
sectionStatus.Allowlist = "Added " .. SenderLabel(entry) .. "."
listState.allowlistAddText = ""
removedAllowlistEntry = nil
return true
end
sectionStatus.Allowlist = SenderLabel(entry) .. " is already allowlisted."
return false
end
local function RemoveAllowlist(guid, entry)
if not NS.Trust or not NS.Trust.RemoveAllowlist then
sectionStatus.Allowlist = "Allowlist API is unavailable."
return
end
if NS.Trust.RemoveAllowlist(guid) then
removedAllowlistEntry = { guid = guid, entry = CopyTable(entry) }
sectionStatus.Allowlist = "Removed " .. SenderLabel(entry) .. "."
ConfigPanel.ShowSection("Allowlist")
end
end
local function UndoAllowlistRemove()
local removed = removedAllowlistEntry
if not removed or not NS.Trust or not NS.Trust.AddAllowlist then
return
end
local entry = removed.entry or {}
NS.Trust.AddAllowlist(removed.guid, entry.name, entry.realm, entry.source or "manual")
removedAllowlistEntry = nil
sectionStatus.Allowlist = "Restored " .. SenderLabel(entry) .. "."
ConfigPanel.ShowSection("Allowlist")
end
local function RemoveBlocked(key)
if NS.DB and NS.DB.RemoveBlockedActor and NS.DB.RemoveBlockedActor(key) then
sectionStatus.Blocked = "Removed blocked actor."
ConfigPanel.ShowSection("Blocked")
return
end
local blocked = GetBlockedActors()
blocked[key] = nil
sectionStatus.Blocked = "Removed blocked actor."
ConfigPanel.ShowSection("Blocked")
end
local function RefreshHistoryPanelMinimap()
if NS.HistoryPanel and NS.HistoryPanel.RefreshMinimap then
NS.HistoryPanel.RefreshMinimap()
end
end
local function SetHistoryPanelMinimapShown(shown)
if NS.HistoryPanel and NS.HistoryPanel.SetMinimapShown then
NS.HistoryPanel.SetMinimapShown(shown)
end
RefreshHistoryPanelMinimap()
end
local function EscapeField(value)
value = tostring(value or "")
value = string.gsub(value, "\\", "\\\\")
value = string.gsub(value, "|", "\\p")
value = string.gsub(value, "\r", "\\r")
value = string.gsub(value, "\n", "\\n")
return value
end
local function UnescapeField(value)
local out = {}
local i = 1
while i <= #value do
local ch = string.sub(value, i, i)
if ch == "\\" then
local nextCh = string.sub(value, i + 1, i + 1)
if nextCh == "\\" then
out[#out + 1] = "\\"
elseif nextCh == "p" then
out[#out + 1] = "|"
elseif nextCh == "n" then
out[#out + 1] = "\n"
elseif nextCh == "r" then
out[#out + 1] = "\r"
else
return nil
end
i = i + 2
else
out[#out + 1] = ch
i = i + 1
end
end
return table.concat(out)
end
local function SplitPipeFields(value)
local fields = {}
local field = {}
local i = 1
while i <= #value do
local ch = string.sub(value, i, i)
if ch == "\\" then
local nextCh = string.sub(value, i + 1, i + 1)
if nextCh == "\\" or nextCh == "p" or nextCh == "n" or nextCh == "r" then
field[#field + 1] = ch .. nextCh
i = i + 2
else
return nil
end
elseif ch == "|" then
fields[#fields + 1] = table.concat(field)
field = {}
i = i + 1
else
field[#field + 1] = ch
i = i + 1
end
end
fields[#fields + 1] = table.concat(field)
for index, raw in ipairs(fields) do
local parsed = UnescapeField(raw)
if parsed == nil then
return nil
end
fields[index] = parsed
end
return fields
end
local function ValidGuid(guid)
return type(guid) == "string"
and guid ~= ""
and string.find(guid, "-", 1, true) ~= nil
and string.find(guid, "^[%w%-]+$") ~= nil
end
local function ValidName(value)
return value == "" or (type(value) == "string" and not string.find(value, "[%c|]"))
end
local function ValidSource(value)
return value == "" or value == "manual" or value == "history" or value == "import"
end
local function ParseImportText(text)
local formatName
local version
local exportedAt
local entries = {}
local seenGuids = {}
if type(text) ~= "string" or text == "" then
return nil, "Import text is empty."
end
local lineNumber = 0
for line in string.gmatch(text .. "\n", "([^\n]*)\n") do
lineNumber = lineNumber + 1
line = string.gsub(line, "\r$", "")
if line ~= "" then
local key, value = string.match(line, "^([A-Za-z]+)=(.*)$")
if not key then
return nil, "Line " .. lineNumber .. " is not key=value."
end
if key == "format" then
if formatName then return nil, "Duplicate format line." end
formatName = value
elseif key == "version" then
if version then return nil, "Duplicate version line." end
version = tonumber(value)
elseif key == "exportedAt" then
if exportedAt then return nil, "Duplicate exportedAt line." end
exportedAt = tonumber(value)
elseif key == "entry" then
local fields = SplitPipeFields(value)
if fields and #fields == 5 then
fields[6] = ""
end
if not fields or #fields ~= 6 then
return nil, "Line " .. lineNumber .. " must have 6 entry fields."
end
local guid = fields[1]
local name = fields[2]
local realm = fields[3]
local source = fields[4]
local addedAt = fields[5]
local lastSeenAt = fields[6]
if not ValidGuid(guid) then
return nil, "Line " .. lineNumber .. " has an invalid GUID."
end
if seenGuids[guid] then
return nil, "Line " .. lineNumber .. " repeats a GUID."
end
if not ValidName(name) or not ValidName(realm) then
return nil, "Line " .. lineNumber .. " has invalid name fields."
end
if not ValidSource(source) then
return nil, "Line " .. lineNumber .. " has an invalid source."
end
if addedAt ~= "" and not tonumber(addedAt) then
return nil, "Line " .. lineNumber .. " has invalid addedAt."
end
if lastSeenAt ~= "" and not tonumber(lastSeenAt) then
return nil, "Line " .. lineNumber .. " has invalid lastSeenAt."
end
seenGuids[guid] = true
entries[#entries + 1] = {
guid = guid,