Author: Isomer <isomer@coders.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     send_reply(sptr, ERR_NONICKNAMEGIVEN);
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     send_reply(sptr, ERR_ERRONEUSNICKNAME, 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     send_reply(sptr, ERR_NICKNAMEINUSE, 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     send_reply(sptr, ERR_NICKNAMEINUSE, 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     exit_client(cptr, acptr, &me, "Overridden by other sign on");
221     return set_nick_name(cptr, sptr, nick, parc, parv);
222   }
223   /*
224    * NICK is coming from local client connection. Just
225    * send error reply and ignore the command.
226    */
227   send_reply(sptr, ERR_NICKNAMEINUSE, nick);
228   return 0;                        /* NICK message ignored */
229 }
230
231
232 /*
233  * ms_nick - server message handler for nicks
234  * parv[0] = sender prefix
235  * parv[1] = nickname
236  *
237  * If from server, source is client:
238  *   parv[2] = timestamp
239  *
240  * Source is server:
241  *   parv[2] = hopcount
242  *   parv[3] = timestamp
243  *   parv[4] = username
244  *   parv[5] = hostname
245  *   parv[6] = umode (optional)
246  *   parv[parc-4] = %<lastmod>:<mask>   <- Only if matching GLINE
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_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
265                          parv[1], cptr);
266     return need_more_params(sptr, "NICK");
267   }
268
269   ircd_strncpy(nick, parv[1], NICKLEN);
270   nick[NICKLEN] = '\0';
271
272   if (IsServer(sptr)) {
273     lastnick = atoi(parv[3]);
274     if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr)) 
275       sptr->serv->lag = TStime() - lastnick;
276   }
277   else {
278     lastnick = atoi(parv[2]); 
279     if (lastnick > OLDEST_TS && !IsBurstOrBurstAck(sptr))
280       sptr->user->server->serv->lag = TStime() - lastnick;
281   }
282   /*
283    * If do_nick_name() returns a null name OR if the server sent a nick
284    * name and do_nick_name() changed it in some way (due to rules of nick
285    * creation) then reject it. If from a server and we reject it,
286    * and KILL it. -avalon 4/4/92
287    */
288   if (0 == do_nick_name(nick) || 0 != strcmp(nick, parv[1])) {
289     send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
290
291     ++ServerStats->is_kill;
292     sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
293                          parv[0], cptr);
294     sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
295                   IsServer(sptr) ? parv[parc - 2] : parv[0], me.name, parv[1],
296                   nick, cptr->name);
297     if (!IsServer(sptr)) {
298       /*
299        * bad nick _change_
300        */
301       sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
302                             parv[0], me.name, cptr->name, parv[0],
303                             sptr->user ? sptr->username : "",
304                             sptr->user ? sptr->user->server->name :
305                             cptr->name);
306     }
307     return 0;
308   }
309   /*
310    * Check against nick name collisions.
311    *
312    * Put this 'if' here so that the nesting goes nicely on the screen :)
313    * We check against server name list before determining if the nickname
314    * is present in the nicklist (due to the way the below for loop is
315    * constructed). -avalon
316    */
317    
318   acptr = FindClient(nick);
319   if (!acptr) {
320     /*
321      * No collisions, all clear...
322      */
323     return set_nick_name(cptr, sptr, nick, parc, parv);
324   }
325   assert(0 != acptr);
326
327   if (IsServer(acptr)) {
328     /*
329      * We have a nickname trying to use the same name as
330      * a server. Send out a nick collision KILL to remove
331      * the nickname. As long as only a KILL is sent out,
332      * there is no danger of the server being disconnected.
333      * Ultimate way to jupiter a nick ? >;-). -avalon
334      */
335     sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C(%C <- %C)", sptr,
336                          acptr->from, cptr);
337     ++ServerStats->is_kill;
338
339     sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s <- %s)", sptr, me.name,
340                   acptr->from->name, cptr->name);
341
342     sptr->flags |= FLAGS_KILLED;
343     /*
344      * if sptr is a server it is exited here, nothing else to do
345      */
346     return exit_client(cptr, sptr, &me, "Nick/Server collision");
347   }
348
349   /*
350    * If acptr == sptr, then we have a client doing a nick
351    * change between *equivalent* nicknames as far as server
352    * is concerned (user is changing the case of his/her
353    * nickname or somesuch)
354    */
355   if (acptr == sptr) {
356     if (strcmp(acptr->name, nick) != 0)
357       /*
358        * Allows change of case in his/her nick
359        */
360       return set_nick_name(cptr, sptr, nick, parc, parv);
361     else
362       /*
363        * This is just ':old NICK old' type thing.
364        * Just forget the whole thing here. There is
365        * no point forwarding it to anywhere,
366        * especially since servers prior to this
367        * version would treat it as nick collision.
368        */
369       return 0;                        /* NICK Message ignored */
370   }
371
372   /*
373    * Note: From this point forward it can be assumed that
374    * acptr != sptr (point to different client structures).
375    */
376   assert(acptr != sptr);
377   /*
378    * If the older one is "non-person", the new entry is just
379    * allowed to overwrite it. Just silently drop non-person,
380    * and proceed with the nick. This should take care of the
381    * "dormant nick" way of generating collisions...
382    */
383   if (IsUnknown(acptr) && MyConnect(acptr)) {
384     ++ServerStats->is_ref;
385     exit_client(cptr, acptr, &me, "Overridden by other sign on");
386     return set_nick_name(cptr, sptr, nick, parc, parv);
387   }
388   /*
389    * Decide, we really have a nick collision and deal with it
390    */
391   /*
392    * NICK was coming from a server connection.
393    * This means we have a race condition (two users signing on
394    * at the same time), or two net fragments reconnecting with the same nick.
395    * The latter can happen because two different users connected
396    * or because one and the same user switched server during a net break.
397    * If the TimeStamps are equal, we kill both (or only 'new'
398    * if it was a ":server NICK new ...").
399    * Otherwise we kill the youngest when user@host differ,
400    * or the oldest when they are the same.
401    * We treat user and ~user as different, because if it wasn't
402    * a faked ~user the AUTH wouldn't have added the '~'.
403    * --Run
404    *
405    */
406   if (IsServer(sptr)) {
407     /*
408      * A new NICK being introduced by a neighbouring
409      * server (e.g. message type ":server NICK new ..." received)
410      *
411      * compare IP address and username
412      */
413     differ =  (acptr->ip.s_addr != htonl(base64toint(parv[parc - 3]))) ||
414               (0 != ircd_strcmp(acptr->user->username, parv[4]));
415     sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
416                          "%C %Tu (%s user@host))", acptr, acptr->from,
417                          acptr->lastnick, cptr, lastnick,
418                          differ ? "Different" : "Same");
419   }
420   else {
421     /*
422      * A NICK change has collided (e.g. message type ":old NICK new").
423      *
424      * compare IP address and username
425      */
426     differ =  (acptr->ip.s_addr != sptr->ip.s_addr) ||
427               (0 != ircd_strcmp(acptr->user->username, sptr->user->username));              
428     sendto_opmask_butone(0, SNO_OLDSNO, "Nick change collision from %C to "
429                          "%C (%C %Tu <- %C %Tu)", sptr, acptr, acptr->from,
430                          acptr->lastnick, cptr, lastnick);
431   }
432   /*
433    * Now remove (kill) the nick on our side if it is the youngest.
434    * If no timestamp was received, we ignore the incoming nick
435    * (and expect a KILL for our legit nick soon ):
436    * When the timestamps are equal we kill both nicks. --Run
437    * acptr->from != cptr should *always* be true (?).
438    *
439    * This exits the client sending the NICK message
440    */
441   if (acptr->from != cptr) {
442     if ((differ && lastnick >= acptr->lastnick) || (!differ && lastnick <= acptr->lastnick)) {
443       if (!IsServer(sptr)) {
444         ++ServerStats->is_kill;
445         sendcmdto_serv_butone(&me, CMD_KILL, sptr, "%C :%s (%s <- %s (Nick "
446                               "collision))", sptr, me.name, acptr->from->name,
447                               cptr->name);
448         assert(!MyConnect(sptr));
449 #if 0
450         /*
451          * XXX - impossible
452          */
453         if (MyConnect(sptr))
454           sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (Ghost 2)", /* XXX DEAD */
455                      NumServ(&me), NumNick(sptr), me.name);
456 #endif
457         sptr->flags |= FLAGS_KILLED;
458         exit_client(cptr, sptr, &me, "Nick collision (you're a ghost)");
459         /*
460          * we have killed sptr off, zero out it's pointer so if it's used
461          * again we'll know about it --Bleep
462          */
463         sptr = 0;
464       }
465       if (lastnick != acptr->lastnick)
466         return 0;                /* Ignore the NICK */
467     }
468     send_reply(acptr, ERR_NICKCOLLISION, nick);
469   }
470
471   ++ServerStats->is_kill;
472   acptr->flags |= FLAGS_KILLED;
473   /*
474    * This exits the client we had before getting the NICK message
475    */
476   if (differ) {
477     sendcmdto_serv_butone(&me, CMD_KILL, acptr, "%C :%s (%s <- %s (older "
478                           "nick overruled))", acptr, me.name,
479                           acptr->from->name, cptr->name);
480     if (MyConnect(acptr))
481       sendcmdto_one(acptr, CMD_QUIT, cptr, ":Local kill by %s (Ghost)",
482                     me.name);
483     exit_client(cptr, acptr, &me, "Nick collision (older nick overruled)");
484   }
485   else {
486     sendcmdto_serv_butone(&me, CMD_KILL, acptr, "%C :%s (%s <- %s (nick "
487                           "collision from same user@host))", acptr, me.name,
488                           acptr->from->name, cptr->name);
489     if (MyConnect(acptr))
490       sendcmdto_one(acptr, CMD_QUIT, cptr, ":Local kill by %s (Ghost: ",
491                     "switched servers too fast)", me.name);
492     exit_client(cptr, acptr, &me, "Nick collision (You collided yourself)");
493   }
494   if (lastnick == acptr->lastnick)
495     return 0;
496
497   assert(0 != sptr);
498   return set_nick_name(cptr, sptr, nick, parc, parv);
499 }
500
501 #if 0
502 /*
503  * m_nick
504  *
505  * parv[0] = sender prefix
506  * parv[1] = nickname
507  *
508  * If from server, source is client:
509  *   parv[2] = timestamp
510  *
511  * Source is server:
512  *   parv[2] = hopcount
513  *   parv[3] = timestamp
514  *   parv[4] = username
515  *   parv[5] = hostname
516  *   parv[6] = umode (optional)
517  *   parv[parc-3] = IP#                 <- Only Protocol >= 10
518  *   parv[parc-2] = YXX, numeric nick   <- Only Protocol >= 10
519  *   parv[parc-1] = info
520  *   parv[0] = server
521  */
522 int m_nick(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
523 {
524   struct Client* acptr;
525   char           nick[NICKLEN + 2];
526   char*          s;
527   time_t         lastnick = 0;
528   int            differ = 1;
529
530   if (parc < 2) {
531     sendto_one(sptr, err_str(ERR_NONICKNAMEGIVEN), me.name, parv[0]); /* XXX DEAD */
532     return 0;
533   }
534   else if ((IsServer(sptr) && parc < 8) || (IsServer(cptr) && parc < 3))
535   {
536     need_more_params(sptr, "NICK");
537     sendto_ops("bad NICK param count for %s from %s", parv[1], cptr->name); /* XXX DEAD */
538     return 0;
539   }
540   if (MyConnect(sptr) && (s = strchr(parv[1], '~')))
541     *s = '\0';
542   ircd_strncpy(nick, parv[1], NICKLEN);
543   nick[NICKLEN] = '\0';
544   if (IsServer(cptr)) {
545     if (IsServer(sptr)) {
546       lastnick = atoi(parv[3]);
547       if (lastnick > OLDEST_TS) 
548         sptr->serv->lag = TStime() - lastnick;
549     } else {
550       lastnick = atoi(parv[2]); 
551       if (lastnick > OLDEST_TS)
552        sptr->user->server->serv->lag = TStime() - lastnick;
553     }
554   }
555   /*
556    * If do_nick_name() returns a null name OR if the server sent a nick
557    * name and do_nick_name() changed it in some way (due to rules of nick
558    * creation) then reject it. If from a server and we reject it,
559    * and KILL it. -avalon 4/4/92
560    */
561   if (do_nick_name(nick) == 0 || (IsServer(cptr) && strcmp(nick, parv[1])))
562   {
563     sendto_one(sptr, err_str(ERR_ERRONEUSNICKNAME), me.name, parv[0], parv[1]); /* XXX DEAD */
564
565     if (IsServer(cptr))
566     {
567       ServerStats->is_kill++;
568       sendto_ops("Bad Nick: %s From: %s %s", /* XXX DEAD */
569           parv[1], parv[0], cptr->name);
570       sendto_one(cptr, "%s " TOK_KILL " %s :%s (%s <- %s[%s])", /* XXX DEAD */
571             NumServ(&me), IsServer(sptr) ? parv[parc - 2] : parv[0], me.name,
572             parv[1], nick, cptr->name);
573       if (!IsServer(sptr))        /* bad nick _change_ */
574       {
575         sendto_highprot_butone(&me, 10, "%s " TOK_KILL " %s :%s (%s <- %s!%s@%s)", /* XXX DEAD */
576             NumServ(&me), parv[0], me.name, cptr->name,
577             parv[0], sptr->user ? sptr->username : "",
578             sptr->user ? sptr->user->server->name : cptr->name);
579       }
580     }
581     return 0;
582   }
583
584   /* 
585    * Check if this is a LOCAL user trying to use a reserved (Juped)
586    * nick, if so tell him that it's a nick in use...
587    */
588   if ((!IsServer(cptr)) && isNickJuped(nick))
589   {
590     sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, /* XXX DEAD */
591         /* parv[0] is empty when connecting */
592         EmptyString(parv[0]) ? "*" : parv[0], nick);
593     return 0;                        /* NICK message ignored */
594   }
595
596   /*
597    * Check against nick name collisions.
598    *
599    * Put this 'if' here so that the nesting goes nicely on the screen :)
600    * We check against server name list before determining if the nickname
601    * is present in the nicklist (due to the way the below for loop is
602    * constructed). -avalon
603    */
604    
605   acptr = FindServer(nick);
606   
607   if (acptr) { /* There is a nick collision with a server */
608     if (MyConnect(sptr))
609     {
610       /* Local user trying to use a nick thats a server
611        * Return an error message and ignore the command
612        */
613       sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, /* XXX DEAD */
614           EmptyString(parv[0]) ? "*" : parv[0], nick);
615       return 0;                        /* NICK message ignored */
616     }
617     
618     /*
619      * We have a nickname trying to use the same name as
620      * a server. Send out a nick collision KILL to remove
621      * the nickname. As long as only a KILL is sent out,
622      * there is no danger of the server being disconnected.
623      * Ultimate way to jupiter a nick ? >;-). -avalon
624      */
625     sendto_ops("Nick collision on %s(%s <- %s)", /* XXX DEAD */
626                sptr->name, acptr->from->name, cptr->name);
627     ServerStats->is_kill++;
628     sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (%s <- %s)", /* XXX DEAD */
629                NumServ(&me), NumNick(sptr), me.name, acptr->from->name,
630                cptr->name);
631     sptr->flags |= FLAGS_KILLED;
632     return exit_client(cptr, sptr, &me, "Nick/Server collision");
633   }
634   
635   acptr = FindClient(nick);
636
637   /* No collisions?  Set the nick name and we're done */
638   if (!acptr)
639     return set_nick_name(cptr, sptr, nick, parc, parv);
640   /*
641    * If acptr == sptr, then we have a client doing a nick
642    * change between *equivalent* nicknames as far as server
643    * is concerned (user is changing the case of his/her
644    * nickname or somesuch)
645    */
646   if (acptr == sptr)
647   {
648     if (strcmp(acptr->name, nick) != 0)
649       /*
650        * Allows change of case in his/her nick
651        */
652       return set_nick_name(cptr, sptr, nick, parc, parv);
653     else
654       /*
655        * This is just ':old NICK old' type thing.
656        * Just forget the whole thing here. There is
657        * no point forwarding it to anywhere,
658        * especially since servers prior to this
659        * version would treat it as nick collision.
660        */
661       return 0;                        /* NICK Message ignored */
662   }
663
664   /*
665    * Note: From this point forward it can be assumed that
666    * acptr != sptr (point to different client structures).
667    */
668   /*
669    * If the older one is "non-person", the new entry is just
670    * allowed to overwrite it. Just silently drop non-person,
671    * and proceed with the nick. This should take care of the
672    * "dormant nick" way of generating collisions...
673    */
674   if (IsUnknown(acptr) && MyConnect(acptr))
675   {
676     ++ServerStats->is_ref;
677     exit_client(cptr, acptr, &me, "Overridden by other sign on");
678     return set_nick_name(cptr, sptr, nick, parc, parv);
679   }
680   /*
681    * Decide, we really have a nick collision and deal with it
682    */
683   if (!IsServer(cptr))
684   {
685     /*
686      * NICK is coming from local client connection. Just
687      * send error reply and ignore the command.
688      */
689     sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, /* XXX DEAD */
690         /* parv[0] is empty when connecting */
691         EmptyString(parv[0]) ? "*" : parv[0], nick);
692     return 0;                        /* NICK message ignored */
693   }
694   /*
695    * NICK was coming from a server connection.
696    * This means we have a race condition (two users signing on
697    * at the same time), or two net fragments reconnecting with the same nick.
698    * The latter can happen because two different users connected
699    * or because one and the same user switched server during a net break.
700    * If the TimeStamps are equal, we kill both (or only 'new'
701    * if it was a ":server NICK new ...").
702    * Otherwise we kill the youngest when user@host differ,
703    * or the oldest when they are the same.
704    * We treat user and ~user as different, because if it wasn't
705    * a faked ~user the AUTH wouldn't have added the '~'.
706    * --Run
707    *
708    */
709   if (IsServer(sptr))
710   {
711     /*
712      * A new NICK being introduced by a neighbouring
713      * server (e.g. message type ":server NICK new ..." received)
714      */
715     differ =  (acptr->ip.s_addr != htonl(base64toint(parv[parc - 3]))) ||
716             (0 != ircd_strcmp(acptr->user->username, parv[4]));
717     sendto_ops("Nick collision on %s (%s " TIME_T_FMT " <- %s " TIME_T_FMT /* XXX DEAD */
718                " (%s user@host))", acptr->name, acptr->from->name, acptr->lastnick,
719                cptr->name, lastnick, differ ? "Different" : "Same");
720   }
721   else
722   {
723     /*
724      * A NICK change has collided (e.g. message type ":old NICK new").
725      */
726     lastnick = atoi(parv[2]);
727     differ =  (acptr->ip.s_addr != sptr->ip.s_addr) ||
728             (0 != ircd_strcmp(acptr->user->username, sptr->user->username));              
729     sendto_ops("Nick change collision from %s to %s (%s " TIME_T_FMT " <- %s " /* XXX DEAD */
730                TIME_T_FMT ")", sptr->name, acptr->name, acptr->from->name,
731                acptr->lastnick, cptr->name, lastnick);
732   }
733   /*
734    * Now remove (kill) the nick on our side if it is the youngest.
735    * If no timestamp was received, we ignore the incoming nick
736    * (and expect a KILL for our legit nick soon ):
737    * When the timestamps are equal we kill both nicks. --Run
738    * acptr->from != cptr should *always* be true (?).
739    */
740   if (acptr->from != cptr)
741   {
742     if ((differ && lastnick >= acptr->lastnick) ||
743         (!differ && lastnick <= acptr->lastnick))
744     {
745       if (!IsServer(sptr))
746       {
747         ServerStats->is_kill++;
748         sendto_highprot_butone(cptr, 10,        /* Kill old from outgoing servers */ /* XXX DEAD */
749                                "%s " TOK_KILL " %s%s :%s (%s <- %s (Nick collision))",
750                                NumServ(&me), NumNick(sptr), me.name, acptr->from->name,
751                                cptr->name);
752         if (MyConnect(sptr) && IsServer(cptr) && Protocol(cptr) > 9)
753           sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (Ghost2)", /* XXX DEAD */
754                      NumServ(&me), NumNick(sptr), me.name);
755         sptr->flags |= FLAGS_KILLED;
756         exit_client(cptr, sptr, &me, "Nick collision (you're a ghost)");
757       }
758       if (lastnick != acptr->lastnick)
759         return 0;                /* Ignore the NICK */
760     }
761     sendto_one(acptr, err_str(ERR_NICKCOLLISION), me.name, acptr->name, nick); /* XXX DEAD */
762   }
763   ServerStats->is_kill++;
764   acptr->flags |= FLAGS_KILLED;
765   if (differ)
766   {
767     sendto_highprot_butone(cptr, 10,        /* Kill our old from outgoing servers */ /* XXX DEAD */
768                            "%s " TOK_KILL " %s%s :%s (%s <- %s (older nick overruled))",
769                            NumServ(&me), NumNick(acptr), me.name, acptr->from->name,
770                            cptr->name);
771     if (MyConnect(acptr) && IsServer(cptr) && Protocol(cptr) > 9)
772       sendto_one(cptr, "%s%s " TOK_QUIT " :Local kill by %s (Ghost)", /* XXX DEAD */
773           NumNick(acptr), me.name);
774     exit_client(cptr, acptr, &me, "Nick collision (older nick overruled)");
775   }
776   else
777   {
778     sendto_highprot_butone(cptr, 10,        /* Kill our old from outgoing servers */ /* XXX DEAD */
779                            "%s " TOK_KILL " %s%s :%s (%s <- %s (nick collision from same user@host))",
780                            NumServ(&me), NumNick(acptr), me.name, acptr->from->name,
781                            cptr->name);
782     if (MyConnect(acptr) && IsServer(cptr) && Protocol(cptr) > 9)
783       sendto_one(cptr, /* XXX DEAD */
784           "%s%s " TOK_QUIT " :Local kill by %s (Ghost: switched servers too fast)",
785           NumNick(acptr), me.name);
786     exit_client(cptr, acptr, &me, "Nick collision (You collided yourself)");
787   }
788   if (lastnick == acptr->lastnick)
789     return 0;
790
791   return set_nick_name(cptr, sptr, nick, parc, parv);
792 }
793
794 #endif /* 0 */