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