]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
Minor tweak, return of OnUserRegister discarded in cmd_user (should not have effected...
[user/henk/code/inspircd.git] / src / cmd_nick.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 "configreader.h"
15 #include "users.h"
16 #include "modules.h"
17 #include "inspircd.h"
18 #include "xline.h"
19 #include "commands/cmd_nick.h"
20
21 extern "C" command_t* init_command(InspIRCd* Instance)
22 {
23         return new cmd_nick(Instance);
24 }
25
26 /** Handle nick changes from users.
27  * NOTE: If you are used to ircds based on ircd2.8, and are looking
28  * for the client introduction code in here, youre in the wrong place.
29  * You need to look in the spanningtree module for this!
30  */
31 CmdResult cmd_nick::Handle (const char** parameters, int pcnt, userrec *user)
32 {
33         char oldnick[NICKMAX];
34
35         if (!*parameters[0] || !*user->nick)
36                 return CMD_FAILURE;
37
38         if (irc::string(user->nick) == irc::string(parameters[0]))
39         {
40                 /* If its exactly the same, even case, dont do anything. */
41                 if (!strcmp(user->nick,parameters[0]))
42                         return CMD_SUCCESS;
43
44                 /* Its a change of case. People insisted that they should be
45                  * able to do silly things like this even though the RFC says
46                  * the nick AAA is the same as the nick aaa.
47                  */
48                 strlcpy(oldnick, user->nick, NICKMAX - 1);
49                 int MOD_RESULT = 0;
50                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
51                 if (MOD_RESULT)
52                         return CMD_FAILURE;
53                 if (user->registered == REG_ALL)
54                         user->WriteCommon("NICK %s",parameters[0]);
55                 strlcpy(user->nick, parameters[0], NICKMAX - 1);
56                 user->InvalidateCache();
57                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
58                 return CMD_SUCCESS;
59         }
60         else
61         {
62                 QLine* mq = ServerInstance->XLines->matches_qline(parameters[0]);
63                 if (mq)
64                 {
65                         ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s", parameters[0], user->nick, user->ident, user->host, mq->reason);
66                         user->WriteServ("432 %s %s :Invalid nickname: %s",user->nick,parameters[0], mq->reason);
67                         return CMD_FAILURE;
68                 }
69                 /* Check for nickname overruled -
70                  * This happens when one user has connected and sent only NICK, and is essentially
71                  * "camping" upon a nickname. To give the new user connecting a fair chance of having
72                  * the nickname too, we force a nickchange on the older user (Simply the one who was
73                  * here first, no TS checks need to take place here)
74                  */
75                 userrec* InUse = ServerInstance->FindNick(parameters[0]);
76                 if (InUse && (InUse != user) && (ServerInstance->IsNick(parameters[0])))
77                 {
78                         if (InUse->registered != REG_ALL)
79                         {
80                                 /* change the nick of the older user to nnn-overruled,
81                                  * where nnn is their file descriptor. We know this to be unique.
82                                  * NOTE: We must do this and not quit the user, even though we do
83                                  * not have UID support yet. This is because if we set this user
84                                  * as quitting and then introduce the new user before the old one
85                                  * has quit, then the user hash gets totally buggered.
86                                  * (Yes, that is a technical term). -- Brain
87                                  */
88                                 std::string changeback = ConvToStr(InUse->GetFd()) + "-overruled";
89                                 InUse->WriteTo(InUse, "NICK %s", changeback.c_str());
90                                 InUse->WriteServ("433 %s %s :Nickname overruled.", InUse->nick, InUse->nick);
91                                 InUse->UpdateNickHash(changeback.c_str());
92                                 strlcpy(InUse->nick, changeback.c_str(), NICKMAX - 1);
93                                 InUse->InvalidateCache();
94                                 /* Take away their nickname-sent state forcing them to send a nick again */
95                                 InUse->registered &= ~REG_NICK;
96                         }
97                         else
98                         {
99                                 user->WriteServ("433 %s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick : "*", parameters[0]);
100                                 return CMD_FAILURE;
101                         }
102                 }
103         }
104         if ((!ServerInstance->IsNick(parameters[0])) && (IS_LOCAL(user)))
105         {
106                 user->WriteServ("432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
107                 return CMD_FAILURE;
108         }
109
110         if (user->registered == REG_ALL)
111         {
112                 int MOD_RESULT = 0;
113                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
114                 if (MOD_RESULT) {
115                         // if a module returns true, the nick change is silently forbidden.
116                         return CMD_FAILURE;
117                 }
118
119                 user->WriteCommon("NICK %s",parameters[0]);
120
121         }
122
123         strlcpy(oldnick, user->nick, NICKMAX - 1);
124
125         /* change the nick of the user in the users_hash */
126         user = user->UpdateNickHash(parameters[0]);
127
128         /* actually change the nick within the record */
129         if (!user) return CMD_FAILURE;
130         if (!*user->nick) return CMD_FAILURE;
131
132         strlcpy(user->nick, parameters[0], NICKMAX - 1);
133
134         user->InvalidateCache();
135
136         if (user->registered < REG_NICKUSER)
137         {
138                 user->registered = (user->registered | REG_NICK);
139
140                 if (ServerInstance->Config->NoUserDns)
141                 {
142                         user->dns_done = true;
143                         ServerInstance->next_call = ServerInstance->Time();
144                 }
145                 else
146                 {
147                         user->StartDNSLookup();
148                         if (user->dns_done)
149                         {
150                                 /* Cached result or instant failure - fall right through if possible */
151                                 ServerInstance->next_call = ServerInstance->Time();
152                         }
153                         else
154                         {
155                                 if (ServerInstance->next_call > ServerInstance->Time() + ServerInstance->Config->dns_timeout)
156                                         ServerInstance->next_call = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
157                         }
158                 }
159         }
160         if (user->registered == REG_NICKUSER)
161         {
162                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
163                 int MOD_RESULT = 0;
164                 FOREACH_RESULT(I_OnUserRegister,OnUserRegister(user));
165                 if (MOD_RESULT > 0)
166                         return CMD_FAILURE;
167         }
168         if (user->registered == REG_ALL)
169         {
170                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
171         }
172
173         return CMD_SUCCESS;
174
175 }
176