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