VB icon

IRC in PHP

Email
Submitted on: 1/1/2015 7:23:00 AM
By: Arnar Mar Sig (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: PHP 4.0
Views: 2100
 
     This code shows you how to create socket and connect to a IRC server and communicate with it. It is well commented and easy to understand. It has some basic IRC commands like PING, CTCP Ping, CTCP Version and you can send raw commands by PRIVMSG. NOTE: You can not "chat" with this program, its more of a bot.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: IRC in PHP
// Description:This code shows you how to create socket and connect to a IRC server and communicate with it. 
It is well commented and easy to understand. It has some basic IRC commands like PING, CTCP Ping, CTCP Version and you can send raw commands by PRIVMSG.
NOTE: You can not "chat" with this program, its more of a bot.
// By: Arnar Mar Sig (from psc cd)
//
// Inputs:none, Just run it in console mode (Worked on win32 and should work on *nix).
Command:
PHP irc.php
where irc.php is the name of the file.
//**************************************

<?
	// Set the PHP Timeout to 0, so we wont get killed by PHP
	set_time_limit(0);
	// define \r\n for easy use
	define ('CRLF', "\r\n");
	// Just some variables we need to connect
	$nick = 'PHPTest'; // The nick
	$username = 'an13810'; // The Username (username@hostname)
	$localhost = 'an13810.ath.cx'; // The localhost, this dosen't really metter, the server will find the right one, or use your IP.
	$remotehost = 'irc.ircnet.is'; // The server we are connecting to
	$realname = 'PHP IRC test'; // Your realname, (real my ass;)
	$channel = '#php.is'; // Channel we join to on connect
	// Open the socket
	$fp = fsockopen($remotehost,6666, &$err_num, &$err_msg, 30);
	if(!$fp) { // Error trying to connect
		print "Sorry, the server is not currently available!";
		exit;
	}
	// Send the connect data (This is a part of the IRC RCF, read it if you are going to code more irc stuff)
	$Header = 'NICK ' . $nick . CRLF;
	$Header .= 'USER ' . $username . ' ' . $localhost . ' ' . $remotehost . ' :' . $realname . CRLF;
	fputs($fp, $Header);
	// define response as a variable, so we wont get a error.
	$response = '';
	while (!feof($fp)) { // Make a while loop untill the socket is closed
		$response .= fgets($fp, 1024); // Append 1024 bytes to $response (if any), from the socket buffer
		while (substr_count($response,CRLF) != 0) { // Check if there is CRLF (linesplit) in $response, and do that untill none
			$offset = strpos($response, CRLF); // Just to know where the line ends
			$data = substr($response,0,$offset); // Split the line from the rest of the data
			$response = substr($response,$offset+2); // Split the rest from the line
			if ( substr($data,0,1) == ':' ) { // If the first char is : then go to this loop
				// Lines starting whit : are in this format
				// :sender command :text
				// So we need to split it like that
				$offsetA = strpos($data, ' '); // Find first space
				$dFrom = substr($data,1,$offsetA-1); // set $dFrom as the sender
				$offsetB = strpos($data, ' :'); // Find the first :
				$dCommand = substr($data,$offsetA+1,$offsetB-$offsetA-1); // Set $dCommand as the command
				$dText = substr($data,$offsetB+2); // set $dText as the text.
				if ( substr($dCommand,0,3) == '004' ) {
					// This is just a part of the connect headers that the server send. (001,002,003,004,005)
					// Some server dont send 005, so i use 004 to know if im connected
					fputs($fp,'JOIN ' . $channel . CRLF); // Join $channel
				}
				elseif ( substr($dCommand,0,7) == 'PRIVMSG' ) {
					// If somebody msgs us, or if there is some tolk on a channal, this is send.
					if ( Ord(substr($dText,0,1)) == 1 ) {
						// If first chars acsii code is 1 then its a CTCP question.
						if ( substr($dText,1,4) == 'PING' ) {
							// Sombody CTCP pinged us, lets respond
							fputs($fp,':' . $nick . ' NOTICE ' . $dFrom . ' :' . chr(1) . 'PING ' . substr($dText,6) . chr(1) . CRLF);
						}
						elseif ( substr($dText,1,7) == 'VERSION' ) {
							// Somebody versiond us, lets respond
							fputs($fp,':' . $nick . ' NOTICE ' . $dFrom . ' :' . chr(1) . 'VERSION PHPirc' . chr(1) . CRLF);
						}
					}
					else {
						// Else, do this. This is just a relay of all privemsg sent to use, will go right to the server.
						// so we can send RAW message for testing:)
						fputs($fp,$dText . CRLF);
					}
				}
				
			}
			elseif ( substr($data,0,4) == 'PING' ) { // Else if first 4 chars are PING do this
				// If the server pings us, respond. This must be done or we will get timeout
				fputs($fp,'PONG ' . substr($data,5) . CRLF); 
			}
		}
	}
	// If we are here, then the server has disconnected use
	
	// Close the socket
	fclose ($fp);	
?>


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.