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