Miroir du dépôt mysql_auth.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.8KB

  1. local modname = minetest.get_current_modname()
  2. local thismod = _G[modname]
  3. function thismod.import_auth_txt()
  4. minetest.log('action', modname .. ": Importing auth.txt")
  5. local auth_file_path = minetest.get_worldpath() .. '/auth.txt'
  6. local create_auth_stmt = thismod.create_auth_stmt
  7. local create_auth_params = thismod.create_auth_params
  8. local conn = mysql_base.conn
  9. local file, errmsg = io.open(auth_file_path, 'rb')
  10. if not file then
  11. minetest.log('action', modname .. ": " .. auth_file_path .. " could not be opened for reading" ..
  12. "(" .. errmsg .. "); no auth entries imported")
  13. return
  14. end
  15. conn:query('SET autocommit=0')
  16. conn:query('START TRANSACTION')
  17. for line in file:lines() do
  18. if line ~= "" then
  19. local fields = line:split(":", true)
  20. local name, password, privilege_string, last_login = unpack(fields)
  21. last_login = tonumber(last_login)
  22. if not (name and password and privilege_string) then
  23. minetest.log('warning', modname .. ": Invalid line in auth.txt, skipped: " .. dump(line))
  24. end
  25. minetest.log('info', modname .. " importing player '"..name.."'")
  26. create_auth_params:set(1, name)
  27. create_auth_params:set(2, password)
  28. create_auth_params:set(3, privilege_string)
  29. create_auth_params:set(4, last_login)
  30. local success, msg = pcall(create_auth_stmt.exec, create_auth_stmt)
  31. if not success then
  32. error(modname .. ": import failed: " .. msg)
  33. end
  34. if create_auth_stmt:affected_rows() ~= 1 then
  35. error(modname .. ": create_auth failed: affected row count is " ..
  36. create_auth_stmt:affected_rows() .. ", expected 1")
  37. end
  38. end
  39. end
  40. conn:query('COMMIT')
  41. conn:query('SET autocommit=1')
  42. io.close(file)
  43. minetest.log('action', modname .. ": Finished importing auth.txt")
  44. end