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 * ---------------------------------------------------
22 /* $ModDesc: Provides channel modes +a and +q */
24 #define PROTECT_VALUE 40000
25 #define FOUNDER_VALUE 50000
27 const char* fakevalue = "on";
29 /* When this is set to true, no restrictions apply to setting or
30 * removal of +qa. This is used while unloading so that the server
31 * can freely clear all of its users of the modes.
33 bool unload_kludge = false;
35 class FounderProtectBase
45 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e) : MyInstance(Instance), extend(ext), type(mtype), list(l), end(e)
49 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
51 userrec* x = MyInstance->FindNick(parameter);
54 if (!channel->HasUser(x))
56 return std::make_pair(false, parameter);
60 std::string item = extend+std::string(channel->name);
61 if (x->GetExt(item,dummyptr))
63 return std::make_pair(true, x->nick);
67 return std::make_pair(false, parameter);
71 return std::make_pair(false, parameter);
74 void RemoveMode(chanrec* channel, char mc)
77 CUList* cl = channel->GetUsers();
78 std::string item = extend+std::string(channel->name);
79 char moderemove[MAXBUF];
80 userrec* n = new userrec(MyInstance);
81 n->SetFd(FD_MAGIC_NUMBER);
82 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
84 if (i->second->GetExt(item, dummyptr))
86 sprintf(moderemove,"-%c",mc);
87 const char* parameters[] = { channel->name, moderemove, i->second->nick };
88 MyInstance->SendMode(parameters, 3, n);
92 unload_kludge = false;
95 void DisplayList(userrec* user, chanrec* channel)
97 CUList* cl = channel->GetUsers();
98 std::string item = extend+std::string(channel->name);
99 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
101 if (i->second->GetExt(item, dummyptr))
103 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
106 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
109 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
111 userrec* theuser = MyInstance->FindNick(parameter);
112 if ((!theuser) || (!channel->HasUser(theuser)))
120 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
122 std::string item = extend+std::string(channel->name);
126 if (!theuser->GetExt(item, dummyptr))
128 theuser->Extend(item, fakevalue);
129 parameter = theuser->nick;
130 return MODEACTION_ALLOW;
135 if (theuser->GetExt(item, dummyptr))
137 theuser->Shrink(item);
138 parameter = theuser->nick;
139 return MODEACTION_ALLOW;
142 return MODEACTION_DENY;
146 class ChanFounder : public ModeHandler, public FounderProtectBase
150 ChanFounder(InspIRCd* Instance, bool using_prefixes)
151 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
152 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387) { }
154 unsigned int GetPrefixRank()
156 return FOUNDER_VALUE;
159 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
161 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
164 void RemoveMode(chanrec* channel)
166 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
169 void RemoveMode(userrec* user)
173 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
175 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
179 return MODEACTION_DENY;
182 // source is a server, or ulined, we'll let them +-q the user.
183 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
185 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
189 // whoops, someones being naughty!
190 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
192 return MODEACTION_DENY;
196 void DisplayList(userrec* user, chanrec* channel)
198 FounderProtectBase::DisplayList(user,channel);
202 class ChanProtect : public ModeHandler, public FounderProtectBase
206 ChanProtect(InspIRCd* Instance, bool using_prefixes)
207 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
208 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389) { }
210 unsigned int GetPrefixRank()
212 return PROTECT_VALUE;
215 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
217 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
220 void RemoveMode(chanrec* channel)
222 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
225 void RemoveMode(userrec* user)
229 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
231 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
234 return MODEACTION_DENY;
236 std::string founder = "cm_founder_"+std::string(channel->name);
238 // source has +q, is a server, or ulined, we'll let them +-a the user.
239 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
241 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
245 // bzzzt, wrong answer!
246 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
247 return MODEACTION_DENY;
251 virtual void DisplayList(userrec* user, chanrec* channel)
253 FounderProtectBase::DisplayList(user, channel);
258 class ModuleChanProtect : public Module
261 bool FirstInGetsFounder;
269 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
271 /* Load config stuff */
274 /* Initialise module variables */
276 cp = new ChanProtect(ServerInstance,QAPrefixes);
277 cf = new ChanFounder(ServerInstance,QAPrefixes);
279 ServerInstance->AddMode(cp, 'a');
280 ServerInstance->AddMode(cf, 'q');
283 void Implements(char* List)
285 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
288 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
290 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
291 user->Shrink("cm_founder_"+std::string(chan->name));
292 user->Shrink("cm_protect_"+std::string(chan->name));
295 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
297 // FIX: when someone parts a channel we must remove their Extensibles!
298 user->Shrink("cm_founder_"+std::string(channel->name));
299 user->Shrink("cm_protect_"+std::string(channel->name));
302 virtual void OnRehash(const std::string ¶meter)
304 /* Create a configreader class and read our flag,
305 * in old versions this was heap-allocated and the
306 * object was kept between rehashes...now we just
307 * stack-allocate it locally.
309 ConfigReader Conf(ServerInstance);
311 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
312 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
315 virtual void OnUserJoin(userrec* user, chanrec* channel)
317 // if the user is the first user into the channel, mark them as the founder, but only if
318 // the config option for it is set
319 if (FirstInGetsFounder)
321 if (channel->GetUserCounter() == 1)
323 // we're using Extensible::Extend to add data into user objects.
324 // this way is best as it adds data thats accessible to other modules
325 // (so long as you document your code properly) without breaking anything
326 // because its encapsulated neatly in a map.
328 // Change requested by katsklaw... when the first in is set to get founder,
329 // to make it clearer that +q has been given, send that one user the +q notice
330 // so that their client's syncronization and their sanity are left intact.
331 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
332 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
334 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
340 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
342 // here we perform access checks, this is the important bit that actually stops kicking/deopping
343 // etc of protected users. There are many types of access check, we're going to handle
344 // a relatively small number of them relevent to our module using a switch statement.
346 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
347 // don't allow action if:
348 // (A) Theyre founder (no matter what)
349 // (B) Theyre protected, and you're not
350 // always allow the action if:
351 // (A) The source is ulined
354 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
355 // without any access checks, we're not worthy :p
356 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
361 std::string founder = "cm_founder_"+std::string(channel->name);
362 std::string protect = "cm_protect_"+std::string(channel->name);
366 // a user has been deopped. Do we let them? hmmm...
368 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
369 if (dest->GetExt(founder,dummyptr))
371 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
372 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
377 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
379 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
381 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
386 // a user is being kicked. do we chop off the end of the army boot?
388 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
389 if (dest->GetExt(founder,dummyptr))
391 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
394 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
396 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
401 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
403 if (dest->GetExt(founder,dummyptr))
405 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
408 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
410 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
415 // same with devoice.
417 if (dest->GetExt(founder,dummyptr))
419 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
422 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
424 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
430 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
434 virtual ~ModuleChanProtect()
436 ServerInstance->Modes->DelMode(cp);
437 ServerInstance->Modes->DelMode(cf);
442 virtual Version GetVersion()
444 return Version(1, 0, 0, 0, VF_COMMON | VF_VENDOR);
447 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
449 // this is called when the server is linking into a net and wants to sync channel data.
450 // we should send our mode changes for the channel here to ensure that other servers
451 // know whos +q/+a on the channel.
452 CUList* cl = chan->GetUsers();
453 string_list commands;
454 std::string founder = "cm_founder_"+std::string(chan->name);
455 std::string protect = "cm_protect_"+std::string(chan->name);
456 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
458 if (i->second->GetExt(founder,dummyptr))
460 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(i->second->nick));
462 if (i->second->GetExt(protect,dummyptr))
464 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(i->second->nick));
472 class ModuleChanProtectFactory : public ModuleFactory
475 ModuleChanProtectFactory()
479 ~ModuleChanProtectFactory()
483 virtual Module * CreateModule(InspIRCd* Me)
485 return new ModuleChanProtect(Me);
491 extern "C" void * init_module( void )
493 return new ModuleChanProtectFactory;