From ec306497f54dac4546c99be2802c0d0728eb3926 Mon Sep 17 00:00:00 2001 From: epriestley Date: Thu, 14 Feb 2013 05:07:50 -0800 Subject: [PATCH] Lock down bot adapter API slightly Summary: - Reduce visibiliy of config. - Add a typehint. Test Plan: Ran campfire/irc bots and chatted with them. Reviewers: indiefan Reviewed By: indiefan CC: aran, amerigomasini Differential Revision: https://secure.phabricator.com/D4923 --- .../bot/adapter/PhabricatorBaseProtocolAdapter.php | 7 ++++++- .../adapter/PhabricatorCampfireProtocolAdapter.php | 10 +++++----- .../bot/adapter/PhabricatorIRCProtocolAdapter.php | 14 +++++++------- .../daemon/bot/handler/PhabricatorBotHandler.php | 2 +- 4 files changed, 19 insertions(+), 14 deletions(-) diff --git a/src/infrastructure/daemon/bot/adapter/PhabricatorBaseProtocolAdapter.php b/src/infrastructure/daemon/bot/adapter/PhabricatorBaseProtocolAdapter.php index 55cb90ebd1..3d7c980f26 100644 --- a/src/infrastructure/daemon/bot/adapter/PhabricatorBaseProtocolAdapter.php +++ b/src/infrastructure/daemon/bot/adapter/PhabricatorBaseProtocolAdapter.php @@ -4,13 +4,18 @@ * Defines the api for protocol adapters for @{class:PhabricatorBot} */ abstract class PhabricatorBaseProtocolAdapter { - protected $config; + + private $config; public function setConfig($config) { $this->config = $config; return $this; } + public function getConfig($key, $default = null) { + return idx($this->config, $key, $default); + } + /** * Performs any connection logic necessary for the protocol */ diff --git a/src/infrastructure/daemon/bot/adapter/PhabricatorCampfireProtocolAdapter.php b/src/infrastructure/daemon/bot/adapter/PhabricatorCampfireProtocolAdapter.php index b2e88e2c63..b567a42d1f 100644 --- a/src/infrastructure/daemon/bot/adapter/PhabricatorCampfireProtocolAdapter.php +++ b/src/infrastructure/daemon/bot/adapter/PhabricatorCampfireProtocolAdapter.php @@ -1,7 +1,7 @@ server = idx($this->config, 'server'); - $this->authtoken = idx($this->config, 'authtoken'); - $ssl = idx($this->config, 'ssl', false); - $rooms = idx($this->config, 'join'); + $this->server = $this->getConfig('server'); + $this->authtoken = $this->getConfig('authtoken'); + $ssl = $this->getConfig('ssl', false); + $rooms = $this->getConfig('join'); // First, join the room if (!$rooms) { diff --git a/src/infrastructure/daemon/bot/adapter/PhabricatorIRCProtocolAdapter.php b/src/infrastructure/daemon/bot/adapter/PhabricatorIRCProtocolAdapter.php index f2d34db246..2d799ed4cd 100644 --- a/src/infrastructure/daemon/bot/adapter/PhabricatorIRCProtocolAdapter.php +++ b/src/infrastructure/daemon/bot/adapter/PhabricatorIRCProtocolAdapter.php @@ -1,7 +1,7 @@ 'MESSAGE'); public function connect() { - $nick = idx($this->config, 'nick', 'phabot'); - $server = idx($this->config, 'server'); - $port = idx($this->config, 'port', 6667); - $pass = idx($this->config, 'pass'); - $ssl = idx($this->config, 'ssl', false); - $user = idx($this->config, 'user', $nick); + $nick = $this->getConfig('nick', 'phabot'); + $server = $this->getConfig('server'); + $port = $this->getConfig('port', 6667); + $pass = $this->getConfig('pass'); + $ssl = $this->getConfig('ssl', false); + $user = $this->getConfig('user', $nick); if (!preg_match('/^[A-Za-z0-9_`[{}^|\]\\-]+$/', $nick)) { throw new Exception( diff --git a/src/infrastructure/daemon/bot/handler/PhabricatorBotHandler.php b/src/infrastructure/daemon/bot/handler/PhabricatorBotHandler.php index bf32d01cb6..025cc51a1e 100644 --- a/src/infrastructure/daemon/bot/handler/PhabricatorBotHandler.php +++ b/src/infrastructure/daemon/bot/handler/PhabricatorBotHandler.php @@ -39,7 +39,7 @@ abstract class PhabricatorBotHandler { return; } - public function replyTo($original_message, $body) { + public function replyTo(PhabricatorBotMessage $original_message, $body) { if ($original_message->getCommand() != 'MESSAGE') { throw new Exception( "Handler is trying to reply to something which is not a message!");