idleirc/irc.php
2020-12-09 18:32:06 -08:00

158 lines
6.5 KiB
PHP
Executable File

<?php
///////
// IdleIRC - 2020
// (C) Chris Dorman, GPLv3
// https://notabug.org/Pentium44/idleirc
///////
// irc.php - used to push and pull data from IRC server.
// Currently supports PING / PONG, data receive, and data push
// Done via PHP sockets.
// Prevent PHP from stopping the script after 30 sec
set_time_limit(0);
// Include variables
include_once("config.php");
// Get username from command line argument / PHP-CLI
$username = $argv[1];
$servaddr = $argv[2]; // If server address is specified
$servport = $argv[3]; // If server port is specified
$server_address = isset($servaddr) ? $servaddr : $server;
$server_port = isset($servport) ? $servport : $port;
// Function to search for username
function get_string_between($string, $start, $end){
$string = ' ' . $string;
$ini = strpos($string, $start);
if ($ini == 0) return '';
$ini += strlen($start);
$len = strpos($string, $end, $ini) - $ini;
return substr($string, $ini, $len);
}
// If username isn't set, exit with error.
if(!isset($username) || $username == "") {
echo "Username not given...";
exit(1);
}
// Create a socket to use
$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
// Connect to IRC server via socket above
socket_connect($socket, $server_address, $server_port);
// NICK and USER calls to IRC
$nickline = "NICK " . $username . "\n";
$userline = "USER " . $username . " 0 * :" . $username . "'s Bot\n";
// Pass NICK and USER back to IRC server over socket
socket_write($socket, $nickline, strlen($nickline));
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);
$data = $r_data;
}
// If client sent something, push it to the IRC server!
if(file_exists(".$username.push")) {
// Grab IRC command from server.php
$pushFile = file_get_contents(".$username.push");
// Push this / these commands to socket
socket_write($socket, $pushFile, strlen($pushFile));
// Remove the push file
unlink(".$username.push");
}
// Check if web client still up, if no pong after 15 seconds, connection closed.
if(!file_exists(".$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; webirc\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.
} /*else if (date("YmdHis.", filemtime(".$username.pingfile"))<(date("YmdHis.", filemtime(".$username.pingfile"))-10)) {
// Debug logging, check if IRC is exiting properly
doLog("Exiting, $username timed out...");
$quitline = "QUIT :$username's web session timed out\n"; // IRC QUIT line
socket_write($socket, $quitline, strlen($quitline)); // Push to socket
socket_close($socket); // Close the socket
exit(1); // Exit the script, nothing to do.
}*/
// If data variable is set and buffer has data to recieve
// RECIEVE IT!
if(isset($data)) { // If data variable is set, there's data from socket
$stringMsg = explode('PRIVMSG', $data); // Strip IRC commands
// Get original contents from socket
$socketFileContents = file_get_contents("$username.log");
$ex = explode(' ', $data);
$data = htmlentities($data);
// 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] == "PART") {
$senderNick = get_string_between($data, ":", "!");
$senderIp = get_string_between($data, "@", " ");
$exitMsg = explode('PART', $data);
$msgline = "<tr><td class='userinfo'><b>$senderNick</b><br /><span style='color:$ipcolor;'>$senderIp</span></td><td> left " . stripslashes(trim($exitMsg[1])) . "</td></tr>";
file_put_contents("$username.log", $socketFileContents . $msgline);
} else if ($ex[1] == "JOIN") {
$senderNick = get_string_between($data, ":", "!");
$senderIp = get_string_between($data, "@", " ");
$msgline = "<tr><td class='userinfo'><b>$senderNick</b><br /><span style='color:$ipcolor;'>$senderIp</span></td><td> joined the channel</td></tr>\n";
file_put_contents("$username.log", $socketFileContents . $msgline);
} else if ($ex[1] == "NICK") {
$senderNick = get_string_between($data, ":", "!");
$senderIp = get_string_between($data, "@", " ");
$nickMsg = explode('NICK', $data);
$msgline = "<tr><td class='userinfo'><b>$senderNick</b><br /><span style='color:$ipcolor;'>$senderIp</span></td><td> $senderNick is now known as" . $nickMsg[1] . "</td></tr>\n";
file_put_contents("$username.log", $socketFileContents . $msgline);
} else if ($ex[2] == $username && (count(explode(":", $stringMsg[1])) > 1)) {
$senderNick = get_string_between($data, ":", "!");
$senderIp = get_string_between($data, "@", " ");
$privMsg = explode(":", $stringMsg[1]);
$posprivMsg = array_splice($privMsg, 1);
$msg;
foreach($posprivMsg as $msgchunk) {
$msg .= $msgchunk;
}
$msgline = "<tr><td class='userinfo'>PM from: <b>$senderNick</b><br /><span style='color:$ipcolor;'>$senderIp</span></td><td> " . stripslashes(trim($msg)) . "</td></tr>\n";
file_put_contents("$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 = "<tr><td class='userinfo'><b>$senderNick</b>:" . $channel[0] . "<br /><span style='color:$ipcolor;'>$senderIp</span></td><td> " . stripslashes(trim($msg[1])) . "</td></tr>\n";
file_put_contents("$username.log", $socketFileContents . $msgline);
} else if ($ex[0] != "PING") {
$msgline = "<tr><td class='userinfo'><span style='color:$ipcolor;'>" . $server_address . "</span> ~ </td><td> " . $data . "</td></tr>\n";
file_put_contents("$username.log", $socketFileContents . $msgline);
}
}
// second sleep to prevent insane CPU load
usleep(1000);
}
?>