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