ircchat/irc.php
2020-11-09 10:44:42 -08:00

90 lines
2.5 KiB
PHP
Executable File

<?php
// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);
include_once("config.php");
// Change these values!
$username = $argv[1];
if(!isset($username) || $username == "") {
echo "Username not given...";
exit(1);
}
echo "Starting client...";
file_put_contents(".$username.socket", "Welcome");
chmod(".$username.socket", 0777);
file_put_contents(".$username.pingfile", "pong");
chmod(".$username.pingfile", 0777);
// Opening the socket to the Rizon network
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
socket_connect($socket, $server, $port);
// Send auth info
// fputs($socket, "PASS " . $password . "\n");
echo "Connected, sending nickname to IRC server...";
$nickline = "NICK " . $username . "\n";
$userline = "USER " . $username . " 0 * :" . $username . "'s Bot\n";
socket_write($socket, $nickline, strlen($nickline));
socket_write($socket, $userline, strlen($userline));
// Join channel
foreach($channels as $channel) {
$channelline = "JOIN " . $channel . "\n";
socket_write($socket, $channelline, strlen($channelline));
}
// Force an endless while
while (1) {
// Continue the rest of the script here
while($bytes = socket_recv($socket, $r_data, 2048, MSG_DONTWAIT) !== '') {
if($bytes !== FALSE) {
//$data = socket_read($socket, 2048, PHP_NORMAL_READ);
$data = $r_data;
}
// If client sent something, push it to the IRC server!
if(file_exists(".$username.push")) {
$pushFile = file_get_contents(".$username.push");
socket_write($socket, $pushFile, strlen($pushFile));
unlink(".$username.push");
}
// Check if web client still up, if no pong after 15 seconds, connection closed.
if(!file_exists(".$username.pingfile") || (date("YmdHis.", filemtime(".$username.pingfile"))<(date("YmdHis.", filemtime(".$username.pingfile"))-10))) {
echo "Exiting, leaving IRC server...";
$quitline = "QUIT :Timed out\n";
socket_write($socket, $quitline, strlen($quitline));
socket_close($socket);
exit(1);
}
if(isset($data)) {
$stringMsg = explode('PRIVMSG', $data);
$socketFileContents = file_get_contents(".$username.socket");
file_put_contents(".$username.socket", $socketFileContents . $data);
$ex = explode(' ', $data);
// Send PONG back to the server
if ($ex[0] == "PING") {
echo "PONG to IRC server...";
$pongline = "PONG " . $ex[1] . "\n";
socket_write($socket, $pongline, strlen($pongline));
}
}
usleep(500);
}
}
?>