Oppaitime's version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

irc.class.php 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. <?php
  2. class IRC_DB extends DB_MYSQL
  3. {
  4. public function halt($Msg)
  5. {
  6. global $Bot;
  7. $Bot->send_to($Bot->get_channel(), 'The database is currently unavailable; try again later.');
  8. }
  9. }
  10. abstract class IRC_BOT
  11. {
  12. abstract protected function connect_events();
  13. abstract protected function channel_events();
  14. abstract protected function query_events();
  15. abstract protected function irc_events();
  16. abstract protected function listener_events();
  17. protected $Debug = false;
  18. protected $Socket = false;
  19. protected $Data = false;
  20. protected $Whois = false;
  21. protected $Identified = [];
  22. protected $Channels = [];
  23. protected $Messages = [];
  24. protected $LastChan = false;
  25. protected $ListenSocket = false;
  26. protected $Listened = false;
  27. protected $Connecting = false;
  28. protected $State = 1; // Drone is live
  29. public $Restart = 0; // Die by default
  30. public function __construct()
  31. {
  32. if (isset($_SERVER['HOME']) && is_dir($_SERVER['HOME']) && getcwd() != $_SERVER['HOME']) {
  33. chdir($_SERVER['HOME']);
  34. }
  35. ob_end_clean();
  36. restore_error_handler(); //Avoid PHP error logging
  37. set_time_limit(0);
  38. }
  39. public function connect()
  40. {
  41. $this->connect_irc();
  42. $this->connect_listener();
  43. $this->post_connect();
  44. }
  45. private function connect_irc($Reconnect = false)
  46. {
  47. $this->Connecting = true;
  48. //Open a socket to the IRC server
  49. if (defined('BOT_PORT_SSL')) {
  50. $IrcAddress = 'tls://' . BOT_SERVER . ':' . BOT_PORT_SSL;
  51. } else {
  52. $IrcAddress = 'tcp://' . BOT_SERVER . ':' . BOT_PORT;
  53. }
  54. while (!$this->Socket = stream_socket_client($IrcAddress, $ErrNr, $ErrStr)) {
  55. sleep(15);
  56. }
  57. stream_set_blocking($this->Socket, 0);
  58. $this->Connecting = false;
  59. if ($Reconnect) {
  60. $this->post_connect();
  61. }
  62. }
  63. private function connect_listener()
  64. {
  65. //create a socket to listen on
  66. $ListenAddress = 'tcp://' . SOCKET_LISTEN_ADDRESS . ':' . SOCKET_LISTEN_PORT;
  67. if (!$this->ListenSocket = stream_socket_server($ListenAddress, $ErrNr, $ErrStr)) {
  68. die("Cannot create listen socket: $ErrStr");
  69. }
  70. stream_set_blocking($this->ListenSocket, false);
  71. }
  72. private function post_connect()
  73. {
  74. fwrite($this->Socket, "NICK ".BOT_NICK."Init\n");
  75. fwrite($this->Socket, "USER ".BOT_NICK." * * :IRC Bot\n");
  76. $this->listen();
  77. }
  78. public function disconnect()
  79. {
  80. fclose($this->ListenSocket);
  81. $this->State = 0; //Drones dead
  82. }
  83. public function get_channel()
  84. {
  85. preg_match('/.+ PRIVMSG ([^:]+) :.+/', $this->Data, $Channel);
  86. if (preg_match('/#.+/', $Channel[1])) {
  87. return $Channel[1];
  88. } else {
  89. return false;
  90. }
  91. }
  92. public function get_nick()
  93. {
  94. preg_match('/:([^!:]+)!.+@[^\s]+ PRIVMSG [^:]+ :.+/', $this->Data, $Nick);
  95. return $Nick[1];
  96. }
  97. protected function get_message()
  98. {
  99. preg_match('/:.+ PRIVMSG [^:]+ :(.+)/', $this->Data, $Msg);
  100. return trim($Msg[1]);
  101. }
  102. protected function get_irc_host()
  103. {
  104. preg_match('/:[^!:]+!.+@([^\s]+) PRIVMSG [^:]+ :.+/', $this->Data, $Host);
  105. return trim($Host[1]);
  106. }
  107. protected function get_word($Select = 1)
  108. {
  109. preg_match('/:.+ PRIVMSG [^:]+ :(.+)/', $this->Data, $Word);
  110. $Word = preg_split(' ', $Word[1]);
  111. return trim($Word[$Select]);
  112. }
  113. protected function get_action()
  114. {
  115. preg_match('/:.+ PRIVMSG [^:]+ :!(\S+)/', $this->Data, $Action);
  116. return strtoupper($Action[1]);
  117. }
  118. protected function send_raw($Text)
  119. {
  120. if (!feof($this->Socket)) {
  121. fwrite($this->Socket, "$Text\n");
  122. } elseif (!$this->Connecting) {
  123. $this->Connecting = true;
  124. sleep(120);
  125. $this->connect_irc(true);
  126. }
  127. }
  128. public function send_to($Channel, $Text)
  129. {
  130. // split the message up into <= 460 character strings and send each individually
  131. // this is used to prevent messages from getting truncated
  132. $Text = wordwrap($Text, 460, "\n", true);
  133. $TextArray = explode("\n", $Text);
  134. foreach ($TextArray as $Text) {
  135. $this->send_raw("PRIVMSG $Channel :$Text");
  136. }
  137. }
  138. protected function whois($Nick)
  139. {
  140. $this->Whois = $Nick;
  141. $this->send_raw("WHOIS $Nick");
  142. }
  143. /*
  144. This function uses blacklisted_ip, which is no longer in RC2.
  145. You can probably find it in old RC1 code kicking aronud if you need it.
  146. protected function ip_check($IP, $Gline = false, $Channel = BOT_REPORT_CHAN) {
  147. if (blacklisted_ip($IP)) {
  148. $this->send_to($Channel, 'TOR IP Detected: '.$IP);
  149. if ($Gline) {
  150. $this->send_raw('GLINE *@'.$IP.' 90d :DNSBL Proxy');
  151. }
  152. }
  153. if (Tools::site_ban_ip($IP)) {
  154. $this->send_to($Channel, 'Site IP Ban Detected: '.$IP);
  155. if ($Gline) {
  156. $this->send_raw('GLINE *@'.$IP.' 90d :IP Ban');
  157. }
  158. }
  159. }*/
  160. protected function listen()
  161. {
  162. G::$Cache->InternalCache = false;
  163. stream_set_timeout($this->Socket, 10000000000);
  164. while ($this->State == 1) {
  165. $NullSock = null;
  166. $Sockets = array($this->Socket, $this->ListenSocket);
  167. if (stream_select($Sockets, $NullSock, $NullSock, null) === false) {
  168. die();
  169. }
  170. foreach ($Sockets as $Socket) {
  171. if ($Socket === $this->Socket) {
  172. $this->irc_events();
  173. } else {
  174. $this->Listened = stream_socket_accept($Socket);
  175. $this->listener_events();
  176. }
  177. }
  178. G::$DB->LinkID = false;
  179. G::$DB->Queries = [];
  180. }
  181. }
  182. }