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 setEventHandler(EventHandler $event_handler)
51 * sets the EventHandlder
53 require_once("Client.class.php");
54 require_once("Numerics.class.php");
55 require_once("P10Formatter.class.php");
56 require_once("P10_Server.class.php");
57 require_once("P10_User.class.php");
58 require_once("P10_Channel.class.php");
59 require_once("P10_ModeSets.class.php");
60 require_once("EventHandler.interface.php");
63 define("ERR_NICK_IN_USE", $e++);
64 define("ERR_INVALID_USER", $e++);
65 define("ERR_INVALID_CHANNAME", $e++);
66 define("ERR_NOT_CONNECTED", $e++);
67 define("ERR_NOT_ON_CHANNEL", $e++);
71 private $settings = array();
73 private $eventHandler = null;
74 private $last_local_numeric = 1;
76 const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (server is connected)
77 const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues
78 const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
79 const FLAG_BURST_PENDING = 0x0008; //we still have to burst
80 const FLAG_CONNECTED = 0x0010; //connected and synced (ready)
81 const FLAG_GOT_PASS = 0x0020; //got PASS from the remote Server
84 public function __construct() {
85 $this->client = new Client();
86 $this->setSetting("recv_timeout", 1000);
89 public function initialize() {
91 trigger_error("Uplink already initialized.", E_USER_ERROR);
94 $self_numeric = $this->getSetting("numeric");
95 $self_name = $this->getSetting("name");
96 $self_description = $this->getSetting("description");
97 if(!$self_numeric || !$self_name) {
98 trigger_error("Server Settings missing.", E_USER_ERROR);
101 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
104 public function loop() {
105 if($this->server == null) {
106 trigger_error("Uplink not initialized.", E_USER_ERROR);
109 if(!$this->client->connected()) {
110 if(($this->flags & self::FLAG_P10SESSION)) {
111 //Server got disconnected
112 $this->server->disconnectServer($this->eventHandler, true);
113 $this->flags &= ~self::FLAG_P10SESSION;
115 $host = $this->getSetting("host");
116 $port = $this->getSetting("port");
117 if($host == null || $port == null) {
118 trigger_error("Uplink Settings missing.", E_USER_ERROR);
121 if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
124 $state = $this->client->connect($host, $port, $this->getSetting("bind"), $this->getSetting("ssl"), $this->getSetting("recv_timeout"));
126 usleep($this->getSetting("recv_timeout") / 1000);
127 $this->flags |= self::FLAG_NOT_CONNECTABLE;
131 $this->loginServer();
133 //try to receive new data from the Uplink
134 $lines = $this->client->recv();
135 if($lines == null) return;
136 foreach($lines as $line) {
137 $this->parseLine($line);
141 public function setUplinkHost($host, $port, $ssl = false, $bind = null) {
142 $this->setSetting("host", $host);
143 $this->setSetting("port", $port);
144 $this->setSetting("ssl", $ssl);
145 $this->setSetting("bind", $bind);
148 public function setLoopTimeout($timeout) {
149 $this->setSetting("recv_timeout", $timeout);
152 public function setUplinkServer($numeric, $name, $password, $description) {
153 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
154 $this->setSetting("name", $name);
155 $this->setSetting("password", $password);
156 $this->setSetting("description", $description);
159 public function setValidateServer($name, $password) {
160 $this->setSetting("their_name", $name);
161 $this->setSetting("their_password", $password);
164 public function setEventHandler($event_handler) {
165 if(!is_a($event_handler, "EventHandler")) {
166 trigger_error((is_object($event_handler) ? get_class($event_handler) : gettype($event_handler))." is NOT a valid EventHandler.", E_USER_ERROR);
169 $this->eventHandler = $event_handler;
172 private function setSetting($setting, $value) {
173 $this->settings[$setting] = $value;
176 private function getSetting($setting) {
177 if(array_key_exists($setting, $this->settings)) {
178 return $this->settings[$setting];
184 private function loginServer() {
185 $password = $this->getSetting("password");
186 $this->send("PASS", $password);
187 $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
190 private function parseLine($line) {
191 $highExplode = explode(" :", $line, 2);
192 $tokens = explode(" ", $highExplode[0]);
193 if(count($highExplode) > 1)
194 $tokens[] = $highExplode[1];
195 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
196 if($cmdPos == 1) $from = $tokens[0];
198 $arguments = array_slice($tokens, $cmdPos + 1);
199 if(($this->flags & self::FLAG_P10SESSION) && $this->eventHandler) {
200 $this->eventHandler->event_preparse($from, strtoupper($tokens[$cmdPos]), $arguments);
202 switch(strtoupper($tokens[$cmdPos])) {
205 $this->recv_pass($from, $arguments);
208 $this->recv_server($from, $arguments);
211 $this->recv_error($from, $arguments);
215 $this->recv_server($from, $arguments);
218 $this->recv_ping($from, $arguments);
221 $this->recv_nick($from, $arguments);
224 $this->recv_end_of_burst($from, $arguments);
227 $this->recv_end_of_burst_ack($from, $arguments);
230 $this->recv_server_quit($from, $arguments);
233 $this->recv_quit($from, $arguments);
236 $this->recv_burst($from, $arguments);
240 $this->recv_join($from, $arguments);
243 $this->recv_part($from, $arguments);
246 $this->recv_kick($from, $arguments);
249 $this->recv_kill($from, $arguments);
252 $this->recv_privmsg($from, $arguments);
255 $this->recv_notice($from, $arguments);
260 if($this->eventHandler)
261 $this->eventHandler->event_unknown_cmd($from, strtoupper($tokens[$cmdPos]), $arguments);
266 private function send($command) {
267 if(func_num_args() > 1) {
268 $args = array_slice(func_get_args(), 1);
269 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
271 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
273 $this->client->send($command);
276 /********************************************************************************************
277 * INCOMING COMMANDS *
278 ********************************************************************************************/
280 private function recv_pass($from, $args) {
281 $their_pass = $this->getSetting("their_password");
283 if($args[0] != $their_pass) {
285 $this->flags |= self::FLAG_SECURITY_QUIT;
286 $this->send("ERROR", "Incorrect password received.");
287 $this->client->disconnect();
290 $this->flags |= self::FLAG_GOT_PASS;
294 private function recv_error($from, $args) {
295 //we received an error - the socket is dead for us, now
296 //maybe we want to log this, later
297 $this->client->disconnect();
300 private function recv_server($from, $args) {
303 $their_name = $this->getSetting("their_name");
304 if($their_name && $args[0] != $their_name) {
305 $this->flags |= self::FLAG_SECURITY_QUIT;
306 $this->send("ERROR", "Invalid Server name");
307 $this->client->disconnect();
310 if($this->getSetting("their_password") && !($this->flags & self::FLAG_GOT_PASS)) {
311 $this->flags |= self::FLAG_SECURITY_QUIT;
312 $this->send("ERROR", "PASS missing.");
313 $this->client->disconnect();
316 $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
317 $this->server->addServer($new_server);
318 $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
319 if($this->eventHandler)
320 $this->eventHandler->event_server($new_server, !($this->flags & self::FLAG_CONNECTED));
322 //another server got a new slave server ^^
323 $server = P10_Server::getServerByNum($from);
324 if($server == null) {
325 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
328 $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
329 $server->addServer($new_server);
330 if($this->eventHandler)
331 $this->eventHandler->event_server($new_server, !($this->flags & self::FLAG_CONNECTED));
335 private function recv_ping($from, $args) {
336 $this->send("Z", $args[0]); //simply PONG
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 = Numerics::parseIP($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 $channel = P10_Channel::getChannelByName($name);
412 $channel = new P10_Channel($name);
413 $channel->setCreateTime($create_time);
414 $modes = $channel->getModes();
415 $userstr = $args[count($args)-1];
416 $modeparamcount = count($args)-3;
417 if($userstr[0] == "%") {
419 $banlist = explode(" ", substr($userstr, 1));
420 foreach($banlist as $ban) {
423 $userstr = $args[count($args)-2];
426 if($userstr[0] == "+") { //MODE String
430 $users = explode(",",$userstr);
431 $isop = false; $isvoice = false;
432 foreach($users as $user) {
433 $uexp = explode(":", $user);
434 if(strlen($uexp[0]) != 5) {
435 trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_ERROR);
438 if(count($uexp) > 1) {
441 for($i = 0; $i < strlen($uexp[1]); $i++) {
442 if($uexp[1][0] == "@") $isop = true;
443 if($uexp[1][0] == "+") $isvoice = true;
446 $user = P10_User::getUserByNum($uexp[0]);
448 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
451 $channel->burstUser($user, $isop, $isvoice);
452 if($this->eventHandler)
453 $this->eventHandler->event_join($user, $channel, true);
455 $modes->parseModes(implode(array_slice($args, 2, $modeparamcount)));
458 private function recv_join($from, $args) {
459 $user = P10_User::getUserByNum($from);
461 trigger_error("Server tries to join an user that does not exist or was not found on recv_join.", E_USER_ERROR);
464 $channel = P10_Channel::getChannelByName($args[0]);
466 $channel = new P10_Channel($args[0]);
467 $channel->joinUser($user);
468 if($this->eventHandler)
469 $this->eventHandler->event_join($user, $channel, false);
472 private function recv_part($from, $args) {
473 $user = P10_User::getUserByNum($from);
475 trigger_error("Server tries to part an user that does not exist or was not found on recv_join.", E_USER_ERROR);
478 $channel = P10_Channel::getChannelByName($args[0]);
480 $channel = new P10_Channel($args[0]);
481 if($this->eventHandler)
482 $this->eventHandler->event_part($user, $channel, $args[1]);
483 $channel->partUser($user);
486 private function recv_kick($from, $args) {
487 $user = P10_User::getUserByNum($from);
489 trigger_error("An unknown user tries to kick another user on recv_kick.", E_USER_ERROR);
492 $channel = P10_Channel::getChannelByName($args[0]);
494 $channel = new P10_Channel($args[0]);
495 $target = P10_User::getUserByNum($args[1]);
496 if($target == null) {
497 trigger_error("Someone tries to kick an user that does not exist or was not found on recv_kick.", E_USER_ERROR);
500 if($this->eventHandler)
501 $this->eventHandler->event_kick($user, $target, $channel, $args[1]);
502 $channel->partUser($user);
505 private function recv_kill($from, $args) {
506 $user = P10_User::getUserByNum($args[0]);
508 trigger_error("Server tries to kill an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
511 if($this->eventHandler)
512 $this->eventHandler->event_quit($user, "Killed (".$args[1].")");
513 $user->quit($args[1]);
516 private function recv_privmsg($from, $args) {
517 $user = P10_User::getUserByNum($from);
519 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);
522 if($this->eventHandler) {
523 if($args[0] == "#") {
524 $channel = P10_Channel::getChannelByName($args[0]);
526 $channel = new P10_Channel($args[0]);
527 $this->eventHandler->event_chanmessage($user, $channel, $args[1]);
529 $targetUser = P10_User::getUserByNum($args[0]);
530 if($targetUser == null) {
531 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);
534 $this->eventHandler->event_privmessage($user, $targetUser, $args[1]);
539 private function recv_notice($from, $args) {
540 $user = P10_User::getUserByNum($from);
542 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);
545 if($this->eventHandler) {
546 if($args[0] == "#") {
547 $channel = P10_Channel::getChannelByName($args[0]);
549 $channel = new P10_Channel($args[0]);
550 $this->eventHandler->event_channotice($user, $channel, $args[1]);
552 $targetUser = P10_User::getUserByNum($args[0]);
553 if($targetUser == null) {
554 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);
557 $this->eventHandler->event_privnotice($user, $targetUser, $args[1]);
562 /********************************************************************************************
564 ********************************************************************************************/
566 private function burst() {
567 foreach($this->server->getUsers() as $user) {
568 $nick = $user->getNick();
569 $connect_time = $user->getConnectTime();
570 $ident = $user->getIdent();
571 $host = $user->getHost();
572 $modes = $user->getModes()->getModeString();
573 $ip = Numerics::numericFromIP($user->getIP());
574 $numeric = $user->getNumeric();
575 $realname = $user->getRealname();
576 $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);
578 foreach(P10_Channel::getChannels() as $channel) {
579 $sorted_users = array('ov' => array(), 'o' => array(), 'v' => array(), '-' => array());
580 $local_users = false;
581 foreach($channel->getUsers() as $user) {
582 if(substr($user->getNumeric(), 0, 2) != $this->server->getNumeric()) continue; //skip users that are not on the local server
583 $privs = $channel->getUserPrivs($user);
585 if(($privs & P10_Channel::USERPRIV_OPED)) $strPrivs .= "o";
586 if(($privs & P10_Channel::USERPRIV_VOICE)) $strPrivs .= "v";
587 if($strPrivs == "") $strPrivs = "-";
589 $sorted_users[$strPrivs][] = $user;
591 if(!$local_users && !$channel->getModes()->hasMode("z")) continue;
593 foreach($sorted_users['-'] as $user) {
594 if($userStr != "") $userStr.=",";
595 $userStr .= $user->getNumeric();
597 foreach($sorted_users['ov'] as $i => $user) {
598 if($userStr != "") $userStr.=",";
599 $userStr .= $user->getNumeric();
600 if($i == 0) $userStr .= ":ov";
602 foreach($sorted_users['o'] as $i => $user) {
603 if($userStr != "") $userStr.=",";
604 $userStr .= $user->getNumeric();
605 if($i == 0) $userStr .= ":o";
607 foreach($sorted_users['v'] as $i => $user) {
608 if($userStr != "") $userStr.=",";
609 $userStr .= $user->getNumeric();
610 if($i == 0) $userStr .= ":v";
613 //TODO: Build ban String
615 $modeString = $channel->getModes()->getModeString();
616 if($modeString != "+") {
617 $burstString .= $modeString;
620 if($burstString != "") $burstString .= " ";
621 $burstString .= $userStr;
623 if($banString != "") {
624 if($burstString != "") $burstString .= " ";
625 $burstString .= ":%".$banString;
627 $this->send("B", $channel->getName(), $channel->getCreateTime(), $burstString);
632 /********************************************************************************************
633 * LOCAL USER FUNCTIONS *
634 ********************************************************************************************/
636 public function addUser($nick, $ident, $host, $ip, $modes, $realname) {
637 $user = P10_User::getUserByNick($nick);
638 if($user != null) return ERR_NICK_IN_USE;
639 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
640 while(P10_User::getUserByNum($numeric)) {
641 $this->last_local_numeric++;
642 $numeric = substr($this->server->getNumeric(),0,2).Numerics::intToNum($this->last_local_numeric, 3);
644 $this->last_local_numeric++;
645 $user = new P10_User($nick, $numeric, $this->server, time(), $ident, $host, $ip, $realname, $modes);
646 if(($this->flags & self::FLAG_CONNECTED)) {
647 $ip = Numerics::numericFromIP($user->getIP());
648 $this->send("N", $nick, $user->getConnectTime(), $ident, $host, $user->getModes()->getModeString(), $ip, $numeric, $realname);
653 public function delUser($user, $reason) {
654 if(!is_a($user, "P10_User")) return ERR_INVALID_USER;
655 if($user->getServer() === $this->server) {
657 $user->quit($reason);
658 if(($this->flags & self::FLAG_CONNECTED))
659 $this->send("Q", $user->getNumeric(), $reason);
662 if(!($this->flags & self::FLAG_CONNECTED))
663 return ERR_NOT_CONNECTED;
664 if($this->eventHandler)
665 $this->eventHandler->event_quit($user, "Killed (".$reason.")");
666 $user->quit("Killed (".$reason.")");
667 $name = ($this->getSetting('his_name') ? $this->getSetting('his_name') : $this->getSetting('name'));
669 $this->send("D", $user->getNumeric(), $name, $reason);
673 public function join($user, $chanName) {
674 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
675 return ERR_INVALID_USER;
676 if($chanName[0] != "#")
677 return ERR_INVALID_CHANNAME;
678 $channel = P10_Channel::getChannelByName($chanName);
680 $channel = new P10_Channel($chanName);
681 $channel->joinUser($user);
682 if(($this->flags & self::FLAG_CONNECTED))
683 $this->send("J", $user->getNumeric(), $chanName, time(), 0);
684 if($this->eventHandler)
685 $this->eventHandler->event_join($user, $channel, false);
688 public function part($user, $chanName, $reason) {
689 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
690 return ERR_INVALID_USER;
691 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
692 return ERR_INVALID_CHANNAME;
693 if(is_a($chanName, "P10_Channel"))
694 $channel = $chanName;
696 $channel = P10_Channel::getChannelByName($chanName);
698 $channel = new P10_Channel($chanName);
699 if(!$user->isOnChannel($channel))
700 return ERR_NOT_ON_CHANNEL;
701 if($this->eventHandler)
702 $this->eventHandler->event_part($user, $channel, $reason);
703 $channel->partUser($user, $reason);
704 if(($this->flags & self::FLAG_CONNECTED))
705 $this->send("L", $user->getNumeric(), $chanName, $reason);
708 public function kick($user, $target, $chanName, $reason) {
709 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
710 return ERR_INVALID_USER;
711 if(!is_a($target, "P10_User"))
712 return ERR_INVALID_USER;
713 if(!((is_string($chanName) && $chanName[0] == "#") || is_a($chanName, "P10_Channel")))
714 return ERR_INVALID_CHANNAME;
715 if(is_a($chanName, "P10_Channel"))
716 $channel = $chanName;
718 $channel = P10_Channel::getChannelByName($chanName);
720 $channel = new P10_Channel($chanName);
721 if(!$target->isOnChannel($channel))
722 return ERR_NOT_ON_CHANNEL;
723 if($this->eventHandler)
724 $this->eventHandler->event_kick($user, $target, $channel, $reason);
725 $channel->partUser($target, $reason);
726 if(($this->flags & self::FLAG_CONNECTED))
727 $this->send("K", $user->getNumeric(), $chanName, $target->getNumeric(), $reason);
730 public function privmsg($user, $target, $message) {
731 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
732 return ERR_INVALID_USER;
733 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
734 return ERR_INVALID_USER;
735 if(is_a($target, "P10_Channel"))
736 $targetStr = $target->getName();
737 else if(is_a($target, "P10_User"))
738 $targetStr = $target->getNumeric();
739 else if($target[0] == "#")
740 $targetStr = $target;
742 $targetStr = P10_User::getUserByNick($target)->getNumeric();
744 if($this->eventHandler) {
745 if($targetStr[0] == "#") {
746 $channel = P10_Channel::getChannelByName($targetStr);
748 $channel = new P10_Channel($targetStr);
749 $this->eventHandler->event_chanmessage($user, $channel, $message);
751 $targetUser = P10_User::getUserByNum($targetStr);
752 $this->eventHandler->event_privmessage($user, $targetUser, $message);
755 if(($this->flags & self::FLAG_CONNECTED))
756 $this->send("P", $user->getNumeric(), $targetStr, $message);
759 public function notice($user, $target, $message) {
760 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
761 return ERR_INVALID_USER;
762 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
763 return ERR_INVALID_USER;
764 if(is_a($target, "P10_Channel"))
765 $targetStr = $target->getName();
766 else if(is_a($target, "P10_User"))
767 $targetStr = $target->getNumeric();
768 else if($target[0] == "#")
769 $targetStr = $target;
771 $targetStr = P10_User::getUserByNick($target)->getNumeric();
773 if($this->eventHandler) {
774 if($targetStr[0] == "#") {
775 $channel = P10_Channel::getChannelByName($targetStr);
777 $channel = new P10_Channel($targetStr);
778 $this->eventHandler->event_channotice($user, $channel, $message);
780 $targetUser = P10_User::getUserByNum($targetStr);
781 $this->eventHandler->event_privnotice($user, $targetUser, $message);
784 if(($this->flags & self::FLAG_CONNECTED))
785 $this->send("O", $user->getNumeric(), $targetStr, $message);
788 public function mode($user, $target, $modes, $force = false) {
789 if(!is_a($user, "P10_User") || !($user->getServer() === $this->server))
790 return ERR_INVALID_USER;
791 if(!is_a($target, "P10_User") && !is_a($target, "P10_Channel") && !(is_string($target) && ($target[0] == "#" || P10_User::getUserByNick($target))))
792 return ERR_INVALID_USER;
793 if(is_a($target, "P10_Channel"))
794 $targetStr = $target->getName();
795 else if(is_a($target, "P10_User"))
796 $targetStr = $target->getNumeric();
797 else if($target[0] == "#")
798 $targetStr = $target;
800 $targetStr = P10_User::getUserByNick($target)->getNumeric();
802 if($targetStr[0] == "#") {
803 $channel = P10_Channel::getChannelByName($targetStr);
805 $channel = new P10_Channel($targetStr);
806 $modes = $channel->getModes()->setModes($modes);
807 if(($this->flags & self::FLAG_CONNECTED))
808 $this->send(($force ? "OM" : "M"), $user->getNumeric(), $targetStr, $modes);
809 if($this->eventHandler)
810 $this->eventHandler->event_chanmode(($force ? $this->server : $user), $channel, $modes);
812 $targetUser = P10_User::getUserByNum($targetStr);
813 if($targetUser->getServer() === $this->server) {
815 $modes = $targetUser->getModes()->setModes($modes);
816 if(($this->flags & self::FLAG_CONNECTED))
817 $this->send("M", $targetUser->getNumeric(), $targetUser->getNick(), $modes);
818 if($this->eventHandler)
819 $this->eventHandler->event_usermode($targetUser, $modes);
822 if(($this->flags & self::FLAG_CONNECTED))
823 $this->send("SM", $user->getNumeric(), $targetUser->getNumeric(), $modes);