]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
Fix some of the include guard names (requested by SaberUK)
[user/henk/code/inspircd.git] / src / modules / m_nicklock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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,"NICKLOCK", 2),
25                 locked(ext)
26         {
27                 flags_needed = 'o';
28                 syntax = "<oldnick> <newnick>";
29                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
30         }
31
32         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
33         {
34                 User* target = ServerInstance->FindNick(parameters[0]);
35
36                 if (!target)
37                 {
38                         user->WriteServ("NOTICE %s :*** No such nickname: '%s'", user->nick.c_str(), parameters[0].c_str());
39                         return CMD_FAILURE;
40                 }
41
42                 /* Do local sanity checks and bails */
43                 if (IS_LOCAL(user))
44                 {
45                         if (!ServerInstance->IsNick(parameters[1].c_str(), ServerInstance->Config->Limits.NickMax))
46                         {
47                                 user->WriteServ("NOTICE %s :*** Invalid nickname '%s'", user->nick.c_str(), parameters[1].c_str());
48                                 return CMD_FAILURE;
49                         }
50
51                         user->WriteServ("947 %s %s :Nickname now locked.", user->nick.c_str(), parameters[1].c_str());
52                 }
53
54                 /* If we made it this far, extend the user */
55                 if (IS_LOCAL(target))
56                 {
57                         locked.set(target, 1);
58
59                         std::string oldnick = target->nick;
60                         if (target->ForceNickChange(parameters[1].c_str()))
61                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKLOCK to change and hold "+oldnick+" to "+parameters[1]);
62                         else
63                         {
64                                 std::string newnick = target->nick;
65                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKLOCK, but "+oldnick+" failed nick change to "+parameters[1]+" and was locked to "+newnick+" instead");
66                         }
67                 }
68
69                 return CMD_SUCCESS;
70         }
71
72         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
73         {
74                 User* dest = ServerInstance->FindNick(parameters[0]);
75                 if (dest)
76                         return ROUTE_OPT_UCAST(dest->server);
77                 return ROUTE_LOCALONLY;
78         }
79 };
80
81 /** Handle /NICKUNLOCK
82  */
83 class CommandNickunlock : public Command
84 {
85  public:
86         LocalIntExt& locked;
87         CommandNickunlock (Module* Creator, LocalIntExt& ext) : Command(Creator,"NICKUNLOCK", 1),
88                 locked(ext)
89         {
90                 flags_needed = 'o';
91                 syntax = "<locked-nick>";
92                 TRANSLATE2(TR_NICK, TR_END);
93         }
94
95         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
96         {
97                 User* target = ServerInstance->FindNick(parameters[0]);
98
99                 if (!target)
100                 {
101                         user->WriteServ("NOTICE %s :*** No such nickname: '%s'", user->nick.c_str(), parameters[0].c_str());
102                         return CMD_FAILURE;
103                 }
104
105                 if (IS_LOCAL(target))
106                 {
107                         if (locked.set(target, 0))
108                         {
109                                 ServerInstance->SNO->WriteGlobalSno('a', user->nick+" used NICKUNLOCK on "+target->nick);
110                                 user->SendText(":%s 945 %s %s :Nickname now unlocked.",
111                                         ServerInstance->Config->ServerName.c_str(),user->nick.c_str(),target->nick.c_str());
112                         }
113                         else
114                         {
115                                 user->SendText(":%s 946 %s %s :This user's nickname is not locked.",
116                                         ServerInstance->Config->ServerName.c_str(),user->nick.c_str(),target->nick.c_str());
117                                 return CMD_FAILURE;
118                         }
119                 }
120
121                 return CMD_SUCCESS;
122         }
123
124         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
125         {
126                 User* dest = ServerInstance->FindNick(parameters[0]);
127                 if (dest)
128                         return ROUTE_OPT_UCAST(dest->server);
129                 return ROUTE_LOCALONLY;
130         }
131 };
132
133
134 class ModuleNickLock : public Module
135 {
136         LocalIntExt locked;
137         CommandNicklock cmd1;
138         CommandNickunlock cmd2;
139  public:
140         ModuleNickLock()
141                 : locked("nick_locked", this), cmd1(this, locked), cmd2(this, locked)
142         {
143                 ServerInstance->AddCommand(&cmd1);
144                 ServerInstance->AddCommand(&cmd2);
145                 ServerInstance->Extensions.Register(&locked);
146                 ServerInstance->Modules->Attach(I_OnUserPreNick, this);
147         }
148
149         ~ModuleNickLock()
150         {
151         }
152
153         Version GetVersion()
154         {
155                 return Version("Provides the NICKLOCK command, allows an oper to chage a users nick and lock them to it until they quit", VF_OPTCOMMON | VF_VENDOR);
156         }
157
158         ModResult OnUserPreNick(User* user, const std::string &newnick)
159         {
160                 if (!IS_LOCAL(user))
161                         return MOD_RES_PASSTHRU;
162
163                 if (ServerInstance->NICKForced.get(user)) /* Allow forced nick changes */
164                         return MOD_RES_PASSTHRU;
165
166                 if (locked.get(user))
167                 {
168                         user->WriteNumeric(447, "%s :You cannot change your nickname (your nick is locked)",user->nick.c_str());
169                         return MOD_RES_DENY;
170                 }
171                 return MOD_RES_PASSTHRU;
172         }
173
174         void Prioritize()
175         {
176                 Module *nflood = ServerInstance->Modules->Find("m_nickflood.so");
177                 ServerInstance->Modules->SetPriority(this, I_OnUserPreJoin, PRIORITY_BEFORE, &nflood);
178         }
179 };
180
181 MODULE_INIT(ModuleNickLock)