From 43f6c20e40fbd770b2f4446bcfe46c940a7f2c88 Mon Sep 17 00:00:00 2001 From: Sadie Powell Date: Sun, 12 Apr 2020 04:26:27 +0100 Subject: Move the oper and snomask modes to core_oper. --- src/coremods/core_oper/core_oper.cpp | 5 ++ src/coremods/core_oper/core_oper.h | 30 ++++++++ src/coremods/core_oper/umode_o.cpp | 56 ++++++++++++++ src/coremods/core_oper/umode_s.cpp | 145 +++++++++++++++++++++++++++++++++++ src/coremods/core_user/core_user.cpp | 4 - src/coremods/core_user/core_user.h | 32 -------- src/coremods/core_user/umode_o.cpp | 56 -------------- src/coremods/core_user/umode_s.cpp | 145 ----------------------------------- 8 files changed, 236 insertions(+), 237 deletions(-) create mode 100644 src/coremods/core_oper/umode_o.cpp create mode 100644 src/coremods/core_oper/umode_s.cpp delete mode 100644 src/coremods/core_user/umode_o.cpp delete mode 100644 src/coremods/core_user/umode_s.cpp (limited to 'src') diff --git a/src/coremods/core_oper/core_oper.cpp b/src/coremods/core_oper/core_oper.cpp index 2f6abea29..ad0309500 100644 --- a/src/coremods/core_oper/core_oper.cpp +++ b/src/coremods/core_oper/core_oper.cpp @@ -24,11 +24,14 @@ class CoreModOper : public Module { + private: CommandDie cmddie; CommandKill cmdkill; CommandOper cmdoper; CommandRehash cmdrehash; CommandRestart cmdrestart; + ModeUserOperator operatormode; + ModeUserServerNoticeMask snomaskmode; public: CoreModOper() @@ -38,6 +41,8 @@ class CoreModOper : public Module , cmdoper(this) , cmdrehash(this) , cmdrestart(this) + , operatormode(this) + , snomaskmode(this) { } diff --git a/src/coremods/core_oper/core_oper.h b/src/coremods/core_oper/core_oper.h index a89dce7ed..b0df7e738 100644 --- a/src/coremods/core_oper/core_oper.h +++ b/src/coremods/core_oper/core_oper.h @@ -129,3 +129,33 @@ class CommandRestart : public Command */ CmdResult Handle(User* user, const Params& parameters) override; }; + +class ModeUserServerNoticeMask : public ModeHandler +{ + private: + /** Process a snomask modifier string, e.g. +abc-de + * @param user The target user + * @param input A sequence of notice mask characters + * @return The cleaned mode sequence which can be output, + * e.g. in the above example if masks c and e are not + * valid, this function will return +ab-d + */ + std::string ProcessNoticeMasks(User* user, const std::string& input); + + public: + ModeUserServerNoticeMask(Module* Creator); + ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) override; + + /** Create a displayable mode string of the snomasks set on a given user + * @param user The user whose notice masks to format + * @return The notice mask character sequence + */ + std::string GetUserParameter(const User* user) const override; +}; + +class ModeUserOperator : public ModeHandler +{ + public: + ModeUserOperator(Module* Creator); + ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding) override; +}; diff --git a/src/coremods/core_oper/umode_o.cpp b/src/coremods/core_oper/umode_o.cpp new file mode 100644 index 000000000..d9d2a6e1c --- /dev/null +++ b/src/coremods/core_oper/umode_o.cpp @@ -0,0 +1,56 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2017 Sadie Powell + * Copyright (C) 2014 Attila Molnar + * Copyright (C) 2012 Robby + * Copyright (C) 2009 Daniel De Graaf + * Copyright (C) 2007 Robin Burchell + * Copyright (C) 2007 Dennis Friis + * Copyright (C) 2006, 2010 Craig Edwards + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "inspircd.h" +#include "core_oper.h" + +ModeUserOperator::ModeUserOperator(Module* Creator) + : ModeHandler(Creator, "oper", 'o', PARAM_NONE, MODETYPE_USER) +{ + oper = true; +} + +ModeAction ModeUserOperator::OnModeChange(User* source, User* dest, Channel*, std::string&, bool adding) +{ + /* Only opers can execute this class at all */ + if (!source->server->IsULine() && !source->IsOper()) + return MODEACTION_DENY; + + /* Not even opers can GIVE the +o mode, only take it away */ + if (adding) + return MODEACTION_DENY; + + /* Set the bitfields. + * Note that oper status is only given in User::Oper() + * NOT here. It is impossible to directly set +o without + * verifying as an oper and getting an opertype assigned + * to your User! + */ + char snomask = IS_LOCAL(dest) ? 'o' : 'O'; + ServerInstance->SNO.WriteToSnoMask(snomask, "User %s de-opered (by %s)", dest->nick.c_str(), source->nick.c_str()); + dest->UnOper(); + + return MODEACTION_ALLOW; +} diff --git a/src/coremods/core_oper/umode_s.cpp b/src/coremods/core_oper/umode_s.cpp new file mode 100644 index 000000000..e00f3bb7b --- /dev/null +++ b/src/coremods/core_oper/umode_s.cpp @@ -0,0 +1,145 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2017, 2020 Sadie Powell + * Copyright (C) 2013, 2016 Attila Molnar + * Copyright (C) 2012, 2019 Robby + * Copyright (C) 2009 Daniel De Graaf + * Copyright (C) 2008, 2010 Craig Edwards + * Copyright (C) 2007 Dennis Friis + * Copyright (C) 2006 Robin Burchell + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + + +#include "inspircd.h" +#include "core_oper.h" + +ModeUserServerNoticeMask::ModeUserServerNoticeMask(Module* Creator) + : ModeHandler(Creator, "snomask", 's', PARAM_SETONLY, MODETYPE_USER) +{ + oper = true; + syntax = "(+|-)|*"; +} + +ModeAction ModeUserServerNoticeMask::OnModeChange(User* source, User* dest, Channel*, std::string ¶meter, bool adding) +{ + if (adding) + { + dest->SetMode(this, true); + // Process the parameter (remove chars we don't understand, remove redundant chars, etc.) + parameter = ProcessNoticeMasks(dest, parameter); + return MODEACTION_ALLOW; + } + else + { + if (dest->IsModeSet(this)) + { + dest->SetMode(this, false); + dest->snomasks.reset(); + return MODEACTION_ALLOW; + } + } + + // Mode not set and trying to unset, deny + return MODEACTION_DENY; +} + +std::string ModeUserServerNoticeMask::GetUserParameter(const User* user) const +{ + std::string ret; + if (!user->IsModeSet(this)) + return ret; + + ret.push_back('+'); + for (unsigned char n = 0; n < 64; n++) + { + if (user->snomasks[n]) + ret.push_back(n + 'A'); + } + return ret; +} + +std::string ModeUserServerNoticeMask::ProcessNoticeMasks(User* user, const std::string& input) +{ + bool adding = true; + std::bitset<64> curr = user->snomasks; + + for (std::string::const_iterator i = input.begin(); i != input.end(); ++i) + { + switch (*i) + { + case '+': + adding = true; + break; + case '-': + adding = false; + break; + case '*': + for (size_t j = 0; j < 64; j++) + { + if (ServerInstance->SNO.IsSnomaskUsable(j+'A')) + curr[j] = adding; + } + break; + default: + // For local users check whether the given snomask is valid and enabled - IsSnomaskUsable() tests both. + // For remote users accept what we were told, unless the snomask char is not a letter. + if (IS_LOCAL(user)) + { + if (!ServerInstance->SNO.IsSnomaskUsable(*i)) + { + user->WriteNumeric(ERR_UNKNOWNSNOMASK, *i, "is an unknown snomask character"); + continue; + } + } + else if (!(((*i >= 'a') && (*i <= 'z')) || ((*i >= 'A') && (*i <= 'Z')))) + continue; + + size_t index = ((*i) - 'A'); + curr[index] = adding; + break; + } + } + + std::string plus = "+"; + std::string minus = "-"; + + // Apply changes and construct two strings consisting of the newly added and the removed snomask chars + for (size_t i = 0; i < 64; i++) + { + bool isset = curr[i]; + if (user->snomasks[i] != isset) + { + user->snomasks[i] = isset; + std::string& appendhere = (isset ? plus : minus); + appendhere.push_back(i+'A'); + } + } + + // Create the final string that will be shown to the user and sent to servers + // Form: "+ABc-de" + std::string output; + if (plus.length() > 1) + output = plus; + + if (minus.length() > 1) + output += minus; + + // Unset the snomask usermode itself if every snomask was unset + if (user->snomasks.none()) + user->SetMode(this, false); + + return output; +} diff --git a/src/coremods/core_user/core_user.cpp b/src/coremods/core_user/core_user.cpp index db2d34915..7ca461052 100644 --- a/src/coremods/core_user/core_user.cpp +++ b/src/coremods/core_user/core_user.cpp @@ -151,8 +151,6 @@ class CoreModUser : public Module CommandIson cmdison; CommandUserhost cmduserhost; SimpleUserModeHandler invisiblemode; - ModeUserOperator operatormode; - ModeUserServerNoticeMask snomaskmode; public: CoreModUser() @@ -168,8 +166,6 @@ class CoreModUser : public Module , cmdison(this) , cmduserhost(this) , invisiblemode(this, "invisible", 'i') - , operatormode(this) - , snomaskmode(this) { } diff --git a/src/coremods/core_user/core_user.h b/src/coremods/core_user/core_user.h index 9f0fe5b4a..ef7e4d79e 100644 --- a/src/coremods/core_user/core_user.h +++ b/src/coremods/core_user/core_user.h @@ -201,35 +201,3 @@ class CommandUserhost : public Command CmdResult Handle(User* user, const Params& parameters) override; }; -/** User mode +s - */ -class ModeUserServerNoticeMask : public ModeHandler -{ - /** Process a snomask modifier string, e.g. +abc-de - * @param user The target user - * @param input A sequence of notice mask characters - * @return The cleaned mode sequence which can be output, - * e.g. in the above example if masks c and e are not - * valid, this function will return +ab-d - */ - std::string ProcessNoticeMasks(User* user, const std::string& input); - - public: - ModeUserServerNoticeMask(Module* Creator); - ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& parameter, bool adding) override; - - /** Create a displayable mode string of the snomasks set on a given user - * @param user The user whose notice masks to format - * @return The notice mask character sequence - */ - std::string GetUserParameter(const User* user) const override; -}; - -/** User mode +o - */ -class ModeUserOperator : public ModeHandler -{ - public: - ModeUserOperator(Module* Creator); - ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding) override; -}; diff --git a/src/coremods/core_user/umode_o.cpp b/src/coremods/core_user/umode_o.cpp deleted file mode 100644 index 47e11fbce..000000000 --- a/src/coremods/core_user/umode_o.cpp +++ /dev/null @@ -1,56 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2017 Sadie Powell - * Copyright (C) 2014 Attila Molnar - * Copyright (C) 2012 Robby - * Copyright (C) 2009 Daniel De Graaf - * Copyright (C) 2007 Robin Burchell - * Copyright (C) 2007 Dennis Friis - * Copyright (C) 2006, 2010 Craig Edwards - * - * This file is part of InspIRCd. InspIRCd is free software: you can - * redistribute it and/or modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation, version 2. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include "inspircd.h" -#include "core_user.h" - -ModeUserOperator::ModeUserOperator(Module* Creator) - : ModeHandler(Creator, "oper", 'o', PARAM_NONE, MODETYPE_USER) -{ - oper = true; -} - -ModeAction ModeUserOperator::OnModeChange(User* source, User* dest, Channel*, std::string&, bool adding) -{ - /* Only opers can execute this class at all */ - if (!source->server->IsULine() && !source->IsOper()) - return MODEACTION_DENY; - - /* Not even opers can GIVE the +o mode, only take it away */ - if (adding) - return MODEACTION_DENY; - - /* Set the bitfields. - * Note that oper status is only given in User::Oper() - * NOT here. It is impossible to directly set +o without - * verifying as an oper and getting an opertype assigned - * to your User! - */ - char snomask = IS_LOCAL(dest) ? 'o' : 'O'; - ServerInstance->SNO.WriteToSnoMask(snomask, "User %s de-opered (by %s)", dest->nick.c_str(), source->nick.c_str()); - dest->UnOper(); - - return MODEACTION_ALLOW; -} diff --git a/src/coremods/core_user/umode_s.cpp b/src/coremods/core_user/umode_s.cpp deleted file mode 100644 index 8d59bde86..000000000 --- a/src/coremods/core_user/umode_s.cpp +++ /dev/null @@ -1,145 +0,0 @@ -/* - * InspIRCd -- Internet Relay Chat Daemon - * - * Copyright (C) 2017, 2020 Sadie Powell - * Copyright (C) 2013, 2016 Attila Molnar - * Copyright (C) 2012, 2019 Robby - * Copyright (C) 2009 Daniel De Graaf - * Copyright (C) 2008, 2010 Craig Edwards - * Copyright (C) 2007 Dennis Friis - * Copyright (C) 2006 Robin Burchell - * - * This file is part of InspIRCd. InspIRCd is free software: you can - * redistribute it and/or modify it under the terms of the GNU General Public - * License as published by the Free Software Foundation, version 2. - * - * This program is distributed in the hope that it will be useful, but WITHOUT - * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS - * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more - * details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - - -#include "inspircd.h" -#include "core_user.h" - -ModeUserServerNoticeMask::ModeUserServerNoticeMask(Module* Creator) - : ModeHandler(Creator, "snomask", 's', PARAM_SETONLY, MODETYPE_USER) -{ - oper = true; - syntax = "(+|-)|*"; -} - -ModeAction ModeUserServerNoticeMask::OnModeChange(User* source, User* dest, Channel*, std::string ¶meter, bool adding) -{ - if (adding) - { - dest->SetMode(this, true); - // Process the parameter (remove chars we don't understand, remove redundant chars, etc.) - parameter = ProcessNoticeMasks(dest, parameter); - return MODEACTION_ALLOW; - } - else - { - if (dest->IsModeSet(this)) - { - dest->SetMode(this, false); - dest->snomasks.reset(); - return MODEACTION_ALLOW; - } - } - - // Mode not set and trying to unset, deny - return MODEACTION_DENY; -} - -std::string ModeUserServerNoticeMask::GetUserParameter(const User* user) const -{ - std::string ret; - if (!user->IsModeSet(this)) - return ret; - - ret.push_back('+'); - for (unsigned char n = 0; n < 64; n++) - { - if (user->snomasks[n]) - ret.push_back(n + 'A'); - } - return ret; -} - -std::string ModeUserServerNoticeMask::ProcessNoticeMasks(User* user, const std::string& input) -{ - bool adding = true; - std::bitset<64> curr = user->snomasks; - - for (std::string::const_iterator i = input.begin(); i != input.end(); ++i) - { - switch (*i) - { - case '+': - adding = true; - break; - case '-': - adding = false; - break; - case '*': - for (size_t j = 0; j < 64; j++) - { - if (ServerInstance->SNO.IsSnomaskUsable(j+'A')) - curr[j] = adding; - } - break; - default: - // For local users check whether the given snomask is valid and enabled - IsSnomaskUsable() tests both. - // For remote users accept what we were told, unless the snomask char is not a letter. - if (IS_LOCAL(user)) - { - if (!ServerInstance->SNO.IsSnomaskUsable(*i)) - { - user->WriteNumeric(ERR_UNKNOWNSNOMASK, *i, "is an unknown snomask character"); - continue; - } - } - else if (!(((*i >= 'a') && (*i <= 'z')) || ((*i >= 'A') && (*i <= 'Z')))) - continue; - - size_t index = ((*i) - 'A'); - curr[index] = adding; - break; - } - } - - std::string plus = "+"; - std::string minus = "-"; - - // Apply changes and construct two strings consisting of the newly added and the removed snomask chars - for (size_t i = 0; i < 64; i++) - { - bool isset = curr[i]; - if (user->snomasks[i] != isset) - { - user->snomasks[i] = isset; - std::string& appendhere = (isset ? plus : minus); - appendhere.push_back(i+'A'); - } - } - - // Create the final string that will be shown to the user and sent to servers - // Form: "+ABc-de" - std::string output; - if (plus.length() > 1) - output = plus; - - if (minus.length() > 1) - output += minus; - - // Unset the snomask usermode itself if every snomask was unset - if (user->snomasks.none()) - user->SetMode(this, false); - - return output; -} -- cgit v1.3.1-10-gc9f91