+ 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(MyInstance, 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))
+ {
+ mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
+ MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
+ mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
+ }
+ }
+
+ 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 (remove_other_privs && 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, TR_NICK),
+ 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);
+ }
+
+ char isoverride=0;
+ Module *Override = ServerInstance->Modules->FindFeature("Override");
+ if (Override)
+ {
+ OVRrequest ovr(NULL,Override,source,"OTHERMODE");
+ const char * tmp = ovr.Send();
+ isoverride = tmp[0];
+ }
+ // 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)) ||
+ isoverride)
+ {
+ 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, TR_NICK),
+ 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);
+ }
+
+ char isoverride=0;
+ Module *Override = ServerInstance->Modules->FindFeature("Override");
+ if (Override)
+ {
+ OVRrequest ovr(NULL,Override,source,"OTHERMODE");
+ const char * tmp = ovr.Send();
+ isoverride = tmp[0];
+ }
+ // 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)) ||
+ isoverride
+ )
+ {
+ 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);
+ }
+
+};
+
+class ModuleChanProtect : public Module
+{
+
+ bool FirstInGetsFounder;
+ char QPrefix;
+ char APrefix;
+ bool DeprivSelf;
+ bool DeprivOthers;
+ bool booting;
+ ChanProtect* cp;
+ ChanFounder* cf;
+
+ public:
+
+ ModuleChanProtect(InspIRCd* Me)
+ : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
+ {
+ /* Load config stuff */
+ LoadSettings();
+ booting = false;
+
+ /* Initialise module variables */
+
+ cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
+ cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
+
+ if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
+ {
+ delete cp;
+ delete cf;
+ throw ModuleException("Could not add new modes!");
+ }
+
+ Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin, I_OnAccessCheck };
+ ServerInstance->Modules->Attach(eventlist, this, 4);
+ }
+
+ virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)