]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/jabber.php
Added Swedish to list of languages
[quix0rs-gnu-social.git] / lib / jabber.php
index 6479968b30ffca20faebe2c13d4860ec250b81d2..bd7d6b7074d38acbeb713a124d67178812eebfbc 100644 (file)
@@ -21,33 +21,6 @@ if (!defined('LACONICA')) { exit(1); }
 
 require_once('XMPPHP/XMPP.php');
 
-# XXX: something of a hack to work around problems with the XMPPHP lib
-
-class Laconica_XMPP extends XMPPHP_XMPP {
-
-       public function presence($status = null, $show = 'available', $to = null, $type='available', $priority=NULL) {
-               if($type == 'available') $type = '';
-               $to      = htmlspecialchars($to);
-               $status = htmlspecialchars($status);
-               if($show == 'unavailable') $type = 'unavailable';
-
-               $out = "<presence";
-               if($to) $out .= " to='$to'";
-               if($type) $out .= " type='$type'";
-               if($show == 'available' and !$status and is_null($priority)) {
-                       $out .= "/>";
-               } else {
-                       $out .= ">";
-                       if($show != 'available') $out .= "<show>$show</show>";
-                       if($status) $out .= "<status>$status</status>";
-                       if(!is_null($priority)) $out .= "<priority>$priority</priority>";
-                       $out .= "</presence>";
-               }
-
-               $this->send($out);
-       }
-}
-
 function jabber_valid_base_jid($jid) {
        # Cheap but effective
        return Validate::email($jid);
@@ -67,10 +40,10 @@ function jabber_daemon_address() {
        return common_config('xmpp', 'user') . '@' . common_config('xmpp', 'server');
 }
 
-function jabber_connect($resource=NULL, $status=NULL, $priority=NULL) {
+function jabber_connect($resource=NULL) {
        static $conn = NULL;
        if (!$conn) {
-               $conn = new Laconica_XMPP(common_config('xmpp', 'host') ?
+               $conn = new XMPPHP_XMPP(common_config('xmpp', 'host') ?
                                                                common_config('xmpp', 'host') :
                                                                common_config('xmpp', 'server'),
                                                                common_config('xmpp', 'port'),
@@ -84,19 +57,22 @@ function jabber_connect($resource=NULL, $status=NULL, $priority=NULL) {
                                                                common_config('xmpp', 'debug') ?
                                                                XMPPHP_Log::LEVEL_VERBOSE :  NULL
                                                                );
-               $conn->autoSubscribe();
-               $conn->useEncryption(common_config('xmpp', 'encryption'));
 
                if (!$conn) {
                        return false;
                }
-               $conn->connect(true); # true = persistent connection
-               if ($conn->isDisconnected()) {
+
+               $conn->autoSubscribe();
+               $conn->useEncryption(common_config('xmpp', 'encryption'));
+
+               try {
+                       $conn->connect(true); # true = persistent connection
+               } catch (XMPPHP_Exception $e) {
+                       common_log(LOG_ERROR, $e->getMessage());
                        return false;
                }
+
        $conn->processUntil('session_start');
-               $conn->getRoster();
-               $conn->presence($presence, 'available', NULL, 'available', $priority);
        }
        return $conn;
 }
@@ -116,6 +92,7 @@ function jabber_send_notice($to, $notice) {
        $msg = jabber_format_notice($profile, $notice);
        $entry = jabber_format_entry($profile, $notice);
        $conn->message($to, $msg, 'chat', NULL, $entry);
+       $profile->free();
        return true;
 }
 
@@ -155,15 +132,9 @@ function jabber_format_entry($profile, $notice) {
        $address .= "<address type='replyto' jid='" . jabber_daemon_address() . "' />\n";
        $address .= "</addresses>\n";
 
-       $event = "<event xmlns='http://jabber.org/protocol/pubsub#event'>\n";
-    $event .= "<items xmlns='http://jabber.org/protocol/pubsub' ";
-       $event .= "node='" . common_local_url('public') . "'>\n";
-       $event .= "<item id='" . $notice->uri ."' />\n";
-       $event .= "</items>\n";
-       $event .= "</event>\n";
-       # FIXME: include the pubsub event, too.
+       # FIXME: include a pubsub event, too.
+
        return $html . $entry . $address;
-#      return $entry . "\n" . $event;
 }
 
 function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
@@ -175,12 +146,14 @@ function jabber_send_message($to, $body, $type='chat', $subject=NULL) {
        return true;
 }
 
-function jabber_send_presence($status, $show='available', $to=Null) {
+function jabber_send_presence($status, $show='available', $to=NULL,
+                                                         $type = 'available', $priority=NULL)
+{
        $conn = jabber_connect();
        if (!$conn) {
                return false;
        }
-       $conn->presence($status, $show, $to);
+       $conn->presence($status, $show, $to, $type, $priority);
        return true;
 }
 
@@ -232,6 +205,9 @@ function jabber_broadcast_notice($notice) {
        $msg = jabber_format_notice($profile, $notice);
        $entry = jabber_format_entry($profile, $notice);
 
+       $profile->free();
+       unset($profile);
+       
        $sent_to = array();
        $conn = jabber_connect();
 
@@ -249,8 +225,12 @@ function jabber_broadcast_notice($notice) {
                                   'Sending reply notice ' . $notice->id . ' to ' . $user->jabber,
                                   __FILE__);
                $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
+               $conn->processTime(0);
+               $sent_to[$user->id] = 1;
        }
 
+       $user->free();
+       
     # Now, get users subscribed to this profile
 
        $user = new User();
@@ -266,9 +246,13 @@ function jabber_broadcast_notice($notice) {
                                           'Sending notice ' . $notice->id . ' to ' . $user->jabber,
                                           __FILE__);
                        $conn->message($user->jabber, $msg, 'chat', NULL, $entry);
+                       # To keep the incoming queue from filling up, we service it after each send.
+                       $conn->processTime(0);
                }
        }
 
+       $user->free();
+       
        return true;
 }
 
@@ -302,7 +286,9 @@ function jabber_public_notice($notice) {
                                           'Sending notice ' . $notice->id . ' to public listener ' . $address,
                                           __FILE__);
                        $conn->message($address, $msg, 'chat', NULL, $entry);
+                       $conn->processTime(0);
                }
+               $profile->free();
        }
 
        return true;