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;
67 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
68 * is greater than the configured loglevel.
70 void do_log(int level, const char *text, ...)
73 char textbuffer[MAXBUF];
75 /* If we were given -debug we output all messages, regardless of configured loglevel */
76 if ((level < Config->LogLevel) && !Config->forcedebug)
81 struct tm *timeinfo = localtime(&TIME);
83 strlcpy(TIMESTR,asctime(timeinfo),26);
90 va_start(argsPtr, text);
91 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
95 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
100 printf("%s %s\n", TIMESTR, textbuffer);
105 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
107 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
109 void readfile(file_cache &F, const char* fname)
112 char linebuf[MAXBUF];
114 log(DEBUG,"readfile: loading %s",fname);
116 file = fopen(fname,"r");
122 fgets(linebuf,sizeof(linebuf),file);
123 linebuf[strlen(linebuf)-1]='\0';
132 F.push_back(linebuf);
140 log(DEBUG,"readfile: failed to load file: %s",fname);
143 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
147 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
148 * is a valid entry in the local FD table.
150 void Write_NoFormat(int sock, const char *text)
155 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
158 if (fd_ref_table[sock])
160 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
163 if (Config->GetIOHook(fd_ref_table[sock]->port))
167 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
169 catch (ModuleException& modexcept)
171 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
176 fd_ref_table[sock]->AddWriteBuf(tb);
178 ServerInstance->stats->statsSent += bytes;
181 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
185 * Same as Write_NoFormat(), but formatted printf() style first.
187 void Write(int sock, char *text, ...)
190 char textbuffer[MAXBUF];
194 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
199 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
203 if (fd_ref_table[sock])
206 va_start(argsPtr, text);
207 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
209 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
212 if (Config->GetIOHook(fd_ref_table[sock]->port))
216 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
218 catch (ModuleException& modexcept)
220 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
225 fd_ref_table[sock]->AddWriteBuf(tb);
227 ServerInstance->stats->statsSent += bytes;
230 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
233 /** WriteServ_NoFormat()
234 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
236 void WriteServ_NoFormat(int sock, const char* text)
241 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
244 if (fd_ref_table[sock])
246 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
249 if (Config->GetIOHook(fd_ref_table[sock]->port))
253 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
255 catch (ModuleException& modexcept)
257 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
262 fd_ref_table[sock]->AddWriteBuf(tb);
264 ServerInstance->stats->statsSent += bytes;
267 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
271 * Same as Write(), except `text' is prefixed with `:server.name '.
273 void WriteServ(int sock, char* text, ...)
276 char textbuffer[MAXBUF];
278 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
283 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
287 if (!fd_ref_table[sock])
290 va_start(argsPtr, text);
291 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
294 WriteServ_NoFormat(sock, textbuffer);
297 /** WriteFrom_NoFormat()
298 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
299 * the nick, user, and host of `user'.
301 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
306 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
309 if (fd_ref_table[sock])
311 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
314 if (Config->GetIOHook(fd_ref_table[sock]->port))
318 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
320 catch (ModuleException& modexcept)
322 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
327 fd_ref_table[sock]->AddWriteBuf(tb);
329 ServerInstance->stats->statsSent += bytes;
332 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
335 /* write text from an originating user to originating user */
337 void WriteFrom(int sock, userrec *user,char* text, ...)
340 char textbuffer[MAXBUF];
344 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
347 if ((!text) || (!user))
349 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
353 if (fd_ref_table[sock])
356 va_start(argsPtr, text);
357 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
359 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
362 if (Config->GetIOHook(fd_ref_table[sock]->port))
366 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
368 catch (ModuleException& modexcept)
370 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
375 fd_ref_table[sock]->AddWriteBuf(tb);
378 ServerInstance->stats->statsSent += bytes;
381 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
384 /* write text to an destination user from a source user (e.g. user privmsg) */
386 void WriteTo(userrec *source, userrec *dest,char *data, ...)
388 char textbuffer[MAXBUF];
391 if ((!dest) || (!data))
393 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
400 va_start(argsPtr, data);
401 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(char* ServName, chanrec* Ptr, 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(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(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 // FIX: Stops a message going to the same person more than once
713 memset(&already_sent,0,MAX_DESCRIPTORS);
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]))
725 already_sent[i->second->fd] = 1;
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 // FIX: Stops a message going to the same person more than once
760 memset(&already_sent,0,MAX_DESCRIPTORS);
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]))
772 already_sent[i->second->fd] = 1;
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 :
860 memset(&already_sent,0,MAX_DESCRIPTORS);
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]))
874 already_sent[i->second->fd] = 1;
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");
903 memset(&already_sent,0,MAX_DESCRIPTORS);
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]))
917 already_sent[i->second->fd] = 1;
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->modebits & 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 (!hasumode(t,modes[n]))
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 (hasumode(t,modes[n]))
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->modebits & 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 if (((ucrec*)(*f))->channel)
1219 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1221 /* No users left in here, mark it for deletion */
1222 to_delete.push_back(((ucrec*)(*f))->channel);
1223 ((ucrec*)(*f))->channel = NULL;
1228 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1230 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1232 chanrec* thischan = (chanrec*)*n;
1233 chan_hash::iterator i2 = chanlist.find(thischan->name);
1234 if (i2 != chanlist.end())
1236 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1247 char* chanmodes(chanrec *chan, bool showkey)
1249 static char scratch[MAXBUF];
1250 static char sparam[MAXBUF];
1251 char* offset = scratch;
1252 std::string extparam = "";
1256 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1264 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1265 for(int n = 0; n < 64; n++)
1274 extparam = (showkey ? chan->key : "<key>");
1277 extparam = ConvToStr(chan->limit);
1285 /* We know these have no parameters */
1288 extparam = chan->GetModeParameter(n+65);
1293 charlcat(sparam,' ',MAXBUF);
1294 strlcat(sparam,extparam.c_str(),MAXBUF);
1299 /* Null terminate scratch */
1301 strlcat(scratch,sparam,MAXBUF);
1306 /* compile a userlist of a channel into a string, each nick seperated by
1307 * spaces and op, voice etc status shown as @ and + */
1309 void userlist(userrec *user,chanrec *c)
1311 if ((!c) || (!user))
1313 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1318 size_t dlen, curlen;
1320 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1323 char* ptr = list + dlen;
1325 CUList *ulist= c->GetUsers();
1327 /* Improvement by Brain - this doesnt change in value, so why was it inside
1330 bool has_user = c->HasUser(user);
1332 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1334 if ((!has_user) && (i->second->modebits & UM_INVISIBLE))
1337 * user is +i, and source not on the channel, does not show
1338 * nick in NAMES list
1343 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1350 if (curlen > (480-NICKMAX))
1352 /* list overflowed into multiple numerics */
1353 WriteServ_NoFormat(user->fd,list);
1355 /* reset our lengths */
1356 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1364 /* if whats left in the list isnt empty, send it */
1367 WriteServ_NoFormat(user->fd,list);
1372 * return a count of the users on a specific channel accounting for
1373 * invisible users who won't increase the count. e.g. for /LIST
1375 int usercount_i(chanrec *c)
1382 CUList *ulist= c->GetUsers();
1383 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1385 if (!(i->second->modebits & UM_INVISIBLE))
1392 int usercount(chanrec *c)
1394 return (c ? c->GetUserCounter() : 0);
1398 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1399 * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
1400 * then their ip will be taken as 'priority' anyway, so for example,
1401 * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
1403 ConnectClass GetClass(userrec *user)
1405 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1407 if ((match(inet_ntoa(user->ip4),i->host.c_str())) || (match(user->host,i->host.c_str())))
1413 return *(Config->Classes.begin());
1417 * sends out an error notice to all connected clients (not to be used
1420 void send_error(char *s)
1422 log(DEBUG,"send_error: %s",s);
1424 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1426 userrec* t = (userrec*)(*i);
1427 if (t->registered == 7)
1429 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1433 // fix - unregistered connections receive ERROR, not NOTICE
1434 Write(t->fd,"ERROR :%s",s);
1439 void Error(int status)
1445 signal(SIGALRM, SIG_IGN);
1446 signal(SIGPIPE, SIG_IGN);
1447 signal(SIGTERM, SIG_IGN);
1448 signal(SIGABRT, SIG_IGN);
1449 signal(SIGSEGV, SIG_IGN);
1450 signal(SIGURG, SIG_IGN);
1451 signal(SIGKILL, SIG_IGN);
1452 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1454 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
1455 size = backtrace(array, 30);
1456 strings = backtrace_symbols(array, size);
1457 for (size_t i = 0; i < size; i++) {
1458 log(DEFAULT,"[%d] %s", i, strings[i]);
1461 WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/");
1463 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
1465 send_error("Somebody screwed up... Whoops. IRC Server terminating.");
1466 signal(SIGSEGV, SIG_DFL);
1467 if (raise(SIGSEGV) == -1)
1469 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
1474 // this function counts all users connected, wether they are registered or NOT.
1477 return clientlist.size();
1480 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1481 int registered_usercount(void)
1485 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1487 if (i->second->registered == 7) c++;
1493 int usercount_invisible(void)
1497 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1499 if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE))
1506 int usercount_opers(void)
1510 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1512 if (*(i->second->oper))
1518 int usercount_unknown(void)
1522 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1524 userrec* t = (userrec*)(*i);
1525 if (t->registered != 7)
1532 long chancount(void)
1534 return chanlist.size();
1541 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1543 userrec* t = (userrec*)(*i);
1544 if (t->registered == 7)
1551 void ShowMOTD(userrec *user)
1553 static char mbuf[MAXBUF];
1554 static char crud[MAXBUF];
1555 std::string WholeMOTD = "";
1557 if (!Config->MOTD.size())
1559 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1563 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1564 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1565 WholeMOTD = WholeMOTD + mbuf;
1567 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1568 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1570 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1571 WholeMOTD = WholeMOTD + mbuf;
1573 // only one write operation
1574 if (Config->GetIOHook(user->port))
1578 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1580 catch (ModuleException& modexcept)
1582 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1587 user->AddWriteBuf(WholeMOTD);
1590 ServerInstance->stats->statsSent += WholeMOTD.length();
1593 void ShowRULES(userrec *user)
1595 if (!Config->RULES.size())
1597 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1600 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1602 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1603 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1605 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1608 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1609 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1610 // registration timeout maximum seconds)
1611 bool AllModulesReportReady(userrec* user)
1613 if (!Config->global_implementation[I_OnCheckReady])
1616 for (int i = 0; i <= MODCOUNT; i++)
1618 if (Config->implement_lists[i][I_OnCheckReady])
1620 int res = modules[i]->OnCheckReady(user);
1629 /* Make Sure Modules Are Avaliable!
1630 * (BugFix By Craig.. See? I do work! :p)
1631 * Modified by brain, requires const char*
1632 * to work with other API functions
1636 bool FileExists (const char* file)
1639 if ((input = fopen (file, "r")) == NULL)
1650 char* CleanFilename(char* name)
1652 char* p = name + strlen(name);
1653 while ((p != name) && (*p != '/')) p--;
1654 return (p != name ? ++p : p);
1657 bool DirValid(char* dirandfile)
1660 char buffer[MAXBUF];
1661 char otherdir[MAXBUF];
1664 strlcpy(work, dirandfile, MAXBUF);
1667 // we just want the dir
1679 // Get the current working directory
1680 if (getcwd(buffer, MAXBUF ) == NULL )
1685 if (getcwd(otherdir, MAXBUF ) == NULL )
1690 size_t t = strlen(work);
1692 if (strlen(otherdir) >= t)
1696 if (!strcmp(otherdir,work))
1709 std::string GetFullProgDir(char** argv, int argc)
1712 char buffer[MAXBUF];
1713 char otherdir[MAXBUF];
1716 strlcpy(work,argv[0],MAXBUF);
1719 // we just want the dir
1731 // Get the current working directory
1732 if (getcwd(buffer, MAXBUF) == NULL)
1737 if (getcwd(otherdir, MAXBUF) == NULL)
1744 int InsertMode(std::string &output, const char* mode, unsigned short section)
1746 unsigned short currsection = 1;
1747 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1749 if(section > 4 || section == 0)
1751 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1755 for(; pos < output.size(); pos++)
1757 if(section == currsection)
1760 if(output[pos] == ',')
1764 output.insert(pos, mode);
1768 bool IsValidChannelName(const char *chname)
1772 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1773 if (!chname || *chname != '#')
1778 c = (char *)chname + 1;
1792 /* too long a name - note funky pointer arithmetic here. */
1793 if ((c - chname) > CHANMAX)
1801 inline int charlcat(char* x,char y,int z)
1818 bool charremove(char* mp, char remove)
1821 bool shift_down = false;
1825 if (*mptr == remove)
1837 void OpenLog(char** argv, int argc)
1841 if (Config->logpath == "")
1843 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1848 Config->log_file = fopen(LOG_FILE,"a+");
1850 if (!Config->log_file)
1852 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1859 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1861 if (!Config->log_file)
1863 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1872 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1873 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1880 if (*Config->DieValue)
1882 printf("WARNING: %s\n\n",Config->DieValue);
1883 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1888 /* We must load the modules AFTER initializing the socket engine, now */
1889 void LoadAllModules(InspIRCd* ServerInstance)
1891 char configToken[MAXBUF];
1892 Config->module_names.clear();
1895 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1897 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1898 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1900 if (!ServerInstance->LoadModule(configToken))
1902 log(DEFAULT,"Exiting due to a module loader error.");
1903 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1908 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);