Skip to content

Commit

Permalink
Initial implementation of spam check
Browse files Browse the repository at this point in the history
It checks during the sends if there are spam warnings then give to the message an attribute spam=true (only for receivers)
  • Loading branch information
Athozus committed Feb 1, 2024
1 parent 75510d2 commit bfe0ef2
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions api.lua
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ function mail.send(m)
local entry = mail.get_storage_entry(m.from)
table.insert(entry.outbox, 1, msg)
mail.set_storage_entry(m.from, entry)
msg.spam = #mail.check_spam(msg) >= 1

-- add in every receivers inbox
for recipient in pairs(recipients) do
Expand Down
3 changes: 3 additions & 0 deletions ui/inbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ function mail.show_inbox(name, sortfieldindex, sortdirection, filter)
if not mail.player_in_list(name, message.to) and cc_color_enable then
table.insert(displayed_color, "additional")
end
if message.spam then
table.insert(displayed_color, "warning")
end
formspec[#formspec + 1] = "," .. mail.get_color(displayed_color)
formspec[#formspec + 1] = ","
formspec[#formspec + 1] = minetest.formspec_escape(message.from)
Expand Down
1 change: 1 addition & 0 deletions util/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ dofile(MP .. "/util/normalize.lua")
dofile(MP .. "/util/colors.lua")
dofile(MP .. "/util/contact.lua")
dofile(MP .. "/util/settings.lua")
dofile(MP .. "/util/spam.lua")
dofile(MP .. "/util/time_ago.lua")
dofile(MP .. "/util/uuid.lua")
39 changes: 39 additions & 0 deletions util/spam.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
local function caps_ratio(str)
local total_caps = 0
for i = 1, #str do -- iteration through each character
local c = str:sub(i,i)
if string.lower(c) ~= c then -- do not count digits as spam
total_caps = total_caps + 1
end
end
return total_caps/(#str or 1) -- avoid division by zero
end

local function words_ratio(str, ratio)
local words = {}
local split_str = str:split(" ")
for _, w in ipairs(split_str) do
if not words[w] then
words[w] = 0
else
words[w] = (words[w] or 0) + 1
end
end
for _, n in pairs(words) do
if n/#split_str >= ratio then
return true
end
end
return false
end

function mail.check_spam(message)
spam_checks = {}
if caps_ratio(message.subject) == 1 or caps_ratio(message.body) > 0.4 then
table.insert(spam_checks, "caps")
end
if words_ratio(message.subject, 0.6) or words_ratio(message.body, 0.2) then
table.insert(spam_checks, "words")
end
return spam_checks
end

0 comments on commit bfe0ef2

Please sign in to comment.