From 6f500a53d812e76cf507487b348989f3cf0262c1 Mon Sep 17 00:00:00 2001 From: mckaygerhard Date: Thu, 10 Feb 2022 17:04:50 -0400 Subject: [PATCH] init empty files if there's no one present * provide a way to initialize files if there is not one currently doe snot touch the auth.txt file neither converted * solves: close: https://codeberg.org/minenux/minetest-mod-auth_rx/issues/6 * solved: close: https://bitbucket.org/sorcerykid/auth_rx/issues/7 * init the files when are fresh install, still do not convert from auth.txt --- db.lua | 34 ++++++++++++++++++++++++++++++++++ filter.lua | 8 +++++++- init.lua | 7 +++++-- 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/db.lua b/db.lua index 15f127a..7dd75a4 100644 --- a/db.lua +++ b/db.lua @@ -411,3 +411,37 @@ function AuthDatabase( path, name ) return self end + + +---------------------------- +-- AuthInitFile Class +---------------------------- + +function AuthInitFile( path, name) + + local file, err = io.open( path .. "/" .. name, "r+b" ) + + -- init empty files if not present + if file then + file:close( ) + return + end + file = io.open( path .. "/" .. name, "w+") + if not file then + minetest.log( "error", "Cannot init file " .. path .. "/" .. name .. " for writing." ) + error( "Fatal exception in InitFiles( ), aborting." ) + end + if name == "auth.db" then + file:write("auth_rx/2.1 @0") + AuthInitFile( path, name .. "x") + end + file:write("") + file:close( ) + + -- convert present txt file if present or autentications, after convert, change extension to auth.txt.converted + -- TODO: made a conversion in lua line by line, after rename , delete it + + -- provide a way to rollback to a txt file if there already a auth db file, to stay in sync + -- TODO way to get sync with txt file + +end diff --git a/filter.lua b/filter.lua index e353ada..71b8d0b 100644 --- a/filter.lua +++ b/filter.lua @@ -485,7 +485,13 @@ function AuthFilter( path, name, debug ) self.refresh = function ( ) local file = io.open( path .. "/" .. name, "r" ) if not file then - error( "The specified ruleset file does not exist." ) + file = io.open( path .. "/" .. name, "w+") + file:write("pass now") + if not file then + error( "The specified ruleset file does not exist." ) + end + file:close( file ) + file = io.open( path .. "/" .. name, "r" ) end src = { } for line in file:lines( ) do diff --git a/init.lua b/init.lua index 8fe7211..66a78ec 100644 --- a/init.lua +++ b/init.lua @@ -6,7 +6,8 @@ -------------------------------------------------------- local world_path = minetest.get_worldpath( ) -local mod_path = minetest.get_modpath( "auth_rx" ) +local mod_name = minetest.get_current_modname() or "auth_rx" +local mod_path = minetest.get_modpath( mod_name ) dofile( mod_path .. "/helpers.lua" ) dofile( mod_path .. "/filter.lua" ) @@ -14,10 +15,12 @@ dofile( mod_path .. "/db.lua" ) dofile( mod_path .. "/watchdog.lua" ) local __commands = dofile( mod_path .. "/commands.lua" ) +AuthInitFile( world_path, "auth.db" ) +AuthInitFile( world_path, "greenlist.mt" ) + ----------------------------------------------------- -- Registered Authentication Handler ----------------------------------------------------- - local auth_filter = AuthFilter( world_path, "greenlist.mt" ) local auth_db = AuthDatabase( world_path, "auth.db" ) local auth_watchdog = AuthWatchdog( )