2013-10-08 18:19:44 +02:00
|
|
|
<?php
|
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
namespace PHPCensor\Plugin;
|
2013-10-10 02:01:06 +02:00
|
|
|
|
2016-07-19 20:28:11 +02:00
|
|
|
use PHPCensor\Builder;
|
|
|
|
use PHPCensor\Model\Build;
|
|
|
|
use PHPCensor\Plugin;
|
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
|
2017-01-10 18:46:19 +01:00
|
|
|
*
|
2017-03-04 16:39:56 +01:00
|
|
|
* @author Dan Cryer <dan@block8.co.uk>
|
2013-10-08 18:19:44 +02:00
|
|
|
*/
|
2016-07-11 18:00:04 +02:00
|
|
|
class Irc extends Plugin
|
2013-10-08 18:19:44 +02:00
|
|
|
{
|
2014-05-02 15:48:40 +02:00
|
|
|
protected $message;
|
|
|
|
protected $server;
|
|
|
|
protected $port;
|
|
|
|
protected $room;
|
|
|
|
protected $nick;
|
2013-10-08 18:19:44 +02:00
|
|
|
|
2017-01-11 16:15:54 +01:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function pluginName()
|
|
|
|
{
|
|
|
|
return 'irc';
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
2016-07-11 18:00:04 +02:00
|
|
|
* {@inheritdoc}
|
2014-12-08 12:25:33 +01:00
|
|
|
*/
|
2016-07-22 09:05:34 +02:00
|
|
|
public function __construct(Builder $builder, Build $build, array $options = [])
|
2013-10-08 18:19:44 +02:00
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
parent::__construct($builder, $build, $options);
|
2013-10-08 18:19:44 +02:00
|
|
|
|
2016-07-11 18:00:04 +02:00
|
|
|
$this->message = $options['message'];
|
2016-07-22 09:05:34 +02:00
|
|
|
$buildSettings = $this->builder->getConfig('build_settings');
|
2013-10-08 18:19:44 +02:00
|
|
|
|
|
|
|
if (isset($buildSettings['irc'])) {
|
|
|
|
$irc = $buildSettings['irc'];
|
|
|
|
|
|
|
|
$this->server = $irc['server'];
|
2016-07-11 18:00:04 +02:00
|
|
|
$this->port = $irc['port'];
|
|
|
|
$this->room = $irc['room'];
|
|
|
|
$this->nick = $irc['nick'];
|
2013-10-08 18:19:44 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-08 12:25:33 +01:00
|
|
|
/**
|
|
|
|
* Run IRC plugin.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-10-08 18:19:44 +02:00
|
|
|
public function execute()
|
|
|
|
{
|
2016-07-22 09:05:34 +02:00
|
|
|
$msg = $this->builder->interpolate($this->message);
|
2013-10-08 18:19:44 +02:00
|
|
|
|
|
|
|
if (empty($this->server) || empty($this->room) || empty($this->nick)) {
|
2017-02-07 13:05:44 +01:00
|
|
|
$this->builder->logFailure('You must configure a server, room and nick.');
|
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);
|
|
|
|
|
2016-04-21 06:58:09 +02:00
|
|
|
$connectCommands = [
|
2015-03-27 11:17:22 +01:00
|
|
|
'USER ' . $this->nick . ' 0 * :' . $this->nick,
|
|
|
|
'NICK ' . $this->nick,
|
2016-04-21 06:58:09 +02:00
|
|
|
];
|
2015-03-27 11:17:22 +01:00
|
|
|
$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
|
|
|
|
|
|
|
/**
|
|
|
|
* @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)) {
|
2016-04-21 06:58:09 +02:00
|
|
|
$matches = [];
|
2015-03-27 11:17:22 +01:00
|
|
|
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)
|
|
|
|
{
|
2016-04-21 06:58:09 +02:00
|
|
|
return $this->executeIrcCommands($socket, [$command]);
|
2015-03-27 11:17:22 +01:00
|
|
|
}
|
2013-10-08 18:19:44 +02:00
|
|
|
}
|