]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/commands/cmd_nick.cpp
Don't check Q:Lines if server is enforcing a nick change (I forgot we had a way to...
[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 char* const* parameters, int, User *user)
29 {
30         char oldnick[NICKMAX];
31
32         if (!*parameters[0] || !*user->nick)
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) == irc::string(parameters[0]))
40         {
41                 /* If its exactly the same, even case, dont do anything. */
42                 if (!strcmp(user->nick,parameters[0]))
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]));
52                 if (MOD_RESULT)
53                         return CMD_FAILURE;
54                 if (user->registered == REG_ALL)
55                         user->WriteCommon("NICK %s",parameters[0]);
56                 strlcpy(user->nick, parameters[0], 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                 if (!allowinvalid)
69                 {
70                         XLine* mq = ServerInstance->XLines->MatchesLine("Q",parameters[0]);
71                         if (mq)
72                         {
73                                 ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s", parameters[0], user->nick, user->ident, user->host, mq->reason);
74                                 user->WriteNumeric(432, "%s %s :Invalid nickname: %s",user->nick,parameters[0], mq->reason);
75                                 return CMD_FAILURE;
76                         }
77                 }
78
79                 /*
80                  * Uh oh.. if the nickname is in use, and it's not in use by the person using it (doh) --
81                  * then we have a potential collide. Check whether someone else is camping on the nick
82                  * (i.e. connect -> send NICK, don't send USER.) If they are camping, force-change the
83                  * camper to their UID, and allow the incoming nick change.
84                  *
85                  * If the guy using the nick is already using it, tell the incoming nick change to gtfo,
86                  * because the nick is already (rightfully) in use. -- w00t
87                  */
88                 User* InUse = ServerInstance->FindNickOnly(parameters[0]);
89                 if (InUse && (InUse != user) && ((ServerInstance->IsNick(parameters[0]) || allowinvalid)))
90                 {
91                         if (InUse->registered != REG_ALL)
92                         {
93                                 /* force the camper to their UUID, and ask them to re-send a NICK. */
94                                 InUse->WriteTo(InUse, "NICK %s", InUse->uuid);
95                                 InUse->WriteNumeric(433, "%s %s :Nickname overruled.", InUse->nick, InUse->nick);
96                                 InUse->UpdateNickHash(InUse->uuid);
97                                 strlcpy(InUse->nick, InUse->uuid, NICKMAX - 1);
98                                 InUse->InvalidateCache();
99                                 InUse->registered &= ~REG_NICK;
100                         }
101                         else
102                         {
103                                 /* No camping, tell the incoming user  to stop trying to change nick ;p */
104                                 user->WriteNumeric(433, "%s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick : "*", parameters[0]);
105                                 return CMD_FAILURE;
106                         }
107                 }
108         }
109         if (((!ServerInstance->IsNick(parameters[0]))) && (IS_LOCAL(user)))
110         {
111                 if (!allowinvalid)
112                 {
113                         user->WriteNumeric(432, "%s %s :Erroneous Nickname",user->nick,parameters[0]);
114                         return CMD_FAILURE;
115                 }
116         }
117
118         int MOD_RESULT = 0;
119         FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
120         if (MOD_RESULT)
121                 // if a module returns true, the nick change is silently forbidden.
122                 return CMD_FAILURE;
123
124         if (user->registered == REG_ALL)
125                 user->WriteCommon("NICK %s",parameters[0]);
126
127         strlcpy(oldnick, user->nick, NICKMAX - 1);
128
129         /* change the nick of the user in the users_hash */
130         user = user->UpdateNickHash(parameters[0]);
131
132         /* actually change the nick within the record */
133         if (!user)
134                 return CMD_FAILURE;
135         if (!*user->nick)
136                 return CMD_FAILURE;
137
138         strlcpy(user->nick, parameters[0], NICKMAX - 1);
139
140         user->InvalidateCache();
141
142         /* Update display nicks */
143         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
144         {
145                 CUList* ulist = v->first->GetUsers();
146                 CUList::iterator i = ulist->find(user);
147                 if (i != ulist->end())
148                         i->second = user->nick;
149         }
150
151         if (user->registered < REG_NICKUSER)
152         {
153                 user->registered = (user->registered | REG_NICK);
154         }
155         if (user->registered == REG_NICKUSER)
156         {
157                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
158                 MOD_RESULT = 0;
159                 FOREACH_RESULT(I_OnUserRegister,OnUserRegister(user));
160                 if (MOD_RESULT > 0)
161                         return CMD_FAILURE;
162         }
163         if (user->registered == REG_ALL)
164         {
165                 user->IncreasePenalty(10);
166                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
167         }
168
169         return CMD_SUCCESS;
170
171 }
172
173 CmdResult CommandNick::HandleInternal(const unsigned int id, const std::deque<classbase*>&)
174 {
175         allowinvalid = (id != 0);
176         return CMD_SUCCESS;
177 }
178