1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-11-23 15:22:41 +01:00

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
This commit is contained in:
epriestley 2013-02-14 05:07:50 -08:00
parent 7b133b8bf2
commit ec306497f5
4 changed files with 19 additions and 14 deletions

View file

@ -4,13 +4,18 @@
* Defines the api for protocol adapters for @{class:PhabricatorBot} * Defines the api for protocol adapters for @{class:PhabricatorBot}
*/ */
abstract class PhabricatorBaseProtocolAdapter { abstract class PhabricatorBaseProtocolAdapter {
protected $config;
private $config;
public function setConfig($config) { public function setConfig($config) {
$this->config = $config; $this->config = $config;
return $this; return $this;
} }
public function getConfig($key, $default = null) {
return idx($this->config, $key, $default);
}
/** /**
* Performs any connection logic necessary for the protocol * Performs any connection logic necessary for the protocol
*/ */

View file

@ -1,7 +1,7 @@
<?php <?php
final class PhabricatorCampfireProtocolAdapter final class PhabricatorCampfireProtocolAdapter
extends PhabricatorBaseProtocolAdapter { extends PhabricatorBaseProtocolAdapter {
private $readBuffers; private $readBuffers;
private $authtoken; private $authtoken;
@ -12,10 +12,10 @@ extends PhabricatorBaseProtocolAdapter {
private $inRooms = array(); private $inRooms = array();
public function connect() { public function connect() {
$this->server = idx($this->config, 'server'); $this->server = $this->getConfig('server');
$this->authtoken = idx($this->config, 'authtoken'); $this->authtoken = $this->getConfig('authtoken');
$ssl = idx($this->config, 'ssl', false); $ssl = $this->getConfig('ssl', false);
$rooms = idx($this->config, 'join'); $rooms = $this->getConfig('join');
// First, join the room // First, join the room
if (!$rooms) { if (!$rooms) {

View file

@ -1,7 +1,7 @@
<?php <?php
final class PhabricatorIRCProtocolAdapter final class PhabricatorIRCProtocolAdapter
extends PhabricatorBaseProtocolAdapter { extends PhabricatorBaseProtocolAdapter {
private $socket; private $socket;
@ -13,12 +13,12 @@ extends PhabricatorBaseProtocolAdapter {
'PRIVMSG' => 'MESSAGE'); 'PRIVMSG' => 'MESSAGE');
public function connect() { public function connect() {
$nick = idx($this->config, 'nick', 'phabot'); $nick = $this->getConfig('nick', 'phabot');
$server = idx($this->config, 'server'); $server = $this->getConfig('server');
$port = idx($this->config, 'port', 6667); $port = $this->getConfig('port', 6667);
$pass = idx($this->config, 'pass'); $pass = $this->getConfig('pass');
$ssl = idx($this->config, 'ssl', false); $ssl = $this->getConfig('ssl', false);
$user = idx($this->config, 'user', $nick); $user = $this->getConfig('user', $nick);
if (!preg_match('/^[A-Za-z0-9_`[{}^|\]\\-]+$/', $nick)) { if (!preg_match('/^[A-Za-z0-9_`[{}^|\]\\-]+$/', $nick)) {
throw new Exception( throw new Exception(

View file

@ -39,7 +39,7 @@ abstract class PhabricatorBotHandler {
return; return;
} }
public function replyTo($original_message, $body) { public function replyTo(PhabricatorBotMessage $original_message, $body) {
if ($original_message->getCommand() != 'MESSAGE') { if ($original_message->getCommand() != 'MESSAGE') {
throw new Exception( throw new Exception(
"Handler is trying to reply to something which is not a message!"); "Handler is trying to reply to something which is not a message!");