[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/daemon/bot/handler/ -> PhabricatorBotHandler.php (source)

   1  <?php
   2  
   3  /**
   4   * Responds to IRC messages. You plug a bunch of these into a
   5   * @{class:PhabricatorBot} to give it special behavior.
   6   */
   7  abstract class PhabricatorBotHandler {
   8  
   9    private $bot;
  10  
  11    final public function __construct(PhabricatorBot $irc_bot) {
  12      $this->bot = $irc_bot;
  13    }
  14  
  15    final protected function writeMessage(PhabricatorBotMessage $message) {
  16      $this->bot->writeMessage($message);
  17      return $this;
  18    }
  19  
  20    final protected function getConduit() {
  21      return $this->bot->getConduit();
  22    }
  23  
  24    final protected function getConfig($key, $default = null) {
  25      return $this->bot->getConfig($key, $default);
  26    }
  27  
  28    final protected function getURI($path) {
  29      $base_uri = new PhutilURI($this->bot->getConfig('conduit.uri'));
  30      $base_uri->setPath($path);
  31      return (string)$base_uri;
  32    }
  33  
  34    final protected function getServiceName() {
  35      return $this->bot->getAdapter()->getServiceName();
  36    }
  37  
  38    final protected function getServiceType() {
  39      return $this->bot->getAdapter()->getServiceType();
  40    }
  41  
  42    abstract public function receiveMessage(PhabricatorBotMessage $message);
  43  
  44    public function runBackgroundTasks() {
  45      return;
  46    }
  47  
  48    public function replyTo(PhabricatorBotMessage $original_message, $body) {
  49      if ($original_message->getCommand() != 'MESSAGE') {
  50        throw new Exception(
  51          'Handler is trying to reply to something which is not a message!');
  52      }
  53  
  54      $reply = id(new PhabricatorBotMessage())
  55        ->setCommand('MESSAGE');
  56  
  57      if ($original_message->getTarget()->isPublic()) {
  58        // This is a public target, like a chatroom. Send the response to the
  59        // chatroom.
  60        $reply->setTarget($original_message->getTarget());
  61      } else {
  62        // This is a private target, like a private message. Send the response
  63        // back to the sender (presumably, we are the target).
  64        $reply->setTarget($original_message->getSender());
  65      }
  66  
  67      $reply->setBody($body);
  68  
  69      return $this->writeMessage($reply);
  70    }
  71  
  72  }


Generated: Sun Nov 30 09:20:46 2014 Cross-referenced by PHPXref 0.7.1