]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
Fix this, I think.
[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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <time.h>
23 #include <string>
24 #ifdef GCC3
25 #include <ext/hash_map>
26 #else
27 #include <hash_map>
28 #endif
29 #include <map>
30 #include <sstream>
31 #include <vector>
32 #include <deque>
33 #include "users.h"
34 #include "ctables.h"
35 #include "globals.h"
36 #include "modules.h"
37 #include "dynamic.h"
38 #include "wildcard.h"
39 #include "message.h"
40 #include "commands.h"
41 #include "mode.h"
42 #include "xline.h"
43 #include "inspstring.h"
44 #include "dnsqueue.h"
45 #include "dns.h"
46 #include "helperfuncs.h"
47 #include "hashcomp.h"
48 #include "socketengine.h"
49 #include "typedefs.h"
50 #include "command_parse.h"
51 #include "cmd_nick.h"
52
53 extern ServerConfig* Config;
54 extern InspIRCd* ServerInstance;
55 extern int MODCOUNT;
56 extern std::vector<Module*> modules;
57 extern std::vector<ircd_module*> factory;
58 extern time_t TIME;
59 extern user_hash clientlist;
60 extern chan_hash chanlist;
61 extern whowas_hash whowas;
62 extern std::vector<userrec*> all_opers;
63 extern std::vector<userrec*> local_users;
64 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
65
66 void cmd_nick::Handle (char **parameters, int pcnt, userrec *user)
67 {
68         char oldnick[NICKMAX];
69
70         if (pcnt < 1) 
71         {
72                 log(DEBUG,"not enough params for handle_nick");
73                 return;
74         }
75         if (!parameters[0])
76         {
77                 log(DEBUG,"invalid parameter passed to handle_nick");
78                 return;
79         }
80         if (!parameters[0][0])
81         {
82                 log(DEBUG,"zero length new nick passed to handle_nick");
83                 return;
84         }
85         if (!user)
86         {
87                 log(DEBUG,"invalid user passed to handle_nick");
88                 return;
89         }
90         if (!user->nick)
91         {
92                 log(DEBUG,"invalid old nick passed to handle_nick");
93                 return;
94         }
95         if (irc::string(user->nick) == irc::string(parameters[0]))
96         {
97                 /* If its exactly the same, even case, dont do anything. */
98                 if (!strcmp(user->nick,parameters[0]))
99                         return;
100                 /* Its a change of case. People insisted that they should be
101                  * able to do silly things like this even though the RFC says
102                  * the nick AAA is the same as the nick aaa.
103                  */
104                 log(DEBUG,"old nick is new nick, not updating hash (case change only)");
105                 strlcpy(oldnick, user->nick, NICKMAX - 1);
106                 int MOD_RESULT = 0;
107                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
108                 if (MOD_RESULT)
109                         return;
110                 if (user->registered == 7)
111                         WriteCommon(user,"NICK %s",parameters[0]);
112                 strlcpy(user->nick, parameters[0], NICKMAX - 1);
113                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
114                 return;
115         }
116         else
117         {
118                 if (strlen(parameters[0]) > 1)
119                 {
120                         if (parameters[0][0] == ':')
121                         {
122                                 *parameters[0]++;
123                         }
124                 }
125                 if (matches_qline(parameters[0]))
126                 {
127                         WriteOpers("*** Q-Lined nickname %s from %s!%s@%s: %s",parameters[0],user->nick,user->ident,user->host,matches_qline(parameters[0]));
128                         WriteServ(user->fd,"432 %s %s :Invalid nickname: %s",user->nick,parameters[0],matches_qline(parameters[0]));
129                         return;
130                 }
131                 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
132                 {
133                         WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
134                         return;
135                 }
136         }
137         if (isnick(parameters[0]) == 0)
138         {
139                 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
140                 return;
141         }
142
143         if (user->registered == 7)
144         {
145                 int MOD_RESULT = 0;
146                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
147                 if (MOD_RESULT) {
148                         // if a module returns true, the nick change is silently forbidden.
149                         return;
150                 }
151
152                 WriteCommon(user,"NICK %s",parameters[0]);
153                 
154         }
155
156         strlcpy(oldnick,user->nick,NICKMAX);
157
158         /* change the nick of the user in the users_hash */
159         user = ReHashNick(user->nick, parameters[0]);
160         /* actually change the nick within the record */
161         if (!user) return;
162         if (!user->nick) return;
163
164         strlcpy(user->nick, parameters[0],NICKMAX);
165
166         log(DEBUG,"new nick set: %s",user->nick);
167         
168         if (user->registered < 3)
169         {
170                 user->registered = (user->registered | 2);
171                 // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
172                 // and unless we're lucky we'll get a duff one later on.
173                 //user->dns_done = (!lookup_dns(user->nick));
174                 //if (user->dns_done)
175                 //      log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
176
177 #ifdef THREADED_DNS
178                 // initialize their dns lookup thread
179                 if (pthread_create(&user->dnsthread, NULL, dns_task, (void *)user) != 0)
180                 {
181                         log(DEBUG,"Failed to create DNS lookup thread for user %s",user->nick);
182                 }
183 #else
184                 user->dns_done = (!lookup_dns(user->nick));
185                 if (user->dns_done)
186                         log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
187 #endif
188         
189         }
190         if (user->registered == 3)
191         {
192                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
193                 FOREACH_MOD(I_OnUserRegister,OnUserRegister(user));
194                 //ConnectUser(user,NULL);
195         }
196         if (user->registered == 7)
197         {
198                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
199         }
200 }
201