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);
96 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
97 fflush(Config->log_file);
103 printf("%s %s\n", TIMESTR, textbuffer);
108 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
110 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
112 void readfile(file_cache &F, const char* fname)
115 char linebuf[MAXBUF];
117 log(DEBUG,"readfile: loading %s",fname);
119 file = fopen(fname,"r");
125 fgets(linebuf,sizeof(linebuf),file);
126 linebuf[strlen(linebuf)-1]='\0';
135 F.push_back(linebuf);
143 log(DEBUG,"readfile: failed to load file: %s",fname);
146 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
150 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
151 * is a valid entry in the local FD table.
153 void Write_NoFormat(int sock, const char *text)
158 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
161 if (fd_ref_table[sock])
163 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
166 if (Config->GetIOHook(fd_ref_table[sock]->port))
170 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
172 catch (ModuleException& modexcept)
174 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
179 fd_ref_table[sock]->AddWriteBuf(tb);
181 ServerInstance->stats->statsSent += bytes;
184 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
188 * Same as Write_NoFormat(), but formatted printf() style first.
190 void Write(int sock, char *text, ...)
193 char textbuffer[MAXBUF];
197 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
202 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
206 if (fd_ref_table[sock])
209 va_start(argsPtr, text);
210 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
212 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
215 if (Config->GetIOHook(fd_ref_table[sock]->port))
219 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
221 catch (ModuleException& modexcept)
223 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
228 fd_ref_table[sock]->AddWriteBuf(tb);
230 ServerInstance->stats->statsSent += bytes;
233 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
236 /** WriteServ_NoFormat()
237 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
239 void WriteServ_NoFormat(int sock, const char* text)
244 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
247 if (fd_ref_table[sock])
249 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);
317 if (Config->GetIOHook(fd_ref_table[sock]->port))
321 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
323 catch (ModuleException& modexcept)
325 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
330 fd_ref_table[sock]->AddWriteBuf(tb);
332 ServerInstance->stats->statsSent += bytes;
335 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
338 /* write text from an originating user to originating user */
340 void WriteFrom(int sock, userrec *user,char* text, ...)
343 char textbuffer[MAXBUF];
347 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
350 if ((!text) || (!user))
352 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
356 if (fd_ref_table[sock])
359 va_start(argsPtr, text);
360 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
362 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
365 if (Config->GetIOHook(fd_ref_table[sock]->port))
369 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
371 catch (ModuleException& modexcept)
373 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
378 fd_ref_table[sock]->AddWriteBuf(tb);
381 ServerInstance->stats->statsSent += bytes;
384 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
387 /* write text to an destination user from a source user (e.g. user privmsg) */
389 void WriteTo(userrec *source, userrec *dest,char *data, ...)
391 char textbuffer[MAXBUF];
394 if ((!dest) || (!data))
396 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
403 va_start(argsPtr, data);
404 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
408 // if no source given send it from the server.
411 WriteServ_NoFormat(dest->fd,textbuffer);
415 WriteFrom_NoFormat(dest->fd,source,textbuffer);
419 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
421 if ((!dest) || (!data))
426 WriteServ_NoFormat(dest->fd,data);
430 WriteFrom_NoFormat(dest->fd,source,data);
434 /* write formatted text from a source user to all users on a channel
435 * including the sender (NOT for privmsg, notice etc!) */
437 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
439 char textbuffer[MAXBUF];
443 if ((!Ptr) || (!user) || (!text))
445 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
449 va_start(argsPtr, text);
450 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
453 ulist = Ptr->GetUsers();
455 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
457 if (i->second->fd != FD_MAGIC_NUMBER)
458 WriteTo_NoFormat(user,i->second,textbuffer);
462 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
466 if ((!Ptr) || (!user) || (!text))
468 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
472 ulist = Ptr->GetUsers();
474 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
476 if (i->second->fd != FD_MAGIC_NUMBER)
477 WriteTo_NoFormat(user,i->second,text);
482 /* write formatted text from a source user to all users on a channel
483 * including the sender (NOT for privmsg, notice etc!) doesnt send to
484 * users on remote servers */
486 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
488 char textbuffer[MAXBUF];
492 if ((!Ptr) || (!text))
494 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
498 va_start(argsPtr, text);
499 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
502 ulist = Ptr->GetUsers();
504 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
506 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
510 WriteServ_NoFormat(i->second->fd,textbuffer);
514 WriteTo_NoFormat(user,i->second,textbuffer);
520 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
524 if ((!Ptr) || (!text))
526 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
530 ulist = Ptr->GetUsers();
532 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
534 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
538 WriteServ_NoFormat(i->second->fd,text);
542 WriteTo_NoFormat(user,i->second,text);
550 void WriteChannelWithServ(const char* ServName, chanrec* Ptr, const char* text, ...)
552 char textbuffer[MAXBUF];
556 if ((!Ptr) || (!text))
558 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
562 va_start(argsPtr, text);
563 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
566 ulist = Ptr->GetUsers();
568 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
570 if (IS_LOCAL(i->second))
571 WriteServ_NoFormat(i->second->fd,textbuffer);
575 void WriteChannelWithServ_NoFormat(const char* ServName, chanrec* Ptr, const char* text)
579 if ((!Ptr) || (!text))
581 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
585 ulist = Ptr->GetUsers();
587 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
589 if (IS_LOCAL(i->second))
590 WriteServ_NoFormat(i->second->fd,text);
596 /* write formatted text from a source user to all users on a channel except
597 * for the sender (for privmsg etc) */
599 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
601 char textbuffer[MAXBUF];
605 if ((!Ptr) || (!user) || (!text))
607 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
611 va_start(argsPtr, text);
612 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
618 ulist = Ptr->GetOppedUsers();
621 ulist = Ptr->GetHalfoppedUsers();
624 ulist = Ptr->GetVoicedUsers();
627 ulist = Ptr->GetUsers();
631 log(DEBUG,"%d users to write to",ulist->size());
633 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
635 if ((IS_LOCAL(i->second)) && (user != i->second))
636 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
640 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
644 if ((!Ptr) || (!user) || (!text))
646 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
653 ulist = Ptr->GetOppedUsers();
656 ulist = Ptr->GetHalfoppedUsers();
659 ulist = Ptr->GetVoicedUsers();
662 ulist = Ptr->GetUsers();
666 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
668 if ((IS_LOCAL(i->second)) && (user != i->second))
669 WriteFrom_NoFormat(i->second->fd,user,text);
673 std::string GetServerDescription(const char* servername)
675 std::string description = "";
677 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
679 if (description != "")
685 // not a remote server that can be found, it must be me.
686 return Config->ServerDesc;
690 /* write a formatted string to all users who share at least one common
691 * channel, including the source user e.g. for use in NICK */
693 void WriteCommon(userrec *u, char* text, ...)
695 char textbuffer[MAXBUF];
697 bool sent_to_at_least_one = false;
701 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
705 if (u->registered != 7)
707 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
711 va_start(argsPtr, text);
712 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
715 // FIX: Stops a message going to the same person more than once
716 memset(&already_sent,0,MAX_DESCRIPTORS);
718 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
720 if (((ucrec*)(*v))->channel)
722 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
724 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
726 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
728 already_sent[i->second->fd] = 1;
729 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
730 sent_to_at_least_one = true;
737 * if the user was not in any channels, no users will receive the text. Make sure the user
738 * receives their OWN message for WriteCommon
740 if (!sent_to_at_least_one)
742 WriteFrom_NoFormat(u->fd,u,textbuffer);
746 void WriteCommon_NoFormat(userrec *u, const char* text)
748 bool sent_to_at_least_one = false;
752 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
756 if (u->registered != 7)
758 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
762 // FIX: Stops a message going to the same person more than once
763 memset(&already_sent,0,MAX_DESCRIPTORS);
765 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
767 if (((ucrec*)(*v))->channel)
769 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
771 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
773 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
775 already_sent[i->second->fd] = 1;
776 WriteFrom_NoFormat(i->second->fd,u,text);
777 sent_to_at_least_one = true;
784 * if the user was not in any channels, no users will receive the text. Make sure the user
785 * receives their OWN message for WriteCommon
787 if (!sent_to_at_least_one)
789 WriteFrom_NoFormat(u->fd,u,text);
794 /* write a formatted string to all users who share at least one common
795 * channel, NOT including the source user e.g. for use in QUIT
798 void WriteCommonExcept(userrec *u, char* text, ...)
800 char textbuffer[MAXBUF];
801 char oper_quit[MAXBUF];
802 bool quit_munge = false;
808 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
812 if (u->registered != 7)
814 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
818 va_start(argsPtr, text);
819 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
822 if ((Config->HideSplits) && (total > 6))
824 /* Yeah yeah, this is ugly. But its fast, live with it. */
825 char* check = textbuffer;
827 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
829 std::stringstream split(check);
830 std::string server_one;
831 std::string server_two;
836 if ((FindServerName(server_one)) && (FindServerName(server_two)))
838 strlcpy(oper_quit,textbuffer,MAXQUIT);
839 strlcpy(check,"*.net *.split",MAXQUIT);
845 if ((Config->HideBans) && (total > 13) && (!quit_munge))
847 char* check = textbuffer;
850 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
854 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
856 strlcpy(oper_quit,textbuffer,MAXQUIT);
857 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
863 memset(&already_sent,0,MAX_DESCRIPTORS);
865 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
867 if (((ucrec*)(*v))->channel)
869 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
871 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
875 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
877 already_sent[i->second->fd] = 1;
881 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
884 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
892 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
896 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
900 if (u->registered != 7)
902 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
906 memset(&already_sent,0,MAX_DESCRIPTORS);
908 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
910 if (((ucrec*)(*v))->channel)
912 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
914 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
918 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
920 already_sent[i->second->fd] = 1;
921 WriteFrom_NoFormat(i->second->fd,u,text);
930 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
931 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
932 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
933 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
934 * uses the oper list, which means if you have 2000 users but only 5 opers,
935 * it iterates 5 times.
937 void WriteOpers(const char* text, ...)
939 char textbuffer[MAXBUF];
944 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
948 va_start(argsPtr, text);
949 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
952 WriteOpers_NoFormat(textbuffer);
955 void WriteOpers_NoFormat(const char* text)
959 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
963 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
969 if (a->modes[UM_SERVERNOTICE])
971 // send server notices to all with +s
972 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
978 void ServerNoticeAll(char* text, ...)
983 char textbuffer[MAXBUF];
984 char formatbuffer[MAXBUF];
986 va_start (argsPtr, text);
987 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
990 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
992 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
994 userrec* t = (userrec*)(*i);
995 WriteServ_NoFormat(t->fd,formatbuffer);
999 void ServerPrivmsgAll(char* text, ...)
1004 char textbuffer[MAXBUF];
1005 char formatbuffer[MAXBUF];
1007 va_start (argsPtr, text);
1008 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1011 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1013 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1015 userrec* t = (userrec*)(*i);
1016 WriteServ_NoFormat(t->fd,formatbuffer);
1020 void WriteMode(const char* modes, int flags, const char* text, ...)
1022 char textbuffer[MAXBUF];
1026 if ((!text) || (!modes) || (!flags))
1028 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1032 va_start(argsPtr, text);
1033 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1035 modelen = strlen(modes);
1037 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1039 userrec* t = (userrec*)(*i);
1040 bool send_to_user = false;
1042 if (flags == WM_AND)
1044 send_to_user = true;
1046 for (int n = 0; n < modelen; n++)
1048 if (!t->modes[modes[n]-65])
1050 send_to_user = false;
1055 else if (flags == WM_OR)
1057 send_to_user = false;
1059 for (int n = 0; n < modelen; n++)
1061 if (t->modes[modes[n]-65])
1063 send_to_user = true;
1071 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1076 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1078 char textbuffer[MAXBUF];
1079 char formatbuffer[MAXBUF];
1082 if ((!text) || (!source))
1084 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1088 va_start(argsPtr, text);
1089 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1092 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1094 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1096 userrec* t = (userrec*)(*i);
1097 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1102 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1104 char textbuffer[MAXBUF];
1105 char formatbuffer[MAXBUF];
1108 if ((!text) || (!source))
1110 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1114 va_start(argsPtr, text);
1115 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1118 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1120 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1122 userrec* t = (userrec*)(*i);
1124 if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS]))
1126 WriteTo_NoFormat(source,t,formatbuffer);
1131 /* convert a string to lowercase. Note following special circumstances
1132 * taken from RFC 1459. Many "official" server branches still hold to this
1133 * rule so i will too;
1135 * Because of IRC's scandanavian origin, the characters {}| are
1136 * considered to be the lower case equivalents of the characters []\,
1137 * respectively. This is a critical issue when determining the
1138 * equivalence of two nicknames.
1140 void strlower(char *n)
1144 for (char* t = n; *t; t++)
1145 *t = lowermap[(unsigned char)*t];
1149 /* Find a user record by nickname and return a pointer to it */
1151 userrec* Find(const std::string &nick)
1153 user_hash::iterator iter = clientlist.find(nick);
1155 if (iter == clientlist.end())
1156 /* Couldn't find it */
1159 return iter->second;
1162 userrec* Find(const char* nick)
1164 user_hash::iterator iter;
1169 iter = clientlist.find(nick);
1171 if (iter == clientlist.end())
1174 return iter->second;
1177 /* find a channel record by channel name and return a pointer to it */
1179 chanrec* FindChan(const char* chan)
1181 chan_hash::iterator iter;
1185 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1189 iter = chanlist.find(chan);
1191 if (iter == chanlist.end())
1192 /* Couldn't find it */
1195 return iter->second;
1199 long GetMaxBans(char* name)
1202 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1205 if (match(name,x.c_str()))
1213 void purge_empty_chans(userrec* u)
1215 std::vector<chanrec*> to_delete;
1217 // firstly decrement the count on each channel
1218 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1220 if (((ucrec*)(*f))->channel)
1222 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1224 /* No users left in here, mark it for deletion */
1225 to_delete.push_back(((ucrec*)(*f))->channel);
1226 ((ucrec*)(*f))->channel = NULL;
1231 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1233 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1235 chanrec* thischan = (chanrec*)*n;
1236 chan_hash::iterator i2 = chanlist.find(thischan->name);
1237 if (i2 != chanlist.end())
1239 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1250 char* chanmodes(chanrec *chan, bool showkey)
1252 static char scratch[MAXBUF];
1253 static char sparam[MAXBUF];
1254 char* offset = scratch;
1255 std::string extparam = "";
1259 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1267 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1268 for(int n = 0; n < 64; n++)
1277 extparam = (showkey ? chan->key : "<key>");
1280 extparam = ConvToStr(chan->limit);
1288 /* We know these have no parameters */
1291 extparam = chan->GetModeParameter(n+65);
1296 charlcat(sparam,' ',MAXBUF);
1297 strlcat(sparam,extparam.c_str(),MAXBUF);
1302 /* Null terminate scratch */
1304 strlcat(scratch,sparam,MAXBUF);
1309 /* compile a userlist of a channel into a string, each nick seperated by
1310 * spaces and op, voice etc status shown as @ and + */
1312 void userlist(userrec *user,chanrec *c)
1314 if ((!c) || (!user))
1316 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1321 size_t dlen, curlen;
1323 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1326 char* ptr = list + dlen;
1328 CUList *ulist= c->GetUsers();
1330 /* Improvement by Brain - this doesnt change in value, so why was it inside
1333 bool has_user = c->HasUser(user);
1335 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1337 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
1340 * user is +i, and source not on the channel, does not show
1341 * nick in NAMES list
1346 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1353 if (curlen > (480-NICKMAX))
1355 /* list overflowed into multiple numerics */
1356 WriteServ_NoFormat(user->fd,list);
1358 /* reset our lengths */
1359 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1367 /* if whats left in the list isnt empty, send it */
1370 WriteServ_NoFormat(user->fd,list);
1375 * return a count of the users on a specific channel accounting for
1376 * invisible users who won't increase the count. e.g. for /LIST
1378 int usercount_i(chanrec *c)
1385 CUList *ulist= c->GetUsers();
1386 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1388 if (!(i->second->modes[UM_INVISIBLE]))
1395 int usercount(chanrec *c)
1397 return (c ? c->GetUserCounter() : 0);
1401 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1402 * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
1403 * then their ip will be taken as 'priority' anyway, so for example,
1404 * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
1406 ConnectClass GetClass(userrec *user)
1408 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1410 if ((match(inet_ntoa(user->ip4),i->host.c_str())) || (match(user->host,i->host.c_str())))
1416 return *(Config->Classes.begin());
1420 * sends out an error notice to all connected clients (not to be used
1423 void send_error(char *s)
1425 log(DEBUG,"send_error: %s",s);
1427 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1429 userrec* t = (userrec*)(*i);
1430 if (t->registered == 7)
1432 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1436 // fix - unregistered connections receive ERROR, not NOTICE
1437 Write(t->fd,"ERROR :%s",s);
1442 void Error(int status)
1448 signal(SIGALRM, SIG_IGN);
1449 signal(SIGPIPE, SIG_IGN);
1450 signal(SIGTERM, SIG_IGN);
1451 signal(SIGABRT, SIG_IGN);
1452 signal(SIGSEGV, SIG_IGN);
1453 signal(SIGURG, SIG_IGN);
1454 signal(SIGKILL, SIG_IGN);
1455 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1457 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
1458 size = backtrace(array, 30);
1459 strings = backtrace_symbols(array, size);
1460 for (size_t i = 0; i < size; i++) {
1461 log(DEFAULT,"[%d] %s", i, strings[i]);
1464 WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/");
1466 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
1468 send_error("Somebody screwed up... Whoops. IRC Server terminating.");
1469 signal(SIGSEGV, SIG_DFL);
1470 if (raise(SIGSEGV) == -1)
1472 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
1477 // this function counts all users connected, wether they are registered or NOT.
1480 return clientlist.size();
1483 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1484 int registered_usercount(void)
1488 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1490 if (i->second->registered == 7) c++;
1496 int usercount_invisible(void)
1500 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1502 if ((i->second->registered == 7) && (i->second->modes[UM_INVISIBLE]))
1509 int usercount_opers(void)
1513 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1515 if (*(i->second->oper))
1521 int usercount_unknown(void)
1525 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1527 userrec* t = (userrec*)(*i);
1528 if (t->registered != 7)
1535 long chancount(void)
1537 return chanlist.size();
1544 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1546 userrec* t = (userrec*)(*i);
1547 if (t->registered == 7)
1554 void ShowMOTD(userrec *user)
1556 static char mbuf[MAXBUF];
1557 static char crud[MAXBUF];
1558 std::string WholeMOTD = "";
1560 if (!Config->MOTD.size())
1562 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1566 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1567 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1568 WholeMOTD = WholeMOTD + mbuf;
1570 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1571 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1573 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1574 WholeMOTD = WholeMOTD + mbuf;
1576 // only one write operation
1577 if (Config->GetIOHook(user->port))
1581 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1583 catch (ModuleException& modexcept)
1585 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1590 user->AddWriteBuf(WholeMOTD);
1593 ServerInstance->stats->statsSent += WholeMOTD.length();
1596 void ShowRULES(userrec *user)
1598 if (!Config->RULES.size())
1600 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1603 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1605 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1606 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1608 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1611 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1612 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1613 // registration timeout maximum seconds)
1614 bool AllModulesReportReady(userrec* user)
1616 if (!Config->global_implementation[I_OnCheckReady])
1619 for (int i = 0; i <= MODCOUNT; i++)
1621 if (Config->implement_lists[i][I_OnCheckReady])
1623 int res = modules[i]->OnCheckReady(user);
1632 /* Make Sure Modules Are Avaliable!
1633 * (BugFix By Craig.. See? I do work! :p)
1634 * Modified by brain, requires const char*
1635 * to work with other API functions
1639 bool FileExists (const char* file)
1642 if ((input = fopen (file, "r")) == NULL)
1653 char* CleanFilename(char* name)
1655 char* p = name + strlen(name);
1656 while ((p != name) && (*p != '/')) p--;
1657 return (p != name ? ++p : p);
1660 bool DirValid(char* dirandfile)
1663 char buffer[MAXBUF];
1664 char otherdir[MAXBUF];
1667 strlcpy(work, dirandfile, MAXBUF);
1670 // we just want the dir
1682 // Get the current working directory
1683 if (getcwd(buffer, MAXBUF ) == NULL )
1688 if (getcwd(otherdir, MAXBUF ) == NULL )
1693 size_t t = strlen(work);
1695 if (strlen(otherdir) >= t)
1699 if (!strcmp(otherdir,work))
1712 std::string GetFullProgDir(char** argv, int argc)
1715 char buffer[MAXBUF];
1716 char otherdir[MAXBUF];
1719 strlcpy(work,argv[0],MAXBUF);
1722 // we just want the dir
1734 // Get the current working directory
1735 if (getcwd(buffer, MAXBUF) == NULL)
1740 if (getcwd(otherdir, MAXBUF) == NULL)
1747 int InsertMode(std::string &output, const char* mode, unsigned short section)
1749 unsigned short currsection = 1;
1750 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1752 if(section > 4 || section == 0)
1754 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1758 for(; pos < output.size(); pos++)
1760 if(section == currsection)
1763 if(output[pos] == ',')
1767 output.insert(pos, mode);
1771 bool IsValidChannelName(const char *chname)
1775 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1776 if (!chname || *chname != '#')
1781 c = (char *)chname + 1;
1795 /* too long a name - note funky pointer arithmetic here. */
1796 if ((c - chname) > CHANMAX)
1804 inline int charlcat(char* x,char y,int z)
1821 bool charremove(char* mp, char remove)
1824 bool shift_down = false;
1828 if (*mptr == remove)
1840 void OpenLog(char** argv, int argc)
1844 if (Config->logpath == "")
1846 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1851 Config->log_file = fopen(LOG_FILE,"a+");
1853 if (!Config->log_file)
1855 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1862 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1864 if (!Config->log_file)
1866 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1875 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1876 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1883 if (*Config->DieValue)
1885 printf("WARNING: %s\n\n",Config->DieValue);
1886 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1891 /* We must load the modules AFTER initializing the socket engine, now */
1892 void LoadAllModules(InspIRCd* ServerInstance)
1894 char configToken[MAXBUF];
1895 Config->module_names.clear();
1898 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1900 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1901 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1903 if (!ServerInstance->LoadModule(configToken))
1905 log(DEFAULT,"Exiting due to a module loader error.");
1906 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1911 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);