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