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