]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
Include tidyups - brain
[user/henk/code/inspircd.git] / src / cmd_nick.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain.net>
8  *                <Craig.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 "helperfuncs.h"
46 #include "hashcomp.h"
47 #include "socketengine.h"
48 #include "typedefs.h"
49 #include "command_parse.h"
50 #include "cmd_nick.h"
51
52 extern ServerConfig* Config;
53 extern InspIRCd* ServerInstance;
54 extern int MODCOUNT;
55 extern std::vector<Module*> modules;
56 extern std::vector<ircd_module*> factory;
57 extern time_t TIME;
58 extern user_hash clientlist;
59 extern chan_hash chanlist;
60 extern whowas_hash whowas;
61 extern std::vector<userrec*> all_opers;
62 extern std::vector<userrec*> local_users;
63 extern userrec* fd_ref_table[65536];
64
65 void cmd_nick::Handle (char **parameters, int pcnt, userrec *user)
66 {
67         if (pcnt < 1) 
68         {
69                 log(DEBUG,"not enough params for handle_nick");
70                 return;
71         }
72         if (!parameters[0])
73         {
74                 log(DEBUG,"invalid parameter passed to handle_nick");
75                 return;
76         }
77         if (!parameters[0][0])
78         {
79                 log(DEBUG,"zero length new nick passed to handle_nick");
80                 return;
81         }
82         if (!user)
83         {
84                 log(DEBUG,"invalid user passed to handle_nick");
85                 return;
86         }
87         if (!user->nick)
88         {
89                 log(DEBUG,"invalid old nick passed to handle_nick");
90                 return;
91         }
92         if (!strcasecmp(user->nick,parameters[0]))
93         {
94                 log(DEBUG,"old nick is new nick, skipping");
95                 return;
96         }
97         else
98         {
99                 if (strlen(parameters[0]) > 1)
100                 {
101                         if (parameters[0][0] == ':')
102                         {
103                                 *parameters[0]++;
104                         }
105                 }
106                 if (matches_qline(parameters[0]))
107                 {
108                         WriteOpers("*** Q-Lined nickname %s from %s!%s@%s: %s",parameters[0],user->nick,user->ident,user->host,matches_qline(parameters[0]));
109                         WriteServ(user->fd,"432 %s %s :Invalid nickname: %s",user->nick,parameters[0],matches_qline(parameters[0]));
110                         return;
111                 }
112                 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
113                 {
114                         WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
115                         return;
116                 }
117         }
118         if (isnick(parameters[0]) == 0)
119         {
120                 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
121                 return;
122         }
123
124         if (user->registered == 7)
125         {
126                 int MOD_RESULT = 0;
127                 FOREACH_RESULT(OnUserPreNick(user,parameters[0]));
128                 if (MOD_RESULT) {
129                         // if a module returns true, the nick change is silently forbidden.
130                         return;
131                 }
132
133                 WriteCommon(user,"NICK %s",parameters[0]);
134                 
135         }
136         
137         char oldnick[NICKMAX];
138         strlcpy(oldnick,user->nick,NICKMAX);
139
140         /* change the nick of the user in the users_hash */
141         user = ReHashNick(user->nick, parameters[0]);
142         /* actually change the nick within the record */
143         if (!user) return;
144         if (!user->nick) return;
145
146         strlcpy(user->nick, parameters[0],NICKMAX);
147
148         log(DEBUG,"new nick set: %s",user->nick);
149         
150         if (user->registered < 3)
151         {
152                 user->registered = (user->registered | 2);
153                 // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
154                 // and unless we're lucky we'll get a duff one later on.
155                 //user->dns_done = (!lookup_dns(user->nick));
156                 //if (user->dns_done)
157                 //      log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
158
159 #ifdef THREADED_DNS
160                 // initialize their dns lookup thread
161                 if (pthread_create(&user->dnsthread, NULL, dns_task, (void *)user) != 0)
162                 {
163                         log(DEBUG,"Failed to create DNS lookup thread for user %s",user->nick);
164                 }
165 #else
166                 user->dns_done = (!lookup_dns(user->nick));
167                 if (user->dns_done)
168                         log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
169 #endif
170         
171         }
172         if (user->registered == 3)
173         {
174                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
175                 FOREACH_MOD OnUserRegister(user);
176                 ConnectUser(user);
177         }
178         if (user->registered == 7)
179         {
180                 FOREACH_MOD OnUserPostNick(user,oldnick);
181         }
182 }
183