]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
93aae23a58512e78ed1669ec01c46d11a9586f93
[user/henk/code/inspircd.git] / src / modules / m_chanprotect.cpp
1 #include <stdio.h>
2
3 #include "users.h"
4 #include "channels.h"
5 #include "modules.h"
6
7 /* $ModDesc: Provides channel modes +a and +q */
8
9 char dummyvalue[] = "on";
10
11 class ModuleChanProtect : public Module
12 {
13         Server *Srv;
14         
15  public:
16  
17         ModuleChanProtect()
18         {
19                 Srv = new Server;
20                 // set up our modes. We're using listmodes and not normal extmodes here.
21                 // listmodes only need one parameter as everything else is assumed by the
22                 // nature of the mode thats being created.
23                 Srv->AddExtendedListMode('a');
24                 Srv->AddExtendedListMode('q');
25         }
26         
27         virtual void OnUserJoin(userrec* user, chanrec* channel)
28         {
29                 // if the user is the first user into the channel, mark them as the founder
30                 if (Srv->CountUsers(channel) == 1)
31                 {
32                         if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
33                         {
34                                 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
35                         }
36                 }
37         }
38         
39         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
40         {
41                 // don't allow action if:
42                 // (A) Theyre founder (no matter what)
43                 // (B) Theyre protected, and you're not
44                 switch (access_type)
45                 {
46                         case AC_DEOP:
47                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
48                                 {
49                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as the're a channel founder");
50                                         return ACR_DENY;
51                                 }
52                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
53                                 {
54                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as the're protected (+a)");
55                                         return ACR_DENY;
56                                 }
57                         break;
58
59                         case AC_KICK:
60                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
61                                 {
62                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as the're a channel founder");
63                                         return ACR_DENY;
64                                 }
65                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
66                                 {
67                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as the're protected (+a)");
68                                         return ACR_DENY;
69                                 }
70                         break;
71
72                         case AC_DEHALFOP:
73                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
74                                 {
75                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as the're a channel founder");
76                                         return ACR_DENY;
77                                 }
78                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
79                                 {
80                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as the're protected (+a)");
81                                         return ACR_DENY;
82                                 }
83                         break;
84
85                         case AC_DEVOICE:
86                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
87                                 {
88                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as the're a channel founder");
89                                         return ACR_DENY;
90                                 }
91                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
92                                 {
93                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as the're protected (+a)");
94                                         return ACR_DENY;
95                                 }
96                         break;
97                 }
98                 return ACR_DEFAULT;
99         }
100         
101         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
102         {
103                 // not out mode, bail
104                 if ((modechar == 'q') && (type == MT_CHANNEL))
105                 {
106                         // set up parameters
107                         chanrec* chan = (chanrec*)target;
108                         userrec* theuser = Srv->FindNick(params[0]);
109                 
110                         // cant find the user given as the parameter, eat the mode change.
111                         if (!theuser)
112                                 return -1;
113                         
114                         // given user isnt even on the channel, eat the mode change
115                         if (!Srv->IsOnChannel(theuser,chan))
116                                 return -1;
117                         
118                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
119                         {
120                                 if (mode_on)
121                                 {
122                                         if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
123                                         {
124                                                 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
125                                                 return 1;
126                                         }
127                                 }
128                                 else
129                                 {
130                                         if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
131                                         {
132                                                 theuser->Shrink("cm_founder_"+std::string(chan->name));
133                                                 return 1;
134                                         }
135                                 }       
136
137                                 return -1;
138                         }
139                         else
140                         {
141                                 WriteServ(user->fd,"482 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
142                                 return -1;
143                         }
144                 }
145                 if ((modechar == 'a') && (type == MT_CHANNEL))
146                 {
147                         // set up parameters
148                         chanrec* chan = (chanrec*)target;
149                         userrec* theuser = Srv->FindNick(params[0]);
150                 
151                         // cant find the user given as the parameter, eat the mode change.
152                         if (!theuser)
153                                 return -1;
154                         
155                         // given user isnt even on the channel, eat the mode change
156                         if (!Srv->IsOnChannel(theuser,chan))
157                                 return -1;
158                         
159                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
160                         {
161                                 if (mode_on)
162                                 {
163                                         if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
164                                         {
165                                                 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
166                                                 return 1;
167                                         }
168                                 }
169                                 else
170                                 {
171                                         if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
172                                         {
173                                                 theuser->Shrink("cm_protect_"+std::string(chan->name));
174                                                 return 1;
175                                         }
176                                 }       
177
178                                 return -1;
179                         }
180                         else
181                         {
182                                 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
183                                 return -1;
184                         }
185                 }
186                 return 0;
187         }
188         
189         virtual ~ModuleChanProtect()
190         {
191                 delete Srv;
192         }
193         
194         virtual Version GetVersion()
195         {
196                 return Version(1,0,0,0);
197         }
198         
199         virtual void OnUserConnect(userrec* user)
200         {
201         }
202
203 };
204
205
206 class ModuleChanProtectFactory : public ModuleFactory
207 {
208  public:
209         ModuleChanProtectFactory()
210         {
211         }
212         
213         ~ModuleChanProtectFactory()
214         {
215         }
216         
217         virtual Module * CreateModule()
218         {
219                 return new ModuleChanProtect;
220         }
221         
222 };
223
224
225 extern "C" void * init_module( void )
226 {
227         return new ModuleChanProtectFactory;
228 }
229