]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
Add skeleton functions for UID stuff.
[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 "configreader.h"
16 #include "users.h"
17 #include "modules.h"
18 #include "xline.h"
19 #include "commands/cmd_nick.h"
20
21 extern "C" DllExport 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         {
37                 /* We cant put blanks in the parameters, so for this (extremely rare) issue we just put '*' here. */
38                 user->WriteServ("432 %s * :Erroneous Nickname", *user->nick ? user->nick : "*");
39                 return CMD_FAILURE;
40         }
41
42         if (irc::string(user->nick) == irc::string(parameters[0]))
43         {
44                 /* If its exactly the same, even case, dont do anything. */
45                 if (!strcmp(user->nick,parameters[0]))
46                         return CMD_SUCCESS;
47
48                 /* Its a change of case. People insisted that they should be
49                  * able to do silly things like this even though the RFC says
50                  * the nick AAA is the same as the nick aaa.
51                  */
52                 strlcpy(oldnick, user->nick, NICKMAX - 1);
53                 int MOD_RESULT = 0;
54                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
55                 if (MOD_RESULT)
56                         return CMD_FAILURE;
57                 if (user->registered == REG_ALL)
58                         user->WriteCommon("NICK %s",parameters[0]);
59                 strlcpy(user->nick, parameters[0], NICKMAX - 1);
60                 user->InvalidateCache();
61                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
62                 return CMD_SUCCESS;
63         }
64         else
65         {
66                 QLine* mq = ServerInstance->XLines->matches_qline(parameters[0]);
67                 if (mq)
68                 {
69                         ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s", parameters[0], user->nick, user->ident, user->host, mq->reason);
70                         user->WriteServ("432 %s %s :Invalid nickname: %s",user->nick,parameters[0], mq->reason);
71                         return CMD_FAILURE;
72                 }
73                 /* Check for nickname overruled -
74                  * This happens when one user has connected and sent only NICK, and is essentially
75                  * "camping" upon a nickname. To give the new user connecting a fair chance of having
76                  * the nickname too, we force a nickchange on the older user (Simply the one who was
77                  * here first, no TS checks need to take place here)
78                  */
79                 userrec* InUse = ServerInstance->FindNick(parameters[0]);
80                 if (InUse && (InUse != user) && (ServerInstance->IsNick(parameters[0])))
81                 {
82                         if (InUse->registered != REG_ALL)
83                         {
84                                 /* change the nick of the older user to nnn-overruled,
85                                  * where nnn is their file descriptor. We know this to be unique.
86                                  * NOTE: We must do this and not quit the user, even though we do
87                                  * not have UID support yet. This is because if we set this user
88                                  * as quitting and then introduce the new user before the old one
89                                  * has quit, then the user hash gets totally buggered.
90                                  * (Yes, that is a technical term). -- Brain
91                                  */
92                                 std::string changeback = ConvToStr(InUse->GetFd()) + "-overruled";
93                                 InUse->WriteTo(InUse, "NICK %s", changeback.c_str());
94                                 InUse->WriteServ("433 %s %s :Nickname overruled.", InUse->nick, InUse->nick);
95                                 InUse->UpdateNickHash(changeback.c_str());
96                                 strlcpy(InUse->nick, changeback.c_str(), NICKMAX - 1);
97                                 InUse->InvalidateCache();
98                                 /* Take away their nickname-sent state forcing them to send a nick again */
99                                 InUse->registered &= ~REG_NICK;
100                         }
101                         else
102                         {
103                                 user->WriteServ("433 %s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick : "*", parameters[0]);
104                                 return CMD_FAILURE;
105                         }
106                 }
107         }
108         if ((!ServerInstance->IsNick(parameters[0])) && (IS_LOCAL(user)))
109         {
110                 user->WriteServ("432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
111                 return CMD_FAILURE;
112         }
113
114         if (user->registered == REG_ALL)
115         {
116                 int MOD_RESULT = 0;
117                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
118                 if (MOD_RESULT) {
119                         // if a module returns true, the nick change is silently forbidden.
120                         return CMD_FAILURE;
121                 }
122
123                 user->WriteCommon("NICK %s",parameters[0]);
124
125         }
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) return CMD_FAILURE;
134         if (!*user->nick) return CMD_FAILURE;
135
136         strlcpy(user->nick, parameters[0], NICKMAX - 1);
137
138         user->InvalidateCache();
139
140         /* Update display nicks */
141         for (UCListIter v = user->chans.begin(); v != user->chans.end(); v++)
142         {
143                 CUList* ulist = v->first->GetUsers();
144                 CUList::iterator i = ulist->find(user);
145                 if (i != ulist->end())
146                         i->second = user->nick;
147         }
148
149         if (user->registered < REG_NICKUSER)
150         {
151                 user->registered = (user->registered | REG_NICK);
152
153                 if (ServerInstance->Config->NoUserDns)
154                 {
155                         user->dns_done = true;
156                         ServerInstance->next_call = ServerInstance->Time();
157                 }
158                 else
159                 {
160                         user->StartDNSLookup();
161                         if (user->dns_done)
162                         {
163                                 /* Cached result or instant failure - fall right through if possible */
164                                 ServerInstance->next_call = ServerInstance->Time();
165                         }
166                         else
167                         {
168                                 if (ServerInstance->next_call > ServerInstance->Time() + ServerInstance->Config->dns_timeout)
169                                         ServerInstance->next_call = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
170                         }
171                 }
172         }
173         if (user->registered == REG_NICKUSER)
174         {
175                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
176                 int 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                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
184         }
185
186         return CMD_SUCCESS;
187
188 }
189