+#define PROTECT_VALUE 40000
+#define FOUNDER_VALUE 50000
+
+/** Handles basic operation of +qa channel modes
+ */
+class FounderProtectBase
+{
+ private:
+ InspIRCd* MyInstance;
+ std::string extend;
+ std::string type;
+ int list;
+ int end;
+ protected:
+ bool& remove_own_privs;
+ bool& remove_other_privs;
+ public:
+ FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
+ MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
+ {
+ }
+
+ ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
+ {
+ User* x = MyInstance->FindNick(parameter);
+ if (x)
+ {
+ if (!channel->HasUser(x))
+ {
+ return std::make_pair(false, parameter);
+ }
+ else
+ {
+ std::string item = extend+std::string(channel->name);
+ if (x->GetExt(item))
+ {
+ return std::make_pair(true, x->nick);
+ }
+ else
+ {
+ return std::make_pair(false, parameter);
+ }
+ }
+ }
+ return std::make_pair(false, parameter);
+ }
+
+ void RemoveMode(Channel* channel, char mc, irc::modestacker* stack)
+ {
+ CUList* cl = channel->GetUsers();
+ std::string item = extend + std::string(channel->name);
+ std::vector<std::string> mode_junk;
+ mode_junk.push_back(channel->name);
+ irc::modestacker modestack(ServerInstance, false);
+ std::deque<std::string> stackresult;
+
+ for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
+ {
+ if (i->first->GetExt(item))
+ {
+ if (stack)
+ stack->Push(mc, i->first->nick);
+ else
+ modestack.Push(mc, i->first->nick);
+ }
+ }
+
+ if (stack)
+ return;
+
+ while (modestack.GetStackedLine(stackresult))
+ {
+ for (size_t j = 0; j < stackresult.size(); j++)
+ {
+ mode_junk.push_back(stackresult[j]);
+ }
+ MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
+ }
+ }
+
+ void DisplayList(User* user, Channel* channel)
+ {
+ CUList* cl = channel->GetUsers();
+ std::string item = extend+std::string(channel->name);
+ for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
+ {
+ if (i->first->GetExt(item))
+ {
+ user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
+ }
+ }
+ user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
+ }
+
+ User* FindAndVerify(std::string ¶meter, Channel* channel)
+ {
+ User* theuser = MyInstance->FindNick(parameter);
+ if ((!theuser) || (!channel->HasUser(theuser)))
+ {
+ parameter.clear();
+ return NULL;
+ }
+ return theuser;
+ }
+
+ bool CanRemoveOthers(User* u1, User* u2, Channel* c)
+ {
+ std::string item = extend+std::string(c->name);
+ return (u1->GetExt(item) && u2->GetExt(item));
+ }
+
+ ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string ¶meter)
+ {
+ std::string item = extend+std::string(channel->name);
+
+ if (adding)
+ {
+ if (!theuser->GetExt(item))
+ {
+ theuser->Extend(item);
+ parameter = theuser->nick;
+ return MODEACTION_ALLOW;
+ }
+ }
+ else
+ {
+ if (theuser->GetExt(item))
+ {
+ theuser->Shrink(item);
+ parameter = theuser->nick;
+ return MODEACTION_ALLOW;
+ }
+ }
+ return MODEACTION_DENY;
+ }
+};
+
+/** Abstraction of FounderProtectBase for channel mode +q
+ */
+class ChanFounder : public ModeHandler, public FounderProtectBase
+{
+ public:
+ ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
+ : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
+ FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
+
+ unsigned int GetPrefixRank()
+ {
+ return FOUNDER_VALUE;
+ }
+
+ ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
+ {
+ return FounderProtectBase::ModeSet(source, dest, channel, parameter);
+ }
+
+ void RemoveMode(Channel* channel, irc::modestacker* stack)
+ {
+ FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
+ }
+
+ void RemoveMode(User* user, irc::modestacker* stack)
+ {
+ }
+
+ ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
+ {
+ User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
+
+ if (!theuser)
+ {
+ return MODEACTION_DENY;
+ }
+
+ if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
+ {
+ return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
+ }
+ // source is a server, or ulined, we'll let them +-q the user.
+ if (source == ServerInstance->FakeClient ||
+ ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
+ (ServerInstance->ULine(source->nick.c_str())) ||
+ (ServerInstance->ULine(source->server)) ||
+ (!*source->server) ||
+ (!IS_LOCAL(source)))
+ {
+ return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
+ }
+ else
+ {
+ // whoops, someones being naughty!
+ source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
+ parameter.clear();
+ return MODEACTION_DENY;
+ }
+ }
+
+ void DisplayList(User* user, Channel* channel)
+ {
+ FounderProtectBase::DisplayList(user,channel);
+ }
+};
+
+/** Abstraction of FounderProtectBase for channel mode +a
+ */
+class ChanProtect : public ModeHandler, public FounderProtectBase
+{
+ public:
+ ChanProtect(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
+ : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
+ FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
+
+ unsigned int GetPrefixRank()
+ {
+ return PROTECT_VALUE;
+ }
+
+ ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string ¶meter)
+ {
+ return FounderProtectBase::ModeSet(source, dest, channel, parameter);
+ }
+
+ void RemoveMode(Channel* channel, irc::modestacker* stack)
+ {
+ FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
+ }
+
+ void RemoveMode(User* user, irc::modestacker* stack)
+ {
+ }
+
+ ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding, bool)
+ {
+ User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
+
+ if (!theuser)
+ return MODEACTION_DENY;
+
+ std::string founder = "cm_founder_"+std::string(channel->name);
+
+ if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
+ {
+ return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
+ }
+ // source has +q, is a server, or ulined, we'll let them +-a the user.
+ if (source == ServerInstance->FakeClient ||
+ ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
+ (ServerInstance->ULine(source->nick.c_str())) ||
+ (ServerInstance->ULine(source->server)) ||
+ (!*source->server) ||
+ (source->GetExt(founder)) ||
+ (!IS_LOCAL(source)))
+ {
+ return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
+ }
+ else
+ {
+ // bzzzt, wrong answer!
+ source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
+ return MODEACTION_DENY;
+ }
+ }
+
+ virtual void DisplayList(User* user, Channel* channel)
+ {
+ FounderProtectBase::DisplayList(user, channel);
+ }
+
+};