irc.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. ///////
  3. // webirc-client - 2013-2020
  4. // (C) Chris Dorman, GPL v3
  5. // https://notabug.org/Pentium44/ircchat
  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. // If username isn't set, exit with error.
  17. if(!isset($username) || $username == "") {
  18. echo "Username not given...";
  19. exit(1);
  20. }
  21. // Create a socket to use
  22. $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  23. // Connect to IRC server via socket above
  24. socket_connect($socket, $server, $port);
  25. // NICK and USER calls to IRC
  26. $nickline = "NICK " . $username . "\n";
  27. $userline = "USER " . $username . " 0 * :" . $username . "'s Bot\n";
  28. // Pass NICK and USER back to IRC server over socket
  29. socket_write($socket, $nickline, strlen($nickline));
  30. socket_write($socket, $userline, strlen($userline));
  31. sleep(1);
  32. // Continue the rest of the script here
  33. // While script will continue as long as socket continues to be active
  34. while($bytes = socket_recv($socket, $r_data, 2048, MSG_DONTWAIT) !== '') {
  35. if($bytes !== FALSE) {
  36. //$data = socket_read($socket, 2048, PHP_NORMAL_READ);
  37. $data = $r_data;
  38. }
  39. // If client sent something, push it to the IRC server!
  40. if(file_exists(".$username.push")) {
  41. // Grab IRC command from server.php
  42. $pushFile = file_get_contents(".$username.push");
  43. // Push this / these commands to socket
  44. socket_write($socket, $pushFile, strlen($pushFile));
  45. // Remove the push file
  46. unlink(".$username.push");
  47. }
  48. // Check if web client still up, if no pong after 15 seconds, connection closed.
  49. if(!file_exists(".$username.pingfile")) { // If file is missing, quit
  50. // Debug logging, check if IRC is exiting properly
  51. doLog("Exiting, $username logged out...");
  52. $quitline = "QUIT :$username left the web client\n"; // IRC QUIT line
  53. socket_write($socket, $quitline, strlen($quitline)); // Push to socket
  54. socket_close($socket); // Close the socket
  55. exit(0); // Exit the script, nothing to do.
  56. } else if (date("YmdHis.", filemtime(".$username.pingfile"))<(date("YmdHis.", filemtime(".$username.pingfile"))-10)) {
  57. // Debug logging, check if IRC is exiting properly
  58. doLog("Exiting, $username timed out...");
  59. $quitline = "QUIT :$username's web session timed out\n"; // IRC QUIT line
  60. socket_write($socket, $quitline, strlen($quitline)); // Push to socket
  61. socket_close($socket); // Close the socket
  62. exit(1); // Exit the script, nothing to do.
  63. }
  64. // If data variable is set and buffer has data to recieve
  65. // RECIEVE IT!
  66. if(isset($data)) { // If data variable is set, there's data from socket
  67. $stringMsg = explode('PRIVMSG', $data); // Strip IRC commands
  68. // Get original contents from socket
  69. $socketFileContents = file_get_contents(".$username.socket");
  70. // Push all content to
  71. file_put_contents(".$username.socket", $socketFileContents . $data);
  72. $ex = explode(' ', $data);
  73. // Send PONG back to the server
  74. if ($ex[0] == "PING") {
  75. // Log pong
  76. doLog("PONG, $username response...");
  77. $pongline = "PONG " . $ex[1] . "\n"; // PONG IRC CMD
  78. // Push to IRC server via socket.
  79. socket_write($socket, $pongline, strlen($pongline));
  80. }
  81. }
  82. // Half second sleep to prevent insane CPU load
  83. usleep(500);
  84. }
  85. ?>