]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/mode.cpp
Introduce ModeProcessFlags, can be passed to ModeParser::Process() to indicate local...
[user/henk/code/inspircd.git] / src / mode.cpp
index a9ac006ae91d88a04cea59854a7920f53e953ece..63008f45c8ffe69bd7b20a6cf72bbb0c322b674c 100644 (file)
@@ -29,7 +29,7 @@
 ModeHandler::ModeHandler(Module* Creator, const std::string& Name, char modeletter, ParamSpec Params, ModeType type)
        : ServiceProvider(Creator, Name, SERVICE_MODE), m_paramtype(TR_TEXT),
        parameters_taken(Params), mode(modeletter), prefix(0), oper(false),
-       list(false), m_type(type), levelrequired(HALFOP_VALUE)
+       list(false), m_type(type), levelrequired(HALFOP_VALUE), prefixrank(0)
 {
 }
 
@@ -44,16 +44,6 @@ ModeHandler::~ModeHandler()
 {
 }
 
-bool ModeHandler::IsListMode()
-{
-       return list;
-}
-
-unsigned int ModeHandler::GetPrefixRank()
-{
-       return 0;
-}
-
 int ModeHandler::GetNumParams(bool adding)
 {
        switch (parameters_taken)
@@ -156,8 +146,8 @@ bool ParamChannelModeHandler::ParamValidate(std::string& parameter)
        return true;
 }
 
-ModeWatcher::ModeWatcher(Module* Creator, char modeletter, ModeType type)
-       : mode(modeletter), m_type(type), creator(Creator)
+ModeWatcher::ModeWatcher(Module* Creator, const std::string& modename, ModeType type)
+       : mode(modename), m_type(type), creator(Creator)
 {
 }
 
@@ -165,22 +155,17 @@ ModeWatcher::~ModeWatcher()
 {
 }
 
-char ModeWatcher::GetModeChar()
-{
-       return mode;
-}
-
 ModeType ModeWatcher::GetModeType()
 {
        return m_type;
 }
 
-bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool, ModeType)
+bool ModeWatcher::BeforeMode(User*, User*, Channel*, std::string&, bool)
 {
        return true;
 }
 
-void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool, ModeType)
+void ModeWatcher::AfterMode(User*, User*, Channel*, const std::string&, bool)
 {
 }
 
@@ -200,7 +185,7 @@ void ModeParser::DisplayCurrentModes(User *user, User* targetuser, Channel* targ
                        /* Display user's current mode string */
                        user->WriteNumeric(RPL_UMODEIS, "%s :+%s",targetuser->nick.c_str(),targetuser->FormatModes());
                        if ((targetuser->IsOper()))
-                               user->WriteNumeric(RPL_SNOMASKIS, "%s +%s :Server notice mask", targetuser->nick.c_str(), targetuser->FormatNoticeMasks());
+                               user->WriteNumeric(RPL_SNOMASKIS, "%s +%s :Server notice mask", targetuser->nick.c_str(), targetuser->FormatNoticeMasks().c_str());
                        return;
                }
                else
@@ -215,7 +200,6 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool
                std::string &parameter, bool SkipACL)
 {
        ModeType type = chan ? MODETYPE_CHANNEL : MODETYPE_USER;
-       unsigned char mask = chan ? MASK_CHANNEL : MASK_USER;
 
        ModeHandler *mh = FindMode(modechar, type);
        int pcnt = mh->GetNumParams(adding);
@@ -269,15 +253,20 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool
                }
        }
 
-       unsigned char handler_id = (modechar - 'A') | mask;
-
-       for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
+       // Ask mode watchers whether this mode change is OK
+       std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mh->name);
+       for (ModeWatchIter i = itpair.first; i != itpair.second; ++i)
        {
-               if ((*watchers)->BeforeMode(user, targetuser, chan, parameter, adding, type) == false)
-                       return MODEACTION_DENY;
-               /* A module whacked the parameter completely, and there was one. abort. */
-               if (pcnt && parameter.empty())
-                       return MODEACTION_DENY;
+               ModeWatcher* mw = i->second;
+               if (mw->GetModeType() == type)
+               {
+                       if (!mw->BeforeMode(user, targetuser, chan, parameter, adding))
+                               return MODEACTION_DENY;
+
+                       // A module whacked the parameter completely, and there was one. Abort.
+                       if (pcnt && parameter.empty())
+                               return MODEACTION_DENY;
+               }
        }
 
        if (IS_LOCAL(user) && !user->IsOper())
@@ -331,13 +320,18 @@ ModeAction ModeParser::TryMode(User* user, User* targetuser, Channel* chan, bool
        if (ma != MODEACTION_ALLOW)
                return ma;
 
-       for (ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
-               (*watchers)->AfterMode(user, targetuser, chan, parameter, adding, type);
+       itpair = modewatchermap.equal_range(mh->name);
+       for (ModeWatchIter i = itpair.first; i != itpair.second; ++i)
+       {
+               ModeWatcher* mw = i->second;
+               if (mw->GetModeType() == type)
+                       mw->AfterMode(user, targetuser, chan, parameter, adding);
+       }
 
        return MODEACTION_ALLOW;
 }
 
-void ModeParser::Process(const std::vector<std::string>& parameters, User *user, bool merge)
+void ModeParser::Process(const std::vector<std::string>& parameters, User* user, ModeProcessFlag flags)
 {
        std::string target = parameters[0];
        Channel* targetchannel = ServerInstance->FindChan(target);
@@ -417,7 +411,7 @@ void ModeParser::Process(const std::vector<std::string>& parameters, User *user,
                        /* Make sure the user isn't trying to slip in an invalid parameter */
                        if ((parameter.find(':') == 0) || (parameter.rfind(' ') != std::string::npos))
                                continue;
-                       if (merge && targetchannel && targetchannel->IsModeSet(modechar) && !mh->IsListMode())
+                       if ((flags & MODE_MERGE) && targetchannel && targetchannel->IsModeSet(modechar) && !mh->IsListMode())
                        {
                                std::string ours = targetchannel->GetModeParameter(modechar);
                                if (!mh->ResolveModeConflict(parameter, ours, targetchannel))
@@ -471,6 +465,9 @@ void ModeParser::Process(const std::vector<std::string>& parameters, User *user,
                LastParse.append(output_mode);
                LastParse.append(output_parameters.str());
 
+               if (!(flags & MODE_LOCALONLY))
+                       ServerInstance->PI->SendMode(user, targetuser, targetchannel, LastParseParams, LastParseTranslate);
+
                if (targetchannel)
                {
                        targetchannel->WriteChannel(user, "MODE %s", LastParse.c_str());
@@ -527,15 +524,24 @@ void ModeParser::DisplayListModes(User* user, Channel* chan, std::string &mode_s
                        display = false;
                }
 
-               unsigned char handler_id = (mletter - 'A') | MASK_CHANNEL;
-
-               for(ModeWatchIter watchers = modewatchers[handler_id].begin(); watchers != modewatchers[handler_id].end(); watchers++)
+               // Ask mode watchers whether it's OK to show the list
+               std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mh->name);
+               for (ModeWatchIter i = itpair.first; i != itpair.second; ++i)
                {
-                       std::string dummyparam;
+                       ModeWatcher* mw = i->second;
+                       if (mw->GetModeType() == MODETYPE_CHANNEL)
+                       {
+                               std::string dummyparam;
 
-                       if (!((*watchers)->BeforeMode(user, NULL, chan, dummyparam, true, MODETYPE_CHANNEL)))
-                               display = false;
+                               if (!mw->BeforeMode(user, NULL, chan, dummyparam, true))
+                               {
+                                       // A mode watcher doesn't want us to show the list
+                                       display = false;
+                                       break;
+                               }
+                       }
                }
+
                if (display)
                        mh->DisplayList(user, chan);
                else
@@ -543,11 +549,6 @@ void ModeParser::DisplayListModes(User* user, Channel* chan, std::string &mode_s
        }
 }
 
-const std::string& ModeParser::GetLastParse()
-{
-       return LastParse;
-}
-
 void ModeParser::CleanMask(std::string &mask)
 {
        std::string::size_type pos_of_pling = mask.find_first_of('!');
@@ -658,7 +659,7 @@ bool ModeParser::DelMode(ModeHandler* mh)
                                stackresult.push_back(chan->name);
                                while (stack.GetStackedLine(stackresult))
                                {
-                                       ServerInstance->SendMode(stackresult, ServerInstance->FakeClient);
+                                       this->Process(stackresult, ServerInstance->FakeClient, MODE_LOCALONLY);
                                        stackresult.erase(stackresult.begin() + 1, stackresult.end());
                                }
                        }
@@ -793,49 +794,24 @@ std::string ModeParser::BuildPrefixes(bool lettersAndModes)
        return lettersAndModes ? "(" + mprefixes + ")" + mletters : mletters;
 }
 
-bool ModeParser::AddModeWatcher(ModeWatcher* mw)
+void ModeParser::AddModeWatcher(ModeWatcher* mw)
 {
-       unsigned char mask = 0;
-       unsigned char pos = 0;
-
-       if (!mw)
-               return false;
-
-       if ((mw->GetModeChar() < 'A') || (mw->GetModeChar() > 'z'))
-               return false;
-
-       mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
-       pos = (mw->GetModeChar()-65) | mask;
-
-       modewatchers[pos].push_back(mw);
-
-       return true;
+       modewatchermap.insert(std::make_pair(mw->GetModeName(), mw));
 }
 
 bool ModeParser::DelModeWatcher(ModeWatcher* mw)
 {
-       unsigned char mask = 0;
-       unsigned char pos = 0;
-
-       if (!mw)
-               return false;
-
-       if ((mw->GetModeChar() < 'A') || (mw->GetModeChar() > 'z'))
-               return false;
-
-       mw->GetModeType() == MODETYPE_USER ? mask = MASK_USER : mask = MASK_CHANNEL;
-       pos = (mw->GetModeChar()-65) | mask;
-
-       ModeWatchIter a = find(modewatchers[pos].begin(),modewatchers[pos].end(),mw);
-
-       if (a == modewatchers[pos].end())
+       std::pair<ModeWatchIter, ModeWatchIter> itpair = modewatchermap.equal_range(mw->GetModeName());
+       for (ModeWatchIter i = itpair.first; i != itpair.second; ++i)
        {
-               return false;
+               if (i->second == mw)
+               {
+                       modewatchermap.erase(i);
+                       return true;
+               }
        }
 
-       modewatchers[pos].erase(a);
-
-       return true;
+       return false;
 }
 
 void ModeHandler::RemoveMode(User* user)