From 9eb14b81f00515f014ad48b837521c2d47bb6743 Mon Sep 17 00:00:00 2001 From: Angel Koilov Date: Fri, 27 Mar 2015 12:17:22 +0200 Subject: [PATCH 1/4] add pingback for IRC plugin --- PHPCI/Plugin/Irc.php | 60 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 11 deletions(-) diff --git a/PHPCI/Plugin/Irc.php b/PHPCI/Plugin/Irc.php index 7fe06753..6d09c389 100644 --- a/PHPCI/Plugin/Irc.php +++ b/PHPCI/Plugin/Irc.php @@ -64,7 +64,7 @@ class Irc implements \PHPCI\Plugin * Run IRC plugin. * @return bool */ - public function execute() + public function execute() { $msg = $this->phpci->interpolate($this->message); @@ -77,19 +77,57 @@ class Irc implements \PHPCI\Plugin } $sock = fsockopen($this->server, $this->port); - fputs($sock, 'USER ' . $this->nick . ' phptesting.org ' . $this->nick . ' :' . $this->nick . "\r\n"); - fputs($sock, 'NICK ' . $this->nick . "\r\n"); - fputs($sock, 'JOIN ' . $this->room . "\r\n"); - fputs($sock, 'PRIVMSG ' . $this->room . ' :' . $msg . "\r\n"); - - while (fgets($sock)) { - // We don't need to do anything, - // but the IRC server doesn't appear to post the message - // unless we wait for responses. - } + stream_set_timeout($sock, 1); + + $connectCommands = [ + 'USER ' . $this->nick . ' 0 * :' . $this->nick, + 'NICK ' . $this->nick, + ]; + $this->executeIrcCommands($sock, $connectCommands); + $this->executeIrcCommand($sock, 'JOIN ' . $this->room); + $this->executeIrcCommand($sock, 'PRIVMSG ' . $this->room . ' :' . $msg); fclose($sock); return true; } + + /** + * @param resource $socket + * @param array $commands + * @return bool + */ + private function executeIrcCommands($socket, array $commands) + { + foreach ($commands as $command) { + fputs($socket, $command . "\n"); + } + + $pingBack = false; + + // almost all servers expect pingback! + while ($response = fgets($socket)) { + $matches = array(); + if (preg_match('/^PING \\:([A-Z0-9]+)/', $response, $matches)) { + $pingBack = $matches[1]; + } + } + + if ($pingBack) { + $command = 'PONG :' . $pingBack . "\n"; + fputs($socket, $command); + } + } + + /** + * + * @param resource $socket + * @param string $command + * @return bool + */ + private function executeIrcCommand($socket, $command) + { + return $this->executeIrcCommands($socket, [$command]); + } + } From 54554e6243ae4ec80a7dbaa45412b447ede6cf64 Mon Sep 17 00:00:00 2001 From: Angel Koilov Date: Mon, 30 Mar 2015 09:12:17 +0300 Subject: [PATCH 2/4] fix [] to array() --- PHPCI/Plugin/Irc.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/PHPCI/Plugin/Irc.php b/PHPCI/Plugin/Irc.php index 6d09c389..383a0b9b 100644 --- a/PHPCI/Plugin/Irc.php +++ b/PHPCI/Plugin/Irc.php @@ -64,7 +64,7 @@ class Irc implements \PHPCI\Plugin * Run IRC plugin. * @return bool */ - public function execute() + public function execute() { $msg = $this->phpci->interpolate($this->message); @@ -79,10 +79,10 @@ class Irc implements \PHPCI\Plugin $sock = fsockopen($this->server, $this->port); stream_set_timeout($sock, 1); - $connectCommands = [ + $connectCommands = array( 'USER ' . $this->nick . ' 0 * :' . $this->nick, 'NICK ' . $this->nick, - ]; + ); $this->executeIrcCommands($sock, $connectCommands); $this->executeIrcCommand($sock, 'JOIN ' . $this->room); $this->executeIrcCommand($sock, 'PRIVMSG ' . $this->room . ' :' . $msg); @@ -127,7 +127,7 @@ class Irc implements \PHPCI\Plugin */ private function executeIrcCommand($socket, $command) { - return $this->executeIrcCommands($socket, [$command]); + return $this->executeIrcCommands($socket, array($command)); } } From bb41b873a4908ebea93dc80319783c583126d8e2 Mon Sep 17 00:00:00 2001 From: Angel Koilov Date: Mon, 30 Mar 2015 09:17:32 +0300 Subject: [PATCH 3/4] fix code formatting --- PHPCI/Plugin/Irc.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/PHPCI/Plugin/Irc.php b/PHPCI/Plugin/Irc.php index 383a0b9b..f78a112f 100644 --- a/PHPCI/Plugin/Irc.php +++ b/PHPCI/Plugin/Irc.php @@ -78,7 +78,7 @@ class Irc implements \PHPCI\Plugin $sock = fsockopen($this->server, $this->port); stream_set_timeout($sock, 1); - + $connectCommands = array( 'USER ' . $this->nick . ' 0 * :' . $this->nick, 'NICK ' . $this->nick, @@ -91,7 +91,7 @@ class Irc implements \PHPCI\Plugin return true; } - + /** * @param resource $socket * @param array $commands @@ -104,7 +104,7 @@ class Irc implements \PHPCI\Plugin } $pingBack = false; - + // almost all servers expect pingback! while ($response = fgets($socket)) { $matches = array(); @@ -118,7 +118,7 @@ class Irc implements \PHPCI\Plugin fputs($socket, $command); } } - + /** * * @param resource $socket @@ -129,5 +129,4 @@ class Irc implements \PHPCI\Plugin { return $this->executeIrcCommands($socket, array($command)); } - } From 39007ddad3d13a53fdb487d950518fa854b9dbe5 Mon Sep 17 00:00:00 2001 From: Angel Koilov Date: Mon, 30 Mar 2015 09:24:30 +0300 Subject: [PATCH 4/4] remove whitespace --- PHPCI/Plugin/Irc.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/PHPCI/Plugin/Irc.php b/PHPCI/Plugin/Irc.php index f78a112f..a7610a8c 100644 --- a/PHPCI/Plugin/Irc.php +++ b/PHPCI/Plugin/Irc.php @@ -120,7 +120,7 @@ class Irc implements \PHPCI\Plugin } /** - * + * * @param resource $socket * @param string $command * @return bool