]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/xmppdaemon.php
Fix error in xmpp help
[quix0rs-gnu-social.git] / scripts / xmppdaemon.php
index b013a34c9da6d9465664b6e056f8f111c03b076f..e067fdcdb232ed30c3eb515ab4fd497aa4445068 100755 (executable)
@@ -29,6 +29,7 @@ define('LACONICA', true);
 
 require_once(INSTALLDIR . '/lib/common.php');
 require_once(INSTALLDIR . '/lib/jabber.php');
+require_once(INSTALLDIR . '/lib/daemon.php');
 
 set_error_handler('common_error_handler');
 
@@ -36,7 +37,7 @@ set_error_handler('common_error_handler');
 # in jabber.php, which create a new XMPP class. A more elegant (?) solution
 # might be to use make this a subclass of XMPP.
 
-class XMPPDaemon {
+class XMPPDaemon extends Daemon {
 
        function XMPPDaemon($resource=NULL) {
                static $attrs = array('server', 'port', 'user', 'password', 'host');
@@ -52,7 +53,7 @@ class XMPPDaemon {
                        $this->resource = common_config('xmpp', 'resource') . 'daemon';
                }
 
-               $this->log(LOG_INFO, "{$this->user}@{$this->server}/{$this->resource}");
+               $this->log(LOG_INFO, "INITIALIZE XMPPDaemon {$this->user}@{$this->server}/{$this->resource}");
        }
 
        function connect() {
@@ -61,22 +62,39 @@ class XMPPDaemon {
 
                $this->log(LOG_INFO, "Connecting to $connect_to on port $this->port");
 
-               $this->conn = jabber_connect($this->resource, "Send me a message to post a notice", 100);
+               $this->conn = jabber_connect($this->resource);
 
                if (!$this->conn) {
                        return false;
                }
-
+               
+               $this->conn->setReconnectTimeout(600);
+               
+               jabber_send_presence("Send me a message to post a notice", 'available',
+                                                        NULL, 'available', 100);
                return !$this->conn->isDisconnected();
        }
 
-       function handle() {
-               $this->conn->addEventHandler('message', 'handle_message', $this);
-               $this->conn->addEventHandler('presence', 'handle_presence', $this);
-               
-               $this->conn->process();
+       function name() {
+               return strtolower('xmppdaemon.'.$this->resource);
+       }
+       
+       function run() {
+               if ($this->connect()) {
+                       
+                       $this->conn->addEventHandler('message', 'handle_message', $this);
+                       $this->conn->addEventHandler('presence', 'handle_presence', $this);
+                       $this->conn->addEventHandler('reconnect', 'handle_reconnect', $this);
+                       
+                       $this->conn->process();
+               }
        }
 
+       function handle_reconnect(&$pl) {
+               $this->conn->processUntil('session_start');
+               $this->conn->presence('Send me a message to post a notice', 'available', NULL, 'available', 100);
+       }
+       
        function get_user($from) {
                $user = User::staticGet('jabber', jabber_normalize_jid($from));
                return $user;
@@ -91,6 +109,17 @@ class XMPPDaemon {
                }
 
                $from = jabber_normalize_jid($pl['from']);
+
+               # Forwarded from another daemon (probably a broadcaster) for
+               # us to handle
+
+               if ($this->is_self($from)) {
+                       $from = $this->get_ofrom($pl);
+                       if (is_null($from) || $this->is_self($from)) {
+                               return;
+                       }
+               }
+
                $user = $this->get_user($from);
 
                if (!$user) {
@@ -116,6 +145,47 @@ class XMPPDaemon {
                        }
                        $this->add_notice($user, $pl);
                }
+               
+               $user->free();
+               unset($user);
+       }
+
+       function is_self($from) {
+               return preg_match('/^'.strtolower(jabber_daemon_address()).'/', strtolower($from));
+       }
+       
+       function get_ofrom($pl) {
+               $xml = $pl['raw'];
+               $addresses = $xml->sub('addresses');
+               if (!$addresses) {
+                       $this->log(LOG_WARNING, 'Forwarded message without addresses');
+                       return NULL;
+               }
+               $address = $addresses->sub('address');
+               if (!$address) {
+                       $this->log(LOG_WARNING, 'Forwarded message without address');
+                       return NULL;
+               }
+               if (!array_key_exists('type', $address->attrs)) {
+                       $this->log(LOG_WARNING, 'No type for forwarded message');
+                       return NULL;
+               }
+               $type = $address->attrs['type'];
+               if ($type != 'ofrom') {
+                       $this->log(LOG_WARNING, 'Type of forwarded message is not ofrom');
+                       return NULL;
+               }
+               if (!array_key_exists('jid', $address->attrs)) {
+                       $this->log(LOG_WARNING, 'No jid for forwarded message');
+                       return NULL;
+               }
+               $jid = $address->attrs['jid'];
+               if (!$jid) {
+                       $this->log(LOG_WARNING, 'Could not get jid from address');
+                       return NULL;
+               }
+               $this->log(LOG_DEBUG, 'Got message forwarded from jid ' . $jid);
+               return $jid;
        }
 
        function is_autoreply($txt) {
@@ -214,6 +284,8 @@ class XMPPDaemon {
                common_broadcast_notice($notice);
                $this->log(LOG_INFO,
                                   'Added notice ' . $notice->id . ' from user ' . $user->nickname);
+               $notice->free();
+               unset($notice);
        }
 
        function handle_presence(&$pl) {
@@ -243,6 +315,8 @@ class XMPPDaemon {
                                                           ' status from presence.');
                                        $this->add_notice($user, $pl);
                                }
+                               $user->free();
+                               unset($user);
                        }
                        break;
                }
@@ -257,14 +331,13 @@ class XMPPDaemon {
        }
 }
 
+ini_set("max_execution_time", "0");
+ini_set("max_input_time", "0");
+set_time_limit(0);
 mb_internal_encoding('UTF-8');
 
-$resource = ($argc > 1) ? $argv[1] : NULL;
+$resource = ($argc > 1) ? $argv[1] : (common_config('xmpp','resource') . '-listen');
 
 $daemon = new XMPPDaemon($resource);
 
-if ($daemon->connect()) {
-       $daemon->handle();
-}
-
-?>
+$daemon->runOnce();