<?php
	/* 
		PHP:IRC Created by Navarr T. Barnier
		

		Must be distributed with WiinVi WiiCR theme.
		Do not Edit.
	*/
	include('irccc.php');
	header('Connection: Keep-Alive');
	header('Pragma: no-cache');
	$server		= $_GET['serv'];
	$username	= $_GET['user'];
	$chan		= $_GET['chan'];
	$port		= $_GET['port'];
	$okay		= FALSE;

			  set_time_limit(0);
	echo '<table style="width:100%;font-size:14pt;">';
	irc_writeout("<strong>Starting Connection to Server...</strong>");
	$irc = new irccc($server,$port,$username,$username,"WiiCR");
	$irc-> join($chan);
	while(TRUE) {
		echo ' ';flush();
		if ($irc->remote['status'] == IRCCC_SYS_CONNECTED) {
			$okay = TRUE;
		}
		if ($irc->remote['status'] == IRCCC_SYS_NOTCONNECTED && $okay) {
			irc_writeout("<strong>IRC Connection has been closed by Server or due to lack of Activity.</strong>");
			$okay = FALSE;
			echo "</table>";die();
		}
		if ($irc->readdata()) {
			$irc->pingcheck();
			foreach($irc->buffer as $line) {
				// :NICK!USER@HOST PRIVMSG #CHAN :TEXT
				$temp = explode(" ",$line);
				$temp2 = explode("!",$line);
				$temp3 = explode(":",$line);
				if ($temp[1] == 'PRIVMSG') {
					if (strtolower($temp[2]) == strtolower($chan)) {
						irc_writeout(substr($line,strlen($temp[0].' '.$temp[1].' '.$temp[2].' :')),substr($temp2[0],1));
					}
				} elseif ($temp[1] == '375') { /* MOTD Start */
				} elseif ($temp[1] == '372') { 
					irc_writeout(substr($line,strlen(':'.$temp3[1].':')),"MOTD");
				} elseif ($temp[1] == '376') {
					irc_writeout('End of <span style="color:red;">MOTD</span>.',"MOTD");
				} elseif ($temp[1] == 'JOIN') {
					if ($temp2[0] == ':'.$username) {
						irc_writeout('<span style="color:rgb(42,140,42);">Joined '.substr($temp[2],1)."</span>");
					} else {
						irc_writeout('<span style="color:rgb(42,140,42);">'.substr($temp2[0],1)." joined ".substr($temp[2],1)."</span>");
					}
				} elseif ($temp[1] == 'PART') {
					if ($temp2[0] == ':'.$username) {
						irc_writeout('<span style="color:red;">Left '.substr($temp[2],1)."</span>");
					} else {
						irc_writeout('<span style="color:red;">'.substr($temp2[0],1)." left ".substr($temp[2],1)." (".substr($line,strlen(":".$temp3[1].":")).")</span>");
					}
				} elseif ($temp[1] == 'QUIT') {
					irc_writeout('<span style="color:red;">'.substr($temp2[0],1)." quit ".substr($temp[2],1)." (".substr($line,strlen(":".$temp3[1].":")).")</span>");
				} elseif ($temp[1] == 'MODE') {
					irc_writeout(substr($temp2[0],1).' set mode '.substr($line,strlen($temp[0].' '.$temp[1].' '.$temp[2].' ')));
				} else {
					irc_writeout($line,'RAW');
				}
			}
			$irc->pingcheck();
			$irc->buffer_clear();
		}
		$rawdata = file_get_contents('irc_wait.txt');
		$data2 = explode("\r\n",$rawdata);
		foreach($data2 as $d) {
			if (substr($d,0,3) == "ME ") {
				$irc->me($chan,substr($d,4));
				irc_writeout($username.' '.substr($d,4),'*',TRUE);
			}
			if (substr($d,0,4) == 'RAW ') {
				$irc->raw(substr($d,4));
				irc_writeout("Raw Data Sent.");
			}
			if (substr($d,0,4) == 'MSG ') {
				$irc->msg($chan,substr($d,4));
				irc_writeout(substr($d,4),$username,TRUE);
			}
			if (substr($d,0,5) == "PRIV ") {
				$t = explode(" ",$d);
				$irc->msg($d[1],substr($d,strlen($t[0]." ".$t[1])));
				irc_writeout("<span style=\"color:purple;\">Message Sent!</span>");
			}
			if (substr($d,0,5) == 'QUIT ') {
				$irc->quit(substr($d,5));
				$f = fopen('irc_wait.txt','w');fclose($f);
				irc_writeout("<strong>Disconnecting from Server...</strong>");die("</table>");
			}
		}
		$f = fopen('irc_wait.txt','w');fclose($f);
	}
	function irc_writeout($data,$user = '*',$me = FALSE) {
		$time = date("H:i:s");
		if ($user == '*') {
			echo '<tr><td>['.$time.']</td><td style="text-align:right;color:purple;" valign="top">*</td><td>'.$data.'</td></tr>';flush();
		} elseif ($me) {
			echo '<tr><td>['.$time.']</td><td style="text-align:right;color:gray;" valign="top">&lt;<strong>'.$user.'</strong>&gt;</td><td>'.$data.'</td></tr>';flush();
		} else {
			echo '<tr><td>['.$time.']</td><td style="text-align:right;color:red;" valign="top">&lt;<strong>'.$user.'</strong>&gt;</td><td>'.$data.'</td></tr>';flush();
		}
	}
?>