]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_knock.cpp
e1654c181f06febd1e9850eb947f7118002f1c8b
[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 #include <stdio.h>
18 #include <string>
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22
23 /* $ModDesc: Provides support for /KNOCK and mode +K */
24
25 Server *Srv;
26          
27 void handle_knock(char **parameters, int pcnt, userrec *user)
28 {
29         chanrec* c = Srv->FindChannel(parameters[0]);
30         std::string line = "";
31
32         for (int i = 1; i < pcnt - 1; i++)
33         {
34                 line = line + std::string(parameters[i]) + " ";
35         }
36         line = line + std::string(parameters[pcnt-1]);
37
38         if (c->IsCustomModeSet('K'))
39         {
40                 WriteServ(user->fd,"480 %s :Can't KNOCK on %s, +K is set.",user->nick, c->name);
41                 return;
42         }
43         if (c->binarymodes & CM_INVITEONLY)
44         {
45                 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());
46                 WriteServ(user->fd,"NOTICE %s :KNOCKing on %s",user->nick,c->name);
47                 return;
48         }
49         else
50         {
51                 WriteServ(user->fd,"480 %s :Can't KNOCK on %s, channel is not invite only so knocking is pointless!",user->nick, c->name);
52                 return;
53         }
54 }
55
56
57 class ModuleKnock : public Module
58 {
59  public:
60         ModuleKnock()
61         {
62                 Srv = new Server;
63                 
64                 Srv->AddExtendedMode('K',MT_CHANNEL,false,0,0);
65                 Srv->AddCommand("KNOCK",handle_knock,0,2,"m_knock.so");
66         }
67
68         virtual void On005Numeric(std::string &output)
69         {
70                 std::stringstream line(output);
71                 std::string temp1, temp2;
72                 while (!line.eof())
73                 {
74                         line >> temp1;
75                         if (temp1.substr(0,10) == "CHANMODES=")
76                         {
77                                 // append the chanmode to the end
78                                 temp1 = temp1.substr(10,temp1.length());
79                                 temp1 = "CHANMODES=" + temp1 + "K";
80                         }
81                         temp2 = temp2 + temp1 + " ";
82                 }
83                 if (temp2.length())
84                 {
85                         output = temp2.substr(0,temp2.length()-1) + std::string(" KNOCK");
86                 }
87                 else output = output + std::string(" KNOCK");
88         }
89
90         virtual ~ModuleKnock()
91         {
92                 delete Srv;
93         }
94         
95         virtual Version GetVersion()
96         {
97                 return Version(1,0,0,1,VF_STATIC|VF_VENDOR);
98         }
99         
100         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
101         {
102                 // check if this is our mode character...
103                 if ((modechar == 'K') && (type == MT_CHANNEL))
104                 {
105                         return 1;
106                 }
107                 else
108                 {
109                         return 0;
110                 }
111         }
112
113 };
114
115 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
116
117 class ModuleKnockFactory : public ModuleFactory
118 {
119  public:
120         ModuleKnockFactory()
121         {
122         }
123         
124         ~ModuleKnockFactory()
125         {
126         }
127         
128         virtual Module * CreateModule()
129         {
130                 return new ModuleKnock;
131         }
132         
133 };
134
135
136 extern "C" void * init_module( void )
137 {
138         return new ModuleKnockFactory;
139 }
140