-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.lua
272 lines (220 loc) · 7.98 KB
/
functions.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
------------------------------------------------------------------------------------------------------------------------
--define
automod = {}
local last_messages_a1 = {}
local last_messages_a2 = {}
local last_messages_b1 = {}
local last_messages_b2 = {}
local last_messages_b3 = {}
------------------------------------------------------------------------------------------------------------------------
--function that removes duplicated characters
local function remove_duplicates(s)
local result = ""
for i = 1, #s do
if s:sub(i, i) ~= s:sub(i + 1, i + 1) then
result = result .. s:sub(i, i)
end
end
return result
end
--function that removes spaces
local function remove_spaces(s)
local no_space = ""
no_space = s:gsub("%s+", "")
return no_space
end
--function that only keeps letters
local function remove_all(s)
local all_removed = ""
all_removed = s:gsub("[%W%d]", "")
return all_removed
end
--function that only keeps letters and spaces
local function remove_parts(s)
local parts_removed = ""
parts_removed = s:gsub("[^%a%s]", "")
return parts_removed
end
--function that replaces special character with spaces
local function replace_parts(s)
local parts_removed = ""
parts_removed = s:gsub("[%W%d_]", " ")
return parts_removed
end
------------------------------------------------------------------------------------------------------------------------
--removes all special characters, spaces so it is just pure plain text
--first if tests with duplicates, second if without
function automod.check_a1(message, name, blacklist)
if not last_messages_a1[name] then
last_messages_a1[name] = {}
end
message = remove_all(string.lower(message))
table.insert(last_messages_a1[name], message)
minetest.chat_send_all("A1: " .. message)
if #last_messages_a1[name] > 20 then
table.remove(last_messages_a1[name], 1)
end
local last_messages_str_a1 = table.concat(last_messages_a1[name])
for _, word in ipairs(blacklist) do
if string.find(last_messages_str_a1, word) then
last_messages_a1[name] = {}
return true
elseif string.find(remove_duplicates(last_messages_str_a1), word) then
last_messages_a1[name] = {}
return true
end
end
end
--removes all spaces but keep special characters
--first if tests with duplicates, second if without
function automod.check_a2(message, name, blacklist)
if not last_messages_a2[name] then
last_messages_a2[name] = {}
end
message = remove_spaces(string.lower(message))
table.insert(last_messages_a2[name], message)
minetest.chat_send_all("A2: " .. message)
if #last_messages_a2[name] > 20 then
table.remove(last_messages_a2[name], 1)
end
local last_messages_str_a2 = table.concat(last_messages_a2[name])
for _, word in ipairs(blacklist) do
if string.find(last_messages_str_a2, word) then
last_messages_a2[name] = {}
return true
elseif string.find(remove_duplicates(last_messages_str_a2), word) then
last_messages_a2[name] = {}
return true
end
end
end
--removes all special characters but keep spaces
--a space is added at the end of each message in the table
--first if tests with duplicates, second if without
function automod.check_b1(message, name, blacklist)
if not last_messages_b1[name] then
last_messages_b1[name] = {}
end
message = remove_parts(string.lower(message)) .. " "
table.insert(last_messages_b1[name], message)
minetest.chat_send_all("B1: " .. message)
if #last_messages_b1[name] > 20 then
table.remove(last_messages_b1[name], 1)
end
local last_messages_str_b1 = table.concat(last_messages_b1[name])
for _, word in ipairs(blacklist) do
if string.find(last_messages_str_b1, word) then
last_messages_b1[name] = {}
return true
elseif string.find(remove_duplicates(last_messages_str_b1), word) then
last_messages_b1[name] = {}
return true
end
end
end
--replace all special characters with spaces
--a space is added at the end of each message in the table
--first if tests with duplicates, second if without
function automod.check_b2(message, name, blacklist)
if not last_messages_b2[name] then
last_messages_b2[name] = {}
end
message = replace_parts(string.lower(message)) .. " "
table.insert(last_messages_b2[name], message)
minetest.chat_send_all("B2: " .. message)
if #last_messages_b2[name] > 20 then
table.remove(last_messages_b2[name], 1)
end
local last_messages_str_b2 = table.concat(last_messages_b2[name])
for _, word in ipairs(blacklist) do
if string.find(last_messages_str_b2, word) then
last_messages_b2[name] = {}
return true
elseif string.find(remove_duplicates(last_messages_str_b2), word) then
last_messages_b2[name] = {}
return true
end
end
end
--keep special characters and spaces but remove duplicates
--a space is added at the end of each message in the table
function automod.check_b3(message, name, blacklist)
if not last_messages_b3[name] then
last_messages_b3[name] = {}
end
message = remove_duplicates(string.lower(message)) .. " "
table.insert(last_messages_b3[name], message)
minetest.chat_send_all("B3: " .. message)
if #last_messages_b3[name] > 20 then
table.remove(last_messages_b3[name], 1)
end
local last_messages_str_b3 = table.concat(last_messages_b3[name])
for _, word in ipairs(blacklist) do
if string.find(last_messages_str_b3, word) then
last_messages_b3[name] = {}
return true
end
end
end
------------------------------------------------------------------------------------------------------------------------
--if "word" is in the blacklist, then "blahwordblah" will still trigger this
function automod.contains_word(target_message, blacklist)
local blacklist_check = remove_duplicates(string.lower(target_message))
for _, word in ipairs(blacklist) do
if string.find(blacklist_check, word) then
return true
end
end
return false
end
--if "word" is in the blacklist, then "blah word blah" or "word" will trigger this
--"blahwordblah" won't
function automod.contains_word2(target_message, blacklist)
local blacklist_check = remove_duplicates(string.lower(target_message))
local word_table = {}
for word in blacklist_check:gmatch("%w+") do
table.insert(word_table, word)
end
for _, word in ipairs(blacklist) do
for _, word_table in ipairs (word_table) do
if word == word_table then
return true
end
end
end
return false
end
--messages that contain all caps and more than 5 characters will trigger this
function automod.contains_caps(target_message)
if target_message == string.upper(target_message)
and string.len(target_message) > 5 then
return true
else
return false
end
end
--messages without a space and contain 15 or more characters will trigger this
function automod.contains_spam(target_message)
if string.len(target_message) > 20
and not string.find(target_message, ' ') then
return true
else
return false
end
end
--this censors the given message using the given blacklist
function automod.censor(message, blacklist)
message = string.lower(remove_duplicates(message))
for _, word in ipairs(blacklist) do
message = message:gsub(word, "*****")
end
return message
end
function automod.censor2(message, blacklist)
message = string.lower(remove_duplicates(message .. " "))
for _, word in ipairs(blacklist) do
message = message:gsub(word, "*****")
end
return message
end
------------------------------------------------------------------------------------------------------------------------