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