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