From 42db3fc395a5a8807e70fef23b79acc9c5babd81 Mon Sep 17 00:00:00 2001 From: Alexandre Russo Date: Wed, 21 May 2014 10:58:25 +0200 Subject: [PATCH] PHPCI/Plugin/XMPP.php : Add XMPP plugin notification to send notification via XMPP Network to recipients list --- PHPCI/Plugin/XMPP.php | 193 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 193 insertions(+) create mode 100644 PHPCI/Plugin/XMPP.php diff --git a/PHPCI/Plugin/XMPP.php b/PHPCI/Plugin/XMPP.php new file mode 100644 index 00000000..2fb3ae63 --- /dev/null +++ b/PHPCI/Plugin/XMPP.php @@ -0,0 +1,193 @@ + +* @package PHPCI +* @subpackage Plugins +*/ +class XMPP implements \PHPCI\Plugin +{ + protected $directory; + protected $args; + protected $phpci; + protected $build; + + /** + * @var string, username of sender account xmpp + */ + protected $username; + + /** + * @var string, alias server of sender account xmpp + */ + protected $server; + + /** + * @var string, password of sender account xmpp + */ + protected $password; + + /** + * @var string, alias for sender + */ + protected $alias; + + /** + * @var string, use tls + */ + protected $tls; + + /** + * @var array, list of recipients xmpp accounts + */ + protected $recipients + + /** + * + * @param Builder $phpci + * @param Build $build + * @param array $options + */ + public function __construct(Builder $phpci, Build $build, array $options = array()) + { + $this->phpci = $phpci; + $this->build = $build; + + $this->username = ''; + $this->password = ''; + $this->server = ''; + $this->alias = ''; + $this->recipients= array(); + $this->tls = false; + + /* + * Set recipients list + */ + if(!empty($options['recipients'])) { + if(is_string($options['recipients'])) { + $this->recipients = array($options['recipients']); + } elseif(is_array($options['recipients'])) { + $this->recipients = $options['recipients']; + } + } + + $this->setOptions($options); + } + + /** + * Set options configuration for plugin + * + * @param array $options + */ + protected function setOptions($options) + { + foreach (array('username', 'password', 'alias', 'tls', 'server') as $key) { + if (array_key_exists($key, $options)) { + $this->{$key} = $options[$key]; + } + } + } + + /** + * Get config format for sendxmpp config file + * + * @return string + */ + protected function getConfigFormat() + { + $conf = $this->username; + if(!empty($this->server)) { + $conf .= ';'.$this->server; + } + + $conf .= ' '.$this->password; + + if(!empty($this->alias)) { + $conf .= ' '.$this->alias; + } + + return $conf; + } + + /** + * Find config file for sendxmpp binary (default is ~/.sendxmpp) + */ + public static function findConfigFile() + { + if (file_exists('~/.sendxmpp')) { + if( md5(file_get_contents('~/.sendxmpp')) !== + md5($this->getConfigFormat())) { + return null; + } + + return true; + } + + return null; + } + + /** + * Send notification message. + */ + public function execute() + { + $sendxmpp = $this->phpci->findBinary('/usr/bin/sendxmpp'); + + if (!$sendxmpp) { + $this->phpci->logFailure('Could not find sendxmpp.'); + return false; + } + + /* + * Try to build conf file + */ + if(is_null(self::findConfigFile())) { + file_put_contents('~/.sendxmpp', $this->getConfigFormat()); + chmod('~/.sendxmpp', 0600); + } + + /* + * Enabled ssl for connection + */ + $tls = ''; + if($this->tls) { + $tls = ' -t'; + } + + /* + * Build message from status build + */ + if($this->build->isSuccessful()) { + $message = "✔ [".$this->build->getProjectTitle()."] Build #". + $this->build->getId()." successful"; + } else { + $message = "✘ [".$this->build->getProjectTitle()."] Build #". + $this->build->getId()." failure"; + } + + /* + * Send XMPP notification for all recipients + */ + $success = false; + foreach($this->recipients as $recipient) { + $success = $this->phpci->executeCommand('echo %s | ' . $sendxmpp . + ' %s %s', $message, $tls, $recipients); + print $this->phpci->getLastOutput(); + } + + return $success; + } +}