From 54de7a8ea4b2a1a795bddb2f4112c44fe026902b Mon Sep 17 00:00:00 2001 From: Darko Date: Tue, 6 May 2014 15:09:59 +0200 Subject: [PATCH] Trying to swicth to new IRCScraper --- bin/monitor.php | 31 +- lib/IRCClient.php | 722 ++++++++++++++++++++++ lib/IRCScraper.php | 905 +++++----------------------- lib/IRCScraper/scrape.php | 56 +- lib/IRCScraper/settings_example.php | 70 ++- 5 files changed, 971 insertions(+), 813 deletions(-) create mode 100644 lib/IRCClient.php diff --git a/bin/monitor.php b/bin/monitor.php index 50c93fa8f..4656c9c1f 100644 --- a/bin/monitor.php +++ b/bin/monitor.php @@ -9,7 +9,7 @@ require_once(dirname(__FILE__) . "/../lib/showsleep.php"); require_once(dirname(__FILE__) . "/../lib/functions.php"); -$version = "0.3r1141"; +$version = "0.3r1142"; $db = new DB(); $functions = new Functions(); @@ -174,6 +174,9 @@ $killed = "false"; $getdate = gmDate("Ymd"); //get microtime +/** + * @return float + */ function microtime_float() { list($usec, $sec) = explode(" ", microtime()); @@ -181,6 +184,11 @@ function microtime_float() return ((float)$usec + (float)$sec); } +/** + * @param $_time + * + * @return string + */ function relativeTime($_time) { $d[0] = array(1, "sec"); @@ -209,6 +217,11 @@ function relativeTime($_time) return $return; } +/** + * @param $cmd + * + * @return bool + */ function command_exist($cmd) { $returnVal = shell_exec("which $cmd 2>/dev/null"); @@ -216,6 +229,9 @@ function command_exist($cmd) return (empty($returnVal) ? false : true); } +/** + * @return int + */ function get_color() { $from = 1; @@ -234,6 +250,11 @@ function get_color() return $number; } +/** + * @param $bytes + * + * @return string + */ function decodeSize($bytes) { $types = array('B', 'KB', 'MB', 'GB', 'TB'); @@ -243,6 +264,9 @@ function decodeSize($bytes) } //get system load +/** + * @return mixed + */ function get_load() { $load = sys_getloadavg(); @@ -250,6 +274,11 @@ function get_load() return $load[0]; } +/** + * @param $pane + * + * @return string + */ function writelog($pane) { $path = dirname(__FILE__) . "/../logs"; diff --git a/lib/IRCClient.php b/lib/IRCClient.php new file mode 100644 index 000000000..6688bc2ae --- /dev/null +++ b/lib/IRCClient.php @@ -0,0 +1,722 @@ + string(The nick name of the person who posted.), + * 'channel' => string(The channel name.), + * 'message' => string(The message the person posted.) + * ); + * + * @note Used with the processChannelMessages() function. + * @var array + * @access protected + */ + protected $_channelData = array(); + + /** + * Nick name when we log in. + * + * @var string + * @access protected + */ + protected $_nickName; + + /** + * User name when we log in. + * + * @var string + * @access protected + */ + protected $_userName; + + /** + * "Real" name when we log in. + * + * @var string + * @access protected + */ + protected $_realName; + + /** + * Password when we log in. + * + * @var string + * @access protected + */ + protected $_password; + + /** + * List of channels and passwords to join. + * + * @var array + * @access protected + */ + protected $_channels; + + /** + * Last time we received a ping or sent a ping to the server. + * + * @var int + * @access protected + */ + protected $_lastPing; + + /** + * How many times we've tried to reconnect to IRC. + * + * @var int + * @access protected + */ + protected $_currentRetries = 0; + + /** + * Turns on or off debugging. + * + * @var bool + */ + protected $_debug = true; + + /** + * Are we already logged in to IRC? + * + * @var bool + */ + protected $_alreadyLoggedIn = false; + + /** + * Disconnect from IRC. + * + * @access public + */ + public function __destruct() + { + $this->quit(); + } + + /** + * Time before giving up when trying to read or write to the IRC server. + * The default is fine, it will ping the server if the server does not ping us + * within this time to keep the connection alive. + * + * @param int $timeout Seconds. + * + * @access public + */ + public function setSocketTimeout($timeout) + { + if (!is_numeric($timeout)) { + echo 'ERROR: IRC socket timeout must be a number!' . PHP_EOL; + } else { + $this->_socket_timeout = $timeout; + } + } + + /** + * Amount of time to wait before giving up when connecting. + * + * @param int $timeout Seconds. + * + * @access public + */ + public function setConnectionTimeout($timeout) + { + if (!is_numeric($timeout)) { + echo 'ERROR: IRC connection timeout must be a number!' . PHP_EOL; + } else { + $this->_remote_connection_timeout = $timeout; + } + } + + /** + * Amount of times to retry before giving up when connecting. + * + * @param int $retries + * + * @access public + */ + public function setConnectionRetries($retries) + { + if (!is_numeric($retries)) { + echo 'ERROR: IRC connection retries must be a number!' . PHP_EOL; + } else { + $this->_reconnectRetries = $retries; + } + } + + /** + * Amount of time to wait between failed connects. + * + * @param int $delay Seconds. + * + * @access public + */ + public function setReConnectDelay($delay) + { + if (!is_numeric($delay)) { + echo 'ERROR: IRC reconnect delay must be a number!' . PHP_EOL; + } else { + $this->_reconnectDelay = $delay; + } + } + + /** + * Connect to a IRC server. + * + * @param string $hostname Host name of the IRC server (can be a IP or a name). + * @param int $port Port number of the IRC server. + * @param bool $tls Use encryption for the socket transport? (make sure the port is right). + * + * @return bool + * + * @access public + */ + public function connect($hostname, $port = 6667, $tls = false) + { + $this->_alreadyLoggedIn = false; + $transport = ($tls === true ? 'tls' : 'tcp'); + + $socket_string = $transport . '://' . $hostname . ':' . $port; + if ($socket_string !== $this->_remote_socket_string || !$this->_connected()) { + if (!is_string($hostname) || $hostname == '') { + echo 'ERROR: IRC host name must not be empty!' . PHP_EOL; + + return false; + } + + if (!is_numeric($port)) { + echo 'ERROR: IRC port must be a number!' . PHP_EOL; + + return false; + } + + $this->_remote_host = $hostname; + $this->_remote_port = $port; + $this->_remote_transport = $transport; + $this->_remote_tls = $tls; + $this->_remote_socket_string = $socket_string; + + // Try to connect until we run out of retries. + while ($this->_reconnectRetries >= $this->_currentRetries++) { + $this->_initiateStream(); + if ($this->_connected()) { + break; + } else { + // Sleep between retries. + sleep($this->_reconnectDelay); + } + } + } else { + $this->_alreadyLoggedIn = true; + } + + // Set last ping time to now. + $this->_lastPing = time(); + // Reset retries. + $this->_currentRetries = $this->_reconnectRetries; + + return $this->_connected(); + } + + /** + * Log in to a IRC server. + * + * @param string $nickName The nick name - visible in the channel. + * @param string $userName The user name - visible in the host name. + * @param string $realName The real name - visible in the WhoIs. + * @param null $password The password - some servers require a password. + * + * @return bool + * + * @access public + */ + public function login($nickName, $userName, $realName, $password = null) + { + if (!$this->_connected()) { + echo 'ERROR: You must connect to IRC first!' . PHP_EOL; + + return false; + } + + if (empty($nickName) || empty($userName) || empty($realName)) { + echo 'ERROR: nick/user/real name must not be empty!' . PHP_EOL; + + return false; + } + + $this->_nickName = $nickName; + $this->_userName = $userName; + $this->_realName = $realName; + $this->_password = $password; + + if (($password !== null && !empty($password)) && !$this->_writeSocket('PASSWORD ' . $password)) { + return false; + } + + if (!$this->_writeSocket('NICK ' . $nickName)) { + return false; + } + + if (!$this->_writeSocket('USER ' . $userName . ' 0 * :' . $realName)) { + return false; + } + + // Loop over socket buffer until we find "001". + while (true) { + $this->_readSocket(); + + // We got pinged, reply with a pong. + if (preg_match('/^PING\s*:(.+?)$/', $this->_buffer, $matches)) { + $this->_pong($matches[1]); + + } else if (preg_match('/^:(.*?)\s*(\d+).*?(:.+?)?$/', $this->_buffer, $matches)) { + // We found 001, which means we are logged in. + if ($matches[2] == 001) { + $this->_remote_host_received = $matches[1]; + break; + + // We got 464, which means we need to send a password. + } else if ($matches[2] == 464) { + // Before the lower check, set the password : username:password + $tempPass = $userName . ':' . $password; + + // Check if the user has his password in this format: username/server:password + if (preg_match('/^.+?\/.+?:.+?$/', $password)) { + $tempPass = $password; + } + + if ($password !== null && !$this->_writeSocket('PASS ' . $tempPass)) { + return false; + } else if (isset($matches[3]) && strpos(strtolower($matches[3]), 'invalid password')) { + echo 'Invalid password or username for (' . $this->_remote_host . ').'; + + return false; + } + } + //ERROR :Closing Link: kevin123[100.100.100.100] (This server is full.) + } else if (preg_match('/^ERROR\s*:/', $this->_buffer)) { + echo $this->_buffer . PHP_EOL; + + return false; + } + } + + return true; + } + + /** + * Quit from IRC. + * + * @param string $message Optional disconnect message. + * + * @return bool + * + * @access public + */ + public function quit($message = null) + { + if ($this->_connected()) { + $this->_writeSocket('QUIT' . ($message === null ? '' : ' :' . $message)); + } + $this->_closeStream(); + + return $this->_connected(); + } + + /** + * Read the incoming buffer in a loop. + * + * @access public + */ + public function readIncoming() + { + while (true) { + + $this->_readSocket(); + + // If the server pings us, return it a pong. + if (preg_match('/^PING\s*:(.+?)$/', $this->_buffer, $matches)) { + if ($matches[1] === $this->_remote_host_received) { + $this->_pong($matches[1]); + } + + // Check for a channel message. + } else if (preg_match('/^:(?P.+?)\!.+?\s+PRIVMSG\s+(?P#.+?)\s+:\s*(?P.+?)\s*$/', + $this->_stripControlCharacters($this->_buffer), + $matches + ) + ) { + + $this->_channelData = + array( + 'nickname' => $matches['nickname'], + 'channel' => $matches['channel'], + 'message' => $matches['message'] + ); + + $this->processChannelMessages(); + } + + // Ping the server if it has not sent us a ping in a while. + if ((time() - $this->_lastPing) > ($this->_socket_timeout / 2)) { + $this->_ping($this->_remote_host_received); + } + } + } + + /** + * Join a channel or multiple channels. + * + * @param array $channels Array of channels with their passwords (null if the channel doesn't need a password). + * array( '#exampleChannel' => 'thePassword', '#exampleChan2' => null ); + * + * @return bool + * + * @access public + */ + public function joinChannels($channels = array()) + { + $this->_channels = $channels; + + if (!$this->_connected()) { + echo 'ERROR: You must connect to IRC first!' . PHP_EOL; + + return false; + } + + if (!empty($channels)) { + foreach ($channels as $channel => $password) { + $this->_joinChannel($channel, $password); + } + } + + return false; + } + + /** + * Implementation. + * Extended classes will use this function to parse the messages in the channel using $this->_channelData. + * + * @access protected + */ + protected function processChannelMessages() + { + } + + /** + * Join a channel. + * + * @param string $channel + * @param string $password + * + * @access protected. + */ + protected function _joinChannel($channel, $password) + { + $this->_writeSocket('JOIN ' . $channel . ($password === null ? '' : ' ' . $password)); + } + + /** + * Send PONG to a host. + * + * @param string $host + * + * @access protected + */ + protected function _pong($host) + { + if ($this->_writeSocket('PONG ' . $host) === false) { + $this->_reconnect(); + } + + // If we got a ping from the IRC server, set the last ping time to now. + if ($host === $this->_remote_host_received) { + $this->_lastPing = time(); + } + } + + /** + * Send PING to a host. + * + * @param string $host + * + * @access protected + */ + protected function _ping($host) + { + $pong = $this->_writeSocket('PING ' . $host); + + // Check if there's a connection error. + if ($pong === false || ((time() - $this->_lastPing) > ($this->_socket_timeout / 2) && !preg_match('/^PONG/', $this->_buffer))) { + $this->_reconnect(); + } + + // If sent a ping from the IRC server, set the last ping time to now. + if ($host === $this->_remote_host_received) { + $this->_lastPing = time(); + } + } + + /** + * Attempt to reconnect to IRC. + * + * @access protected + */ + protected function _reconnect() + { + if (!$this->connect($this->_remote_host, $this->_remote_port, $this->_remote_tls)) { + exit('FATAL: Could not reconnect to (' . $this->_remote_host . ') after (' . $this->_reconnectRetries . ') tries.' . PHP_EOL); + } + + if ($this->_alreadyLoggedIn === false) { + if (!$this->login($this->_nickName, $this->_userName, $this->_realName, $this->_password)) { + exit('FATAL: Could not log in to (' . $this->_remote_host . ')!' . PHP_EOL); + } + + $this->joinChannels($this->_channels); + } + } + + /** + * Read response from the IRC server. + * + * @access protected + */ + protected function _readSocket() + { + $buffer = ''; + do { + stream_set_timeout($this->_socket, $this->_socket_timeout); + $buffer .= fgets($this->_socket, 1024); + } while (!empty($buffer) && !preg_match('/\v+$/', $buffer)); + $this->_buffer = trim($buffer); + + if ($this->_debug && $this->_buffer !== '') { + echo 'RECV ' . $this->_buffer . PHP_EOL; + } + } + + /** + * Send a command to the IRC server. + * + * @param string $command + * + * @return bool + * + * @access protected + */ + protected function _writeSocket($command) + { + $command .= "\r\n"; + for ($written = 0; $written < strlen($command); $written += $fWrite) { + stream_set_timeout($this->_socket, $this->_socket_timeout); + $fWrite = $this->_writeSocketChar(substr($command, $written)); + + // http://www.php.net/manual/en/function.fwrite.php#96951 | fwrite can return 0 causing an infinite loop. + if ($fWrite === false || $fWrite <= 0) { + + // If it failed, try a second time. + $fWrite = $this->_writeSocketChar(substr($command, $written)); + if ($fWrite === false || $fWrite <= 0) { + echo 'ERROR: Could no write to socket! (the IRC server might have closed the connection)' . PHP_EOL; + + return false; + } + } + } + + if ($this->_debug) { + echo 'SEND :' . $command; + } + + return true; + } + + /** + * Write a single character to the socket. + * + * @param string (char) $character A single character. + * + * @return int|bool Number of bytes written or false. + */ + protected function _writeSocketChar($character) + { + return @fwrite($this->_socket, $character); + } + + /** + * Initiate stream socket to IRC server. + * + * @access protected + */ + protected function _initiateStream() + { + $this->_closeStream(); + + $socket = stream_socket_client( + $this->_remote_socket_string, + $error_number, + $error_string, + $this->_remote_connection_timeout + ); + + if ($socket === false) { + echo 'ERROR: ' . $error_string . ' (' . $error_number . ')' . PHP_EOL; + } else { + $this->_socket = $socket; + } + } + + /** + * Close the socket. + * + * @access protected + */ + protected function _closeStream() + { + if (!is_null($this->_socket)) { + $this->_socket = null; + } + } + + /** + * Check if we are connected to the IRC server. + * + * @return bool + * + * @access protected + */ + protected function _connected() + { + return (is_resource($this->_socket) && !feof($this->_socket)); + } + + /** + * Strips control characters from a IRC message. + * + * @param string $text + * + * @return string + * + * @access protected + */ + protected function _stripControlCharacters($text) + { + return preg_replace( + array( + '/(\x03(?:\d{1,2}(?:,\d{1,2})?)?)/', // Color code + '/\x02/', // Bold + '/\x0F/', // Escaped + '/\x16/', // Italic + '/\x1F/', // Underline + '/\x12/' // Device control 2 + ), + '', + $text + ); + } +} \ No newline at end of file diff --git a/lib/IRCScraper.php b/lib/IRCScraper.php index ec44b5de9..524c50f30 100644 --- a/lib/IRCScraper.php +++ b/lib/IRCScraper.php @@ -1,834 +1,241 @@ db = new DB(); - $this->functions = new Functions(); - $this->groupList = array(); - $this->IRC = $irc; - // Use the PingFix module. - new Net_SmartIRC_module_PingFix($irc); - if ($debug) { - $this->IRC->setDebug(SMARTIRC_DEBUG_ALL); - } - $this->serverType = $serverType; - $this->silent = $silent; - $this->resetPreVariables(); - $this->startScraping($socket); - } + protected $db; /** - * Destruct + * Array of ignored channels. + * + * @var array */ - public function __destruct() + protected $ignoredChannels; + + /** + * Regex to ignore categories. + * @var string + */ + protected $categoryIgnoreRegex; + + /** + * Construct + * + * @param bool $silent Run this in silent mode (no text output). + * @param bool $debug Turn on debug? Shows sent/received socket buffer messages. + * + * @access public + */ + public function __construct(&$silent = false, &$debug = false) { - // Close the socket. - if (!is_null($this->IRC)) { - if (!$this->silent) { - echo - 'Disconnecting from ' . - $this->serverType . - '.' . - PHP_EOL; - } - $this->IRC->disconnect(true); + if (defined('SCRAPE_IRC_SOURCE_IGNORE')) { + $this->ignoredChannels = unserialize(SCRAPE_IRC_SOURCE_IGNORE); + } else { + $this->ignoredChannels = array( + '#a.b.cd.image' => false, + '#a.b.console.ps3' => false, + '#a.b.dvd' => false, + '#a.b.erotica' => false, + '#a.b.flac' => false, + '#a.b.foreign' => false, + '#a.b.games.nintendods' => false, + '#a.b.inner-sanctum' => false, + '#a.b.moovee' => false, + '#a.b.movies.divx' => false, + '#a.b.sony.psp' => false, + '#a.b.sounds.mp3.complete_cd' => false, + '#a.b.teevee' => false, + '#a.b.games.wii' => false, + '#a.b.warez' => false, + '#a.b.games.xbox360' => false, + '#pre@corrupt' => false, + '#scnzb' => false, + '#tvnzb' => false + ); } + + $this->categoryIgnoreRegex = false; + if (defined('SCRAPE_IRC_CATEGORY_IGNORE') && SCRAPE_IRC_CATEGORY_IGNORE !== '') { + $this->categoryIgnoreRegex = SCRAPE_IRC_CATEGORY_IGNORE; + } + + $this->db = new DB(); + $this->groupList = array(); + $this->silent = $silent; + $this->_debug = $debug; + $this->resetPreVariables(); + $this->startScraping(); } /** * Main method for scraping. * - * @param bool $socket Use real sockets or fsock? + * @access protected */ - protected function startScraping(&$socket) + protected function startScraping() { - switch($this->serverType) { - case 'efnet': - $server = SCRAPE_IRC_EFNET_SERVER; - $port = SCRAPE_IRC_EFNET_PORT; - $nickname = SCRAPE_IRC_EFNET_NICKNAME; - $username = SCRAPE_IRC_EFNET_USERNAME; - $realname = SCRAPE_IRC_EFNET_REALNAME; - $password = SCRAPE_IRC_EFNET_PASSWORD; - $channelList = array( - // Channel Password. - '#alt.binaries.inner-sanctum' => null, - '#alt.binaries.cd.image' => null, - '#alt.binaries.movies.divx' => null, - '#alt.binaries.sounds.mp3.complete_cd' => null, - '#alt.binaries.warez' => null, - '#alt.binaries.teevee' => 'teevee', - '#alt.binaries.moovee' => 'moovee', - '#alt.binaries.erotica' => 'erotica', - '#alt.binaries.flac' => 'flac', - '#alt.binaries.foreign' => 'foreign', - '#alt.binaries.console.ps3' => null, - '#alt.binaries.games.nintendods' => null, - '#alt.binaries.games.wii' => null, - '#alt.binaries.games.xbox360' => null, - '#alt.binaries.sony.psp' => null, - '#scnzb' => null, - //'#tvnzb' => null - ); - // Check if the user is ignoring channels. - if (defined('SCRAPE_IRC_EFNET_IGNORED_CHANNELS') && SCRAPE_IRC_EFNET_IGNORED_CHANNELS != '') { - $ignored = explode(',', SCRAPE_IRC_EFNET_IGNORED_CHANNELS); - $newList = array(); - foreach($channelList as $channel => $chanpass) { - if (!in_array($channel, $ignored)) { - $newList[$channel] = $chanpass; - } - } - if (empty($newList)) { - exit('ERROR: You have ignored every group there is to scrape!' . PHP_EOL); - } - $channelList = $newList; - unset($newList); - } - $regex = - // Simple regex, more advanced regex below when doing the real checks. - '/' . - 'FILLED.*Pred.*ago' . // a.b.inner-sanctum - '|' . - 'Thank.*you.*Req.*Id.*Request' . // a.b.cd.image, a.b.movies.divx, a.b.sounds.mp3.complete_cd, a.b.warez - '|' . - 'Thank.*?You.*?Request.*?Filled!.*?ReqId' . // a.b.moovee a.b.foreign a.b.flac a.b.teevee - '|' . - 'That.*?was.*?awesome.*?Shall.*?ReqId' . // a.b.erotica - '|' . - 'person.*?filling.*?request.*?for:.*?ReqID:' . // a.b.console.ps3 - '|' . - 'NEW.*?\[NDS\].*?PRE:' . // a.b.games.nintendods - '|' . - 'A\s+new\s+NZB\s+has\s+been\s+added:' . // a.b.games.wii a.b.games.xbox360 - '|' . - 'A\s+NZB\s+is\s+available.*?To\s+Download' . // a.b.sony.psp - '|' . - '\s+NZB:\s+http:\/\/scnzb\.eu\/' . // scnzb - //'|' . - //'^\[SBINDEX\]' . // tvnzb - '|' . - '^\[(MOD|OLD|RE|UN)?NUKE\]' . // Nukes. various channels - '|' . - 'added\s+(nuke|reason)\s+info\s+for:' . // Nukes. a.b.games.xbox360 a.b.games.wii - '/i'; - break; - - case 'corrupt': - $server = SCRAPE_IRC_CORRUPT_SERVER; - $port = SCRAPE_IRC_CORRUPT_PORT; - $nickname = SCRAPE_IRC_CORRUPT_NICKNAME; - $username = SCRAPE_IRC_CORRUPT_USERNAME; - $realname = SCRAPE_IRC_CORRUPT_REALNAME; - $password = SCRAPE_IRC_CORRUPT_PASSWORD; - $channelList = array('#pre' => null); - $regex = '/PRE:.+?\[.+?\]|^(MOD|OLD|RE|UN)?NUKE:\s+/i'; // #pre - break; - - case 'zenet': - $server = SCRAPE_IRC_ZENET_SERVER; - $port = SCRAPE_IRC_ZENET_PORT; - $nickname = SCRAPE_IRC_ZENET_NICKNAME; - $username = SCRAPE_IRC_ZENET_USERNAME; - $realname = SCRAPE_IRC_ZENET_REALNAME; - $password = SCRAPE_IRC_ZENET_PASSWORD; - $channelList = array('#Pre' => null); - $regex = '/^\(PRE\)\s+\(|^\((MOD|OLD|RE|UN)?NUKE\)\s+/i'; // #Pre - break; - - default: - return; - } - - $versions = array( - 'HexChat 2.9.6 [x64] / Windows ' . rand(7, 8) . ' [' . rand(2, 3) . '.' . rand(0, 99) . 'GHz]', - 'irssi v0.8.' . rand(10, 16) . ' - running on Linux i686', - 'KVIrc 4.2.0', - 'mIRC 7.32 Khaled Mardam-Bey', - 'mIRC v6.31 Khaled Mardam-Bey', - 'HydraIRC v0.3.165', - 'xchat 2.8. ' . rand(6, 9) . ' Ubuntu', - 'ZNC 1.' . rand(0, 2) . ' - http://znc.in', - ); - - // Change the CTCP string. - $this->IRC->setCtcpVersion($versions[mt_rand(0, 6)]); - unset($versions); - - // Use real sockets instead of fsock. - $this->IRC->setUseSockets($socket); - - // This will scan channel messages for the regex above. - $this->IRC->registerActionhandler(SMARTIRC_TYPE_CHANNEL, $regex, $this, 'check_type'); - - // If there's a problem during connection, try to reconnect. - $this->IRC->setAutoRetry(true); - - // If problem connecting, wait 20 seconds before reconnecting. - $this->IRC->setReconnectdelay(200000); - - // Try 4 times before giving up. - $this->IRC->setAutoRetryMax(4); - - // If a network error happens, automatically reconnect. - $this->IRC->setAutoReconnect(true); // Connect to IRC. - $connection = $this->IRC->connect($server, $port); - if ($connection === false) { + if ($this->connect(SCRAPE_IRC_SERVER, SCRAPE_IRC_PORT, SCRAPE_IRC_TLS) === false) { exit ( 'Error connecting to (' . - $server . + SCRAPE_IRC_SERVER . ':' . - $port . + SCRAPE_IRC_PORT . '). Please verify your server information and try again.' . PHP_EOL ); } // Login to IRC. - if (!$this->IRC->login( - // Nick name. - $nickname, - // Real name. - $realname, - // User mode. - 0, - // User name. - $username, - // Password. - (empty($password) ? null : $password) - ) - ) { + if ($this->login(SCRAPE_IRC_NICKNAME, SCRAPE_IRC_REALNAME, SCRAPE_IRC_USERNAME, SCRAPE_IRC_PASSWORD) === false) { exit('Error logging in to: (' . - $server . ':' . $port . ') nickname: (' . $nickname . + SCRAPE_IRC_SERVER . ':' . SCRAPE_IRC_PORT . ') nickname: (' . SCRAPE_IRC_NICKNAME . '). Verify your connection information, you might also be banned from this server or there might have been a connection issue.' . PHP_EOL ); } // Join channels. - if (!$this->IRC->joinChannels($channelList)) { - exit('Error joining channels on (' . $server . ':' . $port . ') might be an issue with the server.' . PHP_EOL); - } + $this->joinChannels(array('#nZEDbPRE' => null)); if (!$this->silent) { echo '[' . date('r') . '] [Scraping of IRC channels for (' . - $server . + SCRAPE_IRC_SERVER . ':' . - $port . + SCRAPE_IRC_PORT . ') (' . - $nickname . + SCRAPE_IRC_NICKNAME . ') started.]' . PHP_EOL; } - // Wait for action handlers. - $this->IRC->listen(); - - // If we return from action handlers, disconnect from IRC. - $this->IRC->disconnect(); + // Scan incoming IRC messages. + $this->readIncoming(); } /** - * Check the similarity between 2 words. + * Process bot messages, insert/update PREs. * - * @param string $word1 - * @param string $word2 - * @param int $similarity - * - * @return bool + * @access protected */ - protected function checkSimilarity(&$word1, $word2, $similarity = 49) + protected function processChannelMessages() { - similar_text($word1, $word2, $percent); - if ($percent > $similarity) { - return true; - } - return false; - } + if (preg_match( + '/^(NEW|UPD|NUK): \[DT: (?P