added Roster and Presence support to XMPP.php

new Roster class handles roster and presence
XMPP now uses XPath handlers exclusively


git-svn-id: svn://netflint.net/xmpphp@63 ef36c318-a008-4979-b6e8-6b496270793b
This commit is contained in:
gar 2008-11-12 00:29:00 +00:00
parent f1088f8174
commit b5e26128a9
2 changed files with 225 additions and 9 deletions

162
XMPPHP/Roster.php Normal file
View File

@ -0,0 +1,162 @@
<?php
/**
* XMPPHP: The PHP XMPP Library
* Copyright (C) 2008 Nathanael C. Fritz
* This file is part of SleekXMPP.
*
* XMPPHP is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* XMPPHP 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XMPPHP; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @category xmpphp
* @package XMPPHP
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
* @copyright 2008 Nathanael C. Fritz
*/
/**
* XMPPHP Roster Object
*
* @category xmpphp
* @package XMPPHP
* @author Nathanael C. Fritz <JID: fritzy@netflint.net>
* @author Stephan Wentz <JID: stephan@jabber.wentz.it>
* @author Michael Garvin <JID: gar@netflint.net>
* @copyright 2008 Nathanael C. Fritz
* @version $Id$
*/
class Roster {
/**
* Roster array, handles contacts and presence. Indexed by jid.
* Contains array with potentially two indexes 'contact' and 'presence'
* @var array
*/
protected $roster_array = array();
/**
* Constructor
*
*/
public function __construct($roster_array = array()) {
if ($this->verifyRoster($roster_array)) {
$this->roster_array = $roster_array; //Allow for prepopulation with existing roster
} else {
$this->roster_array = array();
}
}
/**
*
* Check that a given roster array is of a valid structure (empty is still valid)
*
* @param array $roster_array
*/
protected function verifyRoster($roster_array) {
#TODO once we know *what* a valid roster array looks like
return True;
}
/**
*
* Add given contact to roster
*
* @param string $jid
* @param string $subscription
* @param string $name
* @param array $groups
*/
public function addContact($jid, $subscription, $name='', $groups=array()) {
$contact = array('jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups);
if ($this->isContact($jid)) {
$this->roster_array[$jid]['contact'] = $contact;
} else {
$this->roster_array[$jid] = array('contact' => $contact);
}
}
/**
*
* Retrieve contact via jid
*
* @param string $jid
*/
public function getContact($jid) {
if ($this->isContact($jid)) {
return $this->roster_array[$jid]['contact'];
}
}
/**
*
* Discover if a contact exists in the roster via jid
*
* @param string $jid
*/
public function isContact($jid) {
return (array_key_exists($jid, $this->roster_array));
}
/**
*
* Set presence
*
* @param string $presence
* @param integer $priority
* @param string $show
* @param string $status
*/
public function setPresence($presence, $priority, $show, $status) {
list($jid, $resource) = split("/", $presence);
if ($show != 'unavailable') {
if (!$this->isContact($jid)) {
$this->addContact($jid, 'not-in-roster');
}
$resource = $resource ? $resource : '';
$this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status);
} else { //Nuke unavailable resources to save memory
unset($this->roster_array[$jid]['resource'][$resource]);
}
}
/*
*
* Return best presence for jid
*
* @param string $jid
*/
public function getPresence($jid) {
$split = split("/", $jid);
$jid = $split[0];
if($this->isContact($jid)) {
$current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127
foreach($this->roster_array[$jid]['presence'] as $resource => $presence) {
//Highest available priority or just highest priority
if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) {
$current = $presence;
$current['resource'] = $resource;
}
}
return $current;
}
}
/**
*
* Get roster
*
*/
public function getRoster() {
return $this->roster_array;
}
}
?>

View File

@ -27,6 +27,7 @@
/** XMPPHP_XMLStream */ /** XMPPHP_XMLStream */
require_once "XMLStream.php"; require_once "XMLStream.php";
require_once "Roster.php";
/** /**
* XMPPHP Main Class * XMPPHP Main Class
@ -85,6 +86,11 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
*/ */
protected $use_encryption = true; protected $use_encryption = true;
/**
* @var object
*/
public $roster;
/** /**
* Constructor * Constructor
* *
@ -106,16 +112,19 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
if(!$server) $server = $host; if(!$server) $server = $host;
$this->basejid = $this->user . '@' . $this->host; $this->basejid = $this->user . '@' . $this->host;
$this->roster = new Roster();
$this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">'; $this->stream_start = '<stream:stream to="' . $server . '" xmlns:stream="http://etherx.jabber.org/streams" xmlns="jabber:client" version="1.0">';
$this->stream_end = '</stream:stream>'; $this->stream_end = '</stream:stream>';
$this->default_ns = 'jabber:client'; $this->default_ns = 'jabber:client';
$this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler'); $this->addXPathHandler('{http://etherx.jabber.org/streams}features', 'features_handler');
$this->addHandler('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler'); $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}success', 'sasl_success_handler');
$this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler'); $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}failure', 'sasl_failure_handler');
$this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler'); $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-tls}proceed', 'tls_proceed_handler');
$this->addHandler('message', 'jabber:client', 'message_handler'); $this->addXPathHandler('{jabber:client}message', 'message_handler');
$this->addHandler('presence', 'jabber:client', 'presence_handler'); $this->addXPathHandler('{jabber:client}presence', 'presence_handler');
$this->addXPathHandler('iq/{jabber:iq:roster}query', 'roster_iq_handler');
} }
/** /**
@ -136,6 +145,20 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
$this->auto_subscribe = $autoSubscribe; $this->auto_subscribe = $autoSubscribe;
} }
/**
* Specialized addHandler for iq packets
*
* @param string $tagname
* @param string $ns
* @param string $pointer
* @param string $obj
*/
public function addIqHandler($tagname, $ns, $pointer, $obj = null) {
#TODO make this use addXPathHandler once that's written
# {jabber:client}iq/{$ns}$tagname
//$this->addHandler('iq', $ns, $pointer, $obj, 2);
}
/** /**
* Send XMPP Message * Send XMPP Message
* *
@ -218,6 +241,8 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
$payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type']; $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type'];
$payload['from'] = $xml->attrs['from']; $payload['from'] = $xml->attrs['from'];
$payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : ''; $payload['status'] = (isset($xml->sub('status')->data)) ? $xml->sub('status')->data : '';
$payload['priority'] = (isset($xml->sub('priority')->data)) ? intval($xml->sub('priority')->data) : 0;
$this->roster->setPresence($payload['from'], $payload['priority'], $payload['show'], $payload['status']);
$this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG); $this->log->log("Presence: {$payload['from']} [{$payload['show']}] {$payload['status']}", XMPPHP_Log::LEVEL_DEBUG);
if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') { if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') {
if($this->auto_subscribe) { if($this->auto_subscribe) {
@ -300,17 +325,46 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream {
*/ */
public function getRoster() { public function getRoster() {
$id = $this->getID(); $id = $this->getID();
$this->addIdHandler($id, 'roster_get_handler');
$this->send("<iq xmlns='jabber:client' type='get' id='$id'><query xmlns='jabber:iq:roster' /></iq>"); $this->send("<iq xmlns='jabber:client' type='get' id='$id'><query xmlns='jabber:iq:roster' /></iq>");
} }
/** /**
* Roster retrieval handler * Roster iq handler
* Gets all packets matching XPath "iq/{jabber:iq:roster}query'
* *
* @param string $xml * @param string $xml
*/ */
protected function roster_get_handler($xml) { protected function roster_iq_handler($xml) {
// TODO: make this work // TODO: make this work
// TODO: reply if it's a type='set'
$status = "result";
$xmlroster = $xml->sub('query');
foreach($xmlroster->subs as $item) {
$groups = array();
if ($item->name == 'item') {
$jid = $item->attrs['jid']; //REQUIRED
$name = $item->attrs['name']; //MAY
$subscription = $item->attrs['subscription'];
foreach($item->subs as $subitem) {
if ($subitem->name == 'group') {
$groups[] = $subitem->data;
}
}
$contacts[] = array($jid, $subscription, $name, $groups); //Store for action if no errors happen
} else {
$status = "error";
}
}
if ($status == "result") { //No errors, add contacts
foreach($contacts as $contact) {
$this->roster->addContact($contact[0], $contact[1], $contact[2], $contact[3]);
}
}
if ($xml->attrs['type'] == 'set' and $status == 'result') {
#TODO send back result
} elseif ($xml->attrs['type'] == 'set') {
#TODO send back error
}
} }
/** /**