]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
cdb37690ef3d18b847bee788798060912b666a94
[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 <string>
18 #include <vector>
19 #include "inspircd_config.h"
20 #include "configreader.h"
21 #include "hash_map.h"
22 #include "users.h"
23 #include "modules.h"
24 #include "commands.h"
25 #include "xline.h"
26 #include "dns.h"
27 #include "helperfuncs.h"
28 #include "hashcomp.h"
29 #include "commands/cmd_nick.h"
30
31 extern ServerConfig* Config;
32 extern int MODCOUNT;
33 extern std::vector<Module*> modules;
34 extern std::vector<ircd_module*> factory;
35 extern time_t TIME;
36
37 void cmd_nick::Handle (const char** parameters, int pcnt, userrec *user)
38 {
39         char oldnick[NICKMAX];
40
41         if (pcnt < 1) 
42         {
43                 log(DEBUG,"not enough params for handle_nick");
44                 return;
45         }
46         if (!parameters[0])
47         {
48                 log(DEBUG,"invalid parameter passed to handle_nick");
49                 return;
50         }
51         if (!parameters[0][0])
52         {
53                 log(DEBUG,"zero length new nick passed to handle_nick");
54                 return;
55         }
56         if (!user)
57         {
58                 log(DEBUG,"invalid user passed to handle_nick");
59                 return;
60         }
61         if (!user->nick)
62         {
63                 log(DEBUG,"invalid old nick passed to handle_nick");
64                 return;
65         }
66         if (irc::string(user->nick) == irc::string(parameters[0]))
67         {
68                 /* If its exactly the same, even case, dont do anything. */
69                 if (!strcmp(user->nick,parameters[0]))
70                         return;
71                 /* Its a change of case. People insisted that they should be
72                  * able to do silly things like this even though the RFC says
73                  * the nick AAA is the same as the nick aaa.
74                  */
75                 log(DEBUG,"old nick is new nick, not updating hash (case change only)");
76                 strlcpy(oldnick, user->nick, NICKMAX - 1);
77                 int MOD_RESULT = 0;
78                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
79                 if (MOD_RESULT)
80                         return;
81                 if (user->registered == REG_ALL)
82                         WriteCommon(user,"NICK %s",parameters[0]);
83                 strlcpy(user->nick, parameters[0], NICKMAX - 1);
84                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
85                 return;
86         }
87         else
88         {
89                 if ((*parameters[0] == ':') && (*(parameters[0]+1) != 0))
90                 {
91                         parameters[0]++;
92                 }
93                 if (matches_qline(parameters[0]))
94                 {
95                         WriteOpers("*** Q-Lined nickname %s from %s!%s@%s: %s",parameters[0],user->nick,user->ident,user->host,matches_qline(parameters[0]));
96                         WriteServ(user->fd,"432 %s %s :Invalid nickname: %s",user->nick,parameters[0],matches_qline(parameters[0]));
97                         return;
98                 }
99                 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
100                 {
101                         WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
102                         return;
103                 }
104         }
105         if ((isnick(parameters[0]) == 0) && (IS_LOCAL(user)))
106         {
107                 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
108                 return;
109         }
110
111         if (user->registered == REG_ALL)
112         {
113                 int MOD_RESULT = 0;
114                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
115                 if (MOD_RESULT) {
116                         // if a module returns true, the nick change is silently forbidden.
117                         return;
118                 }
119
120                 WriteCommon(user,"NICK %s",parameters[0]);
121                 
122         }
123
124         strlcpy(oldnick, user->nick, NICKMAX - 1);
125
126         /* change the nick of the user in the users_hash */
127         user = user->UpdateNickHash(parameters[0]);
128         /* actually change the nick within the record */
129         if (!user) return;
130         if (!user->nick) return;
131
132         strlcpy(user->nick, parameters[0], NICKMAX - 1);
133
134         log(DEBUG,"new nick set: %s",user->nick);
135         
136         if (user->registered < REG_NICKUSER)
137         {
138                 user->registered = (user->registered | REG_NICK);
139                 // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
140                 // and unless we're lucky we'll get a duff one later on.
141                 //user->dns_done = (!lookup_dns(user->nick));
142                 //if (user->dns_done)
143                 //      log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
144
145                 if (Config->NoUserDns)
146                 {
147                         user->dns_done = true;
148                 }
149                 else
150                 {
151                         user->StartDNSLookup();
152                         if (user->dns_done)
153                                 log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
154                 }
155         }
156         if (user->registered == REG_NICKUSER)
157         {
158                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
159                 FOREACH_MOD(I_OnUserRegister,OnUserRegister(user));
160                 //ConnectUser(user,NULL);
161         }
162         if (user->registered == REG_ALL)
163         {
164                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
165         }
166 }