]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/Msn/msnmanager.php
More info for a proper, fancy-url lighttpd setup
[quix0rs-gnu-social.git] / plugins / Msn / msnmanager.php
index 72de11cb103dd826e2b63b08f0b26b7c77db0d06..82d40d69c92e80eff7d5e42d51b76b159d75ba82 100644 (file)
-<?php
-/*
- * 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
- * the Free Software Foundation, either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
- */
-
-if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
-
-/**
- * AIM background connection manager for AIM-using queue handlers,
- * allowing them to send outgoing messages on the right connection.
- *
- * Input is handled during socket select loop, keepalive pings during idle.
- * Any incoming messages will be handled.
- *
- * In a multi-site queuedaemon.php run, one connection will be instantiated
- * for each site being handled by the current process that has XMPP enabled.
- */
-
-class MsnManager extends ImManager
-{
-
-    public $conn = null;
-    /**
-     * Initialize connection to server.
-     * @return boolean true on success
-     */
-    public function start($master)
-    {
-        if(parent::start($master))
-        {
-            $this->connect();
-            return true;
-        }else{
-            return false;
-        }
-    }
-
-    public function getSockets()
-    {
-        $this->connect();
-        if($this->conn){
-            return $this->conn->getSockets();
-        }else{
-            return array();
-        }
-    }
-
-    /**
-     * Process AIM events that have come in over the wire.
-     * @param resource $socket
-     */
-    public function handleInput($socket)
-    {
-        common_log(LOG_DEBUG, "Servicing the MSN queue.");
-        $this->stats('msn_process');
-        $this->conn->receive();
-    }
-
-    function connect()
-    {
-        if (!$this->conn) {
-            $this->conn=new MSN(array(
-                                      'user' => $this->plugin->user,
-                                      'password' => $this->plugin->password,
-                                      'alias' => $this->plugin->nickname,
-                                      'psm' => 'Send me a message to post a notice',
-                                      'debug' => true
-                                )
-                        );
-            $this->conn->registerHandler("IMIn", array($this, 'handle_msn_message'));
-            $this->conn->signon();
-        }
-        return $this->conn;
-    }
-
-    function handle_msn_message($data)
-    {
-        $this->plugin->enqueue_incoming_raw($data);
-        return true;
-    }
-
-    function send_raw_message($data)
-    {
-        $this->connect();
-        if (!$this->conn) {
-            return false;
-        }
-        $this->conn->sflapSend($data[0],$data[1],$data[2],$data[3]);
-        return true;
-    }
-}
+<?php\r
+/*\r
+ * StatusNet - the distributed open-source microblogging tool\r
+ * Copyright (C) 2008, 2009, StatusNet, Inc.\r
+ *\r
+ * This program is free software: you can redistribute it and/or modify\r
+ * it under the terms of the GNU Affero General Public License as published by\r
+ * the Free Software Foundation, either version 3 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * This program is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU Affero General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Affero General Public License\r
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.\r
+ */\r
+\r
+if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }\r
+\r
+/**\r
+ * MSN background connection manager for MSN-using queue handlers,\r
+ * allowing them to send outgoing messages on the right connection.\r
+ *\r
+ * Input is handled during socket select loop, keepalive pings during idle.\r
+ * Any incoming messages will be handled.\r
+ *\r
+ * In a multi-site queuedaemon.php run, one connection will be instantiated\r
+ * for each site being handled by the current process that has MSN enabled.\r
+ */\r
+class MsnManager extends ImManager {\r
+    public $conn = null;\r
+    protected $lastPing = null;\r
+    protected $pingInterval;\r
+\r
+    /**\r
+     * Initialise connection to server.\r
+     *\r
+     * @return boolean true on success\r
+     */\r
+    public function start($master) {\r
+        if (parent::start($master)) {\r
+            $this->requeue_waiting_messages();\r
+            $this->connect();\r
+            return true;\r
+        } else {\r
+            return false;\r
+        }\r
+    }\r
+\r
+    /**\r
+    * Return any open sockets that the run loop should listen\r
+    * for input on.\r
+    *\r
+    * @return array Array of socket resources\r
+    */\r
+    public function getSockets() {\r
+        $this->connect();\r
+        if ($this->conn) {\r
+            return $this->conn->getSockets();\r
+        } else {\r
+            return array();\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Idle processing for io manager's execution loop.\r
+     * Send keepalive pings to server.\r
+     *\r
+     * @return void\r
+     */\r
+    public function idle($timeout = 0) {\r
+        if (empty($this->lastPing) || time() - $this->lastPing > $this->pingInterval) {\r
+            $this->send_ping();\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Message pump is triggered on socket input, so we only need an idle()\r
+     * call often enough to trigger our outgoing pings.\r
+     */\r
+    public function timeout() {\r
+        return $this->pingInterval;\r
+    }\r
+\r
+    /**\r
+     * Process MSN events that have come in over the wire.\r
+     *\r
+     * @param resource $socket Socket ready\r
+     * @return void\r
+     */\r
+    public function handleInput($socket) {\r
+        common_log(LOG_DEBUG, 'Servicing the MSN queue.');\r
+        $this->stats('msn_process');\r
+        $this->conn->receive();\r
+    }\r
+\r
+    /**\r
+    * Initiate connection\r
+    *\r
+    * @return void\r
+    */\r
+    public function connect() {\r
+        if (!$this->conn) {\r
+            $this->conn = new MSN(\r
+                array(\r
+                    'user' => $this->plugin->user,\r
+                    'password' => $this->plugin->password,\r
+                    'alias' => $this->plugin->nickname,\r
+                    // TRANS: MSN bot status message.\r
+                    'psm' => _m('Send me a message to post a notice'),\r
+                    'debug' => false\r
+                )\r
+            );\r
+            $this->conn->registerHandler('IMin', array($this, 'handle_msn_message'));\r
+            $this->conn->registerHandler('SessionReady', array($this, 'handle_session_ready'));\r
+            $this->conn->registerHandler('Pong', array($this, 'update_ping_time'));\r
+            $this->conn->registerHandler('ConnectFailed', array($this, 'handle_connect_failed'));\r
+            $this->conn->registerHandler('Reconnect', array($this, 'handle_reconnect'));\r
+            $this->conn->signon();\r
+            $this->lastPing = time();\r
+        }\r
+        return $this->conn;\r
+    }\r
+\r
+    /**\r
+    * Called by the idle process to send a ping\r
+    * when necessary\r
+    *\r
+    * @return void\r
+    */\r
+    protected function send_ping() {\r
+        $this->connect();\r
+        if (!$this->conn) {\r
+            return false;\r
+        }\r
+\r
+        $this->conn->sendPing();\r
+        $this->lastPing = time();\r
+        $this->pingInterval = 50;\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Update the time till the next ping\r
+     *\r
+     * @param $data Time till next ping\r
+     * @return void\r
+     */\r
+    public function update_ping_time($data) {\r
+        $this->pingInterval = $data;\r
+    }\r
+\r
+    /**\r
+    * Called via a callback when a message is received\r
+    *\r
+    * Passes it back to the queuing system\r
+    *\r
+    * @param array $data Data\r
+    * @return boolean\r
+    */\r
+    public function handle_msn_message($data) {\r
+        $this->plugin->enqueueIncomingRaw($data);\r
+        return true;\r
+    }\r
+\r
+    /**\r
+    * Called via a callback when a session becomes ready\r
+    *\r
+    * @param array $data Data\r
+    */\r
+    public function handle_session_ready($data) {\r
+        $sessionFailed = false;\r
+        $wm = Msn_waiting_message::top($data['to']);\r
+        while ($wm != NULL) {\r
+            if ($sessionFailed) {\r
+                $this->plugin->sendMessage($wm->screenname, $wm->message);\r
+                $sessionFailed = true;\r
+            } elseif (!$this->conn->sendMessage($wm->screenname, $wm->message, $ignore)) {\r
+                $this->plugin->sendMessage($wm->screenname, $wm->message);\r
+            }\r
+\r
+            $wm->delete();\r
+            $wm = Msn_waiting_message::top($data['to']);\r
+        }\r
+    }\r
+\r
+    /**\r
+    * Requeue messages from the waiting table so we try\r
+    * to send them again\r
+    *\r
+    * @return void\r
+    */\r
+    protected function requeue_waiting_messages() {\r
+        $wm = Msn_waiting_message::top();\r
+        while ($wm != NULL) {\r
+            $this->plugin->sendMessage($wm->screenname, $wm->message);\r
+            $wm->delete();\r
+            $wm = Msn_waiting_message::top();\r
+        }\r
+    }\r
+\r
+    /**\r
+    * Called by callback to log failure during connect\r
+    *\r
+    * @param string $message error message reported\r
+    * @return void\r
+    */\r
+    public function handle_connect_failed($message) {\r
+        common_log(LOG_NOTICE, 'MSN connect failed, retrying: ' . $message);\r
+    }\r
+\r
+    /**\r
+    * Called by callback to log reconnection\r
+    *\r
+    * @param void $data Not used (there to keep callback happy)\r
+    * @return void\r
+    */\r
+    public function handle_reconnect($data) {\r
+        common_log(LOG_NOTICE, 'MSN reconnecting');\r
+        // Requeue messages waiting in the DB\r
+        $this->requeue_waiting_messages();\r
+    }\r
+\r
+    /**\r
+    * Enters a message into the database for sending via a callback\r
+    * when the session is established\r
+    *\r
+    * @param string $to Intended recipient\r
+    * @param string $message Message\r
+    */\r
+    protected function enqueue_waiting_message($to, $message) {\r
+        $wm = new Msn_waiting_message();\r
+\r
+        $wm->screenname = $to;\r
+        $wm->message    = $message;\r
+        $wm->created    = common_sql_now();\r
+        $result         = $wm->insert();\r
+\r
+        if (!$result) {\r
+            common_log_db_error($wm, 'INSERT', __FILE__);\r
+            // TRANS: Server exception thrown when a message to be sent through MSN cannot be added to the database queue.\r
+            throw new ServerException(_m('Database error inserting queue item.'));\r
+        }\r
+\r
+        return true;\r
+    }\r
+\r
+    /**\r
+     * Send a message using the daemon\r
+     *\r
+     * @param $data Message data\r
+     * @return boolean true on success\r
+     */\r
+    public function send_raw_message($data) {\r
+        $this->connect();\r
+        if (!$this->conn) {\r
+            return false;\r
+        }\r
+\r
+        $waitForSession = false;\r
+        if (!$this->conn->sendMessage($data['to'], $data['message'], $waitForSession)) {\r
+            if ($waitForSession) {\r
+                $this->enqueue_waiting_message($data['to'], $data['message']);\r
+            } else {\r
+                return false;\r
+            }\r
+        }\r
+\r
+        // Sending a command updates the time till next ping\r
+        $this->lastPing = time();\r
+        $this->pingInterval = 50;\r
+        return true;\r
+    }\r
+}\r