]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_nick.cpp
ecc254f174a3a3d10cb53403ca819293b4e1ba73
[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  *       | Inspire Internet Relay Chat Daemon |
18  *       +------------------------------------+
19  *
20  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
21  * See: http://wiki.inspircd.org/Credits
22  *
23  * This program is free but copyrighted software; see
24  *      the file COPYING for details.
25  *
26  * ---------------------------------------------------
27  */
28
29 #ifndef __CMD_NICK_H__
30 #define __CMD_NICK_H__
31
32 // include the common header files
33
34 #include "users.h"
35 #include "channels.h"
36
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
40  * may not.
41  */
42 class CommandNick : public Command
43 {
44         bool allowinvalid;
45  public:
46         /** Constructor for nick.
47          */
48         CommandNick (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"NICK", 0, 1, true, 3), allowinvalid(false) { syntax = "<newnick>"; }
49         /** Handle command.
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.
54          */
55         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
56
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
61          */
62         CmdResult HandleInternal(const unsigned int id, const std::deque<classbase*> &parameters);
63 };
64
65 #endif
66
67
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!
72  */
73 CmdResult CommandNick::Handle (const std::vector<std::string>& parameters, User *user)
74 {
75         std::string oldnick;
76
77         if (parameters[0].empty())
78         {
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() : "*");
81                 return CMD_FAILURE;
82         }
83
84         if (((!ServerInstance->IsNick(parameters[0].c_str(), ServerInstance->Config->Limits.NickMax))) && (IS_LOCAL(user)))
85         {
86                 if (!allowinvalid)
87                 {
88                         if (parameters[0] == "0")
89                         {
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);
97                                 return CMD_SUCCESS;
98                         }
99
100                         user->WriteNumeric(432, "%s %s :Erroneous Nickname", user->nick.c_str(),parameters[0].c_str());
101                         return CMD_FAILURE;
102                 }
103         }
104
105         if (assign(user->nick) == parameters[0])
106         {
107                 /* If its exactly the same, even case, dont do anything. */
108                 if (parameters[0] == user->nick)
109                 {
110                         return CMD_SUCCESS;
111                 }
112
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.
116                  */
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)
121                         return CMD_FAILURE;
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));
127                 return CMD_SUCCESS;
128         }
129         else
130         {
131                 /*
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
135                  *
136                  * Also don't check Q:Lines for remote nickchanges, they should have our Q:Lines anyway to enforce themselves.
137                  *              -- w00t
138                  */
139                 if (!allowinvalid || !IS_LOCAL(user))
140                 {
141                         XLine* mq = ServerInstance->XLines->MatchesLine("Q",parameters[0]);
142                         if (mq)
143                         {
144                                 if (user->registered == REG_ALL)
145                                 {
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());
148                                 }
149                                 user->WriteNumeric(432, "%s %s :Invalid nickname: %s",user->nick.c_str(), parameters[0].c_str(), mq->reason.c_str());
150                                 return CMD_FAILURE;
151                         }
152
153                         if (ServerInstance->Config->RestrictBannedUsers)
154                         {
155                                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
156                                 {
157                                         Channel *chan = i->first;
158                                         if (chan->GetStatus(user) < STATUS_VOICE && chan->IsBanned(user))
159                                         {
160                                                 user->WriteNumeric(404, "%s %s :Cannot send to channel (you're banned)", user->nick.c_str(), chan->name.c_str());
161                                                 return CMD_FAILURE;
162                                         }
163                                 }
164                         }
165                 }
166
167                 /*
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.
172                  *
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
175                  */
176                 User* InUse = ServerInstance->FindNickOnly(parameters[0]);
177                 if (InUse && (InUse != user))
178                 {
179                         if (InUse->registered != REG_ALL)
180                         {
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;
188                         }
189                         else
190                         {
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());
193                                 return CMD_FAILURE;
194                         }
195                 }
196         }
197
198
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.
203                 return CMD_FAILURE;
204
205         if (user->registered == REG_ALL)
206                 user->WriteCommon("NICK %s", parameters[0].c_str());
207
208         oldnick.assign(user->nick, 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
209
210         /* change the nick of the user in the users_hash */
211         user = user->UpdateNickHash(parameters[0].c_str());
212
213         /* actually change the nick within the record */
214         if (!user)
215                 return CMD_FAILURE;
216
217         user->nick.assign(parameters[0], 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
218         user->InvalidateCache();
219
220         /* Update display nicks */
221         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
222         {
223                 CUList* ulist = v->first->GetUsers();
224                 CUList::iterator i = ulist->find(user);
225                 if (i != ulist->end())
226                         i->second = user->nick;
227         }
228
229         if (user->registered < REG_NICKUSER)
230         {
231                 user->registered = (user->registered | REG_NICK);
232                 if (user->registered == REG_NICKUSER)
233                 {
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)
237                                 return CMD_FAILURE;
238
239                         // return early to not penalize new users
240                         return CMD_SUCCESS;
241                 }
242         }
243
244         if (user->registered == REG_ALL)
245         {
246                 user->IncreasePenalty(10);
247                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user, oldnick));
248         }
249
250         return CMD_SUCCESS;
251
252 }
253
254 CmdResult CommandNick::HandleInternal(const unsigned int id, const std::deque<classbase*>&)
255 {
256         allowinvalid = (id != 0);
257         return CMD_SUCCESS;
258 }
259
260
261 COMMAND_INIT(CommandNick)