phpci/PHPCI/Plugin/Irc.php

141 lines
3.5 KiB
PHP
Raw Normal View History

2013-10-08 18:19:44 +02:00
<?php
/**
* PHPCI - Continuous Integration for PHP
*
* @copyright Copyright 2015, Block 8 Limited.
* @license https://github.com/Block8/PHPCI/blob/master/LICENSE.md
* @link https://www.phptesting.org/
*/
2013-10-08 18:19:44 +02:00
namespace PHPCI\Plugin;
2013-10-10 02:01:06 +02:00
use PHPCI\Builder;
2014-12-04 16:48:52 +01:00
use PHPCI\Helper\Lang;
2013-10-10 02:01:06 +02:00
use PHPCI\Model\Build;
use PHPCI\PluginInterface;
2013-10-10 02:01:06 +02:00
2013-10-08 18:19:44 +02:00
/**
* IRC Plugin
*
* Sends a notification to an IRC channel.
*
2013-10-08 18:19:44 +02:00
* @author Dan Cryer <dan@block8.co.uk>
* @package PHPCI
* @subpackage Plugins
*/
class Irc implements PluginInterface
2013-10-08 18:19:44 +02:00
{
2014-05-02 15:48:40 +02:00
protected $phpci;
protected $build;
protected $message;
protected $server;
protected $port;
protected $room;
protected $nick;
2013-10-08 18:19:44 +02:00
/**
* Standard Constructor
*
* $options['directory'] Output Directory. Default: %BUILDPATH%
* $options['filename'] Phar Filename. Default: build.phar
* $options['regexp'] Regular Expression Filename Capture. Default: /\.php$/
* $options['stub'] Stub Content. No Default Value
*
* @param Builder $phpci
* @param Build $build
* @param array $options
*/
2013-10-10 02:01:06 +02:00
public function __construct(Builder $phpci, Build $build, array $options = array())
2013-10-08 18:19:44 +02:00
{
$this->phpci = $phpci;
2014-05-02 15:48:40 +02:00
$this->build = $build;
2013-10-08 18:19:44 +02:00
$this->message = $options['message'];
$buildSettings = $phpci->getConfig('build_settings');
if (isset($buildSettings['irc'])) {
$irc = $buildSettings['irc'];
$this->server = $irc['server'];
$this->port = $irc['port'];
$this->room = $irc['room'];
$this->nick = $irc['nick'];
}
}
/**
2015-04-28 09:12:55 +02:00
* {@inheritDocs}
*/
2013-10-08 18:19:44 +02:00
public function execute()
{
$msg = $this->phpci->interpolate($this->message);
if (empty($this->server) || empty($this->room) || empty($this->nick)) {
2014-12-04 16:48:52 +01:00
$this->phpci->logFailure(Lang::get('irc_settings'));
2013-10-08 18:19:44 +02:00
}
if (empty($this->port)) {
$this->port = 6667;
}
$sock = fsockopen($this->server, $this->port);
2015-03-27 11:17:22 +01:00
stream_set_timeout($sock, 1);
$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);
2013-10-08 18:23:21 +02:00
2013-10-08 18:19:44 +02:00
fclose($sock);
return true;
}
2015-03-27 11:17:22 +01:00
/**
2015-04-28 09:12:55 +02:00
* Execute a list of IRC commands.
*
2015-03-27 11:17:22 +01:00
* @param resource $socket
* @param array $commands
2015-04-28 09:12:55 +02:00
*
2015-03-27 11:17:22 +01:00
* @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);
}
}
/**
2015-04-28 09:12:55 +02:00
* Execute a single IRC command.
2015-03-27 11:17:22 +01:00
*
* @param resource $socket
* @param string $command
2015-04-28 09:12:55 +02:00
*
2015-03-27 11:17:22 +01:00
* @return bool
*/
private function executeIrcCommand($socket, $command)
{
return $this->executeIrcCommands($socket, array($command));
}
2013-10-08 18:19:44 +02:00
}