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 * ---------------------------------------------------
16 /* $ModDesc: Provides channel modes +a and +q */
17 /* $ModDep: ../../include/u_listmode.h */
19 #define PROTECT_VALUE 40000
20 #define FOUNDER_VALUE 50000
22 const char* fakevalue = "on";
24 /* When this is set to true, no restrictions apply to setting or
25 * removal of +qa. This is used while unloading so that the server
26 * can freely clear all of its users of the modes.
28 bool unload_kludge = false;
30 /** Handles basic operation of +qa channel modes
32 class FounderProtectBase
42 bool& remove_own_privs;
43 bool& remove_other_privs;
45 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
46 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
50 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
52 userrec* x = MyInstance->FindNick(parameter);
55 if (!channel->HasUser(x))
57 return std::make_pair(false, parameter);
61 std::string item = extend+std::string(channel->name);
62 if (x->GetExt(item,dummyptr))
64 return std::make_pair(true, x->nick);
68 return std::make_pair(false, parameter);
72 return std::make_pair(false, parameter);
75 void RemoveMode(chanrec* channel, char mc)
78 CUList* cl = channel->GetUsers();
79 std::string item = extend + std::string(channel->name);
80 const char* mode_junk[MAXMODES+2];
81 userrec* n = new userrec(MyInstance);
82 n->SetFd(FD_MAGIC_NUMBER);
83 mode_junk[0] = channel->name;
84 irc::modestacker modestack(false);
85 std::deque<std::string> stackresult;
86 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
88 if (i->first->GetExt(item, dummyptr))
90 modestack.Push(mc, i->first->nick);
94 while (modestack.GetStackedLine(stackresult))
96 for (size_t j = 0; j < stackresult.size(); j++)
98 mode_junk[j+1] = stackresult[j].c_str();
100 MyInstance->SendMode(mode_junk, stackresult.size() + 1, n);
104 unload_kludge = false;
107 void DisplayList(userrec* user, chanrec* channel)
109 CUList* cl = channel->GetUsers();
110 std::string item = extend+std::string(channel->name);
111 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
113 if (i->first->GetExt(item, dummyptr))
115 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->first->nick);
118 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
121 userrec* FindAndVerify(std::string ¶meter, chanrec* channel)
123 userrec* theuser = MyInstance->FindNick(parameter);
124 if ((!theuser) || (!channel->HasUser(theuser)))
132 bool CanRemoveOthers(userrec* u1, userrec* u2, chanrec* c)
134 std::string item = extend+std::string(c->name);
135 return (u1->GetExt(item, dummyptr) && u2->GetExt(item, dummyptr));
138 ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string ¶meter)
140 std::string item = extend+std::string(channel->name);
144 if (!theuser->GetExt(item, dummyptr))
146 theuser->Extend(item, fakevalue);
147 parameter = theuser->nick;
148 return MODEACTION_ALLOW;
153 if (theuser->GetExt(item, dummyptr))
155 theuser->Shrink(item);
156 parameter = theuser->nick;
157 return MODEACTION_ALLOW;
160 return MODEACTION_DENY;
164 /** Abstraction of FounderProtectBase for channel mode +q
166 class ChanFounder : public ModeHandler, public FounderProtectBase
170 ChanFounder(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
171 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
172 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
174 unsigned int GetPrefixRank()
176 return FOUNDER_VALUE;
179 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
181 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
184 void RemoveMode(chanrec* channel)
186 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
189 void RemoveMode(userrec* user)
193 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
195 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
199 return MODEACTION_DENY;
202 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
204 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
206 // source is a server, or ulined, we'll let them +-q the user.
207 if ((unload_kludge) || ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
209 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
213 // whoops, someones being naughty!
214 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
216 return MODEACTION_DENY;
220 void DisplayList(userrec* user, chanrec* channel)
222 FounderProtectBase::DisplayList(user,channel);
226 /** Abstraction of FounderProtectBase for channel mode +a
228 class ChanProtect : public ModeHandler, public FounderProtectBase
232 ChanProtect(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
233 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
234 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
236 unsigned int GetPrefixRank()
238 return PROTECT_VALUE;
241 ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string ¶meter)
243 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
246 void RemoveMode(chanrec* channel)
248 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
251 void RemoveMode(userrec* user)
255 ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string ¶meter, bool adding)
257 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
260 return MODEACTION_DENY;
262 std::string founder = "cm_founder_"+std::string(channel->name);
264 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
266 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
268 // source has +q, is a server, or ulined, we'll let them +-a the user.
269 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)))
271 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
275 // bzzzt, wrong answer!
276 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
277 return MODEACTION_DENY;
281 virtual void DisplayList(userrec* user, chanrec* channel)
283 FounderProtectBase::DisplayList(user, channel);
288 class ModuleChanProtect : public Module
291 bool FirstInGetsFounder;
302 ModuleChanProtect(InspIRCd* Me)
303 : Module(Me), FirstInGetsFounder(false), QAPrefixes(false), DeprivSelf(false), DeprivOthers(false), booting(true)
305 /* Load config stuff */
309 /* Initialise module variables */
311 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
312 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
314 if (!ServerInstance->AddMode(cp, 'a') || !ServerInstance->AddMode(cf, 'q'))
315 throw ModuleException("Could not add new modes!");
318 void Implements(char* List)
320 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserPreJoin] = List[I_OnPostJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
323 virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
325 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
326 user->Shrink("cm_founder_"+std::string(chan->name));
327 user->Shrink("cm_protect_"+std::string(chan->name));
330 virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason, bool &silent)
332 // FIX: when someone parts a channel we must remove their Extensibles!
333 user->Shrink("cm_founder_"+std::string(channel->name));
334 user->Shrink("cm_protect_"+std::string(channel->name));
337 virtual void OnRehash(userrec* user, const std::string ¶meter)
339 /* Create a configreader class and read our flag,
340 * in old versions this was heap-allocated and the
341 * object was kept between rehashes...now we just
342 * stack-allocate it locally.
344 ConfigReader Conf(ServerInstance);
346 bool old_qa = QAPrefixes;
348 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
349 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
350 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
351 DeprivOthers = Conf.ReadFlag("options","deprotectothers",0);
353 /* Did the user change the QA prefixes on the fly?
354 * If so, remove all instances of the mode, and reinit
355 * the module with prefixes enabled.
357 if ((old_qa != QAPrefixes) && (!booting))
359 ServerInstance->Modes->DelMode(cp);
360 ServerInstance->Modes->DelMode(cf);
363 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
364 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
365 /* These wont fail, we already owned the mode characters before */
366 ServerInstance->AddMode(cp, 'a');
367 ServerInstance->AddMode(cf, 'q');
368 ServerInstance->WriteOpers("*** WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
372 virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname, std::string &privs)
374 // if the user is the first user into the channel, mark them as the founder, but only if
375 // the config option for it is set
377 if (FirstInGetsFounder && !chan)
383 virtual void OnPostJoin(userrec *user, chanrec *channel)
385 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
386 // before the client is in the channel
388 // This notice was here originally because it was all done prior to the creation of
389 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
390 // not sure it really should be here - ops don't get shown, obviously, and the prefix
391 // will appear in the names list for the user.. remove if desired -Special
393 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
394 user->WriteServ("MODE %s +q %s", channel->name, user->nick);
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)