]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
76ac00bb59d1f1f2c3316782b7fe07ee03e7f9cb
[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
22
23 extern "C" command_t* init_command(InspIRCd* Instance)
24 {
25         return new cmd_nick(Instance);
26 }
27
28 CmdResult cmd_nick::Handle (const char** parameters, int pcnt, userrec *user)
29 {
30         char oldnick[NICKMAX];
31
32         if (!*parameters[0])
33                 return CMD_FAILURE;
34
35         if (!*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                 if ((ServerInstance->FindNick(parameters[0])) && (ServerInstance->FindNick(parameters[0]) != user) && (ServerInstance->IsNick(parameters[0])))
70                 {
71                         userrec* InUse = ServerInstance->FindNick(parameters[0]);
72                         if (InUse->registered != REG_ALL)
73                         {
74                                 /* change the nick of the older user to nnn-overruled,
75                                  * where nnn is their file descriptor. We know this to be unique.
76                                  */
77                                 std::string changeback = ConvToStr(InUse->GetFd()) + "-overruled";
78                                 InUse->WriteFrom("NICK %s", changeback.c_str());
79                                 InUse->WriteServ("433 %s %s :Nickname overruled.", InUse->nick, InUse->nick);
80                                 InUse->UpdateNickHash(changeback.c_str());
81                                 strlcpy(InUse->nick, changeback.c_str(), NICKMAX - 1);
82                                 InUse->InvalidateCache();
83                         }
84                         else
85                         {
86                                 user->WriteServ("433 %s %s :Nickname is already in use.", user->registered >= REG_NICK ? user->nick : "*", parameters[0]);
87                                 return CMD_FAILURE;
88                         }
89                 }
90         }
91         if ((!ServerInstance->IsNick(parameters[0])) && (IS_LOCAL(user)))
92         {
93                 user->WriteServ("432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
94                 return CMD_FAILURE;
95         }
96
97         if (user->registered == REG_ALL)
98         {
99                 int MOD_RESULT = 0;
100                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
101                 if (MOD_RESULT) {
102                         // if a module returns true, the nick change is silently forbidden.
103                         return CMD_FAILURE;
104                 }
105
106                 user->WriteCommon("NICK %s",parameters[0]);
107
108         }
109
110         strlcpy(oldnick, user->nick, NICKMAX - 1);
111
112         /* change the nick of the user in the users_hash */
113         user = user->UpdateNickHash(parameters[0]);
114
115         /* actually change the nick within the record */
116         if (!user) return CMD_FAILURE;
117         if (!*user->nick) return CMD_FAILURE;
118
119         strlcpy(user->nick, parameters[0], NICKMAX - 1);
120
121         user->InvalidateCache();
122
123         if (user->registered < REG_NICKUSER)
124         {
125                 user->registered = (user->registered | REG_NICK);
126
127                 if (ServerInstance->Config->NoUserDns)
128                 {
129                         user->dns_done = true;
130                         ServerInstance->next_call = ServerInstance->Time();
131                 }
132                 else
133                 {
134                         user->StartDNSLookup();
135                         if (user->dns_done)
136                         {
137                                 /* Cached result or instant failure - fall right through if possible */
138                                 ServerInstance->next_call = ServerInstance->Time();
139                         }
140                         else
141                         {
142                                 if (ServerInstance->next_call > ServerInstance->Time() + ServerInstance->Config->dns_timeout)
143                                         ServerInstance->next_call = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
144                         }
145                 }
146         }
147         if (user->registered == REG_NICKUSER)
148         {
149                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
150                 FOREACH_MOD(I_OnUserRegister,OnUserRegister(user));
151                 //ConnectUser(user,NULL);
152         }
153         if (user->registered == REG_ALL)
154         {
155                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
156         }
157
158         return CMD_SUCCESS;
159
160 }
161