Public feed
#724925
<html>
<body>
<?php
// Modify the below settings to configure your server connection
$nick = "PHPTest1";
$username = "PHPTestUsername";
$realname = "PHPTestRealname";
$server = "irc.freenode.net";
$port = "6667";
$channel = "#fairserve";
// Open the connection
$handle = fsockopen( gethostbyname( $server ), $port );
if( ! $handle )
die( "could not open socket<br>\n" );
// Handle initial connection stuff
// Register nick and username
fwrite( $handle, ":".$nick." NICK ".$nick."\n" );
fwrite( $handle, ":".$nick." USER ".$username." "."hostname"." "."server"." ".$realname."\n" );
$wantToJoin = true;
ob_start();
// Read the contents, and print them
while (!feof($handle)) {
$read = fgets( $handle, 8096 );
if( preg_match( "/^\s*$/", $read ) ) continue;
// Parse it a little
if( preg_match( "/^(:(\S+)\s)?(\S+)\s((\S+)\s)?((\S+)\s)?:(.*)$/", $read, $matches ) ) {
$sender = $matches[2];
$command = $matches[3];
$object = $matches[5];
$argument = $matches[7];
$content = preg_replace( "/\s+$/", "", $matches[8] ); // strip trailing space
}
echo "<i>".time().":</i> ";
if( $sender )
echo "<font color=blue>".$sender."</font>\n";
echo "<font color=red>".$command."</font> ".
"<font color=grey>".$object."</font> ".
"<font color=purple>".$argument."</font> ".
$content."<br>";
// Don't wait to send it
ob_flush(); flush();
// Done with output, now handle the stuff
if( $command == "433" ) {
// Nick already in use, try another
$nick .= "_";
fwrite( $handle, ":".$nick." NICK ".$nick."\n" );
} else if( $command == "001" && $wantToJoin ) {
// Join a channel
fwrite( $handle, ":".$nick." JOIN ".$channel."\n" );
$wantToJoin = false;
} else if( $command == "JOIN" ) {
// See if this is me joining, or someone else
preg_match( "/(.*)!.*$/", $sender, $nickMatches );
if( $nickMatches[1] == $nick ) {
// We're in the channel; write something
fwrite( $handle, ":".$nick." PRIVMSG ".$channel." :"."Hi!"."\n" );
}
}
// A little bot magic for now
if( $content == "hi" ) {
preg_match( "/(.*)!.*$/", $sender, $nickMatches );
fwrite( $handle, ":".$nick." PRIVMSG ".$channel." :".$nickMatches[1].": wassup?\n" );
} else if( $content == "plzquit" ) {
fwrite( $handle, ":".$nick." QUIT\n" );
}
}
ob_end_flush();
?>
</body>
</html>