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     ip_registry_connect_fail(acptr->ip.s_addr);
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   send_reply(sptr, ERR_NICKNAMEINUSE, 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-4] = %<lastmod>:<mask>   <- Only if matching GLINE
248  *   parv[parc-3] = IP#                 <- Only Protocol >= 10
249  *   parv[parc-2] = YXX, numeric nick   <- Only Protocol >= 10
250  *   parv[parc-1] = info
251  *   parv[0] = server
252  */
253 int ms_nick(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
254 {
255   struct Client* acptr;
256   char           nick[NICKLEN + 2];
257   time_t         lastnick = 0;
258   int            differ = 1;
259
260   assert(0 != cptr);
261   assert(0 != sptr);
262   assert(IsServer(cptr));
263
264   if ((IsServer(sptr) && parc < 8) || parc < 3) {
265     sendto_opmask_butone(0, SNO_OLDSNO, "bad NICK param count for %s from %C",
266                          parv[1], cptr);
267     return need_more_params(sptr, "NICK");
268   }
269
270   ircd_strncpy(nick, parv[1], NICKLEN);
271   nick[NICKLEN] = '\0';
272
273   if (!IsBurstOrBurstAck(sptr)) {
274      if (IsServer(sptr)) {
275        lastnick = atoi(parv[3]);
276        if (lastnick > OLDEST_TS) 
277          sptr->serv->lag = TStime() - lastnick;
278      }
279      else {
280        lastnick = atoi(parv[2]); 
281        if (lastnick > OLDEST_TS)
282          sptr->user->server->serv->lag = TStime() - lastnick;
283      }
284   }
285   /*
286    * If do_nick_name() returns a null name OR if the server sent a nick
287    * name and do_nick_name() changed it in some way (due to rules of nick
288    * creation) then reject it. If from a server and we reject it,
289    * and KILL it. -avalon 4/4/92
290    */
291   if (0 == do_nick_name(nick) || 0 != strcmp(nick, parv[1])) {
292     send_reply(sptr, ERR_ERRONEUSNICKNAME, parv[1]);
293
294     ++ServerStats->is_kill;
295     sendto_opmask_butone(0, SNO_OLDSNO, "Bad Nick: %s From: %s %C", parv[1],
296                          parv[0], cptr);
297     sendcmdto_one(&me, CMD_KILL, cptr, "%s :%s (%s <- %s[%s])",
298                   IsServer(sptr) ? parv[parc - 2] : parv[0], me.name, parv[1],
299                   nick, cptr->name);
300     if (!IsServer(sptr)) {
301       /*
302        * bad nick _change_
303        */
304       sendcmdto_serv_butone(&me, CMD_KILL, 0, "%s :%s (%s <- %s!%s@%s)",
305                             parv[0], me.name, cptr->name, parv[0],
306                             sptr->user ? sptr->username : "",
307                             sptr->user ? sptr->user->server->name :
308                             cptr->name);
309     }
310     return 0;
311   }
312   /*
313    * Check against nick name collisions.
314    *
315    * Put this 'if' here so that the nesting goes nicely on the screen :)
316    * We check against server name list before determining if the nickname
317    * is present in the nicklist (due to the way the below for loop is
318    * constructed). -avalon
319    */
320    
321   acptr = FindClient(nick);
322   if (!acptr) {
323     /*
324      * No collisions, all clear...
325      */
326     return set_nick_name(cptr, sptr, nick, parc, parv);
327   }
328   assert(0 != acptr);
329
330   if (IsServer(acptr)) {
331     /*
332      * We have a nickname trying to use the same name as
333      * a server. Send out a nick collision KILL to remove
334      * the nickname. As long as only a KILL is sent out,
335      * there is no danger of the server being disconnected.
336      * Ultimate way to jupiter a nick ? >;-). -avalon
337      */
338     sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C(%C <- %C)", sptr,
339                          acptr->from, cptr);
340     ++ServerStats->is_kill;
341
342     sendcmdto_one(&me, CMD_KILL, cptr, "%C :%s (%s <- %s)", sptr, me.name,
343                   acptr->from->name, cptr->name);
344
345     sptr->flags |= FLAGS_KILLED;
346     /*
347      * if sptr is a server it is exited here, nothing else to do
348      */
349     return exit_client(cptr, sptr, &me, "Nick/Server collision");
350   }
351
352   /*
353    * If acptr == sptr, then we have a client doing a nick
354    * change between *equivalent* nicknames as far as server
355    * is concerned (user is changing the case of his/her
356    * nickname or somesuch)
357    */
358   if (acptr == sptr) {
359     if (strcmp(acptr->name, nick) != 0)
360       /*
361        * Allows change of case in his/her nick
362        */
363       return set_nick_name(cptr, sptr, nick, parc, parv);
364     else
365       /*
366        * This is just ':old NICK old' type thing.
367        * Just forget the whole thing here. There is
368        * no point forwarding it to anywhere,
369        * especially since servers prior to this
370        * version would treat it as nick collision.
371        */
372       return 0;                        /* NICK Message ignored */
373   }
374
375   /*
376    * Note: From this point forward it can be assumed that
377    * acptr != sptr (point to different client structures).
378    */
379   assert(acptr != sptr);
380   /*
381    * If the older one is "non-person", the new entry is just
382    * allowed to overwrite it. Just silently drop non-person,
383    * and proceed with the nick. This should take care of the
384    * "dormant nick" way of generating collisions...
385    */
386   if (IsUnknown(acptr) && MyConnect(acptr)) {
387     ++ServerStats->is_ref;
388     ip_registry_connect_fail(acptr->ip.s_addr);
389     exit_client(cptr, acptr, &me, "Overridden by other sign on");
390     return set_nick_name(cptr, sptr, nick, parc, parv);
391   }
392   /*
393    * Decide, we really have a nick collision and deal with it
394    */
395   /*
396    * NICK was coming from a server connection.
397    * This means we have a race condition (two users signing on
398    * at the same time), or two net fragments reconnecting with the same nick.
399    * The latter can happen because two different users connected
400    * or because one and the same user switched server during a net break.
401    * If the TimeStamps are equal, we kill both (or only 'new'
402    * if it was a ":server NICK new ...").
403    * Otherwise we kill the youngest when user@host differ,
404    * or the oldest when they are the same.
405    * We treat user and ~user as different, because if it wasn't
406    * a faked ~user the AUTH wouldn't have added the '~'.
407    * --Run
408    *
409    */
410   if (IsServer(sptr)) {
411     /*
412      * A new NICK being introduced by a neighbouring
413      * server (e.g. message type ":server NICK new ..." received)
414      *
415      * compare IP address and username
416      */
417     differ =  (acptr->ip.s_addr != htonl(base64toint(parv[parc - 3]))) ||
418               (0 != ircd_strcmp(acptr->user->username, parv[4]));
419     sendto_opmask_butone(0, SNO_OLDSNO, "Nick collision on %C (%C %Tu <- "
420                          "%C %Tu (%s user@host))", acptr, acptr->from,
421                          acptr->lastnick, cptr, lastnick,
422                          differ ? "Different" : "Same");
423   }
424   else {
425     /*
426      * A NICK change has collided (e.g. message type ":old NICK new").
427      *
428      * compare IP address and username
429      */
430     differ =  (acptr->ip.s_addr != sptr->ip.s_addr) ||
431               (0 != ircd_strcmp(acptr->user->username, sptr->user->username));              
432     sendto_opmask_butone(0, SNO_OLDSNO, "Nick change collision from %C to "
433                          "%C (%C %Tu <- %C %Tu)", sptr, acptr, acptr->from,
434                          acptr->lastnick, cptr, lastnick);
435   }
436   /*
437    * Now remove (kill) the nick on our side if it is the youngest.
438    * If no timestamp was received, we ignore the incoming nick
439    * (and expect a KILL for our legit nick soon ):
440    * When the timestamps are equal we kill both nicks. --Run
441    * acptr->from != cptr should *always* be true (?).
442    *
443    * This exits the client sending the NICK message
444    */
445   if (acptr->from != cptr) {
446     if ((differ && lastnick >= acptr->lastnick) || (!differ && lastnick <= acptr->lastnick)) {
447       if (!IsServer(sptr)) {
448         ++ServerStats->is_kill;
449         sendcmdto_serv_butone(&me, CMD_KILL, sptr, "%C :%s (%s <- %s (Nick "
450                               "collision))", sptr, me.name, acptr->from->name,
451                               cptr->name);
452         assert(!MyConnect(sptr));
453 #if 0
454         /*
455          * XXX - impossible
456          */
457         if (MyConnect(sptr))
458           sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (Ghost 2)", /* XXX DEAD */
459                      NumServ(&me), NumNick(sptr), me.name);
460 #endif
461         sptr->flags |= FLAGS_KILLED;
462         exit_client(cptr, sptr, &me, "Nick collision (you're a ghost)");
463         /*
464          * we have killed sptr off, zero out it's pointer so if it's used
465          * again we'll know about it --Bleep
466          */
467         sptr = 0;
468       }
469       if (lastnick != acptr->lastnick)
470         return 0;                /* Ignore the NICK */
471     }
472     send_reply(acptr, ERR_NICKCOLLISION, nick);
473   }
474
475   ++ServerStats->is_kill;
476   acptr->flags |= FLAGS_KILLED;
477   /*
478    * This exits the client we had before getting the NICK message
479    */
480   if (differ) {
481     sendcmdto_serv_butone(&me, CMD_KILL, acptr, "%C :%s (%s <- %s (older "
482                           "nick overruled))", acptr, me.name,
483                           acptr->from->name, cptr->name);
484     if (MyConnect(acptr))
485       sendcmdto_one(acptr, CMD_QUIT, cptr, ":Local kill by %s (Ghost)",
486                     me.name);
487     exit_client(cptr, acptr, &me, "Nick collision (older nick overruled)");
488   }
489   else {
490     sendcmdto_serv_butone(&me, CMD_KILL, acptr, "%C :%s (%s <- %s (nick "
491                           "collision from same user@host))", acptr, me.name,
492                           acptr->from->name, cptr->name);
493     if (MyConnect(acptr))
494       sendcmdto_one(acptr, CMD_QUIT, cptr, ":Local kill by %s (Ghost: ",
495                     "switched servers too fast)", me.name);
496     exit_client(cptr, acptr, &me, "Nick collision (You collided yourself)");
497   }
498   if (lastnick == acptr->lastnick)
499     return 0;
500
501   assert(0 != sptr);
502   return set_nick_name(cptr, sptr, nick, parc, parv);
503 }
504
505 #if 0
506 /*
507  * m_nick
508  *
509  * parv[0] = sender prefix
510  * parv[1] = nickname
511  *
512  * If from server, source is client:
513  *   parv[2] = timestamp
514  *
515  * Source is server:
516  *   parv[2] = hopcount
517  *   parv[3] = timestamp
518  *   parv[4] = username
519  *   parv[5] = hostname
520  *   parv[6] = umode (optional)
521  *   parv[parc-3] = IP#                 <- Only Protocol >= 10
522  *   parv[parc-2] = YXX, numeric nick   <- Only Protocol >= 10
523  *   parv[parc-1] = info
524  *   parv[0] = server
525  */
526 int m_nick(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
527 {
528   struct Client* acptr;
529   char           nick[NICKLEN + 2];
530   char*          s;
531   time_t         lastnick = 0;
532   int            differ = 1;
533
534   if (parc < 2) {
535     sendto_one(sptr, err_str(ERR_NONICKNAMEGIVEN), me.name, parv[0]); /* XXX DEAD */
536     return 0;
537   }
538   else if ((IsServer(sptr) && parc < 8) || (IsServer(cptr) && parc < 3))
539   {
540     need_more_params(sptr, "NICK");
541     sendto_ops("bad NICK param count for %s from %s", parv[1], cptr->name); /* XXX DEAD */
542     return 0;
543   }
544   if (MyConnect(sptr) && (s = strchr(parv[1], '~')))
545     *s = '\0';
546   ircd_strncpy(nick, parv[1], NICKLEN);
547   nick[NICKLEN] = '\0';
548   if (IsServer(cptr)) {
549     if (IsServer(sptr)) {
550       lastnick = atoi(parv[3]);
551       if (lastnick > OLDEST_TS) 
552         sptr->serv->lag = TStime() - lastnick;
553     } else {
554       lastnick = atoi(parv[2]); 
555       if (lastnick > OLDEST_TS)
556        sptr->user->server->serv->lag = TStime() - lastnick;
557     }
558   }
559   /*
560    * If do_nick_name() returns a null name OR if the server sent a nick
561    * name and do_nick_name() changed it in some way (due to rules of nick
562    * creation) then reject it. If from a server and we reject it,
563    * and KILL it. -avalon 4/4/92
564    */
565   if (do_nick_name(nick) == 0 || (IsServer(cptr) && strcmp(nick, parv[1])))
566   {
567     sendto_one(sptr, err_str(ERR_ERRONEUSNICKNAME), me.name, parv[0], parv[1]); /* XXX DEAD */
568
569     if (IsServer(cptr))
570     {
571       ServerStats->is_kill++;
572       sendto_ops("Bad Nick: %s From: %s %s", /* XXX DEAD */
573           parv[1], parv[0], cptr->name);
574       sendto_one(cptr, "%s " TOK_KILL " %s :%s (%s <- %s[%s])", /* XXX DEAD */
575             NumServ(&me), IsServer(sptr) ? parv[parc - 2] : parv[0], me.name,
576             parv[1], nick, cptr->name);
577       if (!IsServer(sptr))        /* bad nick _change_ */
578       {
579         sendto_highprot_butone(&me, 10, "%s " TOK_KILL " %s :%s (%s <- %s!%s@%s)", /* XXX DEAD */
580             NumServ(&me), parv[0], me.name, cptr->name,
581             parv[0], sptr->user ? sptr->username : "",
582             sptr->user ? sptr->user->server->name : cptr->name);
583       }
584     }
585     return 0;
586   }
587
588   /* 
589    * Check if this is a LOCAL user trying to use a reserved (Juped)
590    * nick, if so tell him that it's a nick in use...
591    */
592   if ((!IsServer(cptr)) && isNickJuped(nick))
593   {
594     sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, /* XXX DEAD */
595         /* parv[0] is empty when connecting */
596         EmptyString(parv[0]) ? "*" : parv[0], nick);
597     return 0;                        /* NICK message ignored */
598   }
599
600   /*
601    * Check against nick name collisions.
602    *
603    * Put this 'if' here so that the nesting goes nicely on the screen :)
604    * We check against server name list before determining if the nickname
605    * is present in the nicklist (due to the way the below for loop is
606    * constructed). -avalon
607    */
608   if ((acptr = FindServer(nick))) {
609     if (MyConnect(sptr))
610     {
611       sendto_one(sptr, err_str(ERR_NICKNAMEINUSE), me.name, /* XXX DEAD */
612           EmptyString(parv[0]) ? "*" : parv[0], nick);
613       return 0;                        /* NICK message ignored */
614     }
615     /*
616      * We have a nickname trying to use the same name as
617      * a server. Send out a nick collision KILL to remove
618      * the nickname. As long as only a KILL is sent out,
619      * there is no danger of the server being disconnected.
620      * Ultimate way to jupiter a nick ? >;-). -avalon
621      */
622     sendto_ops("Nick collision on %s(%s <- %s)", /* XXX DEAD */
623                sptr->name, acptr->from->name, cptr->name);
624     ServerStats->is_kill++;
625     sendto_one(cptr, "%s " TOK_KILL " %s%s :%s (%s <- %s)", /* XXX DEAD */
626                NumServ(&me), NumNick(sptr), me.name, acptr->from->name,
627                cptr->name);
628     sptr->flags |= FLAGS_KILLED;
629     return exit_client(cptr, sptr, &me, "Nick/Server collision");
630   }
631
632   if (!(acptr = FindClient(nick)))
633     return set_nick_name(cptr, sptr, nick, parc, parv);  /* No collisions, all clear... */
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     ip_registry_connect_fail(acptr->ip.s_addr);
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 */