2 /********************************* PHP-P10 ******************************
3 * P10 uplink class by pk910 (c)2011 pk910 *
4 ************************************************************************
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. *
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. *
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. *
21 ************************************************************************
23 * Uplink/Uplink.class.php
25 * This file contains the basic P10 Protocol handler.
27 ************************************************************************
31 * has to be called after the settings have been set.
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.
38 * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
39 * sets the Uplink connection information.
41 * void setLoopTimeout(int $timeout)
42 * sets the maximum time loop() is blocking the script execution.
44 * void setUplinkServer(int $numeric, String $name, String $password, String $description)
45 * sets the own P10 Server information.
47 * void setValidateServer(String $name, String $password)
48 * sets additional security relevant information about the remote server.
50 * void setHISOptions(String $serverName, String $serverDescription String $usermask)
51 * sets the default Account fakehost
53 * void setEventHandler(EventHandler $event_handler)
54 * sets the EventHandlder
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");
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++);
74 private $settings = array();
76 private $eventHandler = null;
77 private $last_local_numeric = 1;
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
87 public function __construct() {
88 $this->client = new Client();
89 $this->setSetting("recv_timeout", 1000);
90 $this->setSetting("his_usermask", "user.NoMask");
93 public function initialize() {
95 trigger_error("Uplink already initialized.", E_USER_ERROR);
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);
105 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
108 public function loop() {
109 if($this->server == null) {
110 trigger_error("Uplink not initialized.", E_USER_ERROR);
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;
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);
125 if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
128 $state = $this->client->connect($host, $port, $this->getSetting("bind"), $this->getSetting("ssl"), $this->getSetting("recv_timeout"));
130 usleep($this->getSetting("recv_timeout") / 1000);
131 $this->flags |= self::FLAG_NOT_CONNECTABLE;
135 $this->loginServer();
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);
145 public function shutdown() {
146 if($this->client->connected()) {
147 if(($this->flags & self::FLAG_P10SESSION)) {
148 $this->send("SQ", "Shutdown requested.");
150 $this->client->disconnect();
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);
161 public function setLoopTimeout($timeout) {
162 $this->setSetting("recv_timeout", $timeout);
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);
172 public function setValidateServer($name, $password) {
173 $this->setSetting("their_name", $name);
174 $this->setSetting("their_password", $password);
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);
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);
188 $this->eventHandler = $event_handler;
191 private function setSetting($setting, $value) {
192 $this->settings[$setting] = $value;
195 private function getSetting($setting) {
196 if(array_key_exists($setting, $this->settings)) {
197 return $this->settings[$setting];
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());
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];
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);
221 switch(strtoupper($tokens[$cmdPos])) {
224 $this->recv_pass($from, $arguments);
227 $this->recv_server($from, $arguments);
230 $this->recv_error($from, $arguments);
234 $this->recv_server($from, $arguments);
237 $this->recv_ping($from, $arguments);
240 $this->recv_nick($from, $arguments);
243 $this->recv_end_of_burst($from, $arguments);
246 $this->recv_end_of_burst_ack($from, $arguments);
249 $this->recv_server_quit($from, $arguments);
252 $this->recv_quit($from, $arguments);
255 $this->recv_burst($from, $arguments);
259 $this->recv_join($from, $arguments);
262 $this->recv_part($from, $arguments);
265 $this->recv_kick($from, $arguments);
268 $this->recv_kill($from, $arguments);
271 $this->recv_privmsg($from, $arguments);
274 $this->recv_notice($from, $arguments);
277 $this->recv_whois($from, $arguments);
282 if($this->eventHandler)
283 $this->eventHandler->event_unknown_cmd($from, strtoupper($tokens[$cmdPos]), $arguments);
288 private function send($command) {
289 if(func_num_args() > 1) {
290 $args = array_slice(func_get_args(), 1);
291 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
293 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
295 $this->client->send($command);
298 /********************************************************************************************
299 * INCOMING COMMANDS *
300 ********************************************************************************************/
302 private function recv_pass($from, $args) {
303 $their_pass = $this->getSetting("their_password");
305 if($args[0] != $their_pass) {
307 $this->flags |= self::FLAG_SECURITY_QUIT;
308 $this->send("ERROR", "Incorrect password received.");
309 $this->client->disconnect();
312 $this->flags |= self::FLAG_GOT_PASS;
316 private function recv_error($from, $args) {
317 //we received an error - the socket is dead for us, now
318 //maybe we want to log this, later
319 $this->client->disconnect();
322 private function recv_server($from, $args) {
325 $their_name = $this->getSetting("their_name");
326 if($their_name && $args[0] != $their_name) {
327 $this->flags |= self::FLAG_SECURITY_QUIT;
328 $this->send("ERROR", "Invalid Server name");
329 $this->client->disconnect();
332 if($this->getSetting("their_password") && !($this->flags & self::FLAG_GOT_PASS)) {
333 $this->flags |= self::FLAG_SECURITY_QUIT;
334 $this->send("ERROR", "PASS missing.");
335 $this->client->disconnect();
338 $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
339 $this->server->addServer($new_server);
340 $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
341 if($this->eventHandler)
342 $this->eventHandler->event_newserver($new_server, !($this->flags & self::FLAG_CONNECTED));
344 //another server got a new slave server ^^
345 $server = P10_Server::getServerByNum($from);
346 if($server == null) {
347 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
350 $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
351 $server->addServer($new_server);
352 if($this->eventHandler)
353 $this->eventHandler->event_newserver($new_server, !($this->flags & self::FLAG_CONNECTED));
357 private function recv_ping($from, $args) {
358 $this->send("Z", $args[0]); //simply PONG
361 private function recv_nick($from, $args) {
362 if(count($args) <= 2) {
364 $user = P10_User::getUserByNum($from);
366 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);
369 if($this->eventHandler)
370 $this->eventHandler->event_nick($user, $args[0]);
371 $user->setNick($args[0]);
374 $numeric = $args[count($args)-2];
376 $server = P10_Server::getServerByNum($from);
377 if($server == null) {
378 trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
381 if(substr($numeric,0,2) != $from) {
382 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);
384 $connect_time = $args[2];
387 $modes = implode(" ",array_slice($args, 5, count($args)-8));
388 $modes = new P10_UserModeSet($modes);
389 $ip = Numerics::parseIP($args[count($args)-3]);
390 $realname = $args[count($args)-1];
391 $user = new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
392 if($this->eventHandler)
393 $this->eventHandler->event_connect($user, !($this->flags & self::FLAG_CONNECTED));
397 private function recv_end_of_burst($from, $args) {
398 if(($this->flags & self::FLAG_BURST_PENDING)) {
401 $this->flags &= ~self::FLAG_BURST_PENDING;
405 private function recv_end_of_burst_ack($from, $args) {
406 $this->flags |= self::FLAG_CONNECTED;
409 private function recv_server_quit($from, $args) {
410 $server = P10_Server::getServerByName($args[0]);
411 if($server == null) {
412 trigger_error("Server (".$args[0].") not found.", E_USER_ERROR);
415 $server->disconnectServer($this->eventHandler);
418 private function recv_quit($from, $args) {
419 $user = P10_User::getUserByNum($from);
421 trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
424 if($this->eventHandler)
425 $this->eventHandler->event_quit($user, $args[0]);
426 $user->quit($args[0]);
429 private function recv_burst($from, $args) {
431 $create_time = $args[1];
432 $channel = P10_Channel::getChannelByName($name);
434 $channel = new P10_Channel($name);
435 $channel->setCreateTime($create_time);
436 $modes = $channel->getModes();
437 $userstr = $args[count($args)-1];
438 $modeparamcount = count($args)-3;
439 if($userstr[0] == "%") {
441 $banlist = explode(" ", substr($userstr, 1));
442 foreach($banlist as $ban) {
445 $userstr = $args[count($args)-2];
448 if($userstr[0] == "+") { //MODE String
452 $users = explode(",",$userstr);
453 $isop = false; $isvoice = false;
454 foreach($users as $user) {
455 if($user == "") continue;
456 $uexp = explode(":", $user);
457 if(strlen($uexp[0]) != 5) {
458 trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_ERROR);
461 if(count($uexp) > 1) {
464 for($i = 0; $i < strlen($uexp[1]); $i++) {
465 if($uexp[1][0] == "@") $isop = true;
466 if($uexp[1][0] == "+") $isvoice = true;
469 $user = P10_User::getUserByNum($uexp[0]);
471 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
474 $channel->burstUser($user, $isop, $isvoice);
475 if($this->eventHandler)
476 $this->eventHandler->event_join($user, $channel, true);
478 $modes->parseModes(implode(array_slice($args, 2, $modeparamcount)));
481 private function recv_join($from, $args) {
482 $user = P10_User::getUserByNum($from);
484 trigger_error("Server tries to join an user that does not exist or was not found on recv_join.", E_USER_ERROR);
487 $channel = P10_Channel::getChannelByName($args[0]);
489 $channel = new P10_Channel($args[0]);
490 $channel->joinUser($user);
491 if($this->eventHandler)
492 $this->eventHandler->event_join($user, $channel, false);
495 private function recv_part($from, $args) {
496 $user = P10_User::getUserByNum($from);
498 trigger_error("Server tries to part an user that does not exist or was not found on recv_join.", E_USER_ERROR);
501 $channel = P10_Channel::getChannelByName($args[0]);
503 $channel = new P10_Channel($args[0]);
504 if($this->eventHandler)
505 $this->eventHandler->event_part($user, $channel, $args[1]);
506 $channel->partUser($user);
509 private function recv_kick($from, $args) {
510 $user = P10_User::getUserByNum($from);
512 trigger_error("An unknown user tries to kick another user on recv_kick.", E_USER_ERROR);
515 $channel = P10_Channel::getChannelByName($args[0]);
517 $channel = new P10_Channel($args[0]);
518 $target = P10_User::getUserByNum($args[1]);
519 if($target == null) {
520 trigger_error("Someone tries to kick an user that does not exist or was not found on recv_kick.", E_USER_ERROR);
523 if($this->eventHandler)
524 $this->eventHandler->event_kick($user, $target, $channel, $args[1]);
525 $channel->partUser($user);
528 private function recv_kill($from, $args) {
529 $user = P10_User::getUserByNum($args[0]);
531 trigger_error("Server tries to kill an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
534 if($this->eventHandler)
535 $this->eventHandler->event_quit($user, "Killed (".$args[1].")");
536 $user->quit($args[1]);
539 private function recv_privmsg($from, $args) {
540 $user = P10_User::getUserByNum($from);
542 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);
545 if($this->eventHandler) {
546 if($args[0][0] == "#") {
547 $channel = P10_Channel::getChannelByName($args[0]);
549 $channel = new P10_Channel($args[0]);
550 $this->eventHandler->event_chanmessage($user, $channel, $args[1]);
552 $targetUser = P10_User::getUserByNum($args[0]);
553 if($targetUser == null) {
554 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);
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_ERROR);
568 if($this->eventHandler) {
569 if($args[0][0] == "#") {
570 $channel = P10_Channel::getChannelByName($args[0]);
572 $channel = new P10_Channel($args[0]);
573 $this->eventHandler->event_channotice($user, $channel, $args[1]);
575 $targetUser = P10_User::getUserByNum($args[0]);
576 if($targetUser == null) {
577 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);
580 $this->eventHandler->event_privnotice($user, $targetUser, $args[1]);
585 private function recv_whois($from, $args) {
586 /* [get] ACAAF W AX :NetworkServ */
587 $fromUser = P10_User::getUserByNum($from);
588 if($fromUser == null) {
589 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);
592 $users=explode(",",$args[1]);
593 foreach($users as $nick) {
594 $user = P10_User::getUserByNick($nick);
596 $this->send("401", $from, $nick);
599 $nick = $user->getNick();
600 $ident = $user->getIdent();
601 $hostmask = $user->getHost();
602 $modes = $user->getModes();
603 if($modes->hasMode('x')) {
604 if(($fakehost = $modes->hasMode('f'))) {
605 $hostmask = $fakehost;
606 } elseif(($account = $modes->hasMode('r'))) {
607 $hostmask = $account.".".$this->getSetting("his_usermask");
610 $realname = $user->getRealname();
611 $this->send("311", $from , $nick, $ident, $hostmask, $realname);
612 if(((!$modes->hasMode('n') && !$modes->hasMode('k')) || $from == $user->getNumeric()) && count($user->getChannels()) != 0) {
614 foreach($user->getChannels() as $channel) {
615 $cmodes = $channel->getModes();
616 $privs = $channel->getUserPrivs($user);
617 if($cmodes->hasMode("s") && !$fromUser->isOnChannel($channel) && $from != $user->getNumeric()) continue;
618 if($cmodes->hasMode("u") && ($privs & (P10_Channel::USERPRIV_OPED | P10_Channel::USERPRIV_VOICE)) == 0 && $from != $user->getNumeric()) continue;
619 $chanstr = ($channels == "" ? "" : " ");
621 if(($privs & P10_Channel::USERPRIV_OPED)) {
623 } else if(($privs & P10_Channel::USERPRIV_VOICE)) {
626 $chanstr .= $prefix.$channel->getName();
627 if(strlen($channels) + strlen($chanstr) > 450) {
628 $this->send("319", $from, $nick, $channels);
629 $channels = $prefix.$channel->getName();
632 if($channels != "") {
633 $this->send("319", $from, $nick, $channels);
635 if($fromUser->getModes()->hasMode("o") || $from == $user->getNumeric() || !$this->getSetting("his_name")) {
636 $this->send("312", $from, $nick, $user->getServer()->getName(), $user->getServer()->getDescription());
638 $this->send("312", $from, $nick, $this->getSetting("his_name"), $this->getSetting("his_desc"));
640 if($modes->hasMode("o") && (!$modes->hasMode("H") || $fromUser->getModes()->hasMode("o"))) {
641 if($modes->hasMode("S")) {
642 if($modes->hasMode("D"))
643 $this->send("313", $from, $nick, "is a Network Service");
645 $this->send("313", $from, $nick, "is an illegal Network Service - HACKER!");
647 $this->send("313", $from, $nick, "is an IRC Operator");
649 if(($auth = $modes->hasMode("r"))) {
650 $this->send("330", $from, $nick, $auth);
654 $this->send("318", $from, $args[1]);
657 /********************************************************************************************
659 ********************************************************************************************/
661 private function burst() {
662 foreach($this->server->getUsers() as $user) {
663 $nick = $user->getNick();
664 $connect_time = $user->getConnectTime();
665 $ident = $user->getIdent();
666 $host = $user->getHost();
667 $modes = $user->getModes()->getModeString();
668 $ip = Numerics::numericFromIP($user->getIP());
669 $numeric = $user->getNumeric();
670 $realname = $user->getRealname();
671 $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);
673 foreach(P10_Channel::getChannels() as $channel) {
674 $sorted_users = array('ov' => array(), 'o' => array(), 'v' => array(), '-' => array());
675 $local_users = false;
676 foreach($channel->getUsers() as $user) {
677 if(substr($user->getNumeric(), 0, 2) != $this->server->getNumeric()) continue; //skip users that are not on the local server
678 $privs = $channel->getUserPrivs($user);
680 if(($privs & P10_Channel::USERPRIV_OPED)) $strPrivs .= "o";
681 if(($privs & P10_Channel::USERPRIV_VOICE)) $strPrivs .= "v";
682 if($strPrivs == "") $strPrivs = "-";
684 $sorted_users[$strPrivs][] = $user;
686 if(!$local_users && !$channel->getModes()->hasMode("z")) continue;
688 foreach($sorted_users['-'] as $user) {
689 if($userStr != "") $userStr.=",";
690 $userStr .= $user->getNumeric();
692 foreach($sorted_users['ov'] as $i => $user) {
693 if($userStr != "") $userStr.=",";
694 $userStr .= $user->getNumeric();
695 if($i == 0) $userStr .= ":ov";
697 foreach($sorted_users['o'] as $i => $user) {
698 if($userStr != "") $userStr.=",";
699 $userStr .= $user->getNumeric();
700 if($i == 0) $userStr .= ":o";
702 foreach($sorted_users['v'] as $i => $user) {
703 if($userStr != "") $userStr.=",";
704 $userStr .= $user->getNumeric();
705 if($i == 0) $userStr .= ":v";
708 //TODO: Build ban String
710 $modeString = $channel->getModes()->getModeString();
711 if($modeString != "+") {
712 $burstString .= $modeString;
715 if($burstString != "") $burstString .= " ";
716 $burstString .= $userStr;
718 if($banString != "") {
719 if($burstString != "") $burstString .= " ";
720 $burstString .= ":%".$banString;
722 $this->send("B", $channel->getName(), $channel->getCreateTime(), $burstString);
727 /********************************************************************************************
728 * LOCAL USER FUNCTIONS *
729 ********************************************************************************************/
731 public function addUser($nick, $ident, $host, $ip, $modes, $realname) {
732 $user = P10_User::getUserByNick($nick);
733 if($user != null) return ERR_NICK_IN_USE;
734 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
735 while(P10_User::getUserByNum($numeric)) {
736 $this->last_local_numeric++;
737 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
739 $this->last_local_numeric++;
740 $modes = new P10_UserModeSet($modes);
741 $user = new P10_User($nick, $numeric, $this->server, time(), $ident, $host, $ip, $realname, $modes);
742 if(($this->flags & self::FLAG_CONNECTED)) {
743 $ip = Numerics::numericFromIP($user->getIP());
744 $this->send("N", $nick, $user->getConnectTime(), $ident, $host, $user->getModes()->getModeString(), $ip, $numeric, $realname);
749 public function delUser($user, $reason) {
750 if(!is_a($user, "P10_User")) return ERR_INVALID_USER;
751 if($user->getServer() === $this->server) {
753 $user->quit($reason);
754 if(($this->flags & self::FLAG_CONNECTED))
755 $this->send("Q", $user->getNumeric(), $reason);
758 if(!($this->flags & self::FLAG_CONNECTED))
759 return ERR_NOT_CONNECTED;
760 if($this->eventHandler)
761 $this->eventHandler->event_quit($user, "Killed (".$reason.")");
762 $user->quit("Killed (".$reason.")");
763 $name = ($this->getSetting('his_name') ? $this->getSetting('his_name') : $this->getSetting('name'));
765 $this->send("D", $user->getNumeric(), $name, $reason);
769 public function join($user, $chanName, $privs = 0) {
770 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
771 return ERR_INVALID_USER;
772 if($chanName[0] != "#")
773 return ERR_INVALID_CHANNAME;
774 $channel = P10_Channel::getChannelByName($chanName);
776 $channel = new P10_Channel($chanName);
777 $channel->joinUser($user);
778 if(($this->flags & self::FLAG_CONNECTED))
779 $this->send("J", $user->getNumeric(), $chanName, time(), 0);
781 $channel->setUserPrivs($user, $privs);
782 if(($this->flags & self::FLAG_CONNECTED)) {
783 $modestr = "+".(($privs & P10_Channel::USERPRIV_OPED) ? "o" : "").(($privs & P10_Channel::USERPRIV_VOICE) ? "v" : "");
784 $modestr .= (($privs & P10_Channel::USERPRIV_OPED) ? " ".$user->getNumeric() : "");
785 $modestr .= (($privs & P10_Channel::USERPRIV_VOICE) ? " ".$user->getNumeric() : "");
786 $this->send("OM", $user->getNumeric(), $chanName, $modestr);
789 if($this->eventHandler)
790 $this->eventHandler->event_join($user, $channel, false);
793 public function part($user, $chanName, $reason) {
794 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
795 return ERR_INVALID_USER;
796 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
797 return ERR_INVALID_CHANNAME;
798 if(is_a($chanName, "P10_Channel"))
799 $channel = $chanName;
801 $channel = P10_Channel::getChannelByName($chanName);
803 $channel = new P10_Channel($chanName);
804 if(!$user->isOnChannel($channel))
805 return ERR_NOT_ON_CHANNEL;
806 if($this->eventHandler)
807 $this->eventHandler->event_part($user, $channel, $reason);
808 $channel->partUser($user, $reason);
809 if(($this->flags & self::FLAG_CONNECTED))
810 $this->send("L", $user->getNumeric(), $chanName, $reason);
813 public function kick($user, $target, $chanName, $reason) {
814 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
815 return ERR_INVALID_USER;
816 if(!is_a($target, "P10_User"))
817 return ERR_INVALID_USER;
818 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
819 return ERR_INVALID_CHANNAME;
820 if(is_a($chanName, "P10_Channel"))
821 $channel = $chanName;
823 $channel = P10_Channel::getChannelByName($chanName);
825 $channel = new P10_Channel($chanName);
826 if(!$target->isOnChannel($channel))
827 return ERR_NOT_ON_CHANNEL;
828 if($this->eventHandler)
829 $this->eventHandler->event_kick($user, $target, $channel, $reason);
830 $channel->partUser($target, $reason);
831 if(($this->flags & self::FLAG_CONNECTED))
832 $this->send("K", $user->getNumeric(), $chanName, $target->getNumeric(), $reason);
835 public function privmsg($user, $target, $message) {
836 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
837 return ERR_INVALID_USER;
838 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
839 return ERR_INVALID_USER;
840 if(is_a($target, "P10_Channel"))
841 $targetStr = $target->getName();
842 else if(is_a($target, "P10_User"))
843 $targetStr = $target->getNumeric();
844 else if($target[0] == "#")
845 $targetStr = $target;
847 $targetStr = P10_User::getUserByNick($target)->getNumeric();
849 if($this->eventHandler) {
850 if($targetStr[0] == "#") {
851 $channel = P10_Channel::getChannelByName($targetStr);
853 $channel = new P10_Channel($targetStr);
854 $this->eventHandler->event_chanmessage($user, $channel, $message);
856 $targetUser = P10_User::getUserByNum($targetStr);
857 $this->eventHandler->event_privmessage($user, $targetUser, $message);
860 if(($this->flags & self::FLAG_CONNECTED))
861 $this->send("P", $user->getNumeric(), $targetStr, $message);
864 public function notice($user, $target, $message) {
865 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
866 return ERR_INVALID_USER;
867 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
868 return ERR_INVALID_USER;
869 if(is_a($target, "P10_Channel"))
870 $targetStr = $target->getName();
871 else if(is_a($target, "P10_User"))
872 $targetStr = $target->getNumeric();
873 else if($target[0] == "#")
874 $targetStr = $target;
876 $targetStr = P10_User::getUserByNick($target)->getNumeric();
878 if($this->eventHandler) {
879 if($targetStr[0] == "#") {
880 $channel = P10_Channel::getChannelByName($targetStr);
882 $channel = new P10_Channel($targetStr);
883 $this->eventHandler->event_channotice($user, $channel, $message);
885 $targetUser = P10_User::getUserByNum($targetStr);
886 $this->eventHandler->event_privnotice($user, $targetUser, $message);
889 if(($this->flags & self::FLAG_CONNECTED))
890 $this->send("O", $user->getNumeric(), $targetStr, $message);
893 public function mode($user, $target, $modes, $force = false) {
894 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
895 return ERR_INVALID_USER;
896 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
897 return ERR_INVALID_USER;
898 if(is_a($target, "P10_Channel"))
899 $targetStr = $target->getName();
900 else if(is_a($target, "P10_User"))
901 $targetStr = $target->getNumeric();
902 else if($target[0] == "#")
903 $targetStr = $target;
905 $targetStr = P10_User::getUserByNick($target)->getNumeric();
907 if($targetStr[0] == "#") {
908 $channel = P10_Channel::getChannelByName($targetStr);
910 $channel = new P10_Channel($targetStr);
911 $modes = $channel->getModes()->setModes($modes);
912 if(($this->flags & self::FLAG_CONNECTED))
913 $this->send(($force ? "OM" : "M"), $user->getNumeric(), $targetStr, $modes);
914 if($this->eventHandler)
915 $this->eventHandler->event_chanmode(($force ? $this->server : $user), $channel, $modes);
917 $targetUser = P10_User::getUserByNum($targetStr);
918 if($targetUser->getServer() === $this->server) {
920 $modes = $targetUser->getModes()->setModes($modes);
921 if(($this->flags & self::FLAG_CONNECTED))
922 $this->send("M", $targetUser->getNumeric(), $targetUser->getNick(), $modes);
923 if($this->eventHandler)
924 $this->eventHandler->event_usermode($targetUser, $modes);
927 if(($this->flags & self::FLAG_CONNECTED))
928 $this->send("SM", $user->getNumeric(), $targetUser->getNumeric(), $modes);