Author: Bleep <tomh@inxpress.net>
[ircu2.10.12-pk.git] / ircd / m_nick.c
1 /*
2  * IRC - Internet Relay Chat, ircd/m_nick.c
3  * Copyright (C) 1990 Jarkko Oikarinen and
4  *                    University of Oulu, Computing Center
5  *
6  * See file AUTHORS in IRC package for additional names of
7  * the programmers.
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 1, or (at your option)
12  * any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  *
23  * $Id$
24  */
25
26 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "IPcheck.h"
91 #include "client.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_chattr.h"
95 #include "ircd_reply.h"
96 #include "ircd_string.h"
97 #include "msg.h"
98 #include "numeric.h"
99 #include "numnicks.h"
100 #include "s_debug.h"
101 #include "s_misc.h"
102 #include "s_user.h"
103 #include "send.h"
104
105 #include <assert.h>
106 #include <stdlib.h>
107 #include <string.h>
108
109 /*
110  * m_nick - message handler for local clients
111  * parv[0] = sender prefix
112  * parv[1] = nickname
113  */
114 int m_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
115 {
116   struct Client* acptr;
117   char           nick[NICKLEN + 2];
118   char*          arg;
119   char*          s;
120   const char*    client_name;
121
122   assert(0 != cptr);
123   assert(cptr == sptr);
124
125   /*
126    * parv[0] will be empty for clients connecting for the first time
127    */
128   client_name = (*sptr->name) ? sptr->name : "*";
129
130   if (parc < 2) {
131     sendto_one(sptr, err_str(ERR_NONICKNAMEGIVEN), me.name, client_name);
132     return 0;
133   }
134   /*
135    * Don't let them send make us send back a really long string of
136    * garbage
137    */
138   arg = parv[1];
139   if (strlen(arg) > NICKLEN)
140     arg[NICKLEN] = '\0';
141
142   if ((s = strchr(arg, '~')))
143     *s = '\0';
144
145   strcpy(nick, arg);
146
147   /*
148    * If do_nick_name() returns a null name OR if the server sent a nick
149    * name and do_nick_name() changed it in some way (due to rules of nick
150    * creation) then reject it. If from a server and we reject it,
151    * and KILL it. -avalon 4/4/92
152    */
153   if (0 == do_nick_name(nick)) {
154     sendto_one(sptr, err_str(ERR_ERRONEUSNICKNAME), me.name, client_name, arg);
155     return 0;
156   }
157
158   /* 
159    * Check if this is a LOCAL user trying to use a reserved (Juped)
160    * nick, if so tell him that it's a nick in use...
161    */
162   if (isNickJuped(nick)) {
163     sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, client_name, nick);
164     return 0;                        /* NICK message ignored */
165   }
166
167   if (!(acptr = FindClient(nick))) {
168     /*
169      * No collisions, all clear...
170      */
171     return set_nick_name(cptr, sptr, nick, parc, parv);
172   }
173   if (IsServer(acptr)) {
174     sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, client_name, nick);
175     return 0;                        /* NICK message ignored */
176   }
177   /*
178    * If acptr == sptr, then we have a client doing a nick
179    * change between *equivalent* nicknames as far as server
180    * is concerned (user is changing the case of his/her
181    * nickname or somesuch)
182    */
183   if (acptr == sptr) {
184     /*
185      * If acptr == sptr, then we have a client doing a nick
186      * change between *equivalent* nicknames as far as server
187      * is concerned (user is changing the case of his/her
188      * nickname or somesuch)
189      */
190     if (0 != strcmp(acptr->name, nick)) {
191       /*
192        * Allows change of case in his/her nick
193        */
194       return set_nick_name(cptr, sptr, nick, parc, parv);
195     }
196     /*
197      * This is just ':old NICK old' type thing.
198      * Just forget the whole thing here. There is
199      * no point forwarding it to anywhere,
200      * especially since servers prior to this
201      * version would treat it as nick collision.
202      */
203     return 0;
204   }
205   /*
206    * Note: From this point forward it can be assumed that
207    * acptr != sptr (point to different client structures).
208    */
209   assert(acptr != sptr);
210   /*
211    * If the older one is "non-person", the new entry is just
212    * allowed to overwrite it. Just silently drop non-person,
213    * and proceed with the nick. This should take care of the
214    * "dormant nick" way of generating collisions...
215    *
216    * XXX - hmmm can this happen after one is registered?
217    */
218   if (IsUnknown(acptr) && MyConnect(acptr)) {
219     ++ServerStats->is_ref;
220     IPcheck_connect_fail(acptr->ip);
221     exit_client(cptr, acptr, &me, "Overridden by other sign on");
222     return set_nick_name(cptr, sptr, nick, parc, parv);
223   }
224   /*
225    * NICK is coming from local client connection. Just
226    * send error reply and ignore the command.
227    */
228   sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, client_name, nick);
229   return 0;                        /* NICK message ignored */
230 }
231
232
233 /*
234  * ms_nick - server message handler for nicks
235  * parv[0] = sender prefix
236  * parv[1] = nickname
237  *
238  * If from server, source is client:
239  *   parv[2] = timestamp
240  *
241  * Source is server:
242  *   parv[2] = hopcount
243  *   parv[3] = timestamp
244  *   parv[4] = username
245  *   parv[5] = hostname
246  *   parv[6] = umode (optional)
247  *   parv[parc-3] = IP#                 <- Only Protocol >= 10
248  *   parv[parc-2] = YXX, numeric nick   <- Only Protocol >= 10
249  *   parv[parc-1] = info
250  *   parv[0] = server
251  */
252 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
253 {
254   struct Client* acptr;
255   char           nick[NICKLEN + 2];
256   time_t         lastnick = 0;
257   int            differ = 1;
258
259   assert(0 != cptr);
260   assert(0 != sptr);
261   assert(IsServer(cptr));
262
263   if ((IsServer(sptr) && parc < 8) || parc < 3) {
264     sendto_ops("bad NICK param count for %s from %s", parv[1], cptr->name);
265     return need_more_params(sptr, "NICK");
266   }
267
268   ircd_strncpy(nick, parv[1], NICKLEN);
269   nick[NICKLEN] = '\0';
270
271   if (IsServer(sptr)) {
272     lastnick = atoi(parv[3]);
273     if (lastnick > OLDEST_TS) 
274       sptr->serv->lag = TStime() - lastnick;
275   }
276   else {
277     lastnick = atoi(parv[2]); 
278     if (lastnick > OLDEST_TS)
279       sptr->user->server->serv->lag = TStime() - lastnick;
280   }
281   /*
282    * If do_nick_name() returns a null name OR if the server sent a nick
283    * name and do_nick_name() changed it in some way (due to rules of nick
284    * creation) then reject it. If from a server and we reject it,
285    * and KILL it. -avalon 4/4/92
286    */
287   if (0 == do_nick_name(nick) || 0 != strcmp(nick, parv[1])) {
288     sendto_one(sptr, err_str(ERR_ERRONEUSNICKNAME), me.name, parv[0], parv[1]);
289
290     ++ServerStats->is_kill;
291     sendto_ops("Bad Nick: %s From: %s %s", parv[1], parv[0], cptr->name);
292     sendto_one(cptr, "%s " TOK_KILL " %s :%s (%s <- %s[%s])",
293                NumServ(&me), IsServer(sptr) ? parv[parc - 2] : parv[0], me.name,
294                parv[1], nick, cptr->name);
295     if (!IsServer(sptr)) {
296       /*
297        * bad nick _change_
298        */
299       sendto_highprot_butone(cptr, 10, "%s " TOK_KILL " %s :%s (%s <- %s!%s@%s)",
300                              NumServ(&me), parv[0], me.name, cptr->name,
301                              parv[0], sptr->user ? sptr->username : "",
302                              sptr->user ? sptr->user->server->name : cptr->name);
303     }
304     return 0;
305   }
306   /*
307    * Check against nick name collisions.
308    *
309    * Put this 'if' here so that the nesting goes nicely on the screen :)
310    * We check against server name list before determining if the nickname
311    * is present in the nicklist (due to the way the below for loop is
312    * constructed). -avalon
313    */
314   if (!(acptr = FindClient(nick))) {
315     /*
316      * No collisions, all clear...
317      */
318     return set_nick_name(cptr, sptr, nick, parc, parv);
319   }
320   assert(0 != acptr);
321
322   if (IsServer(acptr)) {
323     /*
324      * We have a nickname trying to use the same name as
325      * a server. Send out a nick collision KILL to remove
326      * the nickname. As long as only a KILL is sent out,
327      * there is no danger of the server being disconnected.
328      * Ultimate way to jupiter a nick ? >;-). -avalon
329      */
330     sendto_ops("Nick collision on %s(%s <- %s)", sptr->name, acptr->from->name, cptr->name);
331     ++ServerStats->is_kill;
332
333     sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (%s <- %s)",
334                NumServ(&me), NumNick(sptr), me.name, acptr->from->name,
335                cptr->name);
336
337     sptr->flags |= FLAGS_KILLED;
338     /*
339      * if sptr is a server it is exited here, nothing else to do
340      */
341     return exit_client(cptr, sptr, &me, "Nick/Server collision");
342   }
343
344   /*
345    * If acptr == sptr, then we have a client doing a nick
346    * change between *equivalent* nicknames as far as server
347    * is concerned (user is changing the case of his/her
348    * nickname or somesuch)
349    */
350   if (acptr == sptr) {
351     if (strcmp(acptr->name, nick) != 0)
352       /*
353        * Allows change of case in his/her nick
354        */
355       return set_nick_name(cptr, sptr, nick, parc, parv);
356     else
357       /*
358        * This is just ':old NICK old' type thing.
359        * Just forget the whole thing here. There is
360        * no point forwarding it to anywhere,
361        * especially since servers prior to this
362        * version would treat it as nick collision.
363        */
364       return 0;                        /* NICK Message ignored */
365   }
366
367   /*
368    * Note: From this point forward it can be assumed that
369    * acptr != sptr (point to different client structures).
370    */
371   assert(acptr != sptr);
372   /*
373    * If the older one is "non-person", the new entry is just
374    * allowed to overwrite it. Just silently drop non-person,
375    * and proceed with the nick. This should take care of the
376    * "dormant nick" way of generating collisions...
377    */
378   if (IsUnknown(acptr) && MyConnect(acptr)) {
379     ++ServerStats->is_ref;
380     IPcheck_connect_fail(acptr->ip);
381     exit_client(cptr, acptr, &me, "Overridden by other sign on");
382     return set_nick_name(cptr, sptr, nick, parc, parv);
383   }
384   /*
385    * Decide, we really have a nick collision and deal with it
386    */
387   /*
388    * NICK was coming from a server connection.
389    * This means we have a race condition (two users signing on
390    * at the same time), or two net fragments reconnecting with the same nick.
391    * The latter can happen because two different users connected
392    * or because one and the same user switched server during a net break.
393    * If the TimeStamps are equal, we kill both (or only 'new'
394    * if it was a ":server NICK new ...").
395    * Otherwise we kill the youngest when user@host differ,
396    * or the oldest when they are the same.
397    * We treat user and ~user as different, because if it wasn't
398    * a faked ~user the AUTH wouldn't have added the '~'.
399    * --Run
400    *
401    */
402   if (IsServer(sptr)) {
403     /*
404      * A new NICK being introduced by a neighbouring
405      * server (e.g. message type ":server NICK new ..." received)
406      *
407      * compare IP address and username
408      */
409     differ =  (acptr->ip.s_addr != htonl(base64toint(parv[parc - 3]))) ||
410               (0 != ircd_strcmp(acptr->user->username, parv[4]));
411     sendto_ops("Nick collision on %s (%s " TIME_T_FMT " <- %s " TIME_T_FMT
412                " (%s user@host))", acptr->name, acptr->from->name, acptr->lastnick,
413                cptr->name, lastnick, differ ? "Different" : "Same");
414   }
415   else {
416     /*
417      * A NICK change has collided (e.g. message type ":old NICK new").
418      *
419      * compare IP address and username
420      */
421     differ =  (acptr->ip.s_addr != sptr->ip.s_addr) ||
422               (0 != ircd_strcmp(acptr->user->username, sptr->user->username));              
423     sendto_ops("Nick change collision from %s to %s (%s " TIME_T_FMT " <- %s "
424                TIME_T_FMT ")", sptr->name, acptr->name, acptr->from->name,
425                acptr->lastnick, cptr->name, lastnick);
426   }
427   /*
428    * Now remove (kill) the nick on our side if it is the youngest.
429    * If no timestamp was received, we ignore the incoming nick
430    * (and expect a KILL for our legit nick soon ):
431    * When the timestamps are equal we kill both nicks. --Run
432    * acptr->from != cptr should *always* be true (?).
433    *
434    * This exits the client sending the NICK message
435    */
436   if (acptr->from != cptr) {
437     if ((differ && lastnick >= acptr->lastnick) || (!differ && lastnick <= acptr->lastnick)) {
438       if (!IsServer(sptr)) {
439         ++ServerStats->is_kill;
440         sendto_highprot_butone(cptr, 10,        /* Kill old from outgoing servers */
441                               "%s " TOK_KILL " %s%s :%s (%s <- %s (Nick collision))",
442                               NumServ(&me), NumNick(sptr), me.name, acptr->from->name,
443                               cptr->name);
444         assert(!MyConnect(sptr));
445 #if 0
446         /*
447          * XXX - impossible
448          */
449         if (MyConnect(sptr))
450           sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (Ghost 2)",
451                      NumServ(&me), NumNick(sptr), me.name);
452 #endif
453         sptr->flags |= FLAGS_KILLED;
454         exit_client(cptr, sptr, &me, "Nick collision (you're a ghost)");
455         /*
456          * we have killed sptr off, zero out it's pointer so if it's used
457          * again we'll know about it --Bleep
458          */
459         sptr = 0;
460       }
461       if (lastnick != acptr->lastnick)
462         return 0;                /* Ignore the NICK */
463     }
464     sendto_one(acptr, err_str(ERR_NICKCOLLISION), me.name, acptr->name, nick);
465   }
466
467   ++ServerStats->is_kill;
468   acptr->flags |= FLAGS_KILLED;
469   /*
470    * This exits the client we had before getting the NICK message
471    */
472   if (differ) {
473     sendto_highprot_butone(cptr, 10,        /* Kill our old from outgoing servers */
474                            "%s " TOK_KILL " %s%s :%s (%s <- %s (older nick overruled))",
475                            NumServ(&me), NumNick(acptr), me.name, acptr->from->name,
476                            cptr->name);
477     if (MyConnect(acptr))
478       sendto_one(cptr, "%s%s " TOK_QUIT " :Local kill by %s (Ghost)",
479                  NumNick(acptr), me.name);
480     exit_client(cptr, acptr, &me, "Nick collision (older nick overruled)");
481   }
482   else {
483     sendto_highprot_butone(cptr, 10,        /* Kill our old from outgoing servers */
484                           "%s " TOK_KILL " %s%s :%s (%s <- %s (nick collision from same user@host))",
485                           NumServ(&me), NumNick(acptr), me.name, acptr->from->name,
486                           cptr->name);
487     if (MyConnect(acptr))
488       sendto_one(cptr,
489                  "%s%s " TOK_QUIT " :Local kill by %s (Ghost: switched servers too fast)",
490                   NumNick(acptr), me.name);
491     exit_client(cptr, acptr, &me, "Nick collision (You collided yourself)");
492   }
493   if (lastnick == acptr->lastnick)
494     return 0;
495
496   assert(0 != sptr);
497   return set_nick_name(cptr, sptr, nick, parc, parv);
498 }
499
500 #if 0
501 /*
502  * m_nick
503  *
504  * parv[0] = sender prefix
505  * parv[1] = nickname
506  *
507  * If from server, source is client:
508  *   parv[2] = timestamp
509  *
510  * Source is server:
511  *   parv[2] = hopcount
512  *   parv[3] = timestamp
513  *   parv[4] = username
514  *   parv[5] = hostname
515  *   parv[6] = umode (optional)
516  *   parv[parc-3] = IP#                 <- Only Protocol >= 10
517  *   parv[parc-2] = YXX, numeric nick   <- Only Protocol >= 10
518  *   parv[parc-1] = info
519  *   parv[0] = server
520  */
521 int m_nick(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
522 {
523   struct Client* acptr;
524   char           nick[NICKLEN + 2];
525   char*          s;
526   time_t         lastnick = 0;
527   int            differ = 1;
528
529   if (parc < 2) {
530     sendto_one(sptr, err_str(ERR_NONICKNAMEGIVEN), me.name, parv[0]);
531     return 0;
532   }
533   else if ((IsServer(sptr) && parc < 8) || (IsServer(cptr) && parc < 3))
534   {
535     need_more_params(sptr, "NICK");
536     sendto_ops("bad NICK param count for %s from %s", parv[1], cptr->name);
537     return 0;
538   }
539   if (MyConnect(sptr) && (s = strchr(parv[1], '~')))
540     *s = '\0';
541   ircd_strncpy(nick, parv[1], NICKLEN);
542   nick[NICKLEN] = '\0';
543   if (IsServer(cptr)) {
544     if (IsServer(sptr)) {
545       lastnick = atoi(parv[3]);
546       if (lastnick > OLDEST_TS) 
547         sptr->serv->lag = TStime() - lastnick;
548     } else {
549       lastnick = atoi(parv[2]); 
550       if (lastnick > OLDEST_TS)
551        sptr->user->server->serv->lag = TStime() - lastnick;
552     }
553   }
554   /*
555    * If do_nick_name() returns a null name OR if the server sent a nick
556    * name and do_nick_name() changed it in some way (due to rules of nick
557    * creation) then reject it. If from a server and we reject it,
558    * and KILL it. -avalon 4/4/92
559    */
560   if (do_nick_name(nick) == 0 || (IsServer(cptr) && strcmp(nick, parv[1])))
561   {
562     sendto_one(sptr, err_str(ERR_ERRONEUSNICKNAME), me.name, parv[0], parv[1]);
563
564     if (IsServer(cptr))
565     {
566       ServerStats->is_kill++;
567       sendto_ops("Bad Nick: %s From: %s %s",
568           parv[1], parv[0], cptr->name);
569       sendto_one(cptr, "%s " TOK_KILL " %s :%s (%s <- %s[%s])",
570             NumServ(&me), IsServer(sptr) ? parv[parc - 2] : parv[0], me.name,
571             parv[1], nick, cptr->name);
572       if (!IsServer(sptr))        /* bad nick _change_ */
573       {
574         sendto_highprot_butone(cptr, 10, "%s " TOK_KILL " %s :%s (%s <- %s!%s@%s)",
575             NumServ(&me), parv[0], me.name, cptr->name,
576             parv[0], sptr->user ? sptr->username : "",
577             sptr->user ? sptr->user->server->name : cptr->name);
578       }
579     }
580     return 0;
581   }
582
583   /* 
584    * Check if this is a LOCAL user trying to use a reserved (Juped)
585    * nick, if so tell him that it's a nick in use...
586    */
587   if ((!IsServer(cptr)) && isNickJuped(nick))
588   {
589     sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name,
590         /* parv[0] is empty when connecting */
591         EmptyString(parv[0]) ? "*" : parv[0], nick);
592     return 0;                        /* NICK message ignored */
593   }
594
595   /*
596    * Check against nick name collisions.
597    *
598    * Put this 'if' here so that the nesting goes nicely on the screen :)
599    * We check against server name list before determining if the nickname
600    * is present in the nicklist (due to the way the below for loop is
601    * constructed). -avalon
602    */
603   if ((acptr = FindServer(nick))) {
604     if (MyConnect(sptr))
605     {
606       sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name,
607           EmptyString(parv[0]) ? "*" : parv[0], nick);
608       return 0;                        /* NICK message ignored */
609     }
610     /*
611      * We have a nickname trying to use the same name as
612      * a server. Send out a nick collision KILL to remove
613      * the nickname. As long as only a KILL is sent out,
614      * there is no danger of the server being disconnected.
615      * Ultimate way to jupiter a nick ? >;-). -avalon
616      */
617     sendto_ops("Nick collision on %s(%s <- %s)",
618                sptr->name, acptr->from->name, cptr->name);
619     ServerStats->is_kill++;
620     sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (%s <- %s)",
621                NumServ(&me), NumNick(sptr), me.name, acptr->from->name,
622                cptr->name);
623     sptr->flags |= FLAGS_KILLED;
624     return exit_client(cptr, sptr, &me, "Nick/Server collision");
625   }
626
627   if (!(acptr = FindClient(nick)))
628     return set_nick_name(cptr, sptr, nick, parc, parv);  /* No collisions, all clear... */
629   /*
630    * If acptr == sptr, then we have a client doing a nick
631    * change between *equivalent* nicknames as far as server
632    * is concerned (user is changing the case of his/her
633    * nickname or somesuch)
634    */
635   if (acptr == sptr)
636   {
637     if (strcmp(acptr->name, nick) != 0)
638       /*
639        * Allows change of case in his/her nick
640        */
641       return set_nick_name(cptr, sptr, nick, parc, parv);
642     else
643       /*
644        * This is just ':old NICK old' type thing.
645        * Just forget the whole thing here. There is
646        * no point forwarding it to anywhere,
647        * especially since servers prior to this
648        * version would treat it as nick collision.
649        */
650       return 0;                        /* NICK Message ignored */
651   }
652
653   /*
654    * Note: From this point forward it can be assumed that
655    * acptr != sptr (point to different client structures).
656    */
657   /*
658    * If the older one is "non-person", the new entry is just
659    * allowed to overwrite it. Just silently drop non-person,
660    * and proceed with the nick. This should take care of the
661    * "dormant nick" way of generating collisions...
662    */
663   if (IsUnknown(acptr) && MyConnect(acptr))
664   {
665     ++ServerStats->is_ref;
666     IPcheck_connect_fail(acptr->ip);
667     exit_client(cptr, acptr, &me, "Overridden by other sign on");
668     return set_nick_name(cptr, sptr, nick, parc, parv);
669   }
670   /*
671    * Decide, we really have a nick collision and deal with it
672    */
673   if (!IsServer(cptr))
674   {
675     /*
676      * NICK is coming from local client connection. Just
677      * send error reply and ignore the command.
678      */
679     sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name,
680         /* parv[0] is empty when connecting */
681         EmptyString(parv[0]) ? "*" : parv[0], nick);
682     return 0;                        /* NICK message ignored */
683   }
684   /*
685    * NICK was coming from a server connection.
686    * This means we have a race condition (two users signing on
687    * at the same time), or two net fragments reconnecting with the same nick.
688    * The latter can happen because two different users connected
689    * or because one and the same user switched server during a net break.
690    * If the TimeStamps are equal, we kill both (or only 'new'
691    * if it was a ":server NICK new ...").
692    * Otherwise we kill the youngest when user@host differ,
693    * or the oldest when they are the same.
694    * We treat user and ~user as different, because if it wasn't
695    * a faked ~user the AUTH wouldn't have added the '~'.
696    * --Run
697    *
698    */
699   if (IsServer(sptr))
700   {
701     /*
702      * A new NICK being introduced by a neighbouring
703      * server (e.g. message type ":server NICK new ..." received)
704      */
705     differ =  (acptr->ip.s_addr != htonl(base64toint(parv[parc - 3]))) ||
706             (0 != ircd_strcmp(acptr->user->username, parv[4]));
707     sendto_ops("Nick collision on %s (%s " TIME_T_FMT " <- %s " TIME_T_FMT
708                " (%s user@host))", acptr->name, acptr->from->name, acptr->lastnick,
709                cptr->name, lastnick, differ ? "Different" : "Same");
710   }
711   else
712   {
713     /*
714      * A NICK change has collided (e.g. message type ":old NICK new").
715      */
716     lastnick = atoi(parv[2]);
717     differ =  (acptr->ip.s_addr != sptr->ip.s_addr) ||
718             (0 != ircd_strcmp(acptr->user->username, sptr->user->username));              
719     sendto_ops("Nick change collision from %s to %s (%s " TIME_T_FMT " <- %s "
720                TIME_T_FMT ")", sptr->name, acptr->name, acptr->from->name,
721                acptr->lastnick, cptr->name, lastnick);
722   }
723   /*
724    * Now remove (kill) the nick on our side if it is the youngest.
725    * If no timestamp was received, we ignore the incoming nick
726    * (and expect a KILL for our legit nick soon ):
727    * When the timestamps are equal we kill both nicks. --Run
728    * acptr->from != cptr should *always* be true (?).
729    */
730   if (acptr->from != cptr)
731   {
732     if ((differ && lastnick >= acptr->lastnick) ||
733         (!differ && lastnick <= acptr->lastnick))
734     {
735       if (!IsServer(sptr))
736       {
737         ServerStats->is_kill++;
738         sendto_highprot_butone(cptr, 10,        /* Kill old from outgoing servers */
739                                "%s " TOK_KILL " %s%s :%s (%s <- %s (Nick collision))",
740                                NumServ(&me), NumNick(sptr), me.name, acptr->from->name,
741                                cptr->name);
742         if (MyConnect(sptr) && IsServer(cptr) && Protocol(cptr) > 9)
743           sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (Ghost2)",
744                      NumServ(&me), NumNick(sptr), me.name);
745         sptr->flags |= FLAGS_KILLED;
746         exit_client(cptr, sptr, &me, "Nick collision (you're a ghost)");
747       }
748       if (lastnick != acptr->lastnick)
749         return 0;                /* Ignore the NICK */
750     }
751     sendto_one(acptr, err_str(ERR_NICKCOLLISION), me.name, acptr->name, nick);
752   }
753   ServerStats->is_kill++;
754   acptr->flags |= FLAGS_KILLED;
755   if (differ)
756   {
757     sendto_highprot_butone(cptr, 10,        /* Kill our old from outgoing servers */
758                            "%s " TOK_KILL " %s%s :%s (%s <- %s (older nick overruled))",
759                            NumServ(&me), NumNick(acptr), me.name, acptr->from->name,
760                            cptr->name);
761     if (MyConnect(acptr) && IsServer(cptr) && Protocol(cptr) > 9)
762       sendto_one(cptr, "%s%s " TOK_QUIT " :Local kill by %s (Ghost)",
763           NumNick(acptr), me.name);
764     exit_client(cptr, acptr, &me, "Nick collision (older nick overruled)");
765   }
766   else
767   {
768     sendto_highprot_butone(cptr, 10,        /* Kill our old from outgoing servers */
769                            "%s " TOK_KILL " %s%s :%s (%s <- %s (nick collision from same user@host))",
770                            NumServ(&me), NumNick(acptr), me.name, acptr->from->name,
771                            cptr->name);
772     if (MyConnect(acptr) && IsServer(cptr) && Protocol(cptr) > 9)
773       sendto_one(cptr,
774           "%s%s " TOK_QUIT " :Local kill by %s (Ghost: switched servers too fast)",
775           NumNick(acptr), me.name);
776     exit_client(cptr, acptr, &me, "Nick collision (You collided yourself)");
777   }
778   if (lastnick == acptr->lastnick)
779     return 0;
780
781   return set_nick_name(cptr, sptr, nick, parc, parv);
782 }
783
784 #endif /* 0 */