]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_nicklock.cpp
Change to using userrec::ip as a sockaddr to store port, ip and address family, rathe...
[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                 syntax = "<oldnick> <newnick>";
39         }
40
41         void Handle(const char** parameters, int pcnt, userrec *user)
42         {
43                 userrec* source = Srv->FindNick(std::string(parameters[0]));
44                 irc::string server;
45                 irc::string me;
46
47                 if (source)
48                 {
49                         if (source->GetExt("nick_locked", dummy))
50                         {
51                                 WriteServ(user->fd,"946 %s %s :This user's nickname is already locked.",user->nick,source->nick);
52                                 return;
53                         }
54                         if (Srv->IsNick(std::string(parameters[1])))
55                         {
56                                 // give them a lock flag
57                                 Srv->SendOpers(std::string(user->nick)+" used NICKLOCK to change and hold "+std::string(parameters[0])+" to "+parameters[1]);
58                                 Srv->ChangeUserNick(source,std::string(parameters[1]));
59                                 // only attempt to set their lockflag after we know the change succeeded
60                                 source = Srv->FindNick(std::string(parameters[1]));
61                                 if (source)
62                                         source->Extend("nick_locked", "ON");
63                         }
64                 }
65         }
66 };
67
68 class cmd_nickunlock : public command_t
69 {
70  public:
71         cmd_nickunlock () : command_t("NICKUNLOCK", 'o', 1)
72         {
73                 this->source = "m_nickunlock.so";
74                 syntax = "<locked-nick>";
75         }
76
77         void Handle (const char** parameters, int pcnt, userrec *user)
78         {
79                 userrec* source = Srv->FindNick(std::string(parameters[0]));
80                 if (source)
81                 {
82                         source->Shrink("nick_locked");
83                         WriteServ(user->fd,"945 %s %s :Nickname now unlocked.",user->nick,source->nick);
84                         Srv->SendOpers(std::string(user->nick)+" used NICKUNLOCK on "+std::string(parameters[0]));
85                 }
86         }
87 };
88
89
90 class ModuleNickLock : public Module
91 {
92         cmd_nicklock*   cmd1;
93         cmd_nickunlock* cmd2;
94         char* n;
95  public:
96         ModuleNickLock(Server* Me)
97                 : Module::Module(Me)
98         {
99                 Srv = Me;
100                 cmd1 = new cmd_nicklock();
101                 cmd2 = new cmd_nickunlock();
102                 Srv->AddCommand(cmd1);
103                 Srv->AddCommand(cmd2);
104         }
105         
106         virtual ~ModuleNickLock()
107         {
108         }
109         
110         virtual Version GetVersion()
111         {
112                 return Version(1,0,0,1,VF_VENDOR);
113         }
114
115         void Implements(char* List)
116         {
117                 List[I_OnUserPreNick] = List[I_OnUserQuit] = List[I_OnCleanup] = 1;
118         }
119
120         virtual int OnUserPreNick(userrec* user, const std::string &newnick)
121         {
122                 if (user->GetExt("nick_locked", n))
123                 {
124                         WriteServ(user->fd,"447 %s :You cannot change your nickname (your nick is locked)",user->nick);
125                         return 1;
126                 }
127                 return 0;
128         }
129
130         virtual void OnUserQuit(userrec* user, const std::string &reason)
131         {
132                 user->Shrink("nick_locked");
133         }
134
135         virtual void OnCleanup(int target_type, void* item)
136         {
137                 if(target_type == TYPE_USER)
138                 {
139                         userrec* user = (userrec*)item;
140                         user->Shrink("nick_locked");
141                 }
142         }
143 };
144
145 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
146
147 class ModuleNickLockFactory : public ModuleFactory
148 {
149  public:
150         ModuleNickLockFactory()
151         {
152         }
153         
154         ~ModuleNickLockFactory()
155         {
156         }
157         
158         virtual Module * CreateModule(Server* Me)
159         {
160                 return new ModuleNickLock(Me);
161         }
162         
163 };
164
165
166 extern "C" void * init_module( void )
167 {
168         return new ModuleNickLockFactory;
169 }
170