Lots more work on adapting library. Added more commenting and fixed some stuff on the integration side.

This commit is contained in:
Luke Fitzgerald 2010-06-14 19:53:43 +01:00
parent 2ef01c5b74
commit 0a4738a806
3 changed files with 716 additions and 1358 deletions

View File

@ -1,172 +1,190 @@
<?php <?php
/** /**
* StatusNet - the distributed open-source microblogging tool * StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2009, StatusNet, Inc. * Copyright (C) 2009, StatusNet, Inc.
* *
* Send and receive notices using the AIM network * Send and receive notices using the MSN network
* *
* PHP version 5 * PHP version 5
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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 * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* @category IM * @category IM
* @package StatusNet * @package StatusNet
* @author Craig Andrews <candrews@integralblue.com> * @author Luke Fitzgerald <lw.fitzgerald@googlemail.com>
* @copyright 2009 StatusNet, Inc. * @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
if (!defined('STATUSNET')) { if (!defined('STATUSNET')) {
// This check helps protect against security problems; // This check helps protect against security problems;
// your code file can't be executed directly from the web. // your code file can't be executed directly from the web.
exit(1); exit(1);
} }
// We bundle the phptoclib library... // We bundle the phpmsnclass library...
set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/phptoclib'); set_include_path(get_include_path() . PATH_SEPARATOR . dirname(__FILE__) . '/extlib/phpmsnclass');
/** /**
* Plugin for AIM * Plugin for MSN
* *
* @category Plugin * @category Plugin
* @package StatusNet * @package StatusNet
* @author Craig Andrews <candrews@integralblue.com> * @author Luke Fitzgerald <lw.fitzgerald@googlemail.com>
* @copyright 2009 StatusNet, Inc. * @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/ * @link http://status.net/
*/ */
class MsnPlugin extends ImPlugin class MsnPlugin extends ImPlugin {
{ public $user = null;
public $user = null; public $password = null;
public $password = null; public $nickname = null;
public $publicFeed = array(); public $transport = 'msnim';
public $transport = 'msnim'; /**
* Get the internationalized/translated display name of this IM service
function getDisplayName() *
{ * @return string Name of service
return _m('MSN'); */
} function getDisplayName() {
return _m('MSN');
function normalize($screenname) }
{
$screenname = str_replace(" ","", $screenname); /**
return strtolower($screenname); * Normalize a screenname for comparison
} *
* @param string $screenname screenname to normalize
function daemon_screenname() * @return string an equivalent screenname in normalized form
{ */
return $this->user; function normalize($screenname) {
} $screenname = str_replace(" ","", $screenname);
return strtolower($screenname);
function validate($screenname) }
{
if(preg_match('/^[a-z]\w{2,15}$/i', $screenname)) { /**
return true; * Get the screenname of the daemon that sends and receives messages
}else{ *
return false; * @return string Screenname
} */
} function daemon_screenname() {
return $this->user;
/** }
* Load related modules when needed
* /**
* @param string $cls Name of the class to be loaded * Validate (ensure the validity of) a screenname
* *
* @return boolean hook value; true means continue processing, false means stop. * @param string $screenname screenname to validate
*/ *
function onAutoload($cls) * @return boolean
{ */
$dir = dirname(__FILE__); function validate($screenname) {
//TODO Correct this for MSN screennames
switch ($cls) if(preg_match('/^[a-z]\w{2,15}$/i', $screenname)) {
{ return true;
case 'Msn': }else{
require_once(INSTALLDIR.'/plugins/Msn/extlib/phpmsnclass/msn.class.php'); return false;
return false; }
case 'MsnManager': }
include_once $dir . '/'.strtolower($cls).'.php';
return false; /**
case 'Fake_Msn': * Load related modules when needed
include_once $dir . '/'. $cls .'.php'; *
return false; * @param string $cls Name of the class to be loaded
default: *
return true; * @return boolean hook value; true means continue processing, false means stop.
} */
} public function onAutoload($cls) {
$dir = dirname(__FILE__);
function onStartImDaemonIoManagers(&$classes)
{ switch ($cls) {
parent::onStartImDaemonIoManagers(&$classes); case 'Msn':
$classes[] = new MsnManager($this); // handles sending/receiving require_once(INSTALLDIR.'/plugins/Msn/extlib/phpmsnclass/msn.class.php');
return true; return false;
} case 'MsnManager':
include_once $dir . '/'.strtolower($cls).'.php';
function microiduri($screenname) return false;
{ default:
return 'msnim:' . $screenname; return true;
} }
}
function send_message($screenname, $body)
{ public function onStartImDaemonIoManagers(&$classes) {
//$this->fake_aim->sendIm($screenname, $body); parent::onStartImDaemonIoManagers(&$classes);
//$this->enqueue_outgoing_raw($this->fake_aim->would_be_sent); $classes[] = new MsnManager($this); // handles sending/receiving
$this->enqueue_outgoing_raw(array($screenname, $body)); return true;
return true; }
}
/**
/** * Get a microid URI for the given screenname
* Accept a queued input message. *
* * @param string $screenname
* @return true if processing completed, false if message should be reprocessed * @return string microid URI
*/ */
function receive_raw_message($message) public function microiduri($screenname) {
{ return 'msnim:' . $screenname;
$info=Aim::getMessageInfo($message); }
$from = $info['from'];
$user = $this->get_user($from); /**
$notice_text = $info['message']; * Send a message to a given screenname
*
$this->handle_incoming($from, $notice_text); * @param string $screenname Screenname to send to
* @param string $body Text to send
return true; * @return boolean success value
} */
public function send_message($screenname, $body) {
function initialize(){ $this->enqueue_outgoing_raw(array('to' => $screenname, 'message' => $body));
if(!isset($this->user)){ return true;
throw new Exception("must specify a user"); }
}
if(!isset($this->password)){ /**
throw new Exception("must specify a password"); * Accept a queued input message.
} *
if(!isset($this->nickname)) { * @param array $data Data
throw new Exception("must specify a nickname"); * @return true if processing completed, false if message should be reprocessed
} */
public function receive_raw_message($data) {
$this->fake_msn = new Fake_Msn($this->user,$this->password,4); $this->handle_incoming($data['sender'], $data['message']);
return true; return true;
} }
function onPluginVersion(&$versions) /**
{ * Initialize plugin
$versions[] = array('name' => 'MSN', *
'version' => STATUSNET_VERSION, * @return void
'author' => 'Luke Fitzgerald', */
'homepage' => 'http://status.net/wiki/Plugin:MSN', public function initialize() {
'rawdescription' => if (!isset($this->user)) {
_m('The MSN plugin allows users to send and receive notices over the MSN network.')); throw new Exception("Must specify a user");
return true; }
} if (!isset($this->password)) {
} throw new Exception("Must specify a password");
}
if (!isset($this->nickname)) {
throw new Exception("Must specify a nickname");
}
return true;
}
function onPluginVersion(&$versions) {
$versions[] = array('name' => 'MSN',
'version' => STATUSNET_VERSION,
'author' => 'Luke Fitzgerald',
'homepage' => 'http://status.net/wiki/Plugin:MSN',
'rawdescription' =>
_m('The MSN plugin allows users to send and receive notices over the MSN network.'));
return true;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,158 +1,183 @@
<?php <?php
/* /*
* StatusNet - the distributed open-source microblogging tool * StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008, 2009, StatusNet, Inc. * Copyright (C) 2008, 2009, StatusNet, Inc.
* *
* This program is free software: you can redistribute it and/or modify * 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 * 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 * the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
* This program is distributed in the hope that it will be useful, * This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details. * GNU Affero General Public License for more details.
* *
* You should have received a copy of the GNU Affero General Public License * 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/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); } if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
/** /**
* AIM background connection manager for AIM-using queue handlers, * MSN background connection manager for MSN-using queue handlers,
* allowing them to send outgoing messages on the right connection. * allowing them to send outgoing messages on the right connection.
* *
* Input is handled during socket select loop, keepalive pings during idle. * Input is handled during socket select loop, keepalive pings during idle.
* Any incoming messages will be handled. * Any incoming messages will be handled.
* *
* In a multi-site queuedaemon.php run, one connection will be instantiated * 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. * for each site being handled by the current process that has MSN enabled.
*/ */
class MsnManager extends ImManager class MsnManager extends ImManager {
{ public $conn = null;
public $conn = null; private $lastping = null;
private $pingInterval;
protected $lastping = null;
/**
private $pingInterval; * Initialise connection to server.
*
/** * @return boolean true on success
* Initialize connection to server. */
* @return boolean true on success public function start($master) {
*/ if (parent::start($master)) {
public function start($master) $this->connect();
{ return true;
if(parent::start($master)) } else {
{ return false;
$this->connect(); }
return true; }
}else{
return false; /**
} * Return any open sockets that the run loop should listen
} * for input on.
*
public function getSockets() * @return array Array of socket resources
{ */
$this->connect(); public function getSockets() {
if($this->conn){ $this->connect();
return $this->conn->getSockets(); if ($this->conn) {
}else{ return $this->conn->getSockets();
return array(); } else {
} return array();
} }
}
/**
* Idle processing for io manager's execution loop. /**
* Send keepalive pings to server. * Idle processing for io manager's execution loop.
*/ * Send keepalive pings to server.
public function idle($timeout=0) *
{ * @return void
$now = time(); */
if (empty($this->lastping) || $now - $this->lastping > $pingInterval) { public function idle($timeout = 0) {
$this->send_ping(); if (empty($this->lastping) || time() - $this->lastping > $this->pingInterval) {
} $this->send_ping();
} }
}
/**
* Process MSN events that have come in over the wire. /**
* @param resource $socket * Process MSN events that have come in over the wire.
*/ *
public function handleInput($socket) * @param resource $socket Socket ready
{ * @return void
common_log(LOG_DEBUG, "Servicing the MSN queue."); */
$this->stats('msn_process'); public function handleInput($socket) {
$this->conn->receive(); 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( * Initiate connection
'user' => $this->plugin->user, *
'password' => $this->plugin->password, * @return void
'alias' => $this->plugin->nickname, */
'psm' => 'Send me a message to post a notice', function connect() {
'debug' => true if (!$this->conn) {
) $this->conn = new MSN(array('user' => $this->plugin->user,
); 'password' => $this->plugin->password,
$this->conn->registerHandler("IMIn", array($this, 'handle_msn_message')); 'alias' => $this->plugin->nickname,
$this->conn->registerHandler('Pong', array($this, 'update_ping_time')); 'psm' => 'Send me a message to post a notice',
$this->conn->registerHandler('ConnectFailed', array($this, 'handle_connect_failed')); 'debug' => true));
$this->conn->registerHandler('Reconnect', array($this, 'handle_reconnect')); $this->conn->registerHandler("IMIn", array($this, 'handle_msn_message'));
$this->conn->signon(); $this->conn->registerHandler('Pong', array($this, 'update_ping_time'));
$this->lastping = time(); $this->conn->registerHandler('ConnectFailed', array($this, 'handle_connect_failed'));
} $this->conn->registerHandler('Reconnect', array($this, 'handle_reconnect'));
return $this->conn; $this->conn->signon();
} $this->lastping = time();
}
function send_ping() { return $this->conn;
$this->connect(); }
if (!$this->conn) {
return false; /**
} * Called by the idle process to send a ping
* when necessary
$now = time(); *
* @return void
$this->conn->sendPing(); */
$this->lastping = $now; private function send_ping() {
return true; $this->connect();
} if (!$this->conn) {
return false;
/** }
* Update the time till the next ping
* @param $data Time till next ping $this->conn->sendPing();
*/ $this->lastping = time();
function update_ping_time($data) { return true;
$pingInterval = $data; }
}
/**
function handle_msn_message($data) * Update the time till the next ping
{ * @param $data Time till next ping
$this->plugin->enqueue_incoming_raw($data); */
return true; private function update_ping_time($data) {
} $pingInterval = $data;
}
function handle_connect_failed($data) {
common_log(LOG_NOTICE, 'MSN connect failed, retrying'); /**
} * Called via a callback when a message is received
*
function handle_reconnect($data) { * Passes it back to the queuing system
common_log(LOG_NOTICE, 'MSN reconnecting'); *
} * @param array $data Data
*/
function send_raw_message($data) private function handle_msn_message($data) {
{ $this->plugin->enqueue_incoming_raw($data);
$this->connect(); return true;
if (!$this->conn) { }
return false;
} /**
$this->conn->sflapSend($data[0],$data[1],$data[2],$data[3]); * Called by callback to log failure during connect
*
// Sending a command updates the time till next ping * @param void $data Not used (there to keep callback happy)
$this->lastping = time(); */
$this->pingInterval = 50; function handle_connect_failed($data) {
return true; common_log(LOG_NOTICE, 'MSN connect failed, retrying');
} }
}
/**
* Called by callback to log reconnection
*
* @param void $data Not used (there to keep callback happy)
*/
function handle_reconnect($data) {
common_log(LOG_NOTICE, 'MSN reconnecting');
}
function send_raw_message($data) {
$this->connect();
if (!$this->conn) {
return false;
}
if (!$this->conn->sendMessage($data['to'], $data['message'])) {
return false;
}
// Sending a command updates the time till next ping
$this->lastping = time();
$this->pingInterval = 50;
return true;
}
}