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