1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
20 #include "helperfuncs.h"
22 /* $ModDesc: Provides channel modes +a and +q */
24 const char* fakevalue = "on";
26 class ChanFounder : public ModeHandler
30 ChanFounder(Server* s) : ModeHandler('q', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
32 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
34 userrec* theuser = Srv->FindNick(parameter);
36 // cant find the user given as the parameter, eat the mode change.
40 return MODEACTION_DENY;
43 // given user isnt even on the channel, eat the mode change
44 if (!channel->HasUser(theuser))
47 return MODEACTION_DENY;
50 // source is a server, or ulined, we'll let them +-q the user.
51 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server))
55 if (!theuser->GetExt("cm_founder_"+std::string(channel->name)))
57 theuser->Extend("cm_founder_"+std::string(channel->name),fakevalue);
58 // Tidy the nickname (make case match etc)
59 parameter = theuser->nick;
60 return MODEACTION_ALLOW;
65 if (theuser->GetExt("cm_founder_"+std::string(channel->name)))
67 theuser->Shrink("cm_founder_"+std::string(channel->name));
68 // Tidy the nickname (make case match etc)
69 parameter = theuser->nick;
70 return MODEACTION_ALLOW;
73 return MODEACTION_DENY;
77 // whoops, someones being naughty!
78 WriteServ(source->fd,"468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
80 return MODEACTION_DENY;
84 void DisplayList(userrec* user, chanrec* channel)
86 chanuserlist cl = Srv->GetUsers(channel);
87 for (unsigned int i = 0; i < cl.size(); i++)
89 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
91 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
94 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
99 class ChanProtect : public ModeHandler
102 ChanProtect() : ModeHandler('a', 1, 1, true, MODETYPE_CHANNEL, false) { }
104 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
106 userrec* theuser = Srv->FindNick(parameter);
108 // cant find the user given as the parameter, eat the mode change.
112 return MODEACTION_DENY;
115 // given user isnt even on the channel, eat the mode change
116 if (!chan->HasUser(theuser))
119 return MODEACTION_DENY;
122 // source has +q, is a server, or ulined, we'll let them +-a the user.
123 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt("cm_founder_"+std::string(channel->name))))
127 if (!theuser->GetExt("cm_protect_"+std::string(channel->name)))
129 theuser->Extend("cm_protect_"+std::string(channel->name),fakevalue);
130 // Tidy the nickname (make case match etc)
131 parameter = theuser->nick;
132 return MODEACTION_ALLOW;
137 if (theuser->GetExt("cm_protect_"+std::string(channel->name)))
139 theuser->Shrink("cm_protect_"+std::string(channel->name));
140 // Tidy the nickname (make case match etc)
141 parameter = theuser->nick;
142 return MODEACTION_ALLOW;
145 return MODEACTION_DENY;
149 // bzzzt, wrong answer!
150 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
151 return MODEACTION_DENY;
155 virtual void DisplayList(userrec* user, chanrec* channel)
157 chanuserlist cl = Srv->GetUsers(channel);
158 for (unsigned int i = 0; i < cl.size(); i++)
160 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
162 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
165 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
170 class ModuleChanProtect : public Module
173 bool FirstInGetsFounder;
180 ModuleChanProtect(Server* Me) : Module::Module(Me), Srv(Me)
182 /* Initialise module variables */
183 Conf = new ConfigReader;
185 cp = new ChanProtect(Me);
186 cf = new ChanFounder(Me);
188 Srv->AddMode(cp, 'a');
189 Srv->AdDMode(cf, 'q');
191 // read our config options (main config file)
192 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
195 void Implements(char* List)
197 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
200 virtual void On005Numeric(std::string &output)
202 InsertMode(output,"qa",1);
205 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
207 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
208 user->Shrink("cm_founder_"+std::string(chan->name));
209 user->Shrink("cm_protect_"+std::string(chan->name));
212 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
214 // FIX: when someone parts a channel we must remove their Extensibles!
215 user->Shrink("cm_founder_"+std::string(channel->name));
216 user->Shrink("cm_protect_"+std::string(channel->name));
219 virtual void OnRehash(const std::string ¶meter)
221 // on a rehash we delete our classes for good measure and create them again.
223 Conf = new ConfigReader;
224 // re-read our config options on a rehash
225 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
228 virtual void OnUserJoin(userrec* user, chanrec* channel)
230 // if the user is the first user into the channel, mark them as the founder, but only if
231 // the config option for it is set
232 if (FirstInGetsFounder)
234 if (Srv->CountUsers(channel) == 1)
236 // we're using Extensible::Extend to add data into user objects.
237 // this way is best as it adds data thats accessible to other modules
238 // (so long as you document your code properly) without breaking anything
239 // because its encapsulated neatly in a map.
241 // Change requested by katsklaw... when the first in is set to get founder,
242 // to make it clearer that +q has been given, send that one user the +q notice
243 // so that their client's syncronization and their sanity are left intact.
244 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
245 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
247 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
253 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
255 // here we perform access checks, this is the important bit that actually stops kicking/deopping
256 // etc of protected users. There are many types of access check, we're going to handle
257 // a relatively small number of them relevent to our module using a switch statement.
259 // don't allow action if:
260 // (A) Theyre founder (no matter what)
261 // (B) Theyre protected, and you're not
262 // always allow the action if:
263 // (A) The source is ulined
266 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
267 // without any access checks, we're not worthy :p
268 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
275 // a user has been deopped. Do we let them? hmmm...
277 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
279 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
282 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
284 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
289 // a user is being kicked. do we chop off the end of the army boot?
291 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
293 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
296 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
298 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
303 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
305 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
307 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
310 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
312 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
317 // same with devoice.
319 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
321 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
324 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
326 Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
332 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
336 virtual ~ModuleChanProtect()
343 virtual Version GetVersion()
345 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
348 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
350 // this is called when the server is linking into a net and wants to sync channel data.
351 // we should send our mode changes for the channel here to ensure that other servers
352 // know whos +q/+a on the channel.
353 chanuserlist cl = Srv->GetUsers(chan);
354 string_list commands;
355 for (unsigned int i = 0; i < cl.size(); i++)
357 if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
359 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
361 if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
363 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
371 class ModuleChanProtectFactory : public ModuleFactory
374 ModuleChanProtectFactory()
378 ~ModuleChanProtectFactory()
382 virtual Module * CreateModule(Server* Me)
384 return new ModuleChanProtect(Me);
390 extern "C" void * init_module( void )
392 return new ModuleChanProtectFactory;