]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - scripts/xmppdaemon.php
Merge branch '0.8.x' into 0.9.x
[quix0rs-gnu-social.git] / scripts / xmppdaemon.php
index bd1918ca98aa90b8fbe56e2cb29c75c3577bf348..b2efc07c38c644092b4a1549761efb01af99c330 100755 (executable)
@@ -1,8 +1,8 @@
 #!/usr/bin/env php
 <?php
 /*
- * Laconica - a distributed open-source microblogging tool
- * Copyright (C) 2008, 2009, Control Yourself, Inc.
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2008, 2009, StatusNet, Inc.
  *
  * This program is free software: you can redistribute it and/or modify
  * it under the terms of the GNU Affero General Public License as published by
@@ -60,7 +60,9 @@ class XMPPDaemon extends Daemon
             $this->resource = common_config('xmpp', 'resource') . 'daemon';
         }
 
-        $this->log(LOG_INFO, "INITIALIZE XMPPDaemon {$this->user}@{$this->server}/{$this->resource}");
+        $this->jid = $this->user.'@'.$this->server.'/'.$this->resource;
+
+        $this->log(LOG_INFO, "INITIALIZE XMPPDaemon {$this->jid}");
     }
 
     function connect()
@@ -106,8 +108,23 @@ class XMPPDaemon extends Daemon
 
             $this->log(LOG_DEBUG, "Beginning processing loop.");
 
-            $this->conn->process();
+            while ($this->conn->processTime(60)) {
+                $this->sendPing();
+            }
+        }
+    }
+
+    function sendPing()
+    {
+        if (!isset($this->pingid)) {
+            $this->pingid = 0;
+        } else {
+            $this->pingid++;
         }
+
+        $this->log(LOG_DEBUG, "Sending ping #{$this->pingid}");
+
+               $this->conn->send("<iq from='{$this->jid}' to='{$this->server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
     }
 
     function handle_reconnect(&$pl)
@@ -158,6 +175,10 @@ class XMPPDaemon extends Daemon
 
         $user = $this->get_user($from);
 
+        // For common_current_user to work
+        global $_cur;
+        $_cur = $user;
+
         if (!$user) {
             $this->from_site($from, 'Unknown user; go to ' .
                              common_local_url('imsettings') .
@@ -194,6 +215,7 @@ class XMPPDaemon extends Daemon
 
         $user->free();
         unset($user);
+        unset($_cur);
 
         unset($pl['xml']);
         $pl['xml'] = null;
@@ -294,17 +316,22 @@ class XMPPDaemon extends Daemon
     {
         $body = trim($pl['body']);
         $content_shortened = common_shorten_links($body);
-        if (mb_strlen($content_shortened) > 140) {
+        if (Notice::contentTooLong($content_shortened)) {
           $from = jabber_normalize_jid($pl['from']);
-          $this->from_site($from, "Message too long - maximum is 140 characters, you sent ".mb_strlen($content_shortened));
+          $this->from_site($from, sprintf(_("Message too long - maximum is %d characters, you sent %d"),
+                                          Notice::maxContent(),
+                                          mb_strlen($content_shortened)));
           return;
         }
-        $notice = Notice::saveNew($user->id, $content_shortened, 'xmpp');
-        if (is_string($notice)) {
-            $this->log(LOG_ERR, $notice);
-            $this->from_site($user->jabber, $notice);
+
+        try {
+            $notice = Notice::saveNew($user->id, $content_shortened, 'xmpp');
+        } catch (Exception $e) {
+            $this->log(LOG_ERR, $e->getMessage());
+            $this->from_site($user->jabber, $e->getMessage());
             return;
         }
+
         common_broadcast_notice($notice);
         $this->log(LOG_INFO,
                    'Added notice ' . $notice->id . ' from user ' . $user->nickname);