]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_nick.cpp
0391323dc3af86fe0503270f9d6a1d28b820fa15
[user/henk/code/inspircd.git] / src / commands / cmd_nick.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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 #include "commands/cmd_nick.h"
17
18 extern "C" DllExport Command* init_command(InspIRCd* Instance)
19 {
20         return new CommandNick(Instance);
21 }
22
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!
27  */
28 CmdResult CommandNick::Handle (const std::vector<std::string>& parameters, User *user)
29 {
30         char oldnick[NICKMAX];
31
32         if (parameters[0].empty())
33         {
34                 /* We cant put blanks in the parameters, so for this (extremely rare) issue we just put '*' here. */
35                 user->WriteNumeric(432, "%s * :Erroneous Nickname", user->nick.empty() ? user->nick.c_str() : "*");
36                 return CMD_FAILURE;
37         }
38
39         if (((!ServerInstance->IsNick(parameters[0].c_str()))) && (IS_LOCAL(user)))
40         {
41                 if (!allowinvalid)
42                 {
43                         if (parameters[0] == "0")
44                         {
45                                 // Special case, Fake a /nick UIDHERE. Useful for evading "ERR: NICK IN USE" on connect etc.
46                                 std::vector<std::string> p2;
47                                 std::deque<classbase*> dummy;
48                                 p2.push_back(user->uuid);
49                                 this->HandleInternal(1, dummy);
50                                 this->Handle(p2, user);
51                                 this->HandleInternal(0, dummy);
52                                 return CMD_SUCCESS;
53                         }
54
55                         user->WriteNumeric(432, "%s %s :Erroneous Nickname", user->nick.c_str(),parameters[0].c_str());
56                         return CMD_FAILURE;
57                 }
58         }
59
60         if (assign(user->nick) == parameters[0])
61         {
62                 ServerInstance->Logs->Log("nick", DEBUG, "Change to same nick '%s' %d '%s' '%d'", user->nick.c_str(), user->nick.length(), parameters[0].c_str(), parameters[0].length());
63                 /* If its exactly the same, even case, dont do anything. */
64                 if (parameters[0] == user->nick)
65                 {
66                         ServerInstance->Logs->Log("nick", DEBUG, "Not even a case change");
67                         return CMD_SUCCESS;
68                 }
69
70                 /* Its a change of case. People insisted that they should be
71                  * able to do silly things like this even though the RFC says
72                  * the nick AAA is the same as the nick aaa.
73                  */
74                 strlcpy(oldnick, user->nick.c_str(), NICKMAX - 1);
75                 int MOD_RESULT = 0;
76                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0].c_str()));
77                 if (MOD_RESULT)
78                         return CMD_FAILURE;
79                 if (user->registered == REG_ALL)
80                         user->WriteCommon("NICK %s",parameters[0].c_str());
81                 user->nick.assign(parameters[0], 0, NICKMAX - 1);
82                 user->InvalidateCache();
83                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
84                 return CMD_SUCCESS;
85         }
86         else
87         {
88                 /*
89                  * Don't check Q:Lines if it's a server-enforced change, just on the off-chance some fucking *moron*
90                  * tries to Q:Line SIDs, also, this means we just get our way period, as it really should be.
91                  * Thanks Kein for finding this. -- w00t
92                  *
93                  * Also don't check Q:Lines for remote nickchanges, they should have our Q:Lines anyway to enforce themselves.
94                  *              -- w00t
95                  */
96                 if (!allowinvalid || !IS_LOCAL(user))
97                 {
98                         XLine* mq = ServerInstance->XLines->MatchesLine("Q",parameters[0]);
99                         if (mq)
100                         {
101                                 ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s", parameters[0].c_str(), user->nick.c_str(), user->ident.c_str(), user->host.c_str(), mq->reason);
102                                 user->WriteNumeric(432, "%s %s :Invalid nickname: %s",user->nick.c_str(), parameters[0].c_str(), mq->reason);
103                                 return CMD_FAILURE;
104                         }
105                 }
106
107                 /*
108                  * Uh oh.. if the nickname is in use, and it's not in use by the person using it (doh) --
109                  * then we have a potential collide. Check whether someone else is camping on the nick
110                  * (i.e. connect -> send NICK, don't send USER.) If they are camping, force-change the
111                  * camper to their UID, and allow the incoming nick change.
112                  *
113                  * If the guy using the nick is already using it, tell the incoming nick change to gtfo,
114                  * because the nick is already (rightfully) in use. -- w00t
115                  */
116                 User* InUse = ServerInstance->FindNickOnly(parameters[0]);
117                 if (InUse && (InUse != user))
118                 {
119                         if (InUse->registered != REG_ALL)
120                         {
121                                 /* force the camper to their UUID, and ask them to re-send a NICK. */
122                                 InUse->WriteTo(InUse, "NICK %s", InUse->uuid.c_str());
123                                 InUse->WriteNumeric(433, "%s %s :Nickname overruled.", InUse->nick.c_str(), InUse->nick.c_str());
124                                 InUse->UpdateNickHash(InUse->uuid.c_str());
125                                 InUse->nick.assign(InUse->uuid, 0, NICKMAX - 1);
126                                 InUse->InvalidateCache();
127                                 InUse->registered &= ~REG_NICK;
128                         }
129                         else
130                         {
131                                 /* No camping, tell the incoming user  to stop trying to change nick ;p */
132                                 user->WriteNumeric(433, "%s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick.c_str() : "*", parameters[0].c_str());
133                                 return CMD_FAILURE;
134                         }
135                 }
136         }
137
138
139         int MOD_RESULT = 0;
140         FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user, parameters[0]));
141         if (MOD_RESULT)
142                 // if a module returns true, the nick change is silently forbidden.
143                 return CMD_FAILURE;
144
145         if (user->registered == REG_ALL)
146                 user->WriteCommon("NICK %s", parameters[0].c_str());
147
148         strlcpy(oldnick, user->nick.c_str(), NICKMAX - 1);
149
150         /* change the nick of the user in the users_hash */
151         user = user->UpdateNickHash(parameters[0].c_str());
152
153         /* actually change the nick within the record */
154         if (!user)
155                 return CMD_FAILURE;
156
157         user->nick.assign(parameters[0], 0, NICKMAX - 1);
158         user->InvalidateCache();
159
160         /* Update display nicks */
161         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
162         {
163                 CUList* ulist = v->first->GetUsers();
164                 CUList::iterator i = ulist->find(user);
165                 if (i != ulist->end())
166                         i->second = user->nick;
167         }
168
169         if (user->registered < REG_NICKUSER)
170         {
171                 user->registered = (user->registered | REG_NICK);
172         }
173         if (user->registered == REG_NICKUSER)
174         {
175                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
176                 MOD_RESULT = 0;
177                 FOREACH_RESULT(I_OnUserRegister,OnUserRegister(user));
178                 if (MOD_RESULT > 0)
179                         return CMD_FAILURE;
180         }
181         if (user->registered == REG_ALL)
182         {
183                 user->IncreasePenalty(10);
184                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
185         }
186
187         return CMD_SUCCESS;
188
189 }
190
191 CmdResult CommandNick::HandleInternal(const unsigned int id, const std::deque<classbase*>&)
192 {
193         allowinvalid = (id != 0);
194         return CMD_SUCCESS;
195 }
196