Snippets

Leslie Krause Auth Redux Database Import Script

You are viewing an old version of this snippet. View the current version.
Revised by Leslie Krause 94e3eff
#!/bin/awk -f

################################################################################
# Database Import Script for Auth Redux Mod
# ------------------------------------------
# This script will convert the specified 'auth.txt' file into a database format 
# required by the Auth Redux Mod. The output file will be generated in the same 
# world directory as the original 'auth.txt' file (which will be unchanged).
#
# EXAMPLE:
# awk -f convert.awk ~/.minetest/worlds/world/auth.txt
################################################################################

function error( msg ) {
	skipped++;
	print msg " at line " NR " in " FILENAME ". Skipping.";
}

BEGIN {
	FS = ":";
	OFS = ":";
	checked = 0;
	skipped = 0;

	# determine output file name from arguments

	output_file = ARGV[ 1 ]
	if( sub( /[-_A-Za-z0-9]+\.txt$/, "auth.db", output_file ) == 0 ) {
		# sanity check for nonstandard input file
		output_file = "auth.db";
	}
	print "Converting " ARGV[ 1 ] "...";

	# set default values for new database fields

	approved_addrs = "";
	oldlogin = -1;
	lifetime = 0;
	total_failures = 0;
	total_attempts = 0;
	total_sessions = 0;

	# print database headline to the output file

	print "auth_rx/2.1 @0" > output_file;
}

NF != 4 {
	error( "Malformed record" );
	next;
}

{
	username = $1;
	password = $2;
	assigned_privs = $3;
	newlogin = $4;

	if( !match( username, "^[a-zA-Z0-9_-]+$" ) ) {
		error( "Invalid username field" );
		next;
	}
	if( !match( newlogin, "^[0-9]+$" ) && newlogin != -1 ) {
		error( "Invalid last_login field" );
		next;
	}

	# Database File Format
	# --------------------
	# username
	# password
	# oldlogin
	# newlogin
	# lifetime
	# total_sessions
	# total_attempts
	# total_failures
	# approved_addrs
	# assigned_privs

	print username, password, oldlogin, newlogin, lifetime, total_sessions, total_attempts, total_failures, approved_addrs, assigned_privs > output_file;

	checked++;
}

END {
	print "Done! " checked " records were imported to " output_file " (" skipped " records skipped)."
}
HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.