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