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 char already_sent[MAX_DESCRIPTORS];
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 */
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 only requiring memset every 4 billion or so
713 // messages. This clever trick thought of during discussion with nazzy and w00t.
717 memset(&already_sent,0,MAX_DESCRIPTORS);
721 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
723 if (((ucrec*)(*v))->channel)
725 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
727 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
729 if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id))
731 already_sent[i->second->fd] = uniq_id;
732 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
733 sent_to_at_least_one = true;
740 * if the user was not in any channels, no users will receive the text. Make sure the user
741 * receives their OWN message for WriteCommon
743 if (!sent_to_at_least_one)
745 WriteFrom_NoFormat(u->fd,u,textbuffer);
749 void WriteCommon_NoFormat(userrec *u, const char* text)
751 bool sent_to_at_least_one = false;
755 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
759 if (u->registered != 7)
761 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
765 // XXX: See comment in WriteCommon
769 memset(&already_sent,0,MAX_DESCRIPTORS);
773 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
775 if (((ucrec*)(*v))->channel)
777 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
779 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
781 if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id))
783 already_sent[i->second->fd] = uniq_id;
784 WriteFrom_NoFormat(i->second->fd,u,text);
785 sent_to_at_least_one = true;
792 * if the user was not in any channels, no users will receive the text. Make sure the user
793 * receives their OWN message for WriteCommon
795 if (!sent_to_at_least_one)
797 WriteFrom_NoFormat(u->fd,u,text);
802 /* write a formatted string to all users who share at least one common
803 * channel, NOT including the source user e.g. for use in QUIT
806 void WriteCommonExcept(userrec *u, char* text, ...)
808 char textbuffer[MAXBUF];
809 char oper_quit[MAXBUF];
810 bool quit_munge = false;
816 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
820 if (u->registered != 7)
822 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
826 va_start(argsPtr, text);
827 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
830 if ((Config->HideSplits) && (total > 6))
832 /* Yeah yeah, this is ugly. But its fast, live with it. */
833 char* check = textbuffer;
835 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
837 std::stringstream split(check);
838 std::string server_one;
839 std::string server_two;
844 if ((FindServerName(server_one)) && (FindServerName(server_two)))
846 strlcpy(oper_quit,textbuffer,MAXQUIT);
847 strlcpy(check,"*.net *.split",MAXQUIT);
853 if ((Config->HideBans) && (total > 13) && (!quit_munge))
855 char* check = textbuffer;
858 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
862 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
864 strlcpy(oper_quit,textbuffer,MAXQUIT);
865 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
874 memset(&already_sent,0,MAX_DESCRIPTORS);
878 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
880 if (((ucrec*)(*v))->channel)
882 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
884 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
888 if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id))
890 already_sent[i->second->fd] = uniq_id;
894 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
897 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
905 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
909 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
913 if (u->registered != 7)
915 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
922 memset(&already_sent,0,MAX_DESCRIPTORS);
926 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
928 if (((ucrec*)(*v))->channel)
930 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
932 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
936 if ((i->second->fd > -1) && (already_sent[i->second->fd] != uniq_id))
938 already_sent[i->second->fd] = uniq_id;
939 WriteFrom_NoFormat(i->second->fd,u,text);
948 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
949 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
950 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
951 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
952 * uses the oper list, which means if you have 2000 users but only 5 opers,
953 * it iterates 5 times.
955 void WriteOpers(const char* text, ...)
957 char textbuffer[MAXBUF];
962 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
966 va_start(argsPtr, text);
967 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
970 WriteOpers_NoFormat(textbuffer);
973 void WriteOpers_NoFormat(const char* text)
977 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
981 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
987 if (a->modes[UM_SERVERNOTICE])
989 // send server notices to all with +s
990 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
996 void ServerNoticeAll(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,"NOTICE $%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 ServerPrivmsgAll(char* text, ...)
1022 char textbuffer[MAXBUF];
1023 char formatbuffer[MAXBUF];
1025 va_start (argsPtr, text);
1026 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1029 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1031 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1033 userrec* t = (userrec*)(*i);
1034 WriteServ_NoFormat(t->fd,formatbuffer);
1038 void WriteMode(const char* modes, int flags, const char* text, ...)
1040 char textbuffer[MAXBUF];
1044 if ((!text) || (!modes) || (!flags))
1046 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1050 va_start(argsPtr, text);
1051 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1053 modelen = strlen(modes);
1055 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1057 userrec* t = (userrec*)(*i);
1058 bool send_to_user = false;
1060 if (flags == WM_AND)
1062 send_to_user = true;
1064 for (int n = 0; n < modelen; n++)
1066 if (!t->modes[modes[n]-65])
1068 send_to_user = false;
1073 else if (flags == WM_OR)
1075 send_to_user = false;
1077 for (int n = 0; n < modelen; n++)
1079 if (t->modes[modes[n]-65])
1081 send_to_user = true;
1089 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1094 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1096 char textbuffer[MAXBUF];
1097 char formatbuffer[MAXBUF];
1100 if ((!text) || (!source))
1102 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1106 va_start(argsPtr, text);
1107 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1110 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1112 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1114 userrec* t = (userrec*)(*i);
1115 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1120 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1122 char textbuffer[MAXBUF];
1123 char formatbuffer[MAXBUF];
1126 if ((!text) || (!source))
1128 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1132 va_start(argsPtr, text);
1133 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1136 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1138 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1140 userrec* t = (userrec*)(*i);
1142 if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS]))
1144 WriteTo_NoFormat(source,t,formatbuffer);
1149 /* convert a string to lowercase. Note following special circumstances
1150 * taken from RFC 1459. Many "official" server branches still hold to this
1151 * rule so i will too;
1153 * Because of IRC's scandanavian origin, the characters {}| are
1154 * considered to be the lower case equivalents of the characters []\,
1155 * respectively. This is a critical issue when determining the
1156 * equivalence of two nicknames.
1158 void strlower(char *n)
1162 for (char* t = n; *t; t++)
1163 *t = lowermap[(unsigned char)*t];
1167 /* Find a user record by nickname and return a pointer to it */
1169 userrec* Find(const std::string &nick)
1171 user_hash::iterator iter = clientlist.find(nick);
1173 if (iter == clientlist.end())
1174 /* Couldn't find it */
1177 return iter->second;
1180 userrec* Find(const char* nick)
1182 user_hash::iterator iter;
1187 iter = clientlist.find(nick);
1189 if (iter == clientlist.end())
1192 return iter->second;
1195 /* find a channel record by channel name and return a pointer to it */
1197 chanrec* FindChan(const char* chan)
1199 chan_hash::iterator iter;
1203 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1207 iter = chanlist.find(chan);
1209 if (iter == chanlist.end())
1210 /* Couldn't find it */
1213 return iter->second;
1217 long GetMaxBans(char* name)
1220 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1223 if (match(name,x.c_str()))
1231 void purge_empty_chans(userrec* u)
1233 std::vector<chanrec*> to_delete;
1235 // firstly decrement the count on each channel
1236 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1238 if (((ucrec*)(*f))->channel)
1240 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1242 /* No users left in here, mark it for deletion */
1243 to_delete.push_back(((ucrec*)(*f))->channel);
1244 ((ucrec*)(*f))->channel = NULL;
1249 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1251 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1253 chanrec* thischan = (chanrec*)*n;
1254 chan_hash::iterator i2 = chanlist.find(thischan->name);
1255 if (i2 != chanlist.end())
1257 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1268 char* chanmodes(chanrec *chan, bool showkey)
1270 static char scratch[MAXBUF];
1271 static char sparam[MAXBUF];
1272 char* offset = scratch;
1273 std::string extparam = "";
1277 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1285 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1286 for(int n = 0; n < 64; n++)
1295 extparam = (showkey ? chan->key : "<key>");
1298 extparam = ConvToStr(chan->limit);
1306 /* We know these have no parameters */
1309 extparam = chan->GetModeParameter(n+65);
1314 charlcat(sparam,' ',MAXBUF);
1315 strlcat(sparam,extparam.c_str(),MAXBUF);
1320 /* Null terminate scratch */
1322 strlcat(scratch,sparam,MAXBUF);
1327 /* compile a userlist of a channel into a string, each nick seperated by
1328 * spaces and op, voice etc status shown as @ and + */
1330 void userlist(userrec *user,chanrec *c)
1332 if ((!c) || (!user))
1334 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1339 size_t dlen, curlen;
1341 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1344 char* ptr = list + dlen;
1346 CUList *ulist= c->GetUsers();
1348 /* Improvement by Brain - this doesnt change in value, so why was it inside
1351 bool has_user = c->HasUser(user);
1353 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1355 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
1358 * user is +i, and source not on the channel, does not show
1359 * nick in NAMES list
1364 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1371 if (curlen > (480-NICKMAX))
1373 /* list overflowed into multiple numerics */
1374 WriteServ_NoFormat(user->fd,list);
1376 /* reset our lengths */
1377 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1385 /* if whats left in the list isnt empty, send it */
1388 WriteServ_NoFormat(user->fd,list);
1393 * return a count of the users on a specific channel accounting for
1394 * invisible users who won't increase the count. e.g. for /LIST
1396 int usercount_i(chanrec *c)
1403 CUList *ulist= c->GetUsers();
1404 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1406 if (!(i->second->modes[UM_INVISIBLE]))
1413 int usercount(chanrec *c)
1415 return (c ? c->GetUserCounter() : 0);
1419 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1420 * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
1421 * then their ip will be taken as 'priority' anyway, so for example,
1422 * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
1424 ConnectClass GetClass(userrec *user)
1426 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1428 if ((match(inet_ntoa(user->ip4),i->host.c_str())) || (match(user->host,i->host.c_str())))
1434 return *(Config->Classes.begin());
1438 * sends out an error notice to all connected clients (not to be used
1441 void send_error(char *s)
1443 log(DEBUG,"send_error: %s",s);
1445 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1447 userrec* t = (userrec*)(*i);
1448 if (t->registered == 7)
1450 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1454 // fix - unregistered connections receive ERROR, not NOTICE
1455 Write(t->fd,"ERROR :%s",s);
1460 void Error(int status)
1466 signal(SIGALRM, SIG_IGN);
1467 signal(SIGPIPE, SIG_IGN);
1468 signal(SIGTERM, SIG_IGN);
1469 signal(SIGABRT, SIG_IGN);
1470 signal(SIGSEGV, SIG_IGN);
1471 signal(SIGURG, SIG_IGN);
1472 signal(SIGKILL, SIG_IGN);
1473 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1475 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
1476 size = backtrace(array, 30);
1477 strings = backtrace_symbols(array, size);
1478 for (size_t i = 0; i < size; i++) {
1479 log(DEFAULT,"[%d] %s", i, strings[i]);
1482 WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/");
1484 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
1486 send_error("Somebody screwed up... Whoops. IRC Server terminating.");
1487 signal(SIGSEGV, SIG_DFL);
1488 if (raise(SIGSEGV) == -1)
1490 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
1495 // this function counts all users connected, wether they are registered or NOT.
1498 return clientlist.size();
1501 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1502 int registered_usercount(void)
1506 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1508 if (i->second->registered == 7) c++;
1514 int usercount_invisible(void)
1518 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1520 if ((i->second->registered == 7) && (i->second->modes[UM_INVISIBLE]))
1527 int usercount_opers(void)
1531 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1533 if (*(i->second->oper))
1539 int usercount_unknown(void)
1543 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1545 userrec* t = (userrec*)(*i);
1546 if (t->registered != 7)
1553 long chancount(void)
1555 return chanlist.size();
1562 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1564 userrec* t = (userrec*)(*i);
1565 if (t->registered == 7)
1572 void ShowMOTD(userrec *user)
1574 if (!Config->MOTD.size())
1576 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1579 WriteServ(user->fd,"375 %s :%s message of the day", user->nick, Config->ServerName);
1581 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1582 WriteServ(user->fd,"372 %s :- ",user->nick,Config->MOTD[i].c_str());
1584 WriteServ(user->fd,"376 %s :End of message of the day.", user->nick);
1587 void ShowRULES(userrec *user)
1589 if (!Config->RULES.size())
1591 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1594 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1596 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1597 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1599 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1602 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1603 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1604 // registration timeout maximum seconds)
1605 bool AllModulesReportReady(userrec* user)
1607 if (!Config->global_implementation[I_OnCheckReady])
1610 for (int i = 0; i <= MODCOUNT; i++)
1612 if (Config->implement_lists[i][I_OnCheckReady])
1614 int res = modules[i]->OnCheckReady(user);
1623 /* Make Sure Modules Are Avaliable!
1624 * (BugFix By Craig.. See? I do work! :p)
1625 * Modified by brain, requires const char*
1626 * to work with other API functions
1630 bool FileExists (const char* file)
1633 if ((input = fopen (file, "r")) == NULL)
1644 char* CleanFilename(char* name)
1646 char* p = name + strlen(name);
1647 while ((p != name) && (*p != '/')) p--;
1648 return (p != name ? ++p : p);
1651 bool DirValid(char* dirandfile)
1654 char buffer[MAXBUF];
1655 char otherdir[MAXBUF];
1658 strlcpy(work, dirandfile, MAXBUF);
1661 // we just want the dir
1673 // Get the current working directory
1674 if (getcwd(buffer, MAXBUF ) == NULL )
1679 if (getcwd(otherdir, MAXBUF ) == NULL )
1684 size_t t = strlen(work);
1686 if (strlen(otherdir) >= t)
1690 if (!strcmp(otherdir,work))
1703 std::string GetFullProgDir(char** argv, int argc)
1706 char buffer[MAXBUF];
1707 char otherdir[MAXBUF];
1710 strlcpy(work,argv[0],MAXBUF);
1713 // we just want the dir
1725 // Get the current working directory
1726 if (getcwd(buffer, MAXBUF) == NULL)
1731 if (getcwd(otherdir, MAXBUF) == NULL)
1738 int InsertMode(std::string &output, const char* mode, unsigned short section)
1740 unsigned short currsection = 1;
1741 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1743 if(section > 4 || section == 0)
1745 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1749 for(; pos < output.size(); pos++)
1751 if(section == currsection)
1754 if(output[pos] == ',')
1758 output.insert(pos, mode);
1762 bool IsValidChannelName(const char *chname)
1766 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1767 if (!chname || *chname != '#')
1772 c = (char *)chname + 1;
1786 /* too long a name - note funky pointer arithmetic here. */
1787 if ((c - chname) > CHANMAX)
1795 inline int charlcat(char* x,char y,int z)
1812 bool charremove(char* mp, char remove)
1815 bool shift_down = false;
1819 if (*mptr == remove)
1831 void OpenLog(char** argv, int argc)
1835 if (Config->logpath == "")
1837 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1842 Config->log_file = fopen(LOG_FILE,"a+");
1844 if (!Config->log_file)
1846 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1853 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1855 if (!Config->log_file)
1857 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1866 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1867 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1874 if (*Config->DieValue)
1876 printf("WARNING: %s\n\n",Config->DieValue);
1877 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1882 /* We must load the modules AFTER initializing the socket engine, now */
1883 void LoadAllModules(InspIRCd* ServerInstance)
1885 char configToken[MAXBUF];
1886 Config->module_names.clear();
1889 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1891 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1892 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1894 if (!ServerInstance->LoadModule(configToken))
1896 log(DEFAULT,"Exiting due to a module loader error.");
1897 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1902 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);