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");
64 private $settings = array();
66 private $eventHandler = null;
68 const FLAG_P10SESSION = 0x0001; //connection is in P10 mode (server is connected)
69 const FLAG_SECURITY_QUIT = 0x0002; //local connection abort because of security issues
70 const FLAG_NOT_CONNECTABLE = 0x0004; //remote server is not connectable
71 const FLAG_BURST_PENDING = 0x0008; //we still have to burst
72 const FLAG_CONNECTED = 0x0010; //connected and synced (ready)
73 const FLAG_GOT_PASS = 0x0020; //got PASS from the remote Server
76 public function __construct() {
77 $this->client = new Client();
78 $this->setSetting("recv_timeout", 1000);
81 public function initialize() {
83 trigger_error("Uplink already initialized.", E_USER_ERROR);
86 $self_numeric = $this->getSetting("numeric");
87 $self_name = $this->getSetting("name");
88 $self_description = $this->getSetting("description");
89 if(!$self_numeric || !$self_name) {
90 trigger_error("Server Settings missing.", E_USER_ERROR);
93 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
96 public function loop() {
97 if($this->server == null) {
98 trigger_error("Uplink not initialized.", E_USER_ERROR);
101 if(!$this->client->connected()) {
102 if(($this->flags & self::FLAG_P10SESSION)) {
103 //Server got disconnected
104 $this->server->disconnectServer($this->eventHandler, true);
105 $this->flags &= ~self::FLAG_P10SESSION;
107 $host = $this->getSetting("host");
108 $port = $this->getSetting("port");
109 if($host == null || $port == null) {
110 trigger_error("Uplink Settings missing.", E_USER_ERROR);
113 if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
116 $state = $this->client->connect($host, $port, $this->getSetting("bind"), $this->getSetting("ssl"), $this->getSetting("recv_timeout"));
118 usleep($this->getSetting("recv_timeout") / 1000);
119 $this->flags |= self::FLAG_NOT_CONNECTABLE;
123 $this->loginServer();
125 //try to receive new data from the Uplink
126 $lines = $this->client->recv();
127 if($lines == null) return;
128 foreach($lines as $line) {
129 $this->parseLine($line);
133 public function setUplinkHost($host, $port, $ssl = false, $bind = null) {
134 $this->setSetting("host", $host);
135 $this->setSetting("port", $port);
136 $this->setSetting("ssl", $ssl);
137 $this->setSetting("bind", $bind);
140 public function setLoopTimeout($timeout) {
141 $this->setSetting("recv_timeout", $timeout);
144 public function setUplinkServer($numeric, $name, $password, $description) {
145 $this->setSetting("numeric", Numerics::intToNum($numeric, 2));
146 $this->setSetting("name", $name);
147 $this->setSetting("password", $password);
148 $this->setSetting("description", $description);
151 public function setValidateServer($name, $password) {
152 $this->setSetting("their_name", $name);
153 $this->setSetting("their_password", $password);
156 public function setEventHandler($event_handler) {
157 if(!is_a($event_handler, "EventHandler")) {
158 trigger_error((is_object($event_handler) ? get_class($event_handler) : gettype($event_handler))." is NOT a valid EventHandler.", E_USER_ERROR);
161 $this->eventHandler = $event_handler;
164 private function setSetting($setting, $value) {
165 $this->settings[$setting] = $value;
168 private function getSetting($setting) {
169 if(array_key_exists($setting, $this->settings)) {
170 return $this->settings[$setting];
176 private function loginServer() {
177 $password = $this->getSetting("password");
178 $this->send("PASS", $password);
179 $this->send("SERVER", $this->server->getName(), $this->server->getStartTime(), $this->server->getLinkTime(), $this->server->getNumeric(), $this->server->getDescription());
182 private function parseLine($line) {
183 $highExplode = explode(" :", $line, 2);
184 $tokens = explode(" ", $highExplode[0]);
185 if(count($highExplode) > 1)
186 $tokens[] = $highExplode[1];
187 $cmdPos = (($this->flags & self::FLAG_P10SESSION) ? 1 : 0);
188 if($cmdPos == 1) $from = $tokens[0];
190 $arguments = array_slice($tokens, $cmdPos + 1);
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);
241 private function send($command) {
242 if(func_num_args() > 1) {
243 $args = array_slice(func_get_args(), 1);
244 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, $args);
246 $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
248 $this->client->send($command);
251 /********************************************************************************************
252 * INCOMING COMMANDS *
253 ********************************************************************************************/
255 private function recv_pass($from, $args) {
256 $their_pass = $this->getSetting("their_password");
258 if($args[0] != $their_pass) {
260 $this->flags |= self::FLAG_SECURITY_QUIT;
261 $this->send("ERROR", "Incorrect password received.");
262 $this->client->disconnect();
265 $this->flags |= self::FLAG_GOT_PASS;
269 private function recv_error($from, $args) {
270 //we received an error - the socket is dead for us, now
271 //maybe we want to log this, later
272 $this->client->disconnect();
275 private function recv_server($from, $args) {
278 $their_name = $this->getSetting("their_name");
279 if($their_name && $args[0] != $their_name) {
280 $this->flags |= self::FLAG_SECURITY_QUIT;
281 $this->send("ERROR", "Invalid Server name");
282 $this->client->disconnect();
285 if($this->getSetting("their_password") && !($this->flags & self::FLAG_GOT_PASS)) {
286 $this->flags |= self::FLAG_SECURITY_QUIT;
287 $this->send("ERROR", "PASS missing.");
288 $this->client->disconnect();
291 $new_server = new P10_Server($args[0], substr($args[5],0,2), $this->server, $args[2], $args[3], $args[7]);
292 $this->server->addServer($new_server);
293 $this->flags |= self::FLAG_P10SESSION | self::FLAG_BURST_PENDING;
294 if($this->eventHandler)
295 $this->eventHandler->event_server($new_server, !($this->flags & self::FLAG_CONNECTED));
297 //another server got a new slave server ^^
298 $server = P10_Server::getServerByNum($from);
299 if($server == null) {
300 trigger_error("Parent Server (".$from.") does not exist or was not found on recv_server.", E_USER_ERROR);
303 $new_server = new P10_Server($args[0], substr($args[5],0,2), $server, $args[2], $args[3], $args[7]);
304 $server->addServer($new_server);
305 if($this->eventHandler)
306 $this->eventHandler->event_server($new_server, !($this->flags & self::FLAG_CONNECTED));
310 private function recv_ping($from, $args) {
311 $this->send("Z", $args[0]); //simply PONG
314 private function recv_nick($from, $args) {
315 if(count($args) <= 2) {
317 $user = P10_User::getUserByNum($from);
319 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);
322 if($this->eventHandler)
323 $this->eventHandler->event_nick($user, $args[0]);
324 $user->setNick($args[0]);
327 $numeric = $args[count($args)-2];
329 $server = P10_Server::getServerByNum($from);
330 if($server == null) {
331 trigger_error("Server (".$from.") the User (".$nick.") is coming from does not exist or was not found on recv_nick.", E_USER_ERROR);
334 if(substr($numeric,0,2) != $from) {
335 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);
337 $connect_time = $args[2];
340 $modes = implode(" ",array_slice($args, 5, count($args)-8));
341 $modes = new P10_UserModeSet($modes);
342 $ip = Numerics::parseIP($args[count($args)-3]);
343 $realname = $args[count($args)-1];
344 $user = new P10_User($nick, $numeric, $server, $connect_time, $ident, $host, $ip, $realname, $modes);
345 if($this->eventHandler)
346 $this->eventHandler->event_connect($user, !($this->flags & self::FLAG_CONNECTED));
350 private function recv_end_of_burst($from, $args) {
351 if(($this->flags & self::FLAG_BURST_PENDING)) {
354 $this->flags &= ~self::FLAG_BURST_PENDING;
358 private function recv_end_of_burst_ack($from, $args) {
359 $this->flags |= self::FLAG_CONNECTED;
362 private function recv_server_quit($from, $args) {
363 $server = P10_Server::getServerByName($args[0]);
364 if($server == null) {
365 trigger_error("Server (".$args[0].") not found.", E_USER_ERROR);
368 $server->disconnectServer($this->eventHandler);
371 private function recv_quit($from, $args) {
372 $user = P10_User::getUserByNum($from);
374 trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
377 if($this->eventHandler)
378 $this->eventHandler->event_quit($user, $args[1]);
379 $user->quit($args[0]);
382 private function recv_burst($from, $args) {
384 $create_time = $args[1];
385 $channel = P10_Channel::getChannelByName($name);
387 $channel = new P10_Channel($name);
388 $channel->setCreateTime($create_time);
389 $modes = $channel->getModes();
390 $userstr = $args[count($args)-1];
391 $modeparamcount = count($args)-3;
392 if($userstr[0] == "%") {
394 $banlist = explode(" ", substr($userstr, 1));
395 foreach($banlist as $ban) {
398 $userstr = $args[count($args)-2];
401 if($userstr[0] == "+") { //MODE String
405 $users = explode(",",$userstr);
406 $isop = false; $isvoice = false;
407 foreach($users as $user) {
408 $uexp = explode(":", $user);
409 if(strlen($uexp[0]) != 5) {
410 trigger_error("burst parse error: '".$uexp[0]."' is not an user numeric.", E_USER_ERROR);
413 if(count($uexp) > 1) {
416 for($i = 0; $i < strlen($uexp[1]); $i++) {
417 if($uexp[1][0] == "@") $isop = true;
418 if($uexp[1][0] == "+") $isvoice = true;
421 $user = P10_User::getUserByNum($uexp[0]);
423 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
426 $channel->burstUser($user, $isop, $isvoice);
427 if($this->eventHandler)
428 $this->eventHandler->event_join($user, $channel, true);
430 $modes->parseModes(implode(array_slice($args, 2, $modeparamcount)));
433 private function recv_join($from, $args) {
434 $user = P10_User::getUserByNum($from);
436 trigger_error("Server tries to join an user that does not exist or was not found on recv_join.", E_USER_ERROR);
439 $channel = P10_Channel::getChannelByName($args[0]);
441 $channel = new P10_Channel($args[0]);
442 $channel->joinUser($user);
443 if($this->eventHandler)
444 $this->eventHandler->event_join($user, $channel, false);
447 private function recv_part($from, $args) {
448 $user = P10_User::getUserByNum($from);
450 trigger_error("Server tries to part an user that does not exist or was not found on recv_join.", E_USER_ERROR);
453 $channel = P10_Channel::getChannelByName($args[0]);
455 $channel = new P10_Channel($args[0]);
456 if($this->eventHandler)
457 $this->eventHandler->event_part($user, $channel, $args[1]);
458 $channel->partUser($user);
461 /********************************************************************************************
463 ********************************************************************************************/
465 private function burst() {
466 foreach($this->server->getUsers() as $user) {
467 $nick = $user->getNick();
468 $connect_time = $user->getConnectTime();
469 $ident = $user->getIdent();
470 $host = $user->getHost();
471 $modes = $user->getModes()->getModeString();
472 $ip = Numerics::numericFromIP($user->getIP());
473 $numeric = $user->getNumeric();
474 $realname = $user->getRealname();
475 $this->send("N", $nick, $connect_time, $ident, $host, $modes, $ip, $numeric, $realname);
477 foreach(P10_Channel::getChannels() as $channel) {
478 $sorted_users = array('ov' => array(), 'o' => array(), 'v' => array(), '-' => array());
479 $local_users = false;
480 foreach($channel->getUsers() as $user) {
481 if(substr($user->getNumeric(), 0, 2) != $this->server->getNumeric()) continue; //skip users that are not on the local server
482 $privs = $channel->getUserPrivs($user);
484 if(($privs & P10_Channel::USERPRIV_OPED)) $strPrivs .= "o";
485 if(($privs & P10_Channel::USERPRIV_VOICE)) $strPrivs .= "v";
486 if($strPrivs == "") $strPrivs = "-";
488 $sorted_users[$strPrivs][] = $user;
490 if(!$local_users) continue;
492 foreach($sorted_users['-'] as $user) {
493 if($userStr != "") $userStr.=",";
494 $userStr .= $user->getNumeric();
496 foreach($sorted_users['ov'] as $i => $user) {
497 if($userStr != "") $userStr.=",";
498 $userStr .= $user->getNumeric();
499 if($i == 0) $userStr .= ":ov";
501 foreach($sorted_users['o'] as $i => $user) {
502 if($userStr != "") $userStr.=",";
503 $userStr .= $user->getNumeric();
504 if($i == 0) $userStr .= ":o";
506 foreach($sorted_users['v'] as $i => $user) {
507 if($userStr != "") $userStr.=",";
508 $userStr .= $user->getNumeric();
509 if($i == 0) $userStr .= ":v";
512 //TODO: Build ban String
514 $modeString = $channel->getModes()->getModeString();
515 if($modeString != "+") {
516 $burstString .= $modeString;
519 if($burstString != "") $burstString .= " ";
520 $burstString .= $userStr;
522 if($banString != "") {
523 if($burstString != "") $burstString .= " ";
524 $burstString .= ":%".$banString;
526 $this->send("B", $channel->getName(), $channel->getCreateTime(), $burstString);