]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/helperfuncs.cpp
Remove deprecated config checker and make <die> actually useful.
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
index 7e992c4a6dc7898318b2302f454f637c47fd71e1..6830612c70bedbc1bf0ee473aef6673d7d18d84e 100644 (file)
@@ -101,7 +101,7 @@ bool InspIRCd::IsValidMask(const std::string &mask)
        if (exclamation != 1 || atsign != 1)
                return false;
 
-       if (mask.length() > 250)
+       if (mask.length() > ServerInstance->Config->Limits.GetMaxMask())
                return false;
 
        return true;
@@ -146,19 +146,20 @@ void InspIRCd::ProcessColors(file_cache& input)
        {
                std::string character;
                std::string replace;
-               special_chars(const std::string &c, const std::string &r) : character(c), replace(r) { }
-       }
-
-       special[] = {
-               special_chars("\\002", "\002"),  // Bold
-               special_chars("\\037", "\037"),  // underline
-               special_chars("\\003", "\003"),  // Color
-               special_chars("\\017", "\017"), // Stop colors
-               special_chars("\\u", "\037"),    // Alias for underline
-               special_chars("\\b", "\002"),    // Alias for Bold
-               special_chars("\\x", "\017"),    // Alias for stop
-               special_chars("\\c", "\003"),    // Alias for color
-               special_chars("", "")
+               special_chars(const std::string& c, const std::string& r)
+                       : character(c)
+                       , replace(r)
+               {
+               }
+       } special[] = {
+               special_chars("\\b", "\x02"), // Bold
+               special_chars("\\c", "\x03"), // Color
+               special_chars("\\i", "\x1D"), // Italic
+               special_chars("\\m", "\x11"), // Monospace
+               special_chars("\\s", "\x1E"), // Strikethrough
+               special_chars("\\u", "\x1F"), // Underline
+               special_chars("\\x", "\x0F"), // Reset
+               special_chars("\\", "")
        };
 
        for(file_cache::iterator it = input.begin(), it_end = input.end(); it != it_end; it++)
@@ -193,7 +194,7 @@ void InspIRCd::ProcessColors(file_cache& input)
 }
 
 /* true for valid channel name, false else */
-bool IsChannelHandler::Call(const std::string& chname)
+bool InspIRCd::DefaultIsChannel(const std::string& chname)
 {
        if (chname.empty() || chname.length() > ServerInstance->Config->Limits.ChanMax)
                return false;
@@ -216,7 +217,7 @@ bool IsChannelHandler::Call(const std::string& chname)
 }
 
 /* true for valid nickname, false else */
-bool IsNickHandler::Call(const std::string& n)
+bool InspIRCd::DefaultIsNick(const std::string& n)
 {
        if (n.empty() || n.length() > ServerInstance->Config->Limits.NickMax)
                return false;
@@ -243,7 +244,7 @@ bool IsNickHandler::Call(const std::string& n)
 }
 
 /* return true for good ident, false else */
-bool IsIdentHandler::Call(const std::string& n)
+bool InspIRCd::DefaultIsIdent(const std::string& n)
 {
        if (n.empty())
                return false;
@@ -266,6 +267,65 @@ bool IsIdentHandler::Call(const std::string& n)
        return true;
 }
 
+bool InspIRCd::IsHost(const std::string& host)
+{
+       // Hostnames must be non-empty and shorter than the maximum hostname length.
+       if (host.empty() || host.length() > ServerInstance->Config->Limits.MaxHost)
+               return false;
+
+       unsigned int numdashes = 0;
+       unsigned int numdots = 0;
+       bool seendot = false;
+       const std::string::const_iterator hostend = host.end() - 1;
+       for (std::string::const_iterator iter = host.begin(); iter != host.end(); ++iter)
+       {
+               unsigned char chr = static_cast<unsigned char>(*iter);
+
+               // If the current character is a label separator.
+               if (chr == '.')
+               {
+                       numdots++;
+
+                       // Consecutive separators are not allowed and dashes can not exist at the start or end
+                       // of labels and separators must only exist between labels.
+                       if (seendot || numdashes || iter == host.begin() || iter == hostend)
+                               return false;
+
+                       seendot = true;
+                       continue;
+               }
+
+               // If this point is reached then the character is not a dot.
+               seendot = false;
+
+               // If the current character is a dash.
+               if (chr == '-')
+               {
+                       // Consecutive separators are not allowed and dashes can not exist at the start or end
+                       // of labels and separators must only exist between labels.
+                       if (seendot || numdashes >= 2 || iter == host.begin() || iter == hostend)
+                               return false;
+
+                       numdashes += 1;
+                       continue;
+               }
+
+               // If this point is reached then the character is not a dash.
+               numdashes = 0;
+
+               // Alphanumeric characters are allowed at any position.
+               if ((chr >= '0' && chr <= '9') || (chr >= 'A' && chr <= 'Z') || (chr >= 'a' && chr <= 'z'))
+                       continue;
+
+               return false;
+       }
+
+       // Whilst simple hostnames (e.g. localhost) are valid we do not allow the server to use
+       // them to prevent issues with clients that differentiate between short client and server
+       // prefixes by checking whether the nickname contains a dot.
+       return numdots;
+}
+
 bool InspIRCd::IsSID(const std::string &str)
 {
        /* Returns true if the string given is exactly 3 characters long,
@@ -331,7 +391,7 @@ unsigned long InspIRCd::Duration(const std::string &str)
        return total + subtotal;
 }
 
-const char* InspIRCd::Format(va_list &vaList, const char* formatString)
+std::string InspIRCd::Format(va_list& vaList, const char* formatString)
 {
        static std::vector<char> formatBuffer(1024);
 
@@ -351,12 +411,12 @@ const char* InspIRCd::Format(va_list &vaList, const char* formatString)
                formatBuffer.resize(formatBuffer.size() * 2);
        }
 
-       return &formatBuffer[0];
+       return std::string(&formatBuffer[0]);
 }
 
-const char* InspIRCd::Format(const char* formatString, ...)
+std::string InspIRCd::Format(const char* formatString, ...)
 {
-       const char* ret;
+       std::string ret;
        VAFORMAT(ret, formatString, formatString);
        return ret;
 }
@@ -393,13 +453,13 @@ std::string InspIRCd::TimeString(time_t curtime, const char* format, bool utc)
        return buffer;
 }
 
-std::string InspIRCd::GenRandomStr(int length, bool printable)
+std::string InspIRCd::GenRandomStr(unsigned int length, bool printable)
 {
        char* buf = new char[length];
        GenRandom(buf, length);
        std::string rv;
        rv.resize(length);
-       for(int i=0; i < length; i++)
+       for(size_t i = 0; i < length; i++)
                rv[i] = printable ? 0x3F + (buf[i] & 0x3F) : buf[i];
        delete[] buf;
        return rv;
@@ -415,10 +475,13 @@ unsigned long InspIRCd::GenRandomInt(unsigned long max)
 }
 
 // This is overridden by a higher-quality algorithm when SSL support is loaded
-void GenRandomHandler::Call(char *output, size_t max)
+void InspIRCd::DefaultGenRandom(char* output, size_t max)
 {
-       for(unsigned int i=0; i < max; i++)
-#ifdef _WIN32
+#if defined HAS_ARC4RANDOM_BUF
+       arc4random_buf(output, max);
+#else
+       for (unsigned int i = 0; i < max; ++i)
+# ifdef _WIN32
        {
                unsigned int uTemp;
                if(rand_s(&uTemp) != 0)
@@ -426,7 +489,8 @@ void GenRandomHandler::Call(char *output, size_t max)
                else
                        output[i] = uTemp;
        }
-#else
+# else
                output[i] = random();
+# endif
 #endif
 }