<?php
set_time_limit(0);
ini_set('display_errors', 'on');
$configure = array(
    'server' => 'ssl://irc.psych0tik.net',
    'port' => 6697,
    'nick' => 'minion1',
    'name' => "minion1",
    'channel' => '#valahalla',
    //'channel' => '#minionlab',
    'pass' => null
);
$cycle = 1;
class Bot {
	public $socket;
    	public $msg = array();
	function __construct($configure){
        	$this->configure = $configure;
        	$this->socket = fsockopen($configure['server'], $configure['port']);
        	$this->login($configure);
        	sleep(5);
        	$this->send_data('JOIN', $this->configure['channel']);
        	while (True) {
        		$this->main();
        	}
    	}
    	function login($configure){
    		$this->send_data('USER', $configure['nick'] . ' t0xiksite.herobo.com ' . $configure['nick'] . ' :' . $configure['name']);
    		$this->send_data('NICK', $configure['nick']);
    	}
    	function main(){
		$data = fgets($this->socket, 128);
		$this->ex = explode(' ', $data);
        	if (!$data) {
        		echo "Error";
		        echo "var dump $data:";
        		echo "Var Dump of $data: ";
			var_dump($data);
        		exit();
        	} 
        
        	//Comment the below out to avoid duplicates
		elseif (count($this->ex) < 3) {
			echo "Error";
			var_dump($data);
		} else {
                	$i = 0;
                	while($this->i == 0) {
        			if ($this->configure['channel'] == '#t0x0b0t' || $this->configure['channel'] == '#t0xik'){
                			$this->send_data('PRIVMSG', $this->configure['channel']." :Channel sweet channel!");
                		}
                		sleep(4);
                		$this->send_data('PRIVMSG', $this->configure['channel']." :Hello everyone!");
                		$this->i++;
                	}
        		flush();
        		foreach ($this->ex as $key => $var) {
        			if ($var == 'PING'){
         				//plays ping-pong with the server..
            				$this->send_data('PONG', $this->ex[1]);
        			}
        		echo $var."<br><br>";
        		}
                	$command = "";
                	for ($i=3; $i<count($this->ex); $i++) {
				$command .= $this->ex[$i] . ' ';
                	}
                	$command = str_replace(array(chr(10), chr(13)), '', $command);
                	$command = strtolower($command);
                	$name = $this->ex[0];
                	$pos = strpos($name, '!');
                	$name = substr($name, 1, $pos-1);
                	$command = explode('!', $command);
                	$command = implode($command);
                	if (preg_match("/minion1 go get me/", $command)) {
                		$command = explode("minion1 go get me ", $command);
                		$get = $command[1];
                		$thanx = 1;
                		$this->send_data("PRIVMSG", $this->configure['channel']." :001ACTION gets ".$name." ".$get."001");
                	}
			//not really ready yet, still in development
                	if (preg_match("/minion1: clone /", $command) && $name == "t0xik") {
                		$this->send_data("PRIVMSG", $this->configure['channel'].":Yes master...");
                		$command = explode("minion1: clone ", $command);
                		$user = $command[1];
                		$this->clone_init($this->configure, $user);
                	}
                	if (substr($command, 1, 5) == "!op: " && $name == "t0xik") {
                		$op = explode("!op: ", $command);
                		$oper = $op[1];
                		$this->send_data("MODE", $configure['channel']." :+o ".$oper);
                	}
                	if (preg_match("gbyeg", $command) && preg_match("/minion1g", $command)){
                		$this->send_data("PRIVMSG", $this->configure['channel']." :Bye ".$name."!");
                	}
                	if (preg_match("gthanksg", $command) && $thanx = 1) {
                		$thanx = 0;
                		$this->send_data("PRIVMSG", $this->configure['channel']." :No problem ".$name."!");
                	}
			switch($command){
                		case ':hey minion1 ':
                	        	$this->send_data("PRIVMSG", $this->configure['channel']." :Hey ".$name);
                	        break;
                	        case ':hi minion1 ':
                	        	$this->send_data("PRIVMSG", $this->configure['channel']." :Hi ".$name);
                	        break;
                	        case ':hello minion1 ':
                        		$this->send_data("PRIVMSG", $this->configure['channel']." :Hello ".$name);
				break;
                	        case ':congratulations minion1 ':
                	        	$this->send_data("PRIVMSG", $this->configure['channel']." :Congratz to you too, ".$name."! Wait, what are we talking about?");
                        	break;
                        	case ':minion1: quit ':
                        		if ($name == 't0xik' || $name == "Master") {
                        			$this->send_data("QUIT", " :I am t0xik's evil minion");
                        			$this->send_data("PRIVMSG", $this->configure['channel']." :Bye everyone!");
                        		} else {
                        			$this->send_data("PRIVMSG", $this->configure['channel']." :Only t0xik can tell me to quit, not you ".$name."!");
                        		}
                        	break;
                        	case ':sup minion1 ':
                        		$this->send_data("PRIVMSG", $this->configure['channel']." :Sup ".$name);
                        	break;
                        	case ':minion1: status report ':
                        		$this->send_data("PRIVMSG", $this->configure['channel']." :Well ".$name.", all systems are running fine, and the code is about 75% finished.");
                        		$this->send_data("PRIVMSG", $this->configure['channel']." :I am on my ".$cycle."th evaluation cycle.");
                        	break;
 				}
    			}
    		}
		function send_data($cmd, $msg = null){
			if($msg == null){
				fputs($this->socket, $cmd . "rn");
				echo "<b>$cmd<gb>";
    		     	}else{
    		        	fputs($this->socket, $cmd.' '.$msg."rn");
				echo "<b>$cmd $msg<gb>";
			}
	    	}
		function join_channel($channel){
    	    		$this->send_data('JOIN', $channel);
    		}
    	}
$bot = new Bot($configure);
?>