added scripting interfaces (addUser, delUser, join, part, kick, privmsg, notice,...
[PHP-P10.git] / Uplink / Uplink.class.php
1 <?php
2 /********************************* PHP-P10 ******************************
3  *    P10 uplink class by pk910   (c)2011 pk910                         *
4  ************************************************************************
5  *                          Version 2 (OOP)                             *
6  *                                                                      *
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.                                  *
11  *                                                                      *
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.                         *
16  *                                                                      *
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.       *
20  *                                                                      *
21  ************************************************************************
22  * 
23  *  Uplink/Uplink.class.php
24  *
25  * This file contains the basic P10 Protocol handler.
26  *
27  ************************************************************************
28  * accessable methods:
29  *
30  * void initialize()
31  *     has to be called after the settings have been set.
32  *
33  * void loop() 
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.
37  *
38  * void setUplinkHost(String $host, int $port, bool $ssl = false, String $bind = null)
39  *     sets the Uplink connection information.
40  *
41  * void setLoopTimeout(int $timeout)
42  *     sets the maximum time loop() is blocking the script execution.
43  *
44  * void setUplinkServer(int $numeric, String $name, String $password, String $description)
45  *     sets the own P10 Server information.
46  *
47  * void setValidateServer(String $name, String $password)
48  *     sets additional security relevant information about the remote server.
49  *
50  * void setEventHandler(EventHandler $event_handler)
51  *     sets the EventHandlder
52  */
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");
61
62 $e=1;
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++);
68
69 class Uplink {
70         private $client;
71         private $settings = array();
72         private $server;
73         private $eventHandler = null;
74         private $last_local_numeric = 1;
75         
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
82         private $flags = 0;
83         
84         public function __construct() {
85                 $this->client = new Client();
86                 $this->setSetting("recv_timeout", 1000);
87         }
88         
89         public function initialize() {
90                 if($this->server) {
91                         trigger_error("Uplink already initialized.", E_USER_ERROR);
92                         return;
93                 }
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);
99                         return;
100                 }
101                 $this->server = new P10_Server($self_name, $self_numeric, null, time(), time(), $self_description);
102         }
103         
104         public function loop() {
105                 if($this->server == null) {
106                         trigger_error("Uplink not initialized.", E_USER_ERROR);
107                         return;
108                 }
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;
114                         }
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);
119                                 return;
120                         }
121                         if(($this->flags & self::FLAG_SECURITY_QUIT) || ($this->flags & self::FLAG_NOT_CONNECTABLE)) {
122                                 sleep(1);
123                         }
124                         $state = $this->client->connect($host, $port, $this->getSetting("bind"), $this->getSetting("ssl"), $this->getSetting("recv_timeout"));
125                         if(!$state) {
126                                 usleep($this->getSetting("recv_timeout") / 1000);
127                                 $this->flags |= self::FLAG_NOT_CONNECTABLE;
128                                 return;
129                         }
130                         $this->flags = 0;
131                         $this->loginServer();
132                 }
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);
138                 }
139         }
140         
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);
146         }
147         
148         public function setLoopTimeout($timeout) {
149                 $this->setSetting("recv_timeout", $timeout);
150         }
151         
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);
157         }
158         
159         public function setValidateServer($name, $password) {
160                 $this->setSetting("their_name", $name);
161                 $this->setSetting("their_password", $password);
162         }
163         
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);
167                         return;
168                 }
169                 $this->eventHandler = $event_handler;
170         }
171         
172         private function setSetting($setting, $value) {
173                 $this->settings[$setting] = $value;
174         }
175         
176         private function getSetting($setting) {
177                 if(array_key_exists($setting, $this->settings)) {
178                         return $this->settings[$setting];
179                 } else {
180                         return null;
181                 }
182         }
183         
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());
188         }
189         
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];
197                 else $from = null;
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);
201                 }
202                 switch(strtoupper($tokens[$cmdPos])) {
203                 //pre P10 Session
204                         case "PASS":
205                                 $this->recv_pass($from, $arguments);
206                                 break;
207                         case "SERVER":
208                                 $this->recv_server($from, $arguments);
209                                 break;
210                         case "ERROR":
211                                 $this->recv_error($from, $arguments);
212                                 break;
213                 //P10 Session
214                         case "S":
215                                 $this->recv_server($from, $arguments);
216                                 break;
217                         case "G":
218                                 $this->recv_ping($from, $arguments);
219                                 break;
220                         case "N":
221                                 $this->recv_nick($from, $arguments);
222                                 break;
223                         case "EB":
224                                 $this->recv_end_of_burst($from, $arguments);
225                                 break;
226                         case "EA":
227                                 $this->recv_end_of_burst_ack($from, $arguments);
228                                 break;
229                         case "SQ":
230                                 $this->recv_server_quit($from, $arguments);
231                                 break;
232                         case "Q":
233                                 $this->recv_quit($from, $arguments);
234                                 break;
235                         case "B":
236                                 $this->recv_burst($from, $arguments);
237                                 break;
238                         case "J":
239                         case "C":
240                                 $this->recv_join($from, $arguments);
241                                 break;
242                         case "L":
243                                 $this->recv_part($from, $arguments);
244                                 break;
245                         case "K":
246                                 $this->recv_kick($from, $arguments);
247                                 break;
248                         case "D":
249                                 $this->recv_kill($from, $arguments);
250                                 break;
251                         case "P":
252                                 $this->recv_privmsg($from, $arguments);
253                                 break;
254                         case "O":
255                                 $this->recv_notice($from, $arguments);
256                                 break;
257                 //default
258                         default:
259                                 //unknown cmd
260                                 if($this->eventHandler)
261                                         $this->eventHandler->event_unknown_cmd($from, strtoupper($tokens[$cmdPos]), $arguments);
262                                 break;
263                 }
264         }
265         
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);
270                 } else {
271                         $command = P10Formatter::formatCMD($this->getSetting("numeric"), $command, array());
272                 }
273                 $this->client->send($command);
274         }
275         
276         /********************************************************************************************
277          *                                     INCOMING COMMANDS                                    *
278          ********************************************************************************************/
279         
280         private function recv_pass($from, $args) {
281                 $their_pass = $this->getSetting("their_password");
282                 if($their_pass) {
283                         if($args[0] != $their_pass) {
284                                 //security QUIT
285                                 $this->flags |= self::FLAG_SECURITY_QUIT;
286                                 $this->send("ERROR", "Incorrect password received.");
287                                 $this->client->disconnect();
288                                 return;
289                         }
290                         $this->flags |= self::FLAG_GOT_PASS;
291                 }
292         }
293         
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();
298         }
299         
300         private function recv_server($from, $args) {
301                 if($from == null) {
302                         //our uplink Server
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();
308                                 return;
309                         }
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();
314                                 return;
315                         }
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));
321                 } else {
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);
326                                 return;
327                         }
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));
332                 }
333         }
334         
335         private function recv_ping($from, $args) {
336                 $this->send("Z", $args[0]); //simply PONG
337         }
338         
339         private function recv_nick($from, $args) {
340                 if(count($args) <= 2) {
341                         //Nick change
342                         $user = P10_User::getUserByNum($from);
343                         if($user == null) {
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);
345                                 return;
346                         }
347                         if($this->eventHandler)
348                                 $this->eventHandler->event_nick($user, $args[0]);
349                         $user->setNick($args[0]);
350                 } else {
351                         //New User
352                         $numeric = $args[count($args)-2];
353                         $nick = $args[0];
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);
357                                 return;
358                         }
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);
361                         }
362                         $connect_time = $args[2];
363                         $ident = $args[3];
364                         $host = $args[4];
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));
372                 }
373         }
374         
375         private function recv_end_of_burst($from, $args) {
376                 if(($this->flags & self::FLAG_BURST_PENDING)) {
377                         $this->burst();
378                         $this->send("EA");
379                         $this->flags &= ~self::FLAG_BURST_PENDING;
380                 }
381         }
382         
383         private function recv_end_of_burst_ack($from, $args) {
384                 $this->flags |= self::FLAG_CONNECTED;
385         }
386         
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);
391                         return;
392                 }
393                 $server->disconnectServer($this->eventHandler);
394         }
395         
396         private function recv_quit($from, $args) {
397                 $user = P10_User::getUserByNum($from);
398                 if($user == null) {
399                         trigger_error("Server tries to quit an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
400                         return;
401                 }
402                 if($this->eventHandler)
403                         $this->eventHandler->event_quit($user, $args[0]);
404                 $user->quit($args[0]);
405         }
406         
407         private function recv_burst($from, $args) {
408                 $name = $args[0];
409                 $create_time = $args[1];
410                 $channel = P10_Channel::getChannelByName($name);
411                 if($channel == null)
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] == "%") {
418                         //ban list
419                         $banlist = explode(" ", substr($userstr, 1));
420                         foreach($banlist as $ban) {
421                                 //TODO: save bans
422                         }
423                         $userstr = $args[count($args)-2];
424                         $modeparamcount--;
425                 }
426                 if($userstr[0] == "+") { //MODE String
427                         $modeparamcount++;
428                         $userstr = "";
429                 }
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);
436                                 return;
437                         }
438                         if(count($uexp) > 1) {
439                                 $isop = false;
440                                 $isvoice = false;
441                                 for($i = 0; $i < strlen($uexp[1]); $i++) {
442                                         if($uexp[1][0] == "@") $isop = true;
443                                         if($uexp[1][0] == "+") $isvoice = true;
444                                 }
445                         }
446                         $user = P10_User::getUserByNum($uexp[0]);
447                         if($user == null) {
448                                 trigger_error("burst parse error: cant find User '".$uexp[0]."'.", E_USER_ERROR);
449                                 return;
450                         }
451                         $channel->burstUser($user, $isop, $isvoice);
452                         if($this->eventHandler)
453                                 $this->eventHandler->event_join($user, $channel, true);
454                 }
455                 $modes->parseModes(implode(array_slice($args, 2, $modeparamcount)));
456         }
457         
458         private function recv_join($from, $args) {
459                 $user = P10_User::getUserByNum($from);
460                 if($user == null) {
461                         trigger_error("Server tries to join an user that does not exist or was not found on recv_join.", E_USER_ERROR);
462                         return;
463                 }
464                 $channel = P10_Channel::getChannelByName($args[0]);
465                 if($channel == null)
466                         $channel = new P10_Channel($args[0]);
467                 $channel->joinUser($user);
468                 if($this->eventHandler)
469                         $this->eventHandler->event_join($user, $channel, false);
470         }
471         
472         private function recv_part($from, $args) {
473                 $user = P10_User::getUserByNum($from);
474                 if($user == null) {
475                         trigger_error("Server tries to part an user that does not exist or was not found on recv_join.", E_USER_ERROR);
476                         return;
477                 }
478                 $channel = P10_Channel::getChannelByName($args[0]);
479                 if($channel == null)
480                         $channel = new P10_Channel($args[0]);
481                 if($this->eventHandler)
482                         $this->eventHandler->event_part($user, $channel, $args[1]);
483                 $channel->partUser($user);
484         }
485         
486         private function recv_kick($from, $args) {
487                 $user = P10_User::getUserByNum($from);
488                 if($user == null) {
489                         trigger_error("An unknown user tries to kick another user on recv_kick.", E_USER_ERROR);
490                         return;
491                 }
492                 $channel = P10_Channel::getChannelByName($args[0]);
493                 if($channel == null)
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);
498                         return;
499                 }
500                 if($this->eventHandler)
501                         $this->eventHandler->event_kick($user, $target, $channel, $args[1]);
502                 $channel->partUser($user);
503         }
504         
505         private function recv_kill($from, $args) {
506                 $user = P10_User::getUserByNum($args[0]);
507                 if($user == null) {
508                         trigger_error("Server tries to kill an user that does not exist or was not found on recv_quit.", E_USER_ERROR);
509                         return;
510                 }
511                 if($this->eventHandler)
512                         $this->eventHandler->event_quit($user, "Killed (".$args[1].")");
513                 $user->quit($args[1]);
514         }
515         
516         private function recv_privmsg($from, $args) {
517                 $user = P10_User::getUserByNum($from);
518                 if($user == null) {
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);
520                         return;
521                 }
522                 if($this->eventHandler) {
523                         if($args[0] == "#") {
524                                 $channel = P10_Channel::getChannelByName($args[0]);
525                                 if($channel == null)
526                                         $channel = new P10_Channel($args[0]);
527                                 $this->eventHandler->event_chanmessage($user, $channel, $args[1]);
528                         } else {
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);
532                                         return;
533                                 }
534                                 $this->eventHandler->event_privmessage($user, $targetUser, $args[1]);
535                         }
536                 }
537         }
538         
539         private function recv_notice($from, $args) {
540                 $user = P10_User::getUserByNum($from);
541                 if($user == null) {
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);
543                         return;
544                 }
545                 if($this->eventHandler) {
546                         if($args[0] == "#") {
547                                 $channel = P10_Channel::getChannelByName($args[0]);
548                                 if($channel == null)
549                                         $channel = new P10_Channel($args[0]);
550                                 $this->eventHandler->event_channotice($user, $channel, $args[1]);
551                         } else {
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);
555                                         return;
556                                 }
557                                 $this->eventHandler->event_privnotice($user, $targetUser, $args[1]);
558                         }
559                 }
560         }
561         
562         /********************************************************************************************
563          *                                     SERVER FUNCTIONS                                     *
564          ********************************************************************************************/
565         
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);
577                 }
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);
584                                 $strPrivs = "";
585                                 if(($privs & P10_Channel::USERPRIV_OPED)) $strPrivs .= "o";
586                                 if(($privs & P10_Channel::USERPRIV_VOICE)) $strPrivs .= "v";
587                                 if($strPrivs == "") $strPrivs = "-";
588                                 $local_users = true;
589                                 $sorted_users[$strPrivs][] = $user;
590                         }
591                         if(!$local_users && !$channel->getModes()->hasMode("z")) continue;
592                         $userStr = "";
593                         foreach($sorted_users['-'] as $user) {
594                                 if($userStr != "") $userStr.=",";
595                                 $userStr .= $user->getNumeric();
596                         }
597                         foreach($sorted_users['ov'] as $i => $user) {
598                                 if($userStr != "") $userStr.=",";
599                                 $userStr .= $user->getNumeric();
600                                 if($i == 0) $userStr .= ":ov";
601                         }
602                         foreach($sorted_users['o'] as $i => $user) {
603                                 if($userStr != "") $userStr.=",";
604                                 $userStr .= $user->getNumeric();
605                                 if($i == 0) $userStr .= ":o";
606                         }
607                         foreach($sorted_users['v'] as $i => $user) {
608                                 if($userStr != "") $userStr.=",";
609                                 $userStr .= $user->getNumeric();
610                                 if($i == 0) $userStr .= ":v";
611                         }
612                         $banString = "";
613                         //TODO: Build ban String
614                         $burstString = "";
615                         $modeString = $channel->getModes()->getModeString();
616                         if($modeString != "+") {
617                                 $burstString .= $modeString;
618                         }
619                         if($userStr != "") {
620                                 if($burstString != "") $burstString .= " ";
621                                 $burstString .= $userStr;
622                         }
623                         if($banString != "") {
624                                 if($burstString != "") $burstString .= " ";
625                                 $burstString .= ":%".$banString;
626                         }
627                         $this->send("B", $channel->getName(), $channel->getCreateTime(), $burstString);
628                 }
629                 $this->send("EB");
630         }
631         
632         /********************************************************************************************
633          *                                    LOCAL USER FUNCTIONS                                  *
634          ********************************************************************************************/
635         
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);
643                 }
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);
649                 }
650                 return $user;
651         }
652         
653         public function delUser($user, $reason) {
654                 if(!is_a($user, "P10_User")) return ERR_INVALID_USER;
655                 if($user->getServer() === $this->server) {
656                         //local user (QUIT)
657                         $user->quit($reason);
658                         if(($this->flags & self::FLAG_CONNECTED)) 
659                                 $this->send("Q", $user->getNumeric(), $reason);
660                 } else {
661                         //remote user (KILL)
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'));
668
669                         $this->send("D", $user->getNumeric(), $name, $reason);
670                 }
671         }
672         
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);
679                 if($channel == null)
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);
686         }
687         
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;
695                 else
696                         $channel = P10_Channel::getChannelByName($chanName);
697                 if($channel == null)
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);
706         }
707         
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;
717                 else
718                         $channel = P10_Channel::getChannelByName($chanName);
719                 if($channel == null)
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);
728         }
729         
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;
741                 else
742                         $targetStr = P10_User::getUserByNick($target)->getNumeric();
743                 
744                 if($this->eventHandler) {
745                         if($targetStr[0] == "#") {
746                                 $channel = P10_Channel::getChannelByName($targetStr);
747                                 if($channel == null)
748                                         $channel = new P10_Channel($targetStr);
749                                 $this->eventHandler->event_chanmessage($user, $channel, $message);
750                         } else {
751                                 $targetUser = P10_User::getUserByNum($targetStr);
752                                 $this->eventHandler->event_privmessage($user, $targetUser, $message);
753                         }
754                 }
755                 if(($this->flags & self::FLAG_CONNECTED))
756                         $this->send("P", $user->getNumeric(), $targetStr, $message);
757         }
758         
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;
770                 else
771                         $targetStr = P10_User::getUserByNick($target)->getNumeric();
772                 
773                 if($this->eventHandler) {
774                         if($targetStr[0] == "#") {
775                                 $channel = P10_Channel::getChannelByName($targetStr);
776                                 if($channel == null)
777                                         $channel = new P10_Channel($targetStr);
778                                 $this->eventHandler->event_channotice($user, $channel, $message);
779                         } else {
780                                 $targetUser = P10_User::getUserByNum($targetStr);
781                                 $this->eventHandler->event_privnotice($user, $targetUser, $message);
782                         }
783                 }
784                 if(($this->flags & self::FLAG_CONNECTED))
785                         $this->send("O", $user->getNumeric(), $targetStr, $message);
786         }
787         
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;
799                 else
800                         $targetStr = P10_User::getUserByNick($target)->getNumeric();
801                 
802                 if($targetStr[0] == "#") {
803                         $channel = P10_Channel::getChannelByName($targetStr);
804                         if($channel == null)
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);
811                 } else {
812                         $targetUser = P10_User::getUserByNum($targetStr);
813                         if($targetUser->getServer() === $this->server) {
814                                 //just do it :D
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);
820                         } else {
821                                 //SVSMODE
822                                 if(($this->flags & self::FLAG_CONNECTED))
823                                         $this->send("SM", $user->getNumeric(), $targetUser->getNumeric(), $modes);
824                         }
825                 }
826         }
827         
828         
829         
830 }
831
832 ?>