This repository has been archived by the owner on Aug 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_txt_import.lua
45 lines (44 loc) · 1.79 KB
/
auth_txt_import.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
local modname = minetest.get_current_modname()
local thismod = _G[modname]
function thismod.import_auth_txt()
minetest.log('action', modname .. ": Importing auth.txt")
local auth_file_path = minetest.get_worldpath() .. '/auth.txt'
local create_auth_stmt = thismod.create_auth_stmt
local create_auth_params = thismod.create_auth_params
local conn = mysql_base.conn
local file, errmsg = io.open(auth_file_path, 'rb')
if not file then
minetest.log('action', modname .. ": " .. auth_file_path .. " could not be opened for reading" ..
"(" .. errmsg .. "); no auth entries imported")
return
end
conn:query('SET autocommit=0')
conn:query('START TRANSACTION')
for line in file:lines() do
if line ~= "" then
local fields = line:split(":", true)
local name, password, privilege_string, last_login = unpack(fields)
last_login = tonumber(last_login)
if not (name and password and privilege_string) then
minetest.log('warning', modname .. ": Invalid line in auth.txt, skipped: " .. dump(line))
end
minetest.log('info', modname .. " importing player '"..name.."'")
create_auth_params:set(1, name)
create_auth_params:set(2, password)
create_auth_params:set(3, privilege_string)
create_auth_params:set(4, last_login)
local success, msg = pcall(create_auth_stmt.exec, create_auth_stmt)
if not success then
error(modname .. ": import failed: " .. msg)
end
if create_auth_stmt:affected_rows() ~= 1 then
error(modname .. ": create_auth failed: affected row count is " ..
create_auth_stmt:affected_rows() .. ", expected 1")
end
end
end
conn:query('COMMIT')
conn:query('SET autocommit=1')
io.close(file)
minetest.log('action', modname .. ": Finished importing auth.txt")
end