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 */
23 /* $ModDep: ../../include/u_listmode.h */
25 #define PROTECT_VALUE 40000
26 #define FOUNDER_VALUE 50000
28 const char* fakevalue = "on";
30 /* When this is set to true, no restrictions apply to setting or
31 * removal of +qa. This is used while unloading so that the server
32 * can freely clear all of its users of the modes.
34 bool unload_kludge = false;
36 /** Handles basic operation of +qa channel modes
38 class FounderProtectBase
48 bool remove_own_privs;
50 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool remove_own) :
51 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own)
55 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
57 userrec* x = MyInstance->FindNick(parameter);
60 if (!channel->HasUser(x))
62 return std::make_pair(false, parameter);
66 std::string item = extend+std::string(channel->name);
67 if (x->GetExt(item,dummyptr))
69 return std::make_pair(true, x->nick);
73 return std::make_pair(false, parameter);
77 return std::make_pair(false, parameter);
80 void RemoveMode(chanrec* channel, char mc)
83 CUList* cl = channel->GetUsers();
84 std::string item = extend + std::string(channel->name);
85 const char* mode_junk[MAXMODES+1];
86 userrec* n = new userrec(MyInstance);
87 n->SetFd(FD_MAGIC_NUMBER);
88 mode_junk[0] = channel->name;
89 irc::modestacker modestack(false);
90 std::deque<std::string> stackresult;
91 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
93 if (i->second->GetExt(item, dummyptr))
95 modestack.Push(mc, i->second->nick);
99 while (modestack.GetStackedLine(stackresult))
101 for (size_t j = 0; j < stackresult.size(); j++)
103 mode_junk[j+1] = stackresult[j].c_str();
105 MyInstance->SendMode(mode_junk, stackresult.size() + 1, n);
109 unload_kludge = false;
112 void DisplayList(userrec* user, chanrec* channel)
114 CUList* cl = channel->GetUsers();
115 std::string item = extend+std::string(channel->name);
116 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
118 if (i->second->GetExt(item, dummyptr))
120 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
123 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
126 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
128 userrec* theuser = MyInstance->FindNick(parameter);
129 if ((!theuser) || (!channel->HasUser(theuser)))
137 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
139 std::string item = extend+std::string(channel->name);
143 if (!theuser->GetExt(item, dummyptr))
145 theuser->Extend(item, fakevalue);
146 parameter = theuser->nick;
147 return MODEACTION_ALLOW;
152 if (theuser->GetExt(item, dummyptr))
154 theuser->Shrink(item);
155 parameter = theuser->nick;
156 return MODEACTION_ALLOW;
159 return MODEACTION_DENY;
163 /** Abstraction of FounderProtectBase for channel mode +q
165 class ChanFounder : public ModeHandler, public FounderProtectBase
169 ChanFounder(InspIRCd* Instance, bool using_prefixes, bool depriv_self)
170 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
171 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self) { }
173 unsigned int GetPrefixRank()
175 return FOUNDER_VALUE;
178 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
180 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
183 void RemoveMode(chanrec* channel)
185 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
188 void RemoveMode(userrec* user)
192 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
194 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
198 return MODEACTION_DENY;
201 // source is a server, or ulined, we'll let them +-q the user.
202 if ((unload_kludge) || ((source == theuser) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
204 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
208 // whoops, someones being naughty!
209 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
211 return MODEACTION_DENY;
215 void DisplayList(userrec* user, chanrec* channel)
217 FounderProtectBase::DisplayList(user,channel);
221 /** Abstraction of FounderProtectBase for channel mode +a
223 class ChanProtect : public ModeHandler, public FounderProtectBase
227 ChanProtect(InspIRCd* Instance, bool using_prefixes, bool depriv_self)
228 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
229 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self) { }
231 unsigned int GetPrefixRank()
233 return PROTECT_VALUE;
236 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
238 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
241 void RemoveMode(chanrec* channel)
243 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
246 void RemoveMode(userrec* user)
250 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
252 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
255 return MODEACTION_DENY;
257 std::string founder = "cm_founder_"+std::string(channel->name);
259 // source has +q, is a server, or ulined, we'll let them +-a the user.
260 if ((unload_kludge) || ((source == theuser) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
262 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
266 // bzzzt, wrong answer!
267 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
268 return MODEACTION_DENY;
272 virtual void DisplayList(userrec* user, chanrec* channel)
274 FounderProtectBase::DisplayList(user, channel);
279 class ModuleChanProtect : public Module
282 bool FirstInGetsFounder;
292 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
294 /* Load config stuff */
299 /* Initialise module variables */
301 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf);
302 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf);
304 ServerInstance->AddMode(cp, 'a');
305 ServerInstance->AddMode(cf, 'q');
308 void Implements(char* List)
310 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
313 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
315 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
316 user->Shrink("cm_founder_"+std::string(chan->name));
317 user->Shrink("cm_protect_"+std::string(chan->name));
320 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
322 // FIX: when someone parts a channel we must remove their Extensibles!
323 user->Shrink("cm_founder_"+std::string(channel->name));
324 user->Shrink("cm_protect_"+std::string(channel->name));
327 virtual void OnRehash(const std::string ¶meter)
329 /* Create a configreader class and read our flag,
330 * in old versions this was heap-allocated and the
331 * object was kept between rehashes...now we just
332 * stack-allocate it locally.
334 ConfigReader Conf(ServerInstance);
336 bool old_qa = QAPrefixes;
338 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
339 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
340 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
342 /* Did the user change the QA prefixes on the fly?
343 * If so, remove all instances of the mode, and reinit
344 * the module with prefixes enabled.
346 if ((old_qa != QAPrefixes) && (!booting))
348 ServerInstance->Modes->DelMode(cp);
349 ServerInstance->Modes->DelMode(cf);
352 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf);
353 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf);
354 ServerInstance->AddMode(cp, 'a');
355 ServerInstance->AddMode(cf, 'q');
356 ServerInstance->WriteOpers("*** WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
360 virtual void OnUserJoin(userrec* user, chanrec* channel)
362 // if the user is the first user into the channel, mark them as the founder, but only if
363 // the config option for it is set
364 if (FirstInGetsFounder)
366 if (channel->GetUserCounter() == 1)
368 // we're using Extensible::Extend to add data into user objects.
369 // this way is best as it adds data thats accessible to other modules
370 // (so long as you document your code properly) without breaking anything
371 // because its encapsulated neatly in a map.
373 // Change requested by katsklaw... when the first in is set to get founder,
374 // to make it clearer that +q has been given, send that one user the +q notice
375 // so that their client's syncronization and their sanity are left intact.
376 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
377 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
379 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
385 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
387 // here we perform access checks, this is the important bit that actually stops kicking/deopping
388 // etc of protected users. There are many types of access check, we're going to handle
389 // a relatively small number of them relevent to our module using a switch statement.
391 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
392 // don't allow action if:
393 // (A) Theyre founder (no matter what)
394 // (B) Theyre protected, and you're not
395 // always allow the action if:
396 // (A) The source is ulined
399 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
400 // without any access checks, we're not worthy :p
401 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
403 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns ALLOW");
407 std::string founder = "cm_founder_"+std::string(channel->name);
408 std::string protect = "cm_protect_"+std::string(channel->name);
412 // a user has been deopped. Do we let them? hmmm...
414 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
415 if (dest->GetExt(founder,dummyptr))
417 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
418 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
423 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
425 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
427 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
432 // a user is being kicked. do we chop off the end of the army boot?
434 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
435 if (dest->GetExt(founder,dummyptr))
437 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
440 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
442 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
447 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
449 if (dest->GetExt(founder,dummyptr))
451 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");
454 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
456 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
461 // same with devoice.
463 if (dest->GetExt(founder,dummyptr))
465 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
468 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
470 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
476 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
477 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns DEFAULT");
481 virtual ~ModuleChanProtect()
483 ServerInstance->Modes->DelMode(cp);
484 ServerInstance->Modes->DelMode(cf);
489 virtual Version GetVersion()
491 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
494 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
496 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
497 * so we dont need to propogate it manually
501 // this is called when the server is linking into a net and wants to sync channel data.
502 // we should send our mode changes for the channel here to ensure that other servers
503 // know whos +q/+a on the channel.
504 CUList* cl = chan->GetUsers();
505 string_list commands;
506 std::string founder = "cm_founder_"+std::string(chan->name);
507 std::string protect = "cm_protect_"+std::string(chan->name);
508 irc::modestacker modestack(true);
509 std::deque<std::string> stackresult;
510 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
512 if (i->second->GetExt(founder,dummyptr))
514 modestack.Push('q',i->second->nick);
516 if (i->second->GetExt(protect,dummyptr))
518 modestack.Push('a',i->second->nick);
521 while (modestack.GetStackedLine(stackresult))
523 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
524 std::string line = mode_join.GetJoined();
525 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
533 class ModuleChanProtectFactory : public ModuleFactory
536 ModuleChanProtectFactory()
540 ~ModuleChanProtectFactory()
544 virtual Module * CreateModule(InspIRCd* Me)
546 return new ModuleChanProtect(Me);
552 extern "C" void * init_module( void )
554 return new ModuleChanProtectFactory;