From dae7d9939ebecc3542bd2bad5e1542d714b26800 Mon Sep 17 00:00:00 2001 From: rubenwardy Date: Fri, 2 Feb 2018 19:16:48 +0000 Subject: [PATCH] Add on_violation callback --- init.lua | 36 ++++++++++++++++++++++++------------ readme.md | 28 +++++++++++++++++++++++----- 2 files changed, 47 insertions(+), 17 deletions(-) diff --git a/init.lua b/init.lua index 8c29e1a..68d41e3 100644 --- a/init.lua +++ b/init.lua @@ -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", diff --git a/readme.md b/readme.md index 3b2c979..27293e2 100644 --- a/readme.md +++ b/readme.md @@ -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)