Another year is about to end... So we have to update these damn copyright information :P
[PHP-P10.git] / Uplink / P10_Channel.class.php
1 <?php
2 /******************************* PHP-P10 v2 *****************************
3  * Copyright (C) 2011-2012  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_HALFOP = 0x0002;
58     const USERPRIV_VOICE = 0x0004;
59         private $userPrivs = array();
60         
61         public function __construct($name) {
62                 $this->name = $name;
63                 $this->modes = new P10_ChannelModeSet($this);
64                 $this->create_time = time();
65                 self::$static_channels[strtolower($name)] = $this;
66         }
67         
68         public function getName() {
69                 return $this->name;
70         }
71         
72         public function getModes() {
73                 return $this->modes;
74         }
75         
76         public function setTopic($topic) {
77                 $this->topic = $topic;
78         }
79         
80         public function getTopic() {
81                 return $this->topic;
82         }
83         
84         public function setCreateTime($time) {
85                 $this->create_time = $time;
86         }
87         
88         public function getCreateTime() {
89                 return $this->create_time;
90         }
91         
92         public function joinUser($user) {
93                 $this->users[$user->getNumeric()] = $user;
94                 $this->userPrivs[$user->getNumeric()] = 0;
95                 $user->addChannel($this);
96         }
97         
98         public function burstUser($user, $opped, $halfopped, $voiced) {
99                 $this->users[$user->getNumeric()] = $user;
100                 $this->userPrivs[$user->getNumeric()] = ($opped ? self::USERPRIV_OPED : 0) | ($halfopped ? self::USERPRIV_HALFOP : 0) | ($voiced ? self::USERPRIV_VOICE : 0);
101                 $user->addChannel($this);
102         }
103         
104         private function checkChannel() {
105                 if(count($this->users) == 0 && !$this->modes->hasMode('z')) {
106                         unset(self::$static_channels[strtolower($this->name)]); //drop empty channel
107                 }
108         }
109         
110         public function quitUser($user) {
111                 if(array_key_exists($user->getNumeric(), $this->users)) {
112                         unset($this->users[$user->getNumeric()]);
113                         unset($this->userPrivs[$user->getNumeric()]);
114                         //$user->delChannel($this)  is not necessary because the user quits (the whole Object gets removed later)
115                         $this->checkChannel();
116                 } else {
117                         trigger_error("Tried to quit a User from a Channel it is not joined.", E_USER_WARNING);
118                 }
119         }
120         
121         public function partUser($user) {
122                 if(array_key_exists($user->getNumeric(), $this->users)) {
123                         unset($this->users[$user->getNumeric()]);
124                         unset($this->userPrivs[$user->getNumeric()]);
125                         $user->delChannel($this);
126                         $this->checkChannel();
127                 } else {
128                         trigger_error("Tried to part a User from a Channel it is not joined.", E_USER_WARNING);
129                 }
130         }
131         
132         public function getUserPrivs($user) {
133                 if(array_key_exists($user->getNumeric(), $this->users)) {
134                         return $this->userPrivs[$user->getNumeric()];
135                 } else 
136                         return 0;
137         }
138         
139         public function setUserPrivs($user, $privs) {
140                 if(array_key_exists($user->getNumeric(), $this->users)) {
141                         $this->userPrivs[$user->getNumeric()] = $privs;
142                 }
143         }
144         
145         public function getUserCount() {
146                 return count($this->users);
147         }
148         
149         public function getUsers() {
150                 return $this->users;
151         }
152         
153 }
154
155 ?>