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 /** Handles basic operation of +qa channel modes
23 class FounderProtectBase
32 bool& remove_own_privs;
33 bool& remove_other_privs;
35 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
36 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
40 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
42 User* x = MyInstance->FindNick(parameter);
45 if (!channel->HasUser(x))
47 return std::make_pair(false, parameter);
51 std::string item = extend+std::string(channel->name);
54 return std::make_pair(true, x->nick);
58 return std::make_pair(false, parameter);
62 return std::make_pair(false, parameter);
65 void RemoveMode(Channel* channel, char mc, irc::modestacker* stack)
67 CUList* cl = channel->GetUsers();
68 std::string item = extend + std::string(channel->name);
69 std::vector<std::string> mode_junk;
70 mode_junk.push_back(channel->name);
71 irc::modestacker modestack(MyInstance, false);
72 std::deque<std::string> stackresult;
74 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
76 if (i->first->GetExt(item))
79 stack->Push(mc, i->first->nick);
81 modestack.Push(mc, i->first->nick);
88 while (modestack.GetStackedLine(stackresult))
90 for (size_t j = 0; j < stackresult.size(); j++)
92 mode_junk.push_back(stackresult[j]);
94 MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
98 void DisplayList(User* user, Channel* channel)
100 CUList* cl = channel->GetUsers();
101 std::string item = extend+std::string(channel->name);
102 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
104 if (i->first->GetExt(item))
106 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
109 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
112 User* FindAndVerify(std::string ¶meter, Channel* channel)
114 User* theuser = MyInstance->FindNick(parameter);
115 if ((!theuser) || (!channel->HasUser(theuser)))
123 bool CanRemoveOthers(User* u1, User* u2, Channel* c)
125 std::string item = extend+std::string(c->name);
126 return (u1->GetExt(item) && u2->GetExt(item));
129 ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string ¶meter)
131 std::string item = extend+std::string(channel->name);
135 if (!theuser->GetExt(item))
137 theuser->Extend(item);
138 parameter = theuser->nick;
139 return MODEACTION_ALLOW;
144 if (theuser->GetExt(item))
146 theuser->Shrink(item);
147 parameter = theuser->nick;
148 return MODEACTION_ALLOW;
151 return MODEACTION_DENY;
155 /** Abstraction of FounderProtectBase for channel mode +q
157 class ChanFounder : public ModeHandler, public FounderProtectBase
160 ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
161 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
162 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
164 unsigned int GetPrefixRank()
166 return FOUNDER_VALUE;
169 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
171 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
174 void RemoveMode(Channel* channel, irc::modestacker* stack)
176 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
179 void RemoveMode(User* user, irc::modestacker* stack)
183 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
185 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
189 return MODEACTION_DENY;
192 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
194 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
196 // source is a server, or ulined, we'll let them +-q the user.
197 if (source == ServerInstance->FakeClient ||
198 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
199 (ServerInstance->ULine(source->nick.c_str())) ||
200 (ServerInstance->ULine(source->server)) ||
201 (!*source->server) ||
204 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
208 // whoops, someones being naughty!
209 source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
211 return MODEACTION_DENY;
215 void DisplayList(User* user, Channel* channel)
217 FounderProtectBase::DisplayList(user,channel);
221 /** Abstraction of FounderProtectBase for channel mode +a
223 class ChanProtect : public ModeHandler, public FounderProtectBase
226 ChanProtect(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
227 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
228 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
230 unsigned int GetPrefixRank()
232 return PROTECT_VALUE;
235 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
237 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
240 void RemoveMode(Channel* channel, irc::modestacker* stack)
242 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
245 void RemoveMode(User* user, irc::modestacker* stack)
249 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
251 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
254 return MODEACTION_DENY;
256 std::string founder = "cm_founder_"+std::string(channel->name);
258 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
260 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
262 // source has +q, is a server, or ulined, we'll let them +-a the user.
263 if (source == ServerInstance->FakeClient ||
264 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
265 (ServerInstance->ULine(source->nick.c_str())) ||
266 (ServerInstance->ULine(source->server)) ||
267 (!*source->server) ||
268 (source->GetExt(founder)) ||
271 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
275 // bzzzt, wrong answer!
276 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
277 return MODEACTION_DENY;
281 virtual void DisplayList(User* user, Channel* channel)
283 FounderProtectBase::DisplayList(user, channel);
288 class ModuleChanProtect : public Module
291 bool FirstInGetsFounder;
302 ModuleChanProtect(InspIRCd* Me)
303 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
305 /* Load config stuff */
309 /* Initialise module variables */
311 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
312 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
314 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
318 throw ModuleException("Could not add new modes!");
321 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin, I_OnPostJoin, I_OnAccessCheck };
322 ServerInstance->Modules->Attach(eventlist, this, 5);
325 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
327 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
328 user->Shrink("cm_founder_"+std::string(chan->name));
329 user->Shrink("cm_protect_"+std::string(chan->name));
332 virtual void OnUserPart(User* user, Channel* channel, const std::string &partreason, bool &silent)
334 // FIX: when someone parts a channel we must remove their Extensibles!
335 user->Shrink("cm_founder_"+std::string(channel->name));
336 user->Shrink("cm_protect_"+std::string(channel->name));
341 /* Create a configreader class and read our flag,
342 * in old versions this was heap-allocated and the
343 * object was kept between rehashes...now we just
344 * stack-allocate it locally.
346 ConfigReader Conf(ServerInstance);
348 FirstInGetsFounder = Conf.ReadFlag("options", "noservices", 0);
350 std::string qpre = Conf.ReadValue("options", "qprefix", 0);
351 QPrefix = qpre.empty() ? 0 : qpre[0];
353 std::string apre = Conf.ReadValue("options", "aprefix", 0);
354 APrefix = apre.empty() ? 0 : apre[0];
356 if ((APrefix && QPrefix) && APrefix == QPrefix)
357 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
359 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
360 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
362 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
363 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
365 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
366 DeprivOthers = Conf.ReadFlag("options","deprotectothers",0);
369 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
371 // if the user is the first user into the channel, mark them as the founder, but only if
372 // the config option for it is set
374 if (FirstInGetsFounder && !chan)
375 privs = QPrefix + "@";
380 virtual void OnPostJoin(User *user, Channel *channel)
382 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
383 // before the client is in the channel
385 // This notice was here originally because it was all done prior to the creation of
386 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
387 // not sure it really should be here - ops don't get shown, obviously, and the prefix
388 // will appear in the names list for the user.. remove if desired -Special
390 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
391 user->WriteServ("MODE %s +q %s", channel->name.c_str(), user->nick.c_str());
394 virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
396 // here we perform access checks, this is the important bit that actually stops kicking/deopping
397 // etc of protected users. There are many types of access check, we're going to handle
398 // a relatively small number of them relevent to our module using a switch statement.
399 // don't allow action if:
400 // (A) Theyre founder (no matter what)
401 // (B) Theyre protected, and you're not
402 // always allow the action if:
403 // (A) The source is ulined
406 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
407 // without any access checks, we're not worthy :p
408 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
411 std::string founder("cm_founder_"+channel->name);
412 std::string protect("cm_protect_"+channel->name);
416 // a user has been deopped. Do we let them? hmmm...
418 if (dest->GetExt(founder))
420 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're a channel founder");
423 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
425 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're protected (+a)");
430 // a user is being kicked. do we chop off the end of the army boot?
432 if (dest->GetExt(founder))
434 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're a channel founder");
437 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
439 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're protected (+a)");
444 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
446 if (dest->GetExt(founder))
448 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're a channel founder");
451 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
453 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're protected (+a)");
458 // same with devoice.
460 if (dest->GetExt(founder))
462 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're a channel founder");
465 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
467 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're protected (+a)");
473 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
477 virtual ~ModuleChanProtect()
479 ServerInstance->Modes->DelMode(cp);
480 ServerInstance->Modes->DelMode(cf);
485 virtual Version GetVersion()
487 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
491 MODULE_INIT(ModuleChanProtect)