[ Index ]

PHP Cross Reference of Phabricator

title

Body

[close]

/src/infrastructure/daemon/bot/adapter/ -> PhabricatorCampfireProtocolAdapter.php (source)

   1  <?php
   2  
   3  final class PhabricatorCampfireProtocolAdapter
   4    extends PhabricatorBotBaseStreamingProtocolAdapter {
   5  
   6    public function getServiceType() {
   7      return 'Campfire';
   8    }
   9  
  10    protected function buildStreamingUrl($channel) {
  11      $ssl = $this->getConfig('ssl');
  12  
  13      $url = ($ssl) ? 'https://' : 'http://';
  14      $url .= "streaming.campfirenow.com/room/{$channel}/live.json";
  15  
  16      return $url;
  17    }
  18  
  19    protected function processMessage($m_obj) {
  20        $command = null;
  21        switch ($m_obj['type']) {
  22          case 'TextMessage':
  23            $command = 'MESSAGE';
  24            break;
  25          case 'PasteMessage':
  26            $command = 'PASTE';
  27            break;
  28          default:
  29            // For now, ignore anything which we don't otherwise know about.
  30            break;
  31        }
  32  
  33        if ($command === null) {
  34          return false;
  35        }
  36  
  37        // TODO: These should be usernames, not user IDs.
  38        $sender = id(new PhabricatorBotUser())
  39          ->setName($m_obj['user_id']);
  40  
  41        $target = id(new PhabricatorBotChannel())
  42          ->setName($m_obj['room_id']);
  43  
  44        return id(new PhabricatorBotMessage())
  45          ->setCommand($command)
  46          ->setSender($sender)
  47          ->setTarget($target)
  48          ->setBody($m_obj['body']);
  49    }
  50  
  51    public function writeMessage(PhabricatorBotMessage $message) {
  52      switch ($message->getCommand()) {
  53        case 'MESSAGE':
  54          $this->speak(
  55            $message->getBody(),
  56            $message->getTarget());
  57          break;
  58        case 'SOUND':
  59          $this->speak(
  60            $message->getBody(),
  61            $message->getTarget(),
  62            'SoundMessage');
  63          break;
  64        case 'PASTE':
  65          $this->speak(
  66            $message->getBody(),
  67            $message->getTarget(),
  68            'PasteMessage');
  69          break;
  70      }
  71    }
  72  
  73    protected function joinRoom($room_id) {
  74      $this->performPost("/room/{$room_id}/join.json");
  75      $this->inRooms[$room_id] = true;
  76    }
  77  
  78    private function leaveRoom($room_id) {
  79      $this->performPost("/room/{$room_id}/leave.json");
  80      unset($this->inRooms[$room_id]);
  81    }
  82  
  83    private function speak(
  84      $message,
  85      PhabricatorBotTarget $channel,
  86      $type = 'TextMessage') {
  87  
  88      $room_id = $channel->getName();
  89  
  90      $this->performPost(
  91        "/room/{$room_id}/speak.json",
  92        array(
  93          'message' => array(
  94            'type' => $type,
  95            'body' => $message,
  96          ),
  97        ));
  98    }
  99  
 100    public function __destruct() {
 101      foreach ($this->inRooms as $room_id => $ignored) {
 102        $this->leaveRoom($room_id);
 103      }
 104  
 105      if ($this->readHandles) {
 106        foreach ($this->readHandles as $read_handle) {
 107          curl_multi_remove_handle($this->multiHandle, $read_handle);
 108          curl_close($read_handle);
 109        }
 110      }
 111  
 112      curl_multi_close($this->multiHandle);
 113    }
 114  }


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