]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/cmd_nick.cpp
Renamed to chanrec::modes
[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 std::vector<userrec*> all_opers;
62 extern std::vector<userrec*> local_users;
63 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
64
65 void cmd_nick::Handle (char **parameters, int pcnt, userrec *user)
66 {
67         char oldnick[NICKMAX];
68
69         if (pcnt < 1) 
70         {
71                 log(DEBUG,"not enough params for handle_nick");
72                 return;
73         }
74         if (!parameters[0])
75         {
76                 log(DEBUG,"invalid parameter passed to handle_nick");
77                 return;
78         }
79         if (!parameters[0][0])
80         {
81                 log(DEBUG,"zero length new nick passed to handle_nick");
82                 return;
83         }
84         if (!user)
85         {
86                 log(DEBUG,"invalid user passed to handle_nick");
87                 return;
88         }
89         if (!user->nick)
90         {
91                 log(DEBUG,"invalid old nick passed to handle_nick");
92                 return;
93         }
94         if (irc::string(user->nick) == irc::string(parameters[0]))
95         {
96                 /* If its exactly the same, even case, dont do anything. */
97                 if (!strcmp(user->nick,parameters[0]))
98                         return;
99                 /* Its a change of case. People insisted that they should be
100                  * able to do silly things like this even though the RFC says
101                  * the nick AAA is the same as the nick aaa.
102                  */
103                 log(DEBUG,"old nick is new nick, not updating hash (case change only)");
104                 strlcpy(oldnick, user->nick, NICKMAX - 1);
105                 int MOD_RESULT = 0;
106                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
107                 if (MOD_RESULT)
108                         return;
109                 if (user->registered == 7)
110                         WriteCommon(user,"NICK %s",parameters[0]);
111                 strlcpy(user->nick, parameters[0], NICKMAX - 1);
112                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
113                 return;
114         }
115         else
116         {
117                 if ((*parameters[0] == ':') && (*(parameters[0]+1) != 0))
118                 {
119                         parameters[0]++;
120                 }
121                 if (matches_qline(parameters[0]))
122                 {
123                         WriteOpers("*** Q-Lined nickname %s from %s!%s@%s: %s",parameters[0],user->nick,user->ident,user->host,matches_qline(parameters[0]));
124                         WriteServ(user->fd,"432 %s %s :Invalid nickname: %s",user->nick,parameters[0],matches_qline(parameters[0]));
125                         return;
126                 }
127                 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
128                 {
129                         WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
130                         return;
131                 }
132         }
133         if (isnick(parameters[0]) == 0)
134         {
135                 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
136                 return;
137         }
138
139         if (user->registered == 7)
140         {
141                 int MOD_RESULT = 0;
142                 FOREACH_RESULT(I_OnUserPreNick,OnUserPreNick(user,parameters[0]));
143                 if (MOD_RESULT) {
144                         // if a module returns true, the nick change is silently forbidden.
145                         return;
146                 }
147
148                 WriteCommon(user,"NICK %s",parameters[0]);
149                 
150         }
151
152         strlcpy(oldnick, user->nick, NICKMAX - 1);
153
154         /* change the nick of the user in the users_hash */
155         user = ReHashNick(user->nick, parameters[0]);
156         /* actually change the nick within the record */
157         if (!user) return;
158         if (!user->nick) return;
159
160         strlcpy(user->nick, parameters[0], NICKMAX - 1);
161
162         log(DEBUG,"new nick set: %s",user->nick);
163         
164         if (user->registered < 3)
165         {
166                 user->registered = (user->registered | 2);
167                 // dont attempt to look up the dns until they pick a nick... because otherwise their pointer WILL change
168                 // and unless we're lucky we'll get a duff one later on.
169                 //user->dns_done = (!lookup_dns(user->nick));
170                 //if (user->dns_done)
171                 //      log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
172
173 #ifdef THREADED_DNS
174                 // initialize their dns lookup thread
175                 if (pthread_create(&user->dnsthread, NULL, dns_task, (void *)user) != 0)
176                 {
177                         log(DEBUG,"Failed to create DNS lookup thread for user %s",user->nick);
178                 }
179 #else
180                 user->dns_done = (!lookup_dns(user->nick));
181                 if (user->dns_done)
182                         log(DEBUG,"Aborting dns lookup of %s because dns server experienced a failure.",user->nick);
183 #endif
184         
185         }
186         if (user->registered == 3)
187         {
188                 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
189                 FOREACH_MOD(I_OnUserRegister,OnUserRegister(user));
190                 //ConnectUser(user,NULL);
191         }
192         if (user->registered == 7)
193         {
194                 FOREACH_MOD(I_OnUserPostNick,OnUserPostNick(user,oldnick));
195         }
196 }
197