2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
6 * This file is part of InspIRCd. InspIRCd is free software: you can
7 * redistribute it and/or modify it under the terms of the GNU General Public
8 * License as published by the Free Software Foundation, version 2.
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "configparser.h"
31 reference<ConfigTag> tag;
33 std::string mandatory_tag;
35 Parser(ParseStack& me, int myflags, FILE* conf, const std::string& name, const std::string& mandatorytag)
36 : stack(me), flags(myflags), file(conf), current(name), last_tag(name), ungot(-1), mandatory_tag(mandatorytag)
39 int next(bool eof_ok = false)
48 if (ch == EOF && !eof_ok)
50 throw CoreException("Unexpected end-of-file");
67 throw CoreException("INTERNAL ERROR: cannot unget twice");
81 void nextword(std::string& rv)
86 while (isalnum(ch) || ch == '_'|| ch == '-')
94 bool kv(std::vector<KeyVal>* items, std::set<std::string>& seen)
99 if (ch == '>' && key.empty())
103 else if (ch == '#' && key.empty())
110 throw CoreException("Invalid character " + std::string(1, ch) + " in key (" + key + ")");
117 throw CoreException("Invalid character in value of <" + tag->tag + ":" + key + ">");
122 if (ch == '&' && !(flags & FLAG_USE_COMPAT))
128 if (isalnum(ch) || (varname.empty() && ch == '#'))
129 varname.push_back(ch);
134 stack.errstr << "Invalid XML entity name in value of <" + tag->tag + ":" + key + ">\n"
135 << "To include an ampersand or quote, use & or "\n";
136 throw CoreException("Parse error");
140 throw CoreException("Empty XML entity reference");
141 else if (varname[0] == '#' && (varname.size() == 1 || (varname.size() == 2 && varname[1] == 'x')))
142 throw CoreException("Empty numeric character reference");
143 else if (varname[0] == '#')
145 const char* cvarname = varname.c_str();
147 unsigned long lvalue;
148 if (cvarname[1] == 'x')
149 lvalue = strtoul(cvarname + 2, &endptr, 16);
151 lvalue = strtoul(cvarname + 1, &endptr, 10);
152 if (*endptr != '\0' || lvalue > 255)
153 throw CoreException("Invalid numeric character reference '&" + varname + ";'");
154 value.push_back(static_cast<char>(lvalue));
158 insp::flat_map<std::string, std::string>::iterator var = stack.vars.find(varname);
159 if (var == stack.vars.end())
160 throw CoreException("Undefined XML entity reference '&" + varname + ";'");
161 value.append(var->second);
164 else if (ch == '\\' && (flags & FLAG_USE_COMPAT))
168 value.push_back('\n');
169 else if (isalpha(esc))
170 throw CoreException("Unknown escape character \\" + std::string(1, esc));
172 value.push_back(esc);
180 if (!seen.insert(key).second)
181 throw CoreException("Duplicate key '" + key + "' found");
183 items->push_back(KeyVal(key, value));
196 else if (!isspace(spc))
197 throw CoreException("Invalid character in tag name");
200 throw CoreException("Empty tag name");
202 std::vector<KeyVal>* items;
203 std::set<std::string> seen;
204 tag = ConfigTag::create(name, current.filename, current.line, items);
206 while (kv(items, seen))
208 // Do nothing here (silences a GCC warning).
211 if (name == mandatory_tag)
213 // Found the mandatory tag
214 mandatory_tag.clear();
217 if (name == "include")
219 stack.DoInclude(tag, flags);
221 else if (name == "files")
223 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
225 stack.DoReadFile(i->first, i->second, flags, false);
228 else if (name == "execfiles")
230 for(std::vector<KeyVal>::iterator i = items->begin(); i != items->end(); i++)
232 stack.DoReadFile(i->first, i->second, flags, true);
235 else if (name == "define")
237 if (flags & FLAG_USE_COMPAT)
238 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
239 std::string varname = tag->getString("name");
240 std::string value = tag->getString("value");
242 throw CoreException("Variable definition must include variable name");
243 stack.vars[varname] = value;
245 else if (name == "config")
247 std::string format = tag->getString("format");
249 flags &= ~FLAG_USE_COMPAT;
250 else if (format == "compat")
251 flags |= FLAG_USE_COMPAT;
252 else if (!format.empty())
253 throw CoreException("Unknown configuration format " + format);
257 stack.output.insert(std::make_pair(name, tag));
259 // this is not a leak; reference<> takes care of the delete
273 // this is the one place where an EOF is not an error
274 if (!mandatory_tag.empty())
275 throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found");
290 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
292 throw CoreException("Syntax error - start of tag expected");
296 catch (CoreException& err)
298 stack.errstr << err.GetReason() << " at " << current.str();
300 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
302 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
308 void ParseStack::DoInclude(ConfigTag* tag, int flags)
310 if (flags & FLAG_NO_INC)
311 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
313 std::string mandatorytag;
314 tag->readString("mandatorytag", mandatorytag);
317 if (tag->readString("file", name))
319 if (tag->getBool("noinclude", false))
320 flags |= FLAG_NO_INC;
321 if (tag->getBool("noexec", false))
322 flags |= FLAG_NO_EXEC;
323 if (!ParseFile(ServerInstance->Config->Paths.PrependConfig(name), flags, mandatorytag))
324 throw CoreException("Included");
326 else if (tag->readString("executable", name))
328 if (flags & FLAG_NO_EXEC)
329 throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
330 if (tag->getBool("noinclude", false))
331 flags |= FLAG_NO_INC;
332 if (tag->getBool("noexec", true))
333 flags |= FLAG_NO_EXEC;
334 if (!ParseFile(name, flags, mandatorytag, true))
335 throw CoreException("Included");
339 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
341 if (flags & FLAG_NO_INC)
342 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
343 if (exec && (flags & FLAG_NO_EXEC))
344 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
346 std::string path = ServerInstance->Config->Paths.PrependConfig(name);
347 FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(path.c_str(), "r"), exec);
349 throw CoreException("Could not read \"" + path + "\" for \"" + key + "\" file");
351 file_cache& cache = FilesOutput[key];
355 while (fgets(linebuf, sizeof(linebuf), file))
357 size_t len = strlen(linebuf);
360 if (linebuf[len-1] == '\n')
362 cache.push_back(std::string(linebuf, len));
367 bool ParseStack::ParseFile(const std::string& path, int flags, const std::string& mandatory_tag, bool isexec)
369 ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading (isexec=%d) %s", isexec, path.c_str());
370 if (stdalgo::isin(reading, path))
371 throw CoreException((isexec ? "Executable " : "File ") + path + " is included recursively (looped inclusion)");
373 /* It's not already included, add it to the list of files we've loaded */
375 FileWrapper file((isexec ? popen(path.c_str(), "r") : fopen(path.c_str(), "r")), isexec);
377 throw CoreException("Could not read \"" + path + "\" for include");
379 reading.push_back(path);
380 Parser p(*this, flags, file, path, mandatory_tag);
381 bool ok = p.outer_parse();
386 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
388 for(std::vector<KeyVal>::iterator j = items.begin(); j != items.end(); ++j)
393 if (!allow_lf && (value.find('\n') != std::string::npos))
395 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
396 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
397 for (std::string::iterator n = value.begin(); n != value.end(); n++)
406 std::string ConfigTag::getString(const std::string& key, const std::string& def)
408 std::string res = def;
409 readString(key, res);
413 long ConfigTag::getInt(const std::string &key, long def, long min, long max)
416 if(!readString(key, result))
419 const char* res_cstr = result.c_str();
420 char* res_tail = NULL;
421 long res = strtol(res_cstr, &res_tail, 0);
422 if (res_tail == res_cstr)
424 switch (toupper(*res_tail))
430 res = res * 1024 * 1024;
433 res = res * 1024 * 1024 * 1024;
437 CheckRange(key, res, def, min, max);
441 void ConfigTag::CheckRange(const std::string& key, long& res, long def, long min, long max)
443 if (res < min || res > max)
445 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: <%s:%s> value of %ld is not between %ld and %ld; set to %ld.",
446 tag.c_str(), key.c_str(), res, min, max, def);
451 long ConfigTag::getDuration(const std::string& key, long def, long min, long max)
453 std::string duration;
454 if (!readString(key, duration))
457 long ret = InspIRCd::Duration(duration);
458 CheckRange(key, ret, def, min, max);
462 double ConfigTag::getFloat(const std::string &key, double def)
465 if (!readString(key, result))
467 return strtod(result.c_str(), NULL);
470 bool ConfigTag::getBool(const std::string &key, bool def)
473 if(!readString(key, result))
476 if (result == "yes" || result == "true" || result == "1" || result == "on")
478 if (result == "no" || result == "false" || result == "0" || result == "off")
481 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
482 " is not valid, ignoring");
486 std::string ConfigTag::getTagLocation()
488 return src_name + ":" + ConvToStr(src_line);
491 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, std::vector<KeyVal>*& Items)
493 ConfigTag* rv = new ConfigTag(Tag, file, line);
498 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
499 : tag(Tag), src_name(file), src_line(line)
503 std::string OperInfo::getConfig(const std::string& key)
507 type_block->readString(key, rv);
509 oper_block->readString(key, rv);