From f7a8730ecee66cc472e824f29faa2a1ca085654d Mon Sep 17 00:00:00 2001 From: DariusIII Date: Fri, 7 Nov 2025 13:13:58 +0100 Subject: [PATCH] Fix IRC scraping --- Blacklight/IRCClient.php | 44 +++++++++++++-- Blacklight/IRCScraper.php | 110 +++++++++++++++++++++++++++++--------- config/irc_settings.php | 1 - 3 files changed, 124 insertions(+), 31 deletions(-) diff --git a/Blacklight/IRCClient.php b/Blacklight/IRCClient.php index 2211fda0b..6b1dc74ac 100755 --- a/Blacklight/IRCClient.php +++ b/Blacklight/IRCClient.php @@ -358,6 +358,12 @@ class IRCClient if ((time() - $this->_lastPing) > ($this->_socket_timeout / 2)) { $this->_ping($this->_remote_host_received); } + + // Small sleep to prevent CPU spinning when no data is available + // Only sleep if buffer was empty + if (empty($this->_buffer)) { + usleep(100000); // 100ms + } } } @@ -464,9 +470,32 @@ class IRCClient { $buffer = ''; do { - stream_set_timeout($this->_socket, $this->_socket_timeout); + // Use a shorter timeout for reading to be more responsive + stream_set_timeout($this->_socket, 5); $line = fgets($this->_socket, 1024); + + // Check if connection is still alive and handle timeout/errors if ($line === false) { + $meta = stream_get_meta_data($this->_socket); + + // If stream timed out, it's OK - just no data available + if ($meta['timed_out']) { + // Check if connection is still valid + if (! $this->_connected()) { + echo 'Connection lost (timeout), attempting to reconnect...'.PHP_EOL; + $this->_reconnect(); + } + break; + } + + // If EOF or other error, connection is dead + if ($meta['eof'] || ! $this->_connected()) { + echo 'Connection lost, attempting to reconnect...'.PHP_EOL; + $this->_reconnect(); + break; + } + + // No data available but connection is fine break; } $buffer .= $line; @@ -542,6 +571,9 @@ class IRCClient echo 'ERROR: '.$error_string.' ('.$error_number.')'.PHP_EOL; } else { $this->_socket = $socket; + // Set blocking mode with timeout for proper connection handling + stream_set_blocking($this->_socket, true); + stream_set_timeout($this->_socket, $this->_socket_timeout); } } @@ -573,12 +605,14 @@ class IRCClient { $result = preg_replace( [ - '/(\x03(?:\d{1,2}(?:,\d{1,2})?)?)/', // Color code + '/\x03\d{1,2}(?:,\d{1,2})?/', // Color code with foreground and optional background + '/\x03/', // Color code without parameters (reset) '/\x02/', // Bold - '/\x0F/', // Escaped - '/\x16/', // Italic + '/\x0F/', // Reset/Normal + '/\x16/', // Reverse/Italic '/\x1F/', // Underline - '/\x12/', // Device control 2 + '/\x1D/', // Italic + '/\x11/', // Monospace ], '', $text diff --git a/Blacklight/IRCScraper.php b/Blacklight/IRCScraper.php index 15c9878b1..7d9013bb8 100755 --- a/Blacklight/IRCScraper.php +++ b/Blacklight/IRCScraper.php @@ -187,21 +187,41 @@ class IRCScraper extends IRCClient */ protected function processChannelMessages(): void { + if ($this->_debug && ! $this->_silent) { + echo '[DEBUG] Processing message: '.$this->_channelData['message'].PHP_EOL; + } + if (preg_match( '/^(NEW|UPD|NUK): \[DT: (?P