7b73d40f99f285a5e9da7951ccc6b88be96061cb
[ngircd-alex.git] / src / ngircd / irc-login.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * Please read the file COPYING, README and AUTHORS for more information.
10  *
11  * Login and logout
12  */
13
14
15 #include "portab.h"
16
17 #include "imp.h"
18 #include <assert.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <strings.h>
23
24 #include "ngircd.h"
25 #include "resolve.h"
26 #include "conn-func.h"
27 #include "conf.h"
28 #include "client.h"
29 #include "channel.h"
30 #include "log.h"
31 #include "messages.h"
32 #include "parse.h"
33 #include "irc.h"
34 #include "irc-info.h"
35 #include "irc-write.h"
36
37 #include "exp.h"
38 #include "irc-login.h"
39
40
41 static bool Hello_User PARAMS(( CLIENT *Client ));
42 static void Kill_Nick PARAMS(( char *Nick, char *Reason ));
43 static void Introduce_Client PARAMS((CLIENT *To, CLIENT *Client, int Type));
44 static void cb_introduceClient PARAMS((CLIENT *Client, CLIENT *Prefix,
45                                        void *i));
46
47
48 /**
49  * Handler for the IRC command "PASS".
50  * See RFC 2813 section 4.1.1, and RFC 2812 section 3.1.1.
51  */
52 GLOBAL bool
53 IRC_PASS( CLIENT *Client, REQUEST *Req )
54 {
55         char *type, *orig_flags;
56         int protohigh, protolow;
57
58         assert( Client != NULL );
59         assert( Req != NULL );
60
61         /* Return an error if this is not a local client */
62         if (Client_Conn(Client) <= NONE)
63                 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
64                                           Client_ID(Client), Req->command);
65
66         if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
67                 /* Not yet registered "unknown" connection, PASS with one
68                  * argument: either a regular client, service, or server
69                  * using the old RFC 1459 section 4.1.1 syntax. */
70                 LogDebug("Connection %d: got PASS command (RFC 1459) ...",
71                          Client_Conn(Client));
72         } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
73                     Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
74                    (Req->argc == 3 || Req->argc == 4)) {
75                 /* Not yet registered "unknown" connection or outgoing server
76                  * link, PASS with three or four argument: server using the
77                  * RFC 2813 section 4.1.1 syntax. */
78                 LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
79                          Client_Conn(Client));
80         } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
81                    Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
82                 /* Unregistered connection, but wrong number of arguments: */
83                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
84                                           Client_ID(Client), Req->command);
85         } else {
86                 /* Registered connection, PASS command is not allowed! */
87                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
88                                           Client_ID(Client));
89         }
90
91         Client_SetPassword(Client, Req->argv[0]);
92
93         /* Protocol version */
94         if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
95                 int c2, c4;
96
97                 c2 = Req->argv[1][2];
98                 c4 = Req->argv[1][4];
99
100                 Req->argv[1][4] = '\0';
101                 protolow = atoi(&Req->argv[1][2]);
102                 Req->argv[1][2] = '\0';
103                 protohigh = atoi(Req->argv[1]);
104
105                 Req->argv[1][2] = c2;
106                 Req->argv[1][4] = c4;
107
108                 Client_SetType(Client, CLIENT_GOTPASS_2813);
109         } else {
110                 protohigh = protolow = 0;
111                 Client_SetType(Client, CLIENT_GOTPASS);
112         }
113
114         /* Protocol type, see doc/Protocol.txt */
115         if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
116                 type = &Req->argv[1][4];
117         else
118                 type = NULL;
119
120         /* Protocol flags/options */
121         if (Req->argc >= 4)
122                 orig_flags = Req->argv[3];
123         else
124                 orig_flags = "";
125
126         /* Implementation, version and IRC+ flags */
127         if (Req->argc >= 3) {
128                 char *impl, *ptr, *serverver, *flags;
129
130                 impl = Req->argv[2];
131                 ptr = strchr(impl, '|');
132                 if (ptr)
133                         *ptr = '\0';
134
135                 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
136                         /* The peer seems to be a server which supports the
137                          * IRC+ protocol (see doc/Protocol.txt). */
138                         serverver = ptr + 1;
139                         flags = strchr(serverver, ':');
140                         if (flags) {
141                                 *flags = '\0';
142                                 flags++;
143                         } else
144                                 flags = "";
145                         Log(LOG_INFO,
146                             "Peer announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
147                             impl, serverver, protohigh, protolow, flags);
148                 } else {
149                         /* The peer seems to be a server supporting the
150                          * "original" IRC protocol (RFC 2813). */
151                         if (strchr(orig_flags, 'Z'))
152                                 flags = "Z";
153                         else
154                                 flags = "";
155                         Log(LOG_INFO,
156                             "Peer announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
157                             impl, protohigh, protolow, flags);
158                 }
159                 Client_SetFlags(Client, flags);
160         }
161
162         return CONNECTED;
163 } /* IRC_PASS */
164
165
166 /**
167  * IRC "NICK" command.
168  * This function implements the IRC command "NICK" which is used to register
169  * with the server, to change already registered nicknames and to introduce
170  * new users which are connected to other servers.
171  */
172 GLOBAL bool
173 IRC_NICK( CLIENT *Client, REQUEST *Req )
174 {
175         CLIENT *intr_c, *target, *c;
176         char *nick, *user, *hostname, *modes, *info;
177         int token, hops;
178
179         assert( Client != NULL );
180         assert( Req != NULL );
181
182         /* Some IRC clients, for example BitchX, send the NICK and USER
183          * commands in the wrong order ... */
184         if(Client_Type(Client) == CLIENT_UNKNOWN
185             || Client_Type(Client) == CLIENT_GOTPASS
186             || Client_Type(Client) == CLIENT_GOTNICK
187 #ifndef STRICT_RFC
188             || Client_Type(Client) == CLIENT_GOTUSER
189 #endif
190             || Client_Type(Client) == CLIENT_USER
191             || Client_Type(Client) == CLIENT_SERVICE
192             || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
193         {
194                 /* User registration or change of nickname */
195
196                 /* Wrong number of arguments? */
197                 if( Req->argc != 1 )
198                         return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG,
199                                                    Client_ID( Client ),
200                                                    Req->command );
201
202                 /* Search "target" client */
203                 if( Client_Type( Client ) == CLIENT_SERVER )
204                 {
205                         target = Client_Search( Req->prefix );
206                         if( ! target )
207                                 return IRC_WriteStrClient( Client,
208                                                            ERR_NOSUCHNICK_MSG,
209                                                            Client_ID( Client ),
210                                                            Req->argv[0] );
211                 }
212                 else
213                 {
214                         /* Is this a restricted client? */
215                         if( Client_HasMode( Client, 'r' ))
216                                 return IRC_WriteStrClient( Client,
217                                                            ERR_RESTRICTED_MSG,
218                                                            Client_ID( Client ));
219
220                         target = Client;
221                 }
222
223 #ifndef STRICT_RFC
224                 /* If the clients tries to change to its own nickname we won't
225                  * do anything. This is how the original ircd behaves and some
226                  * clients (for example Snak) expect it to be like this.
227                  * But I doubt that this is "really the right thing" ... */
228                 if( strcmp( Client_ID( target ), Req->argv[0] ) == 0 )
229                         return CONNECTED;
230 #endif
231
232                 /* Check that the new nickname is available. Special case:
233                  * the client only changes from/to upper to lower case. */
234                 if( strcasecmp( Client_ID( target ), Req->argv[0] ) != 0 )
235                 {
236                         if( ! Client_CheckNick( target, Req->argv[0] ))
237                                 return CONNECTED;
238                 }
239
240                 if (Client_Type(target) != CLIENT_USER &&
241                     Client_Type(target) != CLIENT_SERVICE &&
242                     Client_Type(target) != CLIENT_SERVER) {
243                         /* New client */
244                         LogDebug("Connection %d: got valid NICK command ...",
245                              Client_Conn( Client ));
246
247                         /* Register new nickname of this client */
248                         Client_SetID( target, Req->argv[0] );
249
250                         /* If we received a valid USER command already then
251                          * register the new client! */
252                         if( Client_Type( Client ) == CLIENT_GOTUSER )
253                                 return Hello_User( Client );
254                         else
255                                 Client_SetType( Client, CLIENT_GOTNICK );
256                 } else {
257                         /* Nickname change */
258                         if (Client_Conn(target) > NONE) {
259                                 /* Local client */
260                                 Log(LOG_INFO,
261                                     "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
262                                     Client_TypeText(target), Client_Mask(target),
263                                     Client_Conn(target), Client_ID(target),
264                                     Req->argv[0]);
265                                 Conn_UpdateIdle(Client_Conn(target));
266                         } else {
267                                 /* Remote client */
268                                 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
269                                          Client_TypeText(target),
270                                          Client_Mask(target), Client_ID(target),
271                                          Req->argv[0]);
272                         }
273
274                         /* Inform all users and servers (which have to know)
275                          * of this nickname change */
276                         if( Client_Type( Client ) == CLIENT_USER )
277                                 IRC_WriteStrClientPrefix( Client, Client,
278                                                           "NICK :%s",
279                                                           Req->argv[0] );
280                         IRC_WriteStrServersPrefix( Client, target,
281                                                    "NICK :%s", Req->argv[0] );
282                         IRC_WriteStrRelatedPrefix( target, target, false,
283                                                    "NICK :%s", Req->argv[0] );
284
285                         /* Register old nickname for WHOWAS queries */
286                         Client_RegisterWhowas( target );
287
288                         /* Save new nickname */
289                         Client_SetID( target, Req->argv[0] );
290
291                         IRC_SetPenalty( target, 2 );
292                 }
293
294                 return CONNECTED;
295         } else if(Client_Type(Client) == CLIENT_SERVER ||
296                   Client_Type(Client) == CLIENT_SERVICE) {
297                 /* Server or service introduces new client */
298
299                 /* Bad number of parameters? */
300                 if (Req->argc != 2 && Req->argc != 7)
301                         return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
302                                                   Client_ID(Client), Req->command);
303
304                 if (Req->argc >= 7) {
305                         /* RFC 2813 compatible syntax */
306                         nick = Req->argv[0];
307                         hops = atoi(Req->argv[1]);
308                         user = Req->argv[2];
309                         hostname = Req->argv[3];
310                         token = atoi(Req->argv[4]);
311                         modes = Req->argv[5] + 1;
312                         info = Req->argv[6];
313                 } else {
314                         /* RFC 1459 compatible syntax */
315                         nick = Req->argv[0];
316                         hops = 1;
317                         user = Req->argv[0];
318                         hostname = Client_ID(Client);
319                         token = atoi(Req->argv[1]);
320                         modes = "";
321                         info = Req->argv[0];
322                 }
323
324                 c = Client_Search(nick);
325                 if(c) {
326                         /*
327                          * the new nick is already present on this server:
328                          * the new and the old one have to be disconnected now.
329                          */
330                         Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
331                         Kill_Nick( Req->argv[0], "Nick collision" );
332                         return CONNECTED;
333                 }
334
335                 /* Find the Server this client is connected to */
336                 intr_c = Client_GetFromToken(Client, token);
337                 if( ! intr_c )
338                 {
339                         Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
340                         Kill_Nick( Req->argv[0], "Unknown server" );
341                         return CONNECTED;
342                 }
343
344                 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
345                                          token, modes, info, true);
346                 if( ! c )
347                 {
348                         /* out of memory, need to disconnect client to keep network state consistent */
349                         Log( LOG_ALERT, "Can't create client structure! (on connection %d)", Client_Conn( Client ));
350                         Kill_Nick( Req->argv[0], "Server error" );
351                         return CONNECTED;
352                 }
353
354                 /* RFC 2813: client is now fully registered, inform all the
355                  * other servers about the new user.
356                  * RFC 1459: announce the new client only after receiving the
357                  * USER command, first we need more information! */
358                 if (Req->argc < 7) {
359                         LogDebug("Client \"%s\" is beeing registered (RFC 1459) ...",
360                                  Client_Mask(c));
361                         Client_SetType(c, CLIENT_GOTNICK);
362                 } else
363                         Introduce_Client(Client, c, CLIENT_USER);
364
365                 return CONNECTED;
366         }
367         else return IRC_WriteStrClient( Client, ERR_ALREADYREGISTRED_MSG, Client_ID( Client ));
368 } /* IRC_NICK */
369
370
371 /**
372  * Handler for the IRC command "USER".
373  */
374 GLOBAL bool
375 IRC_USER(CLIENT * Client, REQUEST * Req)
376 {
377         CLIENT *c;
378 #ifdef IDENTAUTH
379         char *ptr;
380 #endif
381
382         assert(Client != NULL);
383         assert(Req != NULL);
384
385         if (Client_Type(Client) == CLIENT_GOTNICK ||
386 #ifndef STRICT_RFC
387             Client_Type(Client) == CLIENT_UNKNOWN ||
388 #endif
389             Client_Type(Client) == CLIENT_GOTPASS)
390         {
391                 /* New connection */
392                 if (Req->argc != 4)
393                         return IRC_WriteStrClient(Client,
394                                                   ERR_NEEDMOREPARAMS_MSG,
395                                                   Client_ID(Client),
396                                                   Req->command);
397
398                 /* User name */
399 #ifdef IDENTAUTH
400                 ptr = Client_User(Client);
401                 if (!ptr || !*ptr || *ptr == '~')
402                         Client_SetUser(Client, Req->argv[0], false);
403 #else
404                 Client_SetUser(Client, Req->argv[0], false);
405 #endif
406
407                 /* "Real name" or user info text: Don't set it to the empty
408                  * string, the original ircd can't deal with such "real names"
409                  * (e. g. "USER user * * :") ... */
410                 if (*Req->argv[3])
411                         Client_SetInfo(Client, Req->argv[3]);
412                 else
413                         Client_SetInfo(Client, "-");
414
415                 LogDebug("Connection %d: got valid USER command ...",
416                     Client_Conn(Client));
417                 if (Client_Type(Client) == CLIENT_GOTNICK)
418                         return Hello_User(Client);
419                 else
420                         Client_SetType(Client, CLIENT_GOTUSER);
421                 return CONNECTED;
422
423         } else if (Client_Type(Client) == CLIENT_SERVER ||
424                    Client_Type(Client) == CLIENT_SERVICE) {
425                 /* Server/service updating an user */
426                 if (Req->argc != 4)
427                         return IRC_WriteStrClient(Client,
428                                                   ERR_NEEDMOREPARAMS_MSG,
429                                                   Client_ID(Client),
430                                                   Req->command);
431                 c = Client_Search(Req->prefix);
432                 if (!c)
433                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
434                                                   Client_ID(Client),
435                                                   Req->prefix);
436
437                 Client_SetUser(c, Req->argv[0], true);
438                 Client_SetHostname(c, Req->argv[1]);
439                 Client_SetInfo(c, Req->argv[3]);
440
441                 LogDebug("Connection %d: got valid USER command for \"%s\".",
442                          Client_Conn(Client), Client_Mask(c));
443
444                 /* RFC 1459 style user registration?
445                  * Introduce client to network: */
446                 if (Client_Type(c) == CLIENT_GOTNICK)
447                         Introduce_Client(Client, c, CLIENT_USER);
448
449                 return CONNECTED;
450         } else if (Client_Type(Client) == CLIENT_USER) {
451                 /* Already registered connection */
452                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
453                                           Client_ID(Client));
454         } else {
455                 /* Unexpected/invalid connection state? */
456                 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
457                                           Client_ID(Client));
458         }
459 } /* IRC_USER */
460
461
462 /**
463  * Handler for the IRC command "SERVICE".
464  * This function implements IRC Services registration using the SERVICE command
465  * defined in RFC 2812 3.1.6 and RFC 2813 4.1.4.
466  * At the moment ngIRCd doesn't support directly linked services, so this
467  * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
468  * received from a peer server.
469  */
470 GLOBAL bool
471 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
472 {
473         CLIENT *c, *intr_c;
474         char *nick, *user, *host, *info, *modes, *ptr;
475         int token, hops;
476
477         assert(Client != NULL);
478         assert(Req != NULL);
479
480         if (Client_Type(Client) != CLIENT_GOTPASS &&
481             Client_Type(Client) != CLIENT_SERVER)
482                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
483                                           Client_ID(Client));
484
485         if (Req->argc != 6)
486                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
487                                           Client_ID(Client), Req->command);
488
489         if (Client_Type(Client) != CLIENT_SERVER)
490                 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
491                                   Client_ID(Client), Req->argv[0]);
492
493         /* Bad number of parameters? */
494         if (Req->argc != 6)
495                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
496                                           Client_ID(Client), Req->command);
497
498         nick = Req->argv[0];
499         user = NULL; host = NULL;
500         token = atoi(Req->argv[1]);
501         hops = atoi(Req->argv[4]);
502         info = Req->argv[5];
503
504         /* Validate service name ("nick name") */
505         c = Client_Search(nick);
506         if(c) {
507                 /* Nick name collission: disconnect (KILL) both clients! */
508                 Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
509                     Client_ID(Client), nick);
510                 Kill_Nick(nick, "Nick collision");
511                 return CONNECTED;
512         }
513
514         /* Get the server to which the service is connected */
515         intr_c = Client_GetFromToken(Client, token);
516         if (! intr_c) {
517                 Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
518                     Client_ID(Client), nick);
519                 Kill_Nick(nick, "Unknown server");
520                 return CONNECTED;
521         }
522
523         /* Get user and host name */
524         ptr = strchr(nick, '@');
525         if (ptr) {
526                 *ptr = '\0';
527                 host = ++ptr;
528         }
529         if (!host)
530                 host = Client_Hostname(intr_c);
531         ptr = strchr(nick, '!');
532         if (ptr) {
533                 *ptr = '\0';
534                 user = ++ptr;
535         }
536         if (!user)
537                 user = nick;
538
539         /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
540          * for future usage"; but we use it to transfer the modes and check
541          * that the first character is a '+' sign and ignore it otherwise. */
542         modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
543
544         c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
545                                  token, modes, info, true);
546         if (! c) {
547                 /* Couldn't create client structure, so KILL the service to
548                  * keep network status consistent ... */
549                 Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
550                     Client_Conn(Client));
551                 Kill_Nick(nick, "Server error");
552                 return CONNECTED;
553         }
554
555         Introduce_Client(Client, c, CLIENT_SERVICE);
556         return CONNECTED;
557 } /* IRC_SERVICE */
558
559
560 GLOBAL bool
561 IRC_QUIT( CLIENT *Client, REQUEST *Req )
562 {
563         CLIENT *target;
564         char quitmsg[LINE_LEN];
565
566         assert( Client != NULL );
567         assert( Req != NULL );
568
569         /* Wrong number of arguments? */
570         if( Req->argc > 1 )
571                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
572
573         if (Req->argc == 1)
574                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
575
576         if ( Client_Type( Client ) == CLIENT_SERVER )
577         {
578                 /* Server */
579                 target = Client_Search( Req->prefix );
580                 if( ! target )
581                 {
582                         Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
583                         return CONNECTED;
584                 }
585
586                 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
587
588                 return CONNECTED;
589         }
590         else
591         {
592                 if (Req->argc == 1 && quitmsg[0] != '\"') {
593                         /* " " to avoid confusion */
594                         strlcpy(quitmsg, "\"", sizeof quitmsg);
595                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
596                         strlcat(quitmsg, "\"", sizeof quitmsg );
597                 }
598
599                 /* User, Service, or not yet registered */
600                 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
601
602                 return DISCONNECTED;
603         }
604 } /* IRC_QUIT */
605
606
607 GLOBAL bool
608 IRC_PING(CLIENT *Client, REQUEST *Req)
609 {
610         CLIENT *target, *from;
611
612         assert(Client != NULL);
613         assert(Req != NULL);
614
615         if (Req->argc < 1)
616                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
617                                           Client_ID(Client));
618 #ifdef STRICT_RFC
619         /* Don't ignore additional arguments when in "strict" mode */
620         if (Req->argc > 2)
621                  return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
622                                            Client_ID(Client), Req->command);
623 #endif
624
625         if (Req->argc > 1) {
626                 /* A target has been specified ... */
627                 target = Client_Search(Req->argv[1]);
628
629                 if (!target || Client_Type(target) != CLIENT_SERVER)
630                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
631                                         Client_ID(Client), Req->argv[1]);
632
633                 if (target != Client_ThisServer()) {
634                         /* Ok, we have to forward the PING */
635                         if (Client_Type(Client) == CLIENT_SERVER)
636                                 from = Client_Search(Req->prefix);
637                         else
638                                 from = Client;
639                         if (!from)
640                                 return IRC_WriteStrClient(Client,
641                                                 ERR_NOSUCHSERVER_MSG,
642                                                 Client_ID(Client), Req->prefix);
643
644                         return IRC_WriteStrClientPrefix(target, from,
645                                         "PING %s :%s", Req->argv[0],
646                                         Req->argv[1] );
647                 }
648         }
649
650         if (Client_Type(Client) == CLIENT_SERVER) {
651                 if (Req->prefix)
652                         from = Client_Search(Req->prefix);
653                 else
654                         from = Client;
655         } else
656                 from = Client_ThisServer();
657         if (!from)
658                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
659                                         Client_ID(Client), Req->prefix);
660
661         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
662             Client_Conn(Client));
663
664 #ifdef STRICT_RFC
665         return IRC_WriteStrClient(Client, "PONG %s :%s",
666                 Client_ID(from), Client_ID(Client));
667 #else
668         /* Some clients depend on the argument being returned in the PONG
669          * reply (not mentioned in any RFC, though) */
670         return IRC_WriteStrClient(Client, "PONG %s :%s",
671                 Client_ID(from), Req->argv[0]);
672 #endif
673 } /* IRC_PING */
674
675
676 GLOBAL bool
677 IRC_PONG(CLIENT *Client, REQUEST *Req)
678 {
679         CLIENT *target, *from;
680         char *s;
681
682         assert(Client != NULL);
683         assert(Req != NULL);
684
685         /* Wrong number of arguments? */
686         if (Req->argc < 1)
687                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
688                                           Client_ID(Client));
689         if (Req->argc > 2)
690                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
691                                           Client_ID(Client), Req->command);
692
693         /* Forward? */
694         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
695                 target = Client_Search(Req->argv[0]);
696                 if (!target)
697                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
698                                         Client_ID(Client), Req->argv[0]);
699
700                 from = Client_Search(Req->prefix);
701
702                 if (target != Client_ThisServer() && target != from) {
703                         /* Ok, we have to forward the message. */
704                         if (!from)
705                                 return IRC_WriteStrClient(Client,
706                                                 ERR_NOSUCHSERVER_MSG,
707                                                 Client_ID(Client), Req->prefix);
708
709                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
710                                 s = Client_ID(from);
711                         else
712                                 s = Req->argv[0];
713                         return IRC_WriteStrClientPrefix(target, from,
714                                  "PONG %s :%s", s, Req->argv[1]);
715                 }
716         }
717
718         /* The connection timestamp has already been updated when the data has
719          * been read from so socket, so we don't need to update it here. */
720 #ifdef DEBUG
721         if (Client_Conn(Client) > NONE)
722                 Log(LOG_DEBUG,
723                         "Connection %d: received PONG. Lag: %ld seconds.",
724                         Client_Conn(Client),
725                         time(NULL) - Conn_LastPing(Client_Conn(Client)));
726         else
727                  Log(LOG_DEBUG,
728                         "Connection %d: received PONG.", Client_Conn(Client));
729 #endif
730         return CONNECTED;
731 } /* IRC_PONG */
732
733
734 static bool
735 Hello_User(CLIENT * Client)
736 {
737         assert(Client != NULL);
738
739         /* Check password ... */
740         if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
741                 /* Bad password! */
742                 Log(LOG_ERR,
743                     "Client \"%s\" rejected (connection %d): Bad password!",
744                     Client_Mask(Client), Client_Conn(Client));
745                 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
746                 return DISCONNECTED;
747         }
748
749         Introduce_Client(NULL, Client, CLIENT_USER);
750
751         if (!IRC_WriteStrClient
752             (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
753                 return false;
754         if (!IRC_WriteStrClient
755             (Client, RPL_YOURHOST_MSG, Client_ID(Client),
756              Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
757              TARGET_VENDOR, TARGET_OS))
758                 return false;
759         if (!IRC_WriteStrClient
760             (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
761                 return false;
762         if (!IRC_WriteStrClient
763             (Client, RPL_MYINFO_MSG, Client_ID(Client),
764              Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
765              CHANMODES))
766                 return false;
767
768         /* Features supported by this server (005 numeric, ISUPPORT),
769          * see <http://www.irc.org/tech_docs/005.html> for details. */
770         if (!IRC_Send_ISUPPORT(Client))
771                 return DISCONNECTED;
772
773         if (!IRC_Send_LUSERS(Client))
774                 return DISCONNECTED;
775         if (!IRC_Show_MOTD(Client))
776                 return DISCONNECTED;
777
778         /* Suspend the client for a second ... */
779         IRC_SetPenalty(Client, 1);
780
781         return CONNECTED;
782 } /* Hello_User */
783
784
785 static void
786 Kill_Nick( char *Nick, char *Reason )
787 {
788         REQUEST r;
789
790         assert( Nick != NULL );
791         assert( Reason != NULL );
792
793         r.prefix = (char *)Client_ThisServer( );
794         r.argv[0] = Nick;
795         r.argv[1] = Reason;
796         r.argc = 2;
797
798         Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
799         IRC_KILL( Client_ThisServer( ), &r );
800 } /* Kill_Nick */
801
802
803 static void
804 Introduce_Client(CLIENT *From, CLIENT *Client, int Type)
805 {
806         /* Set client type (user or service) */
807         Client_SetType(Client, Type);
808
809         if (From) {
810                 if (Conf_IsService(Conf_GetServer(Client_Conn(From)),
811                                    Client_ID(Client)))
812                         Client_SetType(Client, CLIENT_SERVICE);
813                 LogDebug("%s \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
814                          Client_TypeText(Client), Client_Mask(Client),
815                          Client_Modes(Client), Client_ID(From),
816                          Client_ID(Client_Introducer(Client)),
817                          Client_Hops(Client), Client_Hops(Client) > 1 ? "s": "");
818         } else
819                 Log(LOG_NOTICE, "%s \"%s\" registered (connection %d).",
820                     Client_TypeText(Client), Client_Mask(Client),
821                     Client_Conn(Client));
822
823         /* Inform other servers */
824         IRC_WriteStrServersPrefixFlag_CB(From,
825                                 From != NULL ? From : Client_ThisServer(),
826                                 '\0', cb_introduceClient, (void *)Client);
827 } /* Introduce_Client */
828
829
830 static void
831 cb_introduceClient(CLIENT *To, CLIENT *Prefix, void *data)
832 {
833         CLIENT *c = (CLIENT *)data;
834         CONN_ID conn;
835         char *modes, *user, *host;
836
837         modes = Client_Modes(c);
838         user = Client_User(c) ? Client_User(c) : "-";
839         host = Client_Hostname(c) ? Client_Hostname(c) : "-";
840
841         conn = Client_Conn(To);
842         if (Conn_Options(conn) & CONN_RFC1459) {
843                 /* RFC 1459 mode: separate NICK and USER commands */
844                 Conn_WriteStr(conn, "NICK %s :%d", Client_ID(c),
845                               Client_Hops(c) + 1);
846                 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
847                               Client_ID(c), user, host,
848                               Client_ID(Client_Introducer(c)), Client_Info(c));
849                 if (modes[0])
850                         Conn_WriteStr(conn, ":%s MODE %s +%s",
851                                       Client_ID(c), Client_ID(c), modes);
852         } else {
853                 /* RFC 2813 mode: one combined NICK or SERVICE command */
854                 if (Client_Type(c) == CLIENT_SERVICE
855                     && strchr(Client_Flags(To), 'S'))
856                         IRC_WriteStrClientPrefix(To, Prefix,
857                                          "SERVICE %s %d * +%s %d :%s",
858                                          Client_Mask(c),
859                                          Client_MyToken(Client_Introducer(c)),
860                                          Client_Modes(c), Client_Hops(c) + 1,
861                                          Client_Info(c));
862                 else
863                         IRC_WriteStrClientPrefix(To, Prefix,
864                                          "NICK %s %d %s %s %d +%s :%s",
865                                          Client_ID(c), Client_Hops(c) + 1,
866                                          user, host,
867                                          Client_MyToken(Client_Introducer(c)),
868                                          modes, Client_Info(c));
869         }
870 } /* cb_introduceClient */
871
872
873 /* -eof- */