IRC Bot

Plaintext Version

<?php
/*
* Bot Class v0.3
* By pyr0t3chnician [at] gmail.com
* VIEW README AT http://www.notan00b.com/classBotREADME.txt
* Sample bot script at http://www.notan00b.com/bot2.txt
* Change the file extension of both to .php to make them work.
*/
class bot{
	var $BotName="MrBot";
	var $BotNick="MrBot";
	var $BotPass="password";
	var $BotOperPass="operpassword";
	var $AdminName="pyr0t3chnician";
	var $AdminEmail="pyr0t3chnician@gmail.com";
	var $AdminHostMask="chatzilla@DME-Client-9299C001.phnx.qwest.net";
	var $IrcHost="zeroirc.info";
	var $IrcPort=6667;
	var $IrcChan="#zeroidentity";
	private $IrcSock;
	var $Line; // entire line
	var $LastSpeaker; // last person to join or speak
	var $LastHow; // how the info was received (JOIN, NOTIFY, PRIVMSG)
	var $LastWhere; // Where (PM or Channel)
	var $LastMessage; // The entire message that was said
	var $LastCommand; // The first word that they said
	var $LastArgs; // The rest of the words after the first word (!say hello <- hello is args)
	var $LastHost;
	var $LastHostMask;
	var $LastBrowser;
	var $MySQLHost="localhost";
	var $MySQLUser;
	var $MySQLPass;
	var $MySQLDB;
	private $MySQLCon;
	private $Commands = array();
	private $EventCommands = array();
	function _construct()
	{
		set_time_limit( 0 );
	}
	function OpenConnection($host='',$channel='',$port=0)//Opens connection and starts the bot.
	{
		if($host!='')
			$this->IrcHost=$host;
		if($channel!='')
			$this->IrcChan=$channel;
		if($port!=0)
			$this->IrcPort=$port;
		$this->IrcSock=fsockopen($this->IrcHost,$this->IrcPort, $errorNo, $errorStr );
		if($this->IrcSock)
		{
			fputs( $this->IrcSock, "USER " . $this->BotNick . " " . $this->BotNick . " *: " . $this->BotName . "\r\n" ); 
			fputs( $this->IrcSock, "NICK " . $this->BotNick . "\r\n" );   
			$this->ConnectionLoop();
		}else
			die( "Unable to connect to " . $this->IrcHost . " on port " . $this->IrcPort . "\r\nError number: " . $errorNo . "\r\nError message: " . $errorStr . "\r\n" );
	}
	private function ConnectionLoop()
	{
		while(!feof($this->IrcSock))
		{
			$this->Line=fgets($this->IrcSock, 4096); 
			echo $this->Line;
			$this->Line = trim($this->Line);
			eregi( "^:(.*)!", $this->Line, $matches ); 
			$this->LastSpeaker = $matches[1];     
			$this->Output("JOIN", $this->IrcChan,'');
			if( ereg( "PING :(.*)", $this->Line, $pong ) )  
				$this->Output("PONG",$pong[1],'');   
			if( eregi( "^:(.*)!(.*)@(.*) PRIVMSG " . $this->BotNick . " :" . chr( 1 ) . "VERSION" . chr( 1 ), $this->Line, $matches ) )  
				$this->Output("NOTICE", $this->LastSpeaker, chr( 1 )."Version: 1.3.3.7".chr( 1 ));   
			$this->ParseLine();
			$this->ExecuteCommand();
			$this->ExecuteEventCommand();
			$this->LastCommand='';
			$this->Line='';
		}
	}
	function Output($type, $who, $message) // Outputs ANY command
	{
		fputs($this->IrcSock,$type." ".$who." :".$message."\r\n");
	}
	private function ParseLine()
	{
		if( eregi( "^:(.*)!(.*)@(.*) (.*) (.*) :(.*)$", $this->Line, $matches ) )
		{
			$this->LastSpeaker=$matches[1];
			$this->LastBrowser=$matches[2];
			$this->LastHost=$matches[3];
			$this->LastHostMask=$matches[2]."@".$matches[3];
			$this->LastHow=$matches[4];
			$this->LastWhere=$matches[5];
			$this->LastMessage=$matches[6];
			$com=explode(" ",$matches[6]);
			$this->LastCommand=$com[0];
			$args='';
			for($a=1;$a<count($com);$a++)
			{
				if($a!=1)
					$args.=" ".$com[$a];
				else
					$args=$com[$a];
			}
			$this->LastArgs=$args;
		}
	}
	function AddCommand($command,$function) // Adds a regular command (any expression, only the first word they type
	{
		$this->Commands[count($this->Commands)]["String"]=$command;
		$this->Commands[count($this->Commands)-1]["Function"]=$function;
	}
	private function ExecuteCommand()
	{
		foreach($this->Commands as $command)
		{
			if($this->LastCommand==$command["String"])
			{
				$execute=$command["Function"];
				$execute($this);
			}
		}
	}
	function AddEventCommand($action,$function) // Adds an EventCommand
	{
		$this->EventCommands[count($this->EventCommands)]["String"]=$action;
		$this->EventCommands[count($this->EventCommands)-1]["Function"]=$function;
	}
	private function ExecuteEventCommand()
	{
		foreach($this->EventCommands as $command)
		{
			if(strpos($this->Line,$command["String"])!==false)
			{
				$execute=$command["Function"];
				$execute($this);
			}
		}
	}
	function BasicOutput($string)
	{
		fputs($this->IrcSock,$string."\r\n");
	}
        function BotQuery($string){
		$this->MySQLCon = mysql_connect($this->MySQLHost,$this->MySQLUser,$this->MySQLPass);
		if (!$this->MySQLCon)
		{
		  die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($this->MySQLDB,$this->MySQLCon);
		mysql_query($string);
		mysql_close($this->MySQLCon);
	}
	function BotReturnArray($string){
		$this->MySQLCon = mysql_connect($this->MySQLHost,$this->MySQLUser,$this->MySQLPass);
		if (!$this->MySQLCon)
		{
		  die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($this->MySQLDB,$this->MySQLCon);
		$result = mysql_query($string);
		if(mysql_num_rows($result)>1)
		{
			$count=0;
			while($row=mysql_fetch_array($result))
			{
				$return[$count]=$row;
				$count++;
			}
		}else
			$return[0]=mysql_fetch_array($result);
		mysql_close($this->MySQLCon);
		return $return;
	}
	function BotReturnCount($string){
		$this->MySQLCon = mysql_connect($this->MySQLHost,$this->MySQLUser,$this->MySQLPass);
		if (!$this->MySQLCon)
		{
		  die('Could not connect: ' . mysql_error());
		}
		mysql_select_db($this->MySQLDB,$this->MySQLCon);
		$result = mysql_query($string);
		$count = mysql_num_rows($result);
		mysql_close($this->MySQLCon);
		return $count;
	}
}
?>