summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAttila Molnar <attilamolnar@hush.com>2014-09-04 12:58:25 +0200
committerAttila Molnar <attilamolnar@hush.com>2014-09-04 12:58:25 +0200
commit5f031349833d18d9fe5495b848c2d3fb7fd7f8f0 (patch)
tree3e5413ffd29e53ddded1be81343547ba0c686e3e /src
parent6fe96bda2531cb1357f35c6d0717910283e2d921 (diff)
Pass Modes::ChangeList references to the OnPreMode hook, make it modifiable
This gets rid of the duplicated mode parsing logic in m_namedmodes
Diffstat (limited to 'src')
-rw-r--r--src/mode.cpp2
-rw-r--r--src/modules.cpp2
-rw-r--r--src/modules/m_namedmodes.cpp69
-rw-r--r--src/modules/m_override.cpp38
-rw-r--r--src/modules/m_samode.cpp2
5 files changed, 59 insertions, 54 deletions
diff --git a/src/mode.cpp b/src/mode.cpp
index 770e6a2e2..e895b77d0 100644
--- a/src/mode.cpp
+++ b/src/mode.cpp
@@ -393,7 +393,7 @@ void ModeParser::Process(const std::vector<std::string>& parameters, User* user,
ModeParamsToChangeList(user, type, parameters, changelist);
ModResult MOD_RESULT;
- FIRST_MOD_RESULT(OnPreMode, MOD_RESULT, (user, targetuser, targetchannel, parameters));
+ FIRST_MOD_RESULT(OnPreMode, MOD_RESULT, (user, targetuser, targetchannel, changelist));
if (IS_LOCAL(user))
{
diff --git a/src/modules.cpp b/src/modules.cpp
index 7e932ed72..0a5a8a702 100644
--- a/src/modules.cpp
+++ b/src/modules.cpp
@@ -95,7 +95,7 @@ ModResult Module::OnUserPreInvite(User*, User*, Channel*, time_t) { DetachEvent(
ModResult Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&, MessageType) { DetachEvent(I_OnUserPreMessage); return MOD_RES_PASSTHRU; }
ModResult Module::OnUserPreNick(LocalUser*, const std::string&) { DetachEvent(I_OnUserPreNick); return MOD_RES_PASSTHRU; }
void Module::OnUserPostNick(User*, const std::string&) { DetachEvent(I_OnUserPostNick); }
-ModResult Module::OnPreMode(User*, User*, Channel*, const std::vector<std::string>&) { DetachEvent(I_OnPreMode); return MOD_RES_PASSTHRU; }
+ModResult Module::OnPreMode(User*, User*, Channel*, Modes::ChangeList&) { DetachEvent(I_OnPreMode); return MOD_RES_PASSTHRU; }
void Module::On005Numeric(std::map<std::string, std::string>&) { DetachEvent(I_On005Numeric); }
ModResult Module::OnKill(User*, User*, const std::string&) { DetachEvent(I_OnKill); return MOD_RES_PASSTHRU; }
void Module::OnLoadModule(Module*) { DetachEvent(I_OnLoadModule); }
diff --git a/src/modules/m_namedmodes.cpp b/src/modules/m_namedmodes.cpp
index 5c0ffeea5..4004db00e 100644
--- a/src/modules/m_namedmodes.cpp
+++ b/src/modules/m_namedmodes.cpp
@@ -89,6 +89,12 @@ class DummyZ : public ModeHandler
{
list = true;
}
+
+ // Handle /MODE #chan Z
+ void DisplayList(User* user, Channel* chan)
+ {
+ ::DisplayList(user, chan);
+ }
};
class ModuleNamedModes : public Module
@@ -110,39 +116,22 @@ class ModuleNamedModes : public Module
ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_FIRST);
}
- ModResult OnPreMode(User* source, User* dest, Channel* channel, const std::vector<std::string>& parameters) CXX11_OVERRIDE
+ ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
{
if (!channel)
return MOD_RES_PASSTHRU;
- if (parameters[1].find('Z') == std::string::npos)
- return MOD_RES_PASSTHRU;
- if (parameters.size() <= 2)
- {
- DisplayList(source, channel);
- return MOD_RES_DENY;
- }
-
- std::vector<std::string> newparms;
- newparms.push_back(parameters[0]);
- newparms.push_back(parameters[1]);
- std::string modelist = newparms[1];
- bool adding = true;
- unsigned int param_at = 2;
- for(unsigned int i = 0; i < modelist.length(); i++)
+ Modes::ChangeList::List& list = modes.getlist();
+ for (Modes::ChangeList::List::iterator i = list.begin(); i != list.end(); )
{
- unsigned char modechar = modelist[i];
- if (modechar == '+' || modechar == '-')
+ Modes::Change& curr = *i;
+ // Replace all namebase (dummyZ) modes being changed with the actual
+ // mode handler and parameter. The parameter format of the namebase mode is
+ // <modename>[=<parameter>].
+ if (curr.mh == &dummyZ)
{
- adding = (modechar == '+');
- continue;
- }
- ModeHandler *mh = ServerInstance->Modes->FindMode(modechar, MODETYPE_CHANNEL);
- if (modechar == 'Z')
- {
- std::string name, value;
- if (param_at < parameters.size())
- name = parameters[param_at++];
+ std::string name = curr.param;
+ std::string value;
std::string::size_type eq = name.find('=');
if (eq != std::string::npos)
{
@@ -150,36 +139,36 @@ class ModuleNamedModes : public Module
name = name.substr(0, eq);
}
- mh = ServerInstance->Modes->FindMode(name, MODETYPE_CHANNEL);
+ ModeHandler* mh = ServerInstance->Modes->FindMode(name, MODETYPE_CHANNEL);
if (!mh)
{
// Mode handler not found
- modelist.erase(i--, 1);
+ i = list.erase(i);
continue;
}
- if (mh->GetNumParams(adding))
+ curr.param.clear();
+ if (mh->GetNumParams(curr.adding))
{
if (value.empty())
{
// Mode needs a parameter but there wasn't one
- modelist.erase(i--, 1);
+ i = list.erase(i);
continue;
}
- newparms.push_back(value);
+ // Change parameter to the text after the '='
+ curr.param = value;
}
- modelist[i] = mh->GetModeChar();
- }
- else if (mh && mh->GetNumParams(adding) && param_at < parameters.size())
- {
- newparms.push_back(parameters[param_at++]);
+ // Put the actual ModeHandler in place of the namebase handler
+ curr.mh = mh;
}
+
+ ++i;
}
- newparms[1] = modelist;
- ServerInstance->Modes->Process(newparms, source);
- return MOD_RES_DENY;
+
+ return MOD_RES_PASSTHRU;
}
};
diff --git a/src/modules/m_override.cpp b/src/modules/m_override.cpp
index 756ef8edc..69f4b3bca 100644
--- a/src/modules/m_override.cpp
+++ b/src/modules/m_override.cpp
@@ -35,14 +35,11 @@ class ModuleOverride : public Module
ChanModeReference key;
ChanModeReference limit;
- static bool IsOverride(unsigned int userlevel, const std::string& modeline)
+ static bool IsOverride(unsigned int userlevel, const Modes::ChangeList::List& list)
{
- for (std::string::const_iterator i = modeline.begin(); i != modeline.end(); ++i)
+ for (Modes::ChangeList::List::const_iterator i = list.begin(); i != list.end(); ++i)
{
- ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
- if (!mh)
- continue;
-
+ ModeHandler* mh = i->mh;
if (mh->GetLevelRequired() > userlevel)
return true;
}
@@ -130,23 +127,42 @@ class ModuleOverride : public Module
return MOD_RES_PASSTHRU;
}
- ModResult OnPreMode(User* source,User* dest,Channel* channel, const std::vector<std::string>& parameters) CXX11_OVERRIDE
+ ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
{
if (!channel)
return MOD_RES_PASSTHRU;
if (!source->IsOper() || !IS_LOCAL(source))
return MOD_RES_PASSTHRU;
+ const Modes::ChangeList::List& list = modes.getlist();
unsigned int mode = channel->GetPrefixValue(source);
- if (!IsOverride(mode, parameters[1]))
+ if (!IsOverride(mode, list))
return MOD_RES_PASSTHRU;
if (CanOverride(source, "MODE"))
{
- std::string msg = source->nick+" overriding modes:";
- for(unsigned int i=0; i < parameters.size(); i++)
- msg += " " + parameters[i];
+ std::string msg = source->nick + " overriding modes: ";
+
+ // Construct a MODE string in the old format for sending it as a snotice
+ std::string params;
+ char pm = 0;
+ for (Modes::ChangeList::List::const_iterator i = list.begin(); i != list.end(); ++i)
+ {
+ const Modes::Change& item = *i;
+ if (!item.param.empty())
+ params.append(1, ' ').append(item.param);
+
+ char wanted_pm = (item.adding ? '+' : '-');
+ if (wanted_pm != pm)
+ {
+ pm = wanted_pm;
+ msg += pm;
+ }
+
+ msg += item.mh->GetModeChar();
+ }
+ msg += params;
ServerInstance->SNO->WriteGlobalSno('v',msg);
return MOD_RES_ALLOW;
}
diff --git a/src/modules/m_samode.cpp b/src/modules/m_samode.cpp
index b8601ef9a..63d4f7622 100644
--- a/src/modules/m_samode.cpp
+++ b/src/modules/m_samode.cpp
@@ -75,7 +75,7 @@ class ModuleSaMode : public Module
return Version("Provides command SAMODE to allow opers to change modes on channels and users", VF_VENDOR);
}
- ModResult OnPreMode(User* source,User* dest,Channel* channel, const std::vector<std::string>& parameters) CXX11_OVERRIDE
+ ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
{
if (cmd.active)
return MOD_RES_ALLOW;