-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathinit.lua
282 lines (227 loc) · 7.67 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
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
273
274
275
276
277
278
279
280
281
282
-- (C) 2016 Tai "DuCake" Kedzierski
-- This code is conveyed to you under the terms
-- of the GNU Lesser General Public License v3.0
-- You should have received a copy of the license
-- in a LICENSE.txt file
-- If not, please see https://www.gnu.org/licenses/lgpl-3.0.html
babel = {}
local modpath = minetest.get_modpath("babelfish")
dofile(modpath.."/chat.lua" )
dofile(modpath.."/utilities.lua" )
dofile(modpath.."/persistence.lua" )
local langprefs = minetest.get_worldpath().."/babel_langprefs"
local engine = minetest.setting_get("babelfish.engine") or "yandex"
babel.key = minetest.setting_get("babelfish.key")
babel.defaultlang = minetest.setting_get("babelfish.defaultlang") or "en"
minetest.register_privilege("babelmoderator")
local chat_history = {}
local player_pref_language = {}
-- ===== SECURITY ======
local ie = minetest.request_insecure_environment()
if not ie then
error("Could not get secure environment. Add babelfish to secure.trusted_mods ")
end
local oldrequire = require
require = ie.require -- override require so that system libraries being loaded can benefit
if not babel.key then engine = "none" end
dofile(modpath.."/"..engine.."_engine.lua")
local httpapitable = minetest.request_http_api()
babel.register_http(httpapitable)
require = oldrequire -- restore the sandbox's require
-- =====================
local function prefsave()
local serdata = minetest.serialize(player_pref_language)
if not serdata then
minetest.log("error", "[babelfish] Data serialization failed")
return
end
local file, err = io.open(langprefs, "w")
if err then
return err
end
file:write(serdata)
file:close()
end
local function prefload()
local file, err = io.open(langprefs, "r")
if err then
minetest.log("error", "[babelfish] Data read failed")
return
end
player_pref_language = minetest.deserialize(file:read("*a")) or {}
file:close()
end
-- ========================== Language engine and overridable validation
function babel.validate_lang(self, langstring)
for target, langname in pairs(babel.langcodes) do
if target == langstring then
return true
end
end
return tostring(langstring).." is not a recognized language"
end
-- =====================================================================/
minetest.register_on_chat_message(function(player, message)
chat_history[player] = message
end)
local function components(mystring)
local iter = mystring:gmatch("%S+")
local targetlang = iter() or ""
local targetphrase = mystring:gsub("^"..targetlang.." ", "")
return targetlang, targetphrase
end
local function validate_player(playername)
if minetest.get_player_by_name(playername) then
return true
end
return false
end
local function dotranslate(lang, phrase, handler)
return babel:translate(phrase, lang, handler)
end
-- Shortcut translation
-- Send a message like "Hello everyone ! %fr"
-- The message is broadcast in original form, then in French
minetest.register_on_chat_message(function(player, message)
if not minetest.check_player_privs(player, {shout = true}) then
return
end
-- Search for "%" token
local n,m = message:find("%%..")
local targetlang = nil
local targetphrase = message
if n then
local sfront = message:sub(1, n-1)
local sback = message:sub(m+1, message:len() )
targetlang = message:sub(n+1, m) -- Removes '%' token
targetphrase = message:gsub("%%"..targetlang,'',1)
end
if not targetlang then
return false
end
-- True, or error string
local validation = babel:validate_lang(targetlang)
if validation ~= true then
babel.chat_send_player(player, validation)
else
dotranslate(targetlang, targetphrase, function(newphrase)
babel.chat_send_all("["..babel.engine.." "..player.."]: "..newphrase)
minetest.log("action", player.." CHAT ["..babel.engine.."]: "..newphrase)
end)
end
end)
local function f_babel(player, argstring)
local targetplayer = argstring
if not player_pref_language[player] then
player_pref_language[player] = babel.defaultlang
end
local targetlang = player_pref_language[player]
local validation = babel:validate_lang(targetlang)
if validation ~= true then
babel.chat_send_player(player, validation)
return
end
if not chat_history[targetplayer] then
babel.chat_send_player(player, targetplayer.." has not said anything")
return
end
dotranslate(targetlang, chat_history[targetplayer], function(newphrase)
babel.chat_send_player(player, "["..babel.engine.."]: "..newphrase)
end)
end
local function f_babelshout(player, argstring)
-- babel equivalent of shout - broadcast translated message
local targetlang, targetphrase = components(argstring)
local validation = babel:validate_lang(targetlang)
if validation ~= true then
babel.chat_send_player(player, validation)
return
end
dotranslate(targetlang, targetphrase, function(newphrase)
babel.chat_send_all("["..babel.engine.." "..player.."]: "..newphrase)
minetest.log("action", player.." CHAT ["..babel.engine.."]: "..newphrase)
end)
end
local function f_babelmsg(player, argstring)
-- babel equivalent of private message
local targetplayer, targetphrase = components(argstring)
local targetlang = player_pref_language[targetplayer]
local validation = babel:validate_lang(targetlang)
if validation ~= true then
babel.chat_send_player(player, validation)
return
end
if not validate_player(targetplayer) then
babel.chat_send_player(player, targetplayer.." is not a connected player")
return
end
dotranslate(targetlang, targetphrase, function(newphrase)
babel.chat_send_player(targetplayer, "["..babel.engine.." PM from "..player.."]: "..newphrase)
minetest.log("action", player.." PM to "..targetplayer.." ["..babel.engine.."]: "..newphrase)
end)
end
local function setplayerlanguage(tplayer, langcode)
if minetest.get_player_by_name(tplayer) then
player_pref_language[tplayer] = langcode
prefsave()
end
end
local function getplayerlanguage(tplayer)
return player_pref_language[tplayer]
end
minetest.register_chatcommand("bblang", {
description = "Set your preferred language",
func = function(player,args)
local validation = babel:validate_lang(args)
if validation ~= true then
babel.chat_send_player(player, validation)
return
else
setplayerlanguage(player, args) -- FIXME this should use the above pref functions
babel.chat_send_player(player, args.." : OK" )
end
end
})
minetest.register_chatcommand("bbcodes", {
description = "List the available language codes",
func = function(player,command)
minetest.chat_send_player(player,dump(babel.langcodes))
end
})
minetest.register_chatcommand("babel", {
description = "Translate a player's last chat message. Use /bblang to set your language",
params = "<playername>",
func = f_babel
})
minetest.register_chatcommand("bb", {
description = "Translate a sentence and transmit it to everybody",
params = "<lang-code> <sentence>",
func = f_babelshout,
privs = {shout = true},
})
minetest.register_chatcommand("bmsg", {
description = "Send a private message to a player, in their preferred language",
params = "<player> <sentence>",
privs = {shout = true},
func = f_babelmsg
})
-- Admin commands
minetest.register_chatcommand("bbset", {
description = "Set a player's preferred language (if they do not know how)",
params = "<player> <language-code>",
privs = {babelmoderator = true},
func = function(player, message)
local tplayer, langcode = components(message)
setplayerlanguage(tplayer, langcode)
end,
})
-- Set player's default language
minetest.register_on_joinplayer(function(player, ip)
local playername = player:get_player_name()
if not getplayerlanguage(playername) then
setplayerlanguage(playername, babel.defaultlang)
end
end)
-- Display help string, and compliance if set
dofile(minetest.get_modpath("babelfish").."/compliance.lua")
prefload()