summaryrefslogtreecommitdiff
path: root/plugins/Xmpp
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/Xmpp')
-rw-r--r--plugins/Xmpp/Fake_XMPP.php104
-rw-r--r--plugins/Xmpp/README35
-rw-r--r--plugins/Xmpp/Sharing_XMPP.php43
-rw-r--r--plugins/Xmpp/XmppPlugin.php247
-rw-r--r--plugins/Xmpp/xmppmanager.php279
5 files changed, 708 insertions, 0 deletions
diff --git a/plugins/Xmpp/Fake_XMPP.php b/plugins/Xmpp/Fake_XMPP.php
new file mode 100644
index 000000000..a0f329ca0
--- /dev/null
+++ b/plugins/Xmpp/Fake_XMPP.php
@@ -0,0 +1,104 @@
+<?php
+/**
+ * StatusNet, the distributed open-source microblogging tool
+ *
+ * Instead of sending XMPP messages, retrieve the raw XML that would be sent
+ *
+ * PHP version 5
+ *
+ * LICENCE: 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/>.
+ *
+ * @category Network
+ * @package StatusNet
+ * @author Brion Vibber <brion@status.net>
+ * @copyright 2010 StatusNet, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
+ * @link http://status.net/
+ */
+
+if (!defined('STATUSNET') && !defined('LACONICA')) {
+ exit(1);
+}
+
+class Fake_XMPP extends XMPPHP_XMPP
+{
+ public $would_be_sent = null;
+
+ /**
+ * Constructor
+ *
+ * @param string $host
+ * @param integer $port
+ * @param string $user
+ * @param string $password
+ * @param string $resource
+ * @param string $server
+ * @param boolean $printlog
+ * @param string $loglevel
+ */
+ public function __construct($host, $port, $user, $password, $resource, $server = null, $printlog = false, $loglevel = null)
+ {
+ parent::__construct($host, $port, $user, $password, $resource, $server, $printlog, $loglevel);
+ // Normally the fulljid isn't filled out until resource binding time;
+ // we need to save it here since we're not talking to a real server.
+ $this->fulljid = "{$this->basejid}/{$this->resource}";
+ }
+
+ /**
+ * Send a formatted message to the outgoing queue for later forwarding
+ * to a real XMPP connection.
+ *
+ * @param string $msg
+ */
+ public function send($msg, $timeout=NULL)
+ {
+ $this->would_be_sent = $msg;
+ }
+
+ //@{
+ /**
+ * Stream i/o functions disabled; only do output
+ */
+ public function connect($timeout = 30, $persistent = false, $sendinit = true)
+ {
+ throw new Exception("Can't connect to server from fake XMPP.");
+ }
+
+ public function disconnect()
+ {
+ throw new Exception("Can't connect to server from fake XMPP.");
+ }
+
+ public function process()
+ {
+ throw new Exception("Can't read stream from fake XMPP.");
+ }
+
+ public function processUntil($event, $timeout=-1)
+ {
+ throw new Exception("Can't read stream from fake XMPP.");
+ }
+
+ public function read()
+ {
+ throw new Exception("Can't read stream from fake XMPP.");
+ }
+
+ public function readyToProcess()
+ {
+ throw new Exception("Can't read stream from fake XMPP.");
+ }
+ //@}
+}
+
diff --git a/plugins/Xmpp/README b/plugins/Xmpp/README
new file mode 100644
index 000000000..9bd71e980
--- /dev/null
+++ b/plugins/Xmpp/README
@@ -0,0 +1,35 @@
+The XMPP plugin allows users to send and receive notices over the XMPP/Jabber/GTalk network.
+
+Installation
+============
+add "addPlugin('xmpp',
+ array('setting'=>'value', 'setting2'=>'value2', ...);"
+to the bottom of your config.php
+
+The daemon included with this plugin must be running. It will be started by
+the plugin along with their other daemons when you run scripts/startdaemons.sh.
+See the StatusNet README for more about queuing and daemons.
+
+Settings
+========
+user*: user part of the jid
+server*: server part of the jid
+resource: resource part of the jid
+port (5222): port on which to connect to the server
+encryption (true): use encryption on the connection
+host (same as server): host to connect to. Usually, you won't set this.
+debug (false): log extra debug info
+public: list of jid's that should get the public feed (firehose)
+
+* required
+default values are in (parenthesis)
+
+Example
+=======
+addPlugin('xmpp', array(
+ 'user=>'update',
+ 'server=>'identi.ca',
+ 'password'=>'...',
+ 'public'=>array('bob@aol.com', 'sue@google.com')
+));
+
diff --git a/plugins/Xmpp/Sharing_XMPP.php b/plugins/Xmpp/Sharing_XMPP.php
new file mode 100644
index 000000000..4b69125da
--- /dev/null
+++ b/plugins/Xmpp/Sharing_XMPP.php
@@ -0,0 +1,43 @@
+<?php
+/**
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2009, StatusNet, Inc.
+ *
+ * Send and receive notices using the Jabber network
+ *
+ * PHP version 5
+ *
+ * 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/>.
+ *
+ * @category Jabber
+ * @package StatusNet
+ * @author Evan Prodromou <evan@status.net>
+ * @copyright 2009 StatusNet, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+ // This check helps protect against security problems;
+ // your code file can't be executed directly from the web.
+ exit(1);
+}
+
+class Sharing_XMPP extends XMPPHP_XMPP
+{
+ function getSocket()
+ {
+ return $this->socket;
+ }
+}
diff --git a/plugins/Xmpp/XmppPlugin.php b/plugins/Xmpp/XmppPlugin.php
new file mode 100644
index 000000000..b9551b852
--- /dev/null
+++ b/plugins/Xmpp/XmppPlugin.php
@@ -0,0 +1,247 @@
+<?php
+/**
+ * StatusNet - the distributed open-source microblogging tool
+ * Copyright (C) 2009, StatusNet, Inc.
+ *
+ * Send and receive notices using the XMPP network
+ *
+ * PHP version 5
+ *
+ * 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/>.
+ *
+ * @category IM
+ * @package StatusNet
+ * @author Evan Prodromou <evan@status.net>
+ * @copyright 2009 StatusNet, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link http://status.net/
+ */
+
+if (!defined('STATUSNET')) {
+ // This check helps protect against security problems;
+ // your code file can't be executed directly from the web.
+ exit(1);
+}
+
+/**
+ * Plugin for XMPP
+ *
+ * @category Plugin
+ * @package StatusNet
+ * @author Evan Prodromou <evan@status.net>
+ * @copyright 2009 StatusNet, Inc.
+ * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
+ * @link http://status.net/
+ */
+
+class XmppPlugin extends ImPlugin
+{
+ public $server = null;
+ public $port = 5222;
+ public $user = 'update';
+ public $resource = null;
+ public $encryption = true;
+ public $password = null;
+ public $host = null; // only set if != server
+ public $debug = false; // print extra debug info
+
+ public $transport = 'xmpp';
+
+ protected $fake_xmpp;
+
+ function getDisplayName(){
+ return _m('XMPP/Jabber/GTalk');
+ }
+
+ function normalize($screenname)
+ {
+ if (preg_match("/(?:([^\@]+)\@)?([^\/]+)(?:\/(.*))?$/", $screenname, $matches)) {
+ $node = $matches[1];
+ $server = $matches[2];
+ return strtolower($node.'@'.$server);
+ } else {
+ return null;
+ }
+ }
+
+ function daemon_screenname()
+ {
+ $ret = $this->user . '@' . $this->server;
+ if($this->resource)
+ {
+ return $ret . '/' . $this->resource;
+ }else{
+ return $ret;
+ }
+ }
+
+ function validate($screenname)
+ {
+ // Cheap but effective
+ return Validate::email($screenname);
+ }
+
+ /**
+ * Load related modules when needed
+ *
+ * @param string $cls Name of the class to be loaded
+ *
+ * @return boolean hook value; true means continue processing, false means stop.
+ */
+
+ function onAutoload($cls)
+ {
+ $dir = dirname(__FILE__);
+
+ switch ($cls)
+ {
+ case 'Sharing_XMPP':
+ case 'Fake_XMPP':
+ include_once $dir . '/'.$cls.'.php';
+ return false;
+ case 'XmppManager':
+ include_once $dir . '/'.strtolower($cls).'.php';
+ return false;
+ default:
+ return true;
+ }
+ }
+
+ function onStartImDaemonIoManagers(&$classes)
+ {
+ parent::onStartImDaemonIoManagers(&$classes);
+ $classes[] = new XmppManager($this); // handles pings/reconnects
+ return true;
+ }
+
+ function microiduri($screenname)
+ {
+ return 'xmpp:' . $screenname;
+ }
+
+ function send_message($screenname, $body)
+ {
+ $this->fake_xmpp->message($screenname, $body, 'chat');
+ $this->enqueue_outgoing_raw($this->fake_xmpp->would_be_sent);
+ return true;
+ }
+
+ function send_notice($screenname, $notice)
+ {
+ $msg = $this->format_notice($notice);
+ $entry = $this->format_entry($notice);
+
+ $this->fake_xmpp->message($screenname, $msg, 'chat', null, $entry);
+ $this->enqueue_outgoing_raw($this->fake_xmpp->would_be_sent);
+ return true;
+ }
+
+ /**
+ * extra information for XMPP messages, as defined by Twitter
+ *
+ * @param Profile $profile Profile of the sending user
+ * @param Notice $notice Notice being sent
+ *
+ * @return string Extra information (Atom, HTML, addresses) in string format
+ */
+
+ function format_entry($notice)
+ {
+ $profile = $notice->getProfile();
+
+ $entry = $notice->asAtomEntry(true, true);
+
+ $xs = new XMLStringer();
+ $xs->elementStart('html', array('xmlns' => 'http://jabber.org/protocol/xhtml-im'));
+ $xs->elementStart('body', array('xmlns' => 'http://www.w3.org/1999/xhtml'));
+ $xs->element('a', array('href' => $profile->profileurl),
+ $profile->nickname);
+ $xs->text(": ");
+ if (!empty($notice->rendered)) {
+ $xs->raw($notice->rendered);
+ } else {
+ $xs->raw(common_render_content($notice->content, $notice));
+ }
+ $xs->text(" ");
+ $xs->element('a', array(
+ 'href'=>common_local_url('conversation',
+ array('id' => $notice->conversation)).'#notice-'.$notice->id
+ ),sprintf(_('[%s]'),$notice->id));
+ $xs->elementEnd('body');
+ $xs->elementEnd('html');
+
+ $html = $xs->getString();
+
+ return $html . ' ' . $entry;
+ }
+
+ function receive_raw_message($pl)
+ {
+ $from = $this->normalize($pl['from']);
+
+ if ($pl['type'] != 'chat') {
+ common_log(LOG_WARNING, "Ignoring message of type ".$pl['type']." from $from.");
+ return true;
+ }
+
+ if (mb_strlen($pl['body']) == 0) {
+ common_log(LOG_WARNING, "Ignoring message with empty body from $from.");
+ return true;
+ }
+
+ return $this->handle_incoming($from, $pl['body']);
+ }
+
+ function initialize(){
+ if(!isset($this->server)){
+ throw new Exception("must specify a server");
+ }
+ if(!isset($this->port)){
+ throw new Exception("must specify a port");
+ }
+ if(!isset($this->user)){
+ throw new Exception("must specify a user");
+ }
+ if(!isset($this->password)){
+ throw new Exception("must specify a password");
+ }
+
+ $this->fake_xmpp = new Fake_XMPP($this->host ?
+ $this->host :
+ $this->server,
+ $this->port,
+ $this->user,
+ $this->password,
+ $this->resource,
+ $this->server,
+ $this->debug ?
+ true : false,
+ $this->debug ?
+ XMPPHP_Log::LEVEL_VERBOSE : null
+ );
+ return true;
+ }
+
+ function onPluginVersion(&$versions)
+ {
+ $versions[] = array('name' => 'XMPP',
+ 'version' => STATUSNET_VERSION,
+ 'author' => 'Craig Andrews, Evan Prodromou',
+ 'homepage' => 'http://status.net/wiki/Plugin:XMPP',
+ 'rawdescription' =>
+ _m('The XMPP plugin allows users to send and receive notices over the XMPP/Jabber network.'));
+ return true;
+ }
+}
+
diff --git a/plugins/Xmpp/xmppmanager.php b/plugins/Xmpp/xmppmanager.php
new file mode 100644
index 000000000..87d818668
--- /dev/null
+++ b/plugins/Xmpp/xmppmanager.php
@@ -0,0 +1,279 @@
+<?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); }
+
+/**
+ * XMPP background connection manager for XMPP-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 XmppManager extends ImManager
+{
+ protected $lastping = null;
+ protected $pingid = null;
+
+ public $conn = null;
+
+ const PING_INTERVAL = 120;
+
+
+ /**
+ * Initialize connection to server.
+ * @return boolean true on success
+ */
+ public function start($master)
+ {
+ if(parent::start($master))
+ {
+ $this->connect();
+ return true;
+ }else{
+ return false;
+ }
+ }
+
+ function send_raw_message($data)
+ {
+ $this->connect();
+ if (!$this->conn || $this->conn->isDisconnected()) {
+ return false;
+ }
+ $this->conn->send($data);
+ return true;
+ }
+
+ /**
+ * Message pump is triggered on socket input, so we only need an idle()
+ * call often enough to trigger our outgoing pings.
+ */
+ function timeout()
+ {
+ return self::PING_INTERVAL;
+ }
+
+ /**
+ * Process XMPP events that have come in over the wire.
+ * @fixme may kill process on XMPP error
+ * @param resource $socket
+ */
+ public function handleInput($socket)
+ {
+ # Process the queue for as long as needed
+ try {
+ common_log(LOG_DEBUG, "Servicing the XMPP queue.");
+ $this->stats('xmpp_process');
+ $this->conn->processTime(0);
+ } catch (XMPPHP_Exception $e) {
+ common_log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
+ die($e->getMessage());
+ }
+ }
+
+ /**
+ * Lists the IM connection socket to allow i/o master to wake
+ * when input comes in here as well as from the queue source.
+ *
+ * @return array of resources
+ */
+ public function getSockets()
+ {
+ $this->connect();
+ if($this->conn){
+ return array($this->conn->getSocket());
+ }else{
+ return array();
+ }
+ }
+
+ /**
+ * Idle processing for io manager's execution loop.
+ * Send keepalive pings to server.
+ *
+ * Side effect: kills process on exception from XMPP library.
+ *
+ * @fixme non-dying error handling
+ */
+ public function idle($timeout=0)
+ {
+ $now = time();
+ if (empty($this->lastping) || $now - $this->lastping > self::PING_INTERVAL) {
+ try {
+ $this->send_ping();
+ } catch (XMPPHP_Exception $e) {
+ common_log(LOG_ERR, "Got an XMPPHP_Exception: " . $e->getMessage());
+ die($e->getMessage());
+ }
+ }
+ }
+
+ function connect()
+ {
+ if (!$this->conn || $this->conn->isDisconnected()) {
+ $resource = 'queue' . posix_getpid();
+ $this->conn = new Sharing_XMPP($this->plugin->host ?
+ $this->plugin->host :
+ $this->plugin->server,
+ $this->plugin->port,
+ $this->plugin->user,
+ $this->plugin->password,
+ $this->plugin->resource,
+ $this->plugin->server,
+ $this->plugin->debug ?
+ true : false,
+ $this->plugin->debug ?
+ XMPPHP_Log::LEVEL_VERBOSE : null
+ );
+
+ if (!$this->conn) {
+ return false;
+ }
+ $this->conn->addEventHandler('message', 'handle_xmpp_message', $this);
+ $this->conn->addEventHandler('reconnect', 'handle_xmpp_reconnect', $this);
+ $this->conn->setReconnectTimeout(600);
+
+ $this->conn->autoSubscribe();
+ $this->conn->useEncryption($this->plugin->encryption);
+
+ try {
+ $this->conn->connect(true); // true = persistent connection
+ } catch (XMPPHP_Exception $e) {
+ common_log(LOG_ERR, $e->getMessage());
+ return false;
+ }
+
+ $this->conn->processUntil('session_start');
+ $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
+ }
+ return $this->conn;
+ }
+
+ function send_ping()
+ {
+ $this->connect();
+ if (!$this->conn || $this->conn->isDisconnected()) {
+ return false;
+ }
+ $now = time();
+ if (!isset($this->pingid)) {
+ $this->pingid = 0;
+ } else {
+ $this->pingid++;
+ }
+
+ common_log(LOG_DEBUG, "Sending ping #{$this->pingid}");
+ $this->conn->send("<iq from='{" . $this->plugin->daemon_screenname() . "}' to='{$this->plugin->server}' id='ping_{$this->pingid}' type='get'><ping xmlns='urn:xmpp:ping'/></iq>");
+ $this->lastping = $now;
+ return true;
+ }
+
+ function handle_xmpp_message(&$pl)
+ {
+ $this->plugin->enqueue_incoming_raw($pl);
+ return true;
+ }
+
+ /**
+ * Callback for Jabber reconnect event
+ * @param $pl
+ */
+ function handle_xmpp_reconnect(&$pl)
+ {
+ common_log(LOG_NOTICE, 'XMPP reconnected');
+
+ $this->conn->processUntil('session_start');
+ $this->send_presence(_m('Send me a message to post a notice'), 'available', null, 'available', 100);
+ }
+
+ /**
+ * sends a presence stanza on the XMPP network
+ *
+ * @param string $status current status, free-form string
+ * @param string $show structured status value
+ * @param string $to recipient of presence, null for general
+ * @param string $type type of status message, related to $show
+ * @param int $priority priority of the presence
+ *
+ * @return boolean success value
+ */
+
+ function send_presence($status, $show='available', $to=null,
+ $type = 'available', $priority=null)
+ {
+ $this->connect();
+ if (!$this->conn || $this->conn->isDisconnected()) {
+ return false;
+ }
+ $this->conn->presence($status, $show, $to, $type, $priority);
+ return true;
+ }
+
+ /**
+ * sends a "special" presence stanza on the XMPP network
+ *
+ * @param string $type Type of presence
+ * @param string $to JID to send presence to
+ * @param string $show show value for presence
+ * @param string $status status value for presence
+ *
+ * @return boolean success flag
+ *
+ * @see send_presence()
+ */
+
+ function special_presence($type, $to=null, $show=null, $status=null)
+ {
+ // FIXME: why use this instead of send_presence()?
+ $this->connect();
+ if (!$this->conn || $this->conn->isDisconnected()) {
+ return false;
+ }
+
+ $to = htmlspecialchars($to);
+ $status = htmlspecialchars($status);
+
+ $out = "<presence";
+ if ($to) {
+ $out .= " to='$to'";
+ }
+ if ($type) {
+ $out .= " type='$type'";
+ }
+ if ($show == 'available' and !$status) {
+ $out .= "/>";
+ } else {
+ $out .= ">";
+ if ($show && ($show != 'available')) {
+ $out .= "<show>$show</show>";
+ }
+ if ($status) {
+ $out .= "<status>$status</status>";
+ }
+ $out .= "</presence>";
+ }
+ $this->conn->send($out);
+ return true;
+ }
+}