From fd0f4d9c523d64dd1c18dc5fa343a09ee071b598 Mon Sep 17 00:00:00 2001 From: epriestley Date: Tue, 23 Aug 2011 13:51:13 -0700 Subject: [PATCH] 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 --- .../daemon/irc/bot/PhabricatorIRCBot.php | 18 ++++++++---------- .../irc/handler/base/PhabricatorIRCHandler.php | 4 ++++ .../protocol/PhabricatorIRCProtocolHandler.php | 9 +++++++++ 3 files changed, 21 insertions(+), 10 deletions(-) diff --git a/src/infrastructure/daemon/irc/bot/PhabricatorIRCBot.php b/src/infrastructure/daemon/irc/bot/PhabricatorIRCBot.php index 8ba5df7fa1..ae2b394133 100644 --- a/src/infrastructure/daemon/irc/bot/PhabricatorIRCBot.php +++ b/src/infrastructure/daemon/irc/bot/PhabricatorIRCBot.php @@ -35,6 +35,7 @@ final class PhabricatorIRCBot extends PhabricatorDaemon { private $readBuffer; private $conduit; + private $config; public function run() { @@ -51,7 +52,6 @@ final class PhabricatorIRCBot extends PhabricatorDaemon { $server = idx($config, 'server'); $port = idx($config, 'port', 6667); - $join = idx($config, 'join', array()); $handlers = idx($config, 'handlers', array()); $pass = idx($config, 'pass'); $nick = idx($config, 'nick', 'phabot'); @@ -59,15 +59,13 @@ final class PhabricatorIRCBot extends PhabricatorDaemon { $ssl = idx($config, 'ssl', false); $nickpass = idx($config, 'nickpass'); + $this->config = $config; + if (!preg_match('/^[A-Za-z0-9_`[{}^|\]\\-]+$/', $nick)) { throw new Exception( "Nickname '{$nick}' is invalid!"); } - if (!$join) { - throw new Exception("No channels to 'join' in config!"); - } - foreach ($handlers as $handler) { $obj = newv($handler, array($this)); $this->handlers[] = $obj; @@ -114,17 +112,17 @@ final class PhabricatorIRCBot extends PhabricatorDaemon { } if ($nickpass) { - $this->writeCommand("NickServ IDENTIFY ", "{$nickpass}"); + $this->writeCommand("NickServ IDENTIFY ", "{$nickpass}"); } $this->writeCommand('NICK', "{$nick}"); - foreach ($join as $channel) { - $this->writeCommand('JOIN', "{$channel}"); - } - $this->runSelectLoop(); } + public function getConfig($key, $default = null) { + return idx($this->config, $key, $default); + } + private function runSelectLoop() { do { $this->stillWorking(); diff --git a/src/infrastructure/daemon/irc/handler/base/PhabricatorIRCHandler.php b/src/infrastructure/daemon/irc/handler/base/PhabricatorIRCHandler.php index 755824273e..4a66ee418b 100644 --- a/src/infrastructure/daemon/irc/handler/base/PhabricatorIRCHandler.php +++ b/src/infrastructure/daemon/irc/handler/base/PhabricatorIRCHandler.php @@ -39,6 +39,10 @@ abstract class PhabricatorIRCHandler { return $this->bot->getConduit(); } + final protected function getConfig($key, $default = null) { + return $this->bot->getConfig($key, $default); + } + abstract public function receiveMessage(PhabricatorIRCMessage $message); } diff --git a/src/infrastructure/daemon/irc/handler/protocol/PhabricatorIRCProtocolHandler.php b/src/infrastructure/daemon/irc/handler/protocol/PhabricatorIRCProtocolHandler.php index 9ba3cda05a..a03ecf1c8e 100644 --- a/src/infrastructure/daemon/irc/handler/protocol/PhabricatorIRCProtocolHandler.php +++ b/src/infrastructure/daemon/irc/handler/protocol/PhabricatorIRCProtocolHandler.php @@ -25,6 +25,15 @@ class PhabricatorIRCProtocolHandler extends PhabricatorIRCHandler { public function receiveMessage(PhabricatorIRCMessage $message) { 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': $this->write('PONG', $message->getRawData()); break;