" . $server_address . " ~ Connecting to $server_address:$server_port... please wait a few\n"; file_put_contents("users/$username.log", $socketFileContents . $msgline); // Connect to IRC server via socket above socket_connect($socket, $server_address, $server_port); sleep(2); // NICK and USER calls to IRC $nickline = "NICK " . $usernickname . "\n"; $userline = "USER " . $usernickname . " 0 * :" . $username . "'s $title $version client\n"; // Pass NICK and USER back to IRC server over socket socket_write($socket, $nickline, strlen($nickline)); // Sleep for a second before sending USER lineage sleep(1); // SEND IT socket_write($socket, $userline, strlen($userline)); //sleep(1); // Continue the rest of the script here // While script will continue as long as socket continues to be active while($bytes = socket_recv($socket, $r_data, 2048, MSG_DONTWAIT) !== '') { if($bytes !== FALSE) { //$data = socket_read($socket, 2048, PHP_NORMAL_READ); $packetdata = $r_data; } // If client sent something, push it to the IRC server! if(file_exists("users/.$username.push")) { // Grab IRC command from server.php $pushFile = file_get_contents("users/.$username.push"); // Push this / these commands to socket socket_write($socket, $pushFile, strlen($pushFile)); // Remove the push file unlink("users/.$username.push"); } // Check if web client still up, if no pong after 15 seconds, connection closed. if(!file_exists("users/.$username.pingfile")) { // If file is missing, quit // Debug logging, check if IRC is exiting properly doLog("Exiting, $username logged out..."); $quitline = "QUIT :$username toggled disconnect; $title $version\n"; // IRC QUIT line socket_write($socket, $quitline, strlen($quitline)); // Push to socket socket_close($socket); // Close the socket exit(0); // Exit the script, nothing to do. } // If data variable is set and buffer has data to recieve // RECIEVE IT! if(isset($packetdata)) { // If data variable is set, there's data from socket //doLog("Server received chunk: $packetdata"); $lines = explode("\r\n", $packetdata); foreach($lines as $data) { if($data == "") { continue; } //doLog("Line: $data"); $socketFileContents = file_get_contents("users/$username.log"); $stringMsg = explode('PRIVMSG', $data); // Strip IRC commands $ex = explode(' ', $data); $data = htmlentities($data); if($ex[1] == "376" || $ex[1] == "366") { continue; } // Send PONG back to the server if ($ex[0] == "PING") { // Log pong doLog("PONG, $username response..."); $pongline = "PONG " . $ex[1] . "\n"; // PONG IRC CMD // Push to IRC server via socket. socket_write($socket, $pongline, strlen($pongline)); } else if ($ex[1] == "353") { // Userlist has been captured, lets generate it for the client. // Grabs raw user list $userlistContent = "" . $ex[4] . "
"; $userlistRaw = explode($ex[4] . " :", $data); $userArray = explode(" ", $userlistRaw[1]); // Generate a list of users, in HTML format for userlist foreach($userArray as $usi) { $userlistContent .= "$usi
"; } file_put_contents("users/$username.userlist", $userlistContent); } else if ($ex[1] == "PART") { $senderNick = get_string_between($data, ":", "!"); $senderIp = get_string_between($data, "@", " "); $exitMsg = explode('PART', $data); $partChannel = explode(' :', $exitMsg[1]); $msgline = "$senderNick: Leaving
$senderIp left " . stripslashes(trim($exitMsg[1])) . ""; file_put_contents("users/$username.log", $socketFileContents . $msgline); file_put_contents("users/.$username.push", "NAMES " . $partChannel[0] . "\n"); } else if ($ex[1] == "JOIN") { $senderNick = get_string_between($data, ":", "!"); $senderIp = get_string_between($data, "@", " "); $joinChannel = explode("JOIN :", $data); if($senderNick != $usernickname) { $msgline = "$senderNick:" . $joinChannel[1] . "
$senderIp joined " . $joinChannel[1] . "\n"; file_put_contents("users/$username.log", $socketFileContents . $msgline); } else { $msgline = "" . $server_address . " ~ Joining " . $joinChannel[1] . "\n"; file_put_contents("users/$username.log", $socketFileContents . $msgline); } file_put_contents("users/.$username.push", "NAMES " . $joinChannel[1] . "\n"); } else if ($ex[1] == "NICK") { $senderNick = get_string_between($data, ":", "!"); $senderIp = get_string_between($data, "@", " "); $nickMsg = explode('NICK :', $data); $msgline = "$senderNick:" . trim($nickMsg[1]); $msgline .= "
$senderIp"; $msgline .= " $senderNick is now known as" . trim($nickMsg[1]); $msgline .= "\n"; file_put_contents("users/$username.log", $socketFileContents . $msgline); } else if ($ex[1] == "QUIT") { $senderNick = get_string_between($data, ":", "!"); $senderIp = get_string_between($data, "@", " "); $quitMsg = explode('QUIT :', $data); $msgline = "$senderNick
$senderIp $senderNick left: " . trim($quitMsg[1]) . "\n"; file_put_contents("users/$username.log", $socketFileContents . $msgline); file_put_contents("users/.$username.push", "NAMES"); } else if ($ex[2] == $usernickname && $ex[1] == "PRIVMSG") { $senderNick = get_string_between($data, ":", "!"); $senderIp = get_string_between($data, "@", " "); $privMsg = explode($usernickname . " :", $stringMsg[1]); file_put_contents("users/.$username.pmed", "$senderNick"); $msgline = "PM from $senderNick
$senderIp " . htmlentities(stripslashes(trim($privMsg[1]))) . "\n"; file_put_contents("users/$username.log", $socketFileContents . $msgline); $msg = ""; } else if ($stringMsg[1] != "") { $senderNick = get_string_between($data, ":", "!"); $senderIp = get_string_between($data, "@", " "); $channel = explode(" :", $stringMsg[1]); $msg = explode($channel[0] . " :", $stringMsg[1]); $msgline = "$senderNick:" . $channel[0] . "
$senderIp " . htmlentities(stripslashes(trim($msg[1]))) . ""; if(usernameInMsg($usernickname, $msg[1])==true) { file_put_contents("users/.$username.mentioned", "$senderNick"); } file_put_contents("users/$username.log", $socketFileContents . $msgline); } else if ($ex[0] == ":$server_address") { $msgline = "" . $server_address . " ~ " . $data . "\n"; file_put_contents("users/$username.log", $socketFileContents . $msgline); } } } // second sleep to prevent insane CPU load usleep(250000); } ?>