-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCampfireNotify.php
More file actions
163 lines (132 loc) · 4.59 KB
/
Copy pathCampfireNotify.php
File metadata and controls
163 lines (132 loc) · 4.59 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
<?php
declare(strict_types=1);
namespace PHPCensor\Plugins\Notification;
use PHPCensor\Common\Build\BuildInterface;
use PHPCensor\Common\ParameterBag;
use PHPCensor\Common\Plugin\Plugin;
/**
* CampfireNotify Plugin - Allows Campfire API actions. Strongly based on icecube (http://labs.mimmin.com/icecube)
*
* @package PHP Censor
* @subpackage Plugins
*
* @author Dmitry Khomutov <poisoncorpsee@gmail.com>
* @author André Cianfarani <acianfa@gmail.com>
*/
class CampfireNotify extends Plugin
{
private string $url = '';
private string $authToken = '';
private string $userAgent;
private string $cookie;
private bool $verbose = false;
private string $roomId = '';
private string $message = '';
/**
* {@inheritDoc}
*/
public static function getName(): string
{
return 'campfire_notify';
}
/**
* {@inheritDoc}
*/
public function execute(): bool
{
$this->joinRoom($this->roomId);
$status = (bool)$this->speak($this->variableInterpolator->interpolate($this->message), $this->roomId);
$this->leaveRoom($this->roomId);
return $status;
}
/**
* {@inheritDoc}
*/
public static function canExecute(string $stage, BuildInterface $build): bool
{
if (\in_array($stage, [
BuildInterface::STAGE_BROKEN,
BuildInterface::STAGE_COMPLETE,
BuildInterface::STAGE_FAILURE,
BuildInterface::STAGE_FIXED,
BuildInterface::STAGE_SUCCESS,
], true)) {
return true;
}
return false;
}
/**
* {@inheritDoc}
*/
protected function initPluginSettings(): void
{
$buildSettings = (array)$this->buildSettings->get('campfire', []);
$buildSettingsBag = new ParameterBag($buildSettings);
$this->url = (string)$buildSettingsBag->get('url', $this->url);
$this->authToken = $this->variableInterpolator->interpolate((string)$buildSettingsBag->get('auth_token', $this->authToken));
$this->roomId = (string)$buildSettingsBag->get('room_id', $this->roomId);
$this->message = (string)$this->options->get('message', $this->message);
$this->verbose = (bool)$this->options->get('verbose', $this->verbose);
$this->userAgent = 'PHP Censor/' . $this->variableInterpolator->interpolate('%SYSTEM_VERSION%');
$this->cookie = "php-censor-cookie";
}
/**
* Join a Campfire room.
*/
private function joinRoom(string $roomId): void
{
$this->getPageByPost('/room/' . $roomId . '/join.json');
}
/**
* Leave a Campfire room.
*/
public function leaveRoom(string $roomId): void
{
$this->getPageByPost('/room/' . $roomId . '/leave.json');
}
/**
* Send a message to a campfire room.
*/
public function speak(string $message, string $roomId, bool $isPaste = false): string
{
$page = '/room/' . $roomId . '/speak.json';
if ($isPaste) {
$type = 'PasteMessage';
} else {
$type = 'TextMessage';
}
return $this->getPageByPost($page, ['message' => ['type' => $type, 'body' => $message]]);
}
/**
* Make a request to Campfire.
*/
private function getPageByPost(string $page, array $data = []): string
{
$url = $this->url . $page;
// The new API allows JSON, so we can pass
// PHP data structures instead of old school POST
$json = \json_encode($data);
// cURL init & config
$handle = \curl_init();
\curl_setopt($handle, CURLOPT_URL, $url);
\curl_setopt($handle, CURLOPT_RETURNTRANSFER, true);
\curl_setopt($handle, CURLOPT_POST, 1);
\curl_setopt($handle, CURLOPT_USERAGENT, $this->userAgent);
\curl_setopt($handle, CURLOPT_VERBOSE, $this->verbose);
\curl_setopt($handle, CURLOPT_FOLLOWLOCATION, 1);
\curl_setopt($handle, CURLOPT_USERPWD, $this->authToken . ':x');
\curl_setopt($handle, CURLOPT_HTTPHEADER, ["Content-type: application/json"]);
\curl_setopt($handle, CURLOPT_COOKIEFILE, $this->cookie);
\curl_setopt($handle, CURLOPT_POSTFIELDS, $json);
$output = (string)\curl_exec($handle);
\curl_close($handle);
// We tend to get one space with an otherwise blank response
$output = \trim($output);
if (\strlen($output)) {
/* Responses are JSON. Decode it to a data structure */
return \json_decode($output);
}
// Simple 200 OK response (such as for joining a room)
return '';
}
}