summaryrefslogtreecommitdiff
path: root/src/modules/m_chanhistory.cpp
diff options
context:
space:
mode:
authorAttila Molnar <attilamolnar@hush.com>2014-02-15 14:38:24 +0100
committerAttila Molnar <attilamolnar@hush.com>2014-02-15 14:38:24 +0100
commit0556720b559d7ec5d8badacf0ac9b11e9c864847 (patch)
tree0435a0e5b8e722fe35afb3f820f804bec8c0c7b2 /src/modules/m_chanhistory.cpp
parent88baaf9e68efd9824b906a69320053734d408e14 (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 'src/modules/m_chanhistory.cpp')
-rw-r--r--src/modules/m_chanhistory.cpp83
1 files changed, 41 insertions, 42 deletions
diff --git a/src/modules/m_chanhistory.cpp b/src/modules/m_chanhistory.cpp
index 24c6237b3..f6e7ea40e 100644
--- a/src/modules/m_chanhistory.cpp
+++ b/src/modules/m_chanhistory.cpp
@@ -30,10 +30,13 @@ struct HistoryList
{
std::deque<HistoryItem> lines;
unsigned int maxlen, maxtime;
- HistoryList(unsigned int len, unsigned int time) : maxlen(len), maxtime(time) {}
+ std::string param;
+
+ HistoryList(unsigned int len, unsigned int time, const std::string& oparam)
+ : maxlen(len), maxtime(time), param(oparam) { }
};
-class HistoryMode : public ModeHandler
+class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
{
bool IsValidDuration(const std::string& duration)
{
@@ -50,57 +53,53 @@ class HistoryMode : public ModeHandler
}
public:
- SimpleExtItem<HistoryList> ext;
unsigned int maxlines;
- HistoryMode(Module* Creator) : ModeHandler(Creator, "history", 'H', PARAM_SETONLY, MODETYPE_CHANNEL),
- ext("history", Creator) { }
+ HistoryMode(Module* Creator)
+ : ParamMode<HistoryMode, SimpleExtItem<HistoryList> >(Creator, "history", 'H')
+ {
+ }
- ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
+ ModeAction OnSet(User* source, Channel* channel, std::string& parameter)
{
- if (adding)
+ std::string::size_type colon = parameter.find(':');
+ if (colon == std::string::npos)
+ return MODEACTION_DENY;
+
+ std::string duration = parameter.substr(colon+1);
+ if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
+ return MODEACTION_DENY;
+
+ unsigned int len = ConvToInt(parameter.substr(0, colon));
+ int time = InspIRCd::Duration(duration);
+ if (len == 0 || time < 0)
+ return MODEACTION_DENY;
+ if (len > maxlines && IS_LOCAL(source))
+ return MODEACTION_DENY;
+ if (len > maxlines)
+ len = maxlines;
+
+ HistoryList* history = ext.get(channel);
+ if (history)
{
- std::string::size_type colon = parameter.find(':');
- if (colon == std::string::npos)
- return MODEACTION_DENY;
-
- std::string duration = parameter.substr(colon+1);
- if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
- return MODEACTION_DENY;
-
- unsigned int len = ConvToInt(parameter.substr(0, colon));
- int time = InspIRCd::Duration(duration);
- if (len == 0 || time < 0)
- return MODEACTION_DENY;
- if (len > maxlines && IS_LOCAL(source))
- return MODEACTION_DENY;
- if (len > maxlines)
- len = maxlines;
- if (parameter == channel->GetModeParameter(this))
- return MODEACTION_DENY;
-
- HistoryList* history = ext.get(channel);
- if (history)
- {
- // Shrink the list if the new line number limit is lower than the old one
- if (len < history->lines.size())
- history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
+ // Shrink the list if the new line number limit is lower than the old one
+ if (len < history->lines.size())
+ history->lines.erase(history->lines.begin(), history->lines.begin() + (history->lines.size() - len));
- history->maxlen = len;
- history->maxtime = time;
- }
- else
- {
- ext.set(channel, new HistoryList(len, time));
- }
+ history->maxlen = len;
+ history->maxtime = time;
+ history->param = parameter;
}
else
{
- if (!channel->IsModeSet(this))
- return MODEACTION_DENY;
- ext.unset(channel);
+ ext.set(channel, new HistoryList(len, time, parameter));
}
return MODEACTION_ALLOW;
}
+
+ void SerializeParam(Channel* chan, const HistoryList* history, std::string& out)
+ {
+ out.append(history->param);
+ }
};
class ModuleChanHistory : public Module