1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-09 16:32:39 +01:00

Use a custom quit message when gracefully shutting down the IRC bot

Summary: Fixes T3173. This doesn't actually fix T3173 but I'm going to redirect that. It does make the bot quit IRC gracefully, with a nicer quit message, which can be customized.

Test Plan: Got a bot to quit IRC with nice messages.

Reviewers: btrahan

Reviewed By: btrahan

Subscribers: epriestley

Maniphest Tasks: T3173

Differential Revision: https://secure.phabricator.com/D10257
This commit is contained in:
epriestley 2014-08-13 15:33:38 -07:00
parent 1ad0e84518
commit f1889aa942
3 changed files with 36 additions and 5 deletions

View file

@ -83,6 +83,8 @@ final class PhabricatorBot extends PhabricatorDaemon {
->connect();
$this->runLoop();
$this->protocolAdapter->disconnect();
}
public function getConfig($key, $default = null) {
@ -104,6 +106,7 @@ final class PhabricatorBot extends PhabricatorDaemon {
$handler->runBackgroundTasks();
}
} while (!$this->shouldExit());
}
public function writeMessage(PhabricatorBotMessage $message) {

View file

@ -21,6 +21,13 @@ abstract class PhabricatorBaseProtocolAdapter {
*/
abstract public function connect();
/**
* Disconnect from the service.
*/
public function disconnect() {
return;
}
/**
* This is the spout for messages coming in from the protocol.
* This will be called in the main event loop of the bot daemon

View file

@ -71,8 +71,13 @@ final class PhabricatorIRCProtocolAdapter
$ok = @stream_select($read, $write, $except, $timeout_sec = 1);
if ($ok === false) {
throw new Exception(
'socket_select() failed: '.socket_strerror(socket_last_error()));
// We may have been interrupted by a signal, like a SIGINT. Try
// selecting again. If the second select works, conclude that the failure
// was most likely because we were signaled.
$ok = @stream_select($read, $write, $except, $timeout_sec = 0);
if ($ok === false) {
throw new Exception('stream_select() failed!');
}
}
if ($read) {
@ -102,6 +107,8 @@ final class PhabricatorIRCProtocolAdapter
$len = fwrite($this->socket, $this->writeBuffer);
if ($len === false) {
throw new Exception('fwrite() failed!');
} else if ($len === 0) {
break;
} else {
$messages[] = id(new PhabricatorBotMessage())
->setCommand('LOG')
@ -250,8 +257,22 @@ final class PhabricatorIRCProtocolAdapter
$data);
}
public function __destruct() {
$this->write('QUIT Goodbye.');
fclose($this->socket);
public function disconnect() {
// NOTE: FreeNode doesn't show quit messages if you've recently joined a
// channel, presumably to prevent some kind of abuse. If you're testing
// this, you may need to stay connected to the network for a few minutes
// before it works. If you disconnect too quickly, the server will replace
// your message with a "Client Quit" message.
$quit = $this->getConfig('quit', pht('Shutting down.'));
$this->write("QUIT :{$quit}");
// Flush the write buffer.
while (strlen($this->writeBuffer)) {
$this->getNextMessages(0);
}
@fclose($this->socket);
$this->socket = null;
}
}