CCCPaste
script_name("Shut Up")
script_author("Maku Kenesu")

require "lib.moonloader"
require "lib.sampfuncs"
local ev = require "lib.samp.events"
local inicfg = require "inicfg"

------------------------------------------------------------
-- BASIC CONFIG
------------------------------------------------------------
local PRIMARY_COLOR = 0xFF5A5A
local LABEL_COLOR = "{FF5A5A}"  -- primary cool red
local ACCENT_COLOR = "{00CED1}" -- accent teal
local label = LABEL_COLOR .. "[Shut Up]{FFFFFF} "
local iniFilePath = "ShutUp.ini"

-- Load or create INI
local config = inicfg.load({
  global = {},
  newbie = {},
  anywhere = {}
}, iniFilePath)

------------------------------------------------------------
-- HELPERS
------------------------------------------------------------
local function saveConfig()
  inicfg.save(config, iniFilePath)
end

-- Ensure the INI file exists on first run
if not doesFileExist(iniFilePath) then
  saveConfig()
end

local function cleanName(name)
  if not name then return "" end
  return name:gsub("_", " "):lower()
end

local function formatName(name)
  return (name:gsub("(%a)([%w_']*)", function(first, rest)
    return first:upper() .. rest:lower()
  end))
end

local function nameInTable(tbl, name)
  local checkName = cleanName(name)
  for _, n in ipairs(tbl) do
    if cleanName(n) == checkName then return true end
  end
  return false
end

local function removeName(tbl, name)
  local checkName = cleanName(name)
  for i, n in ipairs(tbl) do
    if cleanName(n) == checkName then
      table.remove(tbl, i)
      return true
    end
  end
  return false
end

------------------------------------------------------------
-- CHAT FILTER
------------------------------------------------------------
function ev.onServerMessage(color, text)
  for _, n in ipairs(config.anywhere) do
    if text:lower():find(cleanName(n)) then
      sampAddChatMessage(label .. "[Blocked Chat Anywhere]: " .. formatName(n), PRIMARY_COLOR)
      return false
    end
  end

  local globalName = text:match("%(%(.-%s([%w_]+%s?[%w_]*)%:%s")
  if globalName and nameInTable(config.global, globalName) then
    sampAddChatMessage(label .. "[Blocked Global Chat]: " .. formatName(globalName), PRIMARY_COLOR)
    return false
  end

  local newbieName = text:match("%*%*%s.-%s([%w_]+%s?[%w_]*)%:%s")
  if newbieName and nameInTable(config.newbie, newbieName) then
    sampAddChatMessage(label .. "[Blocked Newbie Chat]: " .. formatName(newbieName), PRIMARY_COLOR)
    return false
  end
end

------------------------------------------------------------
-- MAIN LOOP / COMMANDS
------------------------------------------------------------
function main()
  while not isSampAvailable() do wait(100) end
  sampAddChatMessage(label .. "loaded. Type /shutup.help for commands.", PRIMARY_COLOR)

  sampRegisterChatCommand("shutup.help", function()
    sampAddChatMessage(LABEL_COLOR .. "====== Shut Up Commands ======", PRIMARY_COLOR)
    sampAddChatMessage(ACCENT_COLOR .. "/shutup.g [Name]{FFFFFF} - Block player's global chat messages", PRIMARY_COLOR)
    sampAddChatMessage(ACCENT_COLOR .. "/shutup.n [Name]{FFFFFF} - Block player's newbie messages", PRIMARY_COLOR)
    sampAddChatMessage(ACCENT_COLOR .. "/shutthefuckup [Name]{FFFFFF} - Block player's chat anywhere", PRIMARY_COLOR)
    sampAddChatMessage(ACCENT_COLOR .. "/noshutup.g [Name]{FFFFFF} - Unblock player's global chat messages", PRIMARY_COLOR)
    sampAddChatMessage(ACCENT_COLOR .. "/noshutup.n [Name]{FFFFFF} - Unblock player's newbie", PRIMARY_COLOR)
    sampAddChatMessage(ACCENT_COLOR .. "/noshutthefuckup [Name]{FFFFFF} - Unblock player's chat anywhere", PRIMARY_COLOR)
    sampAddChatMessage(ACCENT_COLOR .. "/shutup.list{FFFFFF} - Show blocked list", PRIMARY_COLOR)
  end)

  sampRegisterChatCommand("shutup.g", function(param)
    if not param or param == "" then
      sampAddChatMessage(label .. "Usage: /shutup.g [Name]", PRIMARY_COLOR)
      return
    end
    local cleaned = cleanName(param)
    if not nameInTable(config.global, cleaned) then
      table.insert(config.global, cleaned)
      saveConfig()
      sampAddChatMessage(label .. "Blocked " .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} in global.", PRIMARY_COLOR)
    else
      sampAddChatMessage(label .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} already blocked in global.", PRIMARY_COLOR)
    end
  end)

  sampRegisterChatCommand("shutup.n", function(param)
    if not param or param == "" then
      sampAddChatMessage(label .. "Usage: /shutup.n [Name]", PRIMARY_COLOR)
      return
    end
    local cleaned = cleanName(param)
    if not nameInTable(config.newbie, cleaned) then
      table.insert(config.newbie, cleaned)
      saveConfig()
      sampAddChatMessage(label .. "Blocked " .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} in newbie.", PRIMARY_COLOR)
    else
      sampAddChatMessage(label .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} already blocked in newbie.", PRIMARY_COLOR)
    end
  end)

  sampRegisterChatCommand("shutthefuckup", function(param)
    if not param or param == "" then
      sampAddChatMessage(label .. "Usage: /shutup.a [Name]", PRIMARY_COLOR)
      return
    end
    local cleaned = cleanName(param)
    removeName(config.global, cleaned)
    removeName(config.newbie, cleaned)
    if not nameInTable(config.anywhere, cleaned) then
      table.insert(config.anywhere, cleaned)
      saveConfig()
      sampAddChatMessage(label .. "Blocked " .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} everywhere.", PRIMARY_COLOR)
    else
      sampAddChatMessage(label .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} already blocked everywhere.", PRIMARY_COLOR)
    end
  end)

  sampRegisterChatCommand("noshutup.g", function(param)
    local cleaned = cleanName(param)
    if removeName(config.global, cleaned) then
      saveConfig()
      sampAddChatMessage(label .. "Unblocked " .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} from global.", PRIMARY_COLOR)
    else
      sampAddChatMessage(label .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} not found in global.", PRIMARY_COLOR)
    end
  end)

  sampRegisterChatCommand("noshutup.n", function(param)
    local cleaned = cleanName(param)
    if removeName(config.newbie, cleaned) then
      saveConfig()
      sampAddChatMessage(label .. "Unblocked " .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} from newbie.", PRIMARY_COLOR)
    else
      sampAddChatMessage(label .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} not found in newbie.", PRIMARY_COLOR)
    end
  end)

  sampRegisterChatCommand("shutthefuckup", function(param)
    local cleaned = cleanName(param)
    if removeName(config.anywhere, cleaned) then
      saveConfig()
      sampAddChatMessage(label .. "Unblocked " .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} from anywhere.", PRIMARY_COLOR)
    else
      sampAddChatMessage(label .. ACCENT_COLOR .. formatName(cleaned) .. "{FFFFFF} not found in anywhere.", PRIMARY_COLOR)
    end
  end)

  sampRegisterChatCommand("shutup.list", function()
    local function formatList(tbl)
      local arr = {}
      for _, n in ipairs(tbl) do
        table.insert(arr, formatName(n))
      end
      return table.concat(arr, ", ")
    end

    sampAddChatMessage(LABEL_COLOR .. "==== Blocked Names ====", PRIMARY_COLOR)
    sampAddChatMessage("{FFFFFF}Global: " .. ACCENT_COLOR .. formatList(config.global), PRIMARY_COLOR)
    sampAddChatMessage("{FFFFFF}Newbie: " .. ACCENT_COLOR .. formatList(config.newbie), PRIMARY_COLOR)
    sampAddChatMessage("{FFFFFF}Anywhere: " .. ACCENT_COLOR .. formatList(config.anywhere), PRIMARY_COLOR)
  end)
end