]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
Introduce_Client(): send MODES in RFC 1459 mode, too.
[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, modes, 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 #ifdef IDENTAUTH
414         char *ptr;
415 #endif
416
417         assert(Client != NULL);
418         assert(Req != NULL);
419
420         if (Client_Type(Client) == CLIENT_GOTNICK ||
421 #ifndef STRICT_RFC
422             Client_Type(Client) == CLIENT_UNKNOWN ||
423 #endif
424             Client_Type(Client) == CLIENT_GOTPASS)
425         {
426                 /* New connection */
427                 if (Req->argc != 4)
428                         return IRC_WriteStrClient(Client,
429                                                   ERR_NEEDMOREPARAMS_MSG,
430                                                   Client_ID(Client),
431                                                   Req->command);
432
433                 /* User name */
434 #ifdef IDENTAUTH
435                 ptr = Client_User(Client);
436                 if (!ptr || !*ptr || *ptr == '~')
437                         Client_SetUser(Client, Req->argv[0], false);
438 #else
439                 Client_SetUser(Client, Req->argv[0], false);
440 #endif
441
442                 /* "Real name" or user info text: Don't set it to the empty
443                  * string, the original ircd can't deal with such "real names"
444                  * (e. g. "USER user * * :") ... */
445                 if (*Req->argv[3])
446                         Client_SetInfo(Client, Req->argv[3]);
447                 else
448                         Client_SetInfo(Client, "-");
449
450                 LogDebug("Connection %d: got valid USER command ...",
451                     Client_Conn(Client));
452                 if (Client_Type(Client) == CLIENT_GOTNICK)
453                         return Hello_User(Client);
454                 else
455                         Client_SetType(Client, CLIENT_GOTUSER);
456                 return CONNECTED;
457
458         } else if (Client_Type(Client) == CLIENT_SERVER ||
459                    Client_Type(Client) == CLIENT_SERVICE) {
460                 /* Server/service updating an user */
461                 if (Req->argc != 4)
462                         return IRC_WriteStrClient(Client,
463                                                   ERR_NEEDMOREPARAMS_MSG,
464                                                   Client_ID(Client),
465                                                   Req->command);
466                 c = Client_Search(Req->prefix);
467                 if (!c)
468                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
469                                                   Client_ID(Client),
470                                                   Req->prefix);
471
472                 Client_SetUser(c, Req->argv[0], true);
473                 Client_SetHostname(c, Req->argv[1]);
474                 Client_SetInfo(c, Req->argv[3]);
475
476                 LogDebug("Connection %d: got valid USER command for \"%s\".",
477                          Client_Conn(Client), Client_Mask(c));
478
479                 /* RFC 1459 style user registration? Inform other servers! */
480                 if (Client_Type(c) == CLIENT_GOTNICK) {
481                         Introduce_Client(Client, Client_ID(c), Client_Hops(c),
482                                          Client_User(c), Client_Hostname(c),
483                                          Client_Introducer(c), Client_Modes(c),
484                                          Client_Info(c));
485                         LogDebug("User \"%s\" (+%s) registered (via %s, on %s, %d hop%s).",
486                                  Client_Mask(c), Client_Modes(c), Client_ID(Client),
487                                  Client_ID(Client_Introducer(c)), Client_Hops(c),
488                                  Client_Hops(c) > 1 ? "s": "");
489                         Client_SetType(c, CLIENT_USER);
490                 }
491
492                 return CONNECTED;
493         } else if (Client_Type(Client) == CLIENT_USER) {
494                 /* Already registered connection */
495                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
496                                           Client_ID(Client));
497         } else {
498                 /* Unexpected/invalid connection state? */
499                 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
500                                           Client_ID(Client));
501         }
502 } /* IRC_USER */
503
504
505 /**
506  * Service registration.
507  * ngIRCd does not support services at the moment, so this function is a
508  * dummy that returns ERR_ERRONEUSNICKNAME on each call.
509  */
510 GLOBAL bool
511 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
512 {
513         assert(Client != NULL);
514         assert(Req != NULL);
515
516         if (Client_Type(Client) != CLIENT_GOTPASS)
517                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
518                                           Client_ID(Client));
519
520         if (Req->argc != 6)
521                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
522                                           Client_ID(Client), Req->command);
523
524         return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
525                                   Client_ID(Client), Req->argv[0]);
526 } /* IRC_SERVICE */
527
528
529 GLOBAL bool
530 IRC_QUIT( CLIENT *Client, REQUEST *Req )
531 {
532         CLIENT *target;
533         char quitmsg[LINE_LEN];
534
535         assert( Client != NULL );
536         assert( Req != NULL );
537
538         /* Wrong number of arguments? */
539         if( Req->argc > 1 )
540                 return IRC_WriteStrClient( Client, ERR_NEEDMOREPARAMS_MSG, Client_ID( Client ), Req->command );
541
542         if (Req->argc == 1)
543                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
544
545         if ( Client_Type( Client ) == CLIENT_SERVER )
546         {
547                 /* Server */
548                 target = Client_Search( Req->prefix );
549                 if( ! target )
550                 {
551                         /* Den Client kennen wir nicht (mehr), also nichts zu tun. */
552                         Log( LOG_WARNING, "Got QUIT from %s for unknown client!?", Client_ID( Client ));
553                         return CONNECTED;
554                 }
555
556                 Client_Destroy( target, "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
557
558                 return CONNECTED;
559         }
560         else
561         {
562                 if (Req->argc == 1 && quitmsg[0] != '\"') {
563                         /* " " to avoid confusion */
564                         strlcpy(quitmsg, "\"", sizeof quitmsg);
565                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
566                         strlcat(quitmsg, "\"", sizeof quitmsg );
567                 }
568
569                 /* User, Service, oder noch nicht registriert */
570                 Conn_Close( Client_Conn( Client ), "Got QUIT command.", Req->argc == 1 ? quitmsg : NULL, true);
571
572                 return DISCONNECTED;
573         }
574 } /* IRC_QUIT */
575
576
577 GLOBAL bool
578 IRC_PING(CLIENT *Client, REQUEST *Req)
579 {
580         CLIENT *target, *from;
581
582         assert(Client != NULL);
583         assert(Req != NULL);
584
585         /* Wrong number of arguments? */
586         if (Req->argc < 1)
587                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
588                                           Client_ID(Client));
589 #ifdef STRICT_RFC
590         /* Don't ignore additional arguments when in "strict" mode */
591         if (Req->argc > 2)
592                  return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
593                                            Client_ID(Client), Req->command);
594 #endif
595
596         if (Req->argc > 1) {
597                 /* A target has been specified ... */
598                 target = Client_Search(Req->argv[1]);
599
600                 if (!target || Client_Type(target) != CLIENT_SERVER)
601                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
602                                         Client_ID(Client), Req->argv[1]);
603
604                 if (target != Client_ThisServer()) {
605                         /* Ok, we have to forward the PING */
606                         if (Client_Type(Client) == CLIENT_SERVER)
607                                 from = Client_Search(Req->prefix);
608                         else
609                                 from = Client;
610                         if (!from)
611                                 return IRC_WriteStrClient(Client,
612                                                 ERR_NOSUCHSERVER_MSG,
613                                                 Client_ID(Client), Req->prefix);
614
615                         return IRC_WriteStrClientPrefix(target, from,
616                                         "PING %s :%s", Req->argv[0],
617                                         Req->argv[1] );
618                 }
619         }
620
621         if (Client_Type(Client) == CLIENT_SERVER) {
622                 if (Req->prefix)
623                         from = Client_Search(Req->prefix);
624                 else
625                         from = Client;
626         } else
627                 from = Client_ThisServer();
628         if (!from)
629                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
630                                         Client_ID(Client), Req->prefix);
631
632         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
633             Client_Conn(Client));
634
635 #ifdef STRICT_RFC
636         return IRC_WriteStrClient(Client, "PONG %s :%s",
637                 Client_ID(from), Client_ID(Client));
638 #else
639         /* Some clients depend on the argument being returned in the PONG
640          * reply (not mentioned in any RFC, though) */
641         return IRC_WriteStrClient(Client, "PONG %s :%s",
642                 Client_ID(from), Req->argv[0]);
643 #endif
644 } /* IRC_PING */
645
646
647 GLOBAL bool
648 IRC_PONG(CLIENT *Client, REQUEST *Req)
649 {
650         CLIENT *target, *from;
651         char *s;
652
653         assert(Client != NULL);
654         assert(Req != NULL);
655
656         /* Wrong number of arguments? */
657         if (Req->argc < 1)
658                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
659                                           Client_ID(Client));
660         if (Req->argc > 2)
661                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
662                                           Client_ID(Client), Req->command);
663
664         /* Forward? */
665         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
666                 target = Client_Search(Req->argv[0]);
667                 if (!target)
668                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
669                                         Client_ID(Client), Req->argv[0]);
670
671                 from = Client_Search(Req->prefix);
672
673                 if (target != Client_ThisServer() && target != from) {
674                         /* Ok, we have to forward the message. */
675                         if (!from)
676                                 return IRC_WriteStrClient(Client,
677                                                 ERR_NOSUCHSERVER_MSG,
678                                                 Client_ID(Client), Req->prefix);
679
680                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
681                                 s = Client_ID(from);
682                         else
683                                 s = Req->argv[0];
684                         return IRC_WriteStrClientPrefix(target, from,
685                                  "PONG %s :%s", s, Req->argv[1]);
686                 }
687         }
688
689         /* The connection timestamp has already been updated when the data has
690          * been read from so socket, so we don't need to update it here. */
691
692         if (Client_Conn(Client) > NONE)
693                 Log(LOG_DEBUG,
694                         "Connection %d: received PONG. Lag: %ld seconds.",
695                         Client_Conn(Client),
696                         time(NULL) - Conn_LastPing(Client_Conn(Client)));
697         else
698                  Log(LOG_DEBUG,
699                         "Connection %d: received PONG.", Client_Conn(Client));
700
701         return CONNECTED;
702 } /* IRC_PONG */
703
704
705 static bool
706 Hello_User(CLIENT * Client)
707 {
708         char modes[CLIENT_MODE_LEN + 1] = "+";
709
710         assert(Client != NULL);
711
712         /* Check password ... */
713         if (strcmp(Client_Password(Client), Conf_ServerPwd) != 0) {
714                 /* Bad password! */
715                 Log(LOG_ERR,
716                     "User \"%s\" rejected (connection %d): Bad password!",
717                     Client_Mask(Client), Client_Conn(Client));
718                 Conn_Close(Client_Conn(Client), NULL, "Bad password", true);
719                 return DISCONNECTED;
720         }
721
722         Log(LOG_NOTICE, "User \"%s\" registered (connection %d).",
723             Client_Mask(Client), Client_Conn(Client));
724
725         /* Inform other servers */
726         strlcat(modes, Client_Modes(Client), sizeof(modes));
727         Introduce_Client(NULL, Client_ID(Client), 1, Client_User(Client),
728                          Client_Hostname(Client), NULL, modes,
729                          Client_Info(Client));
730
731         if (!IRC_WriteStrClient
732             (Client, RPL_WELCOME_MSG, Client_ID(Client), Client_Mask(Client)))
733                 return false;
734         if (!IRC_WriteStrClient
735             (Client, RPL_YOURHOST_MSG, Client_ID(Client),
736              Client_ID(Client_ThisServer()), PACKAGE_VERSION, TARGET_CPU,
737              TARGET_VENDOR, TARGET_OS))
738                 return false;
739         if (!IRC_WriteStrClient
740             (Client, RPL_CREATED_MSG, Client_ID(Client), NGIRCd_StartStr))
741                 return false;
742         if (!IRC_WriteStrClient
743             (Client, RPL_MYINFO_MSG, Client_ID(Client),
744              Client_ID(Client_ThisServer()), PACKAGE_VERSION, USERMODES,
745              CHANMODES))
746                 return false;
747
748         /* Features supported by this server (005 numeric, ISUPPORT),
749          * see <http://www.irc.org/tech_docs/005.html> for details. */
750         if (!IRC_Send_ISUPPORT(Client))
751                 return DISCONNECTED;
752
753         Client_SetType(Client, CLIENT_USER);
754
755         if (!IRC_Send_LUSERS(Client))
756                 return DISCONNECTED;
757         if (!IRC_Show_MOTD(Client))
758                 return DISCONNECTED;
759
760         /* Suspend the client for a second ... */
761         IRC_SetPenalty(Client, 1);
762
763         return CONNECTED;
764 } /* Hello_User */
765
766
767 static void
768 Kill_Nick( char *Nick, char *Reason )
769 {
770         REQUEST r;
771
772         assert( Nick != NULL );
773         assert( Reason != NULL );
774
775         r.prefix = (char *)Client_ThisServer( );
776         r.argv[0] = Nick;
777         r.argv[1] = Reason;
778         r.argc = 2;
779
780         Log( LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s", Nick, Reason );
781         IRC_KILL( Client_ThisServer( ), &r );
782 } /* Kill_Nick */
783
784
785 static void
786 Introduce_Client(CLIENT *From, char *Nick, const int HopCount, char *User,
787                  char *Host, CLIENT *Server, char *Mode, char *Name)
788 {
789         INTRO_INFO i;
790
791         i.nick = Nick;
792         i.hopcount = HopCount;
793         i.user = User ? User : "-";
794         i.host = Host ? Host : "-";
795         i.server = Server ? Server : Client_ThisServer();
796         i.mode = Mode ? Mode : "+";
797         i.name = Name ? Name : "";
798
799         IRC_WriteStrServersPrefixFlag_CB(From,
800                                 From != NULL ? From : Client_ThisServer(),
801                                 '\0', cb_introduceClient, (void *)(&i));
802 } /* Introduce_Client */
803
804
805 static void
806 cb_introduceClient(CLIENT *Client, CLIENT *Prefix, void *data)
807 {
808         INTRO_INFO *i = (INTRO_INFO *)data;
809         CONN_ID conn;
810
811         conn = Client_Conn(Client);
812         if (Conn_Options(conn) & CONN_RFC1459) {
813                 /* RFC 1459 mode: separate NICK and USER commands */
814                 Conn_WriteStr(conn, "NICK %s :%d", i->nick, i->hopcount);
815                 Conn_WriteStr(conn, ":%s USER %s %s %s :%s",
816                               i->nick, i->user, i->host,
817                               Client_ID(i->server), i->name);
818                 if (i->mode[0])
819                         Conn_WriteStr(conn, ":%s MODE %s +%s",
820                                       i->nick, i->nick, i->mode);
821         } else {
822                 /* RFC 2813 mode: one combined NICK command */
823                 IRC_WriteStrClientPrefix(Client, Prefix,
824                                          "NICK %s %d %s %s %d +%s :%s",
825                                          i->nick, i->hopcount, i->user, i->host,
826                                          Client_MyToken(i->server), i->mode,
827                                          i->name);
828         }
829 } /* cb_introduceClient */
830
831
832 /* -eof- */