Skip to content

Commit

Permalink
Implemented a basic censor module (refs #87)
Browse files Browse the repository at this point in the history
* implemented chat message censor
* created settings
* created constants for chat type commands
  • Loading branch information
timosmit committed Jan 18, 2019
1 parent 48a1375 commit 4e917f7
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 2 deletions.
6 changes: 6 additions & 0 deletions config/wolfadmin.toml
Expand Up @@ -37,6 +37,12 @@ maxdif = 5
selection = 0
interval = 30

[censor]
file = "censor.toml"
mode = 1
mute = 60
kick = 1

[game]
announcerevives = 1

Expand Down
110 changes: 110 additions & 0 deletions luascripts/wolfadmin/admin/censor.lua
@@ -0,0 +1,110 @@

-- WolfAdmin module for Wolfenstein: Enemy Territory servers.
-- Copyright (C) 2015-2019 Timo 'Timothy' Smit

-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- at your option any later version.

-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.

-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.

local mutes = wolfa_requireModule("admin.mutes")
local history = wolfa_requireModule("admin.history")

local players = wolfa_requireModule("players.players")

local events = wolfa_requireModule("util.events")
local settings = wolfa_requireModule("util.settings")

local toml = wolfa_requireLib("toml")

local censor = {}

local words = {}
local names = {}

function censor.filterMessage(message)
local censored = false

for _, word in ipairs(words) do
local occurrences

message, occurrences = string.gsub(message, word["pattern"], "*censor*")

censored = (censored or occurrences > 0) and true or false
end

return censored, message
end

function censor.punishClient(clientId)
if settings.get("g_censorMute") > 0 then
mutes.add(clientId, -1337, players.MUTE_CHAT + players.MUTE_VOICE, settings.get("g_censorMute"), "censor")

if settings.get("g_playerHistory") ~= 0 then
history.add(clientId, -1337, "mute", "censor")
end

et.trap_SendConsoleCommand(et.EXEC_APPEND, "cchat -1 \"^dmute: ^7"..players.getName(clientId).." ^9has been muted for "..settings.get("g_censorMute").." seconds\";")
end
end

function censor.load()
local fileName = settings.get("g_fileCensor")

if fileName == "" then
return 0
end

local fileDescriptor, fileLength = et.trap_FS_FOpenFile(fileName, et.FS_READ)

if fileLength == -1 then
return 0
end

-- in case someone issued a !readconfig, make sure the old data is removed
censor.clear()

local fileString = et.trap_FS_Read(fileDescriptor, fileLength)

et.trap_FS_FCloseFile(fileDescriptor)

local fileTable = toml.parse(fileString)

if fileTable["word"] then
for _, word in ipairs(fileTable["word"]) do
if word["pattern"] then
table.insert(words, word)
end
end
end

if fileTable["name"] then
for _, name in ipairs(fileTable["name"]) do
if name["pattern"] then
table.insert(names, name)
end
end
end

return #words + #names
end

function censor.clear()
words = {}
names = {}
end

function censor.oninit(levelTime, randomSeed, restartMap)
censor.load()
end
events.handle("onGameInit", censor.oninit)

return censor
5 changes: 3 additions & 2 deletions luascripts/wolfadmin/commands/admin/readconfig.lua
Expand Up @@ -15,6 +15,7 @@
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.

local censor = require (wolfa_getLuaPath()..".admin.censor")
local rules = require (wolfa_getLuaPath()..".admin.rules")

local auth = require (wolfa_getLuaPath()..".auth.auth")
Expand All @@ -41,12 +42,12 @@ commands.addadmin("readconfig", commandReadconfig, auth.PERM_READCONFIG, "reload

function commandReadconfig(clientId, command)
settings.load()
local censorCount = censor.load()
local rulesCount = rules.load()
local greetingsCount = greetings.load()
local spreesCount = sprees.load()

et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"readconfig: loaded "..greetingsCount.." greetings, "..rulesCount.." rules, "..spreesCount.." sprees\";")

et.trap_SendConsoleCommand(et.EXEC_APPEND, "csay "..clientId.." \"readconfig: loaded "..censorCount.." censor patterns, loaded "..greetingsCount.." greetings, "..rulesCount.." rules, "..spreesCount.." sprees\";")
return false
end
commands.addadmin("readconfig", commandReadconfig, auth.PERM_READCONFIG, "reloads the config file", nil, nil, (settings.get("g_standalone") == 0))
13 changes: 13 additions & 0 deletions luascripts/wolfadmin/commands/client/say.lua
Expand Up @@ -15,12 +15,15 @@
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.

local censor = require (wolfa_getLuaPath()..".admin.censor")

local commands = require (wolfa_getLuaPath()..".commands.commands")

local players = require (wolfa_getLuaPath()..".players.players")

local logs = require (wolfa_getLuaPath()..".util.logs")
local settings = require (wolfa_getLuaPath()..".util.settings")
local util = require (wolfa_getLuaPath()..".util.util")

local types = {
["say"] = "chat",
Expand All @@ -39,6 +42,16 @@ function commandSay(clientId, command, ...)
return true
end

local censored, message = censor.filterMessage(...)

if censored and settings.get("g_censorMode") ~= 0 then
censor.punishClient(clientId)

et.G_Say(clientId, util.getChatFromCommand(command), message)

return true
end

logs.writeChat(clientId, types[command], ...)
end
commands.addclient("say", commandSay, "", "", false, (settings.get("g_standalone") == 0))
Expand Down
2 changes: 2 additions & 0 deletions luascripts/wolfadmin/main.lua
Expand Up @@ -18,6 +18,7 @@
local admin
local balancer
local bans
local censor
local history
local mutes
local rules
Expand Down Expand Up @@ -90,6 +91,7 @@ function et_InitGame(levelTime, randomSeed, restartMap)
admin = require (wolfa_getLuaPath()..".admin.admin")
balancer = require (wolfa_getLuaPath()..".admin.balancer")
bans = require (wolfa_getLuaPath()..".admin.bans")
censor = require (wolfa_getLuaPath()..".admin.censor")
history = require (wolfa_getLuaPath()..".admin.history")
mutes = require (wolfa_getLuaPath()..".admin.mutes")
rules = require (wolfa_getLuaPath()..".admin.rules")
Expand Down
5 changes: 5 additions & 0 deletions luascripts/wolfadmin/util/constants.lua
Expand Up @@ -46,6 +46,11 @@ constants.TEAM_SPECTATORS_COLOR = "^2"
constants.TEAM_AXIS_COLOR_NAME = "red"
constants.TEAM_ALLIES_COLOR_NAME = "blue"

constants.SAY_ALL_CMD = "say"
constants.SAY_TEAM_CMD = "say_team"
constants.SAY_BUDDY_CMD = "say_buddy"
constants.SAY_TEAMNL_CMD = "say_teamnl"

constants.CLASS_SOLDIER = 0
constants.CLASS_MEDIC = 1
constants.CLASS_ENGINEER = 2
Expand Down
10 changes: 10 additions & 0 deletions luascripts/wolfadmin/util/settings.lua
Expand Up @@ -23,10 +23,14 @@ local settings = {}
local data = {
["g_logChat"] = "chat.log",
["g_logAdmin"] = "admin.log",
["g_fileCensor"] = "censor.toml",
["g_fileGreetings"] = "greetings.toml",
["g_fileRules"] = "rules.toml",
["g_fileSprees"] = "sprees.toml",
["g_playerHistory"] = 1,
["g_censorMode"] = 1,
["g_censorMute"] = 60,
["g_censorKick"] = 1,
["g_spreeMessages"] = 7,
["g_spreeRecords"] = 1,
["g_botRecords"] = 1,
Expand Down Expand Up @@ -86,6 +90,12 @@ local cfgStructure = {
["selection"] = "g_evenerPlayerSelection",
["interval"] = "g_evenerInterval"
},
["censor"] = {
["file"] = "g_fileCensor",
["mode"] = "g_censorMode",
["mute"] = "g_censorMute",
["kick"] = "g_censorKick"
},
["game"] = {
["announcerevives"] = "g_announceRevives"
},
Expand Down
14 changes: 14 additions & 0 deletions luascripts/wolfadmin/util/util.lua
Expand Up @@ -84,6 +84,20 @@ function util.getTeamFromCode(teamCode)
end
end

function util.getChatFromCommand(chatCommand)
if chatCommand == constants.SAY_ALL_CMD then
return et.SAY_ALL
elseif chatCommand == constants.SAY_TEAM_CMD then
return et.SAY_TEAM
elseif chatCommand == constants.SAY_BUDDY_CMD then
return et.SAY_BUDDY
elseif chatCommand == constants.SAY_TEAMNL_CMD then
return et.SAY_TEAMNL
else
return et.SAY_ALL
end
end

function util.getTeamColor(teamId)
if teamId == constants.TEAM_AXIS then
return constants.TEAM_AXIS_COLOR
Expand Down

0 comments on commit 4e917f7

Please sign in to comment.