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);
259 if($this->eventHandler)
260 $this->eventHandler->event_unknown_cmd($from, strtoupper($tokens[$cmdPos]), $arguments);
265 private function send($command) {
266 if(func_num_args() > 1) {
267 $args = array_slice(func_get_args(), 1);
268 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
270 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
272 $this->client->send($command);
275 /********************************************************************************************
276 * INCOMING COMMANDS *
277 ********************************************************************************************/
279 private function recv_pass($from, $args) {
280 $their_pass = $this->getSetting("their_password");
282 if($args[0] != $their_pass) {
284 $this->flags |= self::FLAG_SECURITY_QUIT;
285 $this->send("ERROR", "Incorrect password received.");
286 $this->client->disconnect();
289 $this->flags |= self::FLAG_GOT_PASS;
293 private function recv_error($from, $args) {
294 //we received an error - the socket is dead for us, now
295 //maybe we want to log this, later
296 $this->client->disconnect();
299 private function recv_server($from, $args) {
302 $their_name = $this->getSetting("their_name");
303 if($their_name && $args[0] != $their_name) {
304 $this->flags |= self::FLAG_SECURITY_QUIT;
305 $this->send("ERROR", "Invalid Server name");
306 $this->client->disconnect();
309 if($this->getSetting("their_password") && !($this->flags & self::FLAG_GOT_PASS)) {
310 $this->flags |= self::FLAG_SECURITY_QUIT;
311 $this->send("ERROR", "PASS missing.");
312 $this->client->disconnect();
315 $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
316 $this->server->addServer($new_server);
317 $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
318 if($this->eventHandler)
319 $this->eventHandler->event_newserver($new_server, !($this->flags & self::FLAG_CONNECTED));
321 //another server got a new slave server ^^
322 $server = P10_Server::getServerByNum($from);
323 if($server == null) {
324 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
327 $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
328 $server->addServer($new_server);
329 if($this->eventHandler)
330 $this->eventHandler->event_newserver($new_server, !($this->flags & self::FLAG_CONNECTED));
334 private function recv_ping($from, $args) {
335 $this->send("Z", $args[0]); //simply PONG
336 P10_Channel::recheckAllChannels();
339 private function recv_nick($from, $args) {
340 if(count($args) <= 2) {
342 $user = P10_User::getUserByNum($from);
344 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);
347 if($this->eventHandler)
348 $this->eventHandler->event_nick($user, $args[0]);
349 $user->setNick($args[0]);
352 $numeric = $args[count($args)-2];
354 $server = P10_Server::getServerByNum($from);
355 if($server == null) {
356 trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
359 if(substr($numeric,0,2) != $from) {
360 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);
362 $connect_time = $args[2];
365 $modes = implode(" ",array_slice($args, 5, count($args)-8));
366 $modes = new P10_UserModeSet($modes);
367 $ip = new IPAddr($args[count($args)-3]);
368 $realname = $args[count($args)-1];
369 $user = new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
370 if($this->eventHandler)
371 $this->eventHandler->event_connect($user, !($this->flags & self::FLAG_CONNECTED));
375 private function recv_end_of_burst($from, $args) {
376 if(($this->flags & self::FLAG_BURST_PENDING)) {
379 $this->flags &= ~self::FLAG_BURST_PENDING;
383 private function recv_end_of_burst_ack($from, $args) {
384 $this->flags |= self::FLAG_CONNECTED;
387 private function recv_server_quit($from, $args) {
388 $server = P10_Server::getServerByName($args[0]);
389 if($server == null) {
390 trigger_error("Server (".$args[0].") not found.", E_USER_ERROR);
393 $server->disconnectServer($this->eventHandler);
396 private function recv_quit($from, $args) {
397 $user = P10_User::getUserByNum($from);
399 trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
402 if($this->eventHandler)
403 $this->eventHandler->event_quit($user, $args[0]);
404 $user->quit($args[0]);
407 private function recv_burst($from, $args) {
409 $create_time = $args[1];
410 if(count($args) == 2) {
411 //we've got an empty channel without any modes set??? dead channel!
414 $channel = P10_Channel::getChannelByName($name);
416 $channel = new P10_Channel($name);
417 $channel->setCreateTime($create_time);
418 $modes = $channel->getModes();
419 $userstr = $args[count($args)-1];
420 if($userstr[0] == "%") {
422 $banlist = explode(" ", substr($userstr, 1));
423 foreach($banlist as $ban) {
426 $userstr = $args[count($args)-2];
428 if($userstr[0] == "+") { //MODE String
431 $users = explode(",",$userstr);
432 $isop = false; $isvoice = false;
433 foreach($users as $user) {
434 if($user == "") continue;
435 $uexp = explode(":", $user);
436 if(strlen($uexp[0]) != 5) {
437 trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_WARNING);
440 if(count($uexp) > 1) {
443 for($i = 0; $i < strlen($uexp[1]); $i++) {
444 if($uexp[1][0] == "@") $isop = true;
445 if($uexp[1][0] == "+") $isvoice = true;
448 $user = P10_User::getUserByNum($uexp[0]);
450 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
453 $channel->burstUser($user, $isop, $isvoice);
454 if($this->eventHandler)
455 $this->eventHandler->event_join($user, $channel, true);
457 $modestr = array_slice($args, 2);
458 if($modestr[0] == "+")
459 $modes->parseModes(implode(" ", $modestr));
462 private function recv_join($from, $args) {
463 $user = P10_User::getUserByNum($from);
465 trigger_error("Server tries to join an user that does not exist or was not found on recv_join.", E_USER_ERROR);
468 $channel = P10_Channel::getChannelByName($args[0]);
470 $channel = new P10_Channel($args[0]);
471 $channel->joinUser($user);
472 if($this->eventHandler)
473 $this->eventHandler->event_join($user, $channel, false);
476 private function recv_part($from, $args) {
477 $user = P10_User::getUserByNum($from);
479 trigger_error("Server tries to part an user that does not exist or was not found on recv_join.", E_USER_ERROR);
482 $channel = P10_Channel::getChannelByName($args[0]);
484 $channel = new P10_Channel($args[0]);
485 if($this->eventHandler)
486 $this->eventHandler->event_part($user, $channel, $args[1]);
487 $channel->partUser($user);
490 private function recv_kick($from, $args) {
491 $user = P10_User::getUserByNum($from);
493 trigger_error("An unknown user tries to kick another user on recv_kick.", E_USER_ERROR);
496 $channel = P10_Channel::getChannelByName($args[0]);
498 $channel = new P10_Channel($args[0]);
499 $target = P10_User::getUserByNum($args[1]);
500 if($target == null) {
501 trigger_error("Someone tries to kick an user that does not exist or was not found on recv_kick.", E_USER_ERROR);
504 if($this->eventHandler)
505 $this->eventHandler->event_kick($user, $target, $channel, $args[1]);
506 $channel->partUser($user);
509 private function recv_kill($from, $args) {
510 $user = P10_User::getUserByNum($args[0]);
512 trigger_error("Server tries to kill an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
515 if($this->eventHandler)
516 $this->eventHandler->event_quit($user, "Killed (".$args[1].")");
517 $user->quit($args[1]);
520 private function recv_privmsg($from, $args) {
521 $user = P10_User::getUserByNum($from);
523 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);
526 if($this->eventHandler) {
527 if($args[0][0] == "#") {
528 $channel = P10_Channel::getChannelByName($args[0]);
530 $channel = new P10_Channel($args[0]);
531 if(strlen($args[1]) > 0 && $args[1][0] == "\001") {
533 $args[1] = substr($args[1],1);
534 if($args[1][strlen($args[1])-1] == "\001")
535 $args[1] = substr($args[1],0,-1);
536 $ctcpexp = explode(" ",$args[1],2);
537 $this->eventHandler->event_chanctcp($user, $channel, strtoupper($ctcpexp[0]), (count($ctcpexp) > 1 ? $ctcpexp[1] : null));
539 $this->eventHandler->event_chanmessage($user, $channel, $args[1]);
540 } else if($args[0][0] == "$") {
542 $this->eventHandler->event_privmessage($user, NULL, $args[1]);
544 $targetUser = P10_User::getUserByNum($args[0]);
545 if($targetUser == null) {
546 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);
549 if(strlen($args[1]) > 0 && $args[1][0] == "\001") {
551 $args[1] = substr($args[1],1);
552 if($args[1][strlen($args[1])-1] == "\001")
553 $args[1] = substr($args[1],0,-1);
554 $ctcpexp = explode(" ",$args[1],2);
555 $this->eventHandler->event_privctcp($user, $targetUser, strtoupper($ctcpexp[0]), (count($ctcpexp) > 1 ? $ctcpexp[1] : null));
557 $this->eventHandler->event_privmessage($user, $targetUser, $args[1]);
562 private function recv_notice($from, $args) {
563 $user = P10_User::getUserByNum($from);
565 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);
568 if($this->eventHandler) {
569 if($args[0][0] == "#") {
570 $channel = P10_Channel::getChannelByName($args[0]);
572 $channel = new P10_Channel($args[0]);
573 if(strlen($args[1]) > 0 && $args[1][0] == "\001") {
575 $args[1] = substr($args[1],1);
576 if($args[1][strlen($args[1])-1] == "\001")
577 $args[1] = substr($args[1],0,-1);
578 $ctcpexp = explode(" ",$args[1],2);
579 $this->eventHandler->event_chanctcpreply($user, $channel, strtoupper($ctcpexp[0]), (count($ctcpexp) > 1 ? $ctcpexp[1] : null));
581 $this->eventHandler->event_channotice($user, $channel, $args[1]);
582 } else if($args[0][0] == "$") {
584 $this->eventHandler->event_privnotice($user, NULL, $args[1]);
586 $targetUser = P10_User::getUserByNum($args[0]);
587 if($targetUser == null) {
588 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);
591 if(strlen($args[1]) > 0 && $args[1][0] == "\001") {
593 $args[1] = substr($args[1],1);
594 if($args[1][strlen($args[1])-1] == "\001")
595 $args[1] = substr($args[1],0,-1);
596 $ctcpexp = explode(" ",$args[1],2);
597 $this->eventHandler->event_privctcpreply($user, $targetUser, strtoupper($ctcpexp[0]), (count($ctcpexp) > 1 ? $ctcpexp[1] : null));
599 $this->eventHandler->event_privnotice($user, $targetUser, $args[1]);
604 private function recv_whois($from, $args) {
605 /* [get] ACAAF W AX :NetworkServ */
606 $fromUser = P10_User::getUserByNum($from);
607 if($fromUser == null) {
608 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);
611 $users=explode(",",$args[1]);
612 foreach($users as $nick) {
613 $user = P10_User::getUserByNick($nick);
615 $this->send("401", $from, $nick);
618 $nick = $user->getNick();
619 $ident = $user->getIdent();
620 $hostmask = $user->getHost();
621 $modes = $user->getModes();
622 if($modes->hasMode('x')) {
623 if(($fakehost = $modes->hasMode('f'))) {
624 $hostmask = $fakehost;
625 } elseif(($account = $modes->hasMode('r'))) {
626 $hostmask = $account.".".$this->getSetting("his_usermask");
629 $realname = $user->getRealname();
630 $this->send("311", $from , $nick, $ident, $hostmask, $realname);
631 if(((!$modes->hasMode('n') && !$modes->hasMode('k')) || $from == $user->getNumeric()) && count($user->getChannels()) != 0) {
633 foreach($user->getChannels() as $channel) {
634 $cmodes = $channel->getModes();
635 $privs = $channel->getUserPrivs($user);
636 if($cmodes->hasMode("s") && !$fromUser->isOnChannel($channel) && $from != $user->getNumeric()) continue;
637 if($cmodes->hasMode("u") && ($privs & (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE)) == 0 && $from != $user->getNumeric()) continue;
638 $chanstr = ($channels == "" ? "" : " ");
640 if(($privs & P10_Channel::USERPRIV_OPED)) {
642 } else if(($privs & P10_Channel::USERPRIV_VOICE)) {
645 $chanstr .= $prefix.$channel->getName();
646 if(strlen($channels) + strlen($chanstr) > 450) {
647 $this->send("319", $from, $nick, $channels);
648 $channels = $prefix.$channel->getName();
651 if($channels != "") {
652 $this->send("319", $from, $nick, $channels);
655 if($fromUser->getModes()->hasMode("o") || $from == $user->getNumeric() || !$this->getSetting("his_name")) {
656 $this->send("312", $from, $nick, $user->getServer()->getName(), $user->getServer()->getDescription());
658 $this->send("312", $from, $nick, $this->getSetting("his_name"), $this->getSetting("his_desc"));
660 if($modes->hasMode("o") && (!$modes->hasMode("H") || $fromUser->getModes()->hasMode("o"))) {
661 if($modes->hasMode("S")) {
662 if($modes->hasMode("D"))
663 $this->send("313", $from, $nick, "is a Network Service");
665 $this->send("313", $from, $nick, "is an illegal Network Service - HACKER!");
667 $this->send("313", $from, $nick, "is an IRC Operator");
669 if(($auth = $modes->hasMode("r"))) {
670 $this->send("330", $from, $nick, $auth);
673 $this->send("318", $from, $args[1]);
676 private function recv_away($from, $args) {
677 $user = P10_User::getUserByNum($from);
679 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);
682 if(count($args) > 0) {
683 $user->setAway($args[0]);
684 if($this->eventHandler)
685 $this->eventHandler->event_away($user, $args[0]);
687 $user->setAway(null);
688 if($this->eventHandler)
689 $this->eventHandler->event_away($user, null);
693 private function recv_mode($from, $args) {
694 $user = P10_User::getUserByNum($from);
695 if($user == null && strlen($from) != 2) {
696 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);
699 $modes = implode(" ",array_slice($args,1));
700 if($args[0][0] == "#") {
701 $channel = P10_Channel::getChannelByName($args[0]);
703 $channel = new P10_Channel($args[0]);
704 $channel->getModes()->setModes($modes);
705 if($this->eventHandler)
706 $this->eventHandler->event_chanmode($user, $channel, $modes);
708 $targetUser = P10_User::getUserByNick($args[0]);
709 if($targetUser == null) {
710 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);
713 $targetUser->getModes()->setModes($modes);
714 if($this->eventHandler)
715 $this->eventHandler->event_usermode($targetUser, $modes);
719 /********************************************************************************************
721 ********************************************************************************************/
723 private function burst() {
724 foreach($this->server->getUsers() as $user) {
725 $nick = $user->getNick();
726 $connect_time = $user->getConnectTime();
727 $ident = $user->getIdent();
728 $host = $user->getHost();
729 $modes = $user->getModes()->getModeString();
730 $ip = $user->getIP()->getNumeric();
731 $numeric = $user->getNumeric();
732 $realname = $user->getRealname();
733 $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);
735 foreach(P10_Channel::getChannels() as $channel) {
736 $sorted_users = array('ov' => array(), 'o' => array(), 'v' => array(), '-' => array());
737 $local_users = false;
738 foreach($channel->getUsers() as $user) {
739 if(substr($user->getNumeric(), 0, 2) != $this->server->getNumeric()) continue; //skip users that are not on the local server
740 $privs = $channel->getUserPrivs($user);
742 if(($privs & P10_Channel::USERPRIV_OPED)) $strPrivs .= "o";
743 if(($privs & P10_Channel::USERPRIV_VOICE)) $strPrivs .= "v";
744 if($strPrivs == "") $strPrivs = "-";
746 $sorted_users[$strPrivs][] = $user;
748 if(!$local_users) continue;
750 foreach($sorted_users['-'] as $user) {
751 if($userStr != "") $userStr.=",";
752 $userStr .= $user->getNumeric();
754 foreach($sorted_users['ov'] as $i => $user) {
755 if($userStr != "") $userStr.=",";
756 $userStr .= $user->getNumeric();
757 if($i == 0) $userStr .= ":ov";
759 foreach($sorted_users['o'] as $i => $user) {
760 if($userStr != "") $userStr.=",";
761 $userStr .= $user->getNumeric();
762 if($i == 0) $userStr .= ":o";
764 foreach($sorted_users['v'] as $i => $user) {
765 if($userStr != "") $userStr.=",";
766 $userStr .= $user->getNumeric();
767 if($i == 0) $userStr .= ":v";
770 //TODO: Build ban String
772 $modeString = $channel->getModes()->getModeString();
773 if($modeString != "+") {
774 $burstString .= $modeString;
777 if($burstString != "") $burstString .= " ";
778 $burstString .= $userStr;
780 if($banString != "") {
781 if($burstString != "") $burstString .= " ";
782 $burstString .= ":%".$banString;
784 $this->send("B", $channel->getName(), $channel->getCreateTime(), $burstString);
789 /********************************************************************************************
790 * LOCAL USER FUNCTIONS *
791 ********************************************************************************************/
793 public function addUser($nick, $ident, $host, $ip, $modes, $realname) {
794 $user = P10_User::getUserByNick($nick);
795 if($user != null) return ERR_NICK_IN_USE;
796 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
797 while(P10_User::getUserByNum($numeric)) {
798 $this->last_local_numeric++;
799 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
801 $this->last_local_numeric++;
802 $modes = new P10_UserModeSet($modes);
803 $ip = new IPAddr($ip);
804 $user = new P10_User($nick, $numeric, $this->server, time(), $ident, $host, $ip, $realname, $modes);
805 if(($this->flags & self::FLAG_CONNECTED)) {
806 $ip = $user->getIP()->getNumeric();
807 $this->send("N", $nick, $user->getConnectTime(), $ident, $host, $user->getModes()->getModeString(), $ip, $numeric, $realname);
812 public function delUser($user, $reason) {
813 if(!is_a($user, "P10_User")) return ERR_INVALID_USER;
814 if($user->getServer() === $this->server) {
816 $user->quit($reason);
817 if(($this->flags & self::FLAG_CONNECTED))
818 $this->send("Q", $user->getNumeric(), $reason);
821 if(!($this->flags & self::FLAG_CONNECTED))
822 return ERR_NOT_CONNECTED;
823 if($this->eventHandler)
824 $this->eventHandler->event_quit($user, "Killed (".$reason.")");
825 $user->quit("Killed (".$reason.")");
826 $name = ($this->getSetting('his_name') ? $this->getSetting('his_name') : $this->getSetting('name'));
828 $this->send("D", $user->getNumeric(), $name, $reason);
832 public function join($user, $chanName, $privs = 0) {
833 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
834 return ERR_INVALID_USER;
835 if($chanName[0] != "#")
836 return ERR_INVALID_CHANNAME;
837 $channel = P10_Channel::getChannelByName($chanName);
839 $channel = new P10_Channel($chanName);
840 $channel->joinUser($user);
841 if(($this->flags & self::FLAG_CONNECTED))
842 $this->send("J", $user->getNumeric(), $chanName, time(), 0);
844 $channel->setUserPrivs($user, $privs);
845 if(($this->flags & self::FLAG_CONNECTED)) {
846 $modestr = "+".(($privs & P10_Channel::USERPRIV_OPED) ? "o" : "").(($privs & P10_Channel::USERPRIV_VOICE) ? "v" : "");
847 $modestr .= (($privs & P10_Channel::USERPRIV_OPED) ? " ".$user->getNumeric() : "");
848 $modestr .= (($privs & P10_Channel::USERPRIV_VOICE) ? " ".$user->getNumeric() : "");
849 $this->send("OM", $user->getNumeric(), $chanName, $modestr);
852 if($this->eventHandler)
853 $this->eventHandler->event_join($user, $channel, false);
856 public function part($user, $chanName, $reason) {
857 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
858 return ERR_INVALID_USER;
859 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
860 return ERR_INVALID_CHANNAME;
861 if(is_a($chanName, "P10_Channel"))
862 $channel = $chanName;
864 $channel = P10_Channel::getChannelByName($chanName);
866 $channel = new P10_Channel($chanName);
867 if(!$user->isOnChannel($channel))
868 return ERR_NOT_ON_CHANNEL;
869 if($this->eventHandler)
870 $this->eventHandler->event_part($user, $channel, $reason);
871 $channel->partUser($user, $reason);
872 if(($this->flags & self::FLAG_CONNECTED))
873 $this->send("L", $user->getNumeric(), $chanName, $reason);
876 public function kick($user, $target, $chanName, $reason) {
877 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
878 return ERR_INVALID_USER;
879 if(!is_a($target, "P10_User"))
880 return ERR_INVALID_USER;
881 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
882 return ERR_INVALID_CHANNAME;
883 if(is_a($chanName, "P10_Channel"))
884 $channel = $chanName;
886 $channel = P10_Channel::getChannelByName($chanName);
888 $channel = new P10_Channel($chanName);
889 if(!$target->isOnChannel($channel))
890 return ERR_NOT_ON_CHANNEL;
891 if($this->eventHandler)
892 $this->eventHandler->event_kick($user, $target, $channel, $reason);
893 $channel->partUser($target, $reason);
894 if(($this->flags & self::FLAG_CONNECTED))
895 $this->send("K", $user->getNumeric(), $chanName, $target->getNumeric(), $reason);
898 public function privmsg($user, $target, $message) {
899 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
900 return ERR_INVALID_USER;
901 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
902 return ERR_INVALID_USER;
903 if(is_a($target, "P10_Channel"))
904 $targetStr = $target->getName();
905 else if(is_a($target, "P10_User"))
906 $targetStr = $target->getNumeric();
907 else if($target[0] == "#")
908 $targetStr = $target;
910 $targetStr = P10_User::getUserByNick($target)->getNumeric();
912 if($this->eventHandler) {
913 if($targetStr[0] == "#") {
914 $channel = P10_Channel::getChannelByName($targetStr);
916 $channel = new P10_Channel($targetStr);
917 $this->eventHandler->event_chanmessage($user, $channel, $message);
919 $targetUser = P10_User::getUserByNum($targetStr);
920 $this->eventHandler->event_privmessage($user, $targetUser, $message);
923 if(($this->flags & self::FLAG_CONNECTED))
924 $this->send("P", $user->getNumeric(), $targetStr, $message);
927 public function notice($user, $target, $message) {
928 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
929 return ERR_INVALID_USER;
930 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
931 return ERR_INVALID_USER;
932 if(is_a($target, "P10_Channel"))
933 $targetStr = $target->getName();
934 else if(is_a($target, "P10_User"))
935 $targetStr = $target->getNumeric();
936 else if($target[0] == "#")
937 $targetStr = $target;
939 $targetStr = P10_User::getUserByNick($target)->getNumeric();
941 if($this->eventHandler) {
942 if($targetStr[0] == "#") {
943 $channel = P10_Channel::getChannelByName($targetStr);
945 $channel = new P10_Channel($targetStr);
946 $this->eventHandler->event_channotice($user, $channel, $message);
948 $targetUser = P10_User::getUserByNum($targetStr);
949 $this->eventHandler->event_privnotice($user, $targetUser, $message);
952 if(($this->flags & self::FLAG_CONNECTED))
953 $this->send("O", $user->getNumeric(), $targetStr, $message);
956 public function mode($user, $target, $modes, $force = false) {
957 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
958 return ERR_INVALID_USER;
959 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
960 return ERR_INVALID_USER;
961 if(is_a($target, "P10_Channel"))
962 $targetStr = $target->getName();
963 else if(is_a($target, "P10_User"))
964 $targetStr = $target->getNumeric();
965 else if($target[0] == "#")
966 $targetStr = $target;
968 $targetStr = P10_User::getUserByNick($target)->getNumeric();
970 if($targetStr[0] == "#") {
971 $channel = P10_Channel::getChannelByName($targetStr);
973 $channel = new P10_Channel($targetStr);
974 $modes = $channel->getModes()->setModes($modes, true);
975 if(($this->flags & self::FLAG_CONNECTED))
976 $this->send(($force ? "OM" : "M"), $user->getNumeric(), $targetStr, $modes);
977 if($this->eventHandler)
978 $this->eventHandler->event_chanmode(($force ? $this->server : $user), $channel, $modes);
980 $targetUser = P10_User::getUserByNum($targetStr);
981 if($targetUser->getServer() === $this->server) {
983 $modes = $targetUser->getModes()->setModes($modes, true);
984 if(($this->flags & self::FLAG_CONNECTED))
985 $this->send("M", $targetUser->getNumeric(), $targetUser->getNick(), $modes);
986 if($this->eventHandler)
987 $this->eventHandler->event_usermode($targetUser, $modes);
990 if(($this->flags & self::FLAG_CONNECTED))
991 $this->send("SM", $user->getNumeric(), $targetUser->getNumeric(), $modes);
996 public function ctcp($user, $target, $command, $text) {
997 return $this->privmsg($user, $target, "\001".strtoupper($command)." ".$text."\001");
1000 public function ctcp_reply($user, $target, $command, $text) {
1001 return $this->notice($user, $target, "\001".strtoupper($command)." ".$text."\001");