Differential D5873 Diff 13013 src/infrastructure/daemon/bot/adapter/PhabricatorJabberProtocolAdapter.php
Changeset View
Changeset View
Standalone View
Standalone View
src/infrastructure/daemon/bot/adapter/PhabricatorJabberProtocolAdapter.php
- This file was added.
| <?php | |||||
| require_once dirname(phutil_get_library_root('phabricator')).'/externals/JAXL/jaxl.php'; | |||||
| final class PhabricatorJabberProtocolAdapter | |||||
| extends PhabricatorBaseProtocolAdapter { | |||||
| public function getServiceType() { | |||||
| return 'Jabber'; | |||||
| } | |||||
| public function getServiceName() { | |||||
| return $this->getConfig('server'); | |||||
| } | |||||
| // Hash map of command translations | |||||
| public static $commandTranslations = array( | |||||
| 'PRIVMSG' => 'MESSAGE'); | |||||
| public function connect() { | |||||
| $server = $this->getConfig('server'); | |||||
| $port = $this->getConfig('port', 5222); | |||||
| $pass = $this->getConfig('pass'); | |||||
| $user = $this->getConfig('user'); | |||||
| $this->muc_host = $this->getConfig('muc_domain', $server); | |||||
| $this->nick = $this->getConfig('nick', $user); | |||||
| $this->messages = array(); | |||||
| $this->conn = new JAXL(array('jid' => "$user@$server", 'pass' => $pass, 'port' => $port, 'priv_dir' => '/tmp/.jaxl', 'log_level' => JAXL_DEBUG)); | |||||
| $this->conn->require_xep(array('0045', '0199')); // 0045 is xep for MUC and 0199 is for ping | |||||
| $this->conn->add_cb('on_auth_success', array($this, 'onAuthSuccess')); | |||||
| $this->conn->add_cb('on_groupchat_message', array($this, 'onGroupMessage')); | |||||
| $this->conn->connect($this->conn->get_socket_path()); | |||||
| $this->conn->start_stream(); | |||||
| JAXLLoop::$clock = new JAXLClock(); | |||||
| } | |||||
| public function onAuthSuccess() { | |||||
| $this->conn->set_status(''); | |||||
| foreach ($this->getConfig('join') as $room) { | |||||
| $this->conn->xeps['0045']->join_room($room.'@'.$this->muc_host.'/'.$this->nick, array('no_history' => true)); | |||||
| } | |||||
| } | |||||
| public function onGroupMessage($stanza) { | |||||
| if (!$stanza->exists('body')) { | |||||
| return; | |||||
| } | |||||
| $senderJid = new XMPPJid($stanza->from); | |||||
| $sender = id(new PhabricatorBotUser())->setName($senderJid->resource); | |||||
| $target = id(new PhabricatorBotChannel())->setName($senderJid->node.'@'.$senderJid->domain); | |||||
| $this->messages[] = id(new PhabricatorBotMessage()) | |||||
| ->setCommand("MESSAGE") | |||||
| ->setSender($sender) | |||||
| ->setTarget($target) | |||||
| ->setBody($stanza->body); | |||||
| } | |||||
| public function getNextMessages($poll_frequency) { | |||||
| JAXLLoop::$secs = $poll_frequency; | |||||
| JAXLLoop::$usecs = 0; | |||||
| JAXLLoop::select(); | |||||
| $messages = $this->messages; | |||||
| $this->messages = array(); | |||||
| return $messages; | |||||
| } | |||||
| public function writeMessage(PhabricatorBotMessage $message) { | |||||
| switch ($message->getCommand()) { | |||||
| case 'MESSAGE': | |||||
| case 'PASTE': | |||||
| $name = $message->getTarget()->getName(); | |||||
| $body = $message->getBody(); | |||||
| $this->conn->xeps['0045']->send_groupchat($name, $body); | |||||
| return true; | |||||
| default: | |||||
| return false; | |||||
| } | |||||
| } | |||||
| public function __destruct() { | |||||
| $this->conn->disconnect(); | |||||
| } | |||||
| } | |||||