]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
26d5bfbd7f1c1c6f55d0f95c37de059bfe83fa88
[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                         return CMD_FAILURE;
67                 }
68
69                 // give them a lock flag
70                 source->Extend("nick_locked", "ON");
71
72                 /* route */
73                 return CMD_SUCCESS;
74         }
75 };
76
77 /** Handle /NICKUNLOCK
78  */
79 class cmd_nickunlock : public command_t
80 {
81  public:
82  cmd_nickunlock (InspIRCd* Instance) : command_t(Instance,"NICKUNLOCK", 'o', 1)
83         {
84                 this->source = "m_nickunlock.so";
85                 syntax = "<locked-nick>";
86         }
87
88         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
89         {
90                 userrec* source = ServerInstance->FindNick(parameters[0]);
91                 if (source)
92                 {
93                         source->Shrink("nick_locked");
94                         user->WriteServ("945 %s %s :Nickname now unlocked.",user->nick,source->nick);
95                         ServerInstance->WriteOpers(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         cmd_nicklock*   cmd1;
107         cmd_nickunlock* cmd2;
108         char* n;
109  public:
110         ModuleNickLock(InspIRCd* Me)
111                 : Module(Me)
112         {
113                 
114                 cmd1 = new cmd_nicklock(ServerInstance);
115                 cmd2 = new cmd_nickunlock(ServerInstance);
116                 ServerInstance->AddCommand(cmd1);
117                 ServerInstance->AddCommand(cmd2);
118         }
119         
120         virtual ~ModuleNickLock()
121         {
122         }
123         
124         virtual Version GetVersion()
125         {
126                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
127         }
128
129         void Implements(char* List)
130         {
131                 List[I_OnUserPreNick] = List[I_OnUserQuit] = List[I_OnCleanup] = 1;
132         }
133
134         virtual int OnUserPreNick(userrec* user, const std::string &newnick)
135         {
136                 if (user->GetExt("nick_locked", n))
137                 {
138                         user->WriteServ("447 %s :You cannot change your nickname (your nick is locked)",user->nick);
139                         return 1;
140                 }
141                 return 0;
142         }
143
144         virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
145         {
146                 user->Shrink("nick_locked");
147         }
148
149         virtual void OnCleanup(int target_type, void* item)
150         {
151                 if(target_type == TYPE_USER)
152                 {
153                         userrec* user = (userrec*)item;
154                         user->Shrink("nick_locked");
155                 }
156         }
157 };
158
159 MODULE_INIT(ModuleNickLock)