]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_nick.cpp
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / src / commands / cmd_nick.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "xline.h"
16
17 /** Handle /NICK. These command handlers can be reloaded by the core,
18  * and handle basic RFC1459 commands. Commands within modules work
19  * the same way, however, they can be fully unloaded, where these
20  * may not.
21  */
22 class CommandNick : public Command
23 {
24  public:
25         /** Constructor for nick.
26          */
27         CommandNick ( Module* parent) : Command(parent,"NICK", 1, 1) { works_before_reg = true; syntax = "<newnick>"; Penalty = 3; }
28         /** Handle command.
29          * @param parameters The parameters to the comamnd
30          * @param pcnt The number of parameters passed to teh command
31          * @param user The user issuing the command
32          * @return A value from CmdResult to indicate command success or failure.
33          */
34         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
35 };
36
37 /** Handle nick changes from users.
38  * NOTE: If you are used to ircds based on ircd2.8, and are looking
39  * for the client introduction code in here, youre in the wrong place.
40  * You need to look in the spanningtree module for this!
41  */
42 CmdResult CommandNick::Handle (const std::vector<std::string>& parameters, User *user)
43 {
44         std::string oldnick;
45
46         if (parameters[0].empty())
47         {
48                 /* We cant put blanks in the parameters, so for this (extremely rare) issue we just put '*' here. */
49                 user->WriteNumeric(432, "%s * :Erroneous Nickname", user->nick.empty() ? user->nick.c_str() : "*");
50                 return CMD_FAILURE;
51         }
52
53         if (((!ServerInstance->IsNick(parameters[0].c_str(), ServerInstance->Config->Limits.NickMax))) && (IS_LOCAL(user)))
54         {
55                 if (!User::NICKForced.get(user))
56                 {
57                         if (parameters[0] == "0")
58                         {
59                                 // Special case, Fake a /nick UIDHERE. Useful for evading "ERR: NICK IN USE" on connect etc.
60                                 std::vector<std::string> p2;
61                                 p2.push_back(user->uuid);
62                                 User::NICKForced.set(user, 1);
63                                 this->Handle(p2, user);
64                                 User::NICKForced.set(user, 0);
65                                 return CMD_SUCCESS;
66                         }
67
68                         user->WriteNumeric(432, "%s %s :Erroneous Nickname", user->nick.c_str(),parameters[0].c_str());
69                         return CMD_FAILURE;
70                 }
71         }
72
73         if (assign(user->nick) == parameters[0])
74         {
75                 /* If its exactly the same, even case, dont do anything. */
76                 if (parameters[0] == user->nick)
77                 {
78                         return CMD_SUCCESS;
79                 }
80
81                 /* Its a change of case. People insisted that they should be
82                  * able to do silly things like this even though the RFC says
83                  * the nick AAA is the same as the nick aaa.
84                  */
85                 oldnick.assign(user->nick, 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
86                 ModResult MOD_RESULT;
87                 FIRST_MOD_RESULT(OnUserPreNick, MOD_RESULT, (user,parameters[0]));
88                 if (MOD_RESULT == MOD_RES_DENY)
89                         return CMD_FAILURE;
90                 if (user->registered == REG_ALL)
91                         user->WriteCommon("NICK %s",parameters[0].c_str());
92                 user->nick.assign(parameters[0], 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
93                 user->InvalidateCache();
94                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
95                 return CMD_SUCCESS;
96         }
97         else
98         {
99                 /*
100                  * Don't check Q:Lines if it's a server-enforced change, just on the off-chance some fucking *moron*
101                  * tries to Q:Line SIDs, also, this means we just get our way period, as it really should be.
102                  * Thanks Kein for finding this. -- w00t
103                  *
104                  * Also don't check Q:Lines for remote nickchanges, they should have our Q:Lines anyway to enforce themselves.
105                  *              -- w00t
106                  */
107                 if (!IS_LOCAL(user))
108                 {
109                         XLine* mq = ServerInstance->XLines->MatchesLine("Q",parameters[0]);
110                         if (mq)
111                         {
112                                 if (user->registered == REG_ALL)
113                                 {
114                                         ServerInstance->SNO->WriteGlobalSno('a', "Q-Lined nickname %s from %s!%s@%s: %s",
115                                                 parameters[0].c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), mq->reason.c_str());
116                                 }
117                                 user->WriteNumeric(432, "%s %s :Invalid nickname: %s",user->nick.c_str(), parameters[0].c_str(), mq->reason.c_str());
118                                 return CMD_FAILURE;
119                         }
120
121                         if (ServerInstance->Config->RestrictBannedUsers)
122                         {
123                                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
124                                 {
125                                         Channel *chan = *i;
126                                         if (chan->GetPrefixValue(user) < VOICE_VALUE && chan->IsBanned(user))
127                                         {
128                                                 user->WriteNumeric(404, "%s %s :Cannot send to channel (you're banned)", user->nick.c_str(), chan->name.c_str());
129                                                 return CMD_FAILURE;
130                                         }
131                                 }
132                         }
133                 }
134
135                 /*
136                  * Uh oh.. if the nickname is in use, and it's not in use by the person using it (doh) --
137                  * then we have a potential collide. Check whether someone else is camping on the nick
138                  * (i.e. connect -> send NICK, don't send USER.) If they are camping, force-change the
139                  * camper to their UID, and allow the incoming nick change.
140                  *
141                  * If the guy using the nick is already using it, tell the incoming nick change to gtfo,
142                  * because the nick is already (rightfully) in use. -- w00t
143                  */
144                 User* InUse = ServerInstance->FindNickOnly(parameters[0]);
145                 if (InUse && (InUse != user))
146                 {
147                         if (InUse->registered != REG_ALL)
148                         {
149                                 /* force the camper to their UUID, and ask them to re-send a NICK. */
150                                 InUse->WriteTo(InUse, "NICK %s", InUse->uuid.c_str());
151                                 InUse->WriteNumeric(433, "%s %s :Nickname overruled.", InUse->nick.c_str(), InUse->nick.c_str());
152                                 InUse->UpdateNickHash(InUse->uuid.c_str());
153                                 InUse->nick.assign(InUse->uuid, 0, IS_LOCAL(InUse) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
154                                 InUse->InvalidateCache();
155                                 InUse->registered &= ~REG_NICK;
156                         }
157                         else
158                         {
159                                 /* No camping, tell the incoming user  to stop trying to change nick ;p */
160                                 user->WriteNumeric(433, "%s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick.c_str() : "*", parameters[0].c_str());
161                                 return CMD_FAILURE;
162                         }
163                 }
164         }
165
166
167         ModResult MOD_RESULT;
168         FIRST_MOD_RESULT(OnUserPreNick, MOD_RESULT, (user, parameters[0]));
169         if (MOD_RESULT == MOD_RES_DENY)
170                 // if a module returns true, the nick change is silently forbidden.
171                 return CMD_FAILURE;
172
173         if (user->registered == REG_ALL)
174                 user->WriteCommon("NICK %s", parameters[0].c_str());
175
176         oldnick.assign(user->nick, 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
177
178         /* change the nick of the user in the users_hash */
179         user = user->UpdateNickHash(parameters[0].c_str());
180
181         /* actually change the nick within the record */
182         if (!user)
183                 return CMD_FAILURE;
184
185         user->nick.assign(parameters[0], 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
186         user->InvalidateCache();
187
188         if (user->registered < REG_NICKUSER)
189         {
190                 user->registered = (user->registered | REG_NICK);
191                 if (user->registered == REG_NICKUSER)
192                 {
193                         /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
194                         FIRST_MOD_RESULT(OnUserRegister, MOD_RESULT, (user));
195                         if (MOD_RESULT == MOD_RES_DENY)
196                                 return CMD_FAILURE;
197
198                         // return early to not penalize new users
199                         return CMD_SUCCESS;
200                 }
201         }
202
203         if (user->registered == REG_ALL)
204         {
205                 user->IncreasePenalty(10);
206                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user, oldnick));
207         }
208
209         return CMD_SUCCESS;
210
211 }
212
213
214 COMMAND_INIT(CommandNick)