1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
16 /* +------------------------------------+
17 * | Inspire Internet Relay Chat Daemon |
18 * +------------------------------------+
20 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
21 * See: http://wiki.inspircd.org/Credits
23 * This program is free but copyrighted software; see
24 * the file COPYING for details.
26 * ---------------------------------------------------
29 #ifndef __CMD_NICK_H__
30 #define __CMD_NICK_H__
32 // include the common header files
37 /** Handle /NICK. These command handlers can be reloaded by the core,
38 * and handle basic RFC1459 commands. Commands within modules work
39 * the same way, however, they can be fully unloaded, where these
42 class CommandNick : public Command
46 /** Constructor for nick.
48 CommandNick (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"NICK", 0, 1, true, 3), allowinvalid(false) { syntax = "<newnick>"; }
50 * @param parameters The parameters to the comamnd
51 * @param pcnt The number of parameters passed to teh command
52 * @param user The user issuing the command
53 * @return A value from CmdResult to indicate command success or failure.
55 CmdResult Handle(const std::vector<std::string>& parameters, User *user);
57 /** Handle internal command
58 * @param id Used to indicate if invalid nick changes are allowed.
59 * Set to 1 to allow invalid nicks and 0 to deny them.
60 * @param parameters Currently unused
62 CmdResult HandleInternal(const unsigned int id, const std::deque<classbase*> ¶meters);
68 /** Handle nick changes from users.
69 * NOTE: If you are used to ircds based on ircd2.8, and are looking
70 * for the client introduction code in here, youre in the wrong place.
71 * You need to look in the spanningtree module for this!
73 CmdResult CommandNick::Handle (const std::vector<std::string>& parameters, User *user)
77 if (parameters[0].empty())
79 /* We cant put blanks in the parameters, so for this (extremely rare) issue we just put '*' here. */
80 user->WriteNumeric(432, "%s * :Erroneous Nickname", user->nick.empty() ? user->nick.c_str() : "*");
84 if (((!ServerInstance->IsNick(parameters[0].c_str(), ServerInstance->Config->Limits.NickMax))) && (IS_LOCAL(user)))
88 if (parameters[0] == "0")
90 // Special case, Fake a /nick UIDHERE. Useful for evading "ERR: NICK IN USE" on connect etc.
91 std::vector<std::string> p2;
92 std::deque<classbase*> dummy;
93 p2.push_back(user->uuid);
94 this->HandleInternal(1, dummy);
95 this->Handle(p2, user);
96 this->HandleInternal(0, dummy);
100 user->WriteNumeric(432, "%s %s :Erroneous Nickname", user->nick.c_str(),parameters[0].c_str());
105 if (assign(user->nick) == parameters[0])
107 /* If its exactly the same, even case, dont do anything. */
108 if (parameters[0] == user->nick)
113 /* Its a change of case. People insisted that they should be
114 * able to do silly things like this even though the RFC says
115 * the nick AAA is the same as the nick aaa.
117 oldnick.assign(user->nick, 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
118 ModResult MOD_RESULT;
119 FIRST_MOD_RESULT(ServerInstance, OnUserPreNick, MOD_RESULT, (user,parameters[0]));
120 if (MOD_RESULT == MOD_RES_DENY)
122 if (user->registered == REG_ALL)
123 user->WriteCommon("NICK %s",parameters[0].c_str());
124 user->nick.assign(parameters[0], 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
125 user->InvalidateCache();
126 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
132 * Don't check Q:Lines if it's a server-enforced change, just on the off-chance some fucking *moron*
133 * tries to Q:Line SIDs, also, this means we just get our way period, as it really should be.
134 * Thanks Kein for finding this. -- w00t
136 * Also don't check Q:Lines for remote nickchanges, they should have our Q:Lines anyway to enforce themselves.
139 if (!allowinvalid || !IS_LOCAL(user))
141 XLine* mq = ServerInstance->XLines->MatchesLine("Q",parameters[0]);
144 if (user->registered == REG_ALL)
146 ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s",
147 parameters[0].c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), mq->reason.c_str());
149 user->WriteNumeric(432, "%s %s :Invalid nickname: %s",user->nick.c_str(), parameters[0].c_str(), mq->reason.c_str());
153 if (ServerInstance->Config->RestrictBannedUsers)
155 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
157 Channel *chan = i->first;
158 if (chan->GetStatus(user) < STATUS_VOICE && chan->IsBanned(user))
160 user->WriteNumeric(404, "%s %s :Cannot send to channel (you're banned)", user->nick.c_str(), chan->name.c_str());
168 * Uh oh.. if the nickname is in use, and it's not in use by the person using it (doh) --
169 * then we have a potential collide. Check whether someone else is camping on the nick
170 * (i.e. connect -> send NICK, don't send USER.) If they are camping, force-change the
171 * camper to their UID, and allow the incoming nick change.
173 * If the guy using the nick is already using it, tell the incoming nick change to gtfo,
174 * because the nick is already (rightfully) in use. -- w00t
176 User* InUse = ServerInstance->FindNickOnly(parameters[0]);
177 if (InUse && (InUse != user))
179 if (InUse->registered != REG_ALL)
181 /* force the camper to their UUID, and ask them to re-send a NICK. */
182 InUse->WriteTo(InUse, "NICK %s", InUse->uuid.c_str());
183 InUse->WriteNumeric(433, "%s %s :Nickname overruled.", InUse->nick.c_str(), InUse->nick.c_str());
184 InUse->UpdateNickHash(InUse->uuid.c_str());
185 InUse->nick.assign(InUse->uuid, 0, IS_LOCAL(InUse) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
186 InUse->InvalidateCache();
187 InUse->registered &= ~REG_NICK;
191 /* No camping, tell the incoming user to stop trying to change nick ;p */
192 user->WriteNumeric(433, "%s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick.c_str() : "*", parameters[0].c_str());
199 ModResult MOD_RESULT;
200 FIRST_MOD_RESULT(ServerInstance, OnUserPreNick, MOD_RESULT, (user, parameters[0]));
201 if (MOD_RESULT == MOD_RES_DENY)
202 // if a module returns true, the nick change is silently forbidden.
205 if (user->registered == REG_ALL)
206 user->WriteCommon("NICK %s", parameters[0].c_str());
208 oldnick.assign(user->nick, 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
210 /* change the nick of the user in the users_hash */
211 user = user->UpdateNickHash(parameters[0].c_str());
213 /* actually change the nick within the record */
217 user->nick.assign(parameters[0], 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
218 user->InvalidateCache();
220 /* Update display nicks */
221 for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
223 CUList* ulist = v->first->GetUsers();
224 CUList::iterator i = ulist->find(user);
225 if (i != ulist->end())
226 i->second = user->nick;
229 if (user->registered < REG_NICKUSER)
231 user->registered = (user->registered | REG_NICK);
232 if (user->registered == REG_NICKUSER)
234 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
235 FIRST_MOD_RESULT(ServerInstance, OnUserRegister, MOD_RESULT, (user));
236 if (MOD_RESULT == MOD_RES_DENY)
239 // return early to not penalize new users
244 if (user->registered == REG_ALL)
246 user->IncreasePenalty(10);
247 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user, oldnick));
254 CmdResult CommandNick::HandleInternal(const unsigned int id, const std::deque<classbase*>&)
256 allowinvalid = (id != 0);
261 COMMAND_INIT(CommandNick)