Move FileExists, CleanFilename, DirValid, GetFullProgDir into class ServerConfig

git-svn-id: http://svn.inspircd.org/repository/trunk/inspircd@4848 e03df62e-2008-0410-955e-edbf42e46eb7
This commit is contained in:
brain 2006-08-10 18:28:37 +00:00
parent fd2eb26472
commit 6c9588419d
8 changed files with 153 additions and 160 deletions

View File

@ -405,6 +405,12 @@ class ServerConfig : public Extensible
Module* GetIOHook(int port);
bool AddIOHook(int port, Module* iomod);
bool DelIOHook(int port);
static std::string GetFullProgDir(char** argv, int argc);
static bool DirValid(const char* dirandfile);
static char* CleanFilename(char* name);
static bool FileExists(const char* file);
};
bool InitializeDisabledCommands(const char* data, InspIRCd* ServerInstance);

View File

@ -19,12 +19,9 @@
// include the common header files
#include <typeinfo>
#include <iostream>
#include <string>
#include <deque>
#include "users.h"
#include "channels.h"
typedef std::deque<std::string> file_cache;
typedef std::pair< std::string, std::string > KeyVal;

View File

@ -53,10 +53,6 @@ void Error(int status);
void ShowMOTD(userrec *user);
void ShowRULES(userrec *user);
bool AllModulesReportReady(userrec* user);
bool DirValid(char* dirandfile);
bool FileExists(const char* file);
char* CleanFilename(char* name);
std::string GetFullProgDir(char** argv, int argc);
int InsertMode(std::string &output, const char* modes, unsigned short section);
bool IsValidChannelName(const char *);

View File

@ -76,7 +76,7 @@ void cmd_modules::Handle (const char** parameters, int pcnt, userrec *user)
{
if (match(ServerInstance->Config->module_names[i].c_str(),parameters[1]))
{
user->WriteServ("900 %s :0x%08lx %d.%d.%d.%d %s (%s)",user->nick,modules[i],V.Major,V.Minor,V.Revision,V.Build,CleanFilename(modulename),flagstate+2);
user->WriteServ("900 %s :0x%08lx %d.%d.%d.%d %s (%s)",user->nick,modules[i],V.Major,V.Minor,V.Revision,V.Build,ServerConfig::CleanFilename(modulename),flagstate+2);
for (int it = 0; itab[it];)
{
char data[MAXBUF];
@ -95,7 +95,7 @@ void cmd_modules::Handle (const char** parameters, int pcnt, userrec *user)
}
}
if (*dlist)
user->WriteServ("900 %s :%s [ %s]",user->nick,CleanFilename(modulename),dlist);
user->WriteServ("900 %s :%s [ %s]",user->nick,ServerConfig::CleanFilename(modulename),dlist);
}
user->WriteServ("900 %s :=== DEBUG: Implementation counts ===",user->nick);
for (int it = 0; itab[it]; it++)
@ -107,12 +107,12 @@ void cmd_modules::Handle (const char** parameters, int pcnt, userrec *user)
}
else
{
user->WriteServ("900 %s :0x%08lx %d.%d.%d.%d %s (%s)",user->nick,modules[i],V.Major,V.Minor,V.Revision,V.Build,CleanFilename(modulename),flagstate+2);
user->WriteServ("900 %s :0x%08lx %d.%d.%d.%d %s (%s)",user->nick,modules[i],V.Major,V.Minor,V.Revision,V.Build,ServerConfig::CleanFilename(modulename),flagstate+2);
}
}
else
{
user->WriteServ("900 %s :%s",user->nick,CleanFilename(modulename));
user->WriteServ("900 %s :%s",user->nick,ServerConfig::CleanFilename(modulename));
}
}
user->WriteServ("901 %s :End of MODULES list",user->nick);

View File

@ -19,7 +19,6 @@
#include "users.h"
#include "modules.h"
#include "commands.h"
#include "helperfuncs.h"
#include "commands/cmd_rehash.h"
extern InspIRCd* ServerInstance;
@ -29,7 +28,7 @@ extern FactoryList factory;
void cmd_rehash::Handle (const char** parameters, int pcnt, userrec *user)
{
user->WriteServ("382 %s %s :Rehashing",user->nick,CleanFilename(CONFIG_FILE));
user->WriteServ("382 %s %s :Rehashing",user->nick,ServerConfig::CleanFilename(CONFIG_FILE));
std::string parameter = "";
if (pcnt)
{
@ -37,7 +36,7 @@ void cmd_rehash::Handle (const char** parameters, int pcnt, userrec *user)
}
else
{
ServerInstance->WriteOpers("%s is rehashing config file %s",user->nick,CleanFilename(CONFIG_FILE));
ServerInstance->WriteOpers("%s is rehashing config file %s",user->nick,ServerConfig::CleanFilename(CONFIG_FILE));
ServerInstance->Config->Read(false,user);
}
FOREACH_MOD(I_OnRehash,OnRehash(parameter));

View File

@ -1323,3 +1323,113 @@ bool ServerConfig::ReadFile(file_cache &F, const char* fname)
return true;
}
bool ServerConfig::FileExists(const char* file)
{
FILE *input;
if ((input = fopen (file, "r")) == NULL)
{
return false;
}
else
{
fclose(input);
return true;
}
}
char* ServerConfig::CleanFilename(char* name)
{
char* p = name + strlen(name);
while ((p != name) && (*p != '/')) p--;
return (p != name ? ++p : p);
}
bool ServerConfig::DirValid(const char* dirandfile)
{
char work[MAXBUF];
char buffer[MAXBUF];
char otherdir[MAXBUF];
int p;
strlcpy(work, dirandfile, MAXBUF);
p = strlen(work);
// we just want the dir
while (*work)
{
if (work[p] == '/')
{
work[p] = '\0';
break;
}
work[p--] = '\0';
}
// Get the current working directory
if (getcwd(buffer, MAXBUF ) == NULL )
return false;
chdir(work);
if (getcwd(otherdir, MAXBUF ) == NULL )
return false;
chdir(buffer);
size_t t = strlen(work);
if (strlen(otherdir) >= t)
{
otherdir[t] = '\0';
if (!strcmp(otherdir,work))
{
return true;
}
return false;
}
else
{
return false;
}
}
std::string ServerConfig::GetFullProgDir(char** argv, int argc)
{
char work[MAXBUF];
char buffer[MAXBUF];
char otherdir[MAXBUF];
int p;
strlcpy(work,argv[0],MAXBUF);
p = strlen(work);
// we just want the dir
while (*work)
{
if (work[p] == '/')
{
work[p] = '\0';
break;
}
work[p--] = '\0';
}
// Get the current working directory
if (getcwd(buffer, MAXBUF) == NULL)
return "";
chdir(work);
if (getcwd(otherdir, MAXBUF) == NULL)
return "";
chdir(buffer);
return otherdir;
}

View File

@ -507,121 +507,6 @@ bool AllModulesReportReady(userrec* user)
return true;
}
/* Make Sure Modules Are Avaliable!
* (BugFix By Craig.. See? I do work! :p)
* Modified by brain, requires const char*
* to work with other API functions
*/
/* XXX - Needed? */
bool FileExists (const char* file)
{
FILE *input;
if ((input = fopen (file, "r")) == NULL)
{
return(false);
}
else
{
fclose (input);
return(true);
}
}
char* CleanFilename(char* name)
{
char* p = name + strlen(name);
while ((p != name) && (*p != '/')) p--;
return (p != name ? ++p : p);
}
bool DirValid(char* dirandfile)
{
char work[MAXBUF];
char buffer[MAXBUF];
char otherdir[MAXBUF];
int p;
strlcpy(work, dirandfile, MAXBUF);
p = strlen(work);
// we just want the dir
while (*work)
{
if (work[p] == '/')
{
work[p] = '\0';
break;
}
work[p--] = '\0';
}
// Get the current working directory
if (getcwd(buffer, MAXBUF ) == NULL )
return false;
chdir(work);
if (getcwd(otherdir, MAXBUF ) == NULL )
return false;
chdir(buffer);
size_t t = strlen(work);
if (strlen(otherdir) >= t)
{
otherdir[t] = '\0';
if (!strcmp(otherdir,work))
{
return true;
}
return false;
}
else
{
return false;
}
}
std::string GetFullProgDir(char** argv, int argc)
{
char work[MAXBUF];
char buffer[MAXBUF];
char otherdir[MAXBUF];
int p;
strlcpy(work,argv[0],MAXBUF);
p = strlen(work);
// we just want the dir
while (*work)
{
if (work[p] == '/')
{
work[p] = '\0';
break;
}
work[p--] = '\0';
}
// Get the current working directory
if (getcwd(buffer, MAXBUF) == NULL)
return "";
chdir(work);
if (getcwd(otherdir, MAXBUF) == NULL)
return "";
chdir(buffer);
return otherdir;
}
int InsertMode(std::string &output, const char* mode, unsigned short section)
{
unsigned short currsection = 1;
@ -685,7 +570,7 @@ void OpenLog(char** argv, int argc)
{
if (ServerInstance->Config->logpath == "")
{
ServerInstance->Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
ServerInstance->Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
}
}
else

View File

@ -141,7 +141,7 @@ void Killed(int status)
void Rehash(int status)
{
ServerInstance->WriteOpers("Rehashing config file %s due to SIGHUP",CleanFilename(CONFIG_FILE));
ServerInstance->WriteOpers("Rehashing config file %s due to SIGHUP",ServerConfig::CleanFilename(CONFIG_FILE));
fclose(ServerInstance->Config->log_file);
OpenLog(NULL,0);
ServerInstance->Config->Read(false,NULL);
@ -234,7 +234,7 @@ InspIRCd::InspIRCd(int argc, char** argv)
this->startup_time = time(NULL);
srand(time(NULL));
log(DEBUG,"*** InspIRCd starting up!");
if (!FileExists(CONFIG_FILE))
if (!ServerConfig::FileExists(CONFIG_FILE))
{
printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
log(DEFAULT,"main: no config");
@ -559,7 +559,7 @@ bool InspIRCd::LoadModule(const char* filename)
std::string filename_str = filename;
#ifndef STATIC_LINK
#ifndef IS_CYGWIN
if (!DirValid(modfile))
if (!ServerConfig::DirValid(modfile))
{
log(DEFAULT,"Module %s is not within the modules directory.",modfile);
snprintf(MODERR,MAXBUF,"Module %s is not within the modules directory.",modfile);
@ -569,7 +569,7 @@ bool InspIRCd::LoadModule(const char* filename)
#endif
log(DEBUG,"Loading module: %s",modfile);
#ifndef STATIC_LINK
if (FileExists(modfile))
if (ServerConfig::FileExists(modfile))
{
#endif
for (unsigned int j = 0; j < Config->module_names.size(); j++)