1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-27 09:12:41 +01:00

Use some HTTPSFuture in CampfireBot

Summary:
  - Use PhutilURI to correct for specifying "https://yourname.campfire.com/" instead of "https://yourname.campfire.com".
  - Use HTTPSFuture to get logging via `--trace` and error detection (CA stuff should be OK since 37signals has real certs).
  - On destruction, only try to leave rooms we've actually joined.

Test Plan: Setup a bot, had it join a room, talked to it.

Reviewers: indiefan

Reviewed By: indiefan

CC: aran

Differential Revision: https://secure.phabricator.com/D4849
This commit is contained in:
epriestley 2013-02-07 10:32:33 -08:00
parent 3ce3f5d368
commit 5f9a063333

View file

@ -9,16 +9,16 @@ extends PhabricatorBaseProtocolAdapter {
private $readHandles; private $readHandles;
private $multiHandle; private $multiHandle;
private $active; private $active;
private $rooms; private $inRooms = array();
public function connect() { public function connect() {
$this->server = idx($this->config, 'server'); $this->server = idx($this->config, 'server');
$this->authtoken = idx($this->config, 'authtoken'); $this->authtoken = idx($this->config, 'authtoken');
$ssl = idx($this->config, 'ssl', false); $ssl = idx($this->config, 'ssl', false);
$this->rooms = idx($this->config, 'join'); $rooms = idx($this->config, 'join');
// First, join the room // First, join the room
if (!$this->rooms) { if (!$rooms) {
throw new Exception("Not configured to join any rooms!"); throw new Exception("Not configured to join any rooms!");
} }
@ -29,7 +29,7 @@ extends PhabricatorBaseProtocolAdapter {
$this->multiHandle = curl_multi_init(); $this->multiHandle = curl_multi_init();
$this->readHandles = array(); $this->readHandles = array();
foreach ($this->rooms as $room_id) { foreach ($rooms as $room_id) {
$this->joinRoom($room_id); $this->joinRoom($room_id);
// Set up the curl stream for reading // Set up the curl stream for reading
@ -138,10 +138,12 @@ extends PhabricatorBaseProtocolAdapter {
private function joinRoom($room_id) { private function joinRoom($room_id) {
$this->performPost("/room/{$room_id}/join.json"); $this->performPost("/room/{$room_id}/join.json");
$this->inRooms[$room_id] = true;
} }
private function leaveRoom($room_id) { private function leaveRoom($room_id) {
$this->performPost("/room/{$room_id}/leave.json"); $this->performPost("/room/{$room_id}/leave.json");
unset($this->inRooms[$room_id]);
} }
private function speak($message, $room_id) { private function speak($message, $room_id) {
@ -154,48 +156,42 @@ extends PhabricatorBaseProtocolAdapter {
} }
private function performPost($endpoint, $data = Null) { private function performPost($endpoint, $data = Null) {
$url = $this->server.$endpoint; $uri = new PhutilURI($this->server);
$uri->setPath($endpoint);
$payload = json_encode($data); $payload = json_encode($data);
// cURL init & config list($output) = id(new HTTPSFuture($uri))
$ch = curl_init(); ->setMethod('POST')
curl_setopt($ch, CURLOPT_URL, $url); ->addHeader('Content-Type', 'application/json')
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); ->addHeader('Authorization', $this->getAuthorizationHeader())
curl_setopt($ch, CURLOPT_POST, 1); ->setData($payload)
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); ->resolvex();
curl_setopt($ch, CURLOPT_USERPWD, $this->authtoken . ':x');
curl_setopt(
$ch,
CURLOPT_HTTPHEADER,
array("Content-type: application/json"));
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
$output = curl_exec($ch);
curl_close($ch);
$output = trim($output); $output = trim($output);
if (strlen($output)) { if (strlen($output)) {
return json_decode($output); return json_decode($output, true);
} }
return true; return true;
} }
public function __destruct() { public function __destruct() {
if ($this->rooms) { foreach ($this->inRooms as $room_id => $ignored) {
foreach ($this->rooms as $room_id) {
$this->leaveRoom($room_id); $this->leaveRoom($room_id);
} }
}
if ($this->readHandles) { if ($this->readHandles) {
foreach ($this->readHandles as $read_handle) { foreach ($this->readHandles as $read_handle) {
curl_multi_remove_handle($this->multiHandle, $read_handle); curl_multi_remove_handle($this->multiHandle, $read_handle);
curl_close($read_handle); curl_close($read_handle);
} }
} }
curl_multi_close($this->multiHandle); curl_multi_close($this->multiHandle);
} }
private function getAuthorizationHeader() {
return 'Basic '.base64_encode($this->authtoken.':x');
}
} }