]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
New function IRC_KillClient() to kill clients
[ngircd-alex.git] / src / ngircd / irc-login.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2013 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 Change_Nick PARAMS((CLIENT * Origin, CLIENT * Target, char *NewNick,
43                                 bool InformClient));
44
45 /**
46  * Handler for the IRC "PASS" command.
47  *
48  * @param Client The client from which this command has been received.
49  * @param Req Request structure with prefix and all parameters.
50  * @return CONNECTED or DISCONNECTED.
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_WriteErrClient(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                 IRC_SetPenalty(Client, 2);
84                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
85                                           Client_ID(Client), Req->command);
86         } else {
87                 /* Registered connection, PASS command is not allowed! */
88                 return IRC_WriteErrClient(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_WriteErrClient(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_WriteErrClient(Client,
212                                                           ERR_RESTRICTED_MSG,
213                                                           Client_ID(Client));
214                         target = Client;
215                 }
216
217 #ifndef STRICT_RFC
218                 /* If the clients tries to change to its own nickname we won't
219                  * do anything. This is how the original ircd behaves and some
220                  * clients (for example Snak) expect it to be like this.
221                  * But I doubt that this is "really the right thing" ... */
222                 if (strcmp(Client_ID(target), Req->argv[0]) == 0)
223                         return CONNECTED;
224 #endif
225
226                 /* Check that the new nickname is available. Special case:
227                  * the client only changes from/to upper to lower case. */
228                 if (strcasecmp(Client_ID(target), Req->argv[0]) != 0) {
229                         if (!Client_CheckNick(target, Req->argv[0]))
230                                 return CONNECTED;
231                 }
232
233                 if (Client_Type(target) != CLIENT_USER &&
234                     Client_Type(target) != CLIENT_SERVICE &&
235                     Client_Type(target) != CLIENT_SERVER) {
236                         /* New client */
237                         LogDebug("Connection %d: got valid NICK command ...",
238                              Client_Conn( Client ));
239
240                         /* Register new nickname of this client */
241                         Client_SetID( target, Req->argv[0] );
242
243 #ifndef STRICT_RFC
244                         if (Conf_AuthPing) {
245                                 Conn_SetAuthPing(Client_Conn(Client), rand());
246                                 IRC_WriteStrClient(Client, "PING :%ld",
247                                         Conn_GetAuthPing(Client_Conn(Client)));
248                                 LogDebug("Connection %d: sent AUTH PING %ld ...",
249                                         Client_Conn(Client),
250                                         Conn_GetAuthPing(Client_Conn(Client)));
251                         }
252 #endif
253
254                         /* If we received a valid USER command already then
255                          * register the new client! */
256                         if( Client_Type( Client ) == CLIENT_GOTUSER )
257                                 return Login_User( Client );
258                         else
259                                 Client_SetType( Client, CLIENT_GOTNICK );
260                 } else {
261                         /* Nickname change */
262                         Change_Nick(Client, target, Req->argv[0],
263                                     Client_Type(Client) == CLIENT_USER ? true : false);
264                         IRC_SetPenalty(target, 2);
265                 }
266
267                 return CONNECTED;
268         } else if(Client_Type(Client) == CLIENT_SERVER ||
269                   Client_Type(Client) == CLIENT_SERVICE) {
270                 /* Server or service introduces new client */
271
272                 /* Bad number of parameters? */
273                 if (Req->argc != 2 && Req->argc != 7) {
274                         IRC_SetPenalty(Client, 2);
275                         return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
276                                                   Client_ID(Client), Req->command);
277                 }
278
279                 if (Req->argc >= 7) {
280                         /* RFC 2813 compatible syntax */
281                         nick = Req->argv[0];
282                         hops = atoi(Req->argv[1]);
283                         user = Req->argv[2];
284                         hostname = Req->argv[3];
285                         token = atoi(Req->argv[4]);
286                         modes = Req->argv[5] + 1;
287                         info = Req->argv[6];
288                 } else {
289                         /* RFC 1459 compatible syntax */
290                         nick = Req->argv[0];
291                         hops = 1;
292                         user = Req->argv[0];
293                         hostname = Client_ID(Client);
294                         token = atoi(Req->argv[1]);
295                         modes = "";
296                         info = Req->argv[0];
297                 }
298
299                 c = Client_Search(nick);
300                 if(c) {
301                         /*
302                          * the new nick is already present on this server:
303                          * the new and the old one have to be disconnected now.
304                          */
305                         Log(LOG_ERR,
306                             "Server %s introduces already registered nick \"%s\"!",
307                             Client_ID(Client), Req->argv[0]);
308                         return IRC_KillClient(Client, NULL, Req->argv[0],
309                                               "Nick collision");
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,
316                             "Server %s introduces nick \"%s\" on unknown server!?",
317                             Client_ID(Client), Req->argv[0]);
318                         return IRC_KillClient(Client, NULL, Req->argv[0],
319                                               "Unknown server");
320                 }
321
322                 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
323                                          token, modes, info, true);
324                 if (!c) {
325                         /* Out of memory, we need to disconnect client to keep
326                          * network state consistent! */
327                         Log(LOG_ALERT,
328                             "Can't create client structure! (on connection %d)",
329                             Client_Conn(Client));
330                         return IRC_KillClient(Client, NULL, Req->argv[0],
331                                               "Server error");
332                 }
333
334                 /* RFC 2813: client is now fully registered, inform all the
335                  * other servers about the new user.
336                  * RFC 1459: announce the new client only after receiving the
337                  * USER command, first we need more information! */
338                 if (Req->argc < 7) {
339                         LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
340                                  Client_Mask(c));
341                         Client_SetType(c, CLIENT_GOTNICK);
342                 } else
343                         Client_Introduce(Client, c, CLIENT_USER);
344
345                 return CONNECTED;
346         }
347         else
348                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
349                                           Client_ID(Client));
350 } /* IRC_NICK */
351
352 /**
353  * Handler for the IRC "SVSNICK" command.
354  *
355  * @param Client The client from which this command has been received.
356  * @param Req Request structure with prefix and all parameters.
357  * @return CONNECTED or DISCONNECTED.
358  */
359 GLOBAL bool
360 IRC_SVSNICK(CLIENT *Client, REQUEST *Req)
361 {
362         CLIENT *from, *target;
363
364         assert(Client != NULL);
365         assert(Req != NULL);
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_WriteErrClient(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_WriteErrClient(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                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
494                                           Client_ID(Client));
495         } else {
496                 /* Unexpected/invalid connection state? */
497                 return IRC_WriteErrClient(Client, ERR_NOTREGISTERED_MSG,
498                                           Client_ID(Client));
499         }
500 } /* IRC_USER */
501
502 /**
503  * Handler for the IRC "SERVICE" command.
504  *
505  * At the moment ngIRCd doesn't support directly linked services, so this
506  * function returns ERR_ERRONEUSNICKNAME when the SERVICE command has not been
507  * received from a peer server.
508  *
509  * @param Client The client from which this command has been received.
510  * @param Req Request structure with prefix and all parameters.
511  * @return CONNECTED or DISCONNECTED.
512  */
513 GLOBAL bool
514 IRC_SERVICE(CLIENT *Client, REQUEST *Req)
515 {
516         CLIENT *c, *intr_c;
517         char *nick, *user, *host, *info, *modes, *ptr;
518         int token, hops;
519
520         assert(Client != NULL);
521         assert(Req != NULL);
522
523         if (Client_Type(Client) != CLIENT_GOTPASS &&
524             Client_Type(Client) != CLIENT_SERVER)
525                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
526                                           Client_ID(Client));
527
528         if (Client_Type(Client) != CLIENT_SERVER)
529                 return IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
530                                   Client_ID(Client), Req->argv[0]);
531
532         nick = Req->argv[0];
533         user = NULL; host = NULL;
534         token = atoi(Req->argv[1]);
535         hops = atoi(Req->argv[4]);
536         info = Req->argv[5];
537
538         /* Validate service name ("nickname") */
539         c = Client_Search(nick);
540         if(c) {
541                 /* Nickname collision: disconnect (KILL) both clients! */
542                 Log(LOG_ERR,
543                     "Server %s introduces already registered service \"%s\"!",
544                     Client_ID(Client), nick);
545                 return IRC_KillClient(Client, NULL, nick, "Nick collision");
546         }
547
548         /* Get the server to which the service is connected */
549         intr_c = Client_GetFromToken(Client, token);
550         if (! intr_c) {
551                 Log(LOG_ERR,
552                     "Server %s introduces service \"%s\" on unknown server!?",
553                     Client_ID(Client), nick);
554                 return IRC_KillClient(Client, NULL, nick, "Unknown server");
555         }
556
557         /* Get user and host name */
558         ptr = strchr(nick, '@');
559         if (ptr) {
560                 *ptr = '\0';
561                 host = ++ptr;
562         }
563         if (!host)
564                 host = Client_Hostname(intr_c);
565         ptr = strchr(nick, '!');
566         if (ptr) {
567                 *ptr = '\0';
568                 user = ++ptr;
569         }
570         if (!user)
571                 user = nick;
572
573         /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
574          * for future usage"; but we use it to transfer the modes and check
575          * that the first character is a '+' sign and ignore it otherwise. */
576         modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
577
578         c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
579                                  token, modes, info, true);
580         if (! c) {
581                 /* Couldn't create client structure, so KILL the service to
582                  * keep network status consistent ... */
583                 Log(LOG_ALERT,
584                     "Can't create client structure! (on connection %d)",
585                     Client_Conn(Client));
586                 return IRC_KillClient(Client, NULL, nick, "Server error");
587         }
588
589         Client_Introduce(Client, c, CLIENT_SERVICE);
590         return CONNECTED;
591 } /* IRC_SERVICE */
592
593 /**
594  * Handler for the IRC "WEBIRC" command.
595  *
596  * @param Client The client from which this command has been received.
597  * @param Req Request structure with prefix and all parameters.
598  * @return CONNECTED or DISCONNECTED.
599  */
600 GLOBAL bool
601 IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
602 {
603         if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
604                 return IRC_WriteErrClient(Client, ERR_PASSWDMISMATCH_MSG,
605                                           Client_ID(Client));
606
607         LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
608                  Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
609
610         Client_SetUser(Client, Req->argv[1], true);
611         Client_SetOrigUser(Client, Req->argv[1]);
612         Client_SetHostname(Client, Req->argv[2]);
613         Client_SetIPAText(Client, Req->argv[3]);
614
615         return CONNECTED;
616 } /* IRC_WEBIRC */
617
618 /**
619  * Handler for the IRC "QUIT" command.
620  *
621  * @param Client The client from which this command has been received.
622  * @param Req Request structure with prefix and all parameters.
623  * @return CONNECTED or DISCONNECTED.
624  */
625 GLOBAL bool
626 IRC_QUIT( CLIENT *Client, REQUEST *Req )
627 {
628         CLIENT *target;
629         char quitmsg[LINE_LEN];
630
631         assert(Client != NULL);
632         assert(Req != NULL);
633
634         if (Req->argc == 1)
635                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
636
637         if (Client_Type(Client) == CLIENT_SERVER) {
638                 /* Server */
639                 target = Client_Search(Req->prefix);
640                 if (!target) {
641                         Log(LOG_WARNING,
642                             "Got QUIT from %s for unknown client!?",
643                             Client_ID(Client));
644                         return CONNECTED;
645                 }
646
647                 if (target != Client) {
648                         Client_Destroy(target, "Got QUIT command",
649                                        Req->argc == 1 ? quitmsg : NULL, true);
650                         return CONNECTED;
651                 } else {
652                         Conn_Close(Client_Conn(Client), "Got QUIT command",
653                                    Req->argc == 1 ? quitmsg : NULL, true);
654                         return DISCONNECTED;
655                 }
656         } else {
657                 if (Req->argc == 1 && quitmsg[0] != '\"') {
658                         /* " " to avoid confusion */
659                         strlcpy(quitmsg, "\"", sizeof quitmsg);
660                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
661                         strlcat(quitmsg, "\"", sizeof quitmsg );
662                 }
663
664                 /* User, Service, or not yet registered */
665                 Conn_Close(Client_Conn(Client), "Got QUIT command",
666                            Req->argc == 1 ? quitmsg : NULL, true);
667
668                 return DISCONNECTED;
669         }
670 } /* IRC_QUIT */
671
672 #ifndef STRICT_RFC
673
674 /**
675  * Handler for HTTP command, e.g. GET and POST
676  *
677  * We handle these commands here to avoid the quite long timeout when
678  * some user tries to access this IRC daemon using an web browser ...
679  *
680  * @param Client The client from which this command has been received.
681  * @param Req Request structure with prefix and all parameters.
682  * @return CONNECTED or DISCONNECTED.
683  */
684 GLOBAL bool
685 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
686 {
687         Req->argc = 1;
688         Req->argv[0] = "Oops, HTTP request received? This is IRC!";
689         return IRC_QUIT(Client, Req);
690 } /* IRC_QUIT_HTTP */
691
692 #endif
693
694 /**
695  * Handler for the IRC "PING" command.
696  *
697  * @param Client The client from which this command has been received.
698  * @param Req Request structure with prefix and all parameters.
699  * @return CONNECTED or DISCONNECTED.
700  */
701 GLOBAL bool
702 IRC_PING(CLIENT *Client, REQUEST *Req)
703 {
704         CLIENT *target, *from;
705
706         assert(Client != NULL);
707         assert(Req != NULL);
708
709         if (Req->argc < 1)
710                 return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
711                                           Client_ID(Client));
712 #ifdef STRICT_RFC
713         /* Don't ignore additional arguments when in "strict" mode */
714         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
715 #endif
716
717         if (Req->argc > 1) {
718                 /* A target has been specified ... */
719                 target = Client_Search(Req->argv[1]);
720
721                 if (!target || Client_Type(target) != CLIENT_SERVER)
722                         return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
723                                         Client_ID(Client), Req->argv[1]);
724
725                 if (target != Client_ThisServer()) {
726                         /* Ok, we have to forward the PING */
727                         if (Client_Type(Client) == CLIENT_SERVER)
728                                 from = Client_Search(Req->prefix);
729                         else
730                                 from = Client;
731                         if (!from)
732                                 return IRC_WriteErrClient(Client,
733                                                 ERR_NOSUCHSERVER_MSG,
734                                                 Client_ID(Client), Req->prefix);
735
736                         return IRC_WriteStrClientPrefix(target, from,
737                                         "PING %s :%s", Req->argv[0],
738                                         Req->argv[1] );
739                 }
740         }
741
742         if (Client_Type(Client) == CLIENT_SERVER) {
743                 if (Req->prefix)
744                         from = Client_Search(Req->prefix);
745                 else
746                         from = Client;
747         } else
748                 from = Client_ThisServer();
749         if (!from)
750                 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
751                                         Client_ID(Client), Req->prefix);
752
753         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
754             Client_Conn(Client));
755
756 #ifdef STRICT_RFC
757         return IRC_WriteStrClient(Client, "PONG %s :%s",
758                 Client_ID(from), Client_ID(Client));
759 #else
760         /* Some clients depend on the argument being returned in the PONG
761          * reply (not mentioned in any RFC, though) */
762         return IRC_WriteStrClient(Client, "PONG %s :%s",
763                 Client_ID(from), Req->argv[0]);
764 #endif
765 } /* IRC_PING */
766
767 /**
768  * Handler for the IRC "PONG" command.
769  *
770  * @param Client The client from which this command has been received.
771  * @param Req Request structure with prefix and all parameters.
772  * @return CONNECTED or DISCONNECTED.
773  */
774 GLOBAL bool
775 IRC_PONG(CLIENT *Client, REQUEST *Req)
776 {
777         CLIENT *target, *from;
778         CONN_ID conn;
779 #ifndef STRICT_RFC
780         long auth_ping;
781 #endif
782         char *s;
783
784         assert(Client != NULL);
785         assert(Req != NULL);
786
787         /* Wrong number of arguments? */
788         if (Req->argc < 1) {
789                 if (Client_Type(Client) == CLIENT_USER)
790                         return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
791                                                   Client_ID(Client));
792                 else
793                         return CONNECTED;
794         }
795         if (Client_Type(Client) == CLIENT_USER) {
796                 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
797         }
798
799         /* Forward? */
800         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
801                 target = Client_Search(Req->argv[0]);
802                 if (!target)
803                         return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
804                                         Client_ID(Client), Req->argv[0]);
805
806                 from = Client_Search(Req->prefix);
807
808                 if (target != Client_ThisServer() && target != from) {
809                         /* Ok, we have to forward the message. */
810                         if (!from)
811                                 return IRC_WriteErrClient(Client,
812                                                 ERR_NOSUCHSERVER_MSG,
813                                                 Client_ID(Client), Req->prefix);
814
815                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
816                                 s = Client_ID(from);
817                         else
818                                 s = Req->argv[0];
819                         return IRC_WriteStrClientPrefix(target, from,
820                                  "PONG %s :%s", s, Req->argv[1]);
821                 }
822         }
823
824         /* The connection timestamp has already been updated when the data has
825          * been read from so socket, so we don't need to update it here. */
826
827         conn = Client_Conn(Client);
828
829 #ifndef STRICT_RFC
830         /* Check authentication PING-PONG ... */
831         auth_ping = Conn_GetAuthPing(conn);
832         if (auth_ping) {
833                 LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
834                          auth_ping, Req->argv[0]);
835                 if (auth_ping == atoi(Req->argv[0])) {
836                         Conn_SetAuthPing(conn, 0);
837                         if (Client_Type(Client) == CLIENT_WAITAUTHPING)
838                                 Login_User(Client);
839                 } else
840                         if (!IRC_WriteStrClient(Client,
841                                         "To connect, type /QUOTE PONG %ld",
842                                         auth_ping))
843                                 return DISCONNECTED;
844         }
845 #endif
846
847         if (Client_Type(Client) == CLIENT_SERVER && Conn_LastPing(conn) == 0) {
848                 Log(LOG_INFO,
849                     "Synchronization with \"%s\" done (connection %d): %ld second%s [%ld users, %ld channels].",
850                     Client_ID(Client), conn, time(NULL) - Conn_GetSignon(conn),
851                     time(NULL) - Conn_GetSignon(conn) == 1 ? "" : "s",
852                     Client_UserCount(), Channel_CountVisible(NULL));
853                 Conn_UpdatePing(conn);
854         } else
855                 LogDebug("Connection %d: received PONG. Lag: %ld seconds.",
856                          conn, time(NULL) - Conn_LastPing(conn));
857
858         return CONNECTED;
859 } /* IRC_PONG */
860
861 /**
862  * Change the nickname of a client.
863  *
864  * @param Origin The client which caused the nickname change.
865  * @param Target The client of which the nickname should be changed.
866  * @param NewNick The new nickname.
867  */
868 static void
869 Change_Nick(CLIENT *Origin, CLIENT *Target, char *NewNick, bool InformClient)
870 {
871         if (Client_Conn(Target) > NONE) {
872                 /* Local client */
873                 Log(LOG_INFO,
874                     "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
875                     Client_TypeText(Target), Client_Mask(Target),
876                     Client_Conn(Target), Client_ID(Target), NewNick);
877                 Conn_UpdateIdle(Client_Conn(Target));
878         } else {
879                 /* Remote client */
880                 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
881                          Client_TypeText(Target),
882                          Client_Mask(Target), Client_ID(Target), NewNick);
883         }
884
885         /* Inform all servers and users (which have to know) of the new name */
886         if (InformClient) {
887                 IRC_WriteStrClientPrefix(Target, Target, "NICK :%s", NewNick);
888                 IRC_WriteStrServersPrefix(NULL, Target, "NICK :%s", NewNick);
889         } else
890                 IRC_WriteStrServersPrefix(Origin, Target, "NICK :%s", NewNick);
891         IRC_WriteStrRelatedPrefix(Target, Target, false, "NICK :%s", NewNick);
892
893         /* Register old nickname for WHOWAS queries */
894         Client_RegisterWhowas(Target);
895
896         /* Save new nickname */
897         Client_SetID(Target, NewNick);
898 }
899
900 /* -eof- */