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