]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
33960463b8b06fdf1cd5c5ce0e882a857b8f3927
[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 "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[65536];
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                 log(DEBUG,"old nick is new nick, not updating hash (case change only)");
98                 strlcpy(oldnick,user->nick,NICKMAX);
99                 int MOD_RESULT = 0;
100                 FOREACH_RESULT(OnUserPreNick(user,parameters[0]));
101                 if (MOD_RESULT)
102                         return;
103                 strlcpy(user->nick,parameters[0],NICKMAX);
104                 if (user->registered == 7)
105                         WriteCommon(user,"NICK %s",parameters[0]);
106                 FOREACH_MOD OnUserPostNick(user,oldnick);
107                 return;
108         }
109         else
110         {
111                 if (strlen(parameters[0]) > 1)
112                 {
113                         if (parameters[0][0] == ':')
114                         {
115                                 *parameters[0]++;
116                         }
117                 }
118                 if (matches_qline(parameters[0]))
119                 {
120                         WriteOpers("*** Q-Lined nickname %s from %s!%s@%s: %s",parameters[0],user->nick,user->ident,user->host,matches_qline(parameters[0]));
121                         WriteServ(user->fd,"432 %s %s :Invalid nickname: %s",user->nick,parameters[0],matches_qline(parameters[0]));
122                         return;
123                 }
124                 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
125                 {
126                         WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
127                         return;
128                 }
129         }
130         if (isnick(parameters[0]) == 0)
131         {
132                 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
133                 return;
134         }
135
136         if (user->registered == 7)
137         {
138                 int MOD_RESULT = 0;
139                 FOREACH_RESULT(OnUserPreNick(user,parameters[0]));
140                 if (MOD_RESULT) {
141                         // if a module returns true, the nick change is silently forbidden.
142                         return;
143                 }
144
145                 WriteCommon(user,"NICK %s",parameters[0]);
146                 
147         }
148
149         strlcpy(oldnick,user->nick,NICKMAX);
150
151         /* change the nick of the user in the users_hash */
152         user = ReHashNick(user->nick, parameters[0]);
153         /* actually change the nick within the record */
154         if (!user) return;
155         if (!user->nick) return;
156
157         strlcpy(user->nick, parameters[0],NICKMAX);
158
159         log(DEBUG,"new nick set: %s",user->nick);
160         
161         if (user->registered < 3)
162         {
163                 user->registered = (user->registered | 2);
164                 // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
165                 // and unless we're lucky we'll get a duff one later on.
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
170 #ifdef THREADED_DNS
171                 // initialize their dns lookup thread
172                 if (pthread_create(&user->dnsthread, NULL, dns_task, (void *)user) != 0)
173                 {
174                         log(DEBUG,"Failed to create DNS lookup thread for user %s",user->nick);
175                 }
176 #else
177                 user->dns_done = (!lookup_dns(user->nick));
178                 if (user->dns_done)
179                         log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
180 #endif
181         
182         }
183         if (user->registered == 3)
184         {
185                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
186                 FOREACH_MOD OnUserRegister(user);
187                 ConnectUser(user);
188         }
189         if (user->registered == 7)
190         {
191                 FOREACH_MOD OnUserPostNick(user,oldnick);
192         }
193 }
194