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_OnUserPreJoin] = List[I_OnPostJoin] = 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 int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname, std::string &privs)
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
380 if (FirstInGetsFounder && !chan)
386 virtual void OnPostJoin(userrec *user, chanrec *channel)
388 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
389 // before the client is in the channel
391 // This notice was here originally because it was all done prior to the creation of
392 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
393 // not sure it really should be here - ops don't get shown, obviously, and the prefix
394 // will appear in the names list for the user.. remove if desired -Special
396 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
397 user->WriteServ("MODE %s +q %s", channel->name, user->nick);
400 virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
402 // here we perform access checks, this is the important bit that actually stops kicking/deopping
403 // etc of protected users. There are many types of access check, we're going to handle
404 // a relatively small number of them relevent to our module using a switch statement.
405 // don't allow action if:
406 // (A) Theyre founder (no matter what)
407 // (B) Theyre protected, and you're not
408 // always allow the action if:
409 // (A) The source is ulined
412 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
413 // without any access checks, we're not worthy :p
414 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
417 std::string founder = "cm_founder_"+std::string(channel->name);
418 std::string protect = "cm_protect_"+std::string(channel->name);
422 // a user has been deopped. Do we let them? hmmm...
424 if (dest->GetExt(founder,dummyptr))
426 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
429 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
431 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
436 // a user is being kicked. do we chop off the end of the army boot?
438 if (dest->GetExt(founder,dummyptr))
440 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
443 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
445 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
450 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
452 if (dest->GetExt(founder,dummyptr))
454 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");
457 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
459 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
464 // same with devoice.
466 if (dest->GetExt(founder,dummyptr))
468 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
471 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
473 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
479 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
483 virtual ~ModuleChanProtect()
485 ServerInstance->Modes->DelMode(cp);
486 ServerInstance->Modes->DelMode(cf);
491 virtual Version GetVersion()
493 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
496 virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
498 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
499 * so we dont need to propogate it manually
503 // this is called when the server is linking into a net and wants to sync channel data.
504 // we should send our mode changes for the channel here to ensure that other servers
505 // know whos +q/+a on the channel.
506 CUList* cl = chan->GetUsers();
507 string_list commands;
508 std::string founder = "cm_founder_"+std::string(chan->name);
509 std::string protect = "cm_protect_"+std::string(chan->name);
510 irc::modestacker modestack(true);
511 std::deque<std::string> stackresult;
512 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
514 if (i->first->GetExt(founder,dummyptr))
516 modestack.Push('q',i->first->nick);
518 if (i->first->GetExt(protect,dummyptr))
520 modestack.Push('a',i->first->nick);
523 while (modestack.GetStackedLine(stackresult))
525 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
526 std::string line = mode_join.GetJoined();
527 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
534 MODULE_INIT(ModuleChanProtect)