Skip to content

Commit

Permalink
Added chatlog message blacklist feature
Browse files Browse the repository at this point in the history
The update introduces a new blacklist feature to the chat module. This allows for certain strings to be blacklisted, preventing them from being logged. The changes include:
- Addition of a 'blacklist' field in the database
- Implementation of checks against the blacklist before logging messages
- Functions to manage (add/remove/toggle) the blacklist
- UI options for managing the blacklist in settings.
This enhancement provides users with more control over what gets logged and helps filter out unwanted content.
  • Loading branch information
Wutname1 committed Aug 2, 2024
1 parent 1842269 commit 2318895
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 21 deletions.
133 changes: 112 additions & 21 deletions Modules/Chatbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ function module:OnInitialize()
CHAT_MSG_INSTANCE_CHAT = true,
CHAT_MSG_CHANNEL = true,
},
blacklist = {
enabled = true,
strings = { 'WTS' },
},
},
}
module.Database = SUI.SpartanUIDB:RegisterNamespace('Chatbox', { profile = defaults })
Expand Down Expand Up @@ -407,6 +411,15 @@ end
function module:LogChatMessage(event, message, sender, languageName, channelName, _, _, _, channelIndex, channelBaseName, _, _, guid, _, _, _, _, _)
if not self.DB.chatLog.enabled then return end

-- Check against blacklist
if self.DB.chatLog.blacklist.enabled then
for _, blacklistedString in ipairs(self.DB.chatLog.blacklist.strings) do
if message:lower():find(blacklistedString:lower(), 1, true) then
return -- Don't log this message
end
end
end

local entry = {
timestamp = time(),
event = event,
Expand Down Expand Up @@ -967,6 +980,19 @@ function module:CleanupOldChatLog()
end
end

-- Add functions to manage the blacklist
function module:AddBlacklistString(string)
if not tContains(self.DB.chatLog.blacklist.strings, string) then table.insert(self.DB.chatLog.blacklist.strings, string) end
end

function module:RemoveBlacklistString(string)
tDeleteItem(self.DB.chatLog.blacklist.strings, string)
end

function module:ToggleBlacklist(enable)
self.DB.chatLog.blacklist.enabled = enable
end

function module:BuildOptions()
--@type AceConfig.OptionsTable
local optTable = {
Expand Down Expand Up @@ -1123,33 +1149,98 @@ function module:BuildOptions()
module.DB.chatLog.typesToLog[key] = value
module:EnableChatLog()
end,
order = 5,
},
cleanupLoginMessages = {
name = L['Clean Up Login Messages'],
desc = L['Remove addon spam and unnecessary messages on login'],
type = 'toggle',
get = function()
return module.DB.chatLog.cleanupLoginMessages
end,
set = function(_, val)
module.DB.chatLog.cleanupLoginMessages = val
end,
order = 4,
order = 6,
},
clearLog = {
name = L['Clear Chat Log'],
desc = L['Clear all saved chat log entries'],
type = 'execute',
func = function()
module:ClearChatLog()
end,
order = 4,
blacklist = {
name = L['Blacklist'],
type = 'group',
order = 7,
inline = true,
args = {},
},
},
},
},
}

local function isBlacklistDuplicate(newString)
for _, existingString in ipairs(module.DB.chatLog.blacklist.strings) do
if newString:lower() == existingString:lower() then return true end
end
return false
end

local function applyBlacklistToHistory(blacklistString)
local newHistory = {}
local removed = 0
for _, entry in ipairs(module.DB.chatLog.history) do
if not string.find(entry.message:lower(), blacklistString:lower()) then
table.insert(newHistory, entry)
else
removed = removed + 1
end
end
if removed > 0 then SUI:Print(string.format(L['Removed %d entries containing %s'], removed, blacklistString)) end
module.DB.chatLog.history = newHistory
end

local function buildBlacklistOptions()
local blacklistOpt = optTable.args.chatLog.args.blacklist.args
table.wipe(blacklistOpt)

blacklistOpt.desc = {
name = L['Blacklisted strings will not be logged'],
type = 'description',
order = 1,
}

blacklistOpt.add = {
name = L['Add Blacklist String'],
desc = L['Add a string to the blacklist'],
type = 'input',
order = 2,
set = function(_, val)
if isBlacklistDuplicate(val) then
SUI:Print(string.format(L["'%s' is already in the blacklist"], val))
else
table.insert(module.DB.chatLog.blacklist.strings, val)
applyBlacklistToHistory(val)
buildBlacklistOptions()
end
end,
}

blacklistOpt.list = {
order = 3,
type = 'group',
inline = true,
name = L['Blacklist'],
args = {},
}

for index, entry in ipairs(module.DB.chatLog.blacklist.strings) do
blacklistOpt.list.args[tostring(index) .. 'label'] = {
type = 'description',
width = 'double',
fontSize = 'medium',
order = index * 2 - 1,
name = entry,
}
blacklistOpt.list.args[tostring(index)] = {
type = 'execute',
name = L['Delete'],
width = 'half',
order = index * 2,
func = function()
table.remove(module.DB.chatLog.blacklist.strings, index)
buildBlacklistOptions()
end,
}
end
end

buildBlacklistOptions()

SUI.Options:AddOptions(optTable, 'Chatbox')
end

Expand Down
8 changes: 8 additions & 0 deletions lang/enUS.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
---@class SUIL
local L = LibStub('AceLocale-3.0'):NewLocale('SpartanUI', 'enUS', true, debug)

L['Blacklisted strings will not be logged'] = true
L['Add Blacklist String'] = true
L['Add a string to the blacklist'] = true
L['Blacklist'] = true
L['Delete'] = true
L["Added '%s' to blacklist and removed matching entries from history"] = true
L["Removed '%s' from blacklist"] = true
L['Removed %d entries containing %s'] = true
L['Chat Types to Log'] = true
L['Say'] = true
L['Yell'] = true
Expand Down

0 comments on commit 2318895

Please sign in to comment.