Add on_violation callback

master
rubenwardy 2018-02-02 19:16:48 +00:00 committed by sofar
parent 1c7f9bf23a
commit dae7d9939e
2 changed files with 47 additions and 17 deletions

View File

@ -25,7 +25,7 @@
]]--
filter = {}
filter = { registered_on_violations = {} }
local words = {}
local muted = {}
local violations = {}
@ -57,6 +57,10 @@ function filter.import_file(filepath)
end
end
function filter.register_on_violation(func)
table.insert(filter.registered_on_violations, func)
end
function filter.check_message(name, message)
for _, w in ipairs(words) do
if string.find(message:lower(), "%f[%a]" .. w .. "%f[%A]") then
@ -111,15 +115,23 @@ function filter.on_violation(name, message)
local resolution
if violations[name] == 1 and minetest.get_player_by_name(name) then
resolution = "warned"
filter.show_warning_formspec(name)
elseif violations[name] <= 3 then
resolution = "muted"
filter.mute(name, 1)
else
resolution = "kicked"
minetest.kick_player(name, "Please mind your language!")
for _, cb in pairs(filter.registered_on_violations) do
if cb(name, message, violations) then
resolution = "custom"
end
end
if not resolution then
if violations[name] == 1 and minetest.get_player_by_name(name) then
resolution = "warned"
filter.show_warning_formspec(name)
elseif violations[name] <= 3 then
resolution = "muted"
filter.mute(name, 1)
else
resolution = "kicked"
minetest.kick_player(name, "Please mind your language!")
end
end
minetest.log("action", "VIOLATION (" .. resolution .. "): <" .. name .. "> ".. message)
@ -145,9 +157,9 @@ local function step()
violations[name] = nil
end
end
minetest.after(2*60, step)
minetest.after(10*60, step)
end
minetest.after(2*60, step)
minetest.after(10*60, step)
minetest.register_chatcommand("filter", {
params = "filter server",

View File

@ -1,5 +1,4 @@
## filter mod
# filter mod
This mod adds a simple chat filter. There is no default word list,
and adding words to the filter list is done through the `/filter`
@ -14,7 +13,26 @@ muted for 1 minute. After that, their `shout` privilege is restored.
If they leave, their `shout` privilege is still restored, but only after
the time expires, not before.
## Bugs
## API
- the `shout` priv is permanently lost if the server shuts down while
a player is muted.
### Callbacks
* filter.register_on_violation(func(name, message, violations))
* Violations is the value of the player's violation counter - which is
incremented on a violation, and halved every 10 minutes.
* Return true if you've handled the violation. No more callbacks will be
executation, and the default behaviour (warning/mute/kick) on violation
will be skipped.
### Methods
* filter.import_file(path)
* Input bad words from a file (`path`) where each line is a new word.
* filter.check_message(name, message)
* Checks message for violation. Returns true if okay, false if bad.
If it returns false, you should cancel the sending of the message and
call filter.on_violation()
* filter.on_violation(name, message)
* Increments violation count, runs callbacks, and punishes the players.
* filter.mute(name, duration)
* filter.show_warning_formspec(name)