\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 "".time().": ";
if( $sender )
echo "".$sender."\n";
echo "".$command." ".
"".$object." ".
"".$argument." ".
$content."
";
// 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();
?>