/* AMX Mod script.
*
* (c) Copyright 2002, Noplay
* This file is provided as is (no warranties).
*
*/

/*
The original idea of this, is from dJeyL's M4

Thanks to OLO for have correct many bogue
*/

/*

This AMX plugin requires MySQL MetaSmall module.

For this to work, you might create your MySQL table with this SQL query :

CREATE TABLE rcon (
  srv tinyint(4) NOT NULL default '0',
  cmd varchar(255) NOT NULL default ''
) TYPE=MyISAM;


Notes :
. check $moddir/addons/amx/mysql.cfg for MySQL access configuration

*/

/*
All server need have an unique ID give by the cvar amx_server_id

you can this in addons/amx/phpamx.cfg file

for example:
amx_server_id "1"

If you have many server on the same computer and on different port, 
i recommend to use the PORT.cfg amx plugin 
*/

#include <amxmod>
#include <mysql>

new srv_id[5]

new mysql



public checkcmd()
{
	new cmd[257]
	new query[256]
    

	if(mysql>0)
 	{
     
		format(query,256,"SELECT cmd FROM rcon WHERE srv='%s'",srv_id)
	 
	
		if(mysql_query(mysql,query)>0)
		{
			while(mysql_nextrow(mysql)>0)
         		{
				mysql_getfield(mysql,1,cmd,256)
			
				server_cmd("%s ",cmd)
                
			}
			format(query,256,"DELETE FROM rcon WHERE srv='%s' and cmd='%s'",srv_id,cmd)
 	 		mysql_query(mysql,query)
     		}        

 	}  
  
	return PLUGIN_HANDLED
} 

public connect_to_srv()
{
   get_cvar_string("amx_server_id",srv_id,4) 
    
   set_task(5.0,"checkcmd",1,"",0,"b")  

   new host[65] 
   new user[33] 
   new pass[33] 
   new db[33] 
   new error[257] 


   get_cvar_string("amx_mysql_host",host,64) 
   get_cvar_string("amx_mysql_user",user,32) 
   get_cvar_string("amx_mysql_pass",pass,32) 
   get_cvar_string("amx_mysql_db",db,32) 
    
   mysql = mysql_connect(host,user,pass,db,error,256) 
   
   if (mysql)
      server_print("[PHPAMX] Connected to MySQL!")
   else
      server_print("[PHPAMX] Failed to connect MySQL!")   
}

public plugin_init() 
{

   register_plugin("MySQL CheckCMD","0.4","Noplay") 
   register_cvar("amx_mysql_host","127.0.0.1") 
   register_cvar("amx_mysql_user","") 
   register_cvar("amx_mysql_pass","") 
   register_cvar("amx_mysql_db","amx")
   register_cvar("amx_server_id","0")
   server_cmd("exec addons/amx/phpamx.cfg")
   set_task(0.25,"connect_to_srv")
   return PLUGIN_CONTINUE 
}

public plugin_end()
{
	mysql_close(mysql)
}