-
Notifications
You must be signed in to change notification settings - Fork 2
/
init.lua
247 lines (208 loc) · 6.05 KB
/
init.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
-- name_restrictions mod by ShadowNinja
-- License: WTFPL
----------------------------
-- Restriction exemptions --
----------------------------
-- For legitimate player names that are caught by the filters.
local exemptions = {}
local temp = minetest.setting_get("name_restrictions.exemptions")
temp = temp and temp:split() or {}
for _, allowed_name in pairs(temp) do
exemptions[allowed_name] = true
end
temp = nil
-- Exempt server owner
exemptions[minetest.setting_get("name")] = true
exemptions["singleplayer"] = true
local function player_exempted(name)
-- Allow specifically exempted players
if exemptions[name] then
return true
end
-- Allow players that already exist
local auth = minetest.get_auth_handler()
if auth.get_auth(name) then
return true
end
return false
end
---------------------
-- Simple matching --
---------------------
local msg_guest = "Guest accounts are disallowed on this server. "..
"Please choose a proper name and try again."
local msg_misleading = "Your player name is misleading. "..
"Please choose a more appropriate name."
local disallowed = {
["^guest[0-9]+"] = msg_guest,
["^squeakecrafter[0-9]+"] = msg_guest,
["adm[1il]n"] = msg_misleading,
["[0o]wn[e3]r"] = msg_misleading,
["^[0-9]+$"] = "All-numeric usernames are disallowed on this server.",
}
minetest.register_on_prejoinplayer(function(name, ip)
if player_exempted(name) then return end
-- Check for disallowed names
local lname = name:lower()
for re, reason in pairs(disallowed) do
if lname:find(re) then
return reason
end
end
end)
------------------------
-- Case-insensitivity --
------------------------
minetest.register_on_prejoinplayer(function(name, ip)
if player_exempted(name) then return end
-- Check for used names
local lname = name:lower()
for iname, data in pairs(minetest.auth_table) do
if iname:lower() == lname and iname ~= name then
return "Sorry, someone else is already using this"
.." name. Please pick another name."
.." Annother possibility is that you used the"
.." wrong case for your name."
end
end
end)
-- Compatability, for old servers with conflicting players
minetest.register_chatcommand("choosecase", {
description = "Choose the casing that a player name should have.",
params = "<name>",
privs = {server=true},
func = function(name, params)
local lname = params:lower()
local worldpath = minetest.get_worldpath()
for iname, data in pairs(minetest.auth_table) do
if iname:lower() == lname and iname ~= params then
minetest.auth_table[iname] = nil
assert(not iname:find("[/\\]"))
os.remove(worldpath.."/players/"..iname)
end
end
return true, "Done."
end,
})
------------------------
-- Anti-impersonation --
------------------------
-- Prevents names that are too similar to another player's name.
local similar_chars = {
-- Only A-Z, a-z, 1-9, dash, and underscore are allowed in player names
"A4",
"B8",
"COco0",
"Ee3",
"Gg69",
"ILil1",
"S5",
"Tt7",
"Zz2",
}
-- Map of characters to a regex of similar characters
local char_map = {}
for _, str in pairs(similar_chars) do
for c in str:gmatch(".") do
if not char_map[c] then
char_map[c] = str
else
char_map[c] = char_map[c] .. str
end
end
end
for c, str in pairs(char_map) do
char_map[c] = "[" .. char_map[c] .."]"
end
-- Characters to match for, containing all characters
local all_chars = "["
for _, str in pairs(similar_chars) do
all_chars = all_chars .. str
end
all_chars = all_chars .. "]"
minetest.register_on_prejoinplayer(function(name, ip)
if player_exempted(name) then return end
-- String off dashes and underscores from the start and end of the name.
local stripped_name = name:match("^[_-]*(.-)[_-]*$")
if not stripped_name or stripped_name == "" then
return "Your name is composed solely of whitespace-like characters."
end
-- Generate a regular expression to match all similar names
local re = stripped_name:gsub(all_chars, char_map)
re = "^[_-]*" .. re .. "[_-]*$"
for authName, _ in pairs(minetest.auth_table) do
if authName ~= name and authName:match(re) then
return "Your name is too similar to another player's name."
end
end
end)
-----------------
-- Name length --
-----------------
local min_name_len = tonumber(minetest.setting_get("name_restrictions.minimum_name_length")) or 3
minetest.register_on_prejoinplayer(function(name, ip)
if player_exempted(name) then return end
if #name < min_name_len then
return "Your player name is too short, please try a longer name."
end
end)
----------------------
-- Pronounceability --
----------------------
-- Original implementation (in Python) by sfan5
local function pronounceable(pronounceability, text)
local pronounceable = 0
local nonpronounceable = 0
local cn = 0
local lastc = ""
for c in text:lower():gmatch(".") do
if c:find("[aeiou0-9_-]") then
if not c:find("[0-9]") then
if cn > 2 then
nonpronounceable = nonpronounceable + 1
else
pronounceable = pronounceable + 1
end
end
cn = 0
else
if cn == 1 and lastc == c and lastc ~= 's' then
nonpronounceable = nonpronounceable + 1
cn = 0
end
if cn > 2 then
nonpronounceable = nonpronounceable + 1
cn = 0
end
if lastc:find("[aeiou]") then
pronounceable = pronounceable + 1
cn = 0
end
if not (c == "r" and lastc:find("[aipfom]")) and
not (lastc == "c" and c == "h") then
cn = cn + 1
end
end
lastc = c
end
if cn > 0 then
nonpronounceable = nonpronounceable + 1
end
return pronounceable * pronounceability >= nonpronounceable
end
-- Pronounceability factor:
-- nil = Checking disabled.
-- 0 = Everything's unpronounceable.
-- 0.5 = Strict checking.
-- 1 = Normal checking.
-- 2 = Relaxed checking.
local pronounceability = tonumber(minetest.setting_get("name_restrictions.pronounceability"))
if pronounceability then
minetest.register_on_prejoinplayer(function(name, ip)
if player_exempted(name) then return end
if not pronounceable(pronounceability, name) then
return "Your player name does not seem to be pronounceable."
.." Please choose a more pronounceable name."
end
end)
end