123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- <?php
- if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
- class XmppManager extends ImManager
- {
- protected $lastping = null;
- protected $pingid = null;
- public $conn = null;
- const PING_INTERVAL = 120;
-
- public function start($master)
- {
- if(parent::start($master))
- {
- $this->connect();
- return true;
- }else{
- return false;
- }
- }
- function send_raw_message($data)
- {
- $this->connect();
- if (!$this->conn || $this->conn->isDisconnected()) {
- return false;
- }
- $this->conn->send($data);
- return true;
- }
-
- function timeout()
- {
- return self::PING_INTERVAL;
- }
-
- public function handleInput($socket)
- {
-
- try {
- common_log(LOG_DEBUG, "Servicing the XMPP queue.");
- $this->stats('xmpp_process');
- $this->conn->processTime(0);
- } catch (XMPPHP_Exception $e) {
- common_log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
- die($e->getMessage());
- }
- }
-
- public function getSockets()
- {
- $this->connect();
- if($this->conn){
- return array($this->conn->getSocket());
- }else{
- return array();
- }
- }
-
- public function idle($timeout=0)
- {
- $now = time();
- if (empty($this->lastping) || $now - $this->lastping > self::PING_INTERVAL) {
- try {
- $this->send_ping();
- } catch (XMPPHP_Exception $e) {
- common_log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
- die($e->getMessage());
- }
- }
- }
- function connect()
- {
- if (!$this->conn || $this->conn->isDisconnected()) {
- $resource = 'queue' . posix_getpid();
- $this->conn = new SharingXMPP($this->plugin->host ?
- $this->plugin->host :
- $this->plugin->server,
- $this->plugin->port,
- $this->plugin->user,
- $this->plugin->password,
- $this->plugin->resource,
- $this->plugin->server,
- $this->plugin->debug ?
- true : false,
- $this->plugin->debug ?
- XMPPHP_Log::LEVEL_VERBOSE : null
- );
- if (!$this->conn) {
- return false;
- }
- $this->conn->addEventHandler('message', 'handle_xmpp_message', $this);
- $this->conn->addEventHandler('reconnect', 'handle_xmpp_reconnect', $this);
- $this->conn->setReconnectTimeout(600);
- $this->conn->autoSubscribe();
- $this->conn->useEncryption($this->plugin->encryption);
- try {
- $this->conn->connect(true);
- } catch (XMPPHP_Exception $e) {
- common_log(LOG_ERR, $e->getMessage());
- return false;
- }
- $this->conn->processUntil('session_start');
-
- $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
- }
- return $this->conn;
- }
- function send_ping()
- {
- $this->connect();
- if (!$this->conn || $this->conn->isDisconnected()) {
- return false;
- }
- $now = time();
- if (!isset($this->pingid)) {
- $this->pingid = 0;
- } else {
- $this->pingid++;
- }
- common_log(LOG_DEBUG, "Sending ping #{$this->pingid}");
- $this->conn->send("<iq from='{" . $this->plugin->daemonScreenname() . "}' to='{$this->plugin->server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
- $this->lastping = $now;
- return true;
- }
- function handle_xmpp_message(&$pl)
- {
- $this->plugin->enqueueIncomingRaw($pl);
- return true;
- }
-
- function handle_xmpp_reconnect(&$pl)
- {
- common_log(LOG_NOTICE, 'XMPP reconnected');
- $this->conn->processUntil('session_start');
-
- $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
- }
-
- function send_presence($status, $show='available', $to=null,
- $type = 'available', $priority=null)
- {
- $this->connect();
- if (!$this->conn || $this->conn->isDisconnected()) {
- return false;
- }
- $this->conn->presence($status, $show, $to, $type, $priority);
- return true;
- }
-
- function special_presence($type, $to=null, $show=null, $status=null)
- {
-
- $this->connect();
- if (!$this->conn || $this->conn->isDisconnected()) {
- return false;
- }
- $to = htmlspecialchars($to);
- $status = htmlspecialchars($status);
- $out = "<presence";
- if ($to) {
- $out .= " to='$to'";
- }
- if ($type) {
- $out .= " type='$type'";
- }
- if ($show == 'available' and !$status) {
- $out .= "/>";
- } else {
- $out .= ">";
- if ($show && ($show != 'available')) {
- $out .= "<show>$show</show>";
- }
- if ($status) {
- $out .= "<status>$status</status>";
- }
- $out .= "</presence>";
- }
- $this->conn->send($out);
- return true;
- }
- }
|