Move IsValidDuration into the core.

This commit is contained in:
Peter Powell 2018-11-24 23:27:16 +00:00
parent 6596fb5ffe
commit cc5aff3e5a
4 changed files with 47 additions and 44 deletions

View File

@ -147,30 +147,3 @@ class CoreExport CommandParser
*/
static std::string TranslateUIDs(const std::vector<TranslateType>& to, const CommandBase::Params& source, bool prefix_final = false, CommandBase* custom_translator = NULL);
};
/** A lookup table of values for multiplier characters used by
* InspIRCd::Duration(). In this lookup table, the indexes for
* the ascii values 'm' and 'M' have the value '60', the indexes
* for the ascii values 'D' and 'd' have a value of '86400', etc.
*/
const int duration_multi[] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 86400, 1, 1, 1, 3600,
1, 1, 1, 1, 60, 1, 1, 1, 1, 1, 1, 1, 1, 1,
604800, 1, 31557600, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 86400, 1, 1, 1, 3600, 1, 1, 1, 1, 60,
1, 1, 1, 1, 1, 1, 1, 1, 1, 604800, 1, 31557600,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};

View File

@ -502,6 +502,12 @@ class CoreExport InspIRCd
*/
static unsigned long Duration(const std::string& str);
/** Determines whether a string contains a valid duration.
* @param str A string containing a time in the form 1y2w3d4h6m5s
* @return True if the string is a valid duration; otherwise, false.
*/
static bool IsValidDuration(const std::string& str);
/** Attempt to compare a password to a string from the config file.
* This will be passed to handling modules which will compare the data
* against possible hashed equivalents in the input string.

View File

@ -349,9 +349,33 @@ void InspIRCd::CheckRoot()
#endif
}
/** Refactored by Brain, Jun 2009. Much faster with some clever O(1) array
* lookups and pointer maths.
/** A lookup table of values for multiplier characters used by
* InspIRCd::Duration(). In this lookup table, the indexes for
* the ascii values 'm' and 'M' have the value '60', the indexes
* for the ascii values 'D' and 'd' have a value of '86400', etc.
*/
static const int duration_multi[] =
{
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 86400, 1, 1, 1, 3600,
1, 1, 1, 1, 60, 1, 1, 1, 1, 1, 1, 1, 1, 1,
604800, 1, 31557600, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 86400, 1, 1, 1, 3600, 1, 1, 1, 1, 60,
1, 1, 1, 1, 1, 1, 1, 1, 1, 604800, 1, 31557600,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
};
unsigned long InspIRCd::Duration(const std::string &str)
{
unsigned char multiplier = 0;
@ -392,6 +416,20 @@ unsigned long InspIRCd::Duration(const std::string &str)
return total + subtotal;
}
bool InspIRCd::IsValidDuration(const std::string& duration)
{
for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
{
unsigned char c = *i;
if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
continue;
if (duration_multi[c] == 1)
return false;
}
return true;
}
std::string InspIRCd::Format(va_list& vaList, const char* formatString)
{
static std::vector<char> formatBuffer(1024);

View File

@ -48,20 +48,6 @@ struct HistoryList
class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
{
bool IsValidDuration(const std::string& duration)
{
for (std::string::const_iterator i = duration.begin(); i != duration.end(); ++i)
{
unsigned char c = *i;
if (((c >= '0') && (c <= '9')) || (c == 's') || (c == 'S'))
continue;
if (duration_multi[c] == 1)
return false;
}
return true;
}
public:
unsigned int maxlines;
HistoryMode(Module* Creator)
@ -79,7 +65,7 @@ class HistoryMode : public ParamMode<HistoryMode, SimpleExtItem<HistoryList> >
}
std::string duration(parameter, colon+1);
if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!IsValidDuration(duration))))
if ((IS_LOCAL(source)) && ((duration.length() > 10) || (!InspIRCd::IsValidDuration(duration))))
{
source->WriteNumeric(Numerics::InvalidModeParameter(channel, this, parameter));
return MODEACTION_DENY;