From 0556720b559d7ec5d8badacf0ac9b11e9c864847 Mon Sep 17 00:00:00 2001 From: Attila Molnar Date: Sat, 15 Feb 2014 14:38:24 +0100 Subject: 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 --- src/modes/cmode_k.cpp | 27 +++++++++++++++++++++++---- src/modes/cmode_l.cpp | 17 +++++++++-------- 2 files changed, 32 insertions(+), 12 deletions(-) (limited to 'src/modes') diff --git a/src/modes/cmode_k.cpp b/src/modes/cmode_k.cpp index e56b26ff1..e14f93a77 100644 --- a/src/modes/cmode_k.cpp +++ b/src/modes/cmode_k.cpp @@ -26,32 +26,51 @@ #include "users.h" #include "builtinmodes.h" -ModeChannelKey::ModeChannelKey() : ModeHandler(NULL, "key", 'k', PARAM_ALWAYS, MODETYPE_CHANNEL) +ModeChannelKey::ModeChannelKey() + : ParamMode(NULL, "key", 'k', PARAM_ALWAYS) { } ModeAction ModeChannelKey::OnModeChange(User* source, User*, Channel* channel, std::string ¶meter, bool adding) { - bool exists = channel->IsModeSet(this); + const std::string* key = ext.get(channel); + bool exists = (key != NULL); if (IS_LOCAL(source)) { if (exists == adding) return MODEACTION_DENY; - if (exists && (parameter != channel->GetModeParameter(this))) + if (exists && (parameter != *key)) { /* Key is currently set and the correct key wasnt given */ return MODEACTION_DENY; } } else { - if (exists && adding && parameter == channel->GetModeParameter(this)) + if (exists && adding && parameter == *key) { /* no-op, don't show */ return MODEACTION_DENY; } } + channel->SetMode(this, adding); if (adding) + { parameter = parameter.substr(0, 32); + ext.set(channel, parameter); + } + else + ext.unset(channel); return MODEACTION_ALLOW; } + +void ModeChannelKey::SerializeParam(Channel* chan, const std::string* key, std::string& out) +{ + out += *key; +} + +ModeAction ModeChannelKey::OnSet(User* source, Channel* chan, std::string& param) +{ + // Dummy function, never called + return MODEACTION_DENY; +} diff --git a/src/modes/cmode_l.cpp b/src/modes/cmode_l.cpp index f057a75e6..128854b50 100644 --- a/src/modes/cmode_l.cpp +++ b/src/modes/cmode_l.cpp @@ -25,7 +25,8 @@ #include "users.h" #include "builtinmodes.h" -ModeChannelLimit::ModeChannelLimit() : ParamChannelModeHandler(NULL, "limit", 'l') +ModeChannelLimit::ModeChannelLimit() + : ParamMode(NULL, "limit", 'l') { } @@ -35,13 +36,13 @@ bool ModeChannelLimit::ResolveModeConflict(std::string &their_param, const std:: return (atoi(their_param.c_str()) < atoi(our_param.c_str())); } -bool ModeChannelLimit::ParamValidate(std::string ¶meter) +ModeAction ModeChannelLimit::OnSet(User* user, Channel* chan, std::string& parameter) { - int limit = atoi(parameter.c_str()); - - if (limit < 0) - return false; + ext.set(chan, ConvToInt(parameter)); + return MODEACTION_ALLOW; +} - parameter = ConvToStr(limit); - return true; +void ModeChannelLimit::SerializeParam(Channel* chan, intptr_t n, std::string& out) +{ + out += ConvToStr(n); } -- cgit v1.2.3