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;
34 bool& add_other_privs;
36 FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others, bool &add_others) :
37 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others), add_other_privs(add_others)
41 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
43 User* x = MyInstance->FindNick(parameter);
46 if (!channel->HasUser(x))
48 return std::make_pair(false, parameter);
52 std::string item = extend+std::string(channel->name);
55 return std::make_pair(true, x->nick);
59 return std::make_pair(false, parameter);
63 return std::make_pair(false, parameter);
66 void RemoveMode(Channel* channel, char mc, irc::modestacker* stack)
68 CUList* cl = channel->GetUsers();
69 std::string item = extend + std::string(channel->name);
70 std::vector<std::string> mode_junk;
71 mode_junk.push_back(channel->name);
72 irc::modestacker modestack(MyInstance, false);
73 std::deque<std::string> stackresult;
75 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
77 if (i->first->GetExt(item))
80 stack->Push(mc, i->first->nick);
82 modestack.Push(mc, i->first->nick);
89 while (modestack.GetStackedLine(stackresult))
91 for (size_t j = 0; j < stackresult.size(); j++)
93 mode_junk.push_back(stackresult[j]);
95 MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
99 void DisplayList(User* user, Channel* channel)
101 CUList* cl = channel->GetUsers();
102 std::string item = extend+std::string(channel->name);
103 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
105 if (i->first->GetExt(item))
107 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
110 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
113 User* FindAndVerify(std::string ¶meter, Channel* channel)
115 User* theuser = MyInstance->FindNick(parameter);
116 if ((!theuser) || (!channel->HasUser(theuser)))
124 bool CanRemoveOthers(User* u1, User* u2, Channel* c)
126 std::string item = extend+std::string(c->name);
127 return (remove_other_privs && u1->GetExt(item) && u2->GetExt(item));
130 bool CanAddOthers(User* u1, User* u2, Channel* c)
132 std::string item = extend+std::string(c->name);
133 return (add_other_privs && u1->GetExt(item));
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))
144 theuser->Extend(item);
145 parameter = theuser->nick;
146 return MODEACTION_ALLOW;
151 if (theuser->GetExt(item))
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
167 ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others, bool &priv_others)
168 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
169 FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others, priv_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, irc::modestacker* stack)
183 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
186 void RemoveMode(User* user, irc::modestacker* stack)
190 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
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 (source == ServerInstance->FakeClient ||
205 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
206 (ServerInstance->ULine(source->nick.c_str())) ||
207 (ServerInstance->ULine(source->server)) ||
208 (!*source->server) ||
211 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
214 // If they have it, allow them to give it.
215 if (adding && FounderProtectBase::CanAddOthers(source, theuser, channel))
217 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
220 // whoops, someones being naughty!
221 source->WriteNumeric(468, "%s %s :You are not permitted to set additional founders", source->nick.c_str(), channel->name.c_str());
223 return MODEACTION_DENY;
226 void DisplayList(User* user, Channel* channel)
228 FounderProtectBase::DisplayList(user,channel);
232 /** Abstraction of FounderProtectBase for channel mode +a
234 class ChanProtect : public ModeHandler, public FounderProtectBase
237 ChanProtect(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others, bool &priv_others)
238 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
239 FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others, priv_others) { }
241 unsigned int GetPrefixRank()
243 return PROTECT_VALUE;
246 ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
248 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
251 void RemoveMode(Channel* channel, irc::modestacker* stack)
253 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
256 void RemoveMode(User* user, irc::modestacker* stack)
260 ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
262 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
265 return MODEACTION_DENY;
267 std::string founder = "cm_founder_"+std::string(channel->name);
269 if (!adding && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
271 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
274 // source has +q, is a server, or ulined, we'll let them +-a the user.
275 if (source == ServerInstance->FakeClient ||
276 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
277 (ServerInstance->ULine(source->nick.c_str())) ||
278 (ServerInstance->ULine(source->server)) ||
279 (!*source->server) ||
280 (source->GetExt(founder)) ||
283 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
286 // If they have it, allow them to give it.
287 if (adding && FounderProtectBase::CanAddOthers(source, theuser, channel))
289 return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
292 // bzzzt, wrong answer!
293 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
294 return MODEACTION_DENY;
297 virtual void DisplayList(User* user, Channel* channel)
299 FounderProtectBase::DisplayList(user, channel);
304 class ModuleChanProtect : public Module
307 bool FirstInGetsFounder;
319 ModuleChanProtect(InspIRCd* Me)
320 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
322 /* Load config stuff */
326 /* Initialise module variables */
328 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers, PrivOthers);
329 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers, PrivOthers);
331 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
335 throw ModuleException("Could not add new modes!");
338 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin, I_OnPostJoin, I_OnAccessCheck };
339 ServerInstance->Modules->Attach(eventlist, this, 5);
342 virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
344 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
345 user->Shrink("cm_founder_"+std::string(chan->name));
346 user->Shrink("cm_protect_"+std::string(chan->name));
349 virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
351 // FIX: when someone parts a channel we must remove their Extensibles!
352 user->Shrink("cm_founder_"+std::string(channel->name));
353 user->Shrink("cm_protect_"+std::string(channel->name));
358 ConfigReader Conf(ServerInstance);
360 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
362 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
363 QPrefix = qpre.empty() ? 0 : qpre[0];
365 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
366 APrefix = apre.empty() ? 0 : apre[0];
368 if ((APrefix && QPrefix) && APrefix == QPrefix)
369 throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
371 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
372 throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
374 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
375 throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
377 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
378 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
379 DeprivOthers = Conf.ReadFlag("chanprotect","setprivsonothers", "yes", 0);
382 virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
384 // if the user is the first user into the channel, mark them as the founder, but only if
385 // the config option for it is set
387 if (FirstInGetsFounder && !chan)
388 privs = std::string(1, QPrefix) + "@";
393 virtual void OnPostJoin(User *user, Channel *channel)
395 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
396 // before the client is in the channel
398 // This notice was here originally because it was all done prior to the creation of
399 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
400 // not sure it really should be here - ops don't get shown, obviously, and the prefix
401 // will appear in the names list for the user.. remove if desired -Special
403 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
404 user->WriteServ("MODE %s +q %s", channel->name.c_str(), user->nick.c_str());
407 virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
409 // here we perform access checks, this is the important bit that actually stops kicking/deopping
410 // etc of protected users. There are many types of access check, we're going to handle
411 // a relatively small number of them relevent to our module using a switch statement.
412 // don't allow action if:
413 // (A) Theyre founder (no matter what)
414 // (B) Theyre protected, unless you're founder or are protected and DeprivOthers is enabled
415 // always allow the action if:
416 // (A) The source is ulined
418 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
419 // without any access checks, we're not worthy :p
420 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
423 std::string founder("cm_founder_"+channel->name);
424 std::string protect("cm_protect_"+channel->name);
426 // Can do anything to yourself if deprotectself is enabled.
427 if (DeprivSelf && source == dest)
430 bool candepriv_founder = (DeprivOthers && source->GetExt(founder));
431 bool candepriv_protected = (source->GetExt(founder) || (DeprivOthers && source->GetExt(protect))); // Can the source remove +a?
435 // a user has been deopped. Do we let them? hmmm...
437 if (dest->GetExt(founder) && !candepriv_founder)
439 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're a channel founder");
442 if ((dest->GetExt(protect)) && !candepriv_protected)
444 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're protected (+a)");
449 // a user is being kicked. do we chop off the end of the army boot?
451 if (dest->GetExt(founder) && !candepriv_founder)
453 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're a channel founder");
456 if ((dest->GetExt(protect)) && !candepriv_protected)
458 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're protected (+a)");
463 // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
465 if (dest->GetExt(founder) && !candepriv_founder)
467 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're a channel founder");
470 if ((dest->GetExt(protect)) && !candepriv_protected)
472 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're protected (+a)");
477 // same with devoice.
479 if (dest->GetExt(founder) && !candepriv_founder)
481 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're a channel founder");
484 if ((dest->GetExt(protect)) && !candepriv_protected)
486 source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're protected (+a)");
492 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
496 virtual ~ModuleChanProtect()
498 ServerInstance->Modes->DelMode(cp);
499 ServerInstance->Modes->DelMode(cf);
504 virtual Version GetVersion()
506 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
510 MODULE_INIT(ModuleChanProtect)