//IRC-style register and identify commands
//prerequisites: server-side scripting and sqlite support
//Creator: Enzo - find me on quakenet in #sauerbraten
// release 10/10/2012
Identifying results in unspec, so this works best in locked mastermode.
//IRC-style register and identify commands
//prerequisites: server-side scripting and sqlite support
//Creator: Enzo - find me on quakenet in #sauerbraten
// release 10/10/2012
defaultvalue "user_db" "default" // database for use if empty dont use databse
users_table = "users" // hardcoded table name with authkeys
sanitize__string = [
//this doesn't really work for some reason
feedback = (format "sanitizing %1" $arg2)
echo $feedback
s = $arg2
s = (strreplace $s "^"" "")
s = (strreplace $s "'" "")
result $s
]
user__exist = [
db_init $user_db
nick = $arg1 //for reasons I can't fathom, sanitize_string screws up the nick
//feedback = (format "user__exist: postsanitization testing if user exists arg1=%1 nick=%2" $arg1 $nick)
//echo $feedback
query = (format "SELECT nick FROM %1 WHERE nick=':0'" $users_table)
qh = (db_pquery $query $nick $user_db)
res = ""
// check for errors
if (= $qh -1) [
db_error $user_db
pm $arg1 "user_exist: Database error"
res = 0
] [
res = (db_getrow $qh $user_db)
(db_finalize $qh $user_db)
if (=s $res "") [
res = 0
//err = (format "user__exist: user %1 not found in db." $nick)
//echo $err
] [
res = 1
//err = (format "user__exist: user %1 found in db." $nick)
//echo $err
//(format "^f1Player ^f0%1 ^f1was connected from ip ^f0%2 ^f1at ^f0%3" $arg1 (int2ip (at $res 0)) (timef (at $res 1) "%d.%m.%Y, %X"))
]
]
result $res
]
cmd_userexist = [
//untested, but should work
nick = (sanitize__string $arg2)
if (= (user__exist $nick) 0) [
pm $arg1 "$nick is not in the user database."
] [
pm $arg1 "$nick is in the user database."
]
]
registercommand "userexist" cmd_userexist 1 "s" "userexist ^f1Check to see if a user has registered. #userexist "
password__match = [
db_init $user_db
nick = $arg1
password = $arg2
query = (format "SELECT nick FROM %1 WHERE nick=':0' AND password=':1'" $users_table)
qh = (db_pquery $query (concat $nick $password) $user_db)
res = ""
// check for errors
if (= $qh -1) [
db_error $user_db
pm $arg1 "user_exist: Database error"
res = 0
] [
res = (db_getrow $qh $user_db)
(db_finalize $qh $user_db)
if (=s $res "") [
res = 0
//err = (format "user__exist: passwords do not match." $nick)
//echo $err
//(format "^f1Player ^f0%1 ^f1have never connected" $arg1)
] [
res = 1
//err = (format "user__exist: passwords do match!" $nick)
//echo $err
//(format "^f1Player ^f0%1 ^f1was connected from ip ^f0%2 ^f1at ^f0%3" $arg1 (int2ip (at $res 0)) (timef (at $res 1) "%d.%m.%Y, %X"))
]
]
result $res
]
cmd_identify = [
nick = (getname $arg1)
password = (sanitize__string $arg2)
ipaddress = (getip $arg1)
if (= (user__exist $nick) 1) [
if (= (password__match $nick $password) 1) [
welcomemsg = (format "Welcome back %1! Unspecing..." (getname $arg1))
pm $arg1 $welcomemsg
spectator $arg1 0 //unspec player
] [
pm $arg1 "Invalid password."
]
] [
pm $arg1 "Your nick has not been registered. Register your nick first using #register "
]
]
registercommand "identify" cmd_identify 1 "s" "identify ^f1Identify yourself, #identify "
cmd_register = [
nick = (getname $arg1)
//feedback = (format "pre-sanitized register nick=%1 arg2=%2" $nick $arg2)
//echo $feedback
password = (sanitize__string $arg2)
ipaddress = (getip $arg1)
//feedback = (format "postsanitized register nick=%1 password=%2" $nick $password)
//echo $feedback
// load keys from database if needed
if (= (user__exist $nick) 0) [
db_init $user_db
//create table users(nick text, password text, email text, ip text, status integer);
//qh = (db_insert_replace $users_table "VALUES(':0',':1','',':2',1)" (concat $nick $password $ipaddress) $adduser_query $user_db)
qh = (db_insert_replace $users_table "VALUES(':0',':1','',':2',1)" (concat $nick $password $ipaddress) $user_db)
// check for errors
if (= $qh -1) [
pm $arg1 "Database error, unable to add user."
db_error $user_db
] [
pm $arg1 "User successfully added. Welcome!"
spectator $arg1 0 //unspec player
// no errors
//authcount = 0
//while [row = (db_getrow $qh $auth_db); result (!=s $row "")] [
// // row format "degrave 23abc54bca4b3c5bc 0"
// adduser (at $row 0) (at $row 1)
// authcount = (+ 1 $authcount)
// //echo (format "adduser %1 %2" (at $row 0) (at $row 1))
//]
//syncmsg = (format "Authkeys reloaded, %1 authkeys were loaded from database" $authcount)
db_finalize $qh $user_db
]
] [
pm $arg1 "User already exists."
]
]
// register our command, for admins only
registercommand "register" cmd_register 1 "s" "register ^f1Register yourself, #register "