summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPeter Powell <petpow@saberuk.com>2017-08-17 18:32:19 +0100
committerPeter Powell <petpow@saberuk.com>2017-11-13 19:44:04 +0000
commit5287af979e5abffb2cfcdadb9a7663b42a5c43e5 (patch)
tree978444d04b4ec69e369e20bf74b4c0c16f2e50f0 /src
parentdb6eefb16360e34dd8a33ce1cc792b3de62cfcef (diff)
Add a class which encapsulates the concept of token lists.
Diffstat (limited to 'src')
-rw-r--r--src/inspstring.cpp110
-rw-r--r--src/modules/m_check.cpp6
-rw-r--r--src/users.cpp33
3 files changed, 118 insertions, 31 deletions
diff --git a/src/inspstring.cpp b/src/inspstring.cpp
index b59492738..283c00d5b 100644
--- a/src/inspstring.cpp
+++ b/src/inspstring.cpp
@@ -124,3 +124,113 @@ bool InspIRCd::TimingSafeCompare(const std::string& one, const std::string& two)
return (diff == 0);
}
+
+void TokenList::AddList(const std::string& tokenlist)
+{
+ std::string token;
+ irc::spacesepstream tokenstream(tokenlist);
+ while (tokenstream.GetToken(token))
+ {
+ if (token[0] == '-')
+ Remove(token.substr(1));
+ else
+ Add(token);
+ }
+
+}
+void TokenList::Add(const std::string& token)
+{
+ // If the token is empty or contains just whitespace it is invalid.
+ if (token.empty() || token.find_first_not_of(" \t") == std::string::npos)
+ return;
+
+ // If the token is a wildcard entry then permissive mode has been enabled.
+ if (token == "*")
+ {
+ permissive = true;
+ tokens.clear();
+ return;
+ }
+
+ // If we are in permissive mode then remove the token from the token list.
+ // Otherwise, add it to the token list.
+ if (permissive)
+ tokens.erase(token);
+ else
+ tokens.insert(token);
+}
+
+void TokenList::Clear()
+{
+ permissive = false;
+ tokens.clear();
+}
+
+bool TokenList::Contains(const std::string& token) const
+{
+ // If we are in permissive mode and the token is in the list
+ // then we don't have it.
+ if (permissive && tokens.find(token) != tokens.end())
+ return false;
+
+ // If we are not in permissive mode and the token is not in
+ // the list then we don't have it.
+ if (!permissive && tokens.find(token) == tokens.end())
+ return false;
+
+ // We have the token!
+ return true;
+}
+
+void TokenList::Remove(const std::string& token)
+{
+ // If the token is empty or contains just whitespace it is invalid.
+ if (token.empty() || token.find_first_not_of(" \t") == std::string::npos)
+ return;
+
+ // If the token is a wildcard entry then permissive mode has been disabled.
+ if (token == "*")
+ {
+ permissive = false;
+ tokens.clear();
+ return;
+ }
+
+ // If we are in permissive mode then add the token to the token list.
+ // Otherwise, remove it from the token list.
+ if (permissive)
+ tokens.insert(token);
+ else
+ tokens.erase(token);
+}
+
+std::string TokenList::ToString() const
+{
+ std::string buffer(permissive ? "*" : "-*");
+ for (insp::flat_set<std::string>::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter)
+ {
+ buffer.push_back(' ');
+ buffer.append(*iter);
+ }
+ return buffer;
+}
+
+bool TokenList::operator==(const TokenList& other) const
+{
+ // Both sets must be in the same mode to be equal.
+ if (permissive != other.permissive)
+ return false;
+
+ // Both sets must be the same size to be equal.
+ if (tokens.size() != other.tokens.size())
+ return false;
+
+ for (insp::flat_set<std::string>::const_iterator iter = tokens.begin(); iter != tokens.end(); ++iter)
+ {
+ // Both sets must contain the same tokens to be equal.
+ if (other.tokens.find(*iter) == other.tokens.end())
+ return false;
+ }
+
+ return true;
+}
diff --git a/src/modules/m_check.cpp b/src/modules/m_check.cpp
index b442dea9c..e401c79f1 100644
--- a/src/modules/m_check.cpp
+++ b/src/modules/m_check.cpp
@@ -197,12 +197,10 @@ class CommandCheck : public Command
context.Write("modeperms", "user=" + umodes + " channel=" + cmodes);
CheckContext::List opcmdlist(context, "commandperms");
- for (OperInfo::PrivSet::const_iterator i = oper->AllowedOperCommands.begin(); i != oper->AllowedOperCommands.end(); ++i)
- opcmdlist.Add(*i);
+ opcmdlist.Add(oper->AllowedOperCommands.ToString());
opcmdlist.Flush();
CheckContext::List privlist(context, "permissions");
- for (OperInfo::PrivSet::const_iterator i = oper->AllowedPrivs.begin(); i != oper->AllowedPrivs.end(); ++i)
- privlist.Add(*i);
+ opcmdlist.Add(oper->AllowedPrivs.ToString());
privlist.Flush();
}
}
diff --git a/src/users.cpp b/src/users.cpp
index 397a13267..f7cd00a07 100644
--- a/src/users.cpp
+++ b/src/users.cpp
@@ -189,12 +189,7 @@ bool LocalUser::HasPermission(const std::string &command)
return false;
}
- if (oper->AllowedOperCommands.find(command) != oper->AllowedOperCommands.end())
- return true;
- else if (oper->AllowedOperCommands.find("*") != oper->AllowedOperCommands.end())
- return true;
-
- return false;
+ return oper->AllowedOperCommands.Contains(command);
}
bool User::HasPrivPermission(const std::string &privstr, bool noisy)
@@ -211,14 +206,8 @@ bool LocalUser::HasPrivPermission(const std::string &privstr, bool noisy)
return false;
}
- if (oper->AllowedPrivs.find(privstr) != oper->AllowedPrivs.end())
- {
+ if (oper->AllowedPrivs.Contains(privstr))
return true;
- }
- else if (oper->AllowedPrivs.find("*") != oper->AllowedPrivs.end())
- {
- return true;
- }
if (noisy)
this->WriteNotice("Oper type " + oper->name + " does not have access to priv " + privstr);
@@ -375,8 +364,8 @@ void User::Oper(OperInfo* info)
void OperInfo::init()
{
- AllowedOperCommands.clear();
- AllowedPrivs.clear();
+ AllowedOperCommands.Clear();
+ AllowedPrivs.Clear();
AllowedUserModes.reset();
AllowedChanModes.reset();
AllowedUserModes['o' - 'A'] = true; // Call me paranoid if you want.
@@ -384,19 +373,9 @@ void OperInfo::init()
for(std::vector<reference<ConfigTag> >::iterator iter = class_blocks.begin(); iter != class_blocks.end(); ++iter)
{
ConfigTag* tag = *iter;
- std::string mycmd, mypriv;
- /* Process commands */
- irc::spacesepstream CommandList(tag->getString("commands"));
- while (CommandList.GetToken(mycmd))
- {
- AllowedOperCommands.insert(mycmd);
- }
- irc::spacesepstream PrivList(tag->getString("privs"));
- while (PrivList.GetToken(mypriv))
- {
- AllowedPrivs.insert(mypriv);
- }
+ AllowedOperCommands.AddList(tag->getString("commands"));
+ AllowedPrivs.AddList(tag->getString("privs"));
std::string modes = tag->getString("usermodes");
for (std::string::const_iterator c = modes.begin(); c != modes.end(); ++c)