diff --git a/XMPPHP/Roster.php b/XMPPHP/Roster.php new file mode 100644 index 0000000..8623e2f --- /dev/null +++ b/XMPPHP/Roster.php @@ -0,0 +1,162 @@ + + * @author Stephan Wentz + * @copyright 2008 Nathanael C. Fritz + */ + +/** + * XMPPHP Roster Object + * + * @category xmpphp + * @package XMPPHP + * @author Nathanael C. Fritz + * @author Stephan Wentz + * @author Michael Garvin + * @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; + } +} +?> diff --git a/XMPPHP/XMPP.php b/XMPPHP/XMPP.php index 0d5369b..204582b 100644 --- a/XMPPHP/XMPP.php +++ b/XMPPHP/XMPP.php @@ -27,6 +27,7 @@ /** XMPPHP_XMLStream */ require_once "XMLStream.php"; +require_once "Roster.php"; /** * XMPPHP Main Class @@ -85,6 +86,11 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { */ protected $use_encryption = true; + /** + * @var object + */ + public $roster; + /** * Constructor * @@ -106,16 +112,19 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { if(!$server) $server = $host; $this->basejid = $this->user . '@' . $this->host; + $this->roster = new Roster(); + $this->stream_start = ''; $this->stream_end = ''; $this->default_ns = 'jabber:client'; - $this->addHandler('features', 'http://etherx.jabber.org/streams', 'features_handler'); - $this->addHandler('success', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_success_handler'); - $this->addHandler('failure', 'urn:ietf:params:xml:ns:xmpp-sasl', 'sasl_failure_handler'); - $this->addHandler('proceed', 'urn:ietf:params:xml:ns:xmpp-tls', 'tls_proceed_handler'); - $this->addHandler('message', 'jabber:client', 'message_handler'); - $this->addHandler('presence', 'jabber:client', 'presence_handler'); + $this->addXPathHandler('{http://etherx.jabber.org/streams}features', 'features_handler'); + $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}success', 'sasl_success_handler'); + $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-sasl}failure', 'sasl_failure_handler'); + $this->addXPathHandler('{urn:ietf:params:xml:ns:xmpp-tls}proceed', 'tls_proceed_handler'); + $this->addXPathHandler('{jabber:client}message', 'message_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; } + /** + * 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 * @@ -218,6 +241,8 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { $payload['show'] = (isset($xml->sub('show')->data)) ? $xml->sub('show')->data : $payload['type']; $payload['from'] = $xml->attrs['from']; $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); if(array_key_exists('type', $xml->attrs) and $xml->attrs['type'] == 'subscribe') { if($this->auto_subscribe) { @@ -300,17 +325,46 @@ class XMPPHP_XMPP extends XMPPHP_XMLStream { */ public function getRoster() { $id = $this->getID(); - $this->addIdHandler($id, 'roster_get_handler'); $this->send(""); } /** - * Roster retrieval handler + * Roster iq handler + * Gets all packets matching XPath "iq/{jabber:iq:roster}query' * * @param string $xml */ - protected function roster_get_handler($xml) { + protected function roster_iq_handler($xml) { // 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 + } } /**