Загрузить файлы в «util»

This commit is contained in:
Nomad Senaxsys 2024-09-28 22:41:02 +03:00
parent 5c832dfc33
commit 6fd7d9a5c3
5 changed files with 197 additions and 0 deletions

113
util/settings.lua Normal file
View File

@ -0,0 +1,113 @@
-- translation
local S = mail.S
mail.settings = {
chat_notifications = {
type = "bool", default = true, group = "notifications", index = 1,
label = S("Chat notifications"), tooltip = S("Receive a message in the chat when there is a new message")
},
onjoin_notifications = {
type = "bool", default = true, group = "notifications", index = 2,
label = S("On join notifications"), tooltip = S("Receive a message at login when inbox isn't empty") },
hud_notifications = {
type = "bool", default = true, group = "notifications", index = 3,
label = S("HUD notifications"), tooltip = S("Show an HUD notification when inbox isn't empty")
},
sound_notifications = {
type = "bool", default = true, group = "notifications", index = 4,
label = S("Sound notifications"), tooltip = S("Play a sound when there is a new message")
},
unreadcolorenable = {
type = "bool", default = true, group = "message_list", index = 1,
label = S("Show unread in different color")
},
cccolorenable = {
type = "bool", default = true, group = "message_list", index = 2,
label = S("Show CC/BCC in different color")
},
defaultsortfield = {
type = "index", default = 3, group = "box_fields", index = 1,
label = S("Default sorting field"), dataset = { S("From/To"), S("Subject"), S("Date") }
},
defaultsortdirection = {
type = "index", default = 1, group = "box_fields", index = 2,
label = S("Default sorting direction"), dataset = { S("Ascending"), S("Descending") }
},
trash_move_enable = {
type = "bool", default = true, group = "other", index = 1,
label = S("Move deleted messages to trash")
},
auto_marking_read = {
type = "bool", default = true, group = "other", index = 2,
label = S("Automatic marking read"), tooltip = S("Mark a message as read when opened")
},
date_format = {
type = "string", default = "%Y-%m-%d %X", group = "date_and_time", index = 3, label = S("Date format"),
dataset = {"%Y-%m-%d %X", "%d/%m/%y %X", "%A %d %B %Y %X"}, format = os.date
},
timezone_offset = {
type = "number", default = 0, group = "date_and_time", index = 4,
label = S("Timezone offset"), tooltip = S("Offset to add to server time."),
},
mute_list = {
type = "list", default = {}, group = "spam", index = 1,
label = S("Mute list")
},
}
mail.settings_groups = {
{ name = "notifications", label = S("Notifications"), index = 1, parent = 0},
{ name = "message_list", label = S("Message list"), index = 2, parent = 0},
{ name = "box_fields", label = S("Fields"), index = 1, parent = "message_list"},
{ name = "spam", label = S("Spam"), index = 3, parent = 0},
{ name = "other", label = S("Other"), index = 4, parent = 0},
{ name = "date_and_time", label = S("Date and Time"), index = 1, parent = "other"}
}
for s, d in pairs(mail.settings) do
mail.selected_idxs[s] = {}
if d.type == "list" then
mail.selected_idxs["index_" .. s] = {}
end
end
function mail.settings.mute_list.check(name, value)
local valid_players = {}
for _, p in ipairs(value) do
if p ~= name and minetest.player_exists(p) then
table.insert(valid_players, p)
end
end
return valid_players
end
function mail.settings.mute_list.sync(name)
if minetest.get_modpath("beerchat") then
local players = {}
for other_player, _ in minetest.get_auth_handler().iterate() do
if beerchat.has_player_muted_player(name, other_player) then
table.insert(players, other_player)
end
end
return players
end
return nil
end
function mail.settings.mute_list.transfer(name, value)
if minetest.get_modpath("beerchat") then
for other_player, _ in minetest.get_auth_handler().iterate() do -- unmute all
if not beerchat.execute_callbacks("before_mute", name, other_player) then
return false
end
minetest.get_player_by_name(name):get_meta():set_string(
"beerchat:muted:" .. other_player, "")
end
for _, other_player in ipairs(value) do -- then mute only players in table
minetest.get_player_by_name(name):get_meta():set_string(
"beerchat:muted:" .. other_player, "true")
end
return true
end
return nil
end

39
util/spam.lua Normal file
View File

@ -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)
local 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

29
util/time_ago.lua Normal file
View File

@ -0,0 +1,29 @@
-- translation
local S = mail.S
function mail.time_ago(t)
local elapsed = os.time() - t
local str = ""
local time_units = {
{ S("years"), 31536000 },
{ S("months"), 2592000 },
{ S("weeks"), 604800 },
{ S("days"), 86400 },
{ S("hours"), 3600 },
{ S("minuts"), 60 },
{ S("seconds"), 1 },
}
for _, u in ipairs(time_units) do
local n = math.modf(elapsed/u[2])
if n > 0 then
str = str .. " " .. n .. " " .. u[1]
elapsed = elapsed - n * u[2]
end
end
str = string.sub(str, 2, -1)
return S("@1 ago", str)
end

9
util/uuid.lua Normal file
View File

@ -0,0 +1,9 @@
-- source: https://gist.github.com/jrus/3197011
local random = math.random
function mail.new_uuid()
local template ='xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'
return string.gsub(template, '[xy]', function (c)
local v = (c == 'x') and random(0, 0xf) or random(8, 0xb)
return string.format('%x', v)
end)
end

7
util/uuid.spec.lua Normal file
View File

@ -0,0 +1,7 @@
mtt.register("uuid", function(callback)
assert(mail.new_uuid())
assert(mail.new_uuid() ~= mail.new_uuid())
callback()
end)