1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
19 /* $ModDesc: Provides channel modes +a and +q */
20 /* $ModDep: ../../include/u_listmode.h */
22 #define PROTECT_VALUE 40000
23 #define FOUNDER_VALUE 50000
25 const char* fakevalue = "on";
27 /* When this is set to true, no restrictions apply to setting or
28 * removal of +qa. This is used while unloading so that the server
29 * can freely clear all of its users of the modes.
31 bool unload_kludge = false;
33 /** Handles basic operation of +qa channel modes
35 class FounderProtectBase
45 bool& remove_own_privs;
46 bool& remove_other_privs;
48 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
49 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
53 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
55 userrec* x = MyInstance->FindNick(parameter);
58 if (!channel->HasUser(x))
60 return std::make_pair(false, parameter);
64 std::string item = extend+std::string(channel->name);
65 if (x->GetExt(item,dummyptr))
67 return std::make_pair(true, x->nick);
71 return std::make_pair(false, parameter);
75 return std::make_pair(false, parameter);
78 void RemoveMode(chanrec* channel, char mc)
81 CUList* cl = channel->GetUsers();
82 std::string item = extend + std::string(channel->name);
83 const char* mode_junk[MAXMODES+2];
84 userrec* n = new userrec(MyInstance);
85 n->SetFd(FD_MAGIC_NUMBER);
86 mode_junk[0] = channel->name;
87 irc::modestacker modestack(false);
88 std::deque<std::string> stackresult;
89 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
91 if (i->first->GetExt(item, dummyptr))
93 modestack.Push(mc, i->first->nick);
97 while (modestack.GetStackedLine(stackresult))
99 for (size_t j = 0; j < stackresult.size(); j++)
101 mode_junk[j+1] = stackresult[j].c_str();
103 MyInstance->SendMode(mode_junk, stackresult.size() + 1, n);
107 unload_kludge = false;
110 void DisplayList(userrec* user, chanrec* channel)
112 CUList* cl = channel->GetUsers();
113 std::string item = extend+std::string(channel->name);
114 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
116 if (i->first->GetExt(item, dummyptr))
118 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->first->nick);
121 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
124 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
126 userrec* theuser = MyInstance->FindNick(parameter);
127 if ((!theuser) || (!channel->HasUser(theuser)))
135 bool CanRemoveOthers(userrec* u1, userrec* u2, chanrec* c)
137 std::string item = extend+std::string(c->name);
138 return (u1->GetExt(item, dummyptr) && u2->GetExt(item, dummyptr));
141 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
143 std::string item = extend+std::string(channel->name);
147 if (!theuser->GetExt(item, dummyptr))
149 theuser->Extend(item, fakevalue);
150 parameter = theuser->nick;
151 return MODEACTION_ALLOW;
156 if (theuser->GetExt(item, dummyptr))
158 theuser->Shrink(item);
159 parameter = theuser->nick;
160 return MODEACTION_ALLOW;
163 return MODEACTION_DENY;
167 /** Abstraction of FounderProtectBase for channel mode +q
169 class ChanFounder : public ModeHandler, public FounderProtectBase
173 ChanFounder(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
174 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
175 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
177 unsigned int GetPrefixRank()
179 return FOUNDER_VALUE;
182 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
184 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
187 void RemoveMode(chanrec* channel)
189 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
192 void RemoveMode(userrec* user)
196 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
198 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
202 return MODEACTION_DENY;
205 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
207 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
209 // source is a server, or ulined, we'll let them +-q the user.
210 if ((unload_kludge) || ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
212 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
216 // whoops, someones being naughty!
217 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
219 return MODEACTION_DENY;
223 void DisplayList(userrec* user, chanrec* channel)
225 FounderProtectBase::DisplayList(user,channel);
229 /** Abstraction of FounderProtectBase for channel mode +a
231 class ChanProtect : public ModeHandler, public FounderProtectBase
235 ChanProtect(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
236 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
237 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
239 unsigned int GetPrefixRank()
241 return PROTECT_VALUE;
244 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
246 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
249 void RemoveMode(chanrec* channel)
251 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
254 void RemoveMode(userrec* user)
258 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
260 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
263 return MODEACTION_DENY;
265 std::string founder = "cm_founder_"+std::string(channel->name);
267 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
269 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
271 // source has +q, is a server, or ulined, we'll let them +-a the user.
272 if ((unload_kludge) || ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
274 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
278 // bzzzt, wrong answer!
279 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
280 return MODEACTION_DENY;
284 virtual void DisplayList(userrec* user, chanrec* channel)
286 FounderProtectBase::DisplayList(user, channel);
291 class ModuleChanProtect : public Module
294 bool FirstInGetsFounder;
305 ModuleChanProtect(InspIRCd* Me)
306 : Module(Me), FirstInGetsFounder(false), QAPrefixes(false), DeprivSelf(false), DeprivOthers(false), booting(true)
308 /* Load config stuff */
312 /* Initialise module variables */
314 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
315 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
317 if (!ServerInstance->AddMode(cp, 'a') || !ServerInstance->AddMode(cf, 'q'))
318 throw ModuleException("Could not add new modes!");
321 void Implements(char* List)
323 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
326 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
328 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
329 user->Shrink("cm_founder_"+std::string(chan->name));
330 user->Shrink("cm_protect_"+std::string(chan->name));
333 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason, bool &silent)
335 // FIX: when someone parts a channel we must remove their Extensibles!
336 user->Shrink("cm_founder_"+std::string(channel->name));
337 user->Shrink("cm_protect_"+std::string(channel->name));
340 virtual void OnRehash(userrec* user, const std::string ¶meter)
342 /* Create a configreader class and read our flag,
343 * in old versions this was heap-allocated and the
344 * object was kept between rehashes...now we just
345 * stack-allocate it locally.
347 ConfigReader Conf(ServerInstance);
349 bool old_qa = QAPrefixes;
351 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
352 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
353 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
354 DeprivOthers = Conf.ReadFlag("options","deprotectothers",0);
356 /* Did the user change the QA prefixes on the fly?
357 * If so, remove all instances of the mode, and reinit
358 * the module with prefixes enabled.
360 if ((old_qa != QAPrefixes) && (!booting))
362 ServerInstance->Modes->DelMode(cp);
363 ServerInstance->Modes->DelMode(cf);
366 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
367 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
368 /* These wont fail, we already owned the mode characters before */
369 ServerInstance->AddMode(cp, 'a');
370 ServerInstance->AddMode(cf, 'q');
371 ServerInstance->WriteOpers("*** WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
375 virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
377 // if the user is the first user into the channel, mark them as the founder, but only if
378 // the config option for it is set
379 if (FirstInGetsFounder)
381 if (channel->GetUserCounter() == 1)
383 // we're using Extensible::Extend to add data into user objects.
384 // this way is best as it adds data thats accessible to other modules
385 // (so long as you document your code properly) without breaking anything
386 // because its encapsulated neatly in a map.
388 // Change requested by katsklaw... when the first in is set to get founder,
389 // to make it clearer that +q has been given, send that one user the +q notice
390 // so that their client's syncronization and their sanity are left intact.
391 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
392 user->Extend("cm_founder_"+std::string(channel->name),fakevalue);
397 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
399 // here we perform access checks, this is the important bit that actually stops kicking/deopping
400 // etc of protected users. There are many types of access check, we're going to handle
401 // a relatively small number of them relevent to our module using a switch statement.
402 // don't allow action if:
403 // (A) Theyre founder (no matter what)
404 // (B) Theyre protected, and you're not
405 // always allow the action if:
406 // (A) The source is ulined
409 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
410 // without any access checks, we're not worthy :p
411 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
414 std::string founder = "cm_founder_"+std::string(channel->name);
415 std::string protect = "cm_protect_"+std::string(channel->name);
419 // a user has been deopped. Do we let them? hmmm...
421 if (dest->GetExt(founder,dummyptr))
423 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
426 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
428 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
433 // a user is being kicked. do we chop off the end of the army boot?
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.
480 virtual ~ModuleChanProtect()
482 ServerInstance->Modes->DelMode(cp);
483 ServerInstance->Modes->DelMode(cf);
488 virtual Version GetVersion()
490 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
493 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
495 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
496 * so we dont need to propogate it manually
500 // this is called when the server is linking into a net and wants to sync channel data.
501 // we should send our mode changes for the channel here to ensure that other servers
502 // know whos +q/+a on the channel.
503 CUList* cl = chan->GetUsers();
504 string_list commands;
505 std::string founder = "cm_founder_"+std::string(chan->name);
506 std::string protect = "cm_protect_"+std::string(chan->name);
507 irc::modestacker modestack(true);
508 std::deque<std::string> stackresult;
509 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
511 if (i->first->GetExt(founder,dummyptr))
513 modestack.Push('q',i->first->nick);
515 if (i->first->GetExt(protect,dummyptr))
517 modestack.Push('a',i->first->nick);
520 while (modestack.GetStackedLine(stackresult))
522 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
523 std::string line = mode_join.GetJoined();
524 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
531 MODULE_INIT(ModuleChanProtect)