]> 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 c29e7d2cc65e6d7bfd28897a2be68b2f4a23640c..6830612c70bedbc1bf0ee473aef6673d7d18d84e 100644 (file)
@@ -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++)
@@ -266,6 +267,65 @@ bool InspIRCd::DefaultIsIdent(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;
 }
@@ -417,8 +477,11 @@ unsigned long InspIRCd::GenRandomInt(unsigned long max)
 // This is overridden by a higher-quality algorithm when SSL support is loaded
 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 InspIRCd::DefaultGenRandom(char* output, size_t max)
                else
                        output[i] = uTemp;
        }
-#else
+# else
                output[i] = random();
+# endif
 #endif
 }