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