]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/configparser.cpp
Change 2.0 cloak function to use variable-length segments
[user/henk/code/inspircd.git] / src / configparser.cpp
index 1373ae5af3b91d5ec120fc0746117fe6dd49f6f3..6b1b47e6de10f8417c41437e8a5ebe0d97245174 100644 (file)
@@ -2,7 +2,7 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
+ *  InspIRCd: (C) 2002-2010 InspIRCd Development Team
  * See: http://wiki.inspircd.org/Credits
  *
  * This program is free but copyrighted software; see
@@ -18,7 +18,7 @@
 struct Parser
 {
        ParseStack& stack;
-       const int flags;
+       int flags;
        FILE* const file;
        fpos current;
        fpos last_tag;
@@ -76,7 +76,7 @@ struct Parser
                int ch = next();
                while (isspace(ch))
                        ch = next();
-               while (isalnum(ch) || ch == '_')
+               while (isalnum(ch) || ch == '_'|| ch == '-')
                {
                        rv.push_back(ch);
                        ch = next();
@@ -112,7 +112,7 @@ struct Parser
                while (1)
                {
                        ch = next();
-                       if (ch == '&')
+                       if (ch == '&' && (flags & FLAG_USE_XML))
                        {
                                std::string varname;
                                while (1)
@@ -134,6 +134,16 @@ struct Parser
                                        throw CoreException("Undefined XML entity reference '&" + varname + ";'");
                                value.append(var->second);
                        }
+                       else if (ch == '\\' && !(flags & FLAG_USE_XML))
+                       {
+                               int esc = next();
+                               if (esc == 'n')
+                                       value.push_back('\n');
+                               else if (isalpha(esc))
+                                       throw CoreException("Unknown escape character \\" + std::string(1, esc));
+                               else
+                                       value.push_back(esc);
+                       }
                        else if (ch == '"')
                                break;
                        else
@@ -167,14 +177,40 @@ struct Parser
                {
                        stack.DoInclude(tag, flags);
                }
+               else if (name == "files")
+               {
+                       for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
+                       {
+                               stack.DoReadFile(i->first, i->second, flags, false);
+                       }
+               }
+               else if (name == "execfiles")
+               {
+                       for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
+                       {
+                               stack.DoReadFile(i->first, i->second, flags, true);
+                       }
+               }
                else if (name == "define")
                {
+                       if (!(flags & FLAG_USE_XML))
+                               throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
                        std::string varname = tag->getString("name");
                        std::string value = tag->getString("value");
                        if (varname.empty())
                                throw CoreException("Variable definition must include variable name");
                        stack.vars[varname] = value;
                }
+               else if (name == "config")
+               {
+                       std::string format = tag->getString("format");
+                       if (format == "xml")
+                               flags |= FLAG_USE_XML;
+                       else if (format == "compat")
+                               flags &= ~FLAG_USE_XML;
+                       else if (!format.empty())
+                               throw CoreException("Unknown configuration format " + format);
+               }
                else
                {
                        stack.output.insert(std::make_pair(name, tag));
@@ -253,6 +289,29 @@ void ParseStack::DoInclude(ConfigTag* tag, int flags)
        }
 }
 
+void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
+{
+       if (flags & FLAG_NO_INC)
+               throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
+       if (exec && (flags & FLAG_NO_EXEC))
+               throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
+
+       FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(name.c_str(), "r"));
+       if (!file)
+               throw CoreException("Could not read \"" + name + "\" for \"" + key + "\" file");
+
+       file_cache& cache = FilesOutput[key];
+       cache.clear();
+
+       char linebuf[MAXBUF*10];
+       while (fgets(linebuf, sizeof(linebuf), file))
+       {
+               int len = strlen(linebuf);
+               if (len)
+                       cache.push_back(std::string(linebuf, len - 1));
+       }
+}
+
 bool ParseStack::ParseFile(const std::string& name, int flags)
 {
        ServerInstance->Logs->Log("CONFIG", DEBUG, "Reading file %s", name.c_str());
@@ -370,7 +429,14 @@ bool ConfigTag::getBool(const std::string &key, bool def)
        if(!readString(key, result))
                return def;
 
-       return (result == "yes" || result == "true" || result == "1" || result == "on");
+       if (result == "yes" || result == "true" || result == "1" || result == "on")
+               return true;
+       if (result == "no" || result == "false" || result == "0" || result == "off")
+               return false;
+
+       ServerInstance->Logs->Log("CONFIG",DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
+               " is not valid, ignoring");
+       return def;
 }
 
 std::string ConfigTag::getTagLocation()