From f0680338833dd76966ba1769980abb9eafb50467 Mon Sep 17 00:00:00 2001 From: brain Date: Wed, 9 Aug 2006 14:20:04 +0000 Subject: Tidy up loglevel enum (remove some C-ish defines) change ServerConfig to pass a pointer to 'this' to Validation etc functions git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4811 e03df62e-2008-0410-955e-edbf42e46eb7 --- include/configreader.h | 12 ++++-- include/helperfuncs.h | 17 ++++---- include/modules.h | 4 -- include/users.h | 11 +++--- include/xline.h | 14 ++++--- src/configreader.cpp | 101 ++++++++++++++++++++++++++++++++--------------- src/helperfuncs.cpp | 45 +-------------------- src/modules.cpp | 4 +- src/modules/m_foobar.cpp | 1 + src/users.cpp | 10 ++--- src/xline.cpp | 12 +++--- 11 files changed, 116 insertions(+), 115 deletions(-) diff --git a/include/configreader.h b/include/configreader.h index e2ff1c2c9..79384c2db 100644 --- a/include/configreader.h +++ b/include/configreader.h @@ -26,9 +26,11 @@ #include "globals.h" #include "modules.h" -typedef bool (*Validator)(const char*, const char*, void*); -typedef bool (*MultiValidator)(const char*, char**, void**, int*); -typedef bool (*MultiNotify)(const char*); +class ServerConfig; + +typedef bool (*Validator)(ServerConfig* conf, const char*, const char*, void*); +typedef bool (*MultiValidator)(ServerConfig* conf, const char*, char**, void**, int*); +typedef bool (*MultiNotify)(ServerConfig* conf, const char*); enum ConfigDataType { DT_NOTHING, DT_INTEGER, DT_CHARPTR, DT_BOOLEAN }; @@ -365,6 +367,10 @@ class ServerConfig : public Extensible */ void Read(bool bail, userrec* user); + /** Read a file into a file_cache object + */ + bool ReadFile(file_cache &F, const char* fname); + /** Load 'filename' into 'target', with the new config parser everything is parsed into * tag/key/value at load-time rather than at read-value time. */ diff --git a/include/helperfuncs.h b/include/helperfuncs.h index 44f9aba49..b4089fa18 100644 --- a/include/helperfuncs.h +++ b/include/helperfuncs.h @@ -27,13 +27,16 @@ #include #include -/** Flags for use with log() +/** Debug levels for use with InspIRCd::Log() */ -#define DEBUG 10 -#define VERBOSE 20 -#define DEFAULT 30 -#define SPARSE 40 -#define NONE 50 +enum DebugLevel +{ + DEBUG = 10, + VERBOSE = 20, + DEFAULT = 30, + SPARSE = 40, + NONE = 50, +}; /* I'm not entirely happy with this, the ## before 'args' is a g++ extension. * The problem is that if you #define log(l, x, args...) and then call it @@ -45,8 +48,6 @@ #define STRINGIFY(x) STRINGIFY2(x) #define log(l, x, args...) InspIRCd::Log(l, __FILE__ ":" STRINGIFY(__LINE__) ": " x, ##args) -void readfile(file_cache &F, const char* fname); - void WriteOpers(const char* text, ...); void WriteOpers_NoFormat(const char* text); diff --git a/include/modules.h b/include/modules.h index f687c9ffe..6104b8715 100644 --- a/include/modules.h +++ b/include/modules.h @@ -18,10 +18,6 @@ #ifndef __MODULES_H #define __MODULES_H -/** log levels - */ -enum DebugLevels { DEBUG, VERBOSE, DEFAULT, SPARSE, NONE }; - /** Used with OnAccessCheck() method of modules */ enum AccessControlType { diff --git a/include/users.h b/include/users.h index 82da66c19..42d0f7376 100644 --- a/include/users.h +++ b/include/users.h @@ -699,10 +699,11 @@ namespace irc }; /* Configuration callbacks */ -bool InitTypes(const char* tag); -bool InitClasses(const char* tag); -bool DoType(const char* tag, char** entries, void** values, int* types); -bool DoClass(const char* tag, char** entries, void** values, int* types); -bool DoneClassesAndTypes(const char* tag); +class ServerConfig; +bool InitTypes(ServerConfig* conf, const char* tag); +bool InitClasses(ServerConfig* conf, const char* tag); +bool DoType(ServerConfig* conf, const char* tag, char** entries, void** values, int* types); +bool DoClass(ServerConfig* conf, const char* tag, char** entries, void** values, int* types); +bool DoneClassesAndTypes(ServerConfig* conf, const char* tag); #endif diff --git a/include/xline.h b/include/xline.h index 740ed395d..b04d2224d 100644 --- a/include/xline.h +++ b/include/xline.h @@ -123,13 +123,15 @@ class QLine : public XLine bool is_global; }; -bool InitXLine(const char* tag); -bool DoneXLine(const char* tag); +class ServerConfig; -bool DoZLine(const char* tag, char** entries, void** values, int* types); -bool DoQLine(const char* tag, char** entries, void** values, int* types); -bool DoKLine(const char* tag, char** entries, void** values, int* types); -bool DoELine(const char* tag, char** entries, void** values, int* types); +bool InitXLine(ServerConfig* conf, const char* tag); +bool DoneXLine(ServerConfig* conf, const char* tag); + +bool DoZLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types); +bool DoQLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types); +bool DoKLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types); +bool DoELine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types); bool add_gline(long duration, const char* source, const char* reason, const char* hostmask); bool add_qline(long duration, const char* source, const char* reason, const char* nickname); diff --git a/src/configreader.cpp b/src/configreader.cpp index 068e4d4ee..c8ecdaf39 100644 --- a/src/configreader.cpp +++ b/src/configreader.cpp @@ -145,13 +145,13 @@ bool ServerConfig::CheckOnce(char* tag, bool bail, userrec* user) return true; } -bool NoValidation(const char* tag, const char* value, void* data) +bool NoValidation(ServerConfig* conf, const char* tag, const char* value, void* data) { log(DEBUG,"No validation for <%s:%s>",tag,value); return true; } -bool ValidateTempDir(const char* tag, const char* value, void* data) +bool ValidateTempDir(ServerConfig* conf, const char* tag, const char* value, void* data) { char* x = (char*)data; if (!*x) @@ -159,7 +159,7 @@ bool ValidateTempDir(const char* tag, const char* value, void* data) return true; } -bool ValidateMaxTargets(const char* tag, const char* value, void* data) +bool ValidateMaxTargets(ServerConfig* conf, const char* tag, const char* value, void* data) { int* x = (int*)data; if ((*x < 0) || (*x > 31)) @@ -170,7 +170,7 @@ bool ValidateMaxTargets(const char* tag, const char* value, void* data) return true; } -bool ValidateSoftLimit(const char* tag, const char* value, void* data) +bool ValidateSoftLimit(ServerConfig* conf, const char* tag, const char* value, void* data) { int* x = (int*)data; if ((*x < 1) || (*x > MAXCLIENTS)) @@ -181,7 +181,7 @@ bool ValidateSoftLimit(const char* tag, const char* value, void* data) return true; } -bool ValidateMaxConn(const char* tag, const char* value, void* data) +bool ValidateMaxConn(ServerConfig* conf, const char* tag, const char* value, void* data) { int* x = (int*)data; if (*x > SOMAXCONN) @@ -191,7 +191,7 @@ bool ValidateMaxConn(const char* tag, const char* value, void* data) return true; } -bool ValidateDnsTimeout(const char* tag, const char* value, void* data) +bool ValidateDnsTimeout(ServerConfig* conf, const char* tag, const char* value, void* data) { int* x = (int*)data; if (!*x) @@ -221,7 +221,7 @@ bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance) return true; } -bool ValidateDnsServer(const char* tag, const char* value, void* data) +bool ValidateDnsServer(ServerConfig* conf, const char* tag, const char* value, void* data) { char* x = (char*)data; if (!*x) @@ -260,7 +260,7 @@ bool ValidateDnsServer(const char* tag, const char* value, void* data) return true; } -bool ValidateModPath(const char* tag, const char* value, void* data) +bool ValidateModPath(ServerConfig* conf, const char* tag, const char* value, void* data) { char* x = (char*)data; if (!*x) @@ -269,7 +269,7 @@ bool ValidateModPath(const char* tag, const char* value, void* data) } -bool ValidateServerName(const char* tag, const char* value, void* data) +bool ValidateServerName(ServerConfig* conf, const char* tag, const char* value, void* data) { char* x = (char*)data; if (!strchr(x,'.')) @@ -281,7 +281,7 @@ bool ValidateServerName(const char* tag, const char* value, void* data) return true; } -bool ValidateNetBufferSize(const char* tag, const char* value, void* data) +bool ValidateNetBufferSize(ServerConfig* conf, const char* tag, const char* value, void* data) { if ((!ServerInstance->Config->NetBufferSize) || (ServerInstance->Config->NetBufferSize > 65535) || (ServerInstance->Config->NetBufferSize < 1024)) { @@ -291,7 +291,7 @@ bool ValidateNetBufferSize(const char* tag, const char* value, void* data) return true; } -bool ValidateMaxWho(const char* tag, const char* value, void* data) +bool ValidateMaxWho(ServerConfig* conf, const char* tag, const char* value, void* data) { if ((!ServerInstance->Config->MaxWhoResults) || (ServerInstance->Config->MaxWhoResults > 65535) || (ServerInstance->Config->MaxWhoResults < 1)) { @@ -301,7 +301,7 @@ bool ValidateMaxWho(const char* tag, const char* value, void* data) return true; } -bool ValidateLogLevel(const char* tag, const char* value, void* data) +bool ValidateLogLevel(ServerConfig* conf, const char* tag, const char* value, void* data) { const char* dbg = (const char*)data; ServerInstance->Config->LogLevel = DEFAULT; @@ -321,21 +321,21 @@ bool ValidateLogLevel(const char* tag, const char* value, void* data) return true; } -bool ValidateMotd(const char* tag, const char* value, void* data) +bool ValidateMotd(ServerConfig* conf, const char* tag, const char* value, void* data) { - readfile(ServerInstance->Config->MOTD,ServerInstance->Config->motd); + conf->ReadFile(ServerInstance->Config->MOTD,ServerInstance->Config->motd); return true; } -bool ValidateRules(const char* tag, const char* value, void* data) +bool ValidateRules(ServerConfig* conf, const char* tag, const char* value, void* data) { - readfile(ServerInstance->Config->RULES,ServerInstance->Config->rules); + conf->ReadFile(ServerInstance->Config->RULES,ServerInstance->Config->rules); return true; } /* Callback called before processing the first tag */ -bool InitConnect(const char* tag) +bool InitConnect(ServerConfig* conf, const char* tag) { log(DEFAULT,"Reading connect classes..."); ServerInstance->Config->Classes.clear(); @@ -344,7 +344,7 @@ bool InitConnect(const char* tag) /* Callback called to process a single tag */ -bool DoConnect(const char* tag, char** entries, void** values, int* types) +bool DoConnect(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { ConnectClass c; char* allow = (char*)values[0]; /* Yeah, there are a lot of values. Live with it. */ @@ -407,7 +407,7 @@ bool DoConnect(const char* tag, char** entries, void** values, int* types) /* Callback called when there are no more tags */ -bool DoneConnect(const char* tag) +bool DoneConnect(ServerConfig* conf, const char* tag) { log(DEBUG,"DoneConnect called for tag: %s",tag); return true; @@ -415,7 +415,7 @@ bool DoneConnect(const char* tag) /* Callback called before processing the first tag */ -bool InitULine(const char* tag) +bool InitULine(ServerConfig* conf, const char* tag) { ServerInstance->Config->ulines.clear(); return true; @@ -423,7 +423,7 @@ bool InitULine(const char* tag) /* Callback called to process a single tag */ -bool DoULine(const char* tag, char** entries, void** values, int* types) +bool DoULine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* server = (char*)values[0]; log(DEBUG,"Read ULINE '%s'",server); @@ -433,14 +433,14 @@ bool DoULine(const char* tag, char** entries, void** values, int* types) /* Callback called when there are no more tags */ -bool DoneULine(const char* tag) +bool DoneULine(ServerConfig* conf, const char* tag) { return true; } /* Callback called before processing the first tag */ -bool InitModule(const char* tag) +bool InitModule(ServerConfig* conf, const char* tag) { old_module_names.clear(); new_module_names.clear(); @@ -455,7 +455,7 @@ bool InitModule(const char* tag) /* Callback called to process a single tag */ -bool DoModule(const char* tag, char** entries, void** values, int* types) +bool DoModule(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* modname = (char*)values[0]; new_module_names.push_back(modname); @@ -464,7 +464,7 @@ bool DoModule(const char* tag, char** entries, void** values, int* types) /* Callback called when there are no more tags */ -bool DoneModule(const char* tag) +bool DoneModule(ServerConfig* conf, const char* tag) { // now create a list of new modules that are due to be loaded // and a seperate list of modules which are due to be unloaded @@ -499,7 +499,7 @@ bool DoneModule(const char* tag) /* Callback called before processing the first tag */ -bool InitMaxBans(const char* tag) +bool InitMaxBans(ServerConfig* conf, const char* tag) { ServerInstance->Config->maxbans.clear(); return true; @@ -507,7 +507,7 @@ bool InitMaxBans(const char* tag) /* Callback called to process a single tag */ -bool DoMaxBans(const char* tag, char** entries, void** values, int* types) +bool DoMaxBans(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* channel = (char*)values[0]; int* limit = (int*)values[1]; @@ -517,7 +517,7 @@ bool DoMaxBans(const char* tag, char** entries, void** values, int* types) /* Callback called when there are no more tags. */ -bool DoneMaxBans(const char* tag) +bool DoneMaxBans(ServerConfig* conf, const char* tag) { return true; } @@ -737,7 +737,7 @@ void ServerConfig::Read(bool bail, userrec* user) break; } - Values[Index].validation_function(Values[Index].tag, Values[Index].value, Values[Index].val); + Values[Index].validation_function(this, Values[Index].tag, Values[Index].value, Values[Index].val); } /* Claim memory for use when reading multiple tags @@ -753,7 +753,7 @@ void ServerConfig::Read(bool bail, userrec* user) /* XXX - Make this use ConfValueInteger and so on */ for (int Index = 0; MultiValues[Index].tag; Index++) { - MultiValues[Index].init_function(MultiValues[Index].tag); + MultiValues[Index].init_function(this, MultiValues[Index].tag); int number_of_tags = ConfValueEnum(this->config_data, MultiValues[Index].tag); @@ -780,10 +780,10 @@ void ServerConfig::Read(bool bail, userrec* user) break; } } - MultiValues[Index].validation_function(MultiValues[Index].tag, (char**)MultiValues[Index].items, ptr, MultiValues[Index].datatype); + MultiValues[Index].validation_function(this, MultiValues[Index].tag, (char**)MultiValues[Index].items, ptr, MultiValues[Index].datatype); } - MultiValues[Index].finish_function(MultiValues[Index].tag); + MultiValues[Index].finish_function(this, MultiValues[Index].tag); } /* Free any memory we claimed @@ -1286,3 +1286,40 @@ int ServerConfig::ConfVarEnum(ConfigDataHash &target, const std::string &tag, in return 0; } + +/** Read the contents of a file located by `fname' into a file_cache pointed at by `F'. + */ +bool ServerConfig::ReadFile(file_cache &F, const char* fname) +{ + FILE* file; + char linebuf[MAXBUF]; + + F.clear(); + file = fopen(fname,"r"); + + if (file) + { + while (!feof(file)) + { + fgets(linebuf,sizeof(linebuf),file); + linebuf[strlen(linebuf)-1]='\0'; + + if (!*linebuf) + { + strcpy(linebuf," "); + } + + if (!feof(file)) + { + F.push_back(linebuf); + } + } + + fclose(file); + } + else + return false; + + return true; +} + diff --git a/src/helperfuncs.cpp b/src/helperfuncs.cpp index 97cb68f33..5adc18650 100644 --- a/src/helperfuncs.cpp +++ b/src/helperfuncs.cpp @@ -61,7 +61,7 @@ extern std::vector local_users; static char TIMESTR[26]; static time_t LAST = 0; -/** log() +/** Log() * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level' * is greater than the configured loglevel. */ @@ -107,49 +107,6 @@ void InspIRCd::Log(int level, const std::string &text) } } -/** readfile() - * Read the contents of a file located by `fname' into a file_cache pointed at by `F'. - * - * XXX - we may want to consider returning a file_cache or pointer to one, less confusing. - */ -void readfile(file_cache &F, const char* fname) -{ - FILE* file; - char linebuf[MAXBUF]; - - log(DEBUG,"readfile: loading %s",fname); - F.clear(); - file = fopen(fname,"r"); - - if (file) - { - while (!feof(file)) - { - fgets(linebuf,sizeof(linebuf),file); - linebuf[strlen(linebuf)-1]='\0'; - - if (!*linebuf) - { - strcpy(linebuf," "); - } - - if (!feof(file)) - { - F.push_back(linebuf); - } - } - - fclose(file); - } - else - { - log(DEBUG,"readfile: failed to load file: %s",fname); - } - - log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size()); -} - - std::string GetServerDescription(const char* servername) { std::string description = ""; diff --git a/src/modules.cpp b/src/modules.cpp index 428af2c6b..e49975500 100644 --- a/src/modules.cpp +++ b/src/modules.cpp @@ -838,7 +838,7 @@ bool ConfigReader::Verify() FileReader::FileReader(const std::string &filename) { file_cache c; - readfile(c,filename.c_str()); + ServerInstance->Config->ReadFile(c,filename.c_str()); this->fc = c; this->CalcSize(); } @@ -874,7 +874,7 @@ void FileReader::CalcSize() void FileReader::LoadFile(const std::string &filename) { file_cache c; - readfile(c,filename.c_str()); + ServerInstance->Config->ReadFile(c,filename.c_str()); this->fc = c; this->CalcSize(); } diff --git a/src/modules/m_foobar.cpp b/src/modules/m_foobar.cpp index 9cd8cb45e..6db01bc03 100644 --- a/src/modules/m_foobar.cpp +++ b/src/modules/m_foobar.cpp @@ -19,6 +19,7 @@ using namespace std; #include "users.h" #include "channels.h" #include "modules.h" +#include "helperfuncs.h" /* $ModDesc: A dummy module for testing */ diff --git a/src/users.cpp b/src/users.cpp index 176def818..53e4695f6 100644 --- a/src/users.cpp +++ b/src/users.cpp @@ -56,7 +56,7 @@ operclass_t operclass; /* XXX: Used for speeding up WriteCommon operations */ unsigned long uniq_id = 0; -bool InitTypes(const char* tag) +bool InitTypes(ServerConfig* conf, const char* tag) { for (opertype_t::iterator n = opertypes.begin(); n != opertypes.end(); n++) { @@ -68,7 +68,7 @@ bool InitTypes(const char* tag) return true; } -bool InitClasses(const char* tag) +bool InitClasses(ServerConfig* conf, const char* tag) { for (operclass_t::iterator n = operclass.begin(); n != operclass.end(); n++) { @@ -80,7 +80,7 @@ bool InitClasses(const char* tag) return true; } -bool DoType(const char* tag, char** entries, void** values, int* types) +bool DoType(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* TypeName = (char*)values[0]; char* Classes = (char*)values[1]; @@ -90,7 +90,7 @@ bool DoType(const char* tag, char** entries, void** values, int* types) return true; } -bool DoClass(const char* tag, char** entries, void** values, int* types) +bool DoClass(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* ClassName = (char*)values[0]; char* CommandList = (char*)values[1]; @@ -100,7 +100,7 @@ bool DoClass(const char* tag, char** entries, void** values, int* types) return true; } -bool DoneClassesAndTypes(const char* tag) +bool DoneClassesAndTypes(ServerConfig* conf, const char* tag) { return true; } diff --git a/src/xline.cpp b/src/xline.cpp index ae66b939d..eb0fa559c 100644 --- a/src/xline.cpp +++ b/src/xline.cpp @@ -97,18 +97,18 @@ bool KSortComparison ( const KLine one, const KLine two ); // this way these days, such as qlines against // services nicks, etc. -bool InitXLine(const char* tag) +bool InitXLine(ServerConfig* conf, const char* tag) { return true; } -bool DoneXLine(const char* tag) +bool DoneXLine(ServerConfig* conf, const char* tag) { apply_lines(APPLY_ALL); return true; } -bool DoZLine(const char* tag, char** entries, void** values, int* types) +bool DoZLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* reason = (char*)values[0]; char* ipmask = (char*)values[1]; @@ -118,7 +118,7 @@ bool DoZLine(const char* tag, char** entries, void** values, int* types) return true; } -bool DoQLine(const char* tag, char** entries, void** values, int* types) +bool DoQLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* reason = (char*)values[0]; char* nick = (char*)values[1]; @@ -128,7 +128,7 @@ bool DoQLine(const char* tag, char** entries, void** values, int* types) return true; } -bool DoKLine(const char* tag, char** entries, void** values, int* types) +bool DoKLine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* reason = (char*)values[0]; char* host = (char*)values[1]; @@ -138,7 +138,7 @@ bool DoKLine(const char* tag, char** entries, void** values, int* types) return true; } -bool DoELine(const char* tag, char** entries, void** values, int* types) +bool DoELine(ServerConfig* conf, const char* tag, char** entries, void** values, int* types) { char* reason = (char*)values[0]; char* host = (char*)values[1]; -- cgit v1.2.3