]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
f79520df750db0d5b3057cc37cd3389f6094c8ed
[user/henk/code/inspircd.git] / src / modules / m_knock.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 "configreader.h"
26 #include "inspircd.h"
27
28 /* $ModDesc: Provides support for /KNOCK and mode +K */
29
30 extern InspIRCd* ServerInstance;
31
32 class cmd_knock : public command_t
33 {
34  public:
35         cmd_knock () : command_t("KNOCK", 0, 2)
36         {
37                 this->source = "m_knock.so";
38                 syntax = "<channel> <reason>";
39         }
40         
41         void Handle (const char** parameters, int pcnt, userrec *user)
42         {
43                 chanrec* c = ServerInstance->FindChan(parameters[0]);
44
45                 if (!c)
46                 {
47                         user->WriteServ("401 %s %s :No such channel",user->nick, parameters[0]);
48                         return;
49                 }
50
51                 std::string line = "";
52
53                 if (c->IsModeSet('K'))
54                 {
55                         user->WriteServ("480 %s :Can't KNOCK on %s, +K is set.",user->nick, c->name);
56                         return;
57                 }
58
59                 for (int i = 1; i < pcnt - 1; i++)
60                 {
61                         line = line + std::string(parameters[i]) + " ";
62                 }
63                 line = line + std::string(parameters[pcnt-1]);
64
65                 if (c->modes[CM_INVITEONLY])
66                 {
67                         c->WriteChannelWithServ((char*)ServerInstance->Config->ServerName,  "NOTICE %s :User %s is KNOCKing on %s (%s)", c->name, user->nick, c->name, line.c_str());
68                         user->WriteServ("NOTICE %s :KNOCKing on %s",user->nick,c->name);
69                         return;
70                 }
71                 else
72                 {
73                         user->WriteServ("480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
74                         return;
75                 }
76         }
77 };
78
79 class Knock : public ModeHandler
80 {
81  public:
82         Knock(InspIRCd* Instance) : ModeHandler(Instance, 'K', 0, 0, false, MODETYPE_CHANNEL, false) { }
83
84         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
85         {
86                 if (adding)
87                 {
88                         if (!channel->IsModeSet('K'))
89                         {
90                                 channel->SetMode('K',true);
91                                 return MODEACTION_ALLOW;
92                         }
93                 }
94                 else
95                 {
96                         if (channel->IsModeSet('K'))
97                         {
98                                 channel->SetMode('K',false);
99                                 return MODEACTION_ALLOW;
100                         }
101                 }
102
103                 return MODEACTION_DENY;
104         }
105 };
106
107 class ModuleKnock : public Module
108 {
109         cmd_knock* mycommand;
110         Knock* kn;
111  public:
112         ModuleKnock(InspIRCd* Me) : Module::Module(Me)
113         {
114                 
115                 kn = new Knock(ServerInstance);
116                 ServerInstance->AddMode(kn, 'K');
117                 mycommand = new cmd_knock();
118                 ServerInstance->AddCommand(mycommand);
119         }
120
121         void Implements(char* List)
122         {
123                 List[I_On005Numeric] = 1;
124         }
125
126         virtual void On005Numeric(std::string &output)
127         {
128                 ServerInstance->ModeGrok->InsertMode(output,"K",4);
129         }
130
131         virtual ~ModuleKnock()
132         {
133                 DELETE(kn);
134         }
135
136         virtual Version GetVersion()
137         {
138                 return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
139         }
140 };
141
142 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
143
144 class ModuleKnockFactory : public ModuleFactory
145 {
146  public:
147         ModuleKnockFactory()
148         {
149         }
150         
151         ~ModuleKnockFactory()
152         {
153         }
154         
155         virtual Module * CreateModule(InspIRCd* Me)
156         {
157                 return new ModuleKnock(Me);
158         }
159         
160 };
161
162
163 extern "C" void * init_module( void )
164 {
165         return new ModuleKnockFactory;
166 }
167