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