mirror of
https://we.phorge.it/source/phorge.git
synced 2025-01-03 11:21:01 +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:
parent
1ad0e84518
commit
f1889aa942
3 changed files with 36 additions and 5 deletions
|
@ -83,6 +83,8 @@ final class PhabricatorBot extends PhabricatorDaemon {
|
||||||
->connect();
|
->connect();
|
||||||
|
|
||||||
$this->runLoop();
|
$this->runLoop();
|
||||||
|
|
||||||
|
$this->protocolAdapter->disconnect();
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getConfig($key, $default = null) {
|
public function getConfig($key, $default = null) {
|
||||||
|
@ -104,6 +106,7 @@ final class PhabricatorBot extends PhabricatorDaemon {
|
||||||
$handler->runBackgroundTasks();
|
$handler->runBackgroundTasks();
|
||||||
}
|
}
|
||||||
} while (!$this->shouldExit());
|
} while (!$this->shouldExit());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public function writeMessage(PhabricatorBotMessage $message) {
|
public function writeMessage(PhabricatorBotMessage $message) {
|
||||||
|
|
|
@ -21,6 +21,13 @@ abstract class PhabricatorBaseProtocolAdapter {
|
||||||
*/
|
*/
|
||||||
abstract public function connect();
|
abstract public function connect();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Disconnect from the service.
|
||||||
|
*/
|
||||||
|
public function disconnect() {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This is the spout for messages coming in from the protocol.
|
* This is the spout for messages coming in from the protocol.
|
||||||
* This will be called in the main event loop of the bot daemon
|
* This will be called in the main event loop of the bot daemon
|
||||||
|
|
|
@ -71,8 +71,13 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
|
|
||||||
$ok = @stream_select($read, $write, $except, $timeout_sec = 1);
|
$ok = @stream_select($read, $write, $except, $timeout_sec = 1);
|
||||||
if ($ok === false) {
|
if ($ok === false) {
|
||||||
throw new Exception(
|
// We may have been interrupted by a signal, like a SIGINT. Try
|
||||||
'socket_select() failed: '.socket_strerror(socket_last_error()));
|
// 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) {
|
if ($read) {
|
||||||
|
@ -102,6 +107,8 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
$len = fwrite($this->socket, $this->writeBuffer);
|
$len = fwrite($this->socket, $this->writeBuffer);
|
||||||
if ($len === false) {
|
if ($len === false) {
|
||||||
throw new Exception('fwrite() failed!');
|
throw new Exception('fwrite() failed!');
|
||||||
|
} else if ($len === 0) {
|
||||||
|
break;
|
||||||
} else {
|
} else {
|
||||||
$messages[] = id(new PhabricatorBotMessage())
|
$messages[] = id(new PhabricatorBotMessage())
|
||||||
->setCommand('LOG')
|
->setCommand('LOG')
|
||||||
|
@ -250,8 +257,22 @@ final class PhabricatorIRCProtocolAdapter
|
||||||
$data);
|
$data);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function __destruct() {
|
public function disconnect() {
|
||||||
$this->write('QUIT Goodbye.');
|
// NOTE: FreeNode doesn't show quit messages if you've recently joined a
|
||||||
fclose($this->socket);
|
// 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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue