]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
e7c9e01994cd85d2d2a8c4f38f2ecba406c26cd6
[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
23  public:
24         CommandNicklock (InspIRCd* Instance, Module* Creator) : Command(Instance, Creator,"NICKLOCK", "o", 2)
25         {
26                 syntax = "<oldnick> <newnick>";
27                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
28         }
29
30         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
31         {
32                 User* target = ServerInstance->FindNick(parameters[0]);
33
34                 /* Do local sanity checks and bails */
35                 if (IS_LOCAL(user))
36                 {
37                         if (target && ServerInstance->ULine(target->server))
38                         {
39                                 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Cannot use an NICKLOCK command on a u-lined client",user->nick.c_str());
40                                 return CMD_FAILURE;
41                         }
42
43                         if (!target)
44                         {
45                                 user->WriteServ("NOTICE %s :*** No such nickname: '%s'", user->nick.c_str(), parameters[0].c_str());
46                                 return CMD_FAILURE;
47                         }
48
49                         if (!ServerInstance->IsNick(parameters[1].c_str(), ServerInstance->Config->Limits.NickMax))
50                         {
51                                 user->WriteServ("NOTICE %s :*** Invalid nickname '%s'", user->nick.c_str(), parameters[1].c_str());
52                                 return CMD_FAILURE;
53                         }
54
55                         user->WriteServ("947 %s %s :Nickname now locked.", user->nick.c_str(), parameters[1].c_str());
56                 }
57
58                 /* If we made it this far, extend the user */
59                 if (target && IS_LOCAL(target))
60                 {
61                         // This has to be done *here*, because this metadata must be stored netwide.
62                         target->Extend("nick_locked", "ON");
63
64                         /* Only send out nick from local server */
65                         target->Extend("nick_locked");
66
67                         std::string oldnick = target->nick;
68                         if (target->ForceNickChange(parameters[1].c_str()))
69                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKLOCK to change and hold "+oldnick+" to "+parameters[1]);
70                         else
71                         {
72                                 std::string newnick = target->nick;
73                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKLOCK, but "+oldnick+" failed nick change to "+parameters[1]+" and was locked to "+newnick+" instead");
74                         }
75                 }
76
77                 return CMD_SUCCESS;
78         }
79
80         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
81         {
82                 User* dest = ServerInstance->FindNick(parameters[0]);
83                 if (dest)
84                         return ROUTE_OPT_UCAST(dest->server);
85                 return ROUTE_LOCALONLY;
86         }
87 };
88
89 /** Handle /NICKUNLOCK
90  */
91 class CommandNickunlock : public Command
92 {
93  public:
94         CommandNickunlock (InspIRCd* Instance, Module* Creator) : Command(Instance, Creator,"NICKUNLOCK", "o", 1)
95         {
96                 syntax = "<locked-nick>";
97                 TRANSLATE2(TR_NICK, TR_END);
98         }
99
100         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
101         {
102                 User* target = ServerInstance->FindNick(parameters[0]);
103
104                 /* Do local sanity checks and bails */
105                 if (IS_LOCAL(user))
106                 {
107                         if (target && ServerInstance->ULine(target->server))
108                         {
109                                 user->WriteNumeric(ERR_NOPRIVILEGES, "%s :Cannot use an NICKUNLOCK command on a u-lined client",user->nick.c_str());
110                                 return CMD_FAILURE;
111                         }
112
113                         if (!target)
114                         {
115                                 user->WriteServ("NOTICE %s :*** No such nickname: '%s'", user->nick.c_str(), parameters[0].c_str());
116                                 return CMD_FAILURE;
117                         }
118                 }
119
120                 if (target && IS_LOCAL(target))
121                 {
122                         if (target->Shrink("nick_locked"))
123                         {
124                                 ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used NICKUNLOCK on "+parameters[0]);
125                                 user->WriteNumeric(945, "%s %s :Nickname now unlocked.",user->nick.c_str(),target->nick.c_str());
126                         }
127                         else
128                         {
129                                 user->WriteNumeric(946, "%s %s :This user's nickname is not locked.",user->nick.c_str(),target->nick.c_str());
130                                 return CMD_FAILURE;
131                         }
132                 }
133
134                 return CMD_SUCCESS;
135         }
136
137         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
138         {
139                 User* dest = ServerInstance->FindNick(parameters[0]);
140                 if (dest)
141                         return ROUTE_OPT_UCAST(dest->server);
142                 return ROUTE_LOCALONLY;
143         }
144 };
145
146
147 class ModuleNickLock : public Module
148 {
149         CommandNicklock cmd1;
150         CommandNickunlock cmd2;
151  public:
152         ModuleNickLock(InspIRCd* Me)
153                 : Module(Me), cmd1(Me, this), cmd2(Me, this)
154         {
155                 ServerInstance->AddCommand(&cmd1);
156                 ServerInstance->AddCommand(&cmd2);
157                 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserQuit, I_OnCleanup };
158                 ServerInstance->Modules->Attach(eventlist, this, 3);
159         }
160
161         virtual ~ModuleNickLock()
162         {
163         }
164
165         virtual Version GetVersion()
166         {
167                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
168         }
169
170
171         virtual 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->GetExt("NICKForced")) /* Allow forced nick changes */
180                         return MOD_RES_PASSTHRU;
181
182                 if (user->GetExt("nick_locked"))
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         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
191         {
192                 user->Shrink("nick_locked");
193         }
194
195         void Prioritize()
196         {
197                 Module *nflood = ServerInstance->Modules->Find("m_nickflood.so");
198                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &nflood);
199         }
200
201         virtual void OnCleanup(int target_type, void* item)
202         {
203                 if(target_type == TYPE_USER)
204                 {
205                         User* user = (User*)item;
206                         user->Shrink("nick_locked");
207                 }
208         }
209 };
210
211 MODULE_INIT(ModuleNickLock)