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(User* source, User* dest, Channel* channel, const std::string ¶meter)
52 User* 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(Channel* 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 mode_junk[0] = channel->name;
82 irc::modestacker modestack(false);
83 std::deque<std::string> stackresult;
85 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
87 if (i->first->GetExt(item, dummyptr))
89 modestack.Push(mc, i->first->nick);
93 while (modestack.GetStackedLine(stackresult))
95 for (size_t j = 0; j < stackresult.size(); j++)
97 mode_junk[j+1] = stackresult[j].c_str();
99 MyInstance->SendMode(mode_junk, stackresult.size() + 1, MyInstance->FakeClient);
102 unload_kludge = false;
105 void DisplayList(User* user, Channel* channel)
107 CUList* cl = channel->GetUsers();
108 std::string item = extend+std::string(channel->name);
109 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
111 if (i->first->GetExt(item, dummyptr))
113 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->first->nick);
116 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
119 User* FindAndVerify(std::string ¶meter, Channel* channel)
121 User* theuser = MyInstance->FindNick(parameter);
122 if ((!theuser) || (!channel->HasUser(theuser)))
130 bool CanRemoveOthers(User* u1, User* u2, Channel* c)
132 std::string item = extend+std::string(c->name);
133 return (u1->GetExt(item, dummyptr) && u2->GetExt(item, dummyptr));
136 ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string ¶meter)
138 std::string item = extend+std::string(channel->name);
142 if (!theuser->GetExt(item, dummyptr))
144 theuser->Extend(item, fakevalue);
145 parameter = theuser->nick;
146 return MODEACTION_ALLOW;
151 if (theuser->GetExt(item, dummyptr))
153 theuser->Shrink(item);
154 parameter = theuser->nick;
155 return MODEACTION_ALLOW;
158 return MODEACTION_DENY;
162 /** Abstraction of FounderProtectBase for channel mode +q
164 class ChanFounder : public ModeHandler, public FounderProtectBase
168 ChanFounder(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
169 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
170 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
172 unsigned int GetPrefixRank()
174 return FOUNDER_VALUE;
177 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
179 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
182 void RemoveMode(Channel* channel)
184 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
187 void RemoveMode(User* user)
191 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
193 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
197 return MODEACTION_DENY;
200 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
202 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
204 // source is a server, or ulined, we'll let them +-q the user.
205 if ((unload_kludge) || ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
207 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
211 // whoops, someones being naughty!
212 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
214 return MODEACTION_DENY;
218 void DisplayList(User* user, Channel* channel)
220 FounderProtectBase::DisplayList(user,channel);
224 /** Abstraction of FounderProtectBase for channel mode +a
226 class ChanProtect : public ModeHandler, public FounderProtectBase
230 ChanProtect(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
231 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
232 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
234 unsigned int GetPrefixRank()
236 return PROTECT_VALUE;
239 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
241 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
244 void RemoveMode(Channel* channel)
246 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
249 void RemoveMode(User* user)
253 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
255 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
258 return MODEACTION_DENY;
260 std::string founder = "cm_founder_"+std::string(channel->name);
262 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
264 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
266 // source has +q, is a server, or ulined, we'll let them +-a the user.
267 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)))
269 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
273 // bzzzt, wrong answer!
274 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
275 return MODEACTION_DENY;
279 virtual void DisplayList(User* user, Channel* channel)
281 FounderProtectBase::DisplayList(user, channel);
286 class ModuleChanProtect : public Module
289 bool FirstInGetsFounder;
300 ModuleChanProtect(InspIRCd* Me)
301 : Module(Me), FirstInGetsFounder(false), QAPrefixes(false), DeprivSelf(false), DeprivOthers(false), booting(true)
303 /* Load config stuff */
307 /* Initialise module variables */
309 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
310 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
312 if (!ServerInstance->AddMode(cp, 'a') || !ServerInstance->AddMode(cf, 'q'))
313 throw ModuleException("Could not add new modes!");
316 void Implements(char* List)
318 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserPreJoin] = List[I_OnPostJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
321 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
323 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
324 user->Shrink("cm_founder_"+std::string(chan->name));
325 user->Shrink("cm_protect_"+std::string(chan->name));
328 virtual void OnUserPart(User* user, Channel* channel, const std::string &partreason, bool &silent)
330 // FIX: when someone parts a channel we must remove their Extensibles!
331 user->Shrink("cm_founder_"+std::string(channel->name));
332 user->Shrink("cm_protect_"+std::string(channel->name));
335 virtual void OnRehash(User* user, const std::string ¶meter)
337 /* Create a configreader class and read our flag,
338 * in old versions this was heap-allocated and the
339 * object was kept between rehashes...now we just
340 * stack-allocate it locally.
342 ConfigReader Conf(ServerInstance);
344 bool old_qa = QAPrefixes;
346 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
347 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
348 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
349 DeprivOthers = Conf.ReadFlag("options","deprotectothers",0);
351 /* Did the user change the QA prefixes on the fly?
352 * If so, remove all instances of the mode, and reinit
353 * the module with prefixes enabled.
355 if ((old_qa != QAPrefixes) && (!booting))
357 ServerInstance->Modes->DelMode(cp);
358 ServerInstance->Modes->DelMode(cf);
361 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
362 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
363 /* These wont fail, we already owned the mode characters before */
364 ServerInstance->AddMode(cp, 'a');
365 ServerInstance->AddMode(cf, 'q');
366 ServerInstance->WriteOpers("*** WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
370 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs)
372 // if the user is the first user into the channel, mark them as the founder, but only if
373 // the config option for it is set
375 if (FirstInGetsFounder && !chan)
381 virtual void OnPostJoin(User *user, Channel *channel)
383 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
384 // before the client is in the channel
386 // This notice was here originally because it was all done prior to the creation of
387 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
388 // not sure it really should be here - ops don't get shown, obviously, and the prefix
389 // will appear in the names list for the user.. remove if desired -Special
391 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
392 user->WriteServ("MODE %s +q %s", channel->name, user->nick);
395 virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
397 // here we perform access checks, this is the important bit that actually stops kicking/deopping
398 // etc of protected users. There are many types of access check, we're going to handle
399 // a relatively small number of them relevent to our module using a switch statement.
400 // don't allow action if:
401 // (A) Theyre founder (no matter what)
402 // (B) Theyre protected, and you're not
403 // always allow the action if:
404 // (A) The source is ulined
407 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
408 // without any access checks, we're not worthy :p
409 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
412 std::string founder = "cm_founder_"+std::string(channel->name);
413 std::string protect = "cm_protect_"+std::string(channel->name);
417 // a user has been deopped. Do we let them? hmmm...
419 if (dest->GetExt(founder,dummyptr))
421 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
424 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
426 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
431 // a user is being kicked. do we chop off the end of the army boot?
433 if (dest->GetExt(founder,dummyptr))
435 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
438 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
440 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
445 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
447 if (dest->GetExt(founder,dummyptr))
449 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");
452 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
454 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
459 // same with devoice.
461 if (dest->GetExt(founder,dummyptr))
463 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
466 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
468 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
474 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
478 virtual ~ModuleChanProtect()
480 ServerInstance->Modes->DelMode(cp);
481 ServerInstance->Modes->DelMode(cf);
486 virtual Version GetVersion()
488 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
491 virtual void OnSyncChannel(Channel* chan, Module* proto, void* opaque)
493 /* NOTE: If +qa prefix is on, this is propagated by the channel join,
494 * so we dont need to propagate it manually
498 // this is called when the server is linking into a net and wants to sync channel data.
499 // we should send our mode changes for the channel here to ensure that other servers
500 // know whos +q/+a on the channel.
501 CUList* cl = chan->GetUsers();
502 string_list commands;
503 std::string founder = "cm_founder_"+std::string(chan->name);
504 std::string protect = "cm_protect_"+std::string(chan->name);
505 irc::modestacker modestack(true);
506 std::deque<std::string> stackresult;
507 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
509 if (i->first->GetExt(founder,dummyptr))
511 modestack.Push('q',i->first->nick);
513 if (i->first->GetExt(protect,dummyptr))
515 modestack.Push('a',i->first->nick);
518 while (modestack.GetStackedLine(stackresult))
520 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
521 std::string line = mode_join.GetJoined();
522 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
529 MODULE_INIT(ModuleChanProtect)