added recv_join & recv_part
[PHP-P10.git] / Uplink / P10_Channel.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
7  * PHP-P10 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  * This program 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 PHP-P10; if not, write to the Free Software Foundation,   *
19  * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Uplink/P10_Channel.class.php
24  *
25  * This class represents a IRC Channel
26  *
27  ************************************************************************
28  * accessable methods:
29  *
30  * static P10_User getChannelByName(String $name) 
31  *     searches and returns the Channel with the provided name
32  *
33  * __construct(String $name)
34  *     *** nothing to say here ***
35  *
36  */
37
38 class P10_Channel {
39         private static $static_channels = array();
40         
41         public static function getChannelByName($name) {
42                 $name = strtolower($name);
43                 if(array_key_exists($name, self::$static_channels)) {
44                         return self::$static_channels[$name];
45                 }
46                 return NULL;
47         }
48         
49         public static function getChannels() {
50                 return self::$static_channels;
51         }       
52         
53         
54         private $name;
55         private $topic;
56         private $modes;
57         private $create_time;
58         private $users = array();
59         const USERPRIV_OPED  = 0x0001;
60         const USERPRIV_VOICE = 0x0002;
61         private $userPrivs = array();
62         
63         public function __construct($name) {
64                 $this->name = $name;
65                 $this->modes = new P10_ChannelModeSet($this);
66                 $this->create_time = time();
67                 self::$static_channels[strtolower($name)] = $this;
68         }
69         
70         public function getName() {
71                 return $this->name;
72         }
73         
74         public function getModes() {
75                 return $this->modes;
76         }
77         
78         public function setTopic($topic) {
79                 $this->topic = $topic;
80         }
81         
82         public function getTopic() {
83                 return $this->topic;
84         }
85         
86         public function setCreateTime($time) {
87                 $this->create_time = $time;
88         }
89         
90         public function getCreateTime() {
91                 return $this->create_time;
92         }
93         
94         public function joinUser($user) {
95                 $this->users[$user->getNumeric()] = $user;
96                 $this->userPrivs[$user->getNumeric()] = 0;
97                 $user->addChannel($this);
98         }
99         
100         public function burstUser($user, $opped, $voiced) {
101                 $this->users[$user->getNumeric()] = $user;
102                 $this->userPrivs[$user->getNumeric()] = ($opped ? self::USERPRIV_OPED : 0) | ($voiced ? self::USERPRIV_VOICE : 0);
103                 $user->addChannel($this);
104         }
105         
106         public function quitUser($user) {
107                 if(array_key_exists($user->getNumeric(), $this->users)) {
108                         unset($this->users[$user->getNumeric()]);
109                         unset($this->userPrivs[$user->getNumeric()]);
110                         //$user->delChannel($this)  is not necessary because the user quits (the whole Object gets removed later)
111                 } else {
112                         trigger_error("Tried to quit a User from a Channel it is not joined.", E_USER_WARNING);
113                 }
114         }
115         
116         public function partUser($user) {
117                 if(array_key_exists($user->getNumeric(), $this->users)) {
118                         unset($this->users[$user->getNumeric()]);
119                         unset($this->userPrivs[$user->getNumeric()]);
120                         $user->delChannel($this);
121                 } else {
122                         trigger_error("Tried to part a User from a Channel it is not joined.", E_USER_WARNING);
123                 }
124         }
125         
126         public function getUserPrivs($user) {
127                 if(array_key_exists($user->getNumeric(), $this->users)) {
128                         return $this->userPrivs[$user->getNumeric()];
129                 } else 
130                         return 0;
131         }
132         
133         public function setUserPrivs($user, $privs) {
134                 if(array_key_exists($user->getNumeric(), $this->users)) {
135                         $this->userPrivs[$user->getNumeric()] = $privs;
136                 }
137         }
138         
139         public function getUsers() {
140                 return $this->users;
141         }
142         
143 }
144
145 ?>