[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4   * Logs chatter.
   5   */
   6  final class PhabricatorBotLogHandler extends PhabricatorBotHandler {
   7  
   8    private $futures = array();
   9  
  10    public function receiveMessage(PhabricatorBotMessage $message) {
  11      switch ($message->getCommand()) {
  12        case 'MESSAGE':
  13          $target = $message->getTarget();
  14          if (!$target->isPublic()) {
  15            // Don't log private messages, although maybe we should for debugging?
  16            break;
  17          }
  18  
  19          $target_name = $target->getName();
  20  
  21          $logs = array(
  22            array(
  23              'channel' => $target_name,
  24              'type'    => 'mesg',
  25              'epoch'   => time(),
  26              'author'  => $message->getSender()->getName(),
  27              'message' => $message->getBody(),
  28              'serviceName' => $this->getServiceName(),
  29              'serviceType' => $this->getServiceType(),
  30            ),
  31          );
  32  
  33          $this->futures[] = $this->getConduit()->callMethod(
  34            'chatlog.record',
  35            array(
  36              'logs' => $logs,
  37            ));
  38  
  39          $prompts = array(
  40            '/where is the (chat)?log\?/i',
  41            '/where am i\?/i',
  42            '/what year is (this|it)\?/i',
  43          );
  44  
  45          $tell = false;
  46          foreach ($prompts as $prompt) {
  47            if (preg_match($prompt, $message->getBody())) {
  48              $tell = true;
  49              break;
  50            }
  51          }
  52  
  53          if ($tell) {
  54            $response = $this->getURI(
  55              '/chatlog/channel/'.phutil_escape_uri($target_name).'/');
  56  
  57            $this->replyTo($message, $response);
  58          }
  59  
  60          break;
  61      }
  62    }
  63  
  64    public function runBackgroundTasks() {
  65      foreach ($this->futures as $key => $future) {
  66        try {
  67          if ($future->isReady()) {
  68            unset($this->futures[$key]);
  69          }
  70        } catch (Exception $ex) {
  71          unset($this->futures[$key]);
  72          phlog($ex);
  73        }
  74      }
  75    }
  76  
  77  }


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