-
Notifications
You must be signed in to change notification settings - Fork 145
Expand file tree
/
Copy pathXmppNotify.php
More file actions
191 lines (158 loc) · 4.48 KB
/
Copy pathXmppNotify.php
File metadata and controls
191 lines (158 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace PHPCensor\Plugin;
use PHPCensor\Builder;
use PHPCensor\Model\Build;
use PHPCensor\Plugin;
/**
* XMPP Notification - Send notification for successful or failure build
*
* @package PHP Censor
* @subpackage Application
*
* @author Alexandre Russo <dev.github@ange7.com>
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
*/
class XmppNotify extends Plugin
{
/**
* @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 = false;
/**
* @var array, list of recipients xmpp accounts
*/
protected $recipients = [];
/**
* @var string, mask to format date
*/
protected $dateFormat = '%c';
/**
* @return string
*/
public static function pluginName()
{
return 'xmpp_notify';
}
/**
* {@inheritDoc}
*/
public function __construct(Builder $builder, Build $build, array $options = [])
{
parent::__construct($builder, $build, $options);
$this->executable = $this->findBinary('sendxmpp');
/*
* Set recipients list
*/
if (!empty($options['recipients'])) {
if (\is_string($options['recipients'])) {
$this->recipients = [$options['recipients']];
} elseif (\is_array($options['recipients'])) {
$this->recipients = $options['recipients'];
}
}
}
/**
* 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 .sendxmpprc)
*/
public function findConfigFile()
{
if (\file_exists($this->builder->buildPath . '.sendxmpprc')) {
if (\md5(\file_get_contents($this->builder->buildPath . '.sendxmpprc'))
!== \md5($this->getConfigFormat())) {
return null;
}
return true;
}
return null;
}
/**
* Send notification message.
*/
public function execute()
{
$sendxmpp = $this->executable;
/*
* Without recipients we can't send notification
*/
if (!\is_array($this->recipients) || \count($this->recipients) === 0) {
return false;
}
/*
* Try to build conf file
*/
$configFile = $this->builder->buildPath . '.sendxmpprc';
if (\is_null($this->findConfigFile())) {
\file_put_contents($configFile, $this->getConfigFormat());
\chmod($configFile, 0600);
}
/*
* Enabled ssl for connection
*/
$tls = '';
if ($this->tls) {
$tls = ' -t';
}
$messageFile = $this->builder->buildPath . \uniqid('xmppmessage');
if ($this->buildMessage($messageFile) === false) {
return false;
}
/*
* Send XMPP notification for all recipients
*/
$cmd = $sendxmpp . "%s -f %s -m %s %s";
$recipients = \implode(' ', $this->recipients);
$success = $this->builder->executeCommand($cmd, $tls, $configFile, $messageFile, $recipients);
print $this->builder->getLastOutput();
/*
* Remove temp message file
*/
$this->builder->executeCommand("rm -rf " . $messageFile);
return $success;
}
/**
* @return int
*/
protected function buildMessage($messageFile)
{
if ($this->build->isSuccessful()) {
$message = "✔ [" . $this->build->getProjectTitle() . "] Build #" . $this->build->getId() . " successful";
} else {
$message = "✘ [" . $this->build->getProjectTitle() . "] Build #" . $this->build->getId() . " failure";
}
$message .= ' (' . \strftime($this->dateFormat) . ')';
return \file_put_contents($messageFile, $message);
}
}