2017-11-05 07:37:49 +03:00
|
|
|
|
|
|
|
--[[
|
|
|
|
|
2018-02-02 22:08:04 +03:00
|
|
|
Copyright 2017-8 Auke Kok <sofar@foo-projects.org>
|
|
|
|
Copyright 2018 rubenwardy <rw@rubenwardy.com>
|
2017-11-05 07:37:49 +03:00
|
|
|
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
a copy of this software and associated documentation files (the
|
|
|
|
"Software"), to deal in the Software without restriction, including
|
|
|
|
without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
permit persons to whom the Software is furnished to do so, subject
|
|
|
|
to the following conditions:
|
|
|
|
|
|
|
|
The above copyright notice and this permission notice shall be included
|
|
|
|
in all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
|
|
|
|
KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
|
|
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
|
|
|
|
]]--
|
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
filter = {}
|
|
|
|
local words = {}
|
|
|
|
local muted = {}
|
|
|
|
local violations = {}
|
2017-11-05 07:37:49 +03:00
|
|
|
local s = minetest.get_mod_storage()
|
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
function filter.init()
|
|
|
|
local sw = s:get_string("words")
|
|
|
|
if sw and sw ~= "" then
|
|
|
|
words = minetest.parse_json(sw)
|
|
|
|
end
|
2017-11-05 07:37:49 +03:00
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
if #words == 0 then
|
|
|
|
filter.import_file(minetest.get_modpath("filter") .. "/words.txt")
|
|
|
|
end
|
2017-11-05 07:37:49 +03:00
|
|
|
end
|
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
function filter.import_file(filepath)
|
|
|
|
local file = io.open(filepath, "r")
|
|
|
|
if file then
|
|
|
|
for line in file:lines() do
|
|
|
|
line = line:trim()
|
|
|
|
if line ~= "" then
|
|
|
|
words[#words + 1] = line:trim()
|
|
|
|
end
|
|
|
|
end
|
2017-11-05 07:37:49 +03:00
|
|
|
return true
|
2018-02-02 20:07:41 +03:00
|
|
|
else
|
|
|
|
return false
|
2017-11-05 07:37:49 +03:00
|
|
|
end
|
2018-02-02 20:07:41 +03:00
|
|
|
end
|
2017-11-05 07:37:49 +03:00
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
function filter.check_message(name, message)
|
2017-11-05 07:37:49 +03:00
|
|
|
for _, w in ipairs(words) do
|
2018-02-02 20:07:41 +03:00
|
|
|
if string.find(message:lower(), "%f[%a]" .. w .. "%f[%A]") then
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
|
2018-02-02 21:23:04 +03:00
|
|
|
function filter.mute(name, duration)
|
|
|
|
do
|
|
|
|
local privs = minetest.get_player_privs(name)
|
|
|
|
privs.shout = nil
|
|
|
|
minetest.set_player_privs(name, privs)
|
|
|
|
end
|
|
|
|
minetest.chat_send_player(name, "Watch your language! You have been temporarily muted")
|
|
|
|
|
|
|
|
muted[name] = true
|
|
|
|
|
|
|
|
minetest.after(duration * 60, function()
|
|
|
|
muted[name] = nil
|
|
|
|
minetest.chat_send_player(name, "Chat privilege reinstated. Please do not abuse chat.")
|
|
|
|
|
|
|
|
local privs = minetest.get_player_privs(name)
|
|
|
|
privs.shout = true
|
|
|
|
minetest.set_player_privs(name, privs)
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2018-02-02 22:08:04 +03:00
|
|
|
function filter.show_warning_formspec(name)
|
|
|
|
local formspec = "size[7,3]bgcolor[#080808BB;true]" .. default.gui_bg .. default.gui_bg_img .. [[
|
|
|
|
image[0,0;2,2;filter_warning.png]
|
|
|
|
label[2.3,0.5;Please watch your language!]
|
|
|
|
]]
|
|
|
|
|
|
|
|
if minetest.global_exists("rules") and rules.show then
|
|
|
|
formspec = formspec .. [[
|
|
|
|
button[0.5,2.1;3,1;rules;Show Rules]
|
|
|
|
button_exit[3.5,2.1;3,1;close;Okay]
|
|
|
|
]]
|
|
|
|
else
|
|
|
|
formspec = formspec .. [[
|
|
|
|
button_exit[2,2.1;3,1;close;Okay]
|
|
|
|
]]
|
|
|
|
end
|
|
|
|
minetest.show_formspec(name, "filter:warning", formspec)
|
|
|
|
end
|
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
function filter.on_violation(name, message)
|
|
|
|
violations[name] = (violations[name] or 0) + 1
|
|
|
|
|
|
|
|
local resolution
|
|
|
|
|
2018-02-02 22:08:04 +03:00
|
|
|
if violations[name] == 1 and minetest.get_player_by_name(name) then
|
|
|
|
resolution = "warned"
|
|
|
|
filter.show_warning_formspec(name)
|
|
|
|
elseif violations[name] <= 3 then
|
2018-02-02 20:07:41 +03:00
|
|
|
resolution = "muted"
|
2018-02-02 21:23:04 +03:00
|
|
|
filter.mute(name, 1)
|
2018-02-02 22:08:04 +03:00
|
|
|
else
|
|
|
|
resolution = "kicked"
|
|
|
|
minetest.kick_player(name, "Please mind your language!")
|
2018-02-02 20:07:41 +03:00
|
|
|
end
|
2017-11-05 07:37:49 +03:00
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
minetest.log("action", "VIOLATION (" .. resolution .. "): <" .. name .. "> ".. message)
|
|
|
|
end
|
2017-11-05 07:37:49 +03:00
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
table.insert(minetest.registered_on_chat_messages, 1, function(name, message)
|
|
|
|
local privs = minetest.get_player_privs(name)
|
|
|
|
if not privs.shout and muted[name] then
|
|
|
|
minetest.chat_send_player(name, "You are temporarily muted.")
|
|
|
|
return true
|
2017-11-05 07:37:49 +03:00
|
|
|
end
|
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
if not filter.check_message(name, message) then
|
|
|
|
filter.on_violation(name, message)
|
|
|
|
return true
|
|
|
|
end
|
2017-11-05 07:37:49 +03:00
|
|
|
end)
|
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
local function step()
|
|
|
|
for name, v in pairs(violations) do
|
|
|
|
violations[name] = math.floor(v * 0.5)
|
|
|
|
if violations[name] < 1 then
|
|
|
|
violations[name] = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
minetest.after(2*60, step)
|
|
|
|
end
|
|
|
|
minetest.after(2*60, step)
|
|
|
|
|
2017-11-05 07:37:49 +03:00
|
|
|
minetest.register_chatcommand("filter", {
|
|
|
|
params = "filter server",
|
|
|
|
description = "manage swear word filter",
|
|
|
|
privs = {server = true},
|
|
|
|
func = function(name, param)
|
|
|
|
local cmd, val = param:match("(%w+) (.+)")
|
|
|
|
if param == "list" then
|
2018-02-02 20:07:41 +03:00
|
|
|
return true, #words .. " words: " .. table.concat(words, ", ")
|
2017-11-05 07:37:49 +03:00
|
|
|
elseif cmd == "add" then
|
|
|
|
table.insert(words, val)
|
|
|
|
s:set_string("words", minetest.write_json(words))
|
|
|
|
return true, "Added \"" .. val .. "\"."
|
|
|
|
elseif cmd == "remove" then
|
|
|
|
for i, w in ipairs(words) do
|
|
|
|
if w == val then
|
|
|
|
table.remove(words, i)
|
|
|
|
s:set_string("words", minetest.write_json(words))
|
|
|
|
return true, "Removed \"" .. val .. "\"."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return true, "\"" .. val .. "\" not found in list."
|
|
|
|
else
|
2018-02-02 20:07:41 +03:00
|
|
|
return true, "I know " .. #words .. " words.\nUsage: /filter <add|remove|list> [<word>]"
|
2017-11-05 07:37:49 +03:00
|
|
|
end
|
|
|
|
end,
|
|
|
|
})
|
2018-02-02 20:07:41 +03:00
|
|
|
|
2018-02-02 22:08:04 +03:00
|
|
|
if minetest.global_exists("rules") and rules.show then
|
|
|
|
minetest.register_on_player_receive_fields(function(player, formname, fields)
|
|
|
|
if formname == "filter:warning" and fields.rules then
|
|
|
|
rules.show(player)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2018-02-02 20:07:41 +03:00
|
|
|
filter.init()
|