]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
9bb0f270d809f81cd249bba6a49aaf944da29961
[user/henk/code/inspircd.git] / src / modules / m_nicklock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "hashcomp.h"
19
20 /* $ModDesc: Provides the NICKLOCK command, allows an oper to chage a users nick and lock them to it until they quit */
21
22 /** Handle /NICKLOCK
23  */
24 class cmd_nicklock : public command_t
25 {
26         char* dummy;
27  public:
28         cmd_nicklock (InspIRCd* Instance) : command_t(Instance,"NICKLOCK", 'o', 2)
29         {
30                 this->source = "m_nicklock.so";
31                 syntax = "<oldnick> <newnick>";
32         }
33
34         CmdResult Handle(const char** parameters, int pcnt, userrec *user)
35         {
36                 userrec* source = ServerInstance->FindNick(parameters[0]);
37                 irc::string server;
38                 irc::string me;
39
40                 // check user exists
41                 if (!source)
42                 {
43                         return CMD_FAILURE;
44                 }
45
46                 // check if user is locked
47                 if (source->GetExt("nick_locked", dummy))
48                 {
49                         user->WriteServ("946 %s %s :This user's nickname is already locked.",user->nick,source->nick);
50                         return CMD_FAILURE;
51                 }
52
53                 // check nick is valid
54                 if (!ServerInstance->IsNick(parameters[1]))
55                 {
56                         return CMD_FAILURE;
57                 }
58
59                 // let others know
60                 ServerInstance->WriteOpers(std::string(user->nick)+" used NICKLOCK to change and hold "+parameters[0]+" to "+parameters[1]);
61
62                 if (!source->ForceNickChange(parameters[1]))
63                 {
64                         // ugh, nickchange failed for some reason -- possibly existing nick?
65                         userrec::QuitUser(ServerInstance, source, "Nickname collision");
66                 }
67
68                 // give them a lock flag
69                 source->Extend("nick_locked", "ON");
70
71                 /* route */
72                 return CMD_SUCCESS;
73         }
74 };
75
76 /** Handle /NICKUNLOCK
77  */
78 class cmd_nickunlock : public command_t
79 {
80  public:
81         cmd_nickunlock (InspIRCd* Instance) : command_t(Instance,"NICKUNLOCK", 'o', 1)
82         {
83                 this->source = "m_nicklock.so";
84                 syntax = "<locked-nick>";
85         }
86
87         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
88         {
89                 userrec* source = ServerInstance->FindNick(parameters[0]);
90                 if (source)
91                 {
92                         source->Shrink("nick_locked");
93                         user->WriteServ("945 %s %s :Nickname now unlocked.",user->nick,source->nick);
94                         ServerInstance->WriteOpers(std::string(user->nick)+" used NICKUNLOCK on "+parameters[0]);
95                         return CMD_SUCCESS;
96                 }
97
98                 return CMD_FAILURE;
99         }
100 };
101
102
103 class ModuleNickLock : public Module
104 {
105         cmd_nicklock*   cmd1;
106         cmd_nickunlock* cmd2;
107         char* n;
108  public:
109         ModuleNickLock(InspIRCd* Me)
110                 : Module(Me)
111         {
112                 
113                 cmd1 = new cmd_nicklock(ServerInstance);
114                 cmd2 = new cmd_nickunlock(ServerInstance);
115                 ServerInstance->AddCommand(cmd1);
116                 ServerInstance->AddCommand(cmd2);
117         }
118         
119         virtual ~ModuleNickLock()
120         {
121         }
122         
123         virtual Version GetVersion()
124         {
125                 return Version(1, 1, 0, 1, VF_COMMON | VF_VENDOR, API_VERSION);
126         }
127
128         void Implements(char* List)
129         {
130                 List[I_OnUserPreNick] = List[I_OnUserQuit] = List[I_OnCleanup] = 1;
131         }
132
133         virtual int OnUserPreNick(userrec* user, const std::string &newnick)
134         {
135                 if (user->GetExt("nick_locked", n))
136                 {
137                         user->WriteServ("447 %s :You cannot change your nickname (your nick is locked)",user->nick);
138                         return 1;
139                 }
140                 return 0;
141         }
142
143         virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
144         {
145                 user->Shrink("nick_locked");
146         }
147
148         virtual void OnCleanup(int target_type, void* item)
149         {
150                 if(target_type == TYPE_USER)
151                 {
152                         userrec* user = (userrec*)item;
153                         user->Shrink("nick_locked");
154                 }
155         }
156 };
157
158 MODULE_INIT(ModuleNickLock)