]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/helperfuncs.cpp
Really clever VOODOO.
[user/henk/code/inspircd.git] / src / helperfuncs.cpp
index 50d9b06318323c440bbaf594267afed55f9a4a46..d96c068ed445e8693c63b45392d7dcaceb284a74 100644 (file)
@@ -2,49 +2,24 @@
  *       | Inspire Internet Relay Chat Daemon |
  *       +------------------------------------+
  *
- *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
- *                    E-mail:
- *             <brain@chatspike.net>
- *             <Craig@chatspike.net>
+ *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
+ * See: http://www.inspircd.org/wiki/index.php/Credits
  *
- * Written by Craig Edwards, Craig McLure, and others.
  * This program is free but copyrighted software; see
- *         the file COPYING for details.
+ *            the file COPYING for details.
  *
  * ---------------------------------------------------
  */
 
 #include <stdarg.h>
-#include "inspircd_config.h"
 #include "configreader.h"
-#include <unistd.h>
-#include <fcntl.h>
-#include <sys/errno.h>
-#include <signal.h>
-#include <time.h>
-#include <string>
-#include <sstream>
-#ifdef HAS_EXECINFO
-#include <execinfo.h>
-#endif
-#include "connection.h"
 #include "users.h"
-#include "ctables.h"
-#include "globals.h"
 #include "modules.h"
-#include "dynamic.h"
 #include "wildcard.h"
 #include "mode.h"
 #include "xline.h"
-#include "commands.h"
-#include "inspstring.h"
-#include "helperfuncs.h"
-#include "hashcomp.h"
-#include "typedefs.h"
 #include "inspircd.h"
 
-extern time_t TIME;
-
 static char TIMESTR[26];
 static time_t LAST = 0;
 
@@ -54,43 +29,46 @@ static time_t LAST = 0;
  */
 void InspIRCd::Log(int level, const char* text, ...)
 {
+       /* Do this check again here so that we save pointless vsnprintf calls */
+       if ((level < Config->LogLevel) && !Config->forcedebug)
+               return;
+
        va_list argsPtr;
-       char textbuffer[MAXBUF];
+       char textbuffer[65536];
 
        va_start(argsPtr, text);
-       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       vsnprintf(textbuffer, 65536, text, argsPtr);
        va_end(argsPtr);
 
-       InspIRCd::Log(level, std::string(textbuffer));
+       this->Log(level, std::string(textbuffer));
 }
 
 void InspIRCd::Log(int level, const std::string &text)
 {
-       extern InspIRCd* ServerInstance;
-
-       if (!ServerInstance || !ServerInstance->Config)
+       if (!this->Config)
                return;
 
        /* If we were given -debug we output all messages, regardless of configured loglevel */
-       if ((level < ServerInstance->Config->LogLevel) && !ServerInstance->Config->forcedebug)
+       if ((level < Config->LogLevel) && !Config->forcedebug)
                return;
 
-       if (TIME != LAST)
+       if (Time() != LAST)
        {
-               struct tm *timeinfo = localtime(&TIME);
+               time_t local = Time();
+               struct tm *timeinfo = localtime(&local);
 
                strlcpy(TIMESTR,asctime(timeinfo),26);
                TIMESTR[24] = ':';
-               LAST = TIME;
+               LAST = Time();
        }
 
-       if (ServerInstance->Config->log_file && ServerInstance->Config->writelog)
+       if (Config->log_file && Config->writelog)
        {
-               fprintf(ServerInstance->Config->log_file,"%s %s\n",TIMESTR,text.c_str());
-               fflush(ServerInstance->Config->log_file);
+               std::string out = std::string(TIMESTR) + " " + text.c_str() + "\n";
+               this->Logger->WriteLogLine(out);
        }
 
-       if (ServerInstance->Config->nofork)
+       if (Config->nofork)
        {
                printf("%s %s\n", TIMESTR, text.c_str());
        }
@@ -193,9 +171,9 @@ void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
        int modelen;
        va_list argsPtr;
 
-       if ((!text) || (!modes) || (!flags))
+       if (!text || !modes || !flags)
        {
-               log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
+               this->Log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
                return;
        }
 
@@ -204,14 +182,12 @@ void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
        va_end(argsPtr);
        modelen = strlen(modes);
 
-       for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
+       if (flags == WM_AND)
        {
-               userrec* t = (userrec*)(*i);
-               bool send_to_user = false;
-
-               if (flags == WM_AND)
+               for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
                {
-                       send_to_user = true;
+                       userrec* t = *i;
+                       bool send_to_user = true;
 
                        for (int n = 0; n < modelen; n++)
                        {
@@ -221,10 +197,17 @@ void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
                                        break;
                                }
                        }
+                       if (send_to_user)
+                               t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
                }
-               else if (flags == WM_OR)
+       }
+       else
+       if (flags == WM_OR)
+       {
+               for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
                {
-                       send_to_user = false;
+                       userrec* t = *i;
+                       bool send_to_user = false;
 
                        for (int n = 0; n < modelen; n++)
                        {
@@ -234,11 +217,8 @@ void InspIRCd::WriteMode(const char* modes, int flags, const char* text, ...)
                                        break;
                                }
                        }
-               }
-
-               if (send_to_user)
-               {
-                       t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
+                       if (send_to_user)
+                               t->WriteServ("NOTICE %s :%s",t->nick,textbuffer);
                }
        }
 }
@@ -322,59 +302,20 @@ void InspIRCd::SendError(const char *s)
        }
 }
 
-void InspIRCd::Error(int status)
-{
-       void *array[300];
-       size_t size;
-       char **strings;
-
-       signal(SIGALRM, SIG_IGN);
-       signal(SIGPIPE, SIG_IGN);
-       signal(SIGTERM, SIG_IGN);
-       signal(SIGABRT, SIG_IGN);
-       signal(SIGSEGV, SIG_IGN);
-       signal(SIGURG, SIG_IGN);
-       signal(SIGKILL, SIG_IGN);
-       log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
-#ifdef HAS_EXECINFO
-       log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
-       size = backtrace(array, 30);
-       strings = backtrace_symbols(array, size);
-       for (size_t i = 0; i < size; i++) {
-               log(DEFAULT,"[%d] %s", i, strings[i]);
-       }
-       free(strings);
-#else
-       log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
-#endif
-       signal(SIGSEGV, SIG_DFL);
-       if (raise(SIGSEGV) == -1)
-       {
-               log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
-       }
-       Exit(status);
-}
-
 // this function counts all users connected, wether they are registered or NOT.
-int InspIRCd::usercnt()
+int InspIRCd::UserCount()
 {
        return clientlist.size();
 }
 
 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
-int InspIRCd::registered_usercount()
+int InspIRCd::RegisteredUserCount()
 {
        int c = 0;
-
-       for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
-       {
-               c += (i->second->registered == REG_ALL);
-       }
-
-       return c;
+       return this->UnregisteredUserCount() - clientlist.size();
 }
 
-int InspIRCd::usercount_invisible()
+int InspIRCd::InvisibleUserCount()
 {
        int c = 0;
 
@@ -386,7 +327,7 @@ int InspIRCd::usercount_invisible()
        return c;
 }
 
-int InspIRCd::usercount_opers()
+int InspIRCd::OperCount()
 {
        int c = 0;
 
@@ -398,7 +339,7 @@ int InspIRCd::usercount_opers()
        return c;
 }
 
-int InspIRCd::usercount_unknown()
+int InspIRCd::UnregisteredUserCount()
 {
        int c = 0;
 
@@ -412,12 +353,12 @@ int InspIRCd::usercount_unknown()
        return c;
 }
 
-long InspIRCd::chancount()
+long InspIRCd::ChannelCount()
 {
        return chanlist.size();
 }
 
-long InspIRCd::local_count()
+long InspIRCd::LocalUserCount()
 {
        int c = 0;
 
@@ -464,6 +405,34 @@ bool InspIRCd::IsChannel(const char *chname)
        return true;
 }
 
+bool InspIRCd::IsNick(const char* n)
+{
+       if (!n || !*n)
+               return false;
+       int p = 0;
+       for (char* i = (char*)n; *i; i++, p++)
+       {
+               if ((*i >= 'A') && (*i <= '}'))
+               {
+                       /* "A"-"}" can occur anywhere in a nickname */
+                       continue;
+               }
+
+               if ((((*i >= '0') && (*i <= '9')) || (*i == '-')) && (i > n))
+               {
+                       /* "0"-"9", "-" can occur anywhere BUT the first char of a nickname */
+                       continue;
+               }
+
+               /* invalid character! abort */
+               return false;
+       }
+
+       /* too long? or not -- pointer arithmetic rocks */
+       return (p < NICKMAX - 1);
+}
+
 void InspIRCd::OpenLog(char** argv, int argc)
 {
        if (!*this->LogFileName)
@@ -472,26 +441,21 @@ void InspIRCd::OpenLog(char** argv, int argc)
                {
                        Config->logpath = ServerConfig::GetFullProgDir(argv,argc) + "/ircd.log";
                }
+
+               Config->log_file = fopen(Config->logpath.c_str(),"a+");
        }
        else
        {
                Config->log_file = fopen(this->LogFileName,"a+");
-
-               if (!Config->log_file)
-               {
-                       printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
-                       Exit(ERROR);
-               }
-               return;
        }
 
-       Config->log_file = fopen(Config->logpath.c_str(),"a+");
-
        if (!Config->log_file)
        {
-               printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
+               printf("ERROR: Could not write to logfile %s: %s\n\n", Config->logpath.c_str(), strerror(errno));
                Exit(ERROR);
        }
+
+       this->Logger = new FileLogger(this, Config->log_file);
 }
 
 void InspIRCd::CheckRoot()
@@ -499,7 +463,7 @@ void InspIRCd::CheckRoot()
        if (geteuid() == 0)
        {
                printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
-               log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
+               this->Log(DEFAULT,"Cant start as root");
                Exit(ERROR);
        }
 }
@@ -509,7 +473,7 @@ void InspIRCd::CheckDie()
        if (*Config->DieValue)
        {
                printf("WARNING: %s\n\n",Config->DieValue);
-               log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
+               this->Log(DEFAULT,"Died because of <die> tag: %s",Config->DieValue);
                Exit(ERROR);
        }
 }
@@ -528,12 +492,34 @@ void InspIRCd::LoadAllModules()
                
                if (!this->LoadModule(configToken))             
                {
-                       log(DEFAULT,"Exiting due to a module loader error.");
+                       this->Log(DEFAULT,"There was an error loading a module: %s", this->ModuleError());
                        printf("\nThere was an error loading a module: %s\n\n",this->ModuleError());
                        Exit(ERROR);
                }
        }
        printf("\nA total of \033[1;32m%d\033[0m module%s been loaded.\n", this->ModCount+1, this->ModCount+1 == 1 ? " has" : "s have");
-       log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);
+       this->Log(DEFAULT,"Total loaded modules: %d", this->ModCount+1);
+}
+
+void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const std::string &text)
+{
+       std::string copy_text = text;
+
+       int MOD_RESULT = 0;
+       FOREACH_RESULT_I(this, I_OnWhoisLine, OnWhoisLine(user, dest, numeric, copy_text));
+
+       if (!MOD_RESULT)
+               user->WriteServ("%d %s", numeric, copy_text.c_str());
+}
+
+void InspIRCd::SendWhoisLine(userrec* user, userrec* dest, int numeric, const char* format, ...)
+{
+       char textbuffer[MAXBUF];
+       va_list argsPtr;
+       va_start (argsPtr, format);
+       vsnprintf(textbuffer, MAXBUF, format, argsPtr);
+       va_end(argsPtr);
+
+       this->SendWhoisLine(user, dest, numeric, std::string(textbuffer));
 }