added P10_Channel.class.php and recv_burst
[PHP-P10.git] / Uplink / Uplink.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/Uplink.class.php
24  *
25  * This file contains the basic P10 Protocol handler.
26  *
27  ************************************************************************
28  * accessable methods:
29  *
30  * void initialize()
31  *     has to be called after the settings have been set.
32  *
33  * void loop() 
34  *     loop function that should be calles as many times as possible. 
35  *     It reads from the socket and BLOCKS the script execution for a 
36  *     specific time if nothing is received.
37  *
38  * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
39  *     sets the Uplink connection information.
40  *
41  * void setLoopTimeout(int $timeout)
42  *     sets the maximum time loop() is blocking the script execution.
43  *
44  * void setUplinkServer(int $numeric, String $name, String $password, String $description)
45  *     sets the own P10 Server information.
46  *
47  * void setValidateServer(String $name, String $password)
48  *     sets additional security relevant information about the remote server.
49  */
50 require_once("Client.class.php");
51 require_once("Numerics.class.php");
52 require_once("P10Formatter.class.php");
53 require_once("P10_Server.class.php");
54 require_once("P10_User.class.php");
55 require_once("P10_Channel.class.php");
56 require_once("P10_ModeSets.class.php");
57
58 class Uplink {
59         private $client;
60         private $settings = array();
61         private $server;
62         
63         const FLAG_P10SESSION      = 0x0001; //connection is in P10 mode (server is connected)
64         const FLAG_SECURITY_QUIT   = 0x0002; //local connection abort because of security issues
65         const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
66         const FLAG_BURST_PENDING   = 0x0008; //we still have to burst
67         private $flags = 0;
68         
69         public function __construct() {
70                 $this->client = new Client();
71                 $this->setSetting("recv_timeout", 1000);
72         }
73         
74         public function initialize() {
75                 if($this->server) {
76                         trigger_error("Uplink already initialized.", E_USER_ERROR);
77                         return;
78                 }
79                 $self_numeric = $this->getSetting("numeric");
80                 $self_name = $this->getSetting("name");
81                 $self_description = $this->getSetting("description");
82                 if(!$self_numeric || !$self_name) {
83                         trigger_error("Server Settings missing.", E_USER_ERROR);
84                         return;
85                 }
86                 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
87         }
88         
89         public function loop() {
90                 if($this->server == null) {
91                         trigger_error("Uplink not initialized.", E_USER_ERROR);
92                         return;
93                 }
94                 if(!$this->client->connected()) {
95                         if(($this->flags & self::FLAG_P10SESSION)) {
96                                 //Server got disconnected
97                                 $this->server->disconnectServer(true);
98                                 $this->flags &= ~self::FLAG_P10SESSION;
99                         }
100                         $host = $this->getSetting("host");
101                         $port = $this->getSetting("port");
102                         if($host == null || $port == null) {
103                                 trigger_error("Uplink Settings missing.", E_USER_ERROR);
104                                 return;
105                         }
106                         if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
107                                 sleep(1);
108                         }
109                         $state = $this->client->connect($host, $port, $this->getSetting("bind"), $this->getSetting("ssl"), $this->getSetting("recv_timeout"));
110                         if(!$state) {
111                                 usleep($this->getSetting("recv_timeout") / 1000);
112                                 $this->flags |= self::FLAG_NOT_CONNECTABLE;
113                                 return;
114                         }
115                         $this->flags = 0;
116                         $this->loginServer();
117                 }
118                 //try to receive new data from the Uplink
119                 $lines = $this->client->recv();
120                 if($lines == null) return;
121                 foreach($lines as $line) {
122                         $this->parseLine($line);
123                 }
124         }
125         
126         public function setUplinkHost($host, $port, $ssl = false, $bind = null) {
127                 $this->setSetting("host", $host);
128                 $this->setSetting("port", $port);
129                 $this->setSetting("ssl", $ssl);
130                 $this->setSetting("bind", $bind);
131         }
132         
133         public function setLoopTimeout($timeout) {
134                 $this->setSetting("recv_timeout", $timeout);
135         }
136         
137         public function setUplinkServer($numeric, $name, $password, $description) {
138                 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
139                 $this->setSetting("name", $name);
140                 $this->setSetting("password", $password);
141                 $this->setSetting("description", $description);
142         }
143         
144         public function setValidateServer($name, $password) {
145                 $this->setSetting("their_name", $name);
146                 $this->setSetting("their_password", $password);
147         }
148         
149         private function setSetting($setting, $value) {
150                 $this->settings[$setting] = $value;
151         }
152         
153         private function getSetting($setting) {
154                 if(array_key_exists($setting, $this->settings)) {
155                         return $this->settings[$setting];
156                 } else {
157                         return null;
158                 }
159         }
160         
161         private function loginServer() {
162                 $password = $this->getSetting("password");
163                 $this->send("PASS", $password);
164                 $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
165         }
166         
167         private function parseLine($line) {
168                 $highExplode = explode(" :", $line, 2);
169                 $tokens = explode(" ", $highExplode[0]);
170                 if(count($highExplode) > 1)
171                         $tokens[] = $highExplode[1];
172                 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
173                 if($cmdPos == 1) $from = $tokens[0];
174                 else $from = null;
175                 $arguments = array_slice($tokens, $cmdPos + 1);
176                 switch(strtoupper($tokens[$cmdPos])) {
177                 //pre P10 Session
178                         case "PASS":
179                                 $this->recv_pass($from, $arguments);
180                                 break;
181                         case "SERVER":
182                                 $this->recv_server($from, $arguments);
183                                 break;
184                         case "ERROR":
185                                 $this->recv_error($from, $arguments);
186                                 break;
187                 //P10 Session
188                         case "S":
189                                 $this->recv_server($from, $arguments);
190                                 break;
191                         case "G":
192                                 $this->recv_ping($from, $arguments);
193                                 break;
194                         case "N":
195                                 $this->recv_nick($from, $arguments);
196                                 break;
197                         case "EB":
198                                 $this->recv_end_of_burst($from, $arguments);
199                                 break;
200                         case "EA":
201                                 $this->recv_end_of_burst_ack($from, $arguments);
202                                 break;
203                         case "SQ":
204                                 $this->recv_server_quit($from, $arguments);
205                                 break;
206                         case "Q":
207                                 $this->recv_quit($from, $arguments);
208                                 break;
209                         case "B":
210                                 $this->recv_burst($from, $arguments);
211                                 break;
212                 //default
213                         default:
214                                 //unknown cmd
215                                 break;
216                 }
217         }
218         
219         private function send($command) {
220                 if(func_num_args() > 1) {
221                         $args = array_slice(func_get_args(), 1);
222                         $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
223                 } else {
224                         $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
225                 }
226                 $this->client->send($command);
227         }
228         
229         /********************************************************************************************
230          *                                     INCOMING COMMANDS                                    *
231          ********************************************************************************************/
232         
233         private function recv_pass($from, $args) {
234                 $their_pass = $this->getSetting("their_password");
235                 if($their_pass) {
236                         if($args[0] != $their_pass) {
237                                 //security QUIT
238                                 $this->flags |= self::FLAG_SECURITY_QUIT;
239                                 $this->send("ERROR", "Incorrect password received.");
240                                 $this->client->disconnect();
241                         }
242                 }
243         }
244         
245         private function recv_error($from, $args) {
246                 //we received an error - the socket is dead for us, now
247                 //maybe we want to log this, later
248                 $this->client->disconnect();
249         }
250         
251         private function recv_server($from, $args) {
252                 if($from == null) {
253                         //our uplink Server
254                         $their_name = $this->getSetting("their_name");
255                         if($their_name && $args[0] != $their_name) {
256                                 $this->flags |= self::FLAG_SECURITY_QUIT;
257                                 $this->send("ERROR", "Invalid Server name");
258                                 $this->client->disconnect();
259                                 return;
260                         }
261                         $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
262                         $this->server->addServer($new_server);
263                         $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
264                 } else {
265                         //another server got a new slave server ^^
266                         $server = P10_Server::getServerByNum($from);
267                         if($server == null) {
268                                 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
269                                 return;
270                         }
271                         $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
272                         $server->addServer($new_server);
273                 }
274         }
275         
276         private function recv_ping($from, $args) {
277                 $this->send("Z", $args[0]); //simply PONG
278         }
279         
280         private function recv_nick($from, $args) {
281                 if(count($args) <= 2) {
282                         //Nick change
283                         $user = P10_User::getUserByNum($from);
284                         if($user == null) {
285                                 trigger_error("Server tries to change the nick of an user that does not exist or was not found on recv_nick.", E_USER_ERROR);
286                                 return;
287                         }
288                         $nick->setNick($args[0]);
289                 } else {
290                         //New User
291                         $numeric = $args[count($args)-2];
292                         $nick = $args[0];
293                         $server = P10_Server::getServerByNum($from);
294                         if($server == null) {
295                                 trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
296                                 return;
297                         }
298                         if(substr($numeric,0,2) != $from) {
299                                 trigger_error("A Server (".$from.") tries to connect a User with an invalid User numeric ('".$numeric."' does not belong to the Server)", E_USER_WARNING);
300                         }
301                         $connect_time = $args[2];
302                         $ident = $args[3];
303                         $host = $args[4];
304                         $modes = implode(" ",array_slice($args, 5, count($args)-8));
305                         $modes = new P10_UserModeSet($modes);
306                         $ip = Numerics::parseIP($args[count($args)-3]);
307                         $realname = $args[count($args)-1];
308                         new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
309                 }
310         }
311         
312         private function recv_end_of_burst($from, $args) {
313                 if(($this->flags & self::FLAG_BURST_PENDING)) {
314                         $this->burst();
315                         $this->send("EA");
316                         $this->flags &= ~self::FLAG_BURST_PENDING;
317                 }
318         }
319         
320         private function recv_end_of_burst_ack($from, $args) {
321                 //nothing to do here?
322         }
323         
324         private function recv_server_quit($from, $args) {
325                 $server = P10_Server::getServerByName($args[0]);
326                 if($server == null) {
327                         trigger_error("Server (".$args[0].") not found.", E_USER_ERROR);
328                         return;
329                 }
330                 $server->disconnectServer();
331         }
332         
333         private function recv_quit($from, $args) {
334                 $user = P10_User::getUserByNum($from);
335                 if($user == null) {
336                         trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
337                         return;
338                 }
339                 $user->quit($args[0]);
340         }
341         
342         private function recv_burst($from, $args) {
343                 //AK B #aide 1292621006 +tn ALAMH:o
344                 $name = $args[0];
345                 $create_time = $args[1];
346                 $channel = P10_Channel::getChannelByName($name);
347                 if($name == null)
348                         $channel = new P10_Channel($name);
349                 $modes = $channel->getModes();
350                 $userstr = $args[count($args)-1];
351                 $modeparamcount = count($args)-3;
352                 if($userstr[0] == "%") {
353                         //ban list
354                         $banlist = explode(" ", substr($userstr, 1));
355                         foreach($banlist as $ban) {
356                                 //TODO: save bans
357                         }
358                         $userstr = $args[count($args)-2];
359                         $modeparamcount--;
360                 }
361                 $users = explode(":",$userstr);
362                 $isop = false; $isvoice = false;
363                 foreach($users as $user) {
364                         $uexp = explode(":", $user);
365                         if(strlen($uexp[0]) != 6) {
366                                 trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_ERROR);
367                                 return;
368                         }
369                         if(count($uexp) > 1) {
370                                 $isop = false;
371                                 $isvoice = false;
372                                 for($i = 0; $i < strlen($uexp[1]); $i++) {
373                                         if($uexp[1][0] == "@") $isop = true;
374                                         if($uexp[1][0] == "+") $isvoice = true;
375                                 }
376                         }
377                         $user = P10_User::getUserByNum($uexp[0]);
378                         if($user == null) {
379                                 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
380                                 return;
381                         }
382                         $channel->burstUser($user, $isop, $isvoice);
383                 }
384                 $modes->parseModes(implode(array_slice($args, 2, $modeparamcount)));
385         }
386         
387         /********************************************************************************************
388          *                                     SERVER FUNCTIONS                                     *
389          ********************************************************************************************/
390         
391         private function burst() {
392                 foreach($this->server->getUsers() as $user) {
393                         $nick = $user->getNick();
394                         $connect_time = $user->getConnectTime();
395                         $ident = $user->getIdent();
396                         $host = $user->getHost();
397                         $modes = $user->getModes()->getModeString();
398                         $ip = Numerics::numericFromIP($user->getIP());
399                         $numeric = $user->getNumeric();
400                         $realname = $user->getRealname();
401                         $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);
402                 }
403                 $this->send("EB");
404         }
405         
406 }
407
408 ?>