Code:
/* amx_auth script by lowe
* 1.0 First Working Version
* 1.2 amxx-0.20
*
*
*/
#include <amxmodx> // Includes the
#include <dbi> // include-files
new sqlhost[64],sqluser[64],sqlpass[64],sqldbname[64],sqlerrormsg[256] // SQL connection related variables
public plugin_init() {
register_plugin("DB.se - Script","1.2","lowe") //register the pluginname, version, and creator
register_dictionary("db.se.txt") // Registers the DB.se dictionary
register_clcmd("amx_auth","auth_me",0,"<nick> <password> Registers your STEAM ID in the Disturbed.se database") // amx_auth commando, to register your steamid into the database
set_task(10.0,"dbseinit")
}
public plugin_modules()
{
require_module("DBI")
}
// *************************************************************************************************************
public dbseinit() {
get_cvar_string("amx_sql_host",sqlhost,64) // Fetch the database info
get_cvar_string("amx_sql_user",sqluser,32) // stated in sql.cfg
get_cvar_string("amx_sql_pass",sqlpass,32)
get_cvar_string("amx_sql_db",sqldbname,32)
new Sql:dbiSqL = dbi_connect(sqlhost,sqluser,sqlpass,sqldbname,sqlerrormsg,255) // And connect to the database
if (dbiSqL <= SQL_FAILED) { // If there is an error
server_print("[Disturbed] %L",LANG_SERVER,"SQL_CANT_CON",sqlerrormsg) // Show the errormessage
dbi_close(dbiSqL) // Close the connection
return PLUGIN_HANDLED // Finish the plugin
}
// If Connected Successfuly
server_print("[Disturbed] %L",LANG_SERVER,"SQL_CONNECTED") // Show the Connected message
return PLUGIN_CONTINUE // And continue with the rest of plugin
}
// *************************************************************************************************************
new g_regged[33] // Adds a variable used to check if a user is regged.
// ********************************Auther Starts Here***********************************************************
public auth_me(id) { // the auther
new iargs = read_argc() // reads the number of arguments
if (iargs != 3) { // if there is to few or to many...
console_print(id,"[AMXX] %L", LANG_PLAYER, "AUTH_MISS_ARGS") // echo an errormessage to users console
return PLUGIN_CONTINUE // and finish the auther function
}
new aname[32],apassword[64],asteamid[32] // Variabels for users nick + pass + steamid
read_argv(1,aname,32) // Save name in variable
read_argv(2,apassword,64) // save password in variable
get_user_authid(id,asteamid,31) // save steamid to variable
g_regged[id] = auther(asteamid, aname, apassword) // execute the auther boolesk function
if (g_regged[id]) { // if auther() returns 1
console_print(id,"[Disturbed] %L",LANG_SERVER,"AUTH_REGGED",asteamid) // Write a confirmation in the console
} else { // if not
console_print(id,"[Disturbed] %L",LANG_SERVER,"AUTH_REGG_FAIL",asteamid,aname) // Echo Failure Msg
}
return PLUGIN_CONTINUE // Continue =)
}
// *****************************************Auther Continue****************************************************
bool:auther(asteamid[], aname[], apassword[]) { // the auther() function
const LONGER = 128 // Save the length of the query variable in a constant
new query[LONGER + 1] // Create variables for query
format(query, LONGER, "SELECT `id` FROM members WHERE nick = '%s' AND pass = ENCODE('%s','Secret :P)", aname, apassword) // Formate Query to check if user is registered
new Result:result = dbi_query(dbiSqL, query) // Execute the query
if (result == RESULT_NONE) { // If nothing is returned
return false // Fail the function
}
if ( result == RESULT_FAILED ) { // If there is a error...
dbi_error(dbiSqL,sqlerrormsg,255) // save error in sqlerrormsg
server_print("[Disturbed] %L",LANG_SERVER,"SQL_ERROR", sqlerrormsg) // and echo the errormessage to the console
return false // Fail the function
}
new aid = dbi_getfield(dbiSqL, 1) // save the users userid
dbi_free_result(result) // Free the result from the query
format(query, LONGER, "SELECT `gid` FROM gamelink where game = 'cstrike' AND id = '%s'", aid) // Formate Query to check if user has a registered steamid
new Result:result = dbi_query(dbiSqL, query) // Execute the query
if ( result == RESULT_FAILED ) { // If there is a error...
dbi_error(dbiSqL,sqlerrormsg,255) // save error in sqlerrormsg
server_print("[Disturbed] %L",LANG_SERVER,"SQL_ERROR", sqlerrormsg) // and echo the errormessage to the console
return false // Fail the function
}
if (result == RESULT_NONE) { // If nothing is returned (aka. First time to set steamid)
dbi_free_result(result) // free the results from the query
format(query, LONGER, "insert into gamelink(gid,id,game) VALUES('%s','%s','cstrike')",asteamid,aid) // Formate Query to save the steamid in the database
new Result:result = dbi_query(dbiSqL, query) // Execute the query
if ( result == RESULT_FAILED ) { // If there is a error...
dbi_error(dbiSqL,sqlerrormsg,255) // save error in sqlerrormsg
server_print("[Disturbed] %L",LANG_SERVER,"SQL_ERROR", sqlerrormsg) // and echo the errormessage to the console
return false // Fail the function
}
}
else { // if things are returned (aka. not first time the query is executed)
dbi_free_result(result)
format(query, LONGER, "update gamelink set gid = '%s' WHERE id = '%s' AND game = 'cstrike' LIMIT 1",asteamid,aid) // Formate Query to save the steamid in the database
new Result:result = dbi_query(dbiSqL, query) // Execute the query
if ( result == RESULT_FAILED ) { // If there is a error...
dbi_error(dbiSqL,sqlerrormsg,255) // save error in sqlerrormsg
server_print("[Disturbed] %L",LANG_SERVER,"SQL_ERROR", sqlerrormsg) // and echo the errormessage to the console
return false // Fail the function
}
}
return true
}
// **************************************************End of Auther**********************************************