irc.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. ///////
  3. // IdleIRC - 2020
  4. // (C) Chris Dorman, GPLv3
  5. // https://notabug.org/Pentium44/idleirc
  6. ///////
  7. // irc.php - used to push and pull data from IRC server.
  8. // Currently supports PING / PONG, data receive, and data push
  9. // Done via PHP sockets.
  10. // Prevent PHP from stopping the script after 30 sec
  11. set_time_limit(0);
  12. // Include variables
  13. include_once("config.php");
  14. // Get username from command line argument / PHP-CLI
  15. $username = $argv[1];
  16. $servaddr = $argv[2]; // If server address is specified
  17. $servport = $argv[3]; // If server port is specified
  18. if(file_exists("users/$username.php")) {
  19. include("users/$username.php");
  20. }
  21. $server_address = isset($servaddr) ? $servaddr : $server;
  22. $server_port = isset($servport) ? $servport : $port;
  23. function usernameInMsg($username, $msgString) {
  24. if(strpos($msgString, $username) !== false){
  25. return true;
  26. } else{
  27. return false;
  28. }
  29. }
  30. // Function to search for username
  31. function get_string_between($string, $start, $end){
  32. $string = ' ' . $string;
  33. $ini = strpos($string, $start);
  34. if ($ini == 0) return '';
  35. $ini += strlen($start);
  36. $len = strpos($string, $end, $ini) - $ini;
  37. return substr($string, $ini, $len);
  38. }
  39. // If username isn't set, exit with error.
  40. if(!isset($username) || $username == "") {
  41. echo "Username not given...";
  42. exit(1);
  43. }
  44. // Create a socket to use
  45. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  46. // Connect to IRC server via socket above
  47. socket_connect($socket, $server_address, $server_port);
  48. // NICK and USER calls to IRC
  49. $nickline = "NICK " . $usernickname . "\n";
  50. $userline = "USER " . $usernickname . " 0 * :" . $username . "'s $title $version client\n";
  51. // Pass NICK and USER back to IRC server over socket
  52. socket_write($socket, $nickline, strlen($nickline));
  53. socket_write($socket, $userline, strlen($userline));
  54. sleep(1);
  55. // Continue the rest of the script here
  56. // While script will continue as long as socket continues to be active
  57. while($bytes = socket_recv($socket, $r_data, 3068, MSG_DONTWAIT) !== '') {
  58. if($bytes !== FALSE) {
  59. //$data = socket_read($socket, 2048, PHP_NORMAL_READ);
  60. $data = $r_data;
  61. }
  62. // If client sent something, push it to the IRC server!
  63. if(file_exists("users/.$username.push")) {
  64. // Grab IRC command from server.php
  65. $pushFile = file_get_contents("users/.$username.push");
  66. // Push this / these commands to socket
  67. socket_write($socket, $pushFile, strlen($pushFile));
  68. // Remove the push file
  69. unlink("users/.$username.push");
  70. }
  71. // Check if web client still up, if no pong after 15 seconds, connection closed.
  72. if(!file_exists("users/.$username.pingfile")) { // If file is missing, quit
  73. // Debug logging, check if IRC is exiting properly
  74. doLog("Exiting, $username logged out...");
  75. $quitline = "QUIT :$username toggled disconnect; webirc\n"; // IRC QUIT line
  76. socket_write($socket, $quitline, strlen($quitline)); // Push to socket
  77. socket_close($socket); // Close the socket
  78. exit(0); // Exit the script, nothing to do.
  79. }
  80. // If data variable is set and buffer has data to recieve
  81. // RECIEVE IT!
  82. if(isset($data)) { // If data variable is set, there's data from socket
  83. $stringMsg = explode('PRIVMSG', $data); // Strip IRC commands
  84. // Get original contents from socket
  85. $socketFileContents = file_get_contents("users/$username.log");
  86. $ex = explode(' ', $data);
  87. $data = htmlentities($data);
  88. // Send PONG back to the server
  89. if ($ex[0] == "PING") {
  90. // Log pong
  91. doLog("PONG, $username response...");
  92. $pongline = "PONG " . $ex[1] . "\n"; // PONG IRC CMD
  93. // Push to IRC server via socket.
  94. socket_write($socket, $pongline, strlen($pongline));
  95. } else if ($ex[1] == "PART") {
  96. $senderNick = get_string_between($data, ":", "!");
  97. $senderIp = get_string_between($data, "@", " ");
  98. $exitMsg = explode('PART', $data);
  99. $msgline = "<tr><td class='userinfo'><b>$senderNick</b><br /><span style='color:$ipcolor;'>$senderIp</span></td><td> left " . stripslashes(trim($exitMsg[1])) . "</td></tr>";
  100. file_put_contents("users/$username.log", $socketFileContents . $msgline);
  101. } else if ($ex[1] == "JOIN") {
  102. $senderNick = get_string_between($data, ":", "!");
  103. $senderIp = get_string_between($data, "@", " ");
  104. if($senderNick != $usernickname) {
  105. $msgline = "<tr><td class='userinfo'><b>$senderNick</b><br /><span style='color:$ipcolor;'>$senderIp</span></td><td> joined the channel</td></tr>\n";
  106. file_put_contents("users/$username.log", $socketFileContents . $msgline);
  107. } else {
  108. $msgline = "<tr><td class='userinfo'><span style='color:$ipcolor;'>" . $server_address . "</span> ~ </td><td> " . $data . "</td></tr>\n";
  109. file_put_contents("users/$username.log", $socketFileContents . $msgline);
  110. }
  111. } else if ($ex[1] == "NICK") {
  112. $senderNick = get_string_between($data, ":", "!");
  113. $senderIp = get_string_between($data, "@", " ");
  114. $nickMsg = explode('NICK', $data);
  115. $msgline = "<tr><td class='userinfo'><b>$senderNick</b><br /><span style='color:$ipcolor;'>$senderIp</span></td><td> $senderNick is now known as" . trim($nickMsg[1]) . "</td></tr>\n";
  116. file_put_contents("users/$username.log", $socketFileContents . $msgline);
  117. } else if ($ex[1] == "QUIT") {
  118. $senderNick = get_string_between($data, ":", "!");
  119. $senderIp = get_string_between($data, "@", " ");
  120. $quitMsg = explode('QUIT :', $data);
  121. $msgline = "<tr><td class='userinfo'><span style='color:$ipcolor;'>$server_address</span> ~</td><td> $senderNick left: " . trim($quitMsg[1]) . "</td></tr>\n";
  122. file_put_contents("users/$username.log", $socketFileContents . $msgline);
  123. } else if ($ex[2] == $usernickname && $ex[1] == "PRIVMSG") {
  124. $senderNick = get_string_between($data, ":", "!");
  125. $senderIp = get_string_between($data, "@", " ");
  126. $privMsg = explode($usernickname . " :", $stringMsg[1]);
  127. file_put_contents("users/.$username.pmed", "$senderNick");
  128. $msgline = "<tr><td class='userinfo'>PM from: <b>$senderNick</b><br /><span style='color:$ipcolor;'>$senderIp</span></td><td> " . htmlentities(stripslashes(trim($privMsg[1]))) . "</td></tr>\n";
  129. file_put_contents("users/$username.log", $socketFileContents . $msgline);
  130. $msg = "";
  131. } else if ($stringMsg[1] != "") {
  132. $senderNick = get_string_between($data, ":", "!");
  133. $senderIp = get_string_between($data, "@", " ");
  134. $channel = explode(" :", $stringMsg[1]);
  135. $msg = explode($channel[0] . " :", $stringMsg[1]);
  136. $msgline = "<tr><td class='userinfo'><b>$senderNick</b>:" . $channel[0] . "<br /><span style='color:$ipcolor;'>$senderIp</span></td><td> " . htmlentities(stripslashes(trim($msg[1]))) . "</td></tr>";
  137. if(usernameInMsg($usernickname, $msg[1])==true) {
  138. file_put_contents("users/.$username.mentioned", "$senderNick");
  139. }
  140. file_put_contents("users/$username.log", $socketFileContents . $msgline);
  141. } else if ($ex[0] != "PING") {
  142. $msgline = "<tr><td class='userinfo'><span style='color:$ipcolor;'>" . $server_address . "</span> ~ </td><td> " . $data . "</td></tr>\n";
  143. file_put_contents("users/$username.log", $socketFileContents . $msgline);
  144. }
  145. }
  146. // second sleep to prevent insane CPU load
  147. usleep(600);
  148. }
  149. ?>