]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
Replace OnAccessCheck with OnPreMode to remove a number of redundant checks
[user/henk/code/inspircd.git] / src / modules / m_nicklock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides the NICKLOCK command, allows an oper to chage a users nick and lock them to it until they quit */
17
18 /** Handle /NICKLOCK
19  */
20 class CommandNicklock : public Command
21 {
22  public:
23         LocalIntExt& locked;
24         CommandNicklock (Module* Creator, LocalIntExt& ext) : Command(Creator->ServerInstance, Creator,"NICKLOCK", "o", 2),
25                 locked(ext)
26         {
27                 syntax = "<oldnick> <newnick>";
28                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
29         }
30
31         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
32         {
33                 User* target = ServerInstance->FindNick(parameters[0]);
34
35                 /* Do local sanity checks and bails */
36                 if (IS_LOCAL(user))
37                 {
38                         if (target && ServerInstance->ULine(target->server))
39                         {
40                                 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Cannot use an NICKLOCK command on a u-lined client",user->nick.c_str());
41                                 return CMD_FAILURE;
42                         }
43
44                         if (!target)
45                         {
46                                 user->WriteServ("NOTICE %s :*** No such nickname: '%s'", user->nick.c_str(), parameters[0].c_str());
47                                 return CMD_FAILURE;
48                         }
49
50                         if (!ServerInstance->IsNick(parameters[1].c_str(), ServerInstance->Config->Limits.NickMax))
51                         {
52                                 user->WriteServ("NOTICE %s :*** Invalid nickname '%s'", user->nick.c_str(), parameters[1].c_str());
53                                 return CMD_FAILURE;
54                         }
55
56                         user->WriteServ("947 %s %s :Nickname now locked.", user->nick.c_str(), parameters[1].c_str());
57                 }
58
59                 /* If we made it this far, extend the user */
60                 if (target && IS_LOCAL(target))
61                 {
62                         locked.set(target, 1);
63
64                         std::string oldnick = target->nick;
65                         if (target->ForceNickChange(parameters[1].c_str()))
66                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKLOCK to change and hold "+oldnick+" to "+parameters[1]);
67                         else
68                         {
69                                 std::string newnick = target->nick;
70                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKLOCK, but "+oldnick+" failed nick change to "+parameters[1]+" and was locked to "+newnick+" instead");
71                         }
72                 }
73
74                 return CMD_SUCCESS;
75         }
76
77         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
78         {
79                 User* dest = ServerInstance->FindNick(parameters[0]);
80                 if (dest)
81                         return ROUTE_OPT_UCAST(dest->server);
82                 return ROUTE_LOCALONLY;
83         }
84 };
85
86 /** Handle /NICKUNLOCK
87  */
88 class CommandNickunlock : public Command
89 {
90  public:
91         LocalIntExt& locked;
92         CommandNickunlock (Module* Creator, LocalIntExt& ext) : Command(Creator->ServerInstance, Creator,"NICKUNLOCK", "o", 1),
93                 locked(ext)
94         {
95                 syntax = "<locked-nick>";
96                 TRANSLATE2(TR_NICK, TR_END);
97         }
98
99         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
100         {
101                 User* target = ServerInstance->FindNick(parameters[0]);
102
103                 /* Do local sanity checks and bails */
104                 if (IS_LOCAL(user))
105                 {
106                         if (target && ServerInstance->ULine(target->server))
107                         {
108                                 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Cannot use an NICKUNLOCK command on a u-lined client",user->nick.c_str());
109                                 return CMD_FAILURE;
110                         }
111
112                         if (!target)
113                         {
114                                 user->WriteServ("NOTICE %s :*** No such nickname: '%s'", user->nick.c_str(), parameters[0].c_str());
115                                 return CMD_FAILURE;
116                         }
117                 }
118
119                 if (target && IS_LOCAL(target))
120                 {
121                         if (locked.set(target, 0))
122                         {
123                                 ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used NICKUNLOCK on "+parameters[0]);
124                                 user->WriteNumeric(945, "%s %s :Nickname now unlocked.",user->nick.c_str(),target->nick.c_str());
125                         }
126                         else
127                         {
128                                 user->WriteNumeric(946, "%s %s :This user's nickname is not locked.",user->nick.c_str(),target->nick.c_str());
129                                 return CMD_FAILURE;
130                         }
131                 }
132
133                 return CMD_SUCCESS;
134         }
135
136         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
137         {
138                 User* dest = ServerInstance->FindNick(parameters[0]);
139                 if (dest)
140                         return ROUTE_OPT_UCAST(dest->server);
141                 return ROUTE_LOCALONLY;
142         }
143 };
144
145
146 class ModuleNickLock : public Module
147 {
148         LocalIntExt locked;
149         CommandNicklock cmd1;
150         CommandNickunlock cmd2;
151  public:
152         ModuleNickLock(InspIRCd* Me)
153                 : Module(Me), locked("nick_locked", this), cmd1(this, locked), cmd2(this, locked)
154         {
155                 ServerInstance->AddCommand(&cmd1);
156                 ServerInstance->AddCommand(&cmd2);
157                 Extensible::Register(&locked);
158                 ServerInstance->Modules->Attach(I_OnUserPreNick, this);
159         }
160
161         ~ModuleNickLock()
162         {
163         }
164
165         Version GetVersion()
166         {
167                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
168         }
169
170
171         ModResult OnUserPreNick(User* user, const std::string &newnick)
172         {
173                 if (!IS_LOCAL(user))
174                         return MOD_RES_PASSTHRU;
175
176                 if (isdigit(newnick[0])) /* Allow a switch to a UID */
177                         return MOD_RES_PASSTHRU;
178
179                 if (User::NICKForced.get(user)) /* Allow forced nick changes */
180                         return MOD_RES_PASSTHRU;
181
182                 if (locked.get(user))
183                 {
184                         user->WriteNumeric(447, "%s :You cannot change your nickname (your nick is locked)",user->nick.c_str());
185                         return MOD_RES_DENY;
186                 }
187                 return MOD_RES_PASSTHRU;
188         }
189
190         void Prioritize()
191         {
192                 Module *nflood = ServerInstance->Modules->Find("m_nickflood.so");
193                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &nflood);
194         }
195 };
196
197 MODULE_INIT(ModuleNickLock)