]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
forward messages from queuehandler to listener
authorEvan Prodromou <evan@prodromou.name>
Sat, 30 Aug 2008 04:07:17 +0000 (00:07 -0400)
committerEvan Prodromou <evan@prodromou.name>
Sat, 30 Aug 2008 04:07:17 +0000 (00:07 -0400)
The queuehandler sends messages to the user. If the user replies, the
messages are typically sent directly to the queuehandler (including
resource), not the default bot JID.

We add a little code for the xmppqueuehandler to periodically service
its queue of received messages. Received messages are forwarded to the
listener to deal with. We use XEP 33, 'addresses', to note the
original sender.

The xmppdaemon checks to see if the 'from' on a message is the daemon
address. If so, it looks for a XEP 33 'addresses' stanza, with an
'ofrom' address, which shows who it was originally is from.

This should let us send from one resource ID and still process
incoming messages in a different connection.

darcs-hash:20080830040717-84dde-59c75bfb107a1dd5fd531106751b4544bfd15656.gz

scripts/xmppdaemon.php
scripts/xmppqueuehandler.php

index ed523cd814c75e3c2ec72efdff5826e28ac360df..b1cdb914c1348c592c3b49bdc6b16854883dba9b 100755 (executable)
@@ -73,7 +73,7 @@ class XMPPDaemon {
        function handle() {
                $this->conn->addEventHandler('message', 'handle_message', $this);
                $this->conn->addEventHandler('presence', 'handle_presence', $this);
-               
+
                $this->conn->process();
        }
 
@@ -91,6 +91,17 @@ class XMPPDaemon {
                }
 
                $from = jabber_normalize_jid($pl['from']);
+
+               # Forwarded from another daemon (probably a broadcaster) for
+               # us to handle
+
+               if (preg_match('/^'.jabber_daemon_address().'/', $from)) {
+                       $from = $this->get_ofrom($pl);
+                       if (is_null($from)) {
+                               return;
+                       }
+               }
+
                $user = $this->get_user($from);
 
                if (!$user) {
@@ -118,6 +129,26 @@ class XMPPDaemon {
                }
        }
 
+       function get_ofrom($pl) {
+               $xml = $pl['raw'];
+               $addresses = $xml->sub('adddresses');
+               if (!$addresses) {
+                       $this->log(LOG_WARNING, 'Forwarded message without addresses');
+                       return NULL;
+               }
+               $address = $xml->sub('address');
+               if (!$address) {
+                       $this->log(LOG_WARNING, 'Forwarded message without address');
+                       return NULL;
+               }
+               $type = $address->attr('type');
+               if ($type != 'ofrom') {
+                       $this->log(LOG_WARNING, 'Type of forwarded message is not ofrom');
+                       return NULL;
+               }
+               return $address->attr('jid');
+       }
+
        function is_autoreply($txt) {
                if (preg_match('/[\[\(]?[Aa]uto-?[Rr]eply[\]\)]/', $txt)) {
                        return true;
index 83928982bfa4db8e2e53f4bacb17626c40896938..a82f5b9445c8c3a9a3627373e8ed11c8c22de96c 100755 (executable)
@@ -36,11 +36,11 @@ set_error_handler('common_error_handler');
 class XmppQueueHandler extends QueueHandler {
 
        var $conn = NULL;
-       
+
        function transport() {
                return 'jabber';
        }
-       
+
        function start() {
                # Low priority; we don't want to receive messages
                $this->conn = jabber_connect($this->_id, NULL, -1);
@@ -56,11 +56,29 @@ class XmppQueueHandler extends QueueHandler {
                # Process the queue for a second
                $this->conn->processTime(1);
        }
-       
+
        function finish() {
        }
-       
+
        function forward_message(&$pl) {
+               $listener = $this->listener();
+               $this->log(LOG_INFO, 'Forwarding message from ' . $pl['from'] . ' to ' . $listener);
+               $this->conn->message($this->listener(), $pl['body'], 'chat', NULL, $this->ofrom($pl['from']));
+       }
+
+       function ofrom($from) {
+               $address = "<addresses xmlns='http://jabber.org/protocol/address'>\n";
+               $address .= "<address type='ofrom' jid='$from' />\n";
+               $address .= "</addresses>\n";
+               return $address;
+       }
+
+       function listener() {
+               if (common_config('xmpp', 'listener')) {
+                       return common_config('xmpp', 'listener');
+               } else {
+                       return jabber_daemon_address() . '/' . common_config('xmpp','resource') . '-listener';
+               }
        }
 }