]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
Removed a pointless check in ./configure --clean that made it only work with one...
[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][0])
33         {
34                 ServerInstance->Log(DEBUG,"zero length new nick passed to handle_nick");
35                 return CMD_FAILURE;
36         }
37         if (!*user->nick)
38         {
39                 ServerInstance->Log(DEBUG,"invalid old nick passed to handle_nick");
40                 return CMD_FAILURE;
41         }
42         ServerInstance->Log(DEBUG,"Fall through");
43         if (irc::string(user->nick) == irc::string(parameters[0]))
44         {
45                 /* If its exactly the same, even case, dont do anything. */
46                 if (!strcmp(user->nick,parameters[0]))
47                         return CMD_SUCCESS;
48
49                 /* Its a change of case. People insisted that they should be
50                  * able to do silly things like this even though the RFC says
51                  * the nick AAA is the same as the nick aaa.
52                  */
53                 ServerInstance->Log(DEBUG,"old nick is new nick, not updating hash (case change only)");
54                 strlcpy(oldnick, user->nick, NICKMAX - 1);
55                 int MOD_RESULT = 0;
56                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
57                 if (MOD_RESULT)
58                         return CMD_FAILURE;
59                 if (user->registered == REG_ALL)
60                         user->WriteCommon("NICK %s",parameters[0]);
61                 strlcpy(user->nick, parameters[0], NICKMAX - 1);
62                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
63                 return CMD_SUCCESS;
64         }
65         else
66         {
67                 QLine* mq = ServerInstance->XLines->matches_qline(parameters[0]);
68                 if (mq)
69                 {
70                         ServerInstance->SNO->WriteToSnoMask('x', "Q-Lined nickname %s from %s!%s@%s: %s", parameters[0], user->nick, user->ident, user->host, mq->reason);
71                         user->WriteServ("432 %s %s :Invalid nickname: %s",user->nick,parameters[0], mq->reason);
72                         return CMD_FAILURE;
73                 }
74                 if ((ServerInstance->FindNick(parameters[0])) && (ServerInstance->FindNick(parameters[0]) != user) && (ServerInstance->IsNick(parameters[0])))
75                 {
76                         userrec* InUse = ServerInstance->FindNick(parameters[0]);
77                         if (InUse->registered != REG_ALL)
78                         {
79                                 userrec::QuitUser(ServerInstance, InUse, "Nickname overruled");
80                         }
81                         else
82                         {
83                                 user->WriteServ("433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
84                                 return CMD_FAILURE;
85                         }
86                 }
87         }
88         if ((!ServerInstance->IsNick(parameters[0])) && (IS_LOCAL(user)))
89         {
90                 user->WriteServ("432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
91                 return CMD_FAILURE;
92         }
93
94         if (user->registered == REG_ALL)
95         {
96                 int MOD_RESULT = 0;
97                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
98                 if (MOD_RESULT) {
99                         // if a module returns true, the nick change is silently forbidden.
100                         return CMD_FAILURE;
101                 }
102
103                 user->WriteCommon("NICK %s",parameters[0]);
104                 
105         }
106
107         strlcpy(oldnick, user->nick, NICKMAX - 1);
108
109         /* change the nick of the user in the users_hash */
110         user = user->UpdateNickHash(parameters[0]);
111         /* actually change the nick within the record */
112         if (!user) return CMD_FAILURE;
113         if (!*user->nick) return CMD_FAILURE;
114
115         strlcpy(user->nick, parameters[0], NICKMAX - 1);
116
117         ServerInstance->Log(DEBUG,"new nick set: %s",user->nick);
118         
119         if (user->registered < REG_NICKUSER)
120         {
121                 user->registered = (user->registered | REG_NICK);
122                 // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
123                 // and unless we're lucky we'll get a duff one later on.
124                 //user->dns_done = (!lookup_dns(user->nick));
125                 //if (user->dns_done)
126                 //      ServerInstance->Log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
127
128                 if (ServerInstance->Config->NoUserDns)
129                 {
130                         user->dns_done = true;
131                 }
132                 else
133                 {
134                         user->StartDNSLookup();
135                         if (user->dns_done)
136                                 ServerInstance->Log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
137                 }
138                 if (ServerInstance->next_call > ServerInstance->Time() + ServerInstance->Config->dns_timeout)
139                         ServerInstance->next_call = ServerInstance->Time() + ServerInstance->Config->dns_timeout;
140         }
141         if (user->registered == REG_NICKUSER)
142         {
143                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
144                 FOREACH_MOD(I_OnUserRegister,OnUserRegister(user));
145                 //ConnectUser(user,NULL);
146         }
147         if (user->registered == REG_ALL)
148         {
149                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
150         }
151
152         return CMD_SUCCESS;
153
154 }
155