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