]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
86054ab3e1a36e9ce4bfdd95cac1e3783ce20674
[user/henk/code/inspircd.git] / src / modules / m_nicklock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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         char* dummy;
23  public:
24         CommandNicklock (InspIRCd* Instance) : Command(Instance,"NICKLOCK", "o", 2)
25         {
26                 this->source = "m_nicklock.so";
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                 irc::string server;
35                 irc::string me;
36
37                 // check user exists
38                 if (!target)
39                 {
40                         return CMD_FAILURE;
41                 }
42
43                 // check if user is locked
44                 if (target->GetExt("nick_locked", dummy))
45                 {
46                         user->WriteNumeric(946, "%s %s :This user's nickname is already locked.",user->nick.c_str(),target->nick.c_str());
47                         return CMD_FAILURE;
48                 }
49
50                 // check nick is valid
51                 if (IS_LOCAL(user) && !ServerInstance->IsNick(parameters[1].c_str(), ServerInstance->Config->Limits.NickMax))
52                 {
53                         return CMD_FAILURE;
54                 }
55
56                 // let others know
57                 ServerInstance->SNO->WriteToSnoMask('A', std::string(user->nick)+" used NICKLOCK to change and hold "+parameters[0]+" to "+parameters[1]);
58
59                 if (!target->ForceNickChange(parameters[1].c_str()))
60                 {
61                         // ugh, nickchange failed for some reason -- possibly existing nick?
62                         if (!target->ForceNickChange(target->uuid.c_str()))
63                         {
64                                 // Well shit, we cant even change them to their UID (this should not happen!)
65                                 ServerInstance->Users->QuitUser(target, "Nickname collision");
66                         }
67                 }
68
69                 // give them a lock flag
70                 target->Extend("nick_locked", "ON");
71
72                 /* route */
73                 return CMD_SUCCESS;
74         }
75 };
76
77 /** Handle /NICKUNLOCK
78  */
79 class CommandNickunlock : public Command
80 {
81  public:
82         CommandNickunlock (InspIRCd* Instance) : Command(Instance,"NICKUNLOCK", "o", 1)
83         {
84                 this->source = "m_nicklock.so";
85                 syntax = "<locked-nick>";
86         }
87
88         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
89         {
90                 User* target = ServerInstance->FindNick(parameters[0]);
91                 if (target)
92                 {
93                         target->Shrink("nick_locked");
94                         user->WriteNumeric(945, "%s %s :Nickname now unlocked.",user->nick.c_str(),target->nick.c_str());
95                         ServerInstance->SNO->WriteToSnoMask('A', std::string(user->nick)+" used NICKUNLOCK on "+parameters[0]);
96                         return CMD_SUCCESS;
97                 }
98
99                 return CMD_FAILURE;
100         }
101 };
102
103
104 class ModuleNickLock : public Module
105 {
106         CommandNicklock*        cmd1;
107         CommandNickunlock*      cmd2;
108         char* n;
109  public:
110         ModuleNickLock(InspIRCd* Me)
111                 : Module(Me)
112         {
113                 
114                 cmd1 = new CommandNicklock(ServerInstance);
115                 cmd2 = new CommandNickunlock(ServerInstance);
116                 ServerInstance->AddCommand(cmd1);
117                 ServerInstance->AddCommand(cmd2);
118                 Implementation eventlist[] = { I_OnUserPreNick, I_OnUserQuit, I_OnCleanup };
119                 ServerInstance->Modules->Attach(eventlist, this, 3);
120         }
121         
122         virtual ~ModuleNickLock()
123         {
124         }
125         
126         virtual Version GetVersion()
127         {
128                 return Version(1, 2, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
129         }
130
131
132         virtual int OnUserPreNick(User* user, const std::string &newnick)
133         {
134                 if (isdigit(newnick[0])) /* allow a switch to a UID */
135                         return 0;
136
137                 if (user->GetExt("nick_locked", n))
138                 {
139                         user->WriteNumeric(447, "%s :You cannot change your nickname (your nick is locked)",user->nick.c_str());
140                         return 1;
141                 }
142                 return 0;
143         }
144
145         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
146         {
147                 user->Shrink("nick_locked");
148         }
149
150         virtual void OnCleanup(int target_type, void* item)
151         {
152                 if(target_type == TYPE_USER)
153                 {
154                         User* user = (User*)item;
155                         user->Shrink("nick_locked");
156                 }
157         }
158 };
159
160 MODULE_INIT(ModuleNickLock)