]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
New 'Implements' system
[user/henk/code/inspircd.git] / src / modules / m_knock.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 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 (char **parameters, int pcnt, userrec *user)
39         {
40                 chanrec* c = Srv->FindChannel(parameters[0]);
41                 std::string line = "";
42
43                 for (int i = 1; i < pcnt - 1; i++)
44                 {
45                         line = line + std::string(parameters[i]) + " ";
46                 }
47                 line = line + std::string(parameters[pcnt-1]);
48
49                 if (c->IsCustomModeSet('K'))
50                 {
51                         WriteServ(user->fd,"480 %s :Can't KNOCK on %s, +K is set.",user->nick, c->name);
52                         return;
53                 }
54                 if (c->binarymodes & CM_INVITEONLY)
55                 {
56                         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());
57                         WriteServ(user->fd,"NOTICE %s :KNOCKing on %s",user->nick,c->name);
58                         return;
59                 }
60                 else
61                 {
62                         WriteServ(user->fd,"480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
63                         return;
64                 }
65         }
66 };
67
68 class ModuleKnock : public Module
69 {
70         cmd_knock* mycommand;
71  public:
72         ModuleKnock(Server* Me)
73                 : Module::Module(Me)
74         {
75                 Srv = Me;
76                 Srv->AddExtendedMode('K',MT_CHANNEL,false,0,0);
77                 mycommand = new cmd_knock();
78                 Srv->AddCommand(mycommand);
79         }
80
81         virtual void On005Numeric(std::string &output)
82         {
83                 std::stringstream line(output);
84                 std::string temp1, temp2;
85                 while (!line.eof())
86                 {
87                         line >> temp1;
88                         if (temp1.substr(0,10) == "CHANMODES=")
89                         {
90                                 // append the chanmode to the end
91                                 temp1 = temp1.substr(10,temp1.length());
92                                 temp1 = "CHANMODES=" + temp1 + "K";
93                         }
94                         temp2 = temp2 + temp1 + " ";
95                 }
96                 if (temp2.length())
97                 {
98                         output = temp2.substr(0,temp2.length()-1) + std::string(" KNOCK");
99                 }
100                 else output = output + std::string(" KNOCK");
101         }
102
103         virtual ~ModuleKnock()
104         {
105         }
106         
107         virtual Version GetVersion()
108         {
109                 return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
110         }
111         
112         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
113         {
114                 // check if this is our mode character...
115                 if ((modechar == 'K') && (type == MT_CHANNEL))
116                 {
117                         return 1;
118                 }
119                 else
120                 {
121                         return 0;
122                 }
123         }
124
125 };
126
127 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
128
129 class ModuleKnockFactory : public ModuleFactory
130 {
131  public:
132         ModuleKnockFactory()
133         {
134         }
135         
136         ~ModuleKnockFactory()
137         {
138         }
139         
140         virtual Module * CreateModule(Server* Me)
141         {
142                 return new ModuleKnock(Me);
143         }
144         
145 };
146
147
148 extern "C" void * init_module( void )
149 {
150         return new ModuleKnockFactory;
151 }
152