All pastes #140119 Raw Edit

PHP IRC Bot

public php v1 · immutable
#140119 ·published 2006-08-20 07:06 UTC
rendered paste body
<?php/* CONFIG */set_time_limit(0);$shutdown = 0;$config = array();$config['network'] = 'Netgamers';$config['server'] = 'irc.netgamers.org';$config['port'] = '6667';$config['usernick'] = 'Tommeh';$config['altnick'] = 'Tommmeh';$config['username'] = 'Tommeh';$config['channels'] =	array(		'#melons',		'#planetia',	);$config['owner'] = array();$config['owner']['usernick'] = "";$config['owner']['identd'] = "";$config['owner']['hostname'] = "Tommy.users.netgamers.org";$logfile = fopen("C:/php/scripts/" . $config['network'] . ".log",'a+t');if (!is_resource($logfile)) {	print("Unable to open / create log file.\n");}else {	fputs($logfile,"\n\n-" . str_repeat("=",48) . "-\n -= Logging commenced at " . date("[d/m/y - H:i:s]") . " =-\n-" . str_repeat("=",48) . "-\n\n");}/* CONNECTION LOOP */while (!$shutdown) {	print('Connecting to ' . $config['server'] . ' on port ' . $config['port'] . "\n");	$connection = array();	$connection['init'] = 1;	$connection['buffer'] = array();	$connection['settings'] = array();	$connection['server'] = array();	$connection['channels'] = array();	while (!is_resource($connection['socket'] = @fsockopen($config['server'],$config['port']))) {		print("Unable to connect to server.\n");	}	if (is_resource($connection['socket'])) {		raw('USER ' . $config['usernick'] . ' localhost localhost :' . $config['username']);		raw('NICK ' . $config['usernick']);		$connection['settings']['nick'] = $config['usernick'];	}	/* DATA LOOP */	while (!feof($connection['socket'])) {		$connection['buffer']['read'] = fgets($connection['socket'],4096);		if ($connection['buffer']['read'] != $oldbuffer) {			if (substr($connection['buffer']['read'],0,6) == 'PING :') {				raw('PONG :' . substr($connection['buffer']['read'],6));				if ($connection['init'] == 1) {					$connection['init'] = 0;					init();				}			}			else {				$connection['buffer']['all'] .= $connection['buffer']['read'];				if (strpos($connection['buffer']['all'],"\r\n") !== false) {					parsebuffer();					checkbuffer();					unset($connection['buffer']['all']);				}			}		}		$oldbuffer = $connection['buffer']['read'];	}}if (is_resource($logfile)) {	fclose($logfile);}?><?php/* COMMON FUNCTIONS */function init() {	global $connection, $config;	foreach ($config['channels'] as $channel) {		raw('JOIN ' . $channel);	}}function raw($what) {	global $connection, $config, $logfile;	fputs($connection['socket'],$what . "\r\n");	print(date("[d/m - H:i:s]") . " -> " . $what . "\n");	if (is_resource($logfile)) {		fputs($logfile,date("[d/m - H:i:s]") . " -> " . $what . "\n");	}}/* BUFFER PARSING */function parsebuffer() {	global $connection, $config, $logfile;	print(date("[d/m - H:i:s]") . " <- " . $connection['buffer']['all']);	if (is_resource($logfile)) {		fputs($logfile,date("[d/m - H:i:s]") . " <- " . $connection['buffer']['all']);	}	$buffer = explode(' ',str_replace("\r\n","",$connection['buffer']['all']),4);	foreach ($buffer as $i => $j) {		if (substr($j,0,1) == ":") {			$buffer[$i] = substr($j,1);		}	}	$posex = strpos($buffer[0],'!');	$posat = strpos($buffer[0],'@');	$connection['buffer']['usernick'] = substr($buffer[0],0,$posex);	$connection['buffer']['identd'] = substr($buffer[0],$posex+1,$posat-$posex-1);	$connection['buffer']['hostname'] = substr($buffer[0],$posat+1);		$connection['buffer']['command'] = $buffer[1];	$connection['buffer']['target'] = $buffer[2];	$connection['buffer']['arguments'] = $buffer[3];}/* COMMAND SWITCHING / RESPONSE */function checkbuffer() {	global $connection, $config, $logfile;	switch($connection['buffer']['command']) {		/* ### Nick in use ###		If first nick in use, re-send NICK command with alternate nick and change settings to match */		case '433':			raw('NICK ' . $config['altnick']);			$connection['settings']['nick'] = $config['altnick'];			break;				/* ### Server info / modes ###		Get server cmodes, umodes, settings */		case '005':			$settings = $connection['buffer']['arguments'];			if (preg_match('/^.*PREFIX=\(([a-zA-Z]+)\)([^\s]+).*$/',$settings,$matches)) {				$connection['server']['modes']['umodes'] = $matches[1];				$connection['server']['modes']['prefixes'] = $matches[2];			}			if (preg_match('/^.*CHANMODES=([^\s]+).*$/',$settings,$matches)) {				$modelist = strrev($matches[1]);				if ($comma = strpos($modelist,",")) {					$modes = explode(",",$modelist,2);					$connection['server']['modes']['cmodes'] = $modes[0];					$connection['server']['modes']['camodes'] = str_replace(",","",$modes[1]);				}				else {					$connection['server']['modes']['cmodes'] = $modeslist;				}			}			break;		/* ### JOIN ###		On JOIN, check for self: if so, create arrays for channel; if someone else, add them to the channel userlist */		case 'JOIN':			$channel = $connection['buffer']['target'];			$nick = $connection['buffer']['usernick'];			if ($nick == $connection['settings']['nick']) {				raw("MODE " . $channel);				$connection['channels'][$channel] = array();				$connection['channels'][$channel]['topic'] = array();				$connection['channels'][$channel]['names'] = 0;			}			else {				$connection['channels'][$channel]['users'][$nick] = "1";			}			break;		/* ### PART, KICK and QUIT ###		On PART, unset nick value in channel; On QUIT, unset nick value in all channels */		case 'PART':			$channel = $connection['buffer']['target'];			$nick = $connection['buffer']['usernick'];			if ($nick == $connection['settings']['nick']) {				unset($connection['channels'][$channel]);			}			else {				unset($connection['channels'][$channel]['users'][$nick]);			}			break;		case 'KICK':			$channel = $connection['buffer']['target'];			$args = explode(" ",$connection['buffer']['arguments'],2);			$nick = $args[0];			if ($nick == $connection['settings']['nick']) {				unset($connection['channels'][$channel]);			}			else {				unset($connection['channels'][$channel]['users'][$nick]);			}			if (array_search($channel,$config['channels'])) {				raw('JOIN ' . $channel);			}			break;		case 'QUIT':			$nick = $connection['buffer']['usernick'];			if ($nick != $connection['settings']['nick']) {				foreach ($connection['channels'] as $channel => $data) {					if (isset($connection['channels'][$channel]['users'][$nick])) {						unset($connection['channels'][$channel]['users'][$nick]);					}				}			}			break;		/* ### NICK ###		On NICK, if self, change settings; if someone else, copy and unset nick value in all channels */		case 'NICK':			$nick = $connection['buffer']['usernick'];			$newnick = $connection['buffer']['target'];			if ($nick == $connection['settings']['nick']) {				$connection['settings']['nick'] = $newnick;			}			foreach ($connection['channels'] as $channel => $data) {				if (isset($connection['channels'][$channel]['users'][$nick])) {					$connection['channels'][$channel]['users'][$newnick] = $connection['channels'][$channel]['users'][$nick];					unset($connection['channels'][$channel]['users'][$nick]);				}			}			break;		/* ### /NAMES list ###		When given /NAMES list, mark channel as compiling list and clear if first list, add nicks to channel, add umodes to users */		case '353':			$list = substr($connection['buffer']['arguments'],strpos($connection['buffer']['arguments'],"#"));			$channel = substr($list,0,strpos($list," "));			if ($connection['channels'][$channel]['names'] = 0) {				$connection['channels'][$channel]['users'] = array();				$connection['channels'][$channel]['names'] = 1;			}			$nicks = substr($list,strpos($list,":")+1);			$nicks = explode(" ",$nicks);			foreach ($nicks as $nick) {				$umode = "1";				$pattern = '/^[' . $connection['server']['modes']['prefixes'] . '].*$/';				if (preg_match($pattern,$nick)) {					$prefix = substr($nick,0,1);					$ppos = strpos($connection['server']['modes']['prefixes'],$prefix);					$mode = substr($connection['server']['modes']['umodes'],$ppos,1);					$umode .= $mode;					$nick = substr($nick,1);				}				$connection['channels'][$channel]['users'][$nick] = $umode;			}			break;		/* ### End of /NAMES list ###		Mark channel as not compiling list */		case '356':			$channel = $connection['buffer']['target'];			$connection['channels'][$channel]['names'] = 0;						break;		/* ### Channel modes list ###		Set channel modes */		case '324':			$channel = substr($connection['buffer']['arguments'],0,strpos($connection['buffer']['arguments']," "));			$modelist = substr($connection['buffer']['arguments'],strpos($connection['buffer']['arguments']," ")+1);			$space = strpos($modelist," ");			if ($space !== false) {				$modelist = substr($modelist,0,$space);			}			$connection['channels'][$channel]['modes'] = substr($modelist,1);			break;		/* ### Topic ###		Set channel topic, time and name */		case '332':			$arguments = explode(" :",$connection['buffer']['arguments'],2);			$channel = $arguments[0];			$topic = $arguments[1];			$connection['channels'][$channel]['topic']['topic'] = $topic;			break;		case '333':			$arguments = explode(" ",$connection['buffer']['arguments'],3);			$channel = $arguments[0];			$name = $arguments[1];			$time = $arguments[2];			$connection['channels'][$channel]['topic']['name'] = $name;			$connection['channels'][$channel]['topic']['time'] = $time;			break;		/* ### MODE ###		Parse and change mode settings for channel / users */		case 'MODE':			$channel = $connection['buffer']['target'];			$modelist = explode(" ",$connection['buffer']['arguments']);			$modes = $modelist[0];			$posplus = strpos($modes,"+");			$posneg = strpos($modes,"-");			if ($posneg !== false && $posplus !== false) {				if ($posneg < $posplus) {					$modesneg = str_split(substr($modes,$posneg+1,$posplus-$posneg-1));					$modesplus = str_split(substr($modes,$posplus+1));				}				elseif ($posneg > $posplus) {					$modesplus = str_split(substr($modes,$posplus+1,$posneg-$posplus-1));					$modesneg = str_split(substr($modes,$posneg+1));				}			}			elseif ($posneg !== false) {				$modesneg = str_split(substr($modes,$posneg+1));			}			elseif ($posplus !== false) {				$modesplus = str_split(substr($modes,$posplus+1));			}			$i = 1;			if (isset($modesneg)) {				foreach ($modesneg as $mode) {					if (strpos($connection['server']['modes']['cmodes'],$mode) !== false) {						$connection['channels'][$channel]['modes'] = str_replace($mode,"",$connection['channels'][$channel]['modes']);					}					elseif (strpos($connection['server']['modes']['camodes'],$mode) !== false) {						$connection['channels'][$channel]['modes'] = str_replace($mode,"",$connection['channels'][$channel]['modes']);						$i++;					}					elseif (strpos($connection['server']['modes']['umodes'],$mode) !== false) {						if (isset($connection['channels'][$channel]['users'][$modelist[$i]])) {							$connection['channels'][$channel]['users'][$modelist[$i]] = str_replace($mode,"",$connection['channels'][$channel]['users'][$modelist[$i]]);						}						$i++;					}				}			}			if (isset($modesplus)) {				foreach ($modesplus as $mode) {					if (strpos($connection['server']['modes']['cmodes'],$mode) !== false) {						$connection['channels'][$channel]['modes'] .= $mode;					}					elseif (strpos($connection['server']['modes']['camodes'],$mode) !== false) {						$connection['channels'][$channel]['modes'] .= $mode;						$i++;					}					elseif (strpos($connection['server']['modes']['umodes'],$mode) !== false) {						if (isset($connection['channels'][$channel]['users'][$modelist[$i]])) {							$connection['channels'][$channel]['users'][$modelist[$i]] .= $mode;						}						$i++;					}				}			}			break;		/* ### PRIVMSG ###		Will create command response code later, for now just debugging */		case 'PRIVMSG':			if ($connection['buffer']['arguments'] == '!debug' && $connection['buffer']['hostname'] == $config['owner']['hostname']) {				print_r($connection);				if (is_resource($logfile)) {					fputs($logfile,"\n" . print_r($connection,1) . "\n\n");				}			}			break;	}}?>