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