]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Xmpp/extlib/XMPPHP/Roster.php
Merge branch '0.9.x' of gitorious.org:statusnet/mainline into 1.0.x
[quix0rs-gnu-social.git] / plugins / Xmpp / extlib / XMPPHP / Roster.php
1 <?php
2 /**
3  * XMPPHP: The PHP XMPP Library
4  * Copyright (C) 2008  Nathanael C. Fritz
5  * This file is part of SleekXMPP.
6  * 
7  * XMPPHP is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  * 
12  * XMPPHP is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with XMPPHP; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20  *
21  * @category   xmpphp 
22  * @package     XMPPHP
23  * @author       Nathanael C. Fritz <JID: fritzy@netflint.net>
24  * @author       Stephan Wentz <JID: stephan@jabber.wentz.it>
25  * @author       Michael Garvin <JID: gar@netflint.net>
26  * @copyright  2008 Nathanael C. Fritz
27  */
28
29 /**
30  * XMPPHP Roster Object
31  * 
32  * @category   xmpphp 
33  * @package     XMPPHP
34  * @author       Nathanael C. Fritz <JID: fritzy@netflint.net>
35  * @author       Stephan Wentz <JID: stephan@jabber.wentz.it>
36  * @author       Michael Garvin <JID: gar@netflint.net>
37  * @copyright  2008 Nathanael C. Fritz
38  * @version     $Id$
39  */
40
41 class Roster {
42         /**
43          * Roster array, handles contacts and presence.  Indexed by jid.
44          * Contains array with potentially two indexes 'contact' and 'presence'
45          * @var array
46          */
47         protected $roster_array = array();
48         /**
49          * Constructor
50          * 
51          */
52         public function __construct($roster_array = array()) {
53                 if ($this->verifyRoster($roster_array)) {
54                         $this->roster_array = $roster_array; //Allow for prepopulation with existing roster
55                 } else {
56                         $this->roster_array = array();
57                 }
58         }
59
60         /**
61          *
62          * Check that a given roster array is of a valid structure (empty is still valid)
63          *
64          * @param array $roster_array
65          */
66         protected function verifyRoster($roster_array) {
67                 #TODO once we know *what* a valid roster array looks like
68                 return True;
69         }
70
71         /**
72          *
73          * Add given contact to roster
74          *
75          * @param string $jid
76          * @param string $subscription
77          * @param string $name
78          * @param array $groups
79          */
80         public function addContact($jid, $subscription, $name='', $groups=array()) {
81                 $contact = array('jid' => $jid, 'subscription' => $subscription, 'name' => $name, 'groups' => $groups);
82                 if ($this->isContact($jid)) {
83                         $this->roster_array[$jid]['contact'] = $contact;
84                 } else {
85                         $this->roster_array[$jid] = array('contact' => $contact);
86                 }
87         }
88
89         /**
90          * 
91          * Retrieve contact via jid
92          *
93          * @param string $jid
94          */
95         public function getContact($jid) {
96                 if ($this->isContact($jid)) {
97                         return $this->roster_array[$jid]['contact'];
98                 }
99         }
100
101         /**
102          *
103          * Discover if a contact exists in the roster via jid
104          *
105          * @param string $jid
106          */
107         public function isContact($jid) {
108                 return (array_key_exists($jid, $this->roster_array));
109         }
110
111         /**
112          *
113          * Set presence
114          *
115          * @param string $presence
116          * @param integer $priority
117          * @param string $show
118          * @param string $status
119         */
120         public function setPresence($presence, $priority, $show, $status) {
121                 list($jid, $resource) = split("/", $presence);
122                 if ($show != 'unavailable') {
123                         if (!$this->isContact($jid)) {
124                                 $this->addContact($jid, 'not-in-roster');
125                         }
126                         $resource = $resource ? $resource : '';
127                         $this->roster_array[$jid]['presence'][$resource] = array('priority' => $priority, 'show' => $show, 'status' => $status);
128                 } else { //Nuke unavailable resources to save memory
129                         unset($this->roster_array[$jid]['resource'][$resource]);
130                 }
131         }
132
133         /*
134          *
135          * Return best presence for jid
136          *
137          * @param string $jid
138          */
139         public function getPresence($jid) {
140                 $split = split("/", $jid);
141                 $jid = $split[0];
142                 if($this->isContact($jid)) {
143                         $current = array('resource' => '', 'active' => '', 'priority' => -129, 'show' => '', 'status' => ''); //Priorities can only be -128 = 127
144                         foreach($this->roster_array[$jid]['presence'] as $resource => $presence) {
145                                 //Highest available priority or just highest priority
146                                 if ($presence['priority'] > $current['priority'] and (($presence['show'] == "chat" or $presence['show'] == "available") or ($current['show'] != "chat" or $current['show'] != "available"))) {
147                                         $current = $presence;
148                                         $current['resource'] = $resource;
149                                 }
150                         }
151                         return $current;
152                 }
153         }
154         /**
155          *
156          * Get roster
157          *
158          */
159         public function getRoster() {
160                 return $this->roster_array;
161         }
162 }
163 ?>