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