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(ConfigItems* items)
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 (items->find(key) != items->end())
181 throw CoreException("Duplicate key '" + key + "' found");
183 (*items)[key] = value;
196 else if (!isspace(spc))
197 throw CoreException("Invalid character in tag name");
200 throw CoreException("Empty tag name");
203 tag = ConfigTag::create(name, current.filename, current.line, items);
207 // Do nothing here (silences a GCC warning).
210 if (name == mandatory_tag)
212 // Found the mandatory tag
213 mandatory_tag.clear();
216 if (name == "include")
218 stack.DoInclude(tag, flags);
220 else if (name == "files")
222 for(ConfigItems::iterator i = items->begin(); i != items->end(); i++)
224 stack.DoReadFile(i->first, i->second, flags, false);
227 else if (name == "execfiles")
229 for(ConfigItems::iterator i = items->begin(); i != items->end(); i++)
231 stack.DoReadFile(i->first, i->second, flags, true);
234 else if (name == "define")
236 if (flags & FLAG_USE_COMPAT)
237 throw CoreException("<define> tags may only be used in XML-style config (add <config format=\"xml\">)");
238 std::string varname = tag->getString("name");
239 std::string value = tag->getString("value");
241 throw CoreException("Variable definition must include variable name");
242 stack.vars[varname] = value;
244 else if (name == "config")
246 std::string format = tag->getString("format");
248 flags &= ~FLAG_USE_COMPAT;
249 else if (format == "compat")
250 flags |= FLAG_USE_COMPAT;
251 else if (!format.empty())
252 throw CoreException("Unknown configuration format " + format);
256 stack.output.insert(std::make_pair(name, tag));
258 // this is not a leak; reference<> takes care of the delete
272 // this is the one place where an EOF is not an error
273 if (!mandatory_tag.empty())
274 throw CoreException("Mandatory tag \"" + mandatory_tag + "\" not found");
289 stack.errstr << "Do not save your files as UTF-16; use ASCII!\n";
291 throw CoreException("Syntax error - start of tag expected");
295 catch (CoreException& err)
297 stack.errstr << err.GetReason() << " at " << current.str();
299 stack.errstr << " (inside tag " << tag->tag << " at line " << tag->src_line << ")\n";
301 stack.errstr << " (last tag was on line " << last_tag.line << ")\n";
307 void ParseStack::DoInclude(ConfigTag* tag, int flags)
309 if (flags & FLAG_NO_INC)
310 throw CoreException("Invalid <include> tag in file included with noinclude=\"yes\"");
312 std::string mandatorytag;
313 tag->readString("mandatorytag", mandatorytag);
316 if (tag->readString("file", name))
318 if (tag->getBool("noinclude", false))
319 flags |= FLAG_NO_INC;
320 if (tag->getBool("noexec", false))
321 flags |= FLAG_NO_EXEC;
322 if (!ParseFile(ServerInstance->Config->Paths.PrependConfig(name), flags, mandatorytag))
323 throw CoreException("Included");
325 else if (tag->readString("executable", name))
327 if (flags & FLAG_NO_EXEC)
328 throw CoreException("Invalid <include:executable> tag in file included with noexec=\"yes\"");
329 if (tag->getBool("noinclude", false))
330 flags |= FLAG_NO_INC;
331 if (tag->getBool("noexec", true))
332 flags |= FLAG_NO_EXEC;
333 if (!ParseFile(name, flags, mandatorytag, true))
334 throw CoreException("Included");
338 void ParseStack::DoReadFile(const std::string& key, const std::string& name, int flags, bool exec)
340 if (flags & FLAG_NO_INC)
341 throw CoreException("Invalid <files> tag in file included with noinclude=\"yes\"");
342 if (exec && (flags & FLAG_NO_EXEC))
343 throw CoreException("Invalid <execfiles> tag in file included with noexec=\"yes\"");
345 std::string path = ServerInstance->Config->Paths.PrependConfig(name);
346 FileWrapper file(exec ? popen(name.c_str(), "r") : fopen(path.c_str(), "r"), exec);
348 throw CoreException("Could not read \"" + path + "\" for \"" + key + "\" file");
350 file_cache& cache = FilesOutput[key];
354 while (fgets(linebuf, sizeof(linebuf), file))
356 size_t len = strlen(linebuf);
359 if (linebuf[len-1] == '\n')
361 cache.push_back(std::string(linebuf, len));
366 bool ParseStack::ParseFile(const std::string& path, int flags, const std::string& mandatory_tag, bool isexec)
368 ServerInstance->Logs->Log("CONFIG", LOG_DEBUG, "Reading (isexec=%d) %s", isexec, path.c_str());
369 if (stdalgo::isin(reading, path))
370 throw CoreException((isexec ? "Executable " : "File ") + path + " is included recursively (looped inclusion)");
372 /* It's not already included, add it to the list of files we've loaded */
374 FileWrapper file((isexec ? popen(path.c_str(), "r") : fopen(path.c_str(), "r")), isexec);
376 throw CoreException("Could not read \"" + path + "\" for include");
378 reading.push_back(path);
379 Parser p(*this, flags, file, path, mandatory_tag);
380 bool ok = p.outer_parse();
385 bool ConfigTag::readString(const std::string& key, std::string& value, bool allow_lf)
387 for(ConfigItems::iterator j = items.begin(); j != items.end(); ++j)
392 if (!allow_lf && (value.find('\n') != std::string::npos))
394 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
395 " contains a linefeed, and linefeeds in this value are not permitted -- stripped to spaces.");
396 for (std::string::iterator n = value.begin(); n != value.end(); n++)
405 std::string ConfigTag::getString(const std::string& key, const std::string& def, size_t minlen, size_t maxlen)
408 if (!readString(key, res))
411 if (res.length() < minlen || res.length() > maxlen)
413 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: The length of <%s:%s> is not between %ld and %ld; value set to %s.",
414 tag.c_str(), key.c_str(), minlen, maxlen, def.c_str());
420 long ConfigTag::getInt(const std::string &key, long def, long min, long max)
423 if(!readString(key, result))
426 const char* res_cstr = result.c_str();
427 char* res_tail = NULL;
428 long res = strtol(res_cstr, &res_tail, 0);
429 if (res_tail == res_cstr)
431 switch (toupper(*res_tail))
437 res = res * 1024 * 1024;
440 res = res * 1024 * 1024 * 1024;
444 CheckRange(key, res, def, min, max);
448 void ConfigTag::CheckRange(const std::string& key, long& res, long def, long min, long max)
450 if (res < min || res > max)
452 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "WARNING: <%s:%s> value of %ld is not between %ld and %ld; set to %ld.",
453 tag.c_str(), key.c_str(), res, min, max, def);
458 long ConfigTag::getDuration(const std::string& key, long def, long min, long max)
460 std::string duration;
461 if (!readString(key, duration))
464 long ret = InspIRCd::Duration(duration);
465 CheckRange(key, ret, def, min, max);
469 double ConfigTag::getFloat(const std::string &key, double def)
472 if (!readString(key, result))
474 return strtod(result.c_str(), NULL);
477 bool ConfigTag::getBool(const std::string &key, bool def)
480 if(!readString(key, result))
483 if (result == "yes" || result == "true" || result == "1" || result == "on")
485 if (result == "no" || result == "false" || result == "0" || result == "off")
488 ServerInstance->Logs->Log("CONFIG", LOG_DEFAULT, "Value of <" + tag + ":" + key + "> at " + getTagLocation() +
489 " is not valid, ignoring");
493 std::string ConfigTag::getTagLocation()
495 return src_name + ":" + ConvToStr(src_line);
498 ConfigTag* ConfigTag::create(const std::string& Tag, const std::string& file, int line, ConfigItems*& Items)
500 ConfigTag* rv = new ConfigTag(Tag, file, line);
505 ConfigTag::ConfigTag(const std::string& Tag, const std::string& file, int line)
506 : tag(Tag), src_name(file), src_line(line)
510 std::string OperInfo::getConfig(const std::string& key)
514 type_block->readString(key, rv);
516 oper_block->readString(key, rv);