1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #include "inspircd_config.h"
18 #include "configreader.h"
25 #include "inspstring.h"
26 #include "helperfuncs.h"
27 #include "userprocess.h"
30 extern ServerConfig *Config;
31 extern InspIRCd* ServerInstance;
35 extern std::vector<Module*> modules;
36 extern std::vector<ircd_module*> factory;
38 std::vector<std::string> old_module_names, new_module_names, added_modules, removed_modules;
40 ServerConfig::ServerConfig()
43 *TempDir = *ServerName = *Network = *ServerDesc = *AdminName = '\0';
44 *HideWhoisServer = *AdminEmail = *AdminNick = *diepass = *restartpass = '\0';
45 *CustomVersion = *motd = *rules = *PrefixQuit = *DieValue = *DNSServer = '\0';
46 *OperOnlyStats = *ModPath = *MyExecutable = *DisabledCommands = *PID = '\0';
48 forcedebug = OperSpyWhois = nofork = HideBans = HideSplits = false;
49 writelog = AllowHalfop = true;
50 dns_timeout = DieDelay = 5;
52 NetBufferSize = 10240;
53 SoftLimit = MAXCLIENTS;
61 void ServerConfig::ClearStack()
63 include_stack.clear();
66 Module* ServerConfig::GetIOHook(int port)
68 std::map<int,Module*>::iterator x = IOHookModule.find(port);
69 return (x != IOHookModule.end() ? x->second : NULL);
72 bool ServerConfig::AddIOHook(int port, Module* iomod)
76 IOHookModule[port] = iomod;
81 ModuleException err("Port already hooked by another module");
87 bool ServerConfig::DelIOHook(int port)
89 std::map<int,Module*>::iterator x = IOHookModule.find(port);
90 if (x != IOHookModule.end())
92 IOHookModule.erase(x);
98 bool ServerConfig::CheckOnce(char* tag, bool bail, userrec* user)
100 int count = ConfValueEnum(Config->config_data, tag);
106 printf("There were errors in your configuration:\nYou have more than one <%s> tag, this is not permitted.\n",tag);
113 WriteServ(user->fd,"There were errors in your configuration:");
114 WriteServ(user->fd,"You have more than one <%s> tag, this is not permitted.\n",tag);
118 WriteOpers("There were errors in the configuration file:");
119 WriteOpers("You have more than one <%s> tag, this is not permitted.\n",tag);
128 printf("There were errors in your configuration:\nYou have not defined a <%s> tag, this is required.\n",tag);
135 WriteServ(user->fd,"There were errors in your configuration:");
136 WriteServ(user->fd,"You have not defined a <%s> tag, this is required.",tag);
140 WriteOpers("There were errors in the configuration file:");
141 WriteOpers("You have not defined a <%s> tag, this is required.",tag);
149 bool NoValidation(const char* tag, const char* value, void* data)
151 log(DEBUG,"No validation for <%s:%s>",tag,value);
155 bool ValidateTempDir(const char* tag, const char* value, void* data)
157 char* x = (char*)data;
159 strlcpy(x,"/tmp",1024);
163 bool ValidateMaxTargets(const char* tag, const char* value, void* data)
166 if ((*x < 0) || (*x > 31))
168 log(DEFAULT,"WARNING: <options:maxtargets> value is greater than 31 or less than 0, set to 20.");
174 bool ValidateSoftLimit(const char* tag, const char* value, void* data)
177 if ((*x < 1) || (*x > MAXCLIENTS))
179 log(DEFAULT,"WARNING: <options:softlimit> value is greater than %d or less than 0, set to %d.",MAXCLIENTS,MAXCLIENTS);
185 bool ValidateMaxConn(const char* tag, const char* value, void* data)
189 log(DEFAULT,"WARNING: <options:somaxconn> value may be higher than the system-defined SOMAXCONN value!");
195 bool ValidateDnsTimeout(const char* tag, const char* value, void* data)
203 bool ValidateDnsServer(const char* tag, const char* value, void* data)
205 char* x = (char*)data;
208 // attempt to look up their nameserver from /etc/resolv.conf
209 log(DEFAULT,"WARNING: <dns:server> not defined, attempting to find working server in /etc/resolv.conf...");
210 ifstream resolv("/etc/resolv.conf");
211 std::string nameserver;
212 bool found_server = false;
214 if (resolv.is_open())
216 while (resolv >> nameserver)
218 if ((nameserver == "nameserver") && (!found_server))
220 resolv >> nameserver;
221 strlcpy(x,nameserver.c_str(),MAXBUF);
223 log(DEFAULT,"<dns:server> set to '%s' as first resolver in /etc/resolv.conf.",nameserver.c_str());
229 log(DEFAULT,"/etc/resolv.conf contains no viable nameserver entries! Defaulting to nameserver '127.0.0.1'!");
230 strlcpy(x,"127.0.0.1",MAXBUF);
235 log(DEFAULT,"/etc/resolv.conf can't be opened! Defaulting to nameserver '127.0.0.1'!");
236 strlcpy(x,"127.0.0.1",MAXBUF);
242 bool ValidateModPath(const char* tag, const char* value, void* data)
244 char* x = (char*)data;
246 strlcpy(x,MOD_PATH,MAXBUF);
251 bool ValidateServerName(const char* tag, const char* value, void* data)
253 char* x = (char*)data;
256 log(DEFAULT,"WARNING: <server:name> '%s' is not a fully-qualified domain name. Changed to '%s%c'",x,x,'.');
257 charlcat(x,'.',MAXBUF);
263 bool ValidateNetBufferSize(const char* tag, const char* value, void* data)
265 if ((!Config->NetBufferSize) || (Config->NetBufferSize > 65535) || (Config->NetBufferSize < 1024))
267 log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
268 Config->NetBufferSize = 10240;
273 bool ValidateMaxWho(const char* tag, const char* value, void* data)
275 if ((!Config->MaxWhoResults) || (Config->MaxWhoResults > 65535) || (Config->MaxWhoResults < 1))
277 log(DEFAULT,"No MaxWhoResults specified or size out of range, setting to default of 128.");
278 Config->MaxWhoResults = 128;
283 bool ValidateLogLevel(const char* tag, const char* value, void* data)
285 const char* dbg = (const char*)data;
286 Config->LogLevel = DEFAULT;
287 if (!strcmp(dbg,"debug"))
289 Config->LogLevel = DEBUG;
290 Config->debugging = 1;
292 else if (!strcmp(dbg,"verbose"))
293 Config->LogLevel = VERBOSE;
294 else if (!strcmp(dbg,"default"))
295 Config->LogLevel = DEFAULT;
296 else if (!strcmp(dbg,"sparse"))
297 Config->LogLevel = SPARSE;
298 else if (!strcmp(dbg,"none"))
299 Config->LogLevel = NONE;
303 bool ValidateMotd(const char* tag, const char* value, void* data)
305 readfile(Config->MOTD,Config->motd);
309 bool ValidateRules(const char* tag, const char* value, void* data)
311 readfile(Config->RULES,Config->rules);
315 /* Callback called before processing the first <connect> tag
317 bool InitConnect(const char* tag)
319 log(DEFAULT,"Reading connect classes...");
320 Config->Classes.clear();
324 /* Callback called to process a single <connect> tag
326 bool DoConnect(const char* tag, char** entries, void** values, int* types)
329 char* allow = (char*)values[0]; /* Yeah, there are a lot of values. Live with it. */
330 char* deny = (char*)values[1];
331 char* password = (char*)values[2];
332 int* timeout = (int*)values[3];
333 int* pingfreq = (int*)values[4];
334 int* flood = (int*)values[5];
335 int* threshold = (int*)values[6];
336 int* sendq = (int*)values[7];
337 int* recvq = (int*)values[8];
338 int* localmax = (int*)values[9];
339 int* globalmax = (int*)values[10];
346 c.registration_timeout = *timeout;
347 c.pingtime = *pingfreq;
349 c.threshold = *threshold;
352 c.maxlocal = *localmax;
353 c.maxglobal = *globalmax;
358 if (c.maxglobal == 0)
360 if (c.threshold == 0)
364 log(DEFAULT,"Warning: Connect allow line '%s' has no flood/threshold settings. Setting this tag to 999 lines in 1 second.",c.host.c_str());
370 if (c.registration_timeout == 0)
371 c.registration_timeout = 90;
374 Config->Classes.push_back(c);
380 Config->Classes.push_back(c);
381 log(DEBUG,"Read connect class type DENY, host=%s",deny);
387 /* Callback called when there are no more <connect> tags
389 bool DoneConnect(const char* tag)
391 log(DEBUG,"DoneConnect called for tag: %s",tag);
395 /* Callback called before processing the first <uline> tag
397 bool InitULine(const char* tag)
399 Config->ulines.clear();
403 /* Callback called to process a single <uline> tag
405 bool DoULine(const char* tag, char** entries, void** values, int* types)
407 char* server = (char*)values[0];
408 log(DEBUG,"Read ULINE '%s'",server);
409 Config->ulines.push_back(server);
413 /* Callback called when there are no more <uline> tags
415 bool DoneULine(const char* tag)
420 /* Callback called before processing the first <module> tag
422 bool InitModule(const char* tag)
424 old_module_names.clear();
425 new_module_names.clear();
426 added_modules.clear();
427 removed_modules.clear();
428 for (std::vector<std::string>::iterator t = Config->module_names.begin(); t != Config->module_names.end(); t++)
430 old_module_names.push_back(*t);
435 /* Callback called to process a single <module> tag
437 bool DoModule(const char* tag, char** entries, void** values, int* types)
439 char* modname = (char*)values[0];
440 new_module_names.push_back(modname);
444 /* Callback called when there are no more <module> tags
446 bool DoneModule(const char* tag)
448 // now create a list of new modules that are due to be loaded
449 // and a seperate list of modules which are due to be unloaded
450 for (std::vector<std::string>::iterator _new = new_module_names.begin(); _new != new_module_names.end(); _new++)
454 for (std::vector<std::string>::iterator old = old_module_names.begin(); old != old_module_names.end(); old++)
461 added_modules.push_back(*_new);
464 for (std::vector<std::string>::iterator oldm = old_module_names.begin(); oldm != old_module_names.end(); oldm++)
467 for (std::vector<std::string>::iterator newm = new_module_names.begin(); newm != new_module_names.end(); newm++)
474 removed_modules.push_back(*oldm);
479 /* Callback called before processing the first <banlist> tag
481 bool InitMaxBans(const char* tag)
483 Config->maxbans.clear();
487 /* Callback called to process a single <banlist> tag
489 bool DoMaxBans(const char* tag, char** entries, void** values, int* types)
491 char* channel = (char*)values[0];
492 int* limit = (int*)values[1];
493 Config->maxbans[channel] = *limit;
497 /* Callback called when there are no more <banlist> tags.
499 bool DoneMaxBans(const char* tag)
504 void ServerConfig::Read(bool bail, userrec* user)
506 char debug[MAXBUF]; /* Temporary buffer for debugging value */
507 char* data[12]; /* Temporary buffers for reading multiple occurance tags into */
508 void* ptr[12]; /* Temporary pointers for passing to callbacks */
509 int r_i[12]; /* Temporary array for casting */
510 int rem = 0, add = 0; /* Number of modules added, number of modules removed */
511 std::ostringstream errstr; /* String stream containing the error output */
513 /* These tags MUST occur and must ONLY occur once in the config file */
514 static char* Once[] = { "server", "admin", "files", "power", "options", "pid", NULL };
516 /* These tags can occur ONCE or not at all */
517 static InitialConfig Values[] = {
518 {"options", "softlimit", &this->SoftLimit, DT_INTEGER, ValidateSoftLimit},
519 {"options", "somaxconn", &this->MaxConn, DT_INTEGER, ValidateMaxConn},
520 {"server", "name", &this->ServerName, DT_CHARPTR, ValidateServerName},
521 {"server", "description", &this->ServerDesc, DT_CHARPTR, NoValidation},
522 {"server", "network", &this->Network, DT_CHARPTR, NoValidation},
523 {"admin", "name", &this->AdminName, DT_CHARPTR, NoValidation},
524 {"admin", "email", &this->AdminEmail, DT_CHARPTR, NoValidation},
525 {"admin", "nick", &this->AdminNick, DT_CHARPTR, NoValidation},
526 {"files", "motd", &this->motd, DT_CHARPTR, ValidateMotd},
527 {"files", "rules", &this->rules, DT_CHARPTR, ValidateRules},
528 {"power", "diepass", &this->diepass, DT_CHARPTR, NoValidation},
529 {"power", "pauseval", &this->DieDelay, DT_INTEGER, NoValidation},
530 {"power", "restartpass", &this->restartpass, DT_CHARPTR, NoValidation},
531 {"options", "prefixquit", &this->PrefixQuit, DT_CHARPTR, NoValidation},
532 {"die", "value", &this->DieValue, DT_CHARPTR, NoValidation},
533 {"options", "loglevel", &debug, DT_CHARPTR, ValidateLogLevel},
534 {"options", "netbuffersize", &this->NetBufferSize, DT_INTEGER, ValidateNetBufferSize},
535 {"options", "maxwho", &this->MaxWhoResults, DT_INTEGER, ValidateMaxWho},
536 {"options", "allowhalfop", &this->AllowHalfop, DT_BOOLEAN, NoValidation},
537 {"dns", "server", &this->DNSServer, DT_CHARPTR, ValidateDnsServer},
538 {"dns", "timeout", &this->dns_timeout, DT_INTEGER, ValidateDnsTimeout},
539 {"options", "moduledir", &this->ModPath, DT_CHARPTR, ValidateModPath},
540 {"disabled", "commands", &this->DisabledCommands, DT_CHARPTR, NoValidation},
541 {"options", "operonlystats", &this->OperOnlyStats, DT_CHARPTR, NoValidation},
542 {"options", "customversion", &this->CustomVersion, DT_CHARPTR, NoValidation},
543 {"options", "hidesplits", &this->HideSplits, DT_BOOLEAN, NoValidation},
544 {"options", "hidebans", &this->HideBans, DT_BOOLEAN, NoValidation},
545 {"options", "hidewhois", &this->HideWhoisServer, DT_CHARPTR, NoValidation},
546 {"options", "operspywhois", &this->OperSpyWhois, DT_BOOLEAN, NoValidation},
547 {"options", "tempdir", &this->TempDir, DT_CHARPTR, ValidateTempDir},
548 {"pid", "file", &this->PID, DT_CHARPTR, NoValidation},
552 /* These tags can occur multiple times, and therefore they have special code to read them
553 * which is different to the code for reading the singular tags listed above.
555 static MultiConfig MultiValues[] = {
558 {"allow", "deny", "password", "timeout", "pingfreq", "flood",
559 "threshold", "sendq", "recvq", "localmax", "globalmax", NULL},
560 {DT_CHARPTR, DT_CHARPTR, DT_CHARPTR, DT_INTEGER, DT_INTEGER, DT_INTEGER,
561 DT_INTEGER, DT_INTEGER, DT_INTEGER, DT_INTEGER, DT_INTEGER},
562 InitConnect, DoConnect, DoneConnect},
567 InitULine,DoULine,DoneULine},
570 {"chan", "limit", NULL},
571 {DT_CHARPTR, DT_INTEGER},
572 InitMaxBans, DoMaxBans, DoneMaxBans},
577 InitModule, DoModule, DoneModule},
580 {"reason", "ipmask", NULL},
581 {DT_CHARPTR, DT_CHARPTR},
582 InitXLine, DoZLine, DoneXLine},
585 {"reason", "nick", NULL},
586 {DT_CHARPTR, DT_CHARPTR},
587 InitXLine, DoQLine, DoneXLine},
590 {"reason", "host", NULL},
591 {DT_CHARPTR, DT_CHARPTR},
592 InitXLine, DoKLine, DoneXLine},
595 {"reason", "host", NULL},
596 {DT_CHARPTR, DT_CHARPTR},
597 InitXLine, DoELine, DoneXLine},
600 {"name", "classes", NULL},
601 {DT_CHARPTR, DT_CHARPTR},
602 InitTypes, DoType, DoneClassesAndTypes},
605 {"name", "commands", NULL},
606 {DT_CHARPTR, DT_CHARPTR},
607 InitClasses, DoClass, DoneClassesAndTypes},
612 include_stack.clear();
614 /* Load and parse the config file, if there are any errors then explode */
616 /* Make a copy here so if it fails then we can carry on running with an unaffected config */
617 ConfigDataHash newconfig;
619 if (this->LoadConf(newconfig, CONFIG_FILE, errstr))
621 /* If we succeeded, set the ircd config to the new one */
622 Config->config_data = newconfig;
627 for(ConfigDataHash::const_iterator i = this->config_data.begin(); i != this->config_data.end(); i++)
629 c = (i->first != last) ? 1 : c+1;
632 std::cout << "[" << i->first << " " << c << "/" << this->config_data.count(i->first) << "]" << std::endl;
634 for(KeyValList::const_iterator j = i->second.begin(); j != i->second.end(); j++)
635 std::cout << "\t" << j->first << " = " << j->second << std::endl;
637 std::cout << "[/" << i->first << " " << c << "/" << this->config_data.count(i->first) << "]" << std::endl;
642 log(DEFAULT, "There were errors in your configuration:\n%s", errstr.str().c_str());
646 /* Unneeded because of the log() aboive? */
647 printf("There were errors in your configuration:\n%s",errstr.str().c_str());
652 std::string errors = errstr.str();
653 std::string::size_type start;
654 unsigned int prefixlen;
657 /* ":Config->ServerName NOTICE user->nick :" */
658 prefixlen = strlen(Config->ServerName) + strlen(user->nick) + 11;
662 WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
664 while(start < errors.length())
666 WriteServ(user->fd, "NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
667 start += 510 - prefixlen;
672 WriteOpers("There were errors in the configuration file:");
674 while(start < errors.length())
676 WriteOpers(errors.substr(start, 360).c_str());
685 /* Check we dont have more than one of singular tags, or any of them missing
687 for (int Index = 0; Once[Index]; Index++)
688 if (!CheckOnce(Once[Index],bail,user))
691 /* Read the values of all the tags which occur once or not at all, and call their callbacks.
693 for (int Index = 0; Values[Index].tag; Index++)
695 int* val_i = (int*) Values[Index].val;
696 char* val_c = (char*) Values[Index].val;
698 switch (Values[Index].datatype)
701 /* Assuming MAXBUF here, potentially unsafe */
702 ConfValue(this->config_data, Values[Index].tag, Values[Index].value, 0, val_c, MAXBUF);
706 ConfValueInteger(this->config_data, Values[Index].tag, Values[Index].value, 0, *val_i);
710 *val_i = ConfValueBool(this->config_data, Values[Index].tag, Values[Index].value, 0);
717 Values[Index].validation_function(Values[Index].tag, Values[Index].value, Values[Index].val);
720 /* Claim memory for use when reading multiple tags
722 for (int n = 0; n < 12; n++)
723 data[n] = new char[MAXBUF];
725 /* Read the multiple-tag items (class tags, connect tags, etc)
726 * and call the callbacks associated with them. We have three
727 * callbacks for these, a 'start', 'item' and 'end' callback.
730 /* XXX - Make this use ConfValueInteger and so on */
731 for (int Index = 0; MultiValues[Index].tag; Index++)
733 MultiValues[Index].init_function(MultiValues[Index].tag);
735 int number_of_tags = ConfValueEnum(this->config_data, MultiValues[Index].tag);
737 for (int tagnum = 0; tagnum < number_of_tags; tagnum++)
739 for (int valuenum = 0; MultiValues[Index].items[valuenum]; valuenum++)
741 ConfValue(this->config_data, MultiValues[Index].tag, MultiValues[Index].items[valuenum], tagnum, data[valuenum], MAXBUF);
743 switch (MultiValues[Index].datatype[valuenum])
746 ptr[valuenum] = data[valuenum];
749 r_i[valuenum] = atoi(data[valuenum]);
750 ptr[valuenum] = &r_i[valuenum];
753 r_i[valuenum] = ((*data[valuenum] == tolower('y')) || (*data[valuenum] == tolower('t')) || (*data[valuenum] == '1'));
754 ptr[valuenum] = &r_i[valuenum];
760 MultiValues[Index].validation_function(MultiValues[Index].tag, (char**)MultiValues[Index].items, ptr, MultiValues[Index].datatype);
763 MultiValues[Index].finish_function(MultiValues[Index].tag);
766 /* Free any memory we claimed
768 for (int n = 0; n < 12; n++)
771 // write once here, to try it out and make sure its ok
772 ServerInstance->WritePID(Config->PID);
774 log(DEFAULT,"Done reading configuration file, InspIRCd is now starting.");
776 /* If we're rehashing, let's load any new modules, and unload old ones
780 ServerInstance->stats->BoundPortCount = BindPorts(false);
782 if (!removed_modules.empty())
783 for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
785 if (ServerInstance->UnloadModule(removing->c_str()))
787 WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
790 WriteServ(user->fd,"973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
797 WriteServ(user->fd,"972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ServerInstance->ModuleError());
801 if (!added_modules.empty())
802 for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
804 if (ServerInstance->LoadModule(adding->c_str()))
806 WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
809 WriteServ(user->fd,"975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
816 WriteServ(user->fd,"974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ServerInstance->ModuleError());
820 log(DEFAULT,"Successfully unloaded %lu of %lu modules and loaded %lu of %lu modules.",(unsigned long)rem,(unsigned long)removed_modules.size(),(unsigned long)add,(unsigned long)added_modules.size());
824 bool ServerConfig::LoadConf(ConfigDataHash &target, const char* filename, std::ostringstream &errorstream)
826 std::ifstream conf(filename);
839 /* Check if the file open failed first */
842 errorstream << "LoadConf: Couldn't open config file: " << filename << std::endl;
846 /* Fix the chmod of the file to restrict it to the current user and group */
847 chmod(filename,0600);
849 for (unsigned int t = 0; t < include_stack.size(); t++)
851 if (std::string(filename) == include_stack[t])
853 errorstream << "File " << filename << " is included recursively (looped inclusion)." << std::endl;
858 /* It's not already included, add it to the list of files we've loaded */
859 include_stack.push_back(filename);
861 /* Start reading characters... */
865 * Here we try and get individual tags on separate lines,
866 * this would be so easy if we just made people format
867 * their config files like that, but they don't so...
868 * We check for a '<' and then know the line is over when
869 * we get a '>' not inside quotes. If we find two '<' and
870 * no '>' then die with an error.
873 if((ch == '#') && !in_quote)
876 if(((ch == '\n') || (ch == '\r')) && in_quote)
878 errorstream << "Got a newline within a quoted section, this is probably a typo: " << filename << ":" << linenumber << std::endl;
897 /* XXX: Added by Brain, May 1st 2006 - Escaping of characters.
898 * Note that this WILL NOT usually allow insertion of newlines,
899 * because a newline is two characters long. Use it primarily to
900 * insert the " symbol.
902 * Note that this also involves a further check when parsing the line,
903 * which can be found below.
905 if ((ch == '\\') && (in_quote) && (in_tag))
908 log(DEBUG,"Escape sequence in config line.");
910 if (conf.get(real_character))
912 log(DEBUG,"Escaping %c", real_character);
913 line += real_character;
918 errorstream << "End of file after a \\, what did you want to escape?: " << filename << ":" << linenumber << std::endl;
931 errorstream << "Got another opening < when the first one wasn't closed: " << filename << ":" << linenumber << std::endl;
939 errorstream << "We're in a quote but outside a tag, interesting. " << filename << ":" << linenumber << std::endl;
944 // errorstream << "Opening new config tag on line " << linenumber << std::endl;
955 // errorstream << "Closing quote in config tag on line " << linenumber << std::endl;
960 // errorstream << "Opening quote in config tag on line " << linenumber << std::endl;
968 errorstream << "Found a (closing) \" outside a tag: " << filename << ":" << linenumber << std::endl;
972 errorstream << "Found a (opening) \" outside a tag: " << filename << ":" << linenumber << std::endl;
982 // errorstream << "Closing config tag on line " << linenumber << std::endl;
986 * If this finds an <include> then ParseLine can simply call
987 * LoadConf() and load the included config into the same ConfigDataHash
990 if(!this->ParseLine(target, line, linenumber, errorstream))
997 errorstream << "Got a closing > when we weren't inside a tag: " << filename << ":" << linenumber << std::endl;
1007 bool ServerConfig::LoadConf(ConfigDataHash &target, const std::string &filename, std::ostringstream &errorstream)
1009 return this->LoadConf(target, filename.c_str(), errorstream);
1012 bool ServerConfig::ParseLine(ConfigDataHash &target, std::string &line, long linenumber, std::ostringstream &errorstream)
1014 std::string tagname;
1015 std::string current_key;
1016 std::string current_value;
1022 got_name = got_key = in_quote = false;
1024 // std::cout << "ParseLine(data, '" << line << "', " << linenumber << ", stream)" << std::endl;
1026 for(std::string::iterator c = line.begin(); c != line.end(); c++)
1030 /* We don't know the tag name yet. */
1041 /* We got to a space, we should have the tagname now. */
1042 if(tagname.length())
1050 /* We have the tag name */
1053 /* We're still reading the key name */
1063 /* We got an '=', end of the key name. */
1069 /* We have the key name, now we're looking for quotes and the value */
1071 /* Correctly handle escaped characters here.
1072 * See the XXX'ed section above.
1074 if ((*c == '\\') && (in_quote))
1077 current_value += *c;
1084 /* We're not already in a quote. */
1089 /* Leaving quotes, we have the value */
1090 results.push_back(KeyVal(current_key, current_value));
1092 // std::cout << "<" << tagname << ":" << current_key << "> " << current_value << std::endl;
1097 if((tagname == "include") && (current_key == "file"))
1099 if(!this->DoInclude(target, current_value, errorstream))
1103 current_key.clear();
1104 current_value.clear();
1111 current_value += *c;
1118 /* Finished parsing the tag, add it to the config hash */
1119 target.insert(std::pair<std::string, KeyValList > (tagname, results));
1124 bool ServerConfig::DoInclude(ConfigDataHash &target, const std::string &file, std::ostringstream &errorstream)
1126 std::string confpath;
1127 std::string newfile;
1128 std::string::size_type pos;
1130 confpath = CONFIG_FILE;
1133 for (std::string::iterator c = newfile.begin(); c != newfile.end(); c++)
1143 if((pos = confpath.find("/inspircd.conf")) != std::string::npos)
1145 /* Leaves us with just the path */
1146 newfile = confpath.substr(0, pos) + std::string("/") + newfile;
1150 errorstream << "Couldn't get config path from: " << confpath << std::endl;
1155 return LoadConf(target, newfile, errorstream);
1158 bool ServerConfig::ConfValue(ConfigDataHash &target, const char* tag, const char* var, int index, char* result, int length)
1161 bool r = ConfValue(target, std::string(tag), std::string(var), index, value);
1162 strlcpy(result, value.c_str(), length);
1166 bool ServerConfig::ConfValue(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, std::string &result)
1168 ConfigDataHash::size_type pos = index;
1169 if((pos >= 0) && (pos < target.count(tag)))
1171 ConfigDataHash::const_iterator iter = target.find(tag);
1173 for(int i = 0; i < index; i++)
1176 for(KeyValList::const_iterator j = iter->second.begin(); j != iter->second.end(); j++)
1187 log(DEBUG, "No <%s> tags in config file.", tag.c_str());
1191 log(DEBUG, "ConfValue got an out-of-range index %d, there are only %d occurences of %s", pos, target.count(tag), tag.c_str());
1197 bool ServerConfig::ConfValueInteger(ConfigDataHash &target, const char* tag, const char* var, int index, int &result)
1199 return ConfValueInteger(target, std::string(tag), std::string(var), index, result);
1202 bool ServerConfig::ConfValueInteger(ConfigDataHash &target, const std::string &tag, const std::string &var, int index, int &result)
1205 std::istringstream stream;
1206 bool r = ConfValue(target, tag, var, index, value);
1208 if(!(stream >> result))
1213 bool ServerConfig::ConfValueBool(ConfigDataHash &target, const char* tag, const char* var, int index)
1215 return ConfValueBool(target, std::string(tag), std::string(var), index);
1218 bool ServerConfig::ConfValueBool(ConfigDataHash &target, const std::string &tag, const std::string &var, int index)
1221 if(!ConfValue(target, tag, var, index, result))
1224 return ((result == "yes") || (result == "true") || (result == "1"));
1227 int ServerConfig::ConfValueEnum(ConfigDataHash &target, const char* tag)
1229 return target.count(tag);
1232 int ServerConfig::ConfValueEnum(ConfigDataHash &target, const std::string &tag)
1234 return target.count(tag);
1237 int ServerConfig::ConfVarEnum(ConfigDataHash &target, const char* tag, int index)
1239 return ConfVarEnum(target, std::string(tag), index);
1242 int ServerConfig::ConfVarEnum(ConfigDataHash &target, const std::string &tag, int index)
1244 ConfigDataHash::size_type pos = index;
1246 if((pos >= 0) && (pos < target.count(tag)))
1248 ConfigDataHash::const_iterator iter = target.find(tag);
1250 for(int i = 0; i < index; i++)
1253 return iter->second.size();
1257 log(DEBUG, "No <%s> tags in config file.", tag.c_str());
1261 log(DEBUG, "ConfVarEnum got an out-of-range index %d, there are only %d occurences of %s", pos, target.count(tag), tag.c_str());