1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
14 #include "configreader.h"
19 #include "commands/cmd_nick.h"
21 extern "C" command_t* init_command(InspIRCd* Instance)
23 return new cmd_nick(Instance);
26 /** Handle nick changes from users.
27 * NOTE: If you are used to ircds based on ircd2.8, and are looking
28 * for the client introduction code in here, youre in the wrong place.
29 * You need to look in the spanningtree module for this!
31 CmdResult cmd_nick::Handle (const char** parameters, int pcnt, userrec *user)
33 char oldnick[NICKMAX];
35 if (!*parameters[0] || !*user->nick)
38 if (irc::string(user->nick) == irc::string(parameters[0]))
40 /* If its exactly the same, even case, dont do anything. */
41 if (!strcmp(user->nick,parameters[0]))
44 /* Its a change of case. People insisted that they should be
45 * able to do silly things like this even though the RFC says
46 * the nick AAA is the same as the nick aaa.
48 strlcpy(oldnick, user->nick, NICKMAX - 1);
50 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
53 if (user->registered == REG_ALL)
54 user->WriteCommon("NICK %s",parameters[0]);
55 strlcpy(user->nick, parameters[0], NICKMAX - 1);
56 user->InvalidateCache();
57 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
62 QLine* mq = ServerInstance->XLines->matches_qline(parameters[0]);
65 ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s", parameters[0], user->nick, user->ident, user->host, mq->reason);
66 user->WriteServ("432 %s %s :Invalid nickname: %s",user->nick,parameters[0], mq->reason);
69 /* Check for nickname overruled -
70 * This happens when one user has connected and sent only NICK, and is essentially
71 * "camping" upon a nickname. To give the new user connecting a fair chance of having
72 * the nickname too, we force a nickchange on the older user (Simply the one who was
73 * here first, no TS checks need to take place here)
75 userrec* InUse = ServerInstance->FindNick(parameters[0]);
76 if (InUse && (InUse != user) && (ServerInstance->IsNick(parameters[0])))
78 if (InUse->registered != REG_ALL)
80 /* change the nick of the older user to nnn-overruled,
81 * where nnn is their file descriptor. We know this to be unique.
82 * NOTE: We must do this and not quit the user, even though we do
83 * not have UID support yet. This is because if we set this user
84 * as quitting and then introduce the new user before the old one
85 * has quit, then the user hash gets totally buggered.
86 * (Yes, that is a technical term). -- Brain
88 std::string changeback = ConvToStr(InUse->GetFd()) + "-overruled";
89 InUse->WriteTo(InUse, "NICK %s", changeback.c_str());
90 InUse->WriteServ("433 %s %s :Nickname overruled.", InUse->nick, InUse->nick);
91 InUse->UpdateNickHash(changeback.c_str());
92 strlcpy(InUse->nick, changeback.c_str(), NICKMAX - 1);
93 InUse->InvalidateCache();
94 /* Take away their nickname-sent state forcing them to send a nick again */
95 InUse->registered &= ~REG_NICK;
99 user->WriteServ("433 %s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick : "*", parameters[0]);
104 if ((!ServerInstance->IsNick(parameters[0])) && (IS_LOCAL(user)))
106 user->WriteServ("432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
110 if (user->registered == REG_ALL)
113 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
115 // if a module returns true, the nick change is silently forbidden.
119 user->WriteCommon("NICK %s",parameters[0]);
123 strlcpy(oldnick, user->nick, NICKMAX - 1);
125 /* change the nick of the user in the users_hash */
126 user = user->UpdateNickHash(parameters[0]);
128 /* actually change the nick within the record */
129 if (!user) return CMD_FAILURE;
130 if (!*user->nick) return CMD_FAILURE;
132 strlcpy(user->nick, parameters[0], NICKMAX - 1);
134 user->InvalidateCache();
136 if (user->registered < REG_NICKUSER)
138 user->registered = (user->registered | REG_NICK);
140 if (ServerInstance->Config->NoUserDns)
142 user->dns_done = true;
143 ServerInstance->next_call = ServerInstance->Time();
147 user->StartDNSLookup();
150 /* Cached result or instant failure - fall right through if possible */
151 ServerInstance->next_call = ServerInstance->Time();
155 if (ServerInstance->next_call > ServerInstance->Time() + ServerInstance->Config->dns_timeout)
156 ServerInstance->next_call = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
160 if (user->registered == REG_NICKUSER)
162 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
164 FOREACH_RESULT(I_OnUserRegister,OnUserRegister(user));
168 if (user->registered == REG_ALL)
170 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));