-#include "globals.h"
-#include "modules.h"
-#include "dynamic.h"
-#include "wildcard.h"
-#include "message.h"
-#include "mode.h"
-#include "xline.h"
-#include "commands.h"
-#include "inspstring.h"
-#include "helperfuncs.h"
-#include "hashcomp.h"
-#include "typedefs.h"
-
-extern int MODCOUNT;
-extern ModuleList modules;
-extern ServerConfig *Config;
-extern InspIRCd* ServerInstance;
-extern time_t TIME;
-extern char lowermap[255];
-extern userrec* fd_ref_table[MAX_DESCRIPTORS];
-static char already_sent[MAX_DESCRIPTORS];
-extern std::vector<userrec*> all_opers;
-extern user_hash clientlist;
-extern chan_hash chanlist;
-
-char LOG_FILE[MAXBUF];
-
-extern std::vector<userrec*> local_users;
-
-static char TIMESTR[26];
-static time_t LAST = 0;
-
-/** 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.
- */
-void log(int level, char *text, ...)
-{
- va_list argsPtr;
- char textbuffer[MAXBUF];
-
- /* If we were given -debug we output all messages, regardless of configured loglevel */
- if ((level < Config->LogLevel) && !Config->forcedebug)
- return;
-
- if (TIME != LAST)
- {
- struct tm *timeinfo = localtime(&TIME);
-
- strlcpy(TIMESTR,asctime(timeinfo),26);
- TIMESTR[24] = ':';
- LAST = TIME;
- }
-
- if (Config->log_file)
- {
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
-
- if (Config->writelog)
- fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
- }
-
- if (Config->nofork)
- {
- printf("%s %s\n", TIMESTR, textbuffer);
- }
-}
-
-/** 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());
-}
-
-/** Write_NoFormat()
- * Writes a given string in `text' to the socket on fd `sock' - only if the socket
- * is a valid entry in the local FD table.
- */
-void Write_NoFormat(int sock, const char *text)
-{
- char tb[MAXBUF];
- int bytes;
-
- if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
- return;
-
- bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
- chop(tb);
-
- if (fd_ref_table[sock])
- {
- if (Config->GetIOHook(fd_ref_table[sock]->port))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
- }
- catch (ModuleException& modexcept)
- {
- log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
- }
- }
- else
- {
- fd_ref_table[sock]->AddWriteBuf(tb);
- }
- ServerInstance->stats->statsSent += bytes;
- }
- else
- log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
-}
-
-/** Write()
- * Same as Write_NoFormat(), but formatted printf() style first.
- */
-void Write(int sock, char *text, ...)
-{
- va_list argsPtr;
- char textbuffer[MAXBUF];
- char tb[MAXBUF];
- int bytes;
-
- if ((sock < 0) || (sock > MAX_DESCRIPTORS))
- return;
-
- if (!text)
- {
- log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
- return;
- }
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
- bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
- chop(tb);
-
- if (fd_ref_table[sock])
- {
- if (Config->GetIOHook(fd_ref_table[sock]->port))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
- }
- catch (ModuleException& modexcept)
- {
- log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
- }
- }
- else
- {
- fd_ref_table[sock]->AddWriteBuf(tb);
- }
- ServerInstance->stats->statsSent += bytes;
- }
- else
- log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
-}
-
-/** WriteServ_NoFormat()
- * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
- */
-void WriteServ_NoFormat(int sock, const char* text)
-{
- char tb[MAXBUF];
- int bytes;
-
- if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
- return;
-
- bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
- chop(tb);
-
- if (fd_ref_table[sock])
- {
- if (Config->GetIOHook(fd_ref_table[sock]->port))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
- }
- catch (ModuleException& modexcept)
- {
- log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
- }
- }
- else
- {
- fd_ref_table[sock]->AddWriteBuf(tb);
- }
- ServerInstance->stats->statsSent += bytes;
- }
- else
- log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
-}
-
-/** WriteServ()
- * Same as Write(), except `text' is prefixed with `:server.name '.
- */
-void WriteServ(int sock, char* text, ...)
-{
- va_list argsPtr;
- char textbuffer[MAXBUF];
-
- if ((sock < 0) || (sock > MAX_DESCRIPTORS))
- return;
-
- if (!text)
- {
- log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
- return;
- }
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
-
- WriteServ_NoFormat(sock, textbuffer);
-}
-
-/** WriteFrom_NoFormat()
- * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
- * the nick, user, and host of `user'.
- */
-void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
-{
- char tb[MAXBUF];
- int bytes;
-
- if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
- return;
-
- bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
- chop(tb);
-
- if (fd_ref_table[sock])
- {
- if (Config->GetIOHook(fd_ref_table[sock]->port))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
- }
- catch (ModuleException& modexcept)
- {
- log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
- }
- }
- else
- {
- fd_ref_table[sock]->AddWriteBuf(tb);
- }
- ServerInstance->stats->statsSent += bytes;
- }
- else
- log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
-}
-
-/* write text from an originating user to originating user */
-
-void WriteFrom(int sock, userrec *user,char* text, ...)
-{
- va_list argsPtr;
- char textbuffer[MAXBUF];
- char tb[MAXBUF];
- int bytes;
-
- if ((sock < 0) || (sock > MAX_DESCRIPTORS))
- return;
-
- if ((!text) || (!user))
- {
- log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
- return;
- }
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
- bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
- chop(tb);
-
- if (fd_ref_table[sock])
- {
- if (Config->GetIOHook(fd_ref_table[sock]->port))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
- }
- catch (ModuleException& modexcept)
- {
- log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
- }
- }
- else
- {
- fd_ref_table[sock]->AddWriteBuf(tb);
- }
-
- ServerInstance->stats->statsSent += bytes;
- }
- else
- log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
-}
-
-/* write text to an destination user from a source user (e.g. user privmsg) */
-
-void WriteTo(userrec *source, userrec *dest,char *data, ...)
-{
- char textbuffer[MAXBUF];
- va_list argsPtr;
-
- if ((!dest) || (!data))
- {
- log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
- return;
- }
-
- if (!IS_LOCAL(dest))
- return;
-
- va_start(argsPtr, data);
- vsnprintf(textbuffer, MAXBUF, data, argsPtr);
- va_end(argsPtr);
- chop(textbuffer);
-
- // if no source given send it from the server.
- if (!source)
- {
- WriteServ_NoFormat(dest->fd,textbuffer);
- }
- else
- {
- WriteFrom_NoFormat(dest->fd,source,textbuffer);
- }
-}
-
-void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
-{
- if ((!dest) || (!data))
- return;
-
- if (!source)
- {
- WriteServ_NoFormat(dest->fd,data);
- }
- else
- {
- WriteFrom_NoFormat(dest->fd,source,data);
- }
-}
-
-/* write formatted text from a source user to all users on a channel
- * including the sender (NOT for privmsg, notice etc!) */
-
-void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
-{
- char textbuffer[MAXBUF];
- va_list argsPtr;
- CUList *ulist;
-
- if ((!Ptr) || (!user) || (!text))
- {
- log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
- return;
- }
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
-
- ulist = Ptr->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if (i->second->fd != FD_MAGIC_NUMBER)
- WriteTo_NoFormat(user,i->second,textbuffer);
- }
-}
-
-void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
-{
- CUList *ulist;
-
- if ((!Ptr) || (!user) || (!text))
- {
- log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
- return;
- }
-
- ulist = Ptr->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if (i->second->fd != FD_MAGIC_NUMBER)
- WriteTo_NoFormat(user,i->second,text);
- }
-}
-
-
-/* write formatted text from a source user to all users on a channel
- * including the sender (NOT for privmsg, notice etc!) doesnt send to
- * users on remote servers */
-
-void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
-{
- char textbuffer[MAXBUF];
- va_list argsPtr;
- CUList *ulist;
-
- if ((!Ptr) || (!text))
- {
- log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
- return;
- }
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
-
- ulist = Ptr->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
- {
- if (!user)
- {
- WriteServ_NoFormat(i->second->fd,textbuffer);
- }
- else
- {
- WriteTo_NoFormat(user,i->second,textbuffer);
- }
- }
- }
-}
-
-void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
-{
- CUList *ulist;
-
- if ((!Ptr) || (!text))
- {
- log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
- return;
- }
-
- ulist = Ptr->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
- {
- if (!user)
- {
- WriteServ_NoFormat(i->second->fd,text);
- }
- else
- {
- WriteTo_NoFormat(user,i->second,text);
- }
- }
- }
-}
-
-
-
-void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
-{
- char textbuffer[MAXBUF];
- va_list argsPtr;
- CUList *ulist;
-
- if ((!Ptr) || (!text))
- {
- log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
- return;
- }
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
-
- ulist = Ptr->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if (IS_LOCAL(i->second))
- WriteServ_NoFormat(i->second->fd,textbuffer);
- }
-}
-
-void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
-{
- CUList *ulist;
-
- if ((!Ptr) || (!text))
- {
- log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
- return;
- }
-
- ulist = Ptr->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if (IS_LOCAL(i->second))
- WriteServ_NoFormat(i->second->fd,text);
- }
-}