- * 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;
-
- if (fd_ref_table[sock])
- {
- bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
-
- if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->GetPort())->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;
- }
-
- if (fd_ref_table[sock])
- {
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
- bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
-
- if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->GetPort())->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;
-
- if (fd_ref_table[sock])
- {
- bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
-
- if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->GetPort())->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;
- }
-
- if (!fd_ref_table[sock])
- 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;
-
- if (fd_ref_table[sock])
- {
- bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
-
- if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->GetPort())->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;
- }
-
- if (fd_ref_table[sock])
- {
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
- bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
-
- if (Config->GetIOHook(fd_ref_table[sock]->GetPort()))
- {
- try
- {
- Config->GetIOHook(fd_ref_table[sock]->GetPort())->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);
-
- // 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);
- }
-}
-
-std::string GetServerDescription(const char* servername)
-{
- std::string description = "";
-
- FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
-
- if (description != "")
- {
- return description;
- }
- else
- {
- // not a remote server that can be found, it must be me.
- return Config->ServerDesc;
- }
-}
-
-/* write a formatted string to all users who share at least one common
- * channel, including the source user e.g. for use in NICK */
-
-void WriteCommon(userrec *u, char* text, ...)
-{
- char textbuffer[MAXBUF];
- va_list argsPtr;
- bool sent_to_at_least_one = false;
-
- if (!u)
- {
- log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
- return;
- }
-
- if (u->registered != REG_ALL)
- {
- log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
- return;
- }
-
- va_start(argsPtr, text);
- vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
-
- // XXX: Save on memset calls by using an ID. This clever trick thought of during discussion with nazzy and w00t.
- uniq_id++;
-
- for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
- {
- if (((ucrec*)(*v))->channel)
- {
- CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
- {
- already_sent[i->second->fd] = uniq_id;
- WriteFrom_NoFormat(i->second->fd,u,textbuffer);
- sent_to_at_least_one = true;
- }
- }
- }
- }
-
- /*
- * if the user was not in any channels, no users will receive the text. Make sure the user
- * receives their OWN message for WriteCommon
- */
- if (!sent_to_at_least_one)
- {
- WriteFrom_NoFormat(u->fd,u,textbuffer);
- }
-}
-
-void WriteCommon_NoFormat(userrec *u, const char* text)
-{
- bool sent_to_at_least_one = false;
-
- if (!u)
- {
- log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
- return;
- }
-
- if (u->registered != REG_ALL)
- {
- log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
- return;
- }
-
- // XXX: See comment in WriteCommon
- uniq_id++;
-
- for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
- {
- if (((ucrec*)(*v))->channel)
- {
- CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
- {
- already_sent[i->second->fd] = uniq_id;
- WriteFrom_NoFormat(i->second->fd,u,text);
- sent_to_at_least_one = true;
- }
- }
- }
- }
-
- /*
- * if the user was not in any channels, no users will receive the text. Make sure the user
- * receives their OWN message for WriteCommon
- */
- if (!sent_to_at_least_one)
- {
- WriteFrom_NoFormat(u->fd,u,text);
- }
-}
-
-
-/* write a formatted string to all users who share at least one common
- * channel, NOT including the source user e.g. for use in QUIT
- */
-
-void WriteCommonExcept(userrec *u, char* text, ...)
-{
- char textbuffer[MAXBUF];
- char oper_quit[MAXBUF];
- bool quit_munge = false;
- va_list argsPtr;
- int total;
-
- if (!u)
- {
- log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
- return;
- }
-
- if (u->registered != REG_ALL)
- {
- log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
- return;
- }
-
- va_start(argsPtr, text);
- total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
- va_end(argsPtr);
-
- if ((Config->HideSplits) && (total > 6))
- {
- /* Yeah yeah, this is ugly. But its fast, live with it. */
- char* check = textbuffer;
-
- if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
- {
- std::stringstream split(check);
- std::string server_one;
- std::string server_two;
-
- split >> server_one;
- split >> server_two;
-
- if ((FindServerName(server_one)) && (FindServerName(server_two)))
- {
- strlcpy(oper_quit,textbuffer,MAXQUIT);
- strlcpy(check,"*.net *.split",MAXQUIT);
- quit_munge = true;
- }
- }
- }
-
- if ((Config->HideBans) && (total > 13) && (!quit_munge))
- {
- char* check = textbuffer;
-
- /* XXX - as above */
- if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
- {
- check++;
-
- if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
- {
- strlcpy(oper_quit,textbuffer,MAXQUIT);
- *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
- quit_munge = true;
- }
- }
- }
-
- uniq_id++;
-
- for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
- {
- if (((ucrec*)(*v))->channel)
- {
- CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if (u != i->second)
- {
- if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
- {
- already_sent[i->second->fd] = uniq_id;
-
- if (quit_munge)
- {
- WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
- }
- else
- WriteFrom_NoFormat(i->second->fd,u,textbuffer);
- }
- }
- }
- }
- }
-}
-
-void WriteCommonExcept_NoFormat(userrec *u, const char* text)
-{
- if (!u)
- {
- log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
- return;
- }
-
- if (u->registered != REG_ALL)
- {
- log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
- return;
- }
-
- uniq_id++;
-
- for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
- {
- if (((ucrec*)(*v))->channel)
- {
- CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
-
- for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
- {
- if (u != i->second)
- {
- if ((IS_LOCAL(i->second)) && (already_sent[i->second->fd] != uniq_id))
- {
- already_sent[i->second->fd] = uniq_id;
- WriteFrom_NoFormat(i->second->fd,u,text);
- }
- }
- }
- }
- }
-}
-
-
-/* XXX - We don't use WriteMode for this because WriteMode is very slow and
- * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
- * the number of modes provided, e.g. if you send WriteMode 'og' to write to
- * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
- * uses the oper list, which means if you have 2000 users but only 5 opers,
- * it iterates 5 times.