2 /******************************* PHP-P10 v2 *****************************
3 * Copyright (C) 2011 Philipp Kreil (pk910) *
5 * This program is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 3 of the License, or *
8 * (at your option) any later version. *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program. If not, see <http://www.gnu.org/licenses/>. *
18 ************************************************************************
20 * Uplink/Uplink.class.php
22 * This file contains the basic P10 Protocol handler.
25 require_once("Client.class.php");
26 require_once("Numerics.class.php");
27 require_once("P10Formatter.class.php");
28 require_once("P10_Server.class.php");
29 require_once("P10_User.class.php");
30 require_once("P10_Channel.class.php");
31 require_once("P10_ModeSets.class.php");
32 require_once("EventHandler.interface.php");
33 require_once("IPAddr.class.php");
36 define("ERR_NICK_IN_USE", $e++);
37 define("ERR_INVALID_USER", $e++);
38 define("ERR_INVALID_CHANNAME", $e++);
39 define("ERR_NOT_CONNECTED", $e++);
40 define("ERR_NOT_ON_CHANNEL", $e++);
44 private $settings = array();
46 private $eventHandler = null;
47 private $last_local_numeric = 0;
49 const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (server is connected)
50 const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues
51 const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
52 const FLAG_BURST_PENDING = 0x0008; //we still have to burst
53 const FLAG_CONNECTED = 0x0010; //connected and synced (ready)
54 const FLAG_GOT_PASS = 0x0020; //got PASS from the remote Server
57 public function __construct() {
58 $this->client = new Client();
59 $this->setSetting("recv_timeout", 1000);
60 $this->setSetting("his_usermask", "user.NoMask");
63 public function initialize() {
65 trigger_error("Uplink already initialized.", E_USER_ERROR);
68 $self_numeric = $this->getSetting("numeric");
69 $self_name = $this->getSetting("name");
70 $self_description = $this->getSetting("description");
71 if(!$self_numeric || !$self_name) {
72 trigger_error("Server Settings missing.", E_USER_ERROR);
75 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
78 public function loop() {
79 if($this->server == null) {
80 trigger_error("Uplink not initialized.", E_USER_ERROR);
83 if(!$this->client->connected()) {
84 if(($this->flags & self::FLAG_P10SESSION)) {
85 //Server got disconnected
86 $this->server->disconnectServer($this->eventHandler, true);
87 $this->flags &= ~self::FLAG_P10SESSION;
89 $host = $this->getSetting("host");
90 $port = $this->getSetting("port");
91 if($host == null || $port == null) {
92 trigger_error("Uplink Settings missing.", E_USER_ERROR);
95 if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
98 $state = $this->client->connect($host, $port, $this->getSetting("bind"), $this->getSetting("ssl"), $this->getSetting("recv_timeout"));
100 usleep($this->getSetting("recv_timeout") / 1000);
101 $this->flags |= self::FLAG_NOT_CONNECTABLE;
105 $this->loginServer();
107 //try to receive new data from the Uplink
108 $lines = $this->client->recv();
109 if($lines == null) return;
110 foreach($lines as $line) {
111 $this->parseLine($line);
115 public function shutdown() {
116 if($this->client->connected()) {
117 if(($this->flags & self::FLAG_P10SESSION)) {
118 $this->send("SQ", "Shutdown requested.");
120 $this->client->disconnect();
124 public function setUplinkHost($host, $port, $ssl = false, $bind = null) {
125 $this->setSetting("host", $host);
126 $this->setSetting("port", $port);
127 $this->setSetting("ssl", $ssl);
128 $this->setSetting("bind", $bind);
131 public function setLoopTimeout($timeout) {
132 $this->setSetting("recv_timeout", $timeout);
135 public function setUplinkServer($numeric, $name, $password, $description) {
136 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
137 $this->setSetting("name", $name);
138 $this->setSetting("password", $password);
139 $this->setSetting("description", $description);
142 public function setValidateServer($name, $password) {
143 $this->setSetting("their_name", $name);
144 $this->setSetting("their_password", $password);
147 public function setHISOptions($servername, $serverdesc, $usermask) {
148 $this->setSetting("his_name", $servername);
149 $this->setSetting("his_desc", $serverdesc);
150 $this->setSetting("his_usermask", $usermask);
153 public function setEventHandler($event_handler) {
154 if(!is_a($event_handler, "EventHandler")) {
155 trigger_error((is_object($event_handler) ? get_class($event_handler) : gettype($event_handler))." is NOT a valid EventHandler.", E_USER_ERROR);
158 $this->eventHandler = $event_handler;
161 private function setSetting($setting, $value) {
162 $this->settings[$setting] = $value;
165 private function getSetting($setting) {
166 if(array_key_exists($setting, $this->settings)) {
167 return $this->settings[$setting];
173 private function loginServer() {
174 $password = $this->getSetting("password");
175 $this->send("PASS", $password);
176 $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
179 private function parseLine($line) {
180 $highExplode = explode(" :", $line, 2);
181 $tokens = explode(" ", $highExplode[0]);
182 if(count($highExplode) > 1)
183 $tokens[] = $highExplode[1];
184 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
185 if($cmdPos == 1) $from = $tokens[0];
187 $arguments = array_slice($tokens, $cmdPos + 1);
188 if(($this->flags & self::FLAG_P10SESSION) && $this->eventHandler) {
189 $this->eventHandler->event_preparse($from, strtoupper($tokens[$cmdPos]), $arguments);
191 switch(strtoupper($tokens[$cmdPos])) {
194 $this->recv_pass($from, $arguments);
197 $this->recv_server($from, $arguments);
200 $this->recv_error($from, $arguments);
204 $this->recv_server($from, $arguments);
207 $this->recv_ping($from, $arguments);
210 $this->recv_nick($from, $arguments);
213 $this->recv_end_of_burst($from, $arguments);
216 $this->recv_end_of_burst_ack($from, $arguments);
219 $this->recv_server_quit($from, $arguments);
222 $this->recv_quit($from, $arguments);
225 $this->recv_burst($from, $arguments);
229 $this->recv_join($from, $arguments);
232 $this->recv_part($from, $arguments);
235 $this->recv_kick($from, $arguments);
238 $this->recv_kill($from, $arguments);
241 $this->recv_privmsg($from, $arguments);
244 $this->recv_notice($from, $arguments);
247 $this->recv_whois($from, $arguments);
250 $this->recv_away($from, $arguments);
254 $this->recv_mode($from, $arguments);
257 $this->recv_account($from, $arguments);
260 $this->recv_fakehost($from, $arguments);
263 $this->recv_newfakehost($from, $arguments);
268 if($this->eventHandler)
269 $this->eventHandler->event_unknown_cmd($from, strtoupper($tokens[$cmdPos]), $arguments);
274 private function send($command) {
275 if(func_num_args() > 1) {
276 $args = array_slice(func_get_args(), 1);
277 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
279 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
281 $this->client->send($command);
284 /********************************************************************************************
285 * INCOMING COMMANDS *
286 ********************************************************************************************/
288 private function recv_pass($from, $args) {
289 $their_pass = $this->getSetting("their_password");
291 if($args[0] != $their_pass) {
293 $this->flags |= self::FLAG_SECURITY_QUIT;
294 $this->send("ERROR", "Incorrect password received.");
295 $this->client->disconnect();
298 $this->flags |= self::FLAG_GOT_PASS;
302 private function recv_error($from, $args) {
303 //we received an error - the socket is dead for us, now
304 //maybe we want to log this, later
305 $this->client->disconnect();
308 private function recv_server($from, $args) {
311 $their_name = $this->getSetting("their_name");
312 if($their_name && $args[0] != $their_name) {
313 $this->flags |= self::FLAG_SECURITY_QUIT;
314 $this->send("ERROR", "Invalid Server name");
315 $this->client->disconnect();
318 if($this->getSetting("their_password") && !($this->flags & self::FLAG_GOT_PASS)) {
319 $this->flags |= self::FLAG_SECURITY_QUIT;
320 $this->send("ERROR", "PASS missing.");
321 $this->client->disconnect();
324 $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
325 $this->server->addServer($new_server);
326 $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
327 if($this->eventHandler)
328 $this->eventHandler->event_newserver($new_server, !($this->flags & self::FLAG_CONNECTED));
330 //another server got a new slave server ^^
331 $server = P10_Server::getServerByNum($from);
332 if($server == null) {
333 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
336 $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
337 $server->addServer($new_server);
338 if($this->eventHandler)
339 $this->eventHandler->event_newserver($new_server, !($this->flags & self::FLAG_CONNECTED));
343 private function recv_ping($from, $args) {
344 $this->send("Z", $args[0]); //simply PONG
345 P10_Channel::recheckAllChannels();
348 private function recv_nick($from, $args) {
349 if(count($args) <= 2) {
351 $user = P10_User::getUserByNum($from);
353 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);
356 if($this->eventHandler)
357 $this->eventHandler->event_nick($user, $args[0]);
358 $user->setNick($args[0]);
361 $numeric = $args[count($args)-2];
363 $server = P10_Server::getServerByNum($from);
364 if($server == null) {
365 trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
368 if(substr($numeric,0,2) != $from) {
369 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);
371 $connect_time = $args[2];
374 $modes = implode(" ",array_slice($args, 5, count($args)-8));
375 $modes = new P10_UserModeSet($modes);
376 $ip = new IPAddr($args[count($args)-3]);
377 $realname = $args[count($args)-1];
378 $user = new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
379 if($this->eventHandler)
380 $this->eventHandler->event_connect($user, !($this->flags & self::FLAG_CONNECTED));
384 private function recv_end_of_burst($from, $args) {
385 if(($this->flags & self::FLAG_BURST_PENDING)) {
388 $this->flags &= ~self::FLAG_BURST_PENDING;
392 private function recv_end_of_burst_ack($from, $args) {
393 $this->flags |= self::FLAG_CONNECTED;
396 private function recv_server_quit($from, $args) {
397 $server = P10_Server::getServerByName($args[0]);
398 if($server == null) {
399 trigger_error("Server (".$args[0].") not found.", E_USER_ERROR);
402 $server->disconnectServer($this->eventHandler);
405 private function recv_quit($from, $args) {
406 $user = P10_User::getUserByNum($from);
408 trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
411 if($this->eventHandler)
412 $this->eventHandler->event_quit($user, $args[0]);
413 $user->quit($args[0]);
416 private function recv_burst($from, $args) {
418 $create_time = $args[1];
419 if(count($args) == 2) {
420 //we've got an empty channel without any modes set??? dead channel!
423 $channel = P10_Channel::getChannelByName($name);
425 $channel = new P10_Channel($name);
426 $channel->setCreateTime($create_time);
427 $modes = $channel->getModes();
428 $userstr = $args[count($args)-1];
429 if($userstr[0] == "%") {
431 $banlist = explode(" ", substr($userstr, 1));
432 foreach($banlist as $ban) {
435 $userstr = $args[count($args)-2];
437 if($userstr[0] == "+") { //MODE String
440 $users = explode(",",$userstr);
441 $isop = false; $isvoice = false;
442 foreach($users as $user) {
443 if($user == "") continue;
444 $uexp = explode(":", $user);
445 if(strlen($uexp[0]) != 5) {
446 trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_WARNING);
449 if(count($uexp) > 1) {
452 for($i = 0; $i < strlen($uexp[1]); $i++) {
453 if($uexp[1][0] == "@") $isop = true;
454 if($uexp[1][0] == "+") $isvoice = true;
457 $user = P10_User::getUserByNum($uexp[0]);
459 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
462 $channel->burstUser($user, $isop, $isvoice);
463 if($this->eventHandler)
464 $this->eventHandler->event_join($user, $channel, true);
466 $modestr = array_slice($args, 2);
467 if($modestr[0] == "+")
468 $modes->parseModes(implode(" ", $modestr));
471 private function recv_join($from, $args) {
472 $user = P10_User::getUserByNum($from);
474 trigger_error("Server tries to join an user that does not exist or was not found on recv_join.", E_USER_ERROR);
477 $channel = P10_Channel::getChannelByName($args[0]);
479 $channel = new P10_Channel($args[0]);
480 $channel->joinUser($user);
481 if($this->eventHandler)
482 $this->eventHandler->event_join($user, $channel, false);
485 private function recv_part($from, $args) {
486 $user = P10_User::getUserByNum($from);
488 trigger_error("Server tries to part an user that does not exist or was not found on recv_join.", E_USER_ERROR);
491 $channel = P10_Channel::getChannelByName($args[0]);
493 $channel = new P10_Channel($args[0]);
494 if($this->eventHandler)
495 $this->eventHandler->event_part($user, $channel, $args[1]);
496 $channel->partUser($user);
499 private function recv_kick($from, $args) {
500 $user = P10_User::getUserByNum($from);
502 trigger_error("An unknown user tries to kick another user on recv_kick.", E_USER_ERROR);
505 $channel = P10_Channel::getChannelByName($args[0]);
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);
513 if($this->eventHandler)
514 $this->eventHandler->event_kick($user, $target, $channel, $args[1]);
515 $channel->partUser($user);
518 private function recv_kill($from, $args) {
519 $user = P10_User::getUserByNum($args[0]);
521 trigger_error("Server tries to kill an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
524 if($this->eventHandler)
525 $this->eventHandler->event_quit($user, "Killed (".$args[1].")");
526 $user->quit($args[1]);
529 private function recv_privmsg($from, $args) {
530 $user = P10_User::getUserByNum($from);
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_WARNING);
535 if($this->eventHandler) {
536 if($args[0][0] == "#") {
537 $channel = P10_Channel::getChannelByName($args[0]);
539 $channel = new P10_Channel($args[0]);
540 if(strlen($args[1]) > 0 && $args[1][0] == "\001") {
542 $args[1] = substr($args[1],1);
543 if($args[1][strlen($args[1])-1] == "\001")
544 $args[1] = substr($args[1],0,-1);
545 $ctcpexp = explode(" ",$args[1],2);
546 $this->eventHandler->event_chanctcp($user, $channel, strtoupper($ctcpexp[0]), (count($ctcpexp) > 1 ? $ctcpexp[1] : null));
548 $this->eventHandler->event_chanmessage($user, $channel, $args[1]);
549 } else if($args[0][0] == "$") {
551 $this->eventHandler->event_privmessage($user, NULL, $args[1]);
553 $targetUser = P10_User::getUserByNum($args[0]);
554 if($targetUser == null) {
555 trigger_error("Server tries to send a privmsg to an user that does not exist or was not found on recv_privmsg.", E_USER_WARNING);
558 if(strlen($args[1]) > 0 && $args[1][0] == "\001") {
560 $args[1] = substr($args[1],1);
561 if($args[1][strlen($args[1])-1] == "\001")
562 $args[1] = substr($args[1],0,-1);
563 $ctcpexp = explode(" ",$args[1],2);
564 $this->eventHandler->event_privctcp($user, $targetUser, strtoupper($ctcpexp[0]), (count($ctcpexp) > 1 ? $ctcpexp[1] : null));
566 $this->eventHandler->event_privmessage($user, $targetUser, $args[1]);
571 private function recv_notice($from, $args) {
572 $user = P10_User::getUserByNum($from);
574 trigger_error("Server tries to send a notice from an user that does not exist or was not found on recv_notice.", E_USER_WARNING);
577 if($this->eventHandler) {
578 if($args[0][0] == "#") {
579 $channel = P10_Channel::getChannelByName($args[0]);
581 $channel = new P10_Channel($args[0]);
582 if(strlen($args[1]) > 0 && $args[1][0] == "\001") {
584 $args[1] = substr($args[1],1);
585 if($args[1][strlen($args[1])-1] == "\001")
586 $args[1] = substr($args[1],0,-1);
587 $ctcpexp = explode(" ",$args[1],2);
588 $this->eventHandler->event_chanctcpreply($user, $channel, strtoupper($ctcpexp[0]), (count($ctcpexp) > 1 ? $ctcpexp[1] : null));
590 $this->eventHandler->event_channotice($user, $channel, $args[1]);
591 } else if($args[0][0] == "$") {
593 $this->eventHandler->event_privnotice($user, NULL, $args[1]);
595 $targetUser = P10_User::getUserByNum($args[0]);
596 if($targetUser == null) {
597 trigger_error("Server tries to send a notice to an user that does not exist or was not found on recv_notice.", E_USER_WARNING);
600 if(strlen($args[1]) > 0 && $args[1][0] == "\001") {
602 $args[1] = substr($args[1],1);
603 if($args[1][strlen($args[1])-1] == "\001")
604 $args[1] = substr($args[1],0,-1);
605 $ctcpexp = explode(" ",$args[1],2);
606 $this->eventHandler->event_privctcpreply($user, $targetUser, strtoupper($ctcpexp[0]), (count($ctcpexp) > 1 ? $ctcpexp[1] : null));
608 $this->eventHandler->event_privnotice($user, $targetUser, $args[1]);
613 private function recv_whois($from, $args) {
614 /* [get] ACAAF W AX :NetworkServ */
615 $fromUser = P10_User::getUserByNum($from);
616 if($fromUser == null) {
617 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);
620 $users=explode(",",$args[1]);
621 foreach($users as $nick) {
622 $user = P10_User::getUserByNick($nick);
624 $this->send("401", $from, $nick);
627 $nick = $user->getNick();
628 $ident = $user->getIdent();
629 $hostmask = $user->getHost();
630 $modes = $user->getModes();
631 if($modes->hasMode('x')) {
632 if(($fakehost = $modes->hasMode('f'))) {
633 $hostmask = $fakehost;
634 } elseif(($account = $modes->hasMode('r'))) {
635 $hostmask = $account.".".$this->getSetting("his_usermask");
638 $realname = $user->getRealname();
639 $this->send("311", $from , $nick, $ident, $hostmask, $realname);
640 if(((!$modes->hasMode('n') && !$modes->hasMode('k')) || $from == $user->getNumeric()) && count($user->getChannels()) != 0) {
642 foreach($user->getChannels() as $channel) {
643 $cmodes = $channel->getModes();
644 $privs = $channel->getUserPrivs($user);
645 if($cmodes->hasMode("s") && !$fromUser->isOnChannel($channel) && $from != $user->getNumeric()) continue;
646 if($cmodes->hasMode("u") && ($privs & (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE)) == 0 && $from != $user->getNumeric()) continue;
647 $chanstr = ($channels == "" ? "" : " ");
649 if(($privs & P10_Channel::USERPRIV_OPED)) {
651 } else if(($privs & P10_Channel::USERPRIV_VOICE)) {
654 $chanstr .= $prefix.$channel->getName();
655 if(strlen($channels) + strlen($chanstr) > 450) {
656 $this->send("319", $from, $nick, $channels);
657 $channels = $prefix.$channel->getName();
660 if($channels != "") {
661 $this->send("319", $from, $nick, $channels);
664 if($fromUser->getModes()->hasMode("o") || $from == $user->getNumeric() || !$this->getSetting("his_name")) {
665 $this->send("312", $from, $nick, $user->getServer()->getName(), $user->getServer()->getDescription());
667 $this->send("312", $from, $nick, $this->getSetting("his_name"), $this->getSetting("his_desc"));
669 if($modes->hasMode("o") && (!$modes->hasMode("H") || $fromUser->getModes()->hasMode("o"))) {
670 if($modes->hasMode("S")) {
671 if($modes->hasMode("D"))
672 $this->send("313", $from, $nick, "is a Network Service");
674 $this->send("313", $from, $nick, "is an illegal Network Service - HACKER!");
676 $this->send("313", $from, $nick, "is an IRC Operator");
678 if(($auth = $modes->hasMode("r"))) {
679 $this->send("330", $from, $nick, $auth);
682 $this->send("318", $from, $args[1]);
685 private function recv_away($from, $args) {
686 $user = P10_User::getUserByNum($from);
688 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);
691 if(count($args) > 0) {
692 $user->setAway($args[0]);
693 if($this->eventHandler)
694 $this->eventHandler->event_away($user, $args[0]);
696 $user->setAway(null);
697 if($this->eventHandler)
698 $this->eventHandler->event_away($user, null);
702 private function recv_mode($from, $args) {
703 $user = P10_User::getUserByNum($from);
704 if($user == null && strlen($from) != 2) {
705 trigger_error("Server tries to send a modechange from an user that does not exist or was not found on recv_mode.", E_USER_ERROR);
708 $modes = implode(" ",array_slice($args,1));
709 if($args[0][0] == "#") {
710 $channel = P10_Channel::getChannelByName($args[0]);
712 $channel = new P10_Channel($args[0]);
713 $channel->getModes()->setModes($modes);
714 if($this->eventHandler && strlen($from) != 2)
715 $this->eventHandler->event_chanmode($user, $channel, $modes);
717 $targetUser = P10_User::getUserByNick($args[0]);
718 if($targetUser == null) {
719 trigger_error("Server tries to send a mode to an user that does not exist or was not found on recv_mode.", E_USER_ERROR);
722 $targetUser->getModes()->setModes($modes);
723 if($this->eventHandler)
724 $this->eventHandler->event_usermode($targetUser, $modes);
728 private function recv_account($from, $args) {
729 $user = P10_User::getUserByNum($args[0]);
731 trigger_error("Server tries to send an auth announce from an user that does not exist or was not found on recv_account.", E_USER_ERROR);
735 $user->getModes()->setModes("+r ".$auth);
736 if($this->eventHandler)
737 $this->eventHandler->event_usermode($user, "+r ".$auth);
740 private function recv_fakehost($from, $args) {
741 $user = P10_User::getUserByNum($args[0]);
743 trigger_error("Server tries to send a fakehost change from an user that does not exist or was not found on recv_fakehost.", E_USER_ERROR);
746 $fakehost = $args[1];
747 $user->getModes()->setModes("+f ".$fakehost);
748 if($this->eventHandler)
749 $this->eventHandler->event_usermode($user, "+f ".$fakehost);
752 private function recv_newfakehost($from, $args) {
753 $user = P10_User::getUserByNum($args[0]);
755 trigger_error("Server tries to send a fakehost change from an user that does not exist or was not found on recv_fakehost.", E_USER_ERROR);
758 $fakeident = $args[1];
759 $fakehost = $args[2];
760 $user->setIdent($fakeident);
761 $user->getModes()->setModes("+f ".$fakehost);
762 if($this->eventHandler)
763 $this->eventHandler->event_usermode($user, "+f ".$fakehost);
766 /********************************************************************************************
768 ********************************************************************************************/
770 private function burst() {
771 foreach($this->server->getUsers() as $user) {
772 $nick = $user->getNick();
773 $connect_time = $user->getConnectTime();
774 $ident = $user->getIdent();
775 $host = $user->getHost();
776 $modes = $user->getModes()->getModeString();
777 $ip = $user->getIP()->getNumeric();
778 $numeric = $user->getNumeric();
779 $realname = $user->getRealname();
780 $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);
782 foreach(P10_Channel::getChannels() as $channel) {
783 $sorted_users = array('ov' => array(), 'o' => array(), 'v' => array(), '-' => array());
784 $local_users = false;
785 foreach($channel->getUsers() as $user) {
786 if(substr($user->getNumeric(), 0, 2) != $this->server->getNumeric()) continue; //skip users that are not on the local server
787 $privs = $channel->getUserPrivs($user);
789 if(($privs & P10_Channel::USERPRIV_OPED)) $strPrivs .= "o";
790 if(($privs & P10_Channel::USERPRIV_VOICE)) $strPrivs .= "v";
791 if($strPrivs == "") $strPrivs = "-";
793 $sorted_users[$strPrivs][] = $user;
795 if(!$local_users) continue;
797 foreach($sorted_users['-'] as $user) {
798 if($userStr != "") $userStr.=",";
799 $userStr .= $user->getNumeric();
801 foreach($sorted_users['ov'] as $i => $user) {
802 if($userStr != "") $userStr.=",";
803 $userStr .= $user->getNumeric();
804 if($i == 0) $userStr .= ":ov";
806 foreach($sorted_users['o'] as $i => $user) {
807 if($userStr != "") $userStr.=",";
808 $userStr .= $user->getNumeric();
809 if($i == 0) $userStr .= ":o";
811 foreach($sorted_users['v'] as $i => $user) {
812 if($userStr != "") $userStr.=",";
813 $userStr .= $user->getNumeric();
814 if($i == 0) $userStr .= ":v";
817 //TODO: Build ban String
819 $modeString = $channel->getModes()->getModeString();
820 if($modeString != "+") {
821 $burstString .= $modeString;
824 if($burstString != "") $burstString .= " ";
825 $burstString .= $userStr;
827 if($banString != "") {
828 if($burstString != "") $burstString .= " ";
829 $burstString .= ":%".$banString;
831 $this->send("B", $channel->getName(), $channel->getCreateTime(), $burstString);
836 /********************************************************************************************
837 * LOCAL USER FUNCTIONS *
838 ********************************************************************************************/
840 public function addUser($nick, $ident, $host, $ip, $modes, $realname) {
841 $user = P10_User::getUserByNick($nick);
842 if($user != null) return ERR_NICK_IN_USE;
843 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
844 while(P10_User::getUserByNum($numeric)) {
845 $this->last_local_numeric++;
846 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
848 $this->last_local_numeric++;
849 $modes = new P10_UserModeSet($modes);
850 $ip = new IPAddr($ip);
851 $user = new P10_User($nick, $numeric, $this->server, time(), $ident, $host, $ip, $realname, $modes);
852 if(($this->flags & self::FLAG_CONNECTED)) {
853 $ip = $user->getIP()->getNumeric();
854 $this->send("N", $nick, $user->getConnectTime(), $ident, $host, $user->getModes()->getModeString(), $ip, $numeric, $realname);
859 public function delUser($user, $reason) {
860 if(!is_a($user, "P10_User")) return ERR_INVALID_USER;
861 if($user->getServer() === $this->server) {
863 $user->quit($reason);
864 if(($this->flags & self::FLAG_CONNECTED))
865 $this->send("Q", $user->getNumeric(), $reason);
868 if(!($this->flags & self::FLAG_CONNECTED))
869 return ERR_NOT_CONNECTED;
870 if($this->eventHandler)
871 $this->eventHandler->event_quit($user, "Killed (".$reason.")");
872 $user->quit("Killed (".$reason.")");
873 $name = ($this->getSetting('his_name') ? $this->getSetting('his_name') : $this->getSetting('name'));
875 $this->send("D", $user->getNumeric(), $name, $reason);
879 public function join($user, $chanName, $privs = 0) {
880 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
881 return ERR_INVALID_USER;
882 if($chanName[0] != "#")
883 return ERR_INVALID_CHANNAME;
884 $channel = P10_Channel::getChannelByName($chanName);
886 $channel = new P10_Channel($chanName);
887 $channel->joinUser($user);
888 if(($this->flags & self::FLAG_CONNECTED))
889 $this->send("J", $user->getNumeric(), $chanName, time(), 0);
891 $channel->setUserPrivs($user, $privs);
892 if(($this->flags & self::FLAG_CONNECTED)) {
893 $modestr = "+".(($privs & P10_Channel::USERPRIV_OPED) ? "o" : "").(($privs & P10_Channel::USERPRIV_VOICE) ? "v" : "");
894 $modestr .= (($privs & P10_Channel::USERPRIV_OPED) ? " ".$user->getNumeric() : "");
895 $modestr .= (($privs & P10_Channel::USERPRIV_VOICE) ? " ".$user->getNumeric() : "");
896 $this->send("OM", $user->getNumeric(), $chanName, $modestr);
899 if($this->eventHandler)
900 $this->eventHandler->event_join($user, $channel, false);
903 public function part($user, $chanName, $reason) {
904 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
905 return ERR_INVALID_USER;
906 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
907 return ERR_INVALID_CHANNAME;
908 if(is_a($chanName, "P10_Channel"))
909 $channel = $chanName;
911 $channel = P10_Channel::getChannelByName($chanName);
913 $channel = new P10_Channel($chanName);
914 if(!$user->isOnChannel($channel))
915 return ERR_NOT_ON_CHANNEL;
916 if($this->eventHandler)
917 $this->eventHandler->event_part($user, $channel, $reason);
918 $channel->partUser($user, $reason);
919 if(($this->flags & self::FLAG_CONNECTED))
920 $this->send("L", $user->getNumeric(), $chanName, $reason);
923 public function kick($user, $target, $chanName, $reason) {
924 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
925 return ERR_INVALID_USER;
926 if(!is_a($target, "P10_User"))
927 return ERR_INVALID_USER;
928 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
929 return ERR_INVALID_CHANNAME;
930 if(is_a($chanName, "P10_Channel"))
931 $channel = $chanName;
933 $channel = P10_Channel::getChannelByName($chanName);
935 $channel = new P10_Channel($chanName);
936 if(!$target->isOnChannel($channel))
937 return ERR_NOT_ON_CHANNEL;
938 if($this->eventHandler)
939 $this->eventHandler->event_kick($user, $target, $channel, $reason);
940 $channel->partUser($target, $reason);
941 if(($this->flags & self::FLAG_CONNECTED))
942 $this->send("K", $user->getNumeric(), $chanName, $target->getNumeric(), $reason);
945 public function privmsg($user, $target, $message) {
946 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
947 return ERR_INVALID_USER;
948 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
949 return ERR_INVALID_USER;
950 if(is_a($target, "P10_Channel"))
951 $targetStr = $target->getName();
952 else if(is_a($target, "P10_User"))
953 $targetStr = $target->getNumeric();
954 else if($target[0] == "#")
955 $targetStr = $target;
957 $targetStr = P10_User::getUserByNick($target)->getNumeric();
959 if($this->eventHandler) {
960 if($targetStr[0] == "#") {
961 $channel = P10_Channel::getChannelByName($targetStr);
963 $channel = new P10_Channel($targetStr);
964 $this->eventHandler->event_chanmessage($user, $channel, $message);
966 $targetUser = P10_User::getUserByNum($targetStr);
967 $this->eventHandler->event_privmessage($user, $targetUser, $message);
970 if(($this->flags & self::FLAG_CONNECTED))
971 $this->send("P", $user->getNumeric(), $targetStr, $message);
974 public function notice($user, $target, $message) {
975 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
976 return ERR_INVALID_USER;
977 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
978 return ERR_INVALID_USER;
979 if(is_a($target, "P10_Channel"))
980 $targetStr = $target->getName();
981 else if(is_a($target, "P10_User"))
982 $targetStr = $target->getNumeric();
983 else if($target[0] == "#")
984 $targetStr = $target;
986 $targetStr = P10_User::getUserByNick($target)->getNumeric();
988 if($this->eventHandler) {
989 if($targetStr[0] == "#") {
990 $channel = P10_Channel::getChannelByName($targetStr);
992 $channel = new P10_Channel($targetStr);
993 $this->eventHandler->event_channotice($user, $channel, $message);
995 $targetUser = P10_User::getUserByNum($targetStr);
996 $this->eventHandler->event_privnotice($user, $targetUser, $message);
999 if(($this->flags & self::FLAG_CONNECTED))
1000 $this->send("O", $user->getNumeric(), $targetStr, $message);
1003 public function mode($user, $target, $modes, $force = false) {
1004 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
1005 return ERR_INVALID_USER;
1006 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
1007 return ERR_INVALID_USER;
1008 if(is_a($target, "P10_Channel"))
1009 $targetStr = $target->getName();
1010 else if(is_a($target, "P10_User"))
1011 $targetStr = $target->getNumeric();
1012 else if($target[0] == "#")
1013 $targetStr = $target;
1015 $targetStr = P10_User::getUserByNick($target)->getNumeric();
1017 if($targetStr[0] == "#") {
1018 $channel = P10_Channel::getChannelByName($targetStr);
1019 if($channel == null)
1020 $channel = new P10_Channel($targetStr);
1021 $modes = $channel->getModes()->setModes($modes, true);
1022 if(($this->flags & self::FLAG_CONNECTED))
1023 $this->send(($force ? "OM" : "M"), $user->getNumeric(), $targetStr, $modes);
1024 if($this->eventHandler)
1025 $this->eventHandler->event_chanmode(($force ? $this->server : $user), $channel, $modes);
1027 $targetUser = P10_User::getUserByNum($targetStr);
1028 if($targetUser->getServer() === $this->server) {
1030 $modes = $targetUser->getModes()->setModes($modes, true);
1031 if(($this->flags & self::FLAG_CONNECTED))
1032 $this->send("M", $targetUser->getNumeric(), $targetUser->getNick(), $modes);
1033 if($this->eventHandler)
1034 $this->eventHandler->event_usermode($targetUser, $modes);
1037 if(($this->flags & self::FLAG_CONNECTED))
1038 $this->send("SM", $user->getNumeric(), $targetUser->getNumeric(), $modes);
1043 public function ctcp($user, $target, $command, $text) {
1044 return $this->privmsg($user, $target, "\001".strtoupper($command)." ".$text."\001");
1047 public function ctcp_reply($user, $target, $command, $text) {
1048 return $this->notice($user, $target, "\001".strtoupper($command)." ".$text."\001");