1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
18 #include "inspircd_config.h"
20 #include "configreader.h"
23 #include <sys/errno.h>
31 #include "connection.h"
42 #include "inspstring.h"
43 #include "helperfuncs.h"
48 extern ModuleList modules;
49 extern ServerConfig *Config;
50 extern InspIRCd* ServerInstance;
52 extern char lowermap[255];
53 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
54 static unsigned long already_sent[MAX_DESCRIPTORS] = {0};
55 extern std::vector<userrec*> all_opers;
56 extern user_hash clientlist;
57 extern chan_hash chanlist;
59 char LOG_FILE[MAXBUF];
61 extern std::vector<userrec*> local_users;
63 static char TIMESTR[26];
64 static time_t LAST = 0;
66 /* XXX: Used for speeding up WriteCommon operations */
67 unsigned long uniq_id = 0;
70 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
71 * is greater than the configured loglevel.
73 void do_log(int level, const char *text, ...)
76 char textbuffer[MAXBUF];
78 /* If we were given -debug we output all messages, regardless of configured loglevel */
79 if ((level < Config->LogLevel) && !Config->forcedebug)
84 struct tm *timeinfo = localtime(&TIME);
86 strlcpy(TIMESTR,asctime(timeinfo),26);
93 va_start(argsPtr, text);
94 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
99 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
100 fflush(Config->log_file);
106 printf("%s %s\n", TIMESTR, textbuffer);
111 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
113 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
115 void readfile(file_cache &F, const char* fname)
118 char linebuf[MAXBUF];
120 log(DEBUG,"readfile: loading %s",fname);
122 file = fopen(fname,"r");
128 fgets(linebuf,sizeof(linebuf),file);
129 linebuf[strlen(linebuf)-1]='\0';
138 F.push_back(linebuf);
146 log(DEBUG,"readfile: failed to load file: %s",fname);
149 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
153 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
154 * is a valid entry in the local FD table.
156 void Write_NoFormat(int sock, const char *text)
161 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
164 if (fd_ref_table[sock])
166 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
168 if (Config->GetIOHook(fd_ref_table[sock]->port))
172 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
174 catch (ModuleException& modexcept)
176 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
181 fd_ref_table[sock]->AddWriteBuf(tb);
183 ServerInstance->stats->statsSent += bytes;
186 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
190 * Same as Write_NoFormat(), but formatted printf() style first.
192 void Write(int sock, char *text, ...)
195 char textbuffer[MAXBUF];
199 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
204 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
208 if (fd_ref_table[sock])
211 va_start(argsPtr, text);
212 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
214 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
216 if (Config->GetIOHook(fd_ref_table[sock]->port))
220 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
222 catch (ModuleException& modexcept)
224 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
229 fd_ref_table[sock]->AddWriteBuf(tb);
231 ServerInstance->stats->statsSent += bytes;
234 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
237 /** WriteServ_NoFormat()
238 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
240 void WriteServ_NoFormat(int sock, const char* text)
245 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
248 if (fd_ref_table[sock])
250 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
252 if (Config->GetIOHook(fd_ref_table[sock]->port))
256 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
258 catch (ModuleException& modexcept)
260 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
265 fd_ref_table[sock]->AddWriteBuf(tb);
267 ServerInstance->stats->statsSent += bytes;
270 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
274 * Same as Write(), except `text' is prefixed with `:server.name '.
276 void WriteServ(int sock, char* text, ...)
279 char textbuffer[MAXBUF];
281 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
286 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
290 if (!fd_ref_table[sock])
293 va_start(argsPtr, text);
294 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
297 WriteServ_NoFormat(sock, textbuffer);
300 /** WriteFrom_NoFormat()
301 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
302 * the nick, user, and host of `user'.
304 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
309 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
312 if (fd_ref_table[sock])
314 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
316 if (Config->GetIOHook(fd_ref_table[sock]->port))
320 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
322 catch (ModuleException& modexcept)
324 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
329 fd_ref_table[sock]->AddWriteBuf(tb);
331 ServerInstance->stats->statsSent += bytes;
334 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
337 /* write text from an originating user to originating user */
339 void WriteFrom(int sock, userrec *user,char* text, ...)
342 char textbuffer[MAXBUF];
346 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
349 if ((!text) || (!user))
351 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
355 if (fd_ref_table[sock])
358 va_start(argsPtr, text);
359 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
361 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
363 if (Config->GetIOHook(fd_ref_table[sock]->port))
367 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
369 catch (ModuleException& modexcept)
371 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
376 fd_ref_table[sock]->AddWriteBuf(tb);
379 ServerInstance->stats->statsSent += bytes;
382 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
385 /* write text to an destination user from a source user (e.g. user privmsg) */
387 void WriteTo(userrec *source, userrec *dest,char *data, ...)
389 char textbuffer[MAXBUF];
392 if ((!dest) || (!data))
394 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
401 va_start(argsPtr, data);
402 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
405 // if no source given send it from the server.
408 WriteServ_NoFormat(dest->fd,textbuffer);
412 WriteFrom_NoFormat(dest->fd,source,textbuffer);
416 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
418 if ((!dest) || (!data))
423 WriteServ_NoFormat(dest->fd,data);
427 WriteFrom_NoFormat(dest->fd,source,data);
431 /* write formatted text from a source user to all users on a channel
432 * including the sender (NOT for privmsg, notice etc!) */
434 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
436 char textbuffer[MAXBUF];
440 if ((!Ptr) || (!user) || (!text))
442 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
446 va_start(argsPtr, text);
447 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
450 ulist = Ptr->GetUsers();
452 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
454 if (i->second->fd != FD_MAGIC_NUMBER)
455 WriteTo_NoFormat(user,i->second,textbuffer);
459 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
463 if ((!Ptr) || (!user) || (!text))
465 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
469 ulist = Ptr->GetUsers();
471 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
473 if (i->second->fd != FD_MAGIC_NUMBER)
474 WriteTo_NoFormat(user,i->second,text);
479 /* write formatted text from a source user to all users on a channel
480 * including the sender (NOT for privmsg, notice etc!) doesnt send to
481 * users on remote servers */
483 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
485 char textbuffer[MAXBUF];
489 if ((!Ptr) || (!text))
491 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
495 va_start(argsPtr, text);
496 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
499 ulist = Ptr->GetUsers();
501 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
503 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
507 WriteServ_NoFormat(i->second->fd,textbuffer);
511 WriteTo_NoFormat(user,i->second,textbuffer);
517 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
521 if ((!Ptr) || (!text))
523 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
527 ulist = Ptr->GetUsers();
529 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
531 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
535 WriteServ_NoFormat(i->second->fd,text);
539 WriteTo_NoFormat(user,i->second,text);
547 void WriteChannelWithServ(const char* ServName, chanrec* Ptr, const char* text, ...)
549 char textbuffer[MAXBUF];
553 if ((!Ptr) || (!text))
555 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
559 va_start(argsPtr, text);
560 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
563 ulist = Ptr->GetUsers();
565 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
567 if (IS_LOCAL(i->second))
568 WriteServ_NoFormat(i->second->fd,textbuffer);
572 void WriteChannelWithServ_NoFormat(const char* ServName, chanrec* Ptr, const char* text)
576 if ((!Ptr) || (!text))
578 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
582 ulist = Ptr->GetUsers();
584 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
586 if (IS_LOCAL(i->second))
587 WriteServ_NoFormat(i->second->fd,text);
593 /* write formatted text from a source user to all users on a channel except
594 * for the sender (for privmsg etc) */
596 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
598 char textbuffer[MAXBUF];
602 if ((!Ptr) || (!user) || (!text))
604 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
608 va_start(argsPtr, text);
609 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
615 ulist = Ptr->GetOppedUsers();
618 ulist = Ptr->GetHalfoppedUsers();
621 ulist = Ptr->GetVoicedUsers();
624 ulist = Ptr->GetUsers();
628 log(DEBUG,"%d users to write to",ulist->size());
630 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
632 if ((IS_LOCAL(i->second)) && (user != i->second))
633 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
637 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
641 if ((!Ptr) || (!user) || (!text))
643 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
650 ulist = Ptr->GetOppedUsers();
653 ulist = Ptr->GetHalfoppedUsers();
656 ulist = Ptr->GetVoicedUsers();
659 ulist = Ptr->GetUsers();
663 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
665 if ((IS_LOCAL(i->second)) && (user != i->second))
666 WriteFrom_NoFormat(i->second->fd,user,text);
670 std::string GetServerDescription(const char* servername)
672 std::string description = "";
674 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
676 if (description != "")
682 // not a remote server that can be found, it must be me.
683 return Config->ServerDesc;
687 /* write a formatted string to all users who share at least one common
688 * channel, including the source user e.g. for use in NICK */
690 void WriteCommon(userrec *u, char* text, ...)
692 char textbuffer[MAXBUF];
694 bool sent_to_at_least_one = false;
698 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
702 if (u->registered != 7)
704 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
708 va_start(argsPtr, text);
709 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
712 // XXX: Save on memset calls by using an ID. This clever trick thought of during discussion with nazzy and w00t.
715 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
717 if (((ucrec*)(*v))->channel)
719 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
721 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
723 if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id))
725 already_sent[i->second->fd] = uniq_id;
726 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
727 sent_to_at_least_one = true;
734 * if the user was not in any channels, no users will receive the text. Make sure the user
735 * receives their OWN message for WriteCommon
737 if (!sent_to_at_least_one)
739 WriteFrom_NoFormat(u->fd,u,textbuffer);
743 void WriteCommon_NoFormat(userrec *u, const char* text)
745 bool sent_to_at_least_one = false;
749 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
753 if (u->registered != 7)
755 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
759 // XXX: See comment in WriteCommon
762 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
764 if (((ucrec*)(*v))->channel)
766 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
768 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
770 if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id))
772 already_sent[i->second->fd] = uniq_id;
773 WriteFrom_NoFormat(i->second->fd,u,text);
774 sent_to_at_least_one = true;
781 * if the user was not in any channels, no users will receive the text. Make sure the user
782 * receives their OWN message for WriteCommon
784 if (!sent_to_at_least_one)
786 WriteFrom_NoFormat(u->fd,u,text);
791 /* write a formatted string to all users who share at least one common
792 * channel, NOT including the source user e.g. for use in QUIT
795 void WriteCommonExcept(userrec *u, char* text, ...)
797 char textbuffer[MAXBUF];
798 char oper_quit[MAXBUF];
799 bool quit_munge = false;
805 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
809 if (u->registered != 7)
811 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
815 va_start(argsPtr, text);
816 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
819 if ((Config->HideSplits) && (total > 6))
821 /* Yeah yeah, this is ugly. But its fast, live with it. */
822 char* check = textbuffer;
824 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
826 std::stringstream split(check);
827 std::string server_one;
828 std::string server_two;
833 if ((FindServerName(server_one)) && (FindServerName(server_two)))
835 strlcpy(oper_quit,textbuffer,MAXQUIT);
836 strlcpy(check,"*.net *.split",MAXQUIT);
842 if ((Config->HideBans) && (total > 13) && (!quit_munge))
844 char* check = textbuffer;
847 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
851 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
853 strlcpy(oper_quit,textbuffer,MAXQUIT);
854 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
862 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
864 if (((ucrec*)(*v))->channel)
866 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
868 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
872 if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id))
874 already_sent[i->second->fd] = uniq_id;
878 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
881 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
889 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
893 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
897 if (u->registered != 7)
899 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
905 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
907 if (((ucrec*)(*v))->channel)
909 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
911 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
915 if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id))
917 already_sent[i->second->fd] = uniq_id;
918 WriteFrom_NoFormat(i->second->fd,u,text);
927 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
928 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
929 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
930 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
931 * uses the oper list, which means if you have 2000 users but only 5 opers,
932 * it iterates 5 times.
934 void WriteOpers(const char* text, ...)
936 char textbuffer[MAXBUF];
941 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
945 va_start(argsPtr, text);
946 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
949 WriteOpers_NoFormat(textbuffer);
952 void WriteOpers_NoFormat(const char* text)
956 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
960 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
966 if (a->modes[UM_SERVERNOTICE])
968 // send server notices to all with +s
969 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
975 void ServerNoticeAll(char* text, ...)
980 char textbuffer[MAXBUF];
981 char formatbuffer[MAXBUF];
983 va_start (argsPtr, text);
984 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
987 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
989 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
991 userrec* t = (userrec*)(*i);
992 WriteServ_NoFormat(t->fd,formatbuffer);
996 void ServerPrivmsgAll(char* text, ...)
1001 char textbuffer[MAXBUF];
1002 char formatbuffer[MAXBUF];
1004 va_start (argsPtr, text);
1005 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1008 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1010 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1012 userrec* t = (userrec*)(*i);
1013 WriteServ_NoFormat(t->fd,formatbuffer);
1017 void WriteMode(const char* modes, int flags, const char* text, ...)
1019 char textbuffer[MAXBUF];
1023 if ((!text) || (!modes) || (!flags))
1025 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1029 va_start(argsPtr, text);
1030 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1032 modelen = strlen(modes);
1034 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1036 userrec* t = (userrec*)(*i);
1037 bool send_to_user = false;
1039 if (flags == WM_AND)
1041 send_to_user = true;
1043 for (int n = 0; n < modelen; n++)
1045 if (!t->modes[modes[n]-65])
1047 send_to_user = false;
1052 else if (flags == WM_OR)
1054 send_to_user = false;
1056 for (int n = 0; n < modelen; n++)
1058 if (t->modes[modes[n]-65])
1060 send_to_user = true;
1068 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1073 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1075 char textbuffer[MAXBUF];
1076 char formatbuffer[MAXBUF];
1079 if ((!text) || (!source))
1081 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1085 va_start(argsPtr, text);
1086 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1089 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1091 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1093 userrec* t = (userrec*)(*i);
1094 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1099 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1101 char textbuffer[MAXBUF];
1102 char formatbuffer[MAXBUF];
1105 if ((!text) || (!source))
1107 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1111 va_start(argsPtr, text);
1112 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1115 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1117 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1119 userrec* t = (userrec*)(*i);
1121 if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS]))
1123 WriteTo_NoFormat(source,t,formatbuffer);
1128 /* convert a string to lowercase. Note following special circumstances
1129 * taken from RFC 1459. Many "official" server branches still hold to this
1130 * rule so i will too;
1132 * Because of IRC's scandanavian origin, the characters {}| are
1133 * considered to be the lower case equivalents of the characters []\,
1134 * respectively. This is a critical issue when determining the
1135 * equivalence of two nicknames.
1137 void strlower(char *n)
1141 for (char* t = n; *t; t++)
1142 *t = lowermap[(unsigned char)*t];
1146 /* Find a user record by nickname and return a pointer to it */
1148 userrec* Find(const std::string &nick)
1150 user_hash::iterator iter = clientlist.find(nick);
1152 if (iter == clientlist.end())
1153 /* Couldn't find it */
1156 return iter->second;
1159 userrec* Find(const char* nick)
1161 user_hash::iterator iter;
1166 iter = clientlist.find(nick);
1168 if (iter == clientlist.end())
1171 return iter->second;
1174 /* find a channel record by channel name and return a pointer to it */
1176 chanrec* FindChan(const char* chan)
1178 chan_hash::iterator iter;
1182 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1186 iter = chanlist.find(chan);
1188 if (iter == chanlist.end())
1189 /* Couldn't find it */
1192 return iter->second;
1196 long GetMaxBans(char* name)
1199 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1202 if (match(name,x.c_str()))
1210 void purge_empty_chans(userrec* u)
1212 std::vector<chanrec*> to_delete;
1214 // firstly decrement the count on each channel
1215 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1217 ucrec* uc = (ucrec*)(*f);
1220 if (uc->channel->DelUser(u) == 0)
1222 /* No users left in here, mark it for deletion */
1223 to_delete.push_back(uc->channel);
1229 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1231 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1233 chanrec* thischan = (chanrec*)*n;
1234 chan_hash::iterator i2 = chanlist.find(thischan->name);
1235 if (i2 != chanlist.end())
1237 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1248 char* chanmodes(chanrec *chan, bool showkey)
1250 static char scratch[MAXBUF];
1251 static char sparam[MAXBUF];
1252 char* offset = scratch;
1253 std::string extparam = "";
1257 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1265 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1266 for(int n = 0; n < 64; n++)
1275 extparam = (showkey ? chan->key : "<key>");
1278 extparam = ConvToStr(chan->limit);
1286 /* We know these have no parameters */
1289 extparam = chan->GetModeParameter(n+65);
1294 charlcat(sparam,' ',MAXBUF);
1295 strlcat(sparam,extparam.c_str(),MAXBUF);
1300 /* Null terminate scratch */
1302 strlcat(scratch,sparam,MAXBUF);
1307 /* compile a userlist of a channel into a string, each nick seperated by
1308 * spaces and op, voice etc status shown as @ and + */
1310 void userlist(userrec *user,chanrec *c)
1312 if ((!c) || (!user))
1314 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1319 size_t dlen, curlen;
1321 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1324 char* ptr = list + dlen;
1326 CUList *ulist= c->GetUsers();
1328 /* Improvement by Brain - this doesnt change in value, so why was it inside
1331 bool has_user = c->HasUser(user);
1333 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1335 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
1338 * user is +i, and source not on the channel, does not show
1339 * nick in NAMES list
1344 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1351 if (curlen > (480-NICKMAX))
1353 /* list overflowed into multiple numerics */
1354 WriteServ_NoFormat(user->fd,list);
1356 /* reset our lengths */
1357 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1365 /* if whats left in the list isnt empty, send it */
1368 WriteServ_NoFormat(user->fd,list);
1373 * return a count of the users on a specific channel accounting for
1374 * invisible users who won't increase the count. e.g. for /LIST
1376 int usercount_i(chanrec *c)
1383 CUList *ulist= c->GetUsers();
1384 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1386 if (!(i->second->modes[UM_INVISIBLE]))
1393 int usercount(chanrec *c)
1395 return (c ? c->GetUserCounter() : 0);
1399 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1400 * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
1401 * then their ip will be taken as 'priority' anyway, so for example,
1402 * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
1404 ConnectClass GetClass(userrec *user)
1406 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1408 if ((match(inet_ntoa(user->ip4),i->host.c_str())) || (match(user->host,i->host.c_str())))
1414 return *(Config->Classes.begin());
1418 * sends out an error notice to all connected clients (not to be used
1421 void send_error(char *s)
1423 log(DEBUG,"send_error: %s",s);
1425 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1427 userrec* t = (userrec*)(*i);
1428 if (t->registered == 7)
1430 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1434 // fix - unregistered connections receive ERROR, not NOTICE
1435 Write(t->fd,"ERROR :%s",s);
1440 void Error(int status)
1446 signal(SIGALRM, SIG_IGN);
1447 signal(SIGPIPE, SIG_IGN);
1448 signal(SIGTERM, SIG_IGN);
1449 signal(SIGABRT, SIG_IGN);
1450 signal(SIGSEGV, SIG_IGN);
1451 signal(SIGURG, SIG_IGN);
1452 signal(SIGKILL, SIG_IGN);
1453 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1455 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
1456 size = backtrace(array, 30);
1457 strings = backtrace_symbols(array, size);
1458 for (size_t i = 0; i < size; i++) {
1459 log(DEFAULT,"[%d] %s", i, strings[i]);
1462 WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/");
1464 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
1466 send_error("Somebody screwed up... Whoops. IRC Server terminating.");
1467 signal(SIGSEGV, SIG_DFL);
1468 if (raise(SIGSEGV) == -1)
1470 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
1475 // this function counts all users connected, wether they are registered or NOT.
1478 return clientlist.size();
1481 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1482 int registered_usercount(void)
1486 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1488 if (i->second->registered == 7) c++;
1494 int usercount_invisible(void)
1498 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1500 if ((i->second->registered == 7) && (i->second->modes[UM_INVISIBLE]))
1507 int usercount_opers(void)
1511 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1513 if (*(i->second->oper))
1519 int usercount_unknown(void)
1523 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1525 userrec* t = (userrec*)(*i);
1526 if (t->registered != 7)
1533 long chancount(void)
1535 return chanlist.size();
1542 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1544 userrec* t = (userrec*)(*i);
1545 if (t->registered == 7)
1552 void ShowMOTD(userrec *user)
1554 if (!Config->MOTD.size())
1556 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1559 WriteServ(user->fd,"375 %s :%s message of the day", user->nick, Config->ServerName);
1561 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1562 WriteServ(user->fd,"372 %s :- %s",user->nick,Config->MOTD[i].c_str());
1564 WriteServ(user->fd,"376 %s :End of message of the day.", user->nick);
1567 void ShowRULES(userrec *user)
1569 if (!Config->RULES.size())
1571 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1574 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1576 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1577 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1579 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1582 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1583 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1584 // registration timeout maximum seconds)
1585 bool AllModulesReportReady(userrec* user)
1587 if (!Config->global_implementation[I_OnCheckReady])
1590 for (int i = 0; i <= MODCOUNT; i++)
1592 if (Config->implement_lists[i][I_OnCheckReady])
1594 int res = modules[i]->OnCheckReady(user);
1603 /* Make Sure Modules Are Avaliable!
1604 * (BugFix By Craig.. See? I do work! :p)
1605 * Modified by brain, requires const char*
1606 * to work with other API functions
1610 bool FileExists (const char* file)
1613 if ((input = fopen (file, "r")) == NULL)
1624 char* CleanFilename(char* name)
1626 char* p = name + strlen(name);
1627 while ((p != name) && (*p != '/')) p--;
1628 return (p != name ? ++p : p);
1631 bool DirValid(char* dirandfile)
1634 char buffer[MAXBUF];
1635 char otherdir[MAXBUF];
1638 strlcpy(work, dirandfile, MAXBUF);
1641 // we just want the dir
1653 // Get the current working directory
1654 if (getcwd(buffer, MAXBUF ) == NULL )
1659 if (getcwd(otherdir, MAXBUF ) == NULL )
1664 size_t t = strlen(work);
1666 if (strlen(otherdir) >= t)
1670 if (!strcmp(otherdir,work))
1683 std::string GetFullProgDir(char** argv, int argc)
1686 char buffer[MAXBUF];
1687 char otherdir[MAXBUF];
1690 strlcpy(work,argv[0],MAXBUF);
1693 // we just want the dir
1705 // Get the current working directory
1706 if (getcwd(buffer, MAXBUF) == NULL)
1711 if (getcwd(otherdir, MAXBUF) == NULL)
1718 int InsertMode(std::string &output, const char* mode, unsigned short section)
1720 unsigned short currsection = 1;
1721 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1723 if(section > 4 || section == 0)
1725 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1729 for(; pos < output.size(); pos++)
1731 if(section == currsection)
1734 if(output[pos] == ',')
1738 output.insert(pos, mode);
1742 bool IsValidChannelName(const char *chname)
1746 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1747 if (!chname || *chname != '#')
1752 c = (char *)chname + 1;
1766 /* too long a name - note funky pointer arithmetic here. */
1767 if ((c - chname) > CHANMAX)
1775 inline int charlcat(char* x,char y,int z)
1792 bool charremove(char* mp, char remove)
1795 bool shift_down = false;
1799 if (*mptr == remove)
1811 void OpenLog(char** argv, int argc)
1815 if (Config->logpath == "")
1817 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1822 Config->log_file = fopen(LOG_FILE,"a+");
1824 if (!Config->log_file)
1826 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1833 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1835 if (!Config->log_file)
1837 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1846 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1847 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1854 if (*Config->DieValue)
1856 printf("WARNING: %s\n\n",Config->DieValue);
1857 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1862 /* We must load the modules AFTER initializing the socket engine, now */
1863 void LoadAllModules(InspIRCd* ServerInstance)
1865 char configToken[MAXBUF];
1866 Config->module_names.clear();
1869 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1871 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1872 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1874 if (!ServerInstance->LoadModule(configToken))
1876 log(DEFAULT,"Exiting due to a module loader error.");
1877 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1882 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);