]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
And make it work here
[user/henk/code/inspircd.git] / src / cmd_nick.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "configreader.h"
18 #include "users.h"
19 #include "modules.h"
20 #include "inspircd.h"
21 #include "xline.h"
22 #include "commands/cmd_nick.h"
23
24 void cmd_nick::Handle (const char** parameters, int pcnt, userrec *user)
25 {
26         char oldnick[NICKMAX];
27
28         if (pcnt < 1) 
29         {
30                 ServerInstance->Log(DEBUG,"not enough params for handle_nick");
31                 return;
32         }
33         if (!parameters[0])
34         {
35                 ServerInstance->Log(DEBUG,"invalid parameter passed to handle_nick");
36                 return;
37         }
38         if (!parameters[0][0])
39         {
40                 ServerInstance->Log(DEBUG,"zero length new nick passed to handle_nick");
41                 return;
42         }
43         if (!user)
44         {
45                 ServerInstance->Log(DEBUG,"invalid user passed to handle_nick");
46                 return;
47         }
48         if (!user->nick)
49         {
50                 ServerInstance->Log(DEBUG,"invalid old nick passed to handle_nick");
51                 return;
52         }
53         if (irc::string(user->nick) == irc::string(parameters[0]))
54         {
55                 /* If its exactly the same, even case, dont do anything. */
56                 if (!strcmp(user->nick,parameters[0]))
57                         return;
58                 /* Its a change of case. People insisted that they should be
59                  * able to do silly things like this even though the RFC says
60                  * the nick AAA is the same as the nick aaa.
61                  */
62                 ServerInstance->Log(DEBUG,"old nick is new nick, not updating hash (case change only)");
63                 strlcpy(oldnick, user->nick, NICKMAX - 1);
64                 int MOD_RESULT = 0;
65                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
66                 if (MOD_RESULT)
67                         return;
68                 if (user->registered == REG_ALL)
69                         user->WriteCommon("NICK %s",parameters[0]);
70                 strlcpy(user->nick, parameters[0], NICKMAX - 1);
71                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
72                 return;
73         }
74         else
75         {
76                 if ((*parameters[0] == ':') && (*(parameters[0]+1) != 0))
77                 {
78                         parameters[0]++;
79                 }
80                 char* mq = ServerInstance->XLines->matches_qline(parameters[0]);
81                 if (mq)
82                 {
83                         ServerInstance->WriteOpers("*** Q-Lined nickname %s from %s!%s@%s: %s",parameters[0],user->nick,user->ident,user->host,mq);
84                         user->WriteServ("432 %s %s :Invalid nickname: %s",user->nick,parameters[0],mq);
85                         return;
86                 }
87                 if ((ServerInstance->FindNick(parameters[0])) && (ServerInstance->FindNick(parameters[0]) != user))
88                 {
89                         user->WriteServ("433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
90                         return;
91                 }
92         }
93         if ((!ServerInstance->IsNick(parameters[0])) && (IS_LOCAL(user)))
94         {
95                 user->WriteServ("432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
96                 return;
97         }
98
99         if (user->registered == REG_ALL)
100         {
101                 int MOD_RESULT = 0;
102                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
103                 if (MOD_RESULT) {
104                         // if a module returns true, the nick change is silently forbidden.
105                         return;
106                 }
107
108                 user->WriteCommon("NICK %s",parameters[0]);
109                 
110         }
111
112         strlcpy(oldnick, user->nick, NICKMAX - 1);
113
114         /* change the nick of the user in the users_hash */
115         user = user->UpdateNickHash(parameters[0]);
116         /* actually change the nick within the record */
117         if (!user) return;
118         if (!user->nick) return;
119
120         strlcpy(user->nick, parameters[0], NICKMAX - 1);
121
122         ServerInstance->Log(DEBUG,"new nick set: %s",user->nick);
123         
124         if (user->registered < REG_NICKUSER)
125         {
126                 user->registered = (user->registered | REG_NICK);
127                 // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
128                 // and unless we're lucky we'll get a duff one later on.
129                 //user->dns_done = (!lookup_dns(user->nick));
130                 //if (user->dns_done)
131                 //      ServerInstance->Log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
132
133                 if (ServerInstance->Config->NoUserDns)
134                 {
135                         user->dns_done = true;
136                 }
137                 else
138                 {
139                         user->StartDNSLookup();
140                         if (user->dns_done)
141                                 ServerInstance->Log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
142                 }
143         }
144         if (user->registered == REG_NICKUSER)
145         {
146                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
147                 FOREACH_MOD(I_OnUserRegister,OnUserRegister(user));
148                 //ConnectUser(user,NULL);
149         }
150         if (user->registered == REG_ALL)
151         {
152                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
153         }
154 }