]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
Updates, should be able to safely unload client modules with queries in progress...
[user/henk/code/inspircd.git] / src / modules / m_nicklock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include <string>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "helperfuncs.h"
25 #include "hashcomp.h"
26
27 /* $ModDesc: Provides the NICKLOCK command, allows an oper to chage a users nick and lock them to it until they quit */
28
29 static Server *Srv;
30
31 class cmd_nicklock : public command_t
32 {
33         char* dummy;
34  public:
35         cmd_nicklock () : command_t("NICKLOCK", 'o', 2)
36         {
37                 this->source = "m_nicklock.so";
38         }
39
40         void Handle(const char** parameters, int pcnt, userrec *user)
41         {
42                 userrec* source = Srv->FindNick(std::string(parameters[0]));
43                 irc::string server;
44                 irc::string me;
45
46                 if (source)
47                 {
48                         if (source->GetExt("nick_locked", dummy))
49                         {
50                                 WriteServ(user->fd,"946 %s %s :This user's nickname is already locked.",user->nick,source->nick);
51                                 return;
52                         }
53                         if (Srv->IsNick(std::string(parameters[1])))
54                         {
55                                 // give them a lock flag
56                                 Srv->SendOpers(std::string(user->nick)+" used NICKLOCK to change and hold "+std::string(parameters[0])+" to "+parameters[1]);
57                                 Srv->ChangeUserNick(source,std::string(parameters[1]));
58                                 // only attempt to set their lockflag after we know the change succeeded
59                                 source = Srv->FindNick(std::string(parameters[1]));
60                                 if (source)
61                                         source->Extend("nick_locked", "ON");
62                         }
63                 }
64         }
65 };
66
67 class cmd_nickunlock : public command_t
68 {
69  public:
70         cmd_nickunlock () : command_t("NICKUNLOCK", 'o', 1)
71         {
72                 this->source = "m_nickunlock.so";
73         }
74
75         void Handle (const char** parameters, int pcnt, userrec *user)
76         {
77                 userrec* source = Srv->FindNick(std::string(parameters[0]));
78                 if (source)
79                 {
80                         source->Shrink("nick_locked");
81                         WriteServ(user->fd,"945 %s %s :Nickname now unlocked.",user->nick,source->nick);
82                         Srv->SendOpers(std::string(user->nick)+" used NICKUNLOCK on "+std::string(parameters[0]));
83                 }
84         }
85 };
86
87
88 class ModuleNickLock : public Module
89 {
90         cmd_nicklock*   cmd1;
91         cmd_nickunlock* cmd2;
92         char* n;
93  public:
94         ModuleNickLock(Server* Me)
95                 : Module::Module(Me)
96         {
97                 Srv = Me;
98                 cmd1 = new cmd_nicklock();
99                 cmd2 = new cmd_nickunlock();
100                 Srv->AddCommand(cmd1);
101                 Srv->AddCommand(cmd2);
102         }
103         
104         virtual ~ModuleNickLock()
105         {
106         }
107         
108         virtual Version GetVersion()
109         {
110                 return Version(1,0,0,1,VF_VENDOR);
111         }
112
113         void Implements(char* List)
114         {
115                 List[I_OnUserPreNick] = List[I_OnUserQuit] = List[I_OnCleanup] = 1;
116         }
117
118         virtual int OnUserPreNick(userrec* user, const std::string &newnick)
119         {
120                 if (user->GetExt("nick_locked", n))
121                 {
122                         WriteServ(user->fd,"447 %s :You cannot change your nickname (your nick is locked)",user->nick);
123                         return 1;
124                 }
125                 return 0;
126         }
127
128         virtual void OnUserQuit(userrec* user, const std::string &reason)
129         {
130                 user->Shrink("nick_locked");
131         }
132
133         virtual void OnCleanup(int target_type, void* item)
134         {
135                 if(target_type == TYPE_USER)
136                 {
137                         userrec* user = (userrec*)item;
138                         user->Shrink("nick_locked");
139                 }
140         }
141 };
142
143 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
144
145 class ModuleNickLockFactory : public ModuleFactory
146 {
147  public:
148         ModuleNickLockFactory()
149         {
150         }
151         
152         ~ModuleNickLockFactory()
153         {
154         }
155         
156         virtual Module * CreateModule(Server* Me)
157         {
158                 return new ModuleNickLock(Me);
159         }
160         
161 };
162
163
164 extern "C" void * init_module( void )
165 {
166         return new ModuleNickLockFactory;
167 }
168