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 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)
52 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
54 userrec* x = MyInstance->FindNick(parameter);
57 if (!channel->HasUser(x))
59 return std::make_pair(false, parameter);
63 std::string item = extend+std::string(channel->name);
64 if (x->GetExt(item,dummyptr))
66 return std::make_pair(true, x->nick);
70 return std::make_pair(false, parameter);
74 return std::make_pair(false, parameter);
77 void RemoveMode(chanrec* channel, char mc)
80 CUList* cl = channel->GetUsers();
81 std::string item = extend + std::string(channel->name);
82 const char* mode_junk[MAXMODES+1];
83 userrec* n = new userrec(MyInstance);
84 n->SetFd(FD_MAGIC_NUMBER);
85 mode_junk[0] = channel->name;
86 irc::modestacker modestack(false);
87 std::deque<std::string> stackresult;
88 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
90 if (i->second->GetExt(item, dummyptr))
92 modestack.Push(mc, i->second->nick);
96 while (modestack.GetStackedLine(stackresult))
98 for (size_t j = 0; j < stackresult.size(); j++)
100 mode_junk[j+1] = stackresult[j].c_str();
102 MyInstance->SendMode(mode_junk, stackresult.size() + 1, n);
106 unload_kludge = false;
109 void DisplayList(userrec* user, chanrec* channel)
111 CUList* cl = channel->GetUsers();
112 std::string item = extend+std::string(channel->name);
113 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
115 if (i->second->GetExt(item, dummyptr))
117 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
120 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
123 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
125 userrec* theuser = MyInstance->FindNick(parameter);
126 if ((!theuser) || (!channel->HasUser(theuser)))
134 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
136 std::string item = extend+std::string(channel->name);
140 if (!theuser->GetExt(item, dummyptr))
142 theuser->Extend(item, fakevalue);
143 parameter = theuser->nick;
144 return MODEACTION_ALLOW;
149 if (theuser->GetExt(item, dummyptr))
151 theuser->Shrink(item);
152 parameter = theuser->nick;
153 return MODEACTION_ALLOW;
156 return MODEACTION_DENY;
160 /** Abstraction of FounderProtectBase for channel mode +q
162 class ChanFounder : public ModeHandler, public FounderProtectBase
166 ChanFounder(InspIRCd* Instance, bool using_prefixes)
167 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
168 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387) { }
170 unsigned int GetPrefixRank()
172 return FOUNDER_VALUE;
175 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
177 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
180 void RemoveMode(chanrec* channel)
182 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
185 void RemoveMode(userrec* user)
189 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
191 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
195 return MODEACTION_DENY;
198 // source is a server, or ulined, we'll let them +-q the user.
199 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
201 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
205 // whoops, someones being naughty!
206 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
208 return MODEACTION_DENY;
212 void DisplayList(userrec* user, chanrec* channel)
214 FounderProtectBase::DisplayList(user,channel);
218 /** Abstraction of FounderProtectBase for channel mode +a
220 class ChanProtect : public ModeHandler, public FounderProtectBase
224 ChanProtect(InspIRCd* Instance, bool using_prefixes)
225 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
226 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389) { }
228 unsigned int GetPrefixRank()
230 return PROTECT_VALUE;
233 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
235 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
238 void RemoveMode(chanrec* channel)
240 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
243 void RemoveMode(userrec* user)
247 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
249 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
252 return MODEACTION_DENY;
254 std::string founder = "cm_founder_"+std::string(channel->name);
256 // source has +q, is a server, or ulined, we'll let them +-a the user.
257 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
259 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
263 // bzzzt, wrong answer!
264 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
265 return MODEACTION_DENY;
269 virtual void DisplayList(userrec* user, chanrec* channel)
271 FounderProtectBase::DisplayList(user, channel);
276 class ModuleChanProtect : public Module
279 bool FirstInGetsFounder;
287 ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
289 /* Load config stuff */
292 /* Initialise module variables */
294 cp = new ChanProtect(ServerInstance,QAPrefixes);
295 cf = new ChanFounder(ServerInstance,QAPrefixes);
297 ServerInstance->AddMode(cp, 'a');
298 ServerInstance->AddMode(cf, 'q');
301 void Implements(char* List)
303 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
306 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
308 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
309 user->Shrink("cm_founder_"+std::string(chan->name));
310 user->Shrink("cm_protect_"+std::string(chan->name));
313 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
315 // FIX: when someone parts a channel we must remove their Extensibles!
316 user->Shrink("cm_founder_"+std::string(channel->name));
317 user->Shrink("cm_protect_"+std::string(channel->name));
320 virtual void OnRehash(const std::string ¶meter)
322 /* Create a configreader class and read our flag,
323 * in old versions this was heap-allocated and the
324 * object was kept between rehashes...now we just
325 * stack-allocate it locally.
327 ConfigReader Conf(ServerInstance);
329 bool old_qa = QAPrefixes;
331 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
332 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
334 /* Did the user change the QA prefixes on the fly?
335 * If so, remove all instances of the mode, and reinit
336 * the module with prefixes enabled.
338 if (old_qa != QAPrefixes)
340 ServerInstance->Modes->DelMode(cp);
341 ServerInstance->Modes->DelMode(cf);
344 cp = new ChanProtect(ServerInstance,QAPrefixes);
345 cf = new ChanFounder(ServerInstance,QAPrefixes);
346 ServerInstance->AddMode(cp, 'a');
347 ServerInstance->AddMode(cf, 'q');
348 ServerInstance->WriteOpers("*** WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
352 virtual void OnUserJoin(userrec* user, chanrec* channel)
354 // if the user is the first user into the channel, mark them as the founder, but only if
355 // the config option for it is set
356 if (FirstInGetsFounder)
358 if (channel->GetUserCounter() == 1)
360 // we're using Extensible::Extend to add data into user objects.
361 // this way is best as it adds data thats accessible to other modules
362 // (so long as you document your code properly) without breaking anything
363 // because its encapsulated neatly in a map.
365 // Change requested by katsklaw... when the first in is set to get founder,
366 // to make it clearer that +q has been given, send that one user the +q notice
367 // so that their client's syncronization and their sanity are left intact.
368 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
369 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
371 ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
377 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
379 // here we perform access checks, this is the important bit that actually stops kicking/deopping
380 // etc of protected users. There are many types of access check, we're going to handle
381 // a relatively small number of them relevent to our module using a switch statement.
383 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
384 // don't allow action if:
385 // (A) Theyre founder (no matter what)
386 // (B) Theyre protected, and you're not
387 // always allow the action if:
388 // (A) The source is ulined
391 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
392 // without any access checks, we're not worthy :p
393 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
395 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns ALLOW");
399 std::string founder = "cm_founder_"+std::string(channel->name);
400 std::string protect = "cm_protect_"+std::string(channel->name);
404 // a user has been deopped. Do we let them? hmmm...
406 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
407 if (dest->GetExt(founder,dummyptr))
409 ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
410 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
415 ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
417 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
419 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
424 // a user is being kicked. do we chop off the end of the army boot?
426 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
427 if (dest->GetExt(founder,dummyptr))
429 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
432 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
434 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
439 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
441 if (dest->GetExt(founder,dummyptr))
443 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");
446 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
448 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
453 // same with devoice.
455 if (dest->GetExt(founder,dummyptr))
457 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
460 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
462 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
468 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
469 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns DEFAULT");
473 virtual ~ModuleChanProtect()
475 ServerInstance->Modes->DelMode(cp);
476 ServerInstance->Modes->DelMode(cf);
481 virtual Version GetVersion()
483 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
486 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
488 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
489 * so we dont need to propogate it manually
493 // this is called when the server is linking into a net and wants to sync channel data.
494 // we should send our mode changes for the channel here to ensure that other servers
495 // know whos +q/+a on the channel.
496 CUList* cl = chan->GetUsers();
497 string_list commands;
498 std::string founder = "cm_founder_"+std::string(chan->name);
499 std::string protect = "cm_protect_"+std::string(chan->name);
500 irc::modestacker modestack(true);
501 std::deque<std::string> stackresult;
502 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
504 if (i->second->GetExt(founder,dummyptr))
506 modestack.Push('q',i->second->nick);
508 if (i->second->GetExt(protect,dummyptr))
510 modestack.Push('a',i->second->nick);
513 while (modestack.GetStackedLine(stackresult))
515 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
516 std::string line = mode_join.GetJoined();
517 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
525 class ModuleChanProtectFactory : public ModuleFactory
528 ModuleChanProtectFactory()
532 ~ModuleChanProtectFactory()
536 virtual Module * CreateModule(InspIRCd* Me)
538 return new ModuleChanProtect(Me);
544 extern "C" void * init_module( void )
546 return new ModuleChanProtectFactory;