]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
4eb439895f581d8ef8c5c7241b6239ea5fc6b7be
[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 "inspircd.h"
15 #include "xline.h"
16 #include "commands/cmd_nick.h"
17
18 extern "C" DllExport command_t* init_command(InspIRCd* Instance)
19 {
20         return new cmd_nick(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 cmd_nick::Handle (const char** parameters, int pcnt, userrec *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->WriteServ("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                 QLine* mq = ServerInstance->XLines->matches_qline(parameters[0]);
64                 if (mq)
65                 {
66                         ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s", parameters[0], user->nick, user->ident, user->host, mq->reason);
67                         user->WriteServ("432 %s %s :Invalid nickname: %s",user->nick,parameters[0], mq->reason);
68                         return CMD_FAILURE;
69                 }
70                 /* Check for nickname overruled -
71                  * This happens when one user has connected and sent only NICK, and is essentially
72                  * "camping" upon a nickname. To give the new user connecting a fair chance of having
73                  * the nickname too, we force a nickchange on the older user (Simply the one who was
74                  * here first, no TS checks need to take place here)
75                  */
76                 userrec* InUse = ServerInstance->FindNickOnly(parameters[0]);
77                 if (InUse && (InUse != user) && ((ServerInstance->IsNick(parameters[0]) || allowinvalid)))
78                 {
79                         if (InUse->registered != REG_ALL)
80                         {
81                                 /* change the nick of the older user to its UUID
82                                  */
83                                 InUse->WriteTo(InUse, "NICK %s", InUse->uuid);
84                                 InUse->WriteServ("433 %s %s :Nickname overruled.", InUse->nick, InUse->nick);
85                                 InUse->UpdateNickHash(InUse->uuid);
86                                 strlcpy(InUse->nick, InUse->uuid, NICKMAX - 1);
87                                 InUse->InvalidateCache();
88                                 /* Take away their nickname-sent state forcing them to send a nick again */
89                                 InUse->registered &= ~REG_NICK;
90                         }
91                         else
92                         {
93                                 user->WriteServ("433 %s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick : "*", parameters[0]);
94                                 return CMD_FAILURE;
95                         }
96                 }
97         }
98         if (((!ServerInstance->IsNick(parameters[0]))) && (IS_LOCAL(user)))
99         {
100                 if (!allowinvalid)
101                 {
102                         user->WriteServ("432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
103                         return CMD_FAILURE;
104                 }
105         }
106
107         int MOD_RESULT = 0;
108         FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
109         if (MOD_RESULT)
110                 // if a module returns true, the nick change is silently forbidden.
111                 return CMD_FAILURE;
112
113         if (user->registered == REG_ALL)
114                 user->WriteCommon("NICK %s",parameters[0]);
115
116         strlcpy(oldnick, user->nick, NICKMAX - 1);
117
118         /* change the nick of the user in the users_hash */
119         user = user->UpdateNickHash(parameters[0]);
120
121         /* actually change the nick within the record */
122         if (!user)
123                 return CMD_FAILURE;
124         if (!*user->nick)
125                 return CMD_FAILURE;
126
127         strlcpy(user->nick, parameters[0], NICKMAX - 1);
128
129         user->InvalidateCache();
130
131         /* Update display nicks */
132         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
133         {
134                 CUList* ulist = v->first->GetUsers();
135                 CUList::iterator i = ulist->find(user);
136                 if (i != ulist->end())
137                         i->second = user->nick;
138         }
139
140         if (user->registered < REG_NICKUSER)
141         {
142                 user->registered = (user->registered | REG_NICK);
143
144                 if (ServerInstance->Config->NoUserDns)
145                 {
146                         user->dns_done = true;
147                         ServerInstance->next_call = ServerInstance->Time();
148                 }
149                 else
150                 {
151                         user->StartDNSLookup();
152                         if (user->dns_done)
153                         {
154                                 /* Cached result or instant failure - fall right through if possible */
155                                 ServerInstance->next_call = ServerInstance->Time();
156                         }
157                         else
158                         {
159                                 if (ServerInstance->next_call > ServerInstance->Time() + ServerInstance->Config->dns_timeout)
160                                         ServerInstance->next_call = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
161                         }
162                 }
163         }
164         if (user->registered == REG_NICKUSER)
165         {
166                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
167                 int MOD_RESULT = 0;
168                 FOREACH_RESULT(I_OnUserRegister,OnUserRegister(user));
169                 if (MOD_RESULT > 0)
170                         return CMD_FAILURE;
171         }
172         if (user->registered == REG_ALL)
173         {
174                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
175         }
176
177         return CMD_SUCCESS;
178
179 }
180
181 CmdResult cmd_nick::HandleInternal(const unsigned int id, const std::deque<classbase*> &parameters)
182 {
183         allowinvalid = (id != 0);
184         return CMD_SUCCESS;
185 }
186