]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
More spelling fixes
[ngircd-alex.git] / src / ngircd / irc-login.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2012 Alexander Barton (alex@barton.de) and Contributors.
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
12 #include "portab.h"
13
14 /**
15  * @file
16  * Login and logout
17  */
18
19 #include "imp.h"
20 #include <assert.h>
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <strings.h>
25
26 #include "conn-func.h"
27 #include "class.h"
28 #include "conf.h"
29 #include "channel.h"
30 #include "log.h"
31 #include "login.h"
32 #include "messages.h"
33 #include "parse.h"
34 #include "irc.h"
35 #include "irc-info.h"
36 #include "irc-macros.h"
37 #include "irc-write.h"
38
39 #include "exp.h"
40 #include "irc-login.h"
41
42 static void Kill_Nick PARAMS((char *Nick, char *Reason));
43 static void Change_Nick PARAMS((CLIENT * Origin, CLIENT * Target, char *NewNick,
44                                 bool InformClient));
45
46 /**
47  * Handler for the IRC "PASS" command.
48  *
49  * @param Client The client from which this command has been received.
50  * @param Req Request structure with prefix and all parameters.
51  * @return CONNECTED or DISCONNECTED.
52  */
53 GLOBAL bool
54 IRC_PASS( CLIENT *Client, REQUEST *Req )
55 {
56         char *type, *orig_flags;
57         int protohigh, protolow;
58
59         assert( Client != NULL );
60         assert( Req != NULL );
61
62         /* Return an error if this is not a local client */
63         if (Client_Conn(Client) <= NONE)
64                 return IRC_WriteStrClient(Client, ERR_UNKNOWNCOMMAND_MSG,
65                                           Client_ID(Client), Req->command);
66
67         if (Client_Type(Client) == CLIENT_UNKNOWN && Req->argc == 1) {
68                 /* Not yet registered "unknown" connection, PASS with one
69                  * argument: either a regular client, service, or server
70                  * using the old RFC 1459 section 4.1.1 syntax. */
71                 LogDebug("Connection %d: got PASS command (RFC 1459) ...",
72                          Client_Conn(Client));
73         } else if ((Client_Type(Client) == CLIENT_UNKNOWN ||
74                     Client_Type(Client) == CLIENT_UNKNOWNSERVER) &&
75                    (Req->argc == 3 || Req->argc == 4)) {
76                 /* Not yet registered "unknown" connection or outgoing server
77                  * link, PASS with three or four argument: server using the
78                  * RFC 2813 section 4.1.1 syntax. */
79                 LogDebug("Connection %d: got PASS command (RFC 2813, new server link) ...",
80                          Client_Conn(Client));
81         } else if (Client_Type(Client) == CLIENT_UNKNOWN ||
82                    Client_Type(Client) == CLIENT_UNKNOWNSERVER) {
83                 /* Unregistered connection, but wrong number of arguments: */
84                 IRC_SetPenalty(Client, 2);
85                 return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
86                                           Client_ID(Client), Req->command);
87         } else {
88                 /* Registered connection, PASS command is not allowed! */
89                 IRC_SetPenalty(Client, 2);
90                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
91                                           Client_ID(Client));
92         }
93
94         Conn_SetPassword(Client_Conn(Client), Req->argv[0]);
95
96         /* Protocol version */
97         if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
98                 int c2, c4;
99
100                 c2 = Req->argv[1][2];
101                 c4 = Req->argv[1][4];
102
103                 Req->argv[1][4] = '\0';
104                 protolow = atoi(&Req->argv[1][2]);
105                 Req->argv[1][2] = '\0';
106                 protohigh = atoi(Req->argv[1]);
107
108                 Req->argv[1][2] = c2;
109                 Req->argv[1][4] = c4;
110
111                 Client_SetType(Client, CLIENT_GOTPASS_2813);
112         } else {
113                 protohigh = protolow = 0;
114                 Client_SetType(Client, CLIENT_GOTPASS);
115         }
116
117         /* Protocol type, see doc/Protocol.txt */
118         if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
119                 type = &Req->argv[1][4];
120         else
121                 type = NULL;
122
123         /* Protocol flags/options */
124         if (Req->argc >= 4)
125                 orig_flags = Req->argv[3];
126         else
127                 orig_flags = "";
128
129         /* Implementation, version and IRC+ flags */
130         if (Req->argc >= 3) {
131                 char *impl, *ptr, *serverver, *flags;
132
133                 impl = Req->argv[2];
134                 ptr = strchr(impl, '|');
135                 if (ptr)
136                         *ptr = '\0';
137
138                 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
139                         /* The peer seems to be a server which supports the
140                          * IRC+ protocol (see doc/Protocol.txt). */
141                         serverver = ptr ? ptr + 1 : "?";
142                         flags = strchr(ptr ? serverver : impl, ':');
143                         if (flags) {
144                                 *flags = '\0';
145                                 flags++;
146                         } else
147                                 flags = "";
148                         Log(LOG_INFO,
149                             "Peer on connection %d announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
150                             Client_Conn(Client), impl, serverver,
151                             protohigh, protolow, flags);
152                 } else {
153                         /* The peer seems to be a server supporting the
154                          * "original" IRC protocol (RFC 2813). */
155                         if (strchr(orig_flags, 'Z'))
156                                 flags = "Z";
157                         else
158                                 flags = "";
159                         Log(LOG_INFO,
160                             "Peer on connection %d announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
161                             Client_Conn(Client), impl,
162                             protohigh, protolow, flags);
163                 }
164                 Client_SetFlags(Client, flags);
165         }
166
167         return CONNECTED;
168 } /* IRC_PASS */
169
170 /**
171  * Handler for the IRC "NICK" command.
172  *
173  * @param Client The client from which this command has been received.
174  * @param Req Request structure with prefix and all parameters.
175  * @return CONNECTED or DISCONNECTED.
176  */
177 GLOBAL bool
178 IRC_NICK( CLIENT *Client, REQUEST *Req )
179 {
180         CLIENT *intr_c, *target, *c;
181         char *nick, *user, *hostname, *modes, *info;
182         int token, hops;
183
184         assert( Client != NULL );
185         assert( Req != NULL );
186
187         /* Some IRC clients, for example BitchX, send the NICK and USER
188          * commands in the wrong order ... */
189         if(Client_Type(Client) == CLIENT_UNKNOWN
190             || Client_Type(Client) == CLIENT_GOTPASS
191             || Client_Type(Client) == CLIENT_GOTNICK
192 #ifndef STRICT_RFC
193             || Client_Type(Client) == CLIENT_GOTUSER
194 #endif
195             || Client_Type(Client) == CLIENT_USER
196             || Client_Type(Client) == CLIENT_SERVICE
197             || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
198         {
199                 /* User registration or change of nickname */
200                 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 1)
201
202                 /* Search "target" client */
203                 if (Client_Type(Client) == CLIENT_SERVER) {
204                         target = Client_Search(Req->prefix);
205                         if (!target)
206                                 return IRC_WriteStrClient( Client,
207                                                            ERR_NOSUCHNICK_MSG,
208                                                            Client_ID( Client ),
209                                                            Req->argv[0] );
210                 } else {
211                         /* Is this a restricted client? */
212                         if (Client_HasMode(Client, 'r')) {
213                                 IRC_SetPenalty(Client, 2);
214                                 return IRC_WriteStrClient( Client,
215                                                            ERR_RESTRICTED_MSG,
216                                                            Client_ID( Client ));
217                         }
218                         target = Client;
219                 }
220
221 #ifndef STRICT_RFC
222                 /* If the clients tries to change to its own nickname we won't
223                  * do anything. This is how the original ircd behaves and some
224                  * clients (for example Snak) expect it to be like this.
225                  * But I doubt that this is "really the right thing" ... */
226                 if (strcmp(Client_ID(target), Req->argv[0]) == 0)
227                         return CONNECTED;
228 #endif
229
230                 /* Check that the new nickname is available. Special case:
231                  * the client only changes from/to upper to lower case. */
232                 if (strcasecmp(Client_ID(target), Req->argv[0]) != 0) {
233                         if (!Client_CheckNick(target, Req->argv[0]))
234                                 return CONNECTED;
235                 }
236
237                 if (Client_Type(target) != CLIENT_USER &&
238                     Client_Type(target) != CLIENT_SERVICE &&
239                     Client_Type(target) != CLIENT_SERVER) {
240                         /* New client */
241                         LogDebug("Connection %d: got valid NICK command ...",
242                              Client_Conn( Client ));
243
244                         /* Register new nickname of this client */
245                         Client_SetID( target, Req->argv[0] );
246
247 #ifndef STRICT_RFC
248                         if (Conf_AuthPing) {
249                                 Conn_SetAuthPing(Client_Conn(Client), rand());
250                                 IRC_WriteStrClient(Client, "PING :%ld",
251                                         Conn_GetAuthPing(Client_Conn(Client)));
252                                 LogDebug("Connection %d: sent AUTH PING %ld ...",
253                                         Client_Conn(Client),
254                                         Conn_GetAuthPing(Client_Conn(Client)));
255                         }
256 #endif
257
258                         /* If we received a valid USER command already then
259                          * register the new client! */
260                         if( Client_Type( Client ) == CLIENT_GOTUSER )
261                                 return Login_User( Client );
262                         else
263                                 Client_SetType( Client, CLIENT_GOTNICK );
264                 } else {
265                         /* Nickname change */
266                         Change_Nick(Client, target, Req->argv[0],
267                                     Client_Type(Client) == CLIENT_USER ? true : false);
268                         IRC_SetPenalty(target, 2);
269                 }
270
271                 return CONNECTED;
272         } else if(Client_Type(Client) == CLIENT_SERVER ||
273                   Client_Type(Client) == CLIENT_SERVICE) {
274                 /* Server or service introduces new client */
275
276                 /* Bad number of parameters? */
277                 if (Req->argc != 2 && Req->argc != 7)
278                         return IRC_WriteStrClient(Client, ERR_NEEDMOREPARAMS_MSG,
279                                                   Client_ID(Client), Req->command);
280
281                 if (Req->argc >= 7) {
282                         /* RFC 2813 compatible syntax */
283                         nick = Req->argv[0];
284                         hops = atoi(Req->argv[1]);
285                         user = Req->argv[2];
286                         hostname = Req->argv[3];
287                         token = atoi(Req->argv[4]);
288                         modes = Req->argv[5] + 1;
289                         info = Req->argv[6];
290                 } else {
291                         /* RFC 1459 compatible syntax */
292                         nick = Req->argv[0];
293                         hops = 1;
294                         user = Req->argv[0];
295                         hostname = Client_ID(Client);
296                         token = atoi(Req->argv[1]);
297                         modes = "";
298                         info = Req->argv[0];
299                 }
300
301                 c = Client_Search(nick);
302                 if(c) {
303                         /*
304                          * the new nick is already present on this server:
305                          * the new and the old one have to be disconnected now.
306                          */
307                         Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
308                         Kill_Nick( Req->argv[0], "Nick collision" );
309                         return CONNECTED;
310                 }
311
312                 /* Find the Server this client is connected to */
313                 intr_c = Client_GetFromToken(Client, token);
314                 if (!intr_c) {
315                         Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
316                         Kill_Nick( Req->argv[0], "Unknown server" );
317                         return CONNECTED;
318                 }
319
320                 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
321                                          token, modes, info, true);
322                 if (!c) {
323                         /* Out of memory, we need to disconnect client to keep
324                          * network state consistent! */
325                         Log(LOG_ALERT,
326                             "Can't create client structure! (on connection %d)",
327                             Client_Conn(Client));
328                         Kill_Nick(Req->argv[0], "Server error");
329                         return CONNECTED;
330                 }
331
332                 /* RFC 2813: client is now fully registered, inform all the
333                  * other servers about the new user.
334                  * RFC 1459: announce the new client only after receiving the
335                  * USER command, first we need more information! */
336                 if (Req->argc < 7) {
337                         LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
338                                  Client_Mask(c));
339                         Client_SetType(c, CLIENT_GOTNICK);
340                 } else
341                         Client_Introduce(Client, c, CLIENT_USER);
342
343                 return CONNECTED;
344         }
345         else
346                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
347                                           Client_ID(Client));
348 } /* IRC_NICK */
349
350 /**
351  * Handler for the IRC "SVSNICK" command.
352  *
353  * @param Client The client from which this command has been received.
354  * @param Req Request structure with prefix and all parameters.
355  * @return CONNECTED or DISCONNECTED.
356  */
357 GLOBAL bool
358 IRC_SVSNICK(CLIENT *Client, REQUEST *Req)
359 {
360         CLIENT *from, *target;
361
362         assert(Client != NULL);
363         assert(Req != NULL);
364
365         _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 2)
366
367         /* Search the originator */
368         from = Client_Search(Req->prefix);
369         if (!from)
370                 from = Client;
371
372         /* Search the target */
373         target = Client_Search(Req->argv[0]);
374         if (!target || Client_Type(target) != CLIENT_USER) {
375                 return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
376                                           Client_ID(Client), Req->argv[0]);
377         }
378
379         if (Client_Conn(target) <= NONE) {
380                 /* We have to forward the message to the server handling
381                  * this user; this is required to make sure all servers
382                  * in the network do follow the nick name change! */
383                 return IRC_WriteStrClientPrefix(Client_NextHop(target), from,
384                                                 "SVSNICK %s %s",
385                                                 Req->argv[0], Req->argv[1]);
386         }
387
388         /* Make sure that the new nickname is valid */
389         if (!Client_CheckNick(from, Req->argv[1]))
390                 return CONNECTED;
391
392         Change_Nick(from, target, Req->argv[1], true);
393         return CONNECTED;
394 }
395
396 /**
397  * Handler for the IRC "USER" command.
398  *
399  * @param Client The client from which this command has been received.
400  * @param Req Request structure with prefix and all parameters.
401  * @return CONNECTED or DISCONNECTED.
402  */
403 GLOBAL bool
404 IRC_USER(CLIENT * Client, REQUEST * Req)
405 {
406         CLIENT *c;
407         char *ptr;
408
409         assert(Client != NULL);
410         assert(Req != NULL);
411
412         if (Client_Type(Client) == CLIENT_GOTNICK ||
413 #ifndef STRICT_RFC
414             Client_Type(Client) == CLIENT_UNKNOWN ||
415 #endif
416             Client_Type(Client) == CLIENT_GOTPASS)
417         {
418                 /* New connection */
419                 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4)
420
421                 /* User name: only alphanumeric characters and limited
422                    punctuation is allowed.*/
423                 ptr = Req->argv[0];
424                 while (*ptr) {
425                         if (!isalnum((int)*ptr) &&
426                             *ptr != '+' && *ptr != '-' && *ptr != '@' &&
427                             *ptr != '.' && *ptr != '_') {
428                                 Conn_Close(Client_Conn(Client), NULL,
429                                            "Invalid user name", true);
430                                 return DISCONNECTED;
431                         }
432                         ptr++;
433                 }
434
435                 /* Save the received username for authentication, and use
436                  * it up to the first '@' as default user name (like ircd2.11,
437                  * bahamut, ircd-seven, ...), prefixed with '~', if needed: */
438                 Client_SetOrigUser(Client, Req->argv[0]);
439                 ptr = strchr(Req->argv[0], '@');
440                 if (ptr)
441                         *ptr = '\0';
442 #ifdef IDENTAUTH
443                 ptr = Client_User(Client);
444                 if (!ptr || !*ptr || *ptr == '~')
445                         Client_SetUser(Client, Req->argv[0], false);
446 #else
447                 Client_SetUser(Client, Req->argv[0], false);
448 #endif
449
450                 /* "Real name" or user info text: Don't set it to the empty
451                  * string, the original ircd can't deal with such "real names"
452                  * (e. g. "USER user * * :") ... */
453                 if (*Req->argv[3])
454                         Client_SetInfo(Client, Req->argv[3]);
455                 else
456                         Client_SetInfo(Client, "-");
457
458                 LogDebug("Connection %d: got valid USER command ...",
459                     Client_Conn(Client));
460                 if (Client_Type(Client) == CLIENT_GOTNICK)
461                         return Login_User(Client);
462                 else
463                         Client_SetType(Client, CLIENT_GOTUSER);
464                 return CONNECTED;
465
466         } else if (Client_Type(Client) == CLIENT_SERVER ||
467                    Client_Type(Client) == CLIENT_SERVICE) {
468                 /* Server/service updating an user */
469                 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4)
470
471                 c = Client_Search(Req->prefix);
472                 if (!c)
473                         return IRC_WriteStrClient(Client, ERR_NOSUCHNICK_MSG,
474                                                   Client_ID(Client),
475                                                   Req->prefix);
476
477                 Client_SetUser(c, Req->argv[0], true);
478                 Client_SetOrigUser(c, Req->argv[0]);
479                 Client_SetHostname(c, Req->argv[1]);
480                 Client_SetInfo(c, Req->argv[3]);
481
482                 LogDebug("Connection %d: got valid USER command for \"%s\".",
483                          Client_Conn(Client), Client_Mask(c));
484
485                 /* RFC 1459 style user registration?
486                  * Introduce client to network: */
487                 if (Client_Type(c) == CLIENT_GOTNICK)
488                         Client_Introduce(Client, c, CLIENT_USER);
489
490                 return CONNECTED;
491         } else if (Client_Type(Client) == CLIENT_USER) {
492                 /* Already registered connection */
493                 IRC_SetPenalty(Client, 2);
494                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
495                                           Client_ID(Client));
496         } else {
497                 /* Unexpected/invalid connection state? */
498                 IRC_SetPenalty(Client, 2);
499                 return IRC_WriteStrClient(Client, ERR_NOTREGISTERED_MSG,
500                                           Client_ID(Client));
501         }
502 } /* IRC_USER */
503
504 /**
505  * Handler for the IRC "SERVICE" command.
506  *
507  * At the moment ngIRCd doesn't support directly linked services, so this
508  * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
509  * received from a peer server.
510  *
511  * @param Client The client from which this command has been received.
512  * @param Req Request structure with prefix and all parameters.
513  * @return CONNECTED or DISCONNECTED.
514  */
515 GLOBAL bool
516 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
517 {
518         CLIENT *c, *intr_c;
519         char *nick, *user, *host, *info, *modes, *ptr;
520         int token, hops;
521
522         assert(Client != NULL);
523         assert(Req != NULL);
524
525         if (Client_Type(Client) != CLIENT_GOTPASS &&
526             Client_Type(Client) != CLIENT_SERVER) {
527                 IRC_SetPenalty(Client, 2);
528                 return IRC_WriteStrClient(Client, ERR_ALREADYREGISTRED_MSG,
529                                           Client_ID(Client));
530         }
531
532         _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 6)
533
534         if (Client_Type(Client) != CLIENT_SERVER)
535                 return IRC_WriteStrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
536                                   Client_ID(Client), Req->argv[0]);
537
538         nick = Req->argv[0];
539         user = NULL; host = NULL;
540         token = atoi(Req->argv[1]);
541         hops = atoi(Req->argv[4]);
542         info = Req->argv[5];
543
544         /* Validate service name ("nickname") */
545         c = Client_Search(nick);
546         if(c) {
547                 /* Nickname collision: disconnect (KILL) both clients! */
548                 Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
549                     Client_ID(Client), nick);
550                 Kill_Nick(nick, "Nick collision");
551                 return CONNECTED;
552         }
553
554         /* Get the server to which the service is connected */
555         intr_c = Client_GetFromToken(Client, token);
556         if (! intr_c) {
557                 Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
558                     Client_ID(Client), nick);
559                 Kill_Nick(nick, "Unknown server");
560                 return CONNECTED;
561         }
562
563         /* Get user and host name */
564         ptr = strchr(nick, '@');
565         if (ptr) {
566                 *ptr = '\0';
567                 host = ++ptr;
568         }
569         if (!host)
570                 host = Client_Hostname(intr_c);
571         ptr = strchr(nick, '!');
572         if (ptr) {
573                 *ptr = '\0';
574                 user = ++ptr;
575         }
576         if (!user)
577                 user = nick;
578
579         /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
580          * for future usage"; but we use it to transfer the modes and check
581          * that the first character is a '+' sign and ignore it otherwise. */
582         modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
583
584         c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
585                                  token, modes, info, true);
586         if (! c) {
587                 /* Couldn't create client structure, so KILL the service to
588                  * keep network status consistent ... */
589                 Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
590                     Client_Conn(Client));
591                 Kill_Nick(nick, "Server error");
592                 return CONNECTED;
593         }
594
595         Client_Introduce(Client, c, CLIENT_SERVICE);
596         return CONNECTED;
597 } /* IRC_SERVICE */
598
599 /**
600  * Handler for the IRC "WEBIRC" command.
601  *
602  * @param Client The client from which this command has been received.
603  * @param Req Request structure with prefix and all parameters.
604  * @return CONNECTED or DISCONNECTED.
605  */
606 GLOBAL bool
607 IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
608 {
609         _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 4)
610
611         if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
612                 return IRC_WriteStrClient(Client, ERR_PASSWDMISMATCH_MSG,
613                                           Client_ID(Client));
614
615         LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
616                  Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
617
618         Client_SetUser(Client, Req->argv[1], true);
619         Client_SetOrigUser(Client, Req->argv[1]);
620         Client_SetHostname(Client, Req->argv[2]);
621         return CONNECTED;
622 } /* IRC_WEBIRC */
623
624 /**
625  * Handler for the IRC "QUIT" command.
626  *
627  * @param Client The client from which this command has been received.
628  * @param Req Request structure with prefix and all parameters.
629  * @return CONNECTED or DISCONNECTED.
630  */
631 GLOBAL bool
632 IRC_QUIT( CLIENT *Client, REQUEST *Req )
633 {
634         CLIENT *target;
635         char quitmsg[LINE_LEN];
636
637         assert(Client != NULL);
638         assert(Req != NULL);
639
640         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 1)
641
642         if (Req->argc == 1)
643                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
644
645         if (Client_Type(Client) == CLIENT_SERVER) {
646                 /* Server */
647                 target = Client_Search(Req->prefix);
648                 if (!target) {
649                         Log(LOG_WARNING,
650                             "Got QUIT from %s for unknown client!?",
651                             Client_ID(Client));
652                         return CONNECTED;
653                 }
654
655                 if (target != Client) {
656                         Client_Destroy(target, "Got QUIT command",
657                                        Req->argc == 1 ? quitmsg : NULL, true);
658                         return CONNECTED;
659                 } else {
660                         Conn_Close(Client_Conn(Client), "Got QUIT command",
661                                    Req->argc == 1 ? quitmsg : NULL, true);
662                         return DISCONNECTED;
663                 }
664         } else {
665                 if (Req->argc == 1 && quitmsg[0] != '\"') {
666                         /* " " to avoid confusion */
667                         strlcpy(quitmsg, "\"", sizeof quitmsg);
668                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
669                         strlcat(quitmsg, "\"", sizeof quitmsg );
670                 }
671
672                 /* User, Service, or not yet registered */
673                 Conn_Close(Client_Conn(Client), "Got QUIT command",
674                            Req->argc == 1 ? quitmsg : NULL, true);
675
676                 return DISCONNECTED;
677         }
678 } /* IRC_QUIT */
679
680 #ifndef STRICT_RFC
681
682 /**
683  * Handler for HTTP command, e.g. GET and POST
684  *
685  * We handle these commands here to avoid the quite long timeout when
686  * some user tries to access this IRC daemon using an web browser ...
687  *
688  * @param Client The client from which this command has been received.
689  * @param Req Request structure with prefix and all parameters.
690  * @return CONNECTED or DISCONNECTED.
691  */
692 GLOBAL bool
693 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
694 {
695         Req->argc = 1;
696         Req->argv[0] = "Oops, HTTP request received? This is IRC!";
697         return IRC_QUIT(Client, Req);
698 } /* IRC_QUIT_HTTP */
699
700 #endif
701
702 /**
703  * Handler for the IRC "PING" command.
704  *
705  * @param Client The client from which this command has been received.
706  * @param Req Request structure with prefix and all parameters.
707  * @return CONNECTED or DISCONNECTED.
708  */
709 GLOBAL bool
710 IRC_PING(CLIENT *Client, REQUEST *Req)
711 {
712         CLIENT *target, *from;
713
714         assert(Client != NULL);
715         assert(Req != NULL);
716
717         if (Req->argc < 1)
718                 return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
719                                           Client_ID(Client));
720 #ifdef STRICT_RFC
721         /* Don't ignore additional arguments when in "strict" mode */
722         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
723 #endif
724
725         if (Req->argc > 1) {
726                 /* A target has been specified ... */
727                 target = Client_Search(Req->argv[1]);
728
729                 if (!target || Client_Type(target) != CLIENT_SERVER)
730                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
731                                         Client_ID(Client), Req->argv[1]);
732
733                 if (target != Client_ThisServer()) {
734                         /* Ok, we have to forward the PING */
735                         if (Client_Type(Client) == CLIENT_SERVER)
736                                 from = Client_Search(Req->prefix);
737                         else
738                                 from = Client;
739                         if (!from)
740                                 return IRC_WriteStrClient(Client,
741                                                 ERR_NOSUCHSERVER_MSG,
742                                                 Client_ID(Client), Req->prefix);
743
744                         return IRC_WriteStrClientPrefix(target, from,
745                                         "PING %s :%s", Req->argv[0],
746                                         Req->argv[1] );
747                 }
748         }
749
750         if (Client_Type(Client) == CLIENT_SERVER) {
751                 if (Req->prefix)
752                         from = Client_Search(Req->prefix);
753                 else
754                         from = Client;
755         } else
756                 from = Client_ThisServer();
757         if (!from)
758                 return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
759                                         Client_ID(Client), Req->prefix);
760
761         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
762             Client_Conn(Client));
763
764 #ifdef STRICT_RFC
765         return IRC_WriteStrClient(Client, "PONG %s :%s",
766                 Client_ID(from), Client_ID(Client));
767 #else
768         /* Some clients depend on the argument being returned in the PONG
769          * reply (not mentioned in any RFC, though) */
770         return IRC_WriteStrClient(Client, "PONG %s :%s",
771                 Client_ID(from), Req->argv[0]);
772 #endif
773 } /* IRC_PING */
774
775 /**
776  * Handler for the IRC "PONG" command.
777  *
778  * @param Client The client from which this command has been received.
779  * @param Req Request structure with prefix and all parameters.
780  * @return CONNECTED or DISCONNECTED.
781  */
782 GLOBAL bool
783 IRC_PONG(CLIENT *Client, REQUEST *Req)
784 {
785         CLIENT *target, *from;
786         CONN_ID conn;
787 #ifndef STRICT_RFC
788         long auth_ping;
789 #endif
790         char *s;
791
792         assert(Client != NULL);
793         assert(Req != NULL);
794
795         /* Wrong number of arguments? */
796         if (Req->argc < 1) {
797                 if (Client_Type(Client) == CLIENT_USER)
798                         return IRC_WriteStrClient(Client, ERR_NOORIGIN_MSG,
799                                                   Client_ID(Client));
800                 else
801                         return CONNECTED;
802         }
803         if (Client_Type(Client) == CLIENT_USER) {
804                 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
805         }
806
807         /* Forward? */
808         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
809                 target = Client_Search(Req->argv[0]);
810                 if (!target)
811                         return IRC_WriteStrClient(Client, ERR_NOSUCHSERVER_MSG,
812                                         Client_ID(Client), Req->argv[0]);
813
814                 from = Client_Search(Req->prefix);
815
816                 if (target != Client_ThisServer() && target != from) {
817                         /* Ok, we have to forward the message. */
818                         if (!from)
819                                 return IRC_WriteStrClient(Client,
820                                                 ERR_NOSUCHSERVER_MSG,
821                                                 Client_ID(Client), Req->prefix);
822
823                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
824                                 s = Client_ID(from);
825                         else
826                                 s = Req->argv[0];
827                         return IRC_WriteStrClientPrefix(target, from,
828                                  "PONG %s :%s", s, Req->argv[1]);
829                 }
830         }
831
832         /* The connection timestamp has already been updated when the data has
833          * been read from so socket, so we don't need to update it here. */
834
835         conn = Client_Conn(Client);
836
837 #ifndef STRICT_RFC
838         /* Check authentication PING-PONG ... */
839         auth_ping = Conn_GetAuthPing(conn);
840         if (auth_ping) {
841                 LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
842                          auth_ping, Req->argv[0]);
843                 if (auth_ping == atoi(Req->argv[0])) {
844                         Conn_SetAuthPing(conn, 0);
845                         if (Client_Type(Client) == CLIENT_WAITAUTHPING)
846                                 Login_User(Client);
847                 } else
848                         if (!IRC_WriteStrClient(Client,
849                                         "To connect, type /QUOTE PONG %ld",
850                                         auth_ping))
851                                 return DISCONNECTED;
852         }
853 #endif
854
855         if (Client_Type(Client) == CLIENT_SERVER && Conn_LastPing(conn) == 0) {
856                 Log(LOG_INFO,
857                     "Synchronization with \"%s\" done (connection %d): %ld second%s [%ld users, %ld channels].",
858                     Client_ID(Client), conn, time(NULL) - Conn_GetSignon(conn),
859                     time(NULL) - Conn_GetSignon(conn) == 1 ? "" : "s",
860                     Client_UserCount(), Channel_CountVisible(NULL));
861                 Conn_UpdatePing(conn);
862         } else
863                 LogDebug("Connection %d: received PONG. Lag: %ld seconds.",
864                          conn, time(NULL) - Conn_LastPing(conn));
865
866         return CONNECTED;
867 } /* IRC_PONG */
868
869 /**
870  * Kill all users with a specific nickname in the network.
871  *
872  * @param Nick Nickname.
873  * @param Reason Reason for the KILL.
874  */
875 static void
876 Kill_Nick(char *Nick, char *Reason)
877 {
878         REQUEST r;
879
880         assert (Nick != NULL);
881         assert (Reason != NULL);
882
883         r.prefix = NULL;
884         r.argv[0] = Nick;
885         r.argv[1] = Reason;
886         r.argc = 2;
887
888         Log(LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s!",
889             Nick, Reason);
890
891         IRC_KILL(Client_ThisServer(), &r);
892 } /* Kill_Nick */
893
894 /**
895  * Change the nickname of a client.
896  *
897  * @param Origin The client which caused the nickname change.
898  * @param Target The client of which the nickname should be changed.
899  * @param NewNick The new nickname.
900  */
901 static void
902 Change_Nick(CLIENT *Origin, CLIENT *Target, char *NewNick, bool InformClient)
903 {
904         if (Client_Conn(Target) > NONE) {
905                 /* Local client */
906                 Log(LOG_INFO,
907                     "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
908                     Client_TypeText(Target), Client_Mask(Target),
909                     Client_Conn(Target), Client_ID(Target), NewNick);
910                 Conn_UpdateIdle(Client_Conn(Target));
911         } else {
912                 /* Remote client */
913                 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
914                          Client_TypeText(Target),
915                          Client_Mask(Target), Client_ID(Target), NewNick);
916         }
917
918         /* Inform all servers and users (which have to know) of the new name */
919         if (InformClient) {
920                 IRC_WriteStrClientPrefix(Target, Target, "NICK :%s", NewNick);
921                 IRC_WriteStrServersPrefix(NULL, Target, "NICK :%s", NewNick);
922         } else
923                 IRC_WriteStrServersPrefix(Origin, Target, "NICK :%s", NewNick);
924         IRC_WriteStrRelatedPrefix(Target, Target, false, "NICK :%s", NewNick);
925
926         /* Register old nickname for WHOWAS queries */
927         Client_RegisterWhowas(Target);
928
929         /* Save new nickname */
930         Client_SetID(Target, NewNick);
931 }
932
933 /* -eof- */