013481d2275029b664019126a6926d3e978dc222
[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_WriteErrClient(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_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
86                                           Client_ID(Client), Req->command);
87         } else {
88                 /* Registered connection, PASS command is not allowed! */
89                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
90                                           Client_ID(Client));
91         }
92
93         Conn_SetPassword(Client_Conn(Client), Req->argv[0]);
94
95         /* Protocol version */
96         if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
97                 int c2, c4;
98
99                 c2 = Req->argv[1][2];
100                 c4 = Req->argv[1][4];
101
102                 Req->argv[1][4] = '\0';
103                 protolow = atoi(&Req->argv[1][2]);
104                 Req->argv[1][2] = '\0';
105                 protohigh = atoi(Req->argv[1]);
106
107                 Req->argv[1][2] = c2;
108                 Req->argv[1][4] = c4;
109
110                 Client_SetType(Client, CLIENT_GOTPASS_2813);
111         } else {
112                 protohigh = protolow = 0;
113                 Client_SetType(Client, CLIENT_GOTPASS);
114         }
115
116         /* Protocol type, see doc/Protocol.txt */
117         if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
118                 type = &Req->argv[1][4];
119         else
120                 type = NULL;
121
122         /* Protocol flags/options */
123         if (Req->argc >= 4)
124                 orig_flags = Req->argv[3];
125         else
126                 orig_flags = "";
127
128         /* Implementation, version and IRC+ flags */
129         if (Req->argc >= 3) {
130                 char *impl, *ptr, *serverver, *flags;
131
132                 impl = Req->argv[2];
133                 ptr = strchr(impl, '|');
134                 if (ptr)
135                         *ptr = '\0';
136
137                 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
138                         /* The peer seems to be a server which supports the
139                          * IRC+ protocol (see doc/Protocol.txt). */
140                         serverver = ptr ? ptr + 1 : "?";
141                         flags = strchr(ptr ? serverver : impl, ':');
142                         if (flags) {
143                                 *flags = '\0';
144                                 flags++;
145                         } else
146                                 flags = "";
147                         Log(LOG_INFO,
148                             "Peer on connection %d announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
149                             Client_Conn(Client), impl, serverver,
150                             protohigh, protolow, flags);
151                 } else {
152                         /* The peer seems to be a server supporting the
153                          * "original" IRC protocol (RFC 2813). */
154                         if (strchr(orig_flags, 'Z'))
155                                 flags = "Z";
156                         else
157                                 flags = "";
158                         Log(LOG_INFO,
159                             "Peer on connection %d announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
160                             Client_Conn(Client), impl,
161                             protohigh, protolow, flags);
162                 }
163                 Client_SetFlags(Client, flags);
164         }
165
166         return CONNECTED;
167 } /* IRC_PASS */
168
169 /**
170  * Handler for the IRC "NICK" command.
171  *
172  * @param Client The client from which this command has been received.
173  * @param Req Request structure with prefix and all parameters.
174  * @return CONNECTED or DISCONNECTED.
175  */
176 GLOBAL bool
177 IRC_NICK( CLIENT *Client, REQUEST *Req )
178 {
179         CLIENT *intr_c, *target, *c;
180         char *nick, *user, *hostname, *modes, *info;
181         int token, hops;
182
183         assert( Client != NULL );
184         assert( Req != NULL );
185
186         /* Some IRC clients, for example BitchX, send the NICK and USER
187          * commands in the wrong order ... */
188         if(Client_Type(Client) == CLIENT_UNKNOWN
189             || Client_Type(Client) == CLIENT_GOTPASS
190             || Client_Type(Client) == CLIENT_GOTNICK
191 #ifndef STRICT_RFC
192             || Client_Type(Client) == CLIENT_GOTUSER
193 #endif
194             || Client_Type(Client) == CLIENT_USER
195             || Client_Type(Client) == CLIENT_SERVICE
196             || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
197         {
198                 /* User registration or change of nickname */
199                 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 1)
200
201                 /* Search "target" client */
202                 if (Client_Type(Client) == CLIENT_SERVER) {
203                         target = Client_Search(Req->prefix);
204                         if (!target)
205                                 return IRC_WriteErrClient(Client,
206                                                           ERR_NOSUCHNICK_MSG,
207                                                           Client_ID(Client),
208                                                           Req->argv[0]);
209                 } else {
210                         /* Is this a restricted client? */
211                         if (Client_HasMode(Client, 'r'))
212                                 return IRC_WriteErrClient(Client,
213                                                           ERR_RESTRICTED_MSG,
214                                                           Client_ID(Client));
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                         IRC_SetPenalty(Client, 2);
276                         return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
277                                                   Client_ID(Client), Req->command);
278                 }
279
280                 if (Req->argc >= 7) {
281                         /* RFC 2813 compatible syntax */
282                         nick = Req->argv[0];
283                         hops = atoi(Req->argv[1]);
284                         user = Req->argv[2];
285                         hostname = Req->argv[3];
286                         token = atoi(Req->argv[4]);
287                         modes = Req->argv[5] + 1;
288                         info = Req->argv[6];
289                 } else {
290                         /* RFC 1459 compatible syntax */
291                         nick = Req->argv[0];
292                         hops = 1;
293                         user = Req->argv[0];
294                         hostname = Client_ID(Client);
295                         token = atoi(Req->argv[1]);
296                         modes = "";
297                         info = Req->argv[0];
298                 }
299
300                 c = Client_Search(nick);
301                 if(c) {
302                         /*
303                          * the new nick is already present on this server:
304                          * the new and the old one have to be disconnected now.
305                          */
306                         Log( LOG_ERR, "Server %s introduces already registered nick \"%s\"!", Client_ID( Client ), Req->argv[0] );
307                         Kill_Nick( Req->argv[0], "Nick collision" );
308                         return CONNECTED;
309                 }
310
311                 /* Find the Server this client is connected to */
312                 intr_c = Client_GetFromToken(Client, token);
313                 if (!intr_c) {
314                         Log( LOG_ERR, "Server %s introduces nick \"%s\" on unknown server!?", Client_ID( Client ), Req->argv[0] );
315                         Kill_Nick( Req->argv[0], "Unknown server" );
316                         return CONNECTED;
317                 }
318
319                 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
320                                          token, modes, info, true);
321                 if (!c) {
322                         /* Out of memory, we need to disconnect client to keep
323                          * network state consistent! */
324                         Log(LOG_ALERT,
325                             "Can't create client structure! (on connection %d)",
326                             Client_Conn(Client));
327                         Kill_Nick(Req->argv[0], "Server error");
328                         return CONNECTED;
329                 }
330
331                 /* RFC 2813: client is now fully registered, inform all the
332                  * other servers about the new user.
333                  * RFC 1459: announce the new client only after receiving the
334                  * USER command, first we need more information! */
335                 if (Req->argc < 7) {
336                         LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
337                                  Client_Mask(c));
338                         Client_SetType(c, CLIENT_GOTNICK);
339                 } else
340                         Client_Introduce(Client, c, CLIENT_USER);
341
342                 return CONNECTED;
343         }
344         else
345                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
346                                           Client_ID(Client));
347 } /* IRC_NICK */
348
349 /**
350  * Handler for the IRC "SVSNICK" command.
351  *
352  * @param Client The client from which this command has been received.
353  * @param Req Request structure with prefix and all parameters.
354  * @return CONNECTED or DISCONNECTED.
355  */
356 GLOBAL bool
357 IRC_SVSNICK(CLIENT *Client, REQUEST *Req)
358 {
359         CLIENT *from, *target;
360
361         assert(Client != NULL);
362         assert(Req != NULL);
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_WriteErrClient(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_WriteErrClient(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_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
491                                           Client_ID(Client));
492         } else {
493                 /* Unexpected/invalid connection state? */
494                 return IRC_WriteErrClient(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_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
523                                           Client_ID(Client));
524
525         if (Client_Type(Client) != CLIENT_SERVER)
526                 return IRC_WriteErrClient(Client, ERR_ERRONEUSNICKNAME_MSG,
527                                   Client_ID(Client), Req->argv[0]);
528
529         nick = Req->argv[0];
530         user = NULL; host = NULL;
531         token = atoi(Req->argv[1]);
532         hops = atoi(Req->argv[4]);
533         info = Req->argv[5];
534
535         /* Validate service name ("nickname") */
536         c = Client_Search(nick);
537         if(c) {
538                 /* Nickname collision: disconnect (KILL) both clients! */
539                 Log(LOG_ERR, "Server %s introduces already registered service \"%s\"!",
540                     Client_ID(Client), nick);
541                 Kill_Nick(nick, "Nick collision");
542                 return CONNECTED;
543         }
544
545         /* Get the server to which the service is connected */
546         intr_c = Client_GetFromToken(Client, token);
547         if (! intr_c) {
548                 Log(LOG_ERR, "Server %s introduces service \"%s\" on unknown server!?",
549                     Client_ID(Client), nick);
550                 Kill_Nick(nick, "Unknown server");
551                 return CONNECTED;
552         }
553
554         /* Get user and host name */
555         ptr = strchr(nick, '@');
556         if (ptr) {
557                 *ptr = '\0';
558                 host = ++ptr;
559         }
560         if (!host)
561                 host = Client_Hostname(intr_c);
562         ptr = strchr(nick, '!');
563         if (ptr) {
564                 *ptr = '\0';
565                 user = ++ptr;
566         }
567         if (!user)
568                 user = nick;
569
570         /* According to RFC 2812/2813 parameter 4 <type> "is currently reserved
571          * for future usage"; but we use it to transfer the modes and check
572          * that the first character is a '+' sign and ignore it otherwise. */
573         modes = (Req->argv[3][0] == '+') ? ++Req->argv[3] : "";
574
575         c = Client_NewRemoteUser(intr_c, nick, hops, user, host,
576                                  token, modes, info, true);
577         if (! c) {
578                 /* Couldn't create client structure, so KILL the service to
579                  * keep network status consistent ... */
580                 Log(LOG_ALERT, "Can't create client structure! (on connection %d)",
581                     Client_Conn(Client));
582                 Kill_Nick(nick, "Server error");
583                 return CONNECTED;
584         }
585
586         Client_Introduce(Client, c, CLIENT_SERVICE);
587         return CONNECTED;
588 } /* IRC_SERVICE */
589
590 /**
591  * Handler for the IRC "WEBIRC" command.
592  *
593  * @param Client The client from which this command has been received.
594  * @param Req Request structure with prefix and all parameters.
595  * @return CONNECTED or DISCONNECTED.
596  */
597 GLOBAL bool
598 IRC_WEBIRC(CLIENT *Client, REQUEST *Req)
599 {
600         if (!Conf_WebircPwd[0] || strcmp(Req->argv[0], Conf_WebircPwd) != 0)
601                 return IRC_WriteErrClient(Client, ERR_PASSWDMISMATCH_MSG,
602                                           Client_ID(Client));
603
604         LogDebug("Connection %d: got valid WEBIRC command: user=%s, host=%s, ip=%s",
605                  Client_Conn(Client), Req->argv[1], Req->argv[2], Req->argv[3]);
606
607         Client_SetUser(Client, Req->argv[1], true);
608         Client_SetOrigUser(Client, Req->argv[1]);
609         Client_SetHostname(Client, Req->argv[2]);
610         Client_SetIPAText(Client, Req->argv[3]);
611
612         return CONNECTED;
613 } /* IRC_WEBIRC */
614
615 /**
616  * Handler for the IRC "QUIT" command.
617  *
618  * @param Client The client from which this command has been received.
619  * @param Req Request structure with prefix and all parameters.
620  * @return CONNECTED or DISCONNECTED.
621  */
622 GLOBAL bool
623 IRC_QUIT( CLIENT *Client, REQUEST *Req )
624 {
625         CLIENT *target;
626         char quitmsg[LINE_LEN];
627
628         assert(Client != NULL);
629         assert(Req != NULL);
630
631         if (Req->argc == 1)
632                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
633
634         if (Client_Type(Client) == CLIENT_SERVER) {
635                 /* Server */
636                 target = Client_Search(Req->prefix);
637                 if (!target) {
638                         Log(LOG_WARNING,
639                             "Got QUIT from %s for unknown client!?",
640                             Client_ID(Client));
641                         return CONNECTED;
642                 }
643
644                 if (target != Client) {
645                         Client_Destroy(target, "Got QUIT command",
646                                        Req->argc == 1 ? quitmsg : NULL, true);
647                         return CONNECTED;
648                 } else {
649                         Conn_Close(Client_Conn(Client), "Got QUIT command",
650                                    Req->argc == 1 ? quitmsg : NULL, true);
651                         return DISCONNECTED;
652                 }
653         } else {
654                 if (Req->argc == 1 && quitmsg[0] != '\"') {
655                         /* " " to avoid confusion */
656                         strlcpy(quitmsg, "\"", sizeof quitmsg);
657                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
658                         strlcat(quitmsg, "\"", sizeof quitmsg );
659                 }
660
661                 /* User, Service, or not yet registered */
662                 Conn_Close(Client_Conn(Client), "Got QUIT command",
663                            Req->argc == 1 ? quitmsg : NULL, true);
664
665                 return DISCONNECTED;
666         }
667 } /* IRC_QUIT */
668
669 #ifndef STRICT_RFC
670
671 /**
672  * Handler for HTTP command, e.g. GET and POST
673  *
674  * We handle these commands here to avoid the quite long timeout when
675  * some user tries to access this IRC daemon using an web browser ...
676  *
677  * @param Client The client from which this command has been received.
678  * @param Req Request structure with prefix and all parameters.
679  * @return CONNECTED or DISCONNECTED.
680  */
681 GLOBAL bool
682 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
683 {
684         Req->argc = 1;
685         Req->argv[0] = "Oops, HTTP request received? This is IRC!";
686         return IRC_QUIT(Client, Req);
687 } /* IRC_QUIT_HTTP */
688
689 #endif
690
691 /**
692  * Handler for the IRC "PING" command.
693  *
694  * @param Client The client from which this command has been received.
695  * @param Req Request structure with prefix and all parameters.
696  * @return CONNECTED or DISCONNECTED.
697  */
698 GLOBAL bool
699 IRC_PING(CLIENT *Client, REQUEST *Req)
700 {
701         CLIENT *target, *from;
702
703         assert(Client != NULL);
704         assert(Req != NULL);
705
706         if (Req->argc < 1)
707                 return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
708                                           Client_ID(Client));
709 #ifdef STRICT_RFC
710         /* Don't ignore additional arguments when in "strict" mode */
711         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
712 #endif
713
714         if (Req->argc > 1) {
715                 /* A target has been specified ... */
716                 target = Client_Search(Req->argv[1]);
717
718                 if (!target || Client_Type(target) != CLIENT_SERVER)
719                         return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
720                                         Client_ID(Client), Req->argv[1]);
721
722                 if (target != Client_ThisServer()) {
723                         /* Ok, we have to forward the PING */
724                         if (Client_Type(Client) == CLIENT_SERVER)
725                                 from = Client_Search(Req->prefix);
726                         else
727                                 from = Client;
728                         if (!from)
729                                 return IRC_WriteErrClient(Client,
730                                                 ERR_NOSUCHSERVER_MSG,
731                                                 Client_ID(Client), Req->prefix);
732
733                         return IRC_WriteStrClientPrefix(target, from,
734                                         "PING %s :%s", Req->argv[0],
735                                         Req->argv[1] );
736                 }
737         }
738
739         if (Client_Type(Client) == CLIENT_SERVER) {
740                 if (Req->prefix)
741                         from = Client_Search(Req->prefix);
742                 else
743                         from = Client;
744         } else
745                 from = Client_ThisServer();
746         if (!from)
747                 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
748                                         Client_ID(Client), Req->prefix);
749
750         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
751             Client_Conn(Client));
752
753 #ifdef STRICT_RFC
754         return IRC_WriteStrClient(Client, "PONG %s :%s",
755                 Client_ID(from), Client_ID(Client));
756 #else
757         /* Some clients depend on the argument being returned in the PONG
758          * reply (not mentioned in any RFC, though) */
759         return IRC_WriteStrClient(Client, "PONG %s :%s",
760                 Client_ID(from), Req->argv[0]);
761 #endif
762 } /* IRC_PING */
763
764 /**
765  * Handler for the IRC "PONG" command.
766  *
767  * @param Client The client from which this command has been received.
768  * @param Req Request structure with prefix and all parameters.
769  * @return CONNECTED or DISCONNECTED.
770  */
771 GLOBAL bool
772 IRC_PONG(CLIENT *Client, REQUEST *Req)
773 {
774         CLIENT *target, *from;
775         CONN_ID conn;
776 #ifndef STRICT_RFC
777         long auth_ping;
778 #endif
779         char *s;
780
781         assert(Client != NULL);
782         assert(Req != NULL);
783
784         /* Wrong number of arguments? */
785         if (Req->argc < 1) {
786                 if (Client_Type(Client) == CLIENT_USER)
787                         return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
788                                                   Client_ID(Client));
789                 else
790                         return CONNECTED;
791         }
792         if (Client_Type(Client) == CLIENT_USER) {
793                 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
794         }
795
796         /* Forward? */
797         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
798                 target = Client_Search(Req->argv[0]);
799                 if (!target)
800                         return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
801                                         Client_ID(Client), Req->argv[0]);
802
803                 from = Client_Search(Req->prefix);
804
805                 if (target != Client_ThisServer() && target != from) {
806                         /* Ok, we have to forward the message. */
807                         if (!from)
808                                 return IRC_WriteErrClient(Client,
809                                                 ERR_NOSUCHSERVER_MSG,
810                                                 Client_ID(Client), Req->prefix);
811
812                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
813                                 s = Client_ID(from);
814                         else
815                                 s = Req->argv[0];
816                         return IRC_WriteStrClientPrefix(target, from,
817                                  "PONG %s :%s", s, Req->argv[1]);
818                 }
819         }
820
821         /* The connection timestamp has already been updated when the data has
822          * been read from so socket, so we don't need to update it here. */
823
824         conn = Client_Conn(Client);
825
826 #ifndef STRICT_RFC
827         /* Check authentication PING-PONG ... */
828         auth_ping = Conn_GetAuthPing(conn);
829         if (auth_ping) {
830                 LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
831                          auth_ping, Req->argv[0]);
832                 if (auth_ping == atoi(Req->argv[0])) {
833                         Conn_SetAuthPing(conn, 0);
834                         if (Client_Type(Client) == CLIENT_WAITAUTHPING)
835                                 Login_User(Client);
836                 } else
837                         if (!IRC_WriteStrClient(Client,
838                                         "To connect, type /QUOTE PONG %ld",
839                                         auth_ping))
840                                 return DISCONNECTED;
841         }
842 #endif
843
844         if (Client_Type(Client) == CLIENT_SERVER && Conn_LastPing(conn) == 0) {
845                 Log(LOG_INFO,
846                     "Synchronization with \"%s\" done (connection %d): %ld second%s [%ld users, %ld channels].",
847                     Client_ID(Client), conn, time(NULL) - Conn_GetSignon(conn),
848                     time(NULL) - Conn_GetSignon(conn) == 1 ? "" : "s",
849                     Client_UserCount(), Channel_CountVisible(NULL));
850                 Conn_UpdatePing(conn);
851         } else
852                 LogDebug("Connection %d: received PONG. Lag: %ld seconds.",
853                          conn, time(NULL) - Conn_LastPing(conn));
854
855         return CONNECTED;
856 } /* IRC_PONG */
857
858 /**
859  * Kill all users with a specific nickname in the network.
860  *
861  * @param Nick Nickname.
862  * @param Reason Reason for the KILL.
863  */
864 static void
865 Kill_Nick(char *Nick, char *Reason)
866 {
867         REQUEST r;
868
869         assert (Nick != NULL);
870         assert (Reason != NULL);
871
872         r.prefix = NULL;
873         r.argv[0] = Nick;
874         r.argv[1] = Reason;
875         r.argc = 2;
876
877         Log(LOG_ERR, "User(s) with nick \"%s\" will be disconnected: %s!",
878             Nick, Reason);
879
880         IRC_KILL(Client_ThisServer(), &r);
881 } /* Kill_Nick */
882
883 /**
884  * Change the nickname of a client.
885  *
886  * @param Origin The client which caused the nickname change.
887  * @param Target The client of which the nickname should be changed.
888  * @param NewNick The new nickname.
889  */
890 static void
891 Change_Nick(CLIENT *Origin, CLIENT *Target, char *NewNick, bool InformClient)
892 {
893         if (Client_Conn(Target) > NONE) {
894                 /* Local client */
895                 Log(LOG_INFO,
896                     "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
897                     Client_TypeText(Target), Client_Mask(Target),
898                     Client_Conn(Target), Client_ID(Target), NewNick);
899                 Conn_UpdateIdle(Client_Conn(Target));
900         } else {
901                 /* Remote client */
902                 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
903                          Client_TypeText(Target),
904                          Client_Mask(Target), Client_ID(Target), NewNick);
905         }
906
907         /* Inform all servers and users (which have to know) of the new name */
908         if (InformClient) {
909                 IRC_WriteStrClientPrefix(Target, Target, "NICK :%s", NewNick);
910                 IRC_WriteStrServersPrefix(NULL, Target, "NICK :%s", NewNick);
911         } else
912                 IRC_WriteStrServersPrefix(Origin, Target, "NICK :%s", NewNick);
913         IRC_WriteStrRelatedPrefix(Target, Target, false, "NICK :%s", NewNick);
914
915         /* Register old nickname for WHOWAS queries */
916         Client_RegisterWhowas(Target);
917
918         /* Save new nickname */
919         Client_SetID(Target, NewNick);
920 }
921
922 /* -eof- */