]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
More work on adapting the phpmsnclass to work with the IM architecture (far from...
authorLuke Fitzgerald <lw.fitzgerald@googlemail.com>
Sat, 12 Jun 2010 18:49:28 +0000 (19:49 +0100)
committerLuke Fitzgerald <lw.fitzgerald@googlemail.com>
Sat, 12 Jun 2010 18:49:28 +0000 (19:49 +0100)
plugins/Msn/extlib/phpmsnclass/msn.class.php
plugins/Msn/msnmanager.php

index 355d828eb533548c1df39780d964e1b5e4341cc6..c387bbeae9958e88fd22175551e8f325de6cd038 100644 (file)
@@ -3203,13 +3203,6 @@ X-OIM-Sequence-Num: 1
         $data = $this->ns_readln();\r
         /*if($data===false)\r
         {\r
-            //If No NS Message Process SendMessageFileQueue\r
-            if (time()-$this->LastPing > $this->ping_wait)\r
-            {\r
-                // NS: >>> PNG\r
-                $this->ns_writeln("PNG");\r
-                $this->LastPing = time();\r
-            }\r
             if(count($this->ChildProcess)<$this->MAXChildProcess)\r
             {\r
                 $Index=0;\r
@@ -3626,8 +3619,8 @@ X-OIM-Sequence-Num: 1
                 break;\r
             case 'QNG':\r
                 // NS: <<< QNG {time}\r
-                @list(/* QNG */, $this->ping_wait) = @explode(' ', $data);\r
-                if ($this->ping_wait == 0) $this->ping_wait = 50;\r
+                //@list(/* QNG */, $this->ping_wait) = @explode(' ', $data);\r
+                //if ($this->ping_wait == 0) $this->ping_wait = 50;\r
                 //if (is_int($use_ping) && $use_ping > 0) $ping_wait = $use_ping;\r
                 //Mod by Ricky Set Online\r
                 break;\r
@@ -3682,31 +3675,100 @@ X-OIM-Sequence-Num: 1
         }\r
     }\r
     \r
-    public function SendMessage($Message, $To) {\r
-        if(!is_array($To))\r
-            $To=array($To);\r
-        $Receiver='';\r
-        foreach($To as $Email)\r
+    public function sendMessageViaSB($message, $to) {\r
+        $socket = $this->switchBoardSessions[$to]['socket'];\r
+        $lastActive = $this->switchBoardSessions[$to]['lastActive'];\r
+        $joined = $this->switchBoardSessions[$to]['joined'];\r
+        \r
+        //TODO Probably not needed (we're not running in a loop anymore)\r
+        /*if($this->kill_me)\r
         {\r
-            list($name,$host,$network)=explode('@',$Email);\r
+            $this->log_message("*** SB Okay, kill me now!");\r
+            endSBSession($socket);\r
+        }*/\r
+        \r
+        if(!$Joined) {\r
+            // If our participant has not joined the session yet we can't message them!\r
+            //TODO Check the behaviour of the queue runner when we return false\r
+            return false;\r
+        }\r
+        \r
+        $aMessage = $this->getMessage($Message);\r
+        //CheckEmotion...\r
+        $MsnObjDefine=$this->GetMsnObjDefine($aMessage);\r
+        if($MsnObjDefine !== '')\r
+        {\r
+            $SendString="MIME-Version: 1.0\r\nContent-Type: text/x-mms-emoticon\r\n\r\n$MsnObjDefine";\r
+            $len = strlen($SendString);\r
+            $this->SB_writeln("MSG $id N $len");\r
+            $id++;\r
+            $this->SB_writedata($SendString);\r
+            $this->id++;\r
+        }\r
+        $len = strlen($aMessage);\r
+        $this->SB_writeln("MSG $id N $len");\r
+        \r
+        // Increment the trID\r
+        $this->switchBoardSessions[$to]['id']++;\r
+        \r
+        $this->SB_writedata($aMessage);\r
+        \r
+        if (feof($this->SBFp))\r
+        {\r
+            // lost connection? error? try OIM later\r
+            @fclose($this->SBFp);\r
+            //TODO introduce callback to add offline message to queue?\r
+            return false;\r
+        }\r
+        $this->SB_writeln("OUT");\r
+        @fclose($this->SBFp);\r
+        return true;\r
+    }\r
+    \r
+    //TODO Not sure if this is needed?\r
+    private function endSBSession($socket) {\r
+        if (feof($this->SBFp))\r
+        {\r
+            // lost connection? error? try OIM later\r
+            @fclose($this->SBFp);\r
+            return false;\r
+        }\r
+        $this->SB_writeln("OUT");\r
+        @fclose($this->SBFp);\r
+        return true;\r
+    }\r
+    \r
+    public function sendMessage($message, $to) {\r
+        if($message != '') {\r
+            list($name,$host,$network)=explode('@',$to);\r
             $network=$network==''?1:$network;\r
-            if($network==1 && isset($this->switchBoardSessions[$Email]) ) {\r
-                $this->debug_message("*** SendMessage to $Receiver use SB message queue.");\r
-                array_push($this->SwitchBoardMessageQueue,$Message);\r
-                continue;\r
+            \r
+            if($network === 1 && isset($this->switchBoardSessions[$to])) {\r
+                $recipient = $name . $host;\r
+                $this->debug_message("*** Sending Message to $recipient using existing SB session");\r
+                $this->sendMessageViaSB($message, $recipient);\r
+            } else {\r
+                $this->debug_message("*** Not MSN network or no existing SB session");\r
+                \r
             }\r
-            $Receiver.="$name@$host@$network,";\r
         }\r
-        if($Receiver=='') return;\r
-        $Receiver=substr($Receiver,0,-1);\r
-        $this->debug_message("*** SendMessage to $Receiver use File queue.");\r
-        file_put_contents($FileName,"TO: $Receiver\n$Message\n");\r
+    }\r
+    \r
+    /**\r
+     * Sends a ping command\r
+     * \r
+     * Should be called about every 50 seconds\r
+     */\r
+    public function send_ping() {\r
+        // NS: >>> PNG\r
+        $this->ns_writeln("PNG");\r
     }\r
     \r
     public function getNSSocket() {\r
         return $this->NSfp;\r
     }\r
     \r
+    // TODO Allow for multiple SB session sockets\r
     public function getSBSocket() {\r
         return $this->SBfp;\r
     }\r
index 72de11cb103dd826e2b63b08f0b26b7c77db0d06..aae6906d6c9643fd45da58126a586fc4f6103831 100644 (file)
@@ -32,8 +32,12 @@ if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
 
 class MsnManager extends ImManager
 {
-
     public $conn = null;
+    
+    protected $lastping = null;
+    
+    const PING_INTERVAL = 50;
+    
     /**
      * Initialize connection to server.
      * @return boolean true on success
@@ -58,9 +62,21 @@ class MsnManager extends ImManager
             return array();
         }
     }
-
+    
+    /**
+     * Idle processing for io manager's execution loop.
+     * Send keepalive pings to server.
+     */
+    public function idle($timeout=0)
+    {
+        $now = time();
+        if (empty($this->lastping) || $now - $this->lastping > self::PING_INTERVAL) {
+            $this->send_ping();
+        }
+    }
+    
     /**
-     * Process AIM events that have come in over the wire.
+     * Process MSN events that have come in over the wire.
      * @param resource $socket
      */
     public function handleInput($socket)
@@ -83,10 +99,24 @@ class MsnManager extends ImManager
                         );
             $this->conn->registerHandler("IMIn", array($this, 'handle_msn_message'));
             $this->conn->signon();
+            $this->lastping = time();
         }
         return $this->conn;
     }
-
+    
+    function send_ping() {
+        $this->connect();
+        if (!$this->conn) {
+            return false;
+        }
+        
+        $now = time();
+        
+        $this->conn->send_ping();
+        $this->lastping = $now;
+        return true;
+    }
+    
     function handle_msn_message($data)
     {
         $this->plugin->enqueue_incoming_raw($data);