]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_nick.cpp
Change Extensible to use strongly typed entries
[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  public:
45         /** Constructor for nick.
46          */
47         CommandNick (InspIRCd* Instance, Module* parent) : Command(Instance,parent,"NICK", 0, 1, true, 3) { syntax = "<newnick>"; }
48         /** Handle command.
49          * @param parameters The parameters to the comamnd
50          * @param pcnt The number of parameters passed to teh command
51          * @param user The user issuing the command
52          * @return A value from CmdResult to indicate command success or failure.
53          */
54         CmdResult Handle(const std::vector<std::string>& parameters, User *user);
55 };
56
57 #endif
58
59
60 /** Handle nick changes from users.
61  * NOTE: If you are used to ircds based on ircd2.8, and are looking
62  * for the client introduction code in here, youre in the wrong place.
63  * You need to look in the spanningtree module for this!
64  */
65 CmdResult CommandNick::Handle (const std::vector<std::string>& parameters, User *user)
66 {
67         std::string oldnick;
68
69         if (parameters[0].empty())
70         {
71                 /* We cant put blanks in the parameters, so for this (extremely rare) issue we just put '*' here. */
72                 user->WriteNumeric(432, "%s * :Erroneous Nickname", user->nick.empty() ? user->nick.c_str() : "*");
73                 return CMD_FAILURE;
74         }
75
76         if (((!ServerInstance->IsNick(parameters[0].c_str(), ServerInstance->Config->Limits.NickMax))) && (IS_LOCAL(user)))
77         {
78                 if (!User::NICKForced.get(user))
79                 {
80                         if (parameters[0] == "0")
81                         {
82                                 // Special case, Fake a /nick UIDHERE. Useful for evading "ERR: NICK IN USE" on connect etc.
83                                 std::vector<std::string> p2;
84                                 p2.push_back(user->uuid);
85                                 User::NICKForced.set(user, 1);
86                                 this->Handle(p2, user);
87                                 User::NICKForced.set(user, 0);
88                                 return CMD_SUCCESS;
89                         }
90
91                         user->WriteNumeric(432, "%s %s :Erroneous Nickname", user->nick.c_str(),parameters[0].c_str());
92                         return CMD_FAILURE;
93                 }
94         }
95
96         if (assign(user->nick) == parameters[0])
97         {
98                 /* If its exactly the same, even case, dont do anything. */
99                 if (parameters[0] == user->nick)
100                 {
101                         return CMD_SUCCESS;
102                 }
103
104                 /* Its a change of case. People insisted that they should be
105                  * able to do silly things like this even though the RFC says
106                  * the nick AAA is the same as the nick aaa.
107                  */
108                 oldnick.assign(user->nick, 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
109                 ModResult MOD_RESULT;
110                 FIRST_MOD_RESULT(ServerInstance, OnUserPreNick, MOD_RESULT, (user,parameters[0]));
111                 if (MOD_RESULT == MOD_RES_DENY)
112                         return CMD_FAILURE;
113                 if (user->registered == REG_ALL)
114                         user->WriteCommon("NICK %s",parameters[0].c_str());
115                 user->nick.assign(parameters[0], 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
116                 user->InvalidateCache();
117                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
118                 return CMD_SUCCESS;
119         }
120         else
121         {
122                 /*
123                  * Don't check Q:Lines if it's a server-enforced change, just on the off-chance some fucking *moron*
124                  * tries to Q:Line SIDs, also, this means we just get our way period, as it really should be.
125                  * Thanks Kein for finding this. -- w00t
126                  *
127                  * Also don't check Q:Lines for remote nickchanges, they should have our Q:Lines anyway to enforce themselves.
128                  *              -- w00t
129                  */
130                 if (!IS_LOCAL(user))
131                 {
132                         XLine* mq = ServerInstance->XLines->MatchesLine("Q",parameters[0]);
133                         if (mq)
134                         {
135                                 if (user->registered == REG_ALL)
136                                 {
137                                         ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s",
138                                                 parameters[0].c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), mq->reason.c_str());
139                                 }
140                                 user->WriteNumeric(432, "%s %s :Invalid nickname: %s",user->nick.c_str(), parameters[0].c_str(), mq->reason.c_str());
141                                 return CMD_FAILURE;
142                         }
143
144                         if (ServerInstance->Config->RestrictBannedUsers)
145                         {
146                                 for (UCListIter i = user->chans.begin(); i != user->chans.end(); i++)
147                                 {
148                                         Channel *chan = i->first;
149                                         if (chan->GetStatus(user) < STATUS_VOICE && chan->IsBanned(user))
150                                         {
151                                                 user->WriteNumeric(404, "%s %s :Cannot send to channel (you're banned)", user->nick.c_str(), chan->name.c_str());
152                                                 return CMD_FAILURE;
153                                         }
154                                 }
155                         }
156                 }
157
158                 /*
159                  * Uh oh.. if the nickname is in use, and it's not in use by the person using it (doh) --
160                  * then we have a potential collide. Check whether someone else is camping on the nick
161                  * (i.e. connect -> send NICK, don't send USER.) If they are camping, force-change the
162                  * camper to their UID, and allow the incoming nick change.
163                  *
164                  * If the guy using the nick is already using it, tell the incoming nick change to gtfo,
165                  * because the nick is already (rightfully) in use. -- w00t
166                  */
167                 User* InUse = ServerInstance->FindNickOnly(parameters[0]);
168                 if (InUse && (InUse != user))
169                 {
170                         if (InUse->registered != REG_ALL)
171                         {
172                                 /* force the camper to their UUID, and ask them to re-send a NICK. */
173                                 InUse->WriteTo(InUse, "NICK %s", InUse->uuid.c_str());
174                                 InUse->WriteNumeric(433, "%s %s :Nickname overruled.", InUse->nick.c_str(), InUse->nick.c_str());
175                                 InUse->UpdateNickHash(InUse->uuid.c_str());
176                                 InUse->nick.assign(InUse->uuid, 0, IS_LOCAL(InUse) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
177                                 InUse->InvalidateCache();
178                                 InUse->registered &= ~REG_NICK;
179                         }
180                         else
181                         {
182                                 /* No camping, tell the incoming user  to stop trying to change nick ;p */
183                                 user->WriteNumeric(433, "%s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick.c_str() : "*", parameters[0].c_str());
184                                 return CMD_FAILURE;
185                         }
186                 }
187         }
188
189
190         ModResult MOD_RESULT;
191         FIRST_MOD_RESULT(ServerInstance, OnUserPreNick, MOD_RESULT, (user, parameters[0]));
192         if (MOD_RESULT == MOD_RES_DENY)
193                 // if a module returns true, the nick change is silently forbidden.
194                 return CMD_FAILURE;
195
196         if (user->registered == REG_ALL)
197                 user->WriteCommon("NICK %s", parameters[0].c_str());
198
199         oldnick.assign(user->nick, 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
200
201         /* change the nick of the user in the users_hash */
202         user = user->UpdateNickHash(parameters[0].c_str());
203
204         /* actually change the nick within the record */
205         if (!user)
206                 return CMD_FAILURE;
207
208         user->nick.assign(parameters[0], 0, IS_LOCAL(user) ? ServerInstance->Config->Limits.NickMax : MAXBUF);
209         user->InvalidateCache();
210
211         /* Update display nicks */
212         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
213         {
214                 CUList* ulist = v->first->GetUsers();
215                 CUList::iterator i = ulist->find(user);
216                 if (i != ulist->end())
217                         i->second = user->nick;
218         }
219
220         if (user->registered < REG_NICKUSER)
221         {
222                 user->registered = (user->registered | REG_NICK);
223                 if (user->registered == REG_NICKUSER)
224                 {
225                         /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
226                         FIRST_MOD_RESULT(ServerInstance, OnUserRegister, MOD_RESULT, (user));
227                         if (MOD_RESULT == MOD_RES_DENY)
228                                 return CMD_FAILURE;
229
230                         // return early to not penalize new users
231                         return CMD_SUCCESS;
232                 }
233         }
234
235         if (user->registered == REG_ALL)
236         {
237                 user->IncreasePenalty(10);
238                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user, oldnick));
239         }
240
241         return CMD_SUCCESS;
242
243 }
244
245
246 COMMAND_INIT(CommandNick)