connect.php 2.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. //#########################################################
  3. // Part of project php-IRC-js
  4. //
  5. // connect.php - connect to server and send commands
  6. //
  7. // Author: Mario Chorvath - Bedna
  8. // Start 2016
  9. //
  10. // Licence GNU General Public License
  11. // Version 2
  12. // http://www.gnu.org/licenses/old-licenses/gpl-2.0.en.html
  13. //#########################################################
  14. // Settings
  15. $ircServer = "irc.freenode.net";
  16. $ircPort = "6667";
  17. $ircChannel = "#KERNEL_ULTRAS";
  18. // Set variables
  19. $loop = "loop";
  20. // Set unlimite execution time
  21. set_time_limit(0);
  22. // Connect to server
  23. $ircSocket = pfsockopen ($ircServer, $ircPort, $eN, $eS);
  24. // If socket open
  25. if ($ircSocket) {
  26. // Login bot
  27. fwrite ($ircSocket, "USER KU-bot127c 0 lol :Kubo137c\r\n");
  28. fwrite ($ircSocket, "NICK KU-bot137c\r\n");
  29. // Join to channel
  30. fwrite ($ircSocket, "JOIN " . $ircChannel . "\r\n");
  31. // Send message to channel
  32. fwrite ($ircSocket, "PRIVMSG #KERNEL_ULTRAS :Im here\r\n");
  33. // Open file "server_response.txt" for write
  34. $handle = fopen ("server_response.txt", "w");
  35. // Open file for logs
  36. $handle_log = fopen ("log.txt", "w");
  37. // Write log
  38. fwrite ($handle_log, "FIRST\n");
  39. while (1) {
  40. // Write log
  41. fwrite ($handle_log, "MAIN\n");
  42. // Read command from file "send"
  43. if (file_exists ("send")) {
  44. // Read command
  45. $command = file_get_contents ("send");
  46. // Remove file
  47. unlink ("send");
  48. // Timeout "for sure"
  49. sleep (1);
  50. // Write command to socket
  51. fwrite ($ircSocket, "$command"."\r\n");
  52. // Flush command to socket
  53. flush ();
  54. // Write log
  55. fwrite ($handle_log, "SEND :$command\n");
  56. }
  57. // If IRC server send message to client
  58. while ($data = fgets ($ircSocket, 128)) {
  59. // fwrite ($handle_log, "DATA :".$data."\n");
  60. fwrite ($handle_log, "READ\n");
  61. // Separate all data by space
  62. $exData = explode (' ', $data);
  63. // Send PONG back to the server
  64. if ($exData[0] == "PING") {
  65. // Write log
  66. fwrite ($handle_log, "PING :".$exData[1]."\n");
  67. // Write PONG to server
  68. fwrite ($ircSocket, "PONG ".$exData[1]."\r\n");
  69. }
  70. // If response other than PING
  71. else {
  72. // Write response to file "server_response.txt"
  73. fwrite ($handle, nl2br ($data));
  74. }
  75. }
  76. }
  77. }
  78. // If error on connection
  79. else {
  80. echo $eS . ": " . $eN;
  81. }
  82. ?>