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