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 * ---------------------------------------------------
16 #include "commands/cmd_nick.h"
18 extern "C" DllExport command_t* init_command(InspIRCd* Instance)
20 return new cmd_nick(Instance);
23 /** Handle nick changes from users.
24 * NOTE: If you are used to ircds based on ircd2.8, and are looking
25 * for the client introduction code in here, youre in the wrong place.
26 * You need to look in the spanningtree module for this!
28 CmdResult cmd_nick::Handle (const char** parameters, int pcnt, userrec *user)
30 char oldnick[NICKMAX];
32 if (!*parameters[0] || !*user->nick)
34 /* We cant put blanks in the parameters, so for this (extremely rare) issue we just put '*' here. */
35 user->WriteServ("432 %s * :Erroneous Nickname", *user->nick ? user->nick : "*");
39 if (irc::string(user->nick) == irc::string(parameters[0]))
41 /* If its exactly the same, even case, dont do anything. */
42 if (!strcmp(user->nick,parameters[0]))
45 /* Its a change of case. People insisted that they should be
46 * able to do silly things like this even though the RFC says
47 * the nick AAA is the same as the nick aaa.
49 strlcpy(oldnick, user->nick, NICKMAX - 1);
51 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
54 if (user->registered == REG_ALL)
55 user->WriteCommon("NICK %s",parameters[0]);
56 strlcpy(user->nick, parameters[0], NICKMAX - 1);
57 user->InvalidateCache();
58 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
63 QLine* mq = ServerInstance->XLines->matches_qline(parameters[0]);
66 ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s", parameters[0], user->nick, user->ident, user->host, mq->reason);
67 user->WriteServ("432 %s %s :Invalid nickname: %s",user->nick,parameters[0], mq->reason);
70 /* Check for nickname overruled -
71 * This happens when one user has connected and sent only NICK, and is essentially
72 * "camping" upon a nickname. To give the new user connecting a fair chance of having
73 * the nickname too, we force a nickchange on the older user (Simply the one who was
74 * here first, no TS checks need to take place here)
76 userrec* InUse = ServerInstance->FindNickOnly(parameters[0]);
77 ServerInstance->Log(DEBUG,"Nick in use");
78 if (InUse && (InUse != user) && ((ServerInstance->IsNick(parameters[0]) || allowinvalid)))
80 if (InUse->registered != REG_ALL)
82 /* change the nick of the older user to its UUID
84 InUse->WriteTo(InUse, "NICK %s", InUse->uuid);
85 InUse->WriteServ("433 %s %s :Nickname overruled.", InUse->nick, InUse->nick);
86 InUse->UpdateNickHash(InUse->uuid);
87 strlcpy(InUse->nick, InUse->uuid, NICKMAX - 1);
88 InUse->InvalidateCache();
89 /* Take away their nickname-sent state forcing them to send a nick again */
90 InUse->registered &= ~REG_NICK;
94 ServerInstance->Log(DEBUG,"Nick in use and user REG_ALL");
95 user->WriteServ("433 %s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick : "*", parameters[0]);
100 if (((!ServerInstance->IsNick(parameters[0]))) && (IS_LOCAL(user)))
104 user->WriteServ("432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
109 if (user->registered == REG_ALL)
112 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
114 // if a module returns true, the nick change is silently forbidden.
118 user->WriteCommon("NICK %s",parameters[0]);
122 strlcpy(oldnick, user->nick, NICKMAX - 1);
124 /* change the nick of the user in the users_hash */
125 user = user->UpdateNickHash(parameters[0]);
127 /* actually change the nick within the record */
128 if (!user) return CMD_FAILURE;
129 if (!*user->nick) return CMD_FAILURE;
131 strlcpy(user->nick, parameters[0], NICKMAX - 1);
133 user->InvalidateCache();
135 /* Update display nicks */
136 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
138 CUList* ulist = v->first->GetUsers();
139 CUList::iterator i = ulist->find(user);
140 if (i != ulist->end())
141 i->second = user->nick;
144 if (user->registered < REG_NICKUSER)
146 user->registered = (user->registered | REG_NICK);
148 if (ServerInstance->Config->NoUserDns)
150 user->dns_done = true;
151 ServerInstance->next_call = ServerInstance->Time();
155 user->StartDNSLookup();
158 /* Cached result or instant failure - fall right through if possible */
159 ServerInstance->next_call = ServerInstance->Time();
163 if (ServerInstance->next_call > ServerInstance->Time() + ServerInstance->Config->dns_timeout)
164 ServerInstance->next_call = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
168 if (user->registered == REG_NICKUSER)
170 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
172 FOREACH_RESULT(I_OnUserRegister,OnUserRegister(user));
176 if (user->registered == REG_ALL)
178 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
185 CmdResult cmd_nick::HandleInternal(const unsigned int id, const std::deque<classbase*> ¶meters)
187 allowinvalid = (id != 0);