changed file headers & added AUTHORS file
[PHP-P10.git] / Uplink / P10_Channel.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011  Philipp Kreil (pk910)                            *
4  *                                                                      *
5  * This program is free software: you can redistribute it and/or modify *
6  * it under the terms of the GNU General Public License as published by *
7  * the Free Software Foundation, either version 3 of the License, or    *
8  * (at your option) any later version.                                  *
9  *                                                                      * 
10  * This program is distributed in the hope that it will be useful,      *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
13  * GNU General Public License for more details.                         *
14  *                                                                      *
15  * You should have received a copy of the GNU General Public License    *
16  * along with this program. If not, see <http://www.gnu.org/licenses/>. *
17  *                                                                      *
18  ************************************************************************
19  * 
20  *  Uplink/P10_Channel.class.php
21  *
22  * This class represents a IRC Channel
23  *
24  */
25
26 class P10_Channel {
27         private static $static_channels = array();
28         
29         public static function getChannelByName($name) {
30                 $name = strtolower($name);
31                 if(array_key_exists($name, self::$static_channels)) {
32                         return self::$static_channels[$name];
33                 }
34                 return NULL;
35         }
36         
37         public static function getChannels() {
38                 return self::$static_channels;
39         }
40         
41         public static function getChannelCount() {
42                 return count(self::$static_channels);
43         }
44         
45         public static function recheckAllChannels() {
46                 foreach(self::$static_channels as $channel) {
47                         $channel->checkChannel();
48                 }
49         }       
50         
51         private $name;
52         private $topic;
53         private $modes;
54         private $create_time;
55         private $users = array();
56         const USERPRIV_OPED  = 0x0001;
57         const USERPRIV_VOICE = 0x0002;
58         private $userPrivs = array();
59         
60         public function __construct($name) {
61                 $this->name = $name;
62                 $this->modes = new P10_ChannelModeSet($this);
63                 $this->create_time = time();
64                 self::$static_channels[strtolower($name)] = $this;
65         }
66         
67         public function getName() {
68                 return $this->name;
69         }
70         
71         public function getModes() {
72                 return $this->modes;
73         }
74         
75         public function setTopic($topic) {
76                 $this->topic = $topic;
77         }
78         
79         public function getTopic() {
80                 return $this->topic;
81         }
82         
83         public function setCreateTime($time) {
84                 $this->create_time = $time;
85         }
86         
87         public function getCreateTime() {
88                 return $this->create_time;
89         }
90         
91         public function joinUser($user) {
92                 $this->users[$user->getNumeric()] = $user;
93                 $this->userPrivs[$user->getNumeric()] = 0;
94                 $user->addChannel($this);
95         }
96         
97         public function burstUser($user, $opped, $voiced) {
98                 $this->users[$user->getNumeric()] = $user;
99                 $this->userPrivs[$user->getNumeric()] = ($opped ? self::USERPRIV_OPED : 0) | ($voiced ? self::USERPRIV_VOICE : 0);
100                 $user->addChannel($this);
101         }
102         
103         private function checkChannel() {
104                 if(count($this->users) == 0 && !$this->modes->hasMode('z')) {
105                         unset(self::$static_channels[strtolower($this->name)]); //drop empty channel
106                 }
107         }
108         
109         public function quitUser($user) {
110                 if(array_key_exists($user->getNumeric(), $this->users)) {
111                         unset($this->users[$user->getNumeric()]);
112                         unset($this->userPrivs[$user->getNumeric()]);
113                         //$user->delChannel($this)  is not necessary because the user quits (the whole Object gets removed later)
114                         $this->checkChannel();
115                 } else {
116                         trigger_error("Tried to quit a User from a Channel it is not joined.", E_USER_WARNING);
117                 }
118         }
119         
120         public function partUser($user) {
121                 if(array_key_exists($user->getNumeric(), $this->users)) {
122                         unset($this->users[$user->getNumeric()]);
123                         unset($this->userPrivs[$user->getNumeric()]);
124                         $user->delChannel($this);
125                         $this->checkChannel();
126                 } else {
127                         trigger_error("Tried to part a User from a Channel it is not joined.", E_USER_WARNING);
128                 }
129         }
130         
131         public function getUserPrivs($user) {
132                 if(array_key_exists($user->getNumeric(), $this->users)) {
133                         return $this->userPrivs[$user->getNumeric()];
134                 } else 
135                         return 0;
136         }
137         
138         public function setUserPrivs($user, $privs) {
139                 if(array_key_exists($user->getNumeric(), $this->users)) {
140                         $this->userPrivs[$user->getNumeric()] = $privs;
141                 }
142         }
143         
144         public function getUserCount() {
145                 return count($this->users);
146         }
147         
148         public function getUsers() {
149                 return $this->users;
150         }
151         
152 }
153
154 ?>