Re: Text file organization
Not thoroughly tested. You must start out with an empty line after each set of users within a group and between groups. Back up your users.ini beforehand, I take no responsibility for lost data.
For example:
Code:
[Owners]
<admin data>
<admin data>
-blank line-
[Noobs]
<admin data>
-blank line-
PHP Code:
AddAdmin( "[Owners]" , "STEAM" , "abc" , "pwd" , "def" , "bugsy1" , 555 ); AddAdmin( "[Noobs]" , "STEAM" , "ghi" , "pwd" , "jkl" , "bugsy2" );
//The idea with this is to write each line from users.ini to a temporary file users.ini_tmp, when the current line from users.ini matches the specified group name, //the plugin will continue looping through until it finds an empty line (under the group name) -- here it will insert the new admin, it will then continue copying lines //from users.ini into users.ini_tmp. If a new admin insert occurred, users.ini will be deleted and users.ini_tmp is renamed to users.ini. If no insert occurred, users.ini_tmp is deleted. AddAdmin( const szAdminGroup[] , const szAuth[] , const szAccessFlags[] , const szPassword[] , const szAcctFlags[] , const szName[] , iTime = 0 ) { new iFile , iTempFile , szBuffer[ 256 ] , iBracketEndPos , szConfig[ MAX_FMT_LENGTH ] , szTmpConfig[ MAX_FMT_LENGTH ] , bool:bInGroup , bool:bDone; //Set full users.ini file into a string get_configsdir( szConfig , charsmax( szConfig ) ); add( szConfig , charsmax( szConfig ) , "/users.ini" ); //Create a temp users.ini (users.ini_tmp) file which will eventually turn into users.ini at the end, if an entry occurred. formatex( szTmpConfig , charsmax( szTmpConfig ) , "%s_tmp" , szConfig ); //Open users.ini for read/text if ( ( iFile = fopen( szConfig , "rt" ) ) ) { //Open temp users.ini for write/text if ( ( iTempFile = fopen( szTmpConfig , "wt" ) ) ) { //Continue looping through each line of users.ini while ( fgets( iFile , szBuffer , charsmax( szBuffer ) ) ) { //No analysis is needed on the line if it begins with a comment character, it will be copied to the temp file as-is. if ( szBuffer[ 0 ] != ';' ) { //Locate the end position of a group name within the line. This will be used in the equali() check to //make it compatible with Windows and Linux. iBracketEndPos = strfind( szBuffer , "]" ); //IF // - The plugin has not yet added the new admin // - The szAdminGroup[] has not yet been found // - The line of text equals the szAdminGroup. We must subtract one character from the end to remove ^n. if ( !bDone && !bInGroup && ( iBracketEndPos > -1 ) && equali( szBuffer , szAdminGroup , iBracketEndPos + 1 ) ) { bInGroup = true; } //IF // - The plugin has not yet added the new admin // - The szAdminGroup[] has been found // - The line of text equals new line which means this is an empty area that can be used for writing the new admin. else if ( !bDone && bInGroup && ( ( szBuffer[ 0 ] == '^r' ) || ( szBuffer[ 0 ] == '^n' ) ) ) { WriteUser( iTempFile , szAuth , szAccessFlags , szPassword , szAcctFlags , szName , iTime ); bDone = true; continue; } } //This writes the line from users.ini to temp users.ini. There is only one instance where we do not want the exact line //from the original users.ini and this is when inserting the new admin. fputs( iTempFile , szBuffer ); } //This indicates we reached the end of the file, the szAdminGroup[] group was found, but a subsequent empty line was not found. //When this occurs, the new admin is written to the end of the file. This will occur when the group is at the bottom of users.ini if ( bInGroup && !bDone ) { log_to_file( "AddAdmin" , "Adding user to end of file" ); WriteUser( iTempFile , szAuth , szAccessFlags , szPassword , szAcctFlags , szName , iTime ); bDone = true; } //Close both files. fclose( iFile ); fclose( iTempFile ); //If a new admin was inserted; delete the original users.ini, rename the temp users.ini to users.ini, and call reload admins. if ( bDone ) { delete_file( szConfig ); rename_file( szTmpConfig , szConfig , true ); server_cmd( "amx_reloadadmins" ); } //A new admin was not inserted so the temp users.ini is deleted, leaving users.ini unmodified. else { delete_file( szTmpConfig ); } } } }
WriteUser( iFile , const szAuth[] , const szAccessFlags[] , const szPassword[] , const szAcctFlags[] , const szName[] , iTime = 0 ) { new szBuffer[ 128 ] , szTime[ 10 ]; //If a time value is specified, format it in a string in the following format (including quotes): "123" if ( iTime ) formatex( szTime , charsmax( szTime ) , "^"%d^"" , iTime ); //Format the entire line, optionally including time if it was a non-zero value. Two new-lines are included on the end. formatex( szBuffer , charsmax( szBuffer ) , "^"%s^" ^"%s^" ^"%s^" ^"%s^" %s; %s^n^n" , szAuth , szPassword , szAccessFlags , szAcctFlags , iTime ? szTime : "" , szName ); //Write admin line to file. fputs( iFile , szBuffer ); }
|