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
32 ChanFounder(Server* s) : ModeHandler('q', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
34 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
36 userrec* x = Find(parameter);
39 if (!channel->HasUser(x))
41 return std::make_pair(false, parameter);
45 std::string founder = "cm_founder_"+std::string(channel->name);
46 if (x->GetExt(founder,dummyptr))
48 return std::make_pair(true, x->nick);
52 return std::make_pair(false, parameter);
56 return std::make_pair(false, parameter);
60 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
62 userrec* theuser = Srv->FindNick(parameter);
64 // cant find the user given as the parameter, eat the mode change.
68 return MODEACTION_DENY;
71 // given user isnt even on the channel, eat the mode change
72 if (!channel->HasUser(theuser))
75 return MODEACTION_DENY;
78 std::string protect = "cm_protect_"+std::string(channel->name);
79 std::string founder = "cm_founder_"+std::string(channel->name);
81 // source is a server, or ulined, we'll let them +-q the user.
82 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
86 if (!theuser->GetExt(founder,dummyptr))
88 theuser->Extend(founder,fakevalue);
89 // Tidy the nickname (make case match etc)
90 parameter = theuser->nick;
91 return MODEACTION_ALLOW;
96 if (theuser->GetExt(founder, dummyptr))
98 theuser->Shrink(founder);
99 // Tidy the nickname (make case match etc)
100 parameter = theuser->nick;
101 return MODEACTION_ALLOW;
104 return MODEACTION_DENY;
108 // whoops, someones being naughty!
109 WriteServ(source->fd,"468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
111 return MODEACTION_DENY;
115 void DisplayList(userrec* user, chanrec* channel)
117 chanuserlist cl = Srv->GetUsers(channel);
118 std::string founder = "cm_founder_"+std::string(channel->name);
119 for (unsigned int i = 0; i < cl.size(); i++)
121 if (cl[i]->GetExt(founder, dummyptr))
123 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
126 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
131 class ChanProtect : public ModeHandler
136 ChanProtect(Server* s) : ModeHandler('a', 1, 1, true, MODETYPE_CHANNEL, false), Srv(s) { }
138 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
140 userrec* x = Find(parameter);
143 if (!channel->HasUser(x))
145 return std::make_pair(false, parameter);
149 std::string founder = "cm_protect_"+std::string(channel->name);
150 if (x->GetExt(founder,dummyptr))
152 return std::make_pair(true, x->nick);
156 return std::make_pair(false, parameter);
160 return std::make_pair(false, parameter);
163 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
165 userrec* theuser = Srv->FindNick(parameter);
167 // cant find the user given as the parameter, eat the mode change.
171 return MODEACTION_DENY;
174 // given user isnt even on the channel, eat the mode change
175 if (!channel->HasUser(theuser))
178 return MODEACTION_DENY;
181 std::string protect = "cm_protect_"+std::string(channel->name);
182 std::string founder = "cm_founder_"+std::string(channel->name);
184 // source has +q, is a server, or ulined, we'll let them +-a the user.
185 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
189 if (!theuser->GetExt(protect,dummyptr))
191 theuser->Extend(protect,fakevalue);
192 // Tidy the nickname (make case match etc)
193 parameter = theuser->nick;
194 return MODEACTION_ALLOW;
199 if (theuser->GetExt(protect,dummyptr))
201 theuser->Shrink(protect);
202 // Tidy the nickname (make case match etc)
203 parameter = theuser->nick;
204 return MODEACTION_ALLOW;
207 return MODEACTION_DENY;
211 // bzzzt, wrong answer!
212 WriteServ(source->fd,"482 %s %s :You are not a channel founder",source->nick, channel->name);
213 return MODEACTION_DENY;
217 virtual void DisplayList(userrec* user, chanrec* channel)
219 chanuserlist cl = Srv->GetUsers(channel);
220 std::string protect = "cm_protect_"+std::string(channel->name);
221 for (unsigned int i = 0; i < cl.size(); i++)
223 if (cl[i]->GetExt(protect,dummyptr))
225 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
228 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
233 class ModuleChanProtect : public Module
236 bool FirstInGetsFounder;
243 ModuleChanProtect(Server* Me) : Module::Module(Me), Srv(Me)
245 /* Initialise module variables */
247 cp = new ChanProtect(Me);
248 cf = new ChanFounder(Me);
250 Srv->AddMode(cp, 'a');
251 Srv->AddMode(cf, 'q');
253 /* Load config stuff */
257 void Implements(char* List)
259 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
262 virtual void On005Numeric(std::string &output)
264 InsertMode(output,"qa",1);
267 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
269 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
270 user->Shrink("cm_founder_"+std::string(chan->name));
271 user->Shrink("cm_protect_"+std::string(chan->name));
274 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
276 // FIX: when someone parts a channel we must remove their Extensibles!
277 user->Shrink("cm_founder_"+std::string(channel->name));
278 user->Shrink("cm_protect_"+std::string(channel->name));
281 virtual void OnRehash(const std::string ¶meter)
283 /* Create a configreader class and read our flag,
284 * in old versions this was heap-allocated and the
285 * object was kept between rehashes...now we just
286 * stack-allocate it locally.
290 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
293 virtual void OnUserJoin(userrec* user, chanrec* channel)
295 // if the user is the first user into the channel, mark them as the founder, but only if
296 // the config option for it is set
297 if (FirstInGetsFounder)
299 if (Srv->CountUsers(channel) == 1)
301 // we're using Extensible::Extend to add data into user objects.
302 // this way is best as it adds data thats accessible to other modules
303 // (so long as you document your code properly) without breaking anything
304 // because its encapsulated neatly in a map.
306 // Change requested by katsklaw... when the first in is set to get founder,
307 // to make it clearer that +q has been given, send that one user the +q notice
308 // so that their client's syncronization and their sanity are left intact.
309 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
310 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
312 Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
318 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
320 // here we perform access checks, this is the important bit that actually stops kicking/deopping
321 // etc of protected users. There are many types of access check, we're going to handle
322 // a relatively small number of them relevent to our module using a switch statement.
324 // don't allow action if:
325 // (A) Theyre founder (no matter what)
326 // (B) Theyre protected, and you're not
327 // always allow the action if:
328 // (A) The source is ulined
331 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
332 // without any access checks, we're not worthy :p
333 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
338 std::string founder = "cm_founder_"+std::string(channel->name);
339 std::string protect = "cm_protect_"+std::string(channel->name);
343 // a user has been deopped. Do we let them? hmmm...
345 if (dest->GetExt(founder,dummyptr))
347 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");
350 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
352 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)");
357 // a user is being kicked. do we chop off the end of the army boot?
359 if (dest->GetExt(founder,dummyptr))
361 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");
364 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
366 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)");
371 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
373 if (dest->GetExt(founder,dummyptr))
375 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");
378 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
380 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)");
385 // same with devoice.
387 if (dest->GetExt(founder,dummyptr))
389 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");
392 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
394 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)");
400 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
404 virtual ~ModuleChanProtect()
410 virtual Version GetVersion()
412 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
415 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
417 // this is called when the server is linking into a net and wants to sync channel data.
418 // we should send our mode changes for the channel here to ensure that other servers
419 // know whos +q/+a on the channel.
420 chanuserlist cl = Srv->GetUsers(chan);
421 string_list commands;
422 std::string founder = "cm_founder_"+std::string(chan->name);
423 std::string protect = "cm_protect_"+std::string(chan->name);
424 for (unsigned int i = 0; i < cl.size(); i++)
426 if (cl[i]->GetExt(founder,dummyptr))
428 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
430 if (cl[i]->GetExt(protect,dummyptr))
432 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
440 class ModuleChanProtectFactory : public ModuleFactory
443 ModuleChanProtectFactory()
447 ~ModuleChanProtectFactory()
451 virtual Module * CreateModule(Server* Me)
453 return new ModuleChanProtect(Me);
459 extern "C" void * init_module( void )
461 return new ModuleChanProtectFactory;