Delay sending JOIN command until after MOTD finishes for IRC bot

Summary: Do JOIN in the protocol handler, after we receive 376 ("end of motd").

Test Plan: Ran bot, it joined a channel after receieving a 376 command.

Reviewers: moos3, codeblock, aran, jungejason, tuomaspelkonen

Reviewed By: moos3

CC: aran, moos3

Differential Revision: 855
This commit is contained in:
epriestley 2011-08-23 13:51:13 -07:00
parent 30024a8d86
commit fd0f4d9c52
3 changed files with 21 additions and 10 deletions

View file

@ -35,6 +35,7 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
private $readBuffer; private $readBuffer;
private $conduit; private $conduit;
private $config;
public function run() { public function run() {
@ -51,7 +52,6 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
$server = idx($config, 'server'); $server = idx($config, 'server');
$port = idx($config, 'port', 6667); $port = idx($config, 'port', 6667);
$join = idx($config, 'join', array());
$handlers = idx($config, 'handlers', array()); $handlers = idx($config, 'handlers', array());
$pass = idx($config, 'pass'); $pass = idx($config, 'pass');
$nick = idx($config, 'nick', 'phabot'); $nick = idx($config, 'nick', 'phabot');
@ -59,15 +59,13 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
$ssl = idx($config, 'ssl', false); $ssl = idx($config, 'ssl', false);
$nickpass = idx($config, 'nickpass'); $nickpass = idx($config, 'nickpass');
$this->config = $config;
if (!preg_match('/^[A-Za-z0-9_`[{}^|\]\\-]+$/', $nick)) { if (!preg_match('/^[A-Za-z0-9_`[{}^|\]\\-]+$/', $nick)) {
throw new Exception( throw new Exception(
"Nickname '{$nick}' is invalid!"); "Nickname '{$nick}' is invalid!");
} }
if (!$join) {
throw new Exception("No channels to 'join' in config!");
}
foreach ($handlers as $handler) { foreach ($handlers as $handler) {
$obj = newv($handler, array($this)); $obj = newv($handler, array($this));
$this->handlers[] = $obj; $this->handlers[] = $obj;
@ -118,11 +116,11 @@ final class PhabricatorIRCBot extends PhabricatorDaemon {
} }
$this->writeCommand('NICK', "{$nick}"); $this->writeCommand('NICK', "{$nick}");
foreach ($join as $channel) { $this->runSelectLoop();
$this->writeCommand('JOIN', "{$channel}");
} }
$this->runSelectLoop(); public function getConfig($key, $default = null) {
return idx($this->config, $key, $default);
} }
private function runSelectLoop() { private function runSelectLoop() {

View file

@ -39,6 +39,10 @@ abstract class PhabricatorIRCHandler {
return $this->bot->getConduit(); return $this->bot->getConduit();
} }
final protected function getConfig($key, $default = null) {
return $this->bot->getConfig($key, $default);
}
abstract public function receiveMessage(PhabricatorIRCMessage $message); abstract public function receiveMessage(PhabricatorIRCMessage $message);
} }

View file

@ -25,6 +25,15 @@ class PhabricatorIRCProtocolHandler extends PhabricatorIRCHandler {
public function receiveMessage(PhabricatorIRCMessage $message) { public function receiveMessage(PhabricatorIRCMessage $message) {
switch ($message->getCommand()) { switch ($message->getCommand()) {
case '376': // End of MOTD
$join = $this->getConfig('join');
if (!$join) {
throw new Exception("Not configured to join any channels!");
}
foreach ($join as $channel) {
$this->write('JOIN', $channel);
}
break;
case 'PING': case 'PING':
$this->write('PONG', $message->getRawData()); $this->write('PONG', $message->getRawData());
break; break;