1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2008 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 */
18 #define PROTECT_VALUE 40000
19 #define FOUNDER_VALUE 50000
21 const char* fakevalue = "on";
23 /* When this is set to true, no restrictions apply to setting or
24 * removal of +qa. This is used while unloading so that the server
25 * can freely clear all of its users of the modes.
27 bool unload_kludge = false;
29 /** Handles basic operation of +qa channel modes
31 class FounderProtectBase
41 bool& remove_own_privs;
42 bool& remove_other_privs;
44 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
45 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
49 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
51 User* x = MyInstance->FindNick(parameter);
54 if (!channel->HasUser(x))
56 return std::make_pair(false, parameter);
60 std::string item = extend+std::string(channel->name);
61 if (x->GetExt(item,dummyptr))
63 return std::make_pair(true, x->nick);
67 return std::make_pair(false, parameter);
71 return std::make_pair(false, parameter);
74 void RemoveMode(Channel* channel, char mc)
77 CUList* cl = channel->GetUsers();
78 std::string item = extend + std::string(channel->name);
79 const char* mode_junk[MAXMODES+2];
80 mode_junk[0] = channel->name;
81 irc::modestacker modestack(false);
82 std::deque<std::string> stackresult;
84 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
86 if (i->first->GetExt(item, dummyptr))
88 modestack.Push(mc, i->first->nick);
92 while (modestack.GetStackedLine(stackresult))
94 for (size_t j = 0; j < stackresult.size(); j++)
96 mode_junk[j+1] = stackresult[j].c_str();
98 MyInstance->SendMode(mode_junk, stackresult.size() + 1, MyInstance->FakeClient);
101 unload_kludge = false;
104 void DisplayList(User* user, Channel* channel)
106 CUList* cl = channel->GetUsers();
107 std::string item = extend+std::string(channel->name);
108 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
110 if (i->first->GetExt(item, dummyptr))
112 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->first->nick);
115 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
118 User* FindAndVerify(std::string ¶meter, Channel* channel)
120 User* theuser = MyInstance->FindNick(parameter);
121 if ((!theuser) || (!channel->HasUser(theuser)))
129 bool CanRemoveOthers(User* u1, User* u2, Channel* c)
131 std::string item = extend+std::string(c->name);
132 return (u1->GetExt(item, dummyptr) && u2->GetExt(item, dummyptr));
135 ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string ¶meter)
137 std::string item = extend+std::string(channel->name);
141 if (!theuser->GetExt(item, dummyptr))
143 theuser->Extend(item, fakevalue);
144 parameter = theuser->nick;
145 return MODEACTION_ALLOW;
150 if (theuser->GetExt(item, dummyptr))
152 theuser->Shrink(item);
153 parameter = theuser->nick;
154 return MODEACTION_ALLOW;
157 return MODEACTION_DENY;
161 /** Abstraction of FounderProtectBase for channel mode +q
163 class ChanFounder : public ModeHandler, public FounderProtectBase
167 ChanFounder(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
168 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
169 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
171 unsigned int GetPrefixRank()
173 return FOUNDER_VALUE;
176 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
178 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
181 void RemoveMode(Channel* channel)
183 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
186 void RemoveMode(User* user)
190 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
192 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
196 return MODEACTION_DENY;
199 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
201 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
203 // source is a server, or ulined, we'll let them +-q the user.
204 if ((unload_kludge) || ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
206 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
210 // whoops, someones being naughty!
211 source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
213 return MODEACTION_DENY;
217 void DisplayList(User* user, Channel* channel)
219 FounderProtectBase::DisplayList(user,channel);
223 /** Abstraction of FounderProtectBase for channel mode +a
225 class ChanProtect : public ModeHandler, public FounderProtectBase
229 ChanProtect(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
230 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
231 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
233 unsigned int GetPrefixRank()
235 return PROTECT_VALUE;
238 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
240 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
243 void RemoveMode(Channel* channel)
245 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
248 void RemoveMode(User* user)
252 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding)
254 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
257 return MODEACTION_DENY;
259 std::string founder = "cm_founder_"+std::string(channel->name);
261 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
263 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
265 // source has +q, is a server, or ulined, we'll let them +-a the user.
266 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)))
268 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
272 // bzzzt, wrong answer!
273 source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
274 return MODEACTION_DENY;
278 virtual void DisplayList(User* user, Channel* channel)
280 FounderProtectBase::DisplayList(user, channel);
285 class ModuleChanProtect : public Module
288 bool FirstInGetsFounder;
299 ModuleChanProtect(InspIRCd* Me)
300 : Module(Me), FirstInGetsFounder(false), QAPrefixes(false), DeprivSelf(false), DeprivOthers(false), booting(true)
302 /* Load config stuff */
306 /* Initialise module variables */
308 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
309 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
311 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
315 throw ModuleException("Could not add new modes!");
318 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnRehash, I_OnUserPreJoin, I_OnPostJoin, I_OnAccessCheck, I_OnSyncChannel };
319 ServerInstance->Modules->Attach(eventlist, this, 7);
322 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
324 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
325 user->Shrink("cm_founder_"+std::string(chan->name));
326 user->Shrink("cm_protect_"+std::string(chan->name));
329 virtual void OnUserPart(User* user, Channel* channel, const std::string &partreason, bool &silent)
331 // FIX: when someone parts a channel we must remove their Extensibles!
332 user->Shrink("cm_founder_"+std::string(channel->name));
333 user->Shrink("cm_protect_"+std::string(channel->name));
336 virtual void OnRehash(User* user, const std::string ¶meter)
338 /* Create a configreader class and read our flag,
339 * in old versions this was heap-allocated and the
340 * object was kept between rehashes...now we just
341 * stack-allocate it locally.
343 ConfigReader Conf(ServerInstance);
345 bool old_qa = QAPrefixes;
347 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
348 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
349 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
350 DeprivOthers = Conf.ReadFlag("options","deprotectothers",0);
352 /* Did the user change the QA prefixes on the fly?
353 * If so, remove all instances of the mode, and reinit
354 * the module with prefixes enabled.
356 if ((old_qa != QAPrefixes) && (!booting))
358 ServerInstance->Modes->DelMode(cp);
359 ServerInstance->Modes->DelMode(cf);
362 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
363 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
364 /* These wont fail, we already owned the mode characters before */
365 ServerInstance->Modes->AddMode(cp);
366 ServerInstance->Modes->AddMode(cf);
367 ServerInstance->SNO->WriteToSnoMask('A', "WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
371 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs)
373 // if the user is the first user into the channel, mark them as the founder, but only if
374 // the config option for it is set
376 if (FirstInGetsFounder && !chan)
382 virtual void OnPostJoin(User *user, Channel *channel)
384 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
385 // before the client is in the channel
387 // This notice was here originally because it was all done prior to the creation of
388 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
389 // not sure it really should be here - ops don't get shown, obviously, and the prefix
390 // will appear in the names list for the user.. remove if desired -Special
392 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
393 user->WriteServ("MODE %s +q %s", channel->name, user->nick);
396 virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
398 // here we perform access checks, this is the important bit that actually stops kicking/deopping
399 // etc of protected users. There are many types of access check, we're going to handle
400 // a relatively small number of them relevent to our module using a switch statement.
401 // don't allow action if:
402 // (A) Theyre founder (no matter what)
403 // (B) Theyre protected, and you're not
404 // always allow the action if:
405 // (A) The source is ulined
408 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
409 // without any access checks, we're not worthy :p
410 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
413 std::string founder = "cm_founder_"+std::string(channel->name);
414 std::string protect = "cm_protect_"+std::string(channel->name);
418 // a user has been deopped. Do we let them? hmmm...
420 if (dest->GetExt(founder,dummyptr))
422 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
425 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
427 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
432 // a user is being kicked. do we chop off the end of the army boot?
434 if (dest->GetExt(founder,dummyptr))
436 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
439 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
441 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
446 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
448 if (dest->GetExt(founder,dummyptr))
450 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");
453 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
455 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
460 // same with devoice.
462 if (dest->GetExt(founder,dummyptr))
464 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
467 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
469 source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
475 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
479 virtual ~ModuleChanProtect()
481 ServerInstance->Modes->DelMode(cp);
482 ServerInstance->Modes->DelMode(cf);
487 virtual Version GetVersion()
489 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
492 virtual void OnSyncChannel(Channel* chan, Module* proto, void* opaque)
494 /* NOTE: If +qa prefix is on, this is propagated by the channel join,
495 * so we dont need to propagate it manually
499 // this is called when the server is linking into a net and wants to sync channel data.
500 // we should send our mode changes for the channel here to ensure that other servers
501 // know whos +q/+a on the channel.
502 CUList* cl = chan->GetUsers();
503 string_list commands;
504 std::string founder = "cm_founder_"+std::string(chan->name);
505 std::string protect = "cm_protect_"+std::string(chan->name);
506 irc::modestacker modestack(true);
507 std::deque<std::string> stackresult;
508 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
510 if (i->first->GetExt(founder,dummyptr))
512 modestack.Push('q',i->first->nick);
514 if (i->first->GetExt(protect,dummyptr))
516 modestack.Push('a',i->first->nick);
519 while (modestack.GetStackedLine(stackresult))
521 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
522 std::string line = mode_join.GetJoined();
523 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
530 MODULE_INIT(ModuleChanProtect)