From e0f24649adce0fb74588bd89c71b2190df83180a Mon Sep 17 00:00:00 2001 From: Dorian Wouters Date: Tue, 23 Aug 2016 16:58:36 +0200 Subject: [PATCH] Add automatic auth.txt import upon auth table creation --- auth_txt_import.lua | 40 ++++++++++++++++++++++++++++++++++++++++ init.lua | 17 ++++++++++++++++- 2 files changed, 56 insertions(+), 1 deletion(-) create mode 100644 auth_txt_import.lua diff --git a/auth_txt_import.lua b/auth_txt_import.lua new file mode 100644 index 0000000..e90325a --- /dev/null +++ b/auth_txt_import.lua @@ -0,0 +1,40 @@ +local modname = minetest.get_current_modname() +local modpath = minetest.get_modpath(modname) + +local thismod = _G[modname] + +function thismod.import_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 file, errmsg = io.open(auth_file_path, 'rb') + if not file then + minetest.log("info", modname .. ": " .. auth_file_path .. " could not be opened for reading" .. + "(" .. errmsg .. "); no auth entries imported") + return + end + 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 + io.close(file) +end diff --git a/init.lua b/init.lua index 15f0ec9..1484a4b 100644 --- a/init.lua +++ b/init.lua @@ -88,6 +88,9 @@ do return nil end local parts = string_splitdots(name) + if not parts then + return cfg[name] + end local tbl = cfg[parts[1]] for n = 2, #parts do if tbl == nil then @@ -100,7 +103,7 @@ do end end - local conn + local conn, dbname do -- MySQL API backend mysql.config(get('db.api')) @@ -119,6 +122,7 @@ do connopts.options = connopts.options or {} connopts.options.MYSQL_OPT_RECONNECT = true conn = mysql.connect(connopts) + dbname = connopts.db thismod.conn = conn -- LuaPower's MySQL interface throws an error when the connection fails, no need to check if @@ -180,6 +184,7 @@ do end end + local auth_table_created do -- Auth table existence check and setup conn:query("SHOW TABLES LIKE '" .. tables.auths.name .. "'") local res = conn:store_result() @@ -197,6 +202,9 @@ do 'PRIMARY KEY (' .. S.userid .. '),' .. 'UNIQUE (' .. S.username .. ')' .. ')') + minetest.log('action', modname .. " created table '" .. dbname .. "." .. tables.auths.name .. + "'") + auth_table_created = true end end @@ -245,6 +253,13 @@ do ',' .. S.lastlogin .. ' FROM ' .. tables.auths.name thismod.enumerate_auths_query = enumerate_auths_query + if auth_table_created and get('import_auth_txt_on_table_create') ~= 'false' then + if not thismod.import_auth_txt then + dofile(modpath .. '/auth_txt_import.lua') + end + thismod.import_auth_txt() + end + thismod.auth_handler = { get_auth = function(name) assert(type(name) == 'string')