]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/irc-login.c
b7fe5feda3339b9659012f7352563a8c2af3b8cf
[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                 return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
84                                           Client_ID(Client), Req->command);
85         } else {
86                 /* Registered connection, PASS command is not allowed! */
87                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
88                                           Client_ID(Client));
89         }
90
91         Conn_SetPassword(Client_Conn(Client), Req->argv[0]);
92
93         /* Protocol version */
94         if (Req->argc >= 2 && strlen(Req->argv[1]) >= 4) {
95                 int c2, c4;
96
97                 c2 = Req->argv[1][2];
98                 c4 = Req->argv[1][4];
99
100                 Req->argv[1][4] = '\0';
101                 protolow = atoi(&Req->argv[1][2]);
102                 Req->argv[1][2] = '\0';
103                 protohigh = atoi(Req->argv[1]);
104
105                 Req->argv[1][2] = c2;
106                 Req->argv[1][4] = c4;
107
108                 Client_SetType(Client, CLIENT_GOTPASS_2813);
109         } else {
110                 protohigh = protolow = 0;
111                 Client_SetType(Client, CLIENT_GOTPASS);
112         }
113
114         /* Protocol type, see doc/Protocol.txt */
115         if (Req->argc >= 2 && strlen(Req->argv[1]) > 4)
116                 type = &Req->argv[1][4];
117         else
118                 type = NULL;
119
120         /* Protocol flags/options */
121         if (Req->argc >= 4)
122                 orig_flags = Req->argv[3];
123         else
124                 orig_flags = "";
125
126         /* Implementation, version and IRC+ flags */
127         if (Req->argc >= 3) {
128                 char *impl, *ptr, *serverver, *flags;
129
130                 impl = Req->argv[2];
131                 ptr = strchr(impl, '|');
132                 if (ptr)
133                         *ptr = '\0';
134
135                 if (type && strcmp(type, PROTOIRCPLUS) == 0) {
136                         /* The peer seems to be a server which supports the
137                          * IRC+ protocol (see doc/Protocol.txt). */
138                         serverver = ptr ? ptr + 1 : "?";
139                         flags = strchr(ptr ? serverver : impl, ':');
140                         if (flags) {
141                                 *flags = '\0';
142                                 flags++;
143                         } else
144                                 flags = "";
145                         Log(LOG_INFO,
146                             "Peer on connection %d announces itself as %s-%s using protocol %d.%d/IRC+ (flags: \"%s\").",
147                             Client_Conn(Client), impl, serverver,
148                             protohigh, protolow, flags);
149                 } else {
150                         /* The peer seems to be a server supporting the
151                          * "original" IRC protocol (RFC 2813). */
152                         if (strchr(orig_flags, 'Z'))
153                                 flags = "Z";
154                         else
155                                 flags = "";
156                         Log(LOG_INFO,
157                             "Peer on connection %d announces itself as \"%s\" using protocol %d.%d (flags: \"%s\").",
158                             Client_Conn(Client), impl,
159                             protohigh, protolow, flags);
160                 }
161                 Client_SetFlags(Client, flags);
162         }
163
164         return CONNECTED;
165 } /* IRC_PASS */
166
167 /**
168  * Handler for the IRC "NICK" command.
169  *
170  * @param Client The client from which this command has been received.
171  * @param Req Request structure with prefix and all parameters.
172  * @return CONNECTED or DISCONNECTED.
173  */
174 GLOBAL bool
175 IRC_NICK( CLIENT *Client, REQUEST *Req )
176 {
177         CLIENT *intr_c, *target, *c;
178         char *nick, *user, *hostname, *modes, *info;
179         int token, hops;
180
181         assert( Client != NULL );
182         assert( Req != NULL );
183
184         /* Some IRC clients, for example BitchX, send the NICK and USER
185          * commands in the wrong order ... */
186         if(Client_Type(Client) == CLIENT_UNKNOWN
187             || Client_Type(Client) == CLIENT_GOTPASS
188             || Client_Type(Client) == CLIENT_GOTNICK
189 #ifndef STRICT_RFC
190             || Client_Type(Client) == CLIENT_GOTUSER
191 #endif
192             || Client_Type(Client) == CLIENT_USER
193             || Client_Type(Client) == CLIENT_SERVICE
194             || (Client_Type(Client) == CLIENT_SERVER && Req->argc == 1))
195         {
196                 /* User registration or change of nickname */
197                 _IRC_ARGC_EQ_OR_RETURN_(Client, Req, 1)
198
199                 /* Search "target" client */
200                 if (Client_Type(Client) == CLIENT_SERVER) {
201                         target = Client_Search(Req->prefix);
202                         if (!target)
203                                 return IRC_WriteErrClient(Client,
204                                                           ERR_NOSUCHNICK_MSG,
205                                                           Client_ID(Client),
206                                                           Req->argv[0]);
207                 } else {
208                         /* Is this a restricted client? */
209                         if (Client_HasMode(Client, 'r'))
210                                 return IRC_WriteErrClient(Client,
211                                                           ERR_RESTRICTED_MSG,
212                                                           Client_ID(Client));
213                         target = Client;
214                 }
215
216 #ifndef STRICT_RFC
217                 /* If the clients tries to change to its own nickname we won't
218                  * do anything. This is how the original ircd behaves and some
219                  * clients (for example Snak) expect it to be like this.
220                  * But I doubt that this is "really the right thing" ... */
221                 if (strcmp(Client_ID(target), Req->argv[0]) == 0)
222                         return CONNECTED;
223 #endif
224
225                 /* Check that the new nickname is available. Special case:
226                  * the client only changes from/to upper to lower case. */
227                 if (strcasecmp(Client_ID(target), Req->argv[0]) != 0) {
228                         if (!Client_CheckNick(target, Req->argv[0]))
229                                 return CONNECTED;
230                 }
231
232                 if (Client_Type(target) != CLIENT_USER &&
233                     Client_Type(target) != CLIENT_SERVICE &&
234                     Client_Type(target) != CLIENT_SERVER) {
235                         /* New client */
236                         LogDebug("Connection %d: got valid NICK command ...",
237                              Client_Conn( Client ));
238
239                         /* Register new nickname of this client */
240                         Client_SetID( target, Req->argv[0] );
241
242 #ifndef STRICT_RFC
243                         if (Conf_AuthPing) {
244 #ifdef HAVE_ARC4RANDOM
245                                 Conn_SetAuthPing(Client_Conn(Client), arc4random());
246 #else
247                                 Conn_SetAuthPing(Client_Conn(Client), rand());
248 #endif
249                                 IRC_WriteStrClient(Client, "PING :%ld",
250                                         Conn_GetAuthPing(Client_Conn(Client)));
251                                 LogDebug("Connection %d: sent AUTH PING %ld ...",
252                                         Client_Conn(Client),
253                                         Conn_GetAuthPing(Client_Conn(Client)));
254                         }
255 #endif
256
257                         /* If we received a valid USER command already then
258                          * register the new client! */
259                         if( Client_Type( Client ) == CLIENT_GOTUSER )
260                                 return Login_User( Client );
261                         else
262                                 Client_SetType( Client, CLIENT_GOTNICK );
263                 } else {
264                         /* Nickname change */
265                         Change_Nick(Client, target, Req->argv[0],
266                                     Client_Type(Client) == CLIENT_USER ? true : false);
267                         IRC_SetPenalty(target, 2);
268                 }
269
270                 return CONNECTED;
271         } else if(Client_Type(Client) == CLIENT_SERVER ||
272                   Client_Type(Client) == CLIENT_SERVICE) {
273                 /* Server or service introduces new client */
274
275                 /* Bad number of parameters? */
276                 if (Req->argc != 2 && Req->argc != 7)
277                         return IRC_WriteErrClient(Client, ERR_NEEDMOREPARAMS_MSG,
278                                                   Client_ID(Client), Req->command);
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,
307                             "Server %s introduces already registered nick \"%s\"!",
308                             Client_ID(Client), Req->argv[0]);
309                         return IRC_KillClient(Client, NULL, Req->argv[0],
310                                               "Nick collision");
311                 }
312
313                 /* Find the Server this client is connected to */
314                 intr_c = Client_GetFromToken(Client, token);
315                 if (!intr_c) {
316                         Log(LOG_ERR,
317                             "Server %s introduces nick \"%s\" on unknown server!?",
318                             Client_ID(Client), Req->argv[0]);
319                         return IRC_KillClient(Client, NULL, Req->argv[0],
320                                               "Unknown server");
321                 }
322
323                 c = Client_NewRemoteUser(intr_c, nick, hops, user, hostname,
324                                          token, modes, info, true);
325                 if (!c) {
326                         /* Out of memory, we need to disconnect client to keep
327                          * network state consistent! */
328                         Log(LOG_ALERT,
329                             "Can't create client structure! (on connection %d)",
330                             Client_Conn(Client));
331                         return IRC_KillClient(Client, NULL, Req->argv[0],
332                                               "Server error");
333                 }
334
335                 /* RFC 2813: client is now fully registered, inform all the
336                  * other servers about the new user.
337                  * RFC 1459: announce the new client only after receiving the
338                  * USER command, first we need more information! */
339                 if (Req->argc < 7) {
340                         LogDebug("Client \"%s\" is being registered (RFC 1459) ...",
341                                  Client_Mask(c));
342                         Client_SetType(c, CLIENT_GOTNICK);
343                 } else
344                         Client_Introduce(Client, c, CLIENT_USER);
345
346                 return CONNECTED;
347         }
348         else
349                 return IRC_WriteErrClient(Client, ERR_ALREADYREGISTRED_MSG,
350                                           Client_ID(Client));
351 } /* IRC_NICK */
352
353 /**
354  * Handler for the IRC "SVSNICK" command.
355  *
356  * @param Client The client from which this command has been received.
357  * @param Req Request structure with prefix and all parameters.
358  * @return CONNECTED or DISCONNECTED.
359  */
360 GLOBAL bool
361 IRC_SVSNICK(CLIENT *Client, REQUEST *Req)
362 {
363         CLIENT *from, *target;
364
365         assert(Client != NULL);
366         assert(Req != NULL);
367
368         /* Search the originator */
369         from = Client_Search(Req->prefix);
370         if (!from)
371                 from = Client;
372
373         /* Search the target */
374         target = Client_Search(Req->argv[0]);
375         if (!target || Client_Type(target) != CLIENT_USER)
376                 return IRC_WriteErrClient(Client, ERR_NOSUCHNICK_MSG,
377                                           Client_ID(Client), Req->argv[0]);
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         if (Conf_DNS)
613                 Client_SetHostname(Client, Req->argv[2]);
614         else
615                 Client_SetHostname(Client, Req->argv[3]);
616         Client_SetIPAText(Client, Req->argv[3]);
617
618         return CONNECTED;
619 } /* IRC_WEBIRC */
620
621 /**
622  * Handler for the IRC "QUIT" command.
623  *
624  * @param Client The client from which this command has been received.
625  * @param Req Request structure with prefix and all parameters.
626  * @return CONNECTED or DISCONNECTED.
627  */
628 GLOBAL bool
629 IRC_QUIT( CLIENT *Client, REQUEST *Req )
630 {
631         CLIENT *target;
632         char quitmsg[COMMAND_LEN];
633
634         assert(Client != NULL);
635         assert(Req != NULL);
636
637         if (Req->argc == 1)
638                 strlcpy(quitmsg, Req->argv[0], sizeof quitmsg);
639
640         if (Client_Type(Client) == CLIENT_SERVER) {
641                 /* Server */
642                 target = Client_Search(Req->prefix);
643                 if (!target) {
644                         Log(LOG_WARNING,
645                             "Got QUIT from %s for unknown client!?",
646                             Client_ID(Client));
647                         return CONNECTED;
648                 }
649
650                 if (target != Client) {
651                         Client_Destroy(target, "Got QUIT command",
652                                        Req->argc == 1 ? quitmsg : NULL, true);
653                         return CONNECTED;
654                 } else {
655                         Conn_Close(Client_Conn(Client), "Got QUIT command",
656                                    Req->argc == 1 ? quitmsg : NULL, true);
657                         return DISCONNECTED;
658                 }
659         } else {
660                 if (Req->argc == 1 && quitmsg[0] != '\"') {
661                         /* " " to avoid confusion */
662                         strlcpy(quitmsg, "\"", sizeof quitmsg);
663                         strlcat(quitmsg, Req->argv[0], sizeof quitmsg-1);
664                         strlcat(quitmsg, "\"", sizeof quitmsg );
665                 }
666
667                 /* User, Service, or not yet registered */
668                 Conn_Close(Client_Conn(Client), "Got QUIT command",
669                            Req->argc == 1 ? quitmsg : NULL, true);
670
671                 return DISCONNECTED;
672         }
673 } /* IRC_QUIT */
674
675 #ifndef STRICT_RFC
676
677 /**
678  * Handler for HTTP command, e.g. GET and POST
679  *
680  * We handle these commands here to avoid the quite long timeout when
681  * some user tries to access this IRC daemon using an web browser ...
682  *
683  * @param Client The client from which this command has been received.
684  * @param Req Request structure with prefix and all parameters.
685  * @return CONNECTED or DISCONNECTED.
686  */
687 GLOBAL bool
688 IRC_QUIT_HTTP( CLIENT *Client, REQUEST *Req )
689 {
690         Req->argc = 1;
691         Req->argv[0] = "Oops, HTTP request received? This is IRC!";
692         return IRC_QUIT(Client, Req);
693 } /* IRC_QUIT_HTTP */
694
695 #endif
696
697 /**
698  * Handler for the IRC "PING" command.
699  *
700  * @param Client The client from which this command has been received.
701  * @param Req Request structure with prefix and all parameters.
702  * @return CONNECTED or DISCONNECTED.
703  */
704 GLOBAL bool
705 IRC_PING(CLIENT *Client, REQUEST *Req)
706 {
707         CLIENT *target, *from;
708
709         assert(Client != NULL);
710         assert(Req != NULL);
711
712         if (Req->argc < 1)
713                 return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
714                                           Client_ID(Client));
715 #ifdef STRICT_RFC
716         /* Don't ignore additional arguments when in "strict" mode */
717         _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
718 #endif
719
720         if (Req->argc > 1) {
721                 /* A target has been specified ... */
722                 target = Client_Search(Req->argv[1]);
723
724                 if (!target || Client_Type(target) != CLIENT_SERVER)
725                         return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
726                                         Client_ID(Client), Req->argv[1]);
727
728                 if (target != Client_ThisServer()) {
729                         /* Ok, we have to forward the PING */
730                         if (Client_Type(Client) == CLIENT_SERVER)
731                                 from = Client_Search(Req->prefix);
732                         else
733                                 from = Client;
734                         if (!from)
735                                 return IRC_WriteErrClient(Client,
736                                                 ERR_NOSUCHSERVER_MSG,
737                                                 Client_ID(Client), Req->prefix);
738
739                         return IRC_WriteStrClientPrefix(target, from,
740                                         "PING %s :%s", Req->argv[0],
741                                         Req->argv[1] );
742                 }
743         }
744
745         if (Client_Type(Client) == CLIENT_SERVER) {
746                 if (Req->prefix)
747                         from = Client_Search(Req->prefix);
748                 else
749                         from = Client;
750         } else
751                 from = Client_ThisServer();
752         if (!from)
753                 return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
754                                         Client_ID(Client), Req->prefix);
755
756         Log(LOG_DEBUG, "Connection %d: got PING, sending PONG ...",
757             Client_Conn(Client));
758
759 #ifdef STRICT_RFC
760         return IRC_WriteStrClient(Client, "PONG %s :%s",
761                 Client_ID(from), Client_ID(Client));
762 #else
763         /* Some clients depend on the argument being returned in the PONG
764          * reply (not mentioned in any RFC, though) */
765         return IRC_WriteStrClient(Client, "PONG %s :%s",
766                 Client_ID(from), Req->argv[0]);
767 #endif
768 } /* IRC_PING */
769
770 /**
771  * Handler for the IRC "PONG" command.
772  *
773  * @param Client The client from which this command has been received.
774  * @param Req Request structure with prefix and all parameters.
775  * @return CONNECTED or DISCONNECTED.
776  */
777 GLOBAL bool
778 IRC_PONG(CLIENT *Client, REQUEST *Req)
779 {
780         CLIENT *target, *from;
781         CONN_ID conn;
782 #ifndef STRICT_RFC
783         long auth_ping;
784 #endif
785         char *s;
786
787         assert(Client != NULL);
788         assert(Req != NULL);
789
790         /* Wrong number of arguments? */
791         if (Req->argc < 1) {
792                 if (Client_Type(Client) == CLIENT_USER)
793                         return IRC_WriteErrClient(Client, ERR_NOORIGIN_MSG,
794                                                   Client_ID(Client));
795                 else
796                         return CONNECTED;
797         }
798         if (Client_Type(Client) == CLIENT_USER) {
799                 _IRC_ARGC_LE_OR_RETURN_(Client, Req, 2)
800         }
801
802         /* Forward? */
803         if (Req->argc == 2 && Client_Type(Client) == CLIENT_SERVER) {
804                 target = Client_Search(Req->argv[0]);
805                 if (!target)
806                         return IRC_WriteErrClient(Client, ERR_NOSUCHSERVER_MSG,
807                                         Client_ID(Client), Req->argv[0]);
808
809                 from = Client_Search(Req->prefix);
810
811                 if (target != Client_ThisServer() && target != from) {
812                         /* Ok, we have to forward the message. */
813                         if (!from)
814                                 return IRC_WriteErrClient(Client,
815                                                 ERR_NOSUCHSERVER_MSG,
816                                                 Client_ID(Client), Req->prefix);
817
818                         if (Client_Type(Client_NextHop(target)) != CLIENT_SERVER)
819                                 s = Client_ID(from);
820                         else
821                                 s = Req->argv[0];
822                         return IRC_WriteStrClientPrefix(target, from,
823                                  "PONG %s :%s", s, Req->argv[1]);
824                 }
825         }
826
827         /* The connection timestamp has already been updated when the data has
828          * been read from so socket, so we don't need to update it here. */
829
830         conn = Client_Conn(Client);
831
832 #ifndef STRICT_RFC
833         /* Check authentication PING-PONG ... */
834         auth_ping = Conn_GetAuthPing(conn);
835         if (auth_ping) {
836                 LogDebug("AUTH PONG: waiting for token \"%ld\", got \"%s\" ...",
837                          auth_ping, Req->argv[0]);
838                 if (auth_ping == atoi(Req->argv[0])) {
839                         Conn_SetAuthPing(conn, 0);
840                         if (Client_Type(Client) == CLIENT_WAITAUTHPING)
841                                 Login_User(Client);
842                 } else
843                         if (!IRC_WriteStrClient(Client,
844                                         "To connect, type /QUOTE PONG %ld",
845                                         auth_ping))
846                                 return DISCONNECTED;
847         }
848 #endif
849
850         if (Client_Type(Client) == CLIENT_SERVER && Conn_LastPing(conn) == 0) {
851                 Log(LOG_INFO,
852                     "Synchronization with \"%s\" done (connection %d): %ld second%s [%ld users, %ld channels].",
853                     Client_ID(Client), conn, time(NULL) - Conn_GetSignon(conn),
854                     time(NULL) - Conn_GetSignon(conn) == 1 ? "" : "s",
855                     Client_UserCount(), Channel_CountVisible(NULL));
856                 Conn_UpdatePing(conn);
857         } else
858                 LogDebug("Connection %d: received PONG. Lag: %ld seconds.",
859                          conn, time(NULL) - Conn_LastPing(conn));
860
861         return CONNECTED;
862 } /* IRC_PONG */
863
864 /**
865  * Change the nickname of a client.
866  *
867  * @param Origin The client which caused the nickname change.
868  * @param Target The client of which the nickname should be changed.
869  * @param NewNick The new nickname.
870  */
871 static void
872 Change_Nick(CLIENT *Origin, CLIENT *Target, char *NewNick, bool InformClient)
873 {
874         if (Client_Conn(Target) > NONE) {
875                 /* Local client */
876                 Log(LOG_INFO,
877                     "%s \"%s\" changed nick (connection %d): \"%s\" -> \"%s\".",
878                     Client_TypeText(Target), Client_Mask(Target),
879                     Client_Conn(Target), Client_ID(Target), NewNick);
880                 Conn_UpdateIdle(Client_Conn(Target));
881         } else {
882                 /* Remote client */
883                 LogDebug("%s \"%s\" changed nick: \"%s\" -> \"%s\".",
884                          Client_TypeText(Target),
885                          Client_Mask(Target), Client_ID(Target), NewNick);
886         }
887
888         /* Inform all servers and users (which have to know) of the new name */
889         if (InformClient) {
890                 IRC_WriteStrClientPrefix(Target, Target, "NICK :%s", NewNick);
891                 IRC_WriteStrServersPrefix(NULL, Target, "NICK :%s", NewNick);
892         } else
893                 IRC_WriteStrServersPrefix(Origin, Target, "NICK :%s", NewNick);
894         IRC_WriteStrRelatedPrefix(Target, Target, false, "NICK :%s", NewNick);
895
896         /* Register old nickname for WHOWAS queries */
897         Client_RegisterWhowas(Target);
898
899         /* Save new nickname */
900         Client_SetID(Target, NewNick);
901 }
902
903 /* -eof- */