037d4633170c904026ee89f5f144e87294f52c94
[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  * void setHISOptions(String $serverName, String $serverDescription String $usermask)
51  *     sets the default Account fakehost
52  *
53  * void setEventHandler(EventHandler $event_handler)
54  *     sets the EventHandlder
55  */
56 require_once("Client.class.php");
57 require_once("Numerics.class.php");
58 require_once("P10Formatter.class.php");
59 require_once("P10_Server.class.php");
60 require_once("P10_User.class.php");
61 require_once("P10_Channel.class.php");
62 require_once("P10_ModeSets.class.php");
63 require_once("EventHandler.interface.php");
64
65 $e=1;
66 define("ERR_NICK_IN_USE", $e++);
67 define("ERR_INVALID_USER", $e++);
68 define("ERR_INVALID_CHANNAME", $e++);
69 define("ERR_NOT_CONNECTED", $e++);
70 define("ERR_NOT_ON_CHANNEL", $e++);
71
72 class Uplink {
73         private $client;
74         private $settings = array();
75         private $server;
76         private $eventHandler = null;
77         private $last_local_numeric = 1;
78         
79         const FLAG_P10SESSION      = 0x0001; //connection is in P10 mode (server is connected)
80         const FLAG_SECURITY_QUIT   = 0x0002; //local connection abort because of security issues
81         const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
82         const FLAG_BURST_PENDING   = 0x0008; //we still have to burst
83         const FLAG_CONNECTED       = 0x0010; //connected and synced (ready)
84         const FLAG_GOT_PASS        = 0x0020; //got PASS from the remote Server
85         private $flags = 0;
86         
87         public function __construct() {
88                 $this->client = new Client();
89                 $this->setSetting("recv_timeout", 1000);
90                 $this->setSetting("his_usermask", "user.NoMask");
91         }
92         
93         public function initialize() {
94                 if($this->server) {
95                         trigger_error("Uplink already initialized.", E_USER_ERROR);
96                         return;
97                 }
98                 $self_numeric = $this->getSetting("numeric");
99                 $self_name = $this->getSetting("name");
100                 $self_description = $this->getSetting("description");
101                 if(!$self_numeric || !$self_name) {
102                         trigger_error("Server Settings missing.", E_USER_ERROR);
103                         return;
104                 }
105                 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
106         }
107         
108         public function loop() {
109                 if($this->server == null) {
110                         trigger_error("Uplink not initialized.", E_USER_ERROR);
111                         return;
112                 }
113                 if(!$this->client->connected()) {
114                         if(($this->flags & self::FLAG_P10SESSION)) {
115                                 //Server got disconnected
116                                 $this->server->disconnectServer($this->eventHandler, true);
117                                 $this->flags &= ~self::FLAG_P10SESSION;
118                         }
119                         $host = $this->getSetting("host");
120                         $port = $this->getSetting("port");
121                         if($host == null || $port == null) {
122                                 trigger_error("Uplink Settings missing.", E_USER_ERROR);
123                                 return;
124                         }
125                         if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
126                                 sleep(1);
127                         }
128                         $state = $this->client->connect($host, $port, $this->getSetting("bind"), $this->getSetting("ssl"), $this->getSetting("recv_timeout"));
129                         if(!$state) {
130                                 usleep($this->getSetting("recv_timeout") / 1000);
131                                 $this->flags |= self::FLAG_NOT_CONNECTABLE;
132                                 return;
133                         }
134                         $this->flags = 0;
135                         $this->loginServer();
136                 }
137                 //try to receive new data from the Uplink
138                 $lines = $this->client->recv();
139                 if($lines == null) return;
140                 foreach($lines as $line) {
141                         $this->parseLine($line);
142                 }
143         }
144         
145         public function setUplinkHost($host, $port, $ssl = false, $bind = null) {
146                 $this->setSetting("host", $host);
147                 $this->setSetting("port", $port);
148                 $this->setSetting("ssl", $ssl);
149                 $this->setSetting("bind", $bind);
150         }
151         
152         public function setLoopTimeout($timeout) {
153                 $this->setSetting("recv_timeout", $timeout);
154         }
155         
156         public function setUplinkServer($numeric, $name, $password, $description) {
157                 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
158                 $this->setSetting("name", $name);
159                 $this->setSetting("password", $password);
160                 $this->setSetting("description", $description);
161         }
162         
163         public function setValidateServer($name, $password) {
164                 $this->setSetting("their_name", $name);
165                 $this->setSetting("their_password", $password);
166         }
167         
168         public function setHISOptions($servername, $serverdesc, $usermask) {
169                 $this->setSetting("his_name", $servername);
170                 $this->setSetting("his_desc", $serverdesc);
171                 $this->setSetting("his_usermask", $usermask);
172         }
173         
174         public function setEventHandler($event_handler) {
175                 if(!is_a($event_handler, "EventHandler")) {
176                         trigger_error((is_object($event_handler) ? get_class($event_handler) : gettype($event_handler))." is NOT a valid EventHandler.", E_USER_ERROR);
177                         return;
178                 }
179                 $this->eventHandler = $event_handler;
180         }
181         
182         private function setSetting($setting, $value) {
183                 $this->settings[$setting] = $value;
184         }
185         
186         private function getSetting($setting) {
187                 if(array_key_exists($setting, $this->settings)) {
188                         return $this->settings[$setting];
189                 } else {
190                         return null;
191                 }
192         }
193         
194         private function loginServer() {
195                 $password = $this->getSetting("password");
196                 $this->send("PASS", $password);
197                 $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
198         }
199         
200         private function parseLine($line) {
201                 $highExplode = explode(" :", $line, 2);
202                 $tokens = explode(" ", $highExplode[0]);
203                 if(count($highExplode) > 1)
204                         $tokens[] = $highExplode[1];
205                 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
206                 if($cmdPos == 1) $from = $tokens[0];
207                 else $from = null;
208                 $arguments = array_slice($tokens, $cmdPos + 1);
209                 if(($this->flags & self::FLAG_P10SESSION) && $this->eventHandler) {
210                         $this->eventHandler->event_preparse($from, strtoupper($tokens[$cmdPos]), $arguments);
211                 }
212                 switch(strtoupper($tokens[$cmdPos])) {
213                 //pre P10 Session
214                         case "PASS":
215                                 $this->recv_pass($from, $arguments);
216                                 break;
217                         case "SERVER":
218                                 $this->recv_server($from, $arguments);
219                                 break;
220                         case "ERROR":
221                                 $this->recv_error($from, $arguments);
222                                 break;
223                 //P10 Session
224                         case "S":
225                                 $this->recv_server($from, $arguments);
226                                 break;
227                         case "G":
228                                 $this->recv_ping($from, $arguments);
229                                 break;
230                         case "N":
231                                 $this->recv_nick($from, $arguments);
232                                 break;
233                         case "EB":
234                                 $this->recv_end_of_burst($from, $arguments);
235                                 break;
236                         case "EA":
237                                 $this->recv_end_of_burst_ack($from, $arguments);
238                                 break;
239                         case "SQ":
240                                 $this->recv_server_quit($from, $arguments);
241                                 break;
242                         case "Q":
243                                 $this->recv_quit($from, $arguments);
244                                 break;
245                         case "B":
246                                 $this->recv_burst($from, $arguments);
247                                 break;
248                         case "J":
249                         case "C":
250                                 $this->recv_join($from, $arguments);
251                                 break;
252                         case "L":
253                                 $this->recv_part($from, $arguments);
254                                 break;
255                         case "K":
256                                 $this->recv_kick($from, $arguments);
257                                 break;
258                         case "D":
259                                 $this->recv_kill($from, $arguments);
260                                 break;
261                         case "P":
262                                 $this->recv_privmsg($from, $arguments);
263                                 break;
264                         case "O":
265                                 $this->recv_notice($from, $arguments);
266                                 break;
267                         case "W":
268                                 $this->recv_whois($from, $arguments);
269                                 break;
270                 //default
271                         default:
272                                 //unknown cmd
273                                 if($this->eventHandler)
274                                         $this->eventHandler->event_unknown_cmd($from, strtoupper($tokens[$cmdPos]), $arguments);
275                                 break;
276                 }
277         }
278         
279         private function send($command) {
280                 if(func_num_args() > 1) {
281                         $args = array_slice(func_get_args(), 1);
282                         $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
283                 } else {
284                         $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
285                 }
286                 $this->client->send($command);
287         }
288         
289         /********************************************************************************************
290          *                                     INCOMING COMMANDS                                    *
291          ********************************************************************************************/
292         
293         private function recv_pass($from, $args) {
294                 $their_pass = $this->getSetting("their_password");
295                 if($their_pass) {
296                         if($args[0] != $their_pass) {
297                                 //security QUIT
298                                 $this->flags |= self::FLAG_SECURITY_QUIT;
299                                 $this->send("ERROR", "Incorrect password received.");
300                                 $this->client->disconnect();
301                                 return;
302                         }
303                         $this->flags |= self::FLAG_GOT_PASS;
304                 }
305         }
306         
307         private function recv_error($from, $args) {
308                 //we received an error - the socket is dead for us, now
309                 //maybe we want to log this, later
310                 $this->client->disconnect();
311         }
312         
313         private function recv_server($from, $args) {
314                 if($from == null) {
315                         //our uplink Server
316                         $their_name = $this->getSetting("their_name");
317                         if($their_name && $args[0] != $their_name) {
318                                 $this->flags |= self::FLAG_SECURITY_QUIT;
319                                 $this->send("ERROR", "Invalid Server name");
320                                 $this->client->disconnect();
321                                 return;
322                         }
323                         if($this->getSetting("their_password") && !($this->flags & self::FLAG_GOT_PASS)) {
324                                 $this->flags |= self::FLAG_SECURITY_QUIT;
325                                 $this->send("ERROR", "PASS missing.");
326                                 $this->client->disconnect();
327                                 return;
328                         }
329                         $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
330                         $this->server->addServer($new_server);
331                         $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
332                         if($this->eventHandler)
333                                 $this->eventHandler->event_server($new_server, !($this->flags & self::FLAG_CONNECTED));
334                 } else {
335                         //another server got a new slave server ^^
336                         $server = P10_Server::getServerByNum($from);
337                         if($server == null) {
338                                 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
339                                 return;
340                         }
341                         $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
342                         $server->addServer($new_server);
343                         if($this->eventHandler)
344                                 $this->eventHandler->event_server($new_server, !($this->flags & self::FLAG_CONNECTED));
345                 }
346         }
347         
348         private function recv_ping($from, $args) {
349                 $this->send("Z", $args[0]); //simply PONG
350         }
351         
352         private function recv_nick($from, $args) {
353                 if(count($args) <= 2) {
354                         //Nick change
355                         $user = P10_User::getUserByNum($from);
356                         if($user == null) {
357                                 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);
358                                 return;
359                         }
360                         if($this->eventHandler)
361                                 $this->eventHandler->event_nick($user, $args[0]);
362                         $user->setNick($args[0]);
363                 } else {
364                         //New User
365                         $numeric = $args[count($args)-2];
366                         $nick = $args[0];
367                         $server = P10_Server::getServerByNum($from);
368                         if($server == null) {
369                                 trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
370                                 return;
371                         }
372                         if(substr($numeric,0,2) != $from) {
373                                 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);
374                         }
375                         $connect_time = $args[2];
376                         $ident = $args[3];
377                         $host = $args[4];
378                         $modes = implode(" ",array_slice($args, 5, count($args)-8));
379                         $modes = new P10_UserModeSet($modes);
380                         $ip = Numerics::parseIP($args[count($args)-3]);
381                         $realname = $args[count($args)-1];
382                         $user = new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
383                         if($this->eventHandler)
384                                 $this->eventHandler->event_connect($user, !($this->flags & self::FLAG_CONNECTED));
385                 }
386         }
387         
388         private function recv_end_of_burst($from, $args) {
389                 if(($this->flags & self::FLAG_BURST_PENDING)) {
390                         $this->burst();
391                         $this->send("EA");
392                         $this->flags &= ~self::FLAG_BURST_PENDING;
393                 }
394         }
395         
396         private function recv_end_of_burst_ack($from, $args) {
397                 $this->flags |= self::FLAG_CONNECTED;
398         }
399         
400         private function recv_server_quit($from, $args) {
401                 $server = P10_Server::getServerByName($args[0]);
402                 if($server == null) {
403                         trigger_error("Server (".$args[0].") not found.", E_USER_ERROR);
404                         return;
405                 }
406                 $server->disconnectServer($this->eventHandler);
407         }
408         
409         private function recv_quit($from, $args) {
410                 $user = P10_User::getUserByNum($from);
411                 if($user == null) {
412                         trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
413                         return;
414                 }
415                 if($this->eventHandler)
416                         $this->eventHandler->event_quit($user, $args[0]);
417                 $user->quit($args[0]);
418         }
419         
420         private function recv_burst($from, $args) {
421                 $name = $args[0];
422                 $create_time = $args[1];
423                 $channel = P10_Channel::getChannelByName($name);
424                 if($channel == null)
425                         $channel = new P10_Channel($name);
426                 $channel->setCreateTime($create_time);
427                 $modes = $channel->getModes();
428                 $userstr = $args[count($args)-1];
429                 $modeparamcount = count($args)-3;
430                 if($userstr[0] == "%") {
431                         //ban list
432                         $banlist = explode(" ", substr($userstr, 1));
433                         foreach($banlist as $ban) {
434                                 //TODO: save bans
435                         }
436                         $userstr = $args[count($args)-2];
437                         $modeparamcount--;
438                 }
439                 if($userstr[0] == "+") { //MODE String
440                         $modeparamcount++;
441                         $userstr = "";
442                 }
443                 $users = explode(",",$userstr);
444                 $isop = false; $isvoice = false;
445                 foreach($users as $user) {
446                         $uexp = explode(":", $user);
447                         if(strlen($uexp[0]) != 5) {
448                                 trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_ERROR);
449                                 return;
450                         }
451                         if(count($uexp) > 1) {
452                                 $isop = false;
453                                 $isvoice = false;
454                                 for($i = 0; $i < strlen($uexp[1]); $i++) {
455                                         if($uexp[1][0] == "@") $isop = true;
456                                         if($uexp[1][0] == "+") $isvoice = true;
457                                 }
458                         }
459                         $user = P10_User::getUserByNum($uexp[0]);
460                         if($user == null) {
461                                 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
462                                 return;
463                         }
464                         $channel->burstUser($user, $isop, $isvoice);
465                         if($this->eventHandler)
466                                 $this->eventHandler->event_join($user, $channel, true);
467                 }
468                 $modes->parseModes(implode(array_slice($args, 2, $modeparamcount)));
469         }
470         
471         private function recv_join($from, $args) {
472                 $user = P10_User::getUserByNum($from);
473                 if($user == null) {
474                         trigger_error("Server tries to join an user that does not exist or was not found on recv_join.", E_USER_ERROR);
475                         return;
476                 }
477                 $channel = P10_Channel::getChannelByName($args[0]);
478                 if($channel == null)
479                         $channel = new P10_Channel($args[0]);
480                 $channel->joinUser($user);
481                 if($this->eventHandler)
482                         $this->eventHandler->event_join($user, $channel, false);
483         }
484         
485         private function recv_part($from, $args) {
486                 $user = P10_User::getUserByNum($from);
487                 if($user == null) {
488                         trigger_error("Server tries to part an user that does not exist or was not found on recv_join.", E_USER_ERROR);
489                         return;
490                 }
491                 $channel = P10_Channel::getChannelByName($args[0]);
492                 if($channel == null)
493                         $channel = new P10_Channel($args[0]);
494                 if($this->eventHandler)
495                         $this->eventHandler->event_part($user, $channel, $args[1]);
496                 $channel->partUser($user);
497         }
498         
499         private function recv_kick($from, $args) {
500                 $user = P10_User::getUserByNum($from);
501                 if($user == null) {
502                         trigger_error("An unknown user tries to kick another user on recv_kick.", E_USER_ERROR);
503                         return;
504                 }
505                 $channel = P10_Channel::getChannelByName($args[0]);
506                 if($channel == null)
507                         $channel = new P10_Channel($args[0]);
508                 $target = P10_User::getUserByNum($args[1]);
509                 if($target == null) {
510                         trigger_error("Someone tries to kick an user that does not exist or was not found on recv_kick.", E_USER_ERROR);
511                         return;
512                 }
513                 if($this->eventHandler)
514                         $this->eventHandler->event_kick($user, $target, $channel, $args[1]);
515                 $channel->partUser($user);
516         }
517         
518         private function recv_kill($from, $args) {
519                 $user = P10_User::getUserByNum($args[0]);
520                 if($user == null) {
521                         trigger_error("Server tries to kill an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
522                         return;
523                 }
524                 if($this->eventHandler)
525                         $this->eventHandler->event_quit($user, "Killed (".$args[1].")");
526                 $user->quit($args[1]);
527         }
528         
529         private function recv_privmsg($from, $args) {
530                 $user = P10_User::getUserByNum($from);
531                 if($user == null) {
532                         trigger_error("Server tries to send a privmsg from an user that does not exist or was not found on recv_privmsg.", E_USER_ERROR);
533                         return;
534                 }
535                 if($this->eventHandler) {
536                         if($args[0] == "#") {
537                                 $channel = P10_Channel::getChannelByName($args[0]);
538                                 if($channel == null)
539                                         $channel = new P10_Channel($args[0]);
540                                 $this->eventHandler->event_chanmessage($user, $channel, $args[1]);
541                         } else {
542                                 $targetUser = P10_User::getUserByNum($args[0]);
543                                 if($targetUser == null) {
544                                         trigger_error("Server tries to send a privmsg to an user that does not exist or was not found on recv_privmsg.", E_USER_ERROR);
545                                         return;
546                                 }
547                                 $this->eventHandler->event_privmessage($user, $targetUser, $args[1]);
548                         }
549                 }
550         }
551         
552         private function recv_notice($from, $args) {
553                 $user = P10_User::getUserByNum($from);
554                 if($user == null) {
555                         trigger_error("Server tries to send a notice from an user that does not exist or was not found on recv_notice.", E_USER_ERROR);
556                         return;
557                 }
558                 if($this->eventHandler) {
559                         if($args[0] == "#") {
560                                 $channel = P10_Channel::getChannelByName($args[0]);
561                                 if($channel == null)
562                                         $channel = new P10_Channel($args[0]);
563                                 $this->eventHandler->event_channotice($user, $channel, $args[1]);
564                         } else {
565                                 $targetUser = P10_User::getUserByNum($args[0]);
566                                 if($targetUser == null) {
567                                         trigger_error("Server tries to send a notice to an user that does not exist or was not found on recv_notice.", E_USER_ERROR);
568                                         return;
569                                 }
570                                 $this->eventHandler->event_privnotice($user, $targetUser, $args[1]);
571                         }
572                 }
573         }
574         
575         private function recv_whois($from, $args) {
576                 /* [get] ACAAF W AX :NetworkServ */
577                 $fromUser = P10_User::getUserByNum($from);
578                 if($fromUser == null) {
579                         trigger_error("Server tries to send a whois from an user that does not exist or was not found on recv_whois.", E_USER_ERROR);
580                         return;
581                 }
582                 $users=explode(",",$args[1]);
583                 foreach($users as $nick) {
584                         $user = P10_User::getUserByNick($nick);
585                         if(!$user) {
586                                 $this->send("401", $from, $nick);
587                                 continue;
588                         }
589                         $nick = $user->getNick();
590                         $ident = $user->getIdent();
591                         $hostmask = $user->getHost();
592                         $modes = $user->getModes();
593                         if($modes->hasMode('x')) {
594                                 if(($fakehost = $modes->hasMode('f'))) {
595                                         $hostmask = $fakehost;
596                                 } elseif(($account = $modes->hasMode('r'))) {
597                                         $hostmask = $account.".".$this->getSetting("his_usermask");
598                                 }
599                         }
600                         $realname = $user->getRealname();
601                         $this->send("311", $from , $nick, $ident, $hostmask, $realname);
602                         if(((!$modes->hasMode('n') && !$modes->hasMode('k')) || $from == $user->getNumeric()) && count($user->getChannels()) != 0) {
603                                 $channels = "";
604                                 foreach($user->getChannels() as $channel) {
605                                         $cmodes = $channel->getModes();
606                                         $privs = $channel->getUserPrivs($user);
607                                         if($cmodes->hasMode("s") && !$fromUser->isOnChannel($channel) && $from != $user->getNumeric()) continue;
608                                         if($cmodes->hasMode("u") && ($privs & (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE)) == 0 && $from != $user->getNumeric()) continue;
609                                         $chanstr = ($channels == "" ? "" : " ");
610                                         $prefix = "";
611                                         if(($privs & P10_Channel::USERPRIV_OPED)) {
612                                                 $prefix = "@";
613                                         } else if(($privs & P10_Channel::USERPRIV_VOICE)) {
614                                                 $prefix = "+";
615                                         }
616                                         $chanstr .= $prefix.$channel->getName();
617                                         if(strlen($channels) + strlen($chanstr) > 450) {
618                                                 $this->send("319", $from, $nick, $channels);
619                                                 $channels = $prefix.$channel->getName();
620                                         }
621                                 }
622                                 if($channels != "") {
623                                         $this->send("319", $from, $nick, $channels);
624                                 }
625                                 if($fromUser->getModes()->hasMode("o") || $from == $user->getNumeric() || !$this->getSetting("his_name")) {
626                                         $this->send("312", $from, $nick, $user->getServer()->getName(), $user->getServer()->getDescription());
627                                 } else {
628                                         $this->send("312", $from, $nick, $this->getSetting("his_name"), $this->getSetting("his_desc"));
629                                 }
630                                 if($modes->hasMode("o") && (!$modes->hasMode("H") || $fromUser->getModes()->hasMode("o"))) {
631                                         if($modes->hasMode("S")) {
632                                                 if($modes->hasMode("D"))
633                                                         $this->send("313", $from, $nick, "is a Network Service");
634                                                 else
635                                                         $this->send("313", $from, $nick, "is an illegal Network Service - HACKER!");
636                                         } else
637                                                 $this->send("313", $from, $nick, "is an IRC Operator");
638                                 }
639                                 if(($auth = $modes->hasMode("r"))) {
640                                         $this->send("330", $from, $nick, $auth);
641                                 }
642                         }
643                 }
644                 $this->send("318", $from, $args[1]);
645         }
646         
647         /********************************************************************************************
648          *                                     SERVER FUNCTIONS                                     *
649          ********************************************************************************************/
650         
651         private function burst() {
652                 foreach($this->server->getUsers() as $user) {
653                         $nick = $user->getNick();
654                         $connect_time = $user->getConnectTime();
655                         $ident = $user->getIdent();
656                         $host = $user->getHost();
657                         $modes = $user->getModes()->getModeString();
658                         $ip = Numerics::numericFromIP($user->getIP());
659                         $numeric = $user->getNumeric();
660                         $realname = $user->getRealname();
661                         $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);
662                 }
663                 foreach(P10_Channel::getChannels() as $channel) {
664                         $sorted_users = array('ov' => array(), 'o' => array(), 'v' => array(), '-' => array());
665                         $local_users = false;
666                         foreach($channel->getUsers() as $user) {
667                                 if(substr($user->getNumeric(), 0, 2) != $this->server->getNumeric()) continue; //skip users that are not on the local server
668                                 $privs = $channel->getUserPrivs($user);
669                                 $strPrivs = "";
670                                 if(($privs & P10_Channel::USERPRIV_OPED)) $strPrivs .= "o";
671                                 if(($privs & P10_Channel::USERPRIV_VOICE)) $strPrivs .= "v";
672                                 if($strPrivs == "") $strPrivs = "-";
673                                 $local_users = true;
674                                 $sorted_users[$strPrivs][] = $user;
675                         }
676                         if(!$local_users && !$channel->getModes()->hasMode("z")) continue;
677                         $userStr = "";
678                         foreach($sorted_users['-'] as $user) {
679                                 if($userStr != "") $userStr.=",";
680                                 $userStr .= $user->getNumeric();
681                         }
682                         foreach($sorted_users['ov'] as $i => $user) {
683                                 if($userStr != "") $userStr.=",";
684                                 $userStr .= $user->getNumeric();
685                                 if($i == 0) $userStr .= ":ov";
686                         }
687                         foreach($sorted_users['o'] as $i => $user) {
688                                 if($userStr != "") $userStr.=",";
689                                 $userStr .= $user->getNumeric();
690                                 if($i == 0) $userStr .= ":o";
691                         }
692                         foreach($sorted_users['v'] as $i => $user) {
693                                 if($userStr != "") $userStr.=",";
694                                 $userStr .= $user->getNumeric();
695                                 if($i == 0) $userStr .= ":v";
696                         }
697                         $banString = "";
698                         //TODO: Build ban String
699                         $burstString = "";
700                         $modeString = $channel->getModes()->getModeString();
701                         if($modeString != "+") {
702                                 $burstString .= $modeString;
703                         }
704                         if($userStr != "") {
705                                 if($burstString != "") $burstString .= " ";
706                                 $burstString .= $userStr;
707                         }
708                         if($banString != "") {
709                                 if($burstString != "") $burstString .= " ";
710                                 $burstString .= ":%".$banString;
711                         }
712                         $this->send("B", $channel->getName(), $channel->getCreateTime(), $burstString);
713                 }
714                 $this->send("EB");
715         }
716         
717         /********************************************************************************************
718          *                                    LOCAL USER FUNCTIONS                                  *
719          ********************************************************************************************/
720         
721         public function addUser($nick, $ident, $host, $ip, $modes, $realname) {
722                 $user = P10_User::getUserByNick($nick);
723                 if($user != null) return ERR_NICK_IN_USE;
724                 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
725                 while(P10_User::getUserByNum($numeric)) {
726                         $this->last_local_numeric++;
727                         $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
728                 }
729                 $this->last_local_numeric++;
730                 $user = new P10_User($nick, $numeric, $this->server, time(), $ident, $host, $ip, $realname, $modes);
731                 if(($this->flags & self::FLAG_CONNECTED)) {
732                         $ip = Numerics::numericFromIP($user->getIP());
733                         $this->send("N", $nick, $user->getConnectTime(), $ident, $host, $user->getModes()->getModeString(), $ip, $numeric, $realname);
734                 }
735                 return $user;
736         }
737         
738         public function delUser($user, $reason) {
739                 if(!is_a($user, "P10_User")) return ERR_INVALID_USER;
740                 if($user->getServer() === $this->server) {
741                         //local user (QUIT)
742                         $user->quit($reason);
743                         if(($this->flags & self::FLAG_CONNECTED)) 
744                                 $this->send("Q", $user->getNumeric(), $reason);
745                 } else {
746                         //remote user (KILL)
747                         if(!($this->flags & self::FLAG_CONNECTED)) 
748                                 return ERR_NOT_CONNECTED;
749                         if($this->eventHandler)
750                                 $this->eventHandler->event_quit($user, "Killed (".$reason.")");
751                         $user->quit("Killed (".$reason.")");
752                         $name = ($this->getSetting('his_name') ? $this->getSetting('his_name') : $this->getSetting('name'));
753
754                         $this->send("D", $user->getNumeric(), $name, $reason);
755                 }
756         }
757         
758         public function join($user, $chanName) {
759                 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
760                         return ERR_INVALID_USER;
761                 if($chanName[0] != "#")
762                         return ERR_INVALID_CHANNAME;
763                 $channel = P10_Channel::getChannelByName($chanName);
764                 if($channel == null)
765                         $channel = new P10_Channel($chanName);
766                 $channel->joinUser($user);
767                 if(($this->flags & self::FLAG_CONNECTED))
768                         $this->send("J", $user->getNumeric(), $chanName, time(), 0);
769                 if($this->eventHandler)
770                         $this->eventHandler->event_join($user, $channel, false);
771         }
772         
773         public function part($user, $chanName, $reason) {
774                 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
775                         return ERR_INVALID_USER;
776                 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
777                         return ERR_INVALID_CHANNAME;
778                 if(is_a($chanName, "P10_Channel"))
779                         $channel = $chanName;
780                 else
781                         $channel = P10_Channel::getChannelByName($chanName);
782                 if($channel == null)
783                         $channel = new P10_Channel($chanName);
784                 if(!$user->isOnChannel($channel)) 
785                         return ERR_NOT_ON_CHANNEL;
786                 if($this->eventHandler)
787                         $this->eventHandler->event_part($user, $channel, $reason);
788                 $channel->partUser($user, $reason);
789                 if(($this->flags & self::FLAG_CONNECTED))
790                         $this->send("L", $user->getNumeric(), $chanName, $reason);
791         }
792         
793         public function kick($user, $target, $chanName, $reason) {
794                 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
795                         return ERR_INVALID_USER;
796                 if(!is_a($target, "P10_User"))
797                         return ERR_INVALID_USER;
798                 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
799                         return ERR_INVALID_CHANNAME;
800                 if(is_a($chanName, "P10_Channel"))
801                         $channel = $chanName;
802                 else
803                         $channel = P10_Channel::getChannelByName($chanName);
804                 if($channel == null)
805                         $channel = new P10_Channel($chanName);
806                 if(!$target->isOnChannel($channel)) 
807                         return ERR_NOT_ON_CHANNEL;
808                 if($this->eventHandler)
809                         $this->eventHandler->event_kick($user, $target, $channel, $reason);
810                 $channel->partUser($target, $reason);
811                 if(($this->flags & self::FLAG_CONNECTED))
812                         $this->send("K", $user->getNumeric(), $chanName, $target->getNumeric(), $reason);
813         }
814         
815         public function privmsg($user, $target, $message) {
816                 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
817                         return ERR_INVALID_USER;
818                 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
819                         return ERR_INVALID_USER;
820                 if(is_a($target, "P10_Channel"))
821                         $targetStr = $target->getName();
822                 else if(is_a($target, "P10_User"))
823                         $targetStr = $target->getNumeric();
824                 else if($target[0] == "#")
825                         $targetStr = $target;
826                 else
827                         $targetStr = P10_User::getUserByNick($target)->getNumeric();
828                 
829                 if($this->eventHandler) {
830                         if($targetStr[0] == "#") {
831                                 $channel = P10_Channel::getChannelByName($targetStr);
832                                 if($channel == null)
833                                         $channel = new P10_Channel($targetStr);
834                                 $this->eventHandler->event_chanmessage($user, $channel, $message);
835                         } else {
836                                 $targetUser = P10_User::getUserByNum($targetStr);
837                                 $this->eventHandler->event_privmessage($user, $targetUser, $message);
838                         }
839                 }
840                 if(($this->flags & self::FLAG_CONNECTED))
841                         $this->send("P", $user->getNumeric(), $targetStr, $message);
842         }
843         
844         public function notice($user, $target, $message) {
845                 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
846                         return ERR_INVALID_USER;
847                 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
848                         return ERR_INVALID_USER;
849                 if(is_a($target, "P10_Channel"))
850                         $targetStr = $target->getName();
851                 else if(is_a($target, "P10_User"))
852                         $targetStr = $target->getNumeric();
853                 else if($target[0] == "#")
854                         $targetStr = $target;
855                 else
856                         $targetStr = P10_User::getUserByNick($target)->getNumeric();
857                 
858                 if($this->eventHandler) {
859                         if($targetStr[0] == "#") {
860                                 $channel = P10_Channel::getChannelByName($targetStr);
861                                 if($channel == null)
862                                         $channel = new P10_Channel($targetStr);
863                                 $this->eventHandler->event_channotice($user, $channel, $message);
864                         } else {
865                                 $targetUser = P10_User::getUserByNum($targetStr);
866                                 $this->eventHandler->event_privnotice($user, $targetUser, $message);
867                         }
868                 }
869                 if(($this->flags & self::FLAG_CONNECTED))
870                         $this->send("O", $user->getNumeric(), $targetStr, $message);
871         }
872         
873         public function mode($user, $target, $modes, $force = false) {
874                 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
875                         return ERR_INVALID_USER;
876                 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
877                         return ERR_INVALID_USER;
878                 if(is_a($target, "P10_Channel"))
879                         $targetStr = $target->getName();
880                 else if(is_a($target, "P10_User"))
881                         $targetStr = $target->getNumeric();
882                 else if($target[0] == "#")
883                         $targetStr = $target;
884                 else
885                         $targetStr = P10_User::getUserByNick($target)->getNumeric();
886                 
887                 if($targetStr[0] == "#") {
888                         $channel = P10_Channel::getChannelByName($targetStr);
889                         if($channel == null)
890                                 $channel = new P10_Channel($targetStr);
891                         $modes = $channel->getModes()->setModes($modes);
892                         if(($this->flags & self::FLAG_CONNECTED))
893                                 $this->send(($force ? "OM" : "M"), $user->getNumeric(), $targetStr, $modes);
894                         if($this->eventHandler)
895                                 $this->eventHandler->event_chanmode(($force ? $this->server : $user), $channel, $modes);
896                 } else {
897                         $targetUser = P10_User::getUserByNum($targetStr);
898                         if($targetUser->getServer() === $this->server) {
899                                 //just do it :D
900                                 $modes = $targetUser->getModes()->setModes($modes);
901                                 if(($this->flags & self::FLAG_CONNECTED))
902                                         $this->send("M", $targetUser->getNumeric(), $targetUser->getNick(), $modes);
903                                 if($this->eventHandler)
904                                         $this->eventHandler->event_usermode($targetUser, $modes);
905                         } else {
906                                 //SVSMODE
907                                 if(($this->flags & self::FLAG_CONNECTED))
908                                         $this->send("SM", $user->getNumeric(), $targetUser->getNumeric(), $modes);
909                         }
910                 }
911         }
912         
913         
914         
915 }
916
917 ?>