diff options
author | Attila Molnar <attilamolnar@hush.com> | 2014-02-15 14:38:24 +0100 |
---|---|---|
committer | Attila Molnar <attilamolnar@hush.com> | 2014-02-15 14:38:24 +0100 |
commit | 0556720b559d7ec5d8badacf0ac9b11e9c864847 (patch) | |
tree | 0435a0e5b8e722fe35afb3f820f804bec8c0c7b2 /include | |
parent | 88baaf9e68efd9824b906a69320053734d408e14 (diff) |
Add ParamModeBase and ParamMode, change all parameter modes to inherit from ParamMode
- Type of the extension used to store data is a template parameter
- The extension is automatically unset when the mode is unset
- Handlers inheriting from ParamMode have to provide OnSet() and SerializeParam(); may optionally provide OnUnset()
- Transparently handle the case when OnSet() modifies the mode parameter
- Remove Channel::custom_mode_params map; ask the mode handlers to serialize their parameters instead
Diffstat (limited to 'include')
-rw-r--r-- | include/builtinmodes.h | 9 | ||||
-rw-r--r-- | include/channels.h | 31 | ||||
-rw-r--r-- | include/extensible.h | 1 | ||||
-rw-r--r-- | include/mode.h | 10 | ||||
-rw-r--r-- | include/parammode.h | 75 | ||||
-rw-r--r-- | include/typedefs.h | 4 |
6 files changed, 101 insertions, 29 deletions
diff --git a/include/builtinmodes.h b/include/builtinmodes.h index ce73a7817..6aab727cc 100644 --- a/include/builtinmodes.h +++ b/include/builtinmodes.h @@ -47,21 +47,24 @@ class ModeChannelInviteOnly : public SimpleChannelModeHandler /** Channel mode +k */ -class ModeChannelKey : public ModeHandler +class ModeChannelKey : public ParamMode<ModeChannelKey, LocalStringExt> { public: ModeChannelKey(); ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding); + void SerializeParam(Channel* chan, const std::string* key, std::string& out); + ModeAction OnSet(User* source, Channel* chan, std::string& param); }; /** Channel mode +l */ -class ModeChannelLimit : public ParamChannelModeHandler +class ModeChannelLimit : public ParamMode<ModeChannelLimit, LocalIntExt> { public: ModeChannelLimit(); - bool ParamValidate(std::string& parameter); bool ResolveModeConflict(std::string &their_param, const std::string &our_param, Channel* channel); + void SerializeParam(Channel* chan, intptr_t n, std::string& out); + ModeAction OnSet(User* source, Channel* channel, std::string& parameter); }; /** Channel mode +m diff --git a/include/channels.h b/include/channels.h index 9b018b23e..ba2018e97 100644 --- a/include/channels.h +++ b/include/channels.h @@ -24,6 +24,7 @@ #include "membership.h" #include "mode.h" +#include "parammode.h" /** Holds an entry for a ban list, exemption list, or invite list. * This class contains a single element in a channel list, such as a banlist. @@ -47,11 +48,6 @@ class CoreExport Channel : public Extensible, public InviteBase<Channel> */ std::bitset<64> modes; - /** Parameters for custom modes. - * One for each custom mode letter. - */ - CustomModeList custom_mode_params; - /** Remove the given membership from the channel's internal map of * memberships and destroy the Membership object. * This function does not remove the channel from User::chanlist. @@ -113,13 +109,6 @@ class CoreExport Channel : public Extensible, public InviteBase<Channel> */ void SetMode(ModeHandler* mode, bool value); - /** Sets or unsets a custom mode in the channels info - * @param mode The mode character to set or unset - * @param parameter The parameter string to associate with this mode character. - * If it is empty, the mode is unset; if it is nonempty, the mode is set. - */ - void SetModeParam(ModeHandler* mode, const std::string& parameter); - /** Returns true if a mode is set on a channel * @param mode The mode character you wish to query * @return True if the custom mode is set, false if otherwise @@ -140,6 +129,7 @@ class CoreExport Channel : public Extensible, public InviteBase<Channel> */ std::string GetModeParameter(ModeHandler* mode); std::string GetModeParameter(ChanModeReference& mode); + std::string GetModeParameter(ParamModeBase* pm); /** Sets the channel topic. * @param user The user setting the topic. @@ -344,6 +334,23 @@ inline std::string Channel::GetModeParameter(ChanModeReference& mode) return GetModeParameter(*mode); } +inline std::string Channel::GetModeParameter(ModeHandler* mh) +{ + std::string out; + ParamModeBase* pm = mh->IsParameterMode(); + if (pm && this->IsModeSet(pm)) + pm->GetParameter(this, out); + return out; +} + +inline std::string Channel::GetModeParameter(ParamModeBase* pm) +{ + std::string out; + if (this->IsModeSet(pm)) + pm->GetParameter(this, out); + return out; +} + inline bool Channel::IsModeSet(ChanModeReference& mode) { if (!mode) diff --git a/include/extensible.h b/include/extensible.h index a0544bba1..0e1afdbdf 100644 --- a/include/extensible.h +++ b/include/extensible.h @@ -184,6 +184,7 @@ class CoreExport LocalIntExt : public LocalExtItem std::string serialize(SerializeFormat format, const Extensible* container, void* item) const; intptr_t get(const Extensible* container) const; intptr_t set(Extensible* container, intptr_t value); + void unset(Extensible* container) { set(container, 0); } void free(void* item); }; diff --git a/include/mode.h b/include/mode.h index dd5334c66..3193af65f 100644 --- a/include/mode.h +++ b/include/mode.h @@ -409,16 +409,6 @@ class CoreExport SimpleChannelModeHandler : public ModeHandler virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding); }; -class CoreExport ParamChannelModeHandler : public ModeHandler -{ - public: - ParamChannelModeHandler(Module* Creator, const std::string& Name, char modeletter) - : ModeHandler(Creator, Name, modeletter, PARAM_SETONLY, MODETYPE_CHANNEL) {} - virtual ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string ¶meter, bool adding); - /** Validate the parameter - you may change the value to normalize it. Return true if it is valid. */ - virtual bool ParamValidate(std::string& parameter); -}; - /** * The ModeWatcher class can be used to alter the behaviour of a mode implemented * by the core or by another module. To use ModeWatcher, derive a class from it, diff --git a/include/parammode.h b/include/parammode.h new file mode 100644 index 000000000..b0005262e --- /dev/null +++ b/include/parammode.h @@ -0,0 +1,75 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2014 Attila Molnar <attilamolnar@hush.com> + * + * 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 <http://www.gnu.org/licenses/>. + */ + + +#pragma once + +class CoreExport ParamModeBase : public ModeHandler +{ + private: + virtual void OnUnsetInternal(User* source, Channel* chan) = 0; + + public: + ParamModeBase(Module* Creator, const std::string& Name, char modeletter, ParamSpec ps) + : ModeHandler(Creator, Name, modeletter, ps, MODETYPE_CHANNEL, MC_PARAM) { } + + ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string& param, bool adding) CXX11_OVERRIDE; + + // Does nothing by default + void OnUnset(User* source, Channel* chan) { } + virtual ModeAction OnSet(User* source, Channel* chan, std::string& param) = 0; + virtual void GetParameter(Channel* chan, std::string& out) = 0; +}; + +/** Defines a parameter mode + * T = Child class + * ExtItemT = Type of the extension item used to store the parameter + * + * When unsetting the mode, the extension is automatically unset. + */ +template <typename T, typename ExtItemT> +class ParamMode : public ParamModeBase +{ + public: + ExtItemT ext; + + /** + * @param Creator Module handling this mode + * @param Name The internal name of this mode + * @param modeletter The mode letter of this mode + * @param ps The parameter type of this mode, one of ParamSpec + */ + ParamMode(Module* Creator, const std::string& Name, char modeletter, ParamSpec ps = PARAM_SETONLY) + : ParamModeBase(Creator, Name, modeletter, ps) + , ext("parammode_" + Name, Creator) + { + } + + void OnUnsetInternal(User* source, Channel* chan) CXX11_OVERRIDE + { + T* mh = static_cast<T*>(this); + mh->OnUnset(source, chan); + ext.unset(chan); + } + + void GetParameter(Channel* chan, std::string& out) CXX11_OVERRIDE + { + T* mh = static_cast<T*>(this); + mh->SerializeParam(chan, ext.get(chan), out); + } +}; diff --git a/include/typedefs.h b/include/typedefs.h index 77a45ce4e..336084c55 100644 --- a/include/typedefs.h +++ b/include/typedefs.h @@ -77,10 +77,6 @@ typedef UserChanList::iterator UCListIter; */ typedef std::vector<Membership*> IncludeChanList; -/** A list of custom modes parameters on a channel - */ -typedef std::map<char,std::string> CustomModeList; - /** A cached text file stored with its contents as lines */ typedef std::vector<std::string> file_cache; |