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 log(int level, 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 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
161 if (fd_ref_table[sock])
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 va_start(argsPtr, text);
204 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
206 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
209 if (fd_ref_table[sock])
211 if (Config->GetIOHook(fd_ref_table[sock]->port))
215 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
217 catch (ModuleException& modexcept)
219 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
224 fd_ref_table[sock]->AddWriteBuf(tb);
226 ServerInstance->stats->statsSent += bytes;
229 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
232 /** WriteServ_NoFormat()
233 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
235 void WriteServ_NoFormat(int sock, const char* text)
240 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
243 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
246 if (fd_ref_table[sock])
248 if (Config->GetIOHook(fd_ref_table[sock]->port))
252 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
254 catch (ModuleException& modexcept)
256 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
261 fd_ref_table[sock]->AddWriteBuf(tb);
263 ServerInstance->stats->statsSent += bytes;
266 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
270 * Same as Write(), except `text' is prefixed with `:server.name '.
272 void WriteServ(int sock, char* text, ...)
275 char textbuffer[MAXBUF];
277 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
282 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
286 va_start(argsPtr, text);
287 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
290 WriteServ_NoFormat(sock, textbuffer);
293 /** WriteFrom_NoFormat()
294 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
295 * the nick, user, and host of `user'.
297 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
302 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
305 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
308 if (fd_ref_table[sock])
310 if (Config->GetIOHook(fd_ref_table[sock]->port))
314 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
316 catch (ModuleException& modexcept)
318 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
323 fd_ref_table[sock]->AddWriteBuf(tb);
325 ServerInstance->stats->statsSent += bytes;
328 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
331 /* write text from an originating user to originating user */
333 void WriteFrom(int sock, userrec *user,char* text, ...)
336 char textbuffer[MAXBUF];
340 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
343 if ((!text) || (!user))
345 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
349 va_start(argsPtr, text);
350 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
352 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
355 if (fd_ref_table[sock])
357 if (Config->GetIOHook(fd_ref_table[sock]->port))
361 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
363 catch (ModuleException& modexcept)
365 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
370 fd_ref_table[sock]->AddWriteBuf(tb);
373 ServerInstance->stats->statsSent += bytes;
376 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
379 /* write text to an destination user from a source user (e.g. user privmsg) */
381 void WriteTo(userrec *source, userrec *dest,char *data, ...)
383 char textbuffer[MAXBUF];
386 if ((!dest) || (!data))
388 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
395 va_start(argsPtr, data);
396 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
400 // if no source given send it from the server.
403 WriteServ_NoFormat(dest->fd,textbuffer);
407 WriteFrom_NoFormat(dest->fd,source,textbuffer);
411 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
413 if ((!dest) || (!data))
418 WriteServ_NoFormat(dest->fd,data);
422 WriteFrom_NoFormat(dest->fd,source,data);
426 /* write formatted text from a source user to all users on a channel
427 * including the sender (NOT for privmsg, notice etc!) */
429 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
431 char textbuffer[MAXBUF];
435 if ((!Ptr) || (!user) || (!text))
437 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
441 va_start(argsPtr, text);
442 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
445 ulist = Ptr->GetUsers();
447 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
449 if (i->second->fd != FD_MAGIC_NUMBER)
450 WriteTo_NoFormat(user,i->second,textbuffer);
454 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
458 if ((!Ptr) || (!user) || (!text))
460 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
464 ulist = Ptr->GetUsers();
466 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
468 if (i->second->fd != FD_MAGIC_NUMBER)
469 WriteTo_NoFormat(user,i->second,text);
474 /* write formatted text from a source user to all users on a channel
475 * including the sender (NOT for privmsg, notice etc!) doesnt send to
476 * users on remote servers */
478 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
480 char textbuffer[MAXBUF];
484 if ((!Ptr) || (!text))
486 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
490 va_start(argsPtr, text);
491 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
494 ulist = Ptr->GetUsers();
496 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
498 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
502 WriteServ_NoFormat(i->second->fd,textbuffer);
506 WriteTo_NoFormat(user,i->second,textbuffer);
512 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
516 if ((!Ptr) || (!text))
518 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
522 ulist = Ptr->GetUsers();
524 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
526 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
530 WriteServ_NoFormat(i->second->fd,text);
534 WriteTo_NoFormat(user,i->second,text);
542 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
544 char textbuffer[MAXBUF];
548 if ((!Ptr) || (!text))
550 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
554 va_start(argsPtr, text);
555 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
558 ulist = Ptr->GetUsers();
560 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
562 if (IS_LOCAL(i->second))
563 WriteServ_NoFormat(i->second->fd,textbuffer);
567 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
571 if ((!Ptr) || (!text))
573 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
577 ulist = Ptr->GetUsers();
579 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
581 if (IS_LOCAL(i->second))
582 WriteServ_NoFormat(i->second->fd,text);
588 /* write formatted text from a source user to all users on a channel except
589 * for the sender (for privmsg etc) */
591 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
593 char textbuffer[MAXBUF];
597 if ((!Ptr) || (!user) || (!text))
599 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
603 va_start(argsPtr, text);
604 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
610 ulist = Ptr->GetOppedUsers();
613 ulist = Ptr->GetHalfoppedUsers();
616 ulist = Ptr->GetVoicedUsers();
619 ulist = Ptr->GetUsers();
623 log(DEBUG,"%d users to write to",ulist->size());
625 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
627 if ((IS_LOCAL(i->second)) && (user != i->second))
628 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
632 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
636 if ((!Ptr) || (!user) || (!text))
638 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
645 ulist = Ptr->GetOppedUsers();
648 ulist = Ptr->GetHalfoppedUsers();
651 ulist = Ptr->GetVoicedUsers();
654 ulist = Ptr->GetUsers();
658 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
660 if ((IS_LOCAL(i->second)) && (user != i->second))
661 WriteFrom_NoFormat(i->second->fd,user,text);
665 std::string GetServerDescription(char* servername)
667 std::string description = "";
669 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
671 if (description != "")
677 // not a remote server that can be found, it must be me.
678 return Config->ServerDesc;
682 /* write a formatted string to all users who share at least one common
683 * channel, including the source user e.g. for use in NICK */
685 void WriteCommon(userrec *u, char* text, ...)
687 char textbuffer[MAXBUF];
689 bool sent_to_at_least_one = false;
693 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
697 if (u->registered != 7)
699 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
703 va_start(argsPtr, text);
704 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
707 // FIX: Stops a message going to the same person more than once
708 memset(&already_sent,0,MAX_DESCRIPTORS);
710 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
712 if (((ucrec*)(*v))->channel)
714 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
716 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
718 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
720 already_sent[i->second->fd] = 1;
721 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
722 sent_to_at_least_one = true;
729 * if the user was not in any channels, no users will receive the text. Make sure the user
730 * receives their OWN message for WriteCommon
732 if (!sent_to_at_least_one)
734 WriteFrom_NoFormat(u->fd,u,textbuffer);
738 void WriteCommon_NoFormat(userrec *u, const char* text)
740 bool sent_to_at_least_one = false;
744 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
748 if (u->registered != 7)
750 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
754 // FIX: Stops a message going to the same person more than once
755 memset(&already_sent,0,MAX_DESCRIPTORS);
757 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
759 if (((ucrec*)(*v))->channel)
761 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
763 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
765 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
767 already_sent[i->second->fd] = 1;
768 WriteFrom_NoFormat(i->second->fd,u,text);
769 sent_to_at_least_one = true;
776 * if the user was not in any channels, no users will receive the text. Make sure the user
777 * receives their OWN message for WriteCommon
779 if (!sent_to_at_least_one)
781 WriteFrom_NoFormat(u->fd,u,text);
786 /* write a formatted string to all users who share at least one common
787 * channel, NOT including the source user e.g. for use in QUIT
790 void WriteCommonExcept(userrec *u, char* text, ...)
792 char textbuffer[MAXBUF];
793 char oper_quit[MAXBUF];
794 bool quit_munge = false;
800 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
804 if (u->registered != 7)
806 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
810 va_start(argsPtr, text);
811 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
814 if ((Config->HideSplits) && (total > 6))
816 /* Yeah yeah, this is ugly. But its fast, live with it. */
817 char* check = textbuffer;
819 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
821 std::stringstream split(check);
822 std::string server_one;
823 std::string server_two;
828 if ((FindServerName(server_one)) && (FindServerName(server_two)))
830 strlcpy(oper_quit,textbuffer,MAXQUIT);
831 strlcpy(check,"*.net *.split",MAXQUIT);
837 if ((Config->HideBans) && (total > 13) && (!quit_munge))
839 char* check = textbuffer;
842 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
846 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
848 strlcpy(oper_quit,textbuffer,MAXQUIT);
849 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
855 memset(&already_sent,0,MAX_DESCRIPTORS);
857 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
859 if (((ucrec*)(*v))->channel)
861 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
863 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
867 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
869 already_sent[i->second->fd] = 1;
873 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
876 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
884 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
888 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
892 if (u->registered != 7)
894 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
898 memset(&already_sent,0,MAX_DESCRIPTORS);
900 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
902 if (((ucrec*)(*v))->channel)
904 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
906 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
910 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
912 already_sent[i->second->fd] = 1;
913 WriteFrom_NoFormat(i->second->fd,u,text);
922 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
923 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
924 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
925 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
926 * uses the oper list, which means if you have 2000 users but only 5 opers,
927 * it iterates 5 times.
929 void WriteOpers(const char* text, ...)
931 char textbuffer[MAXBUF];
936 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
940 va_start(argsPtr, text);
941 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
944 WriteOpers_NoFormat(textbuffer);
947 void WriteOpers_NoFormat(const char* text)
951 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
955 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
961 if (a->modebits & UM_SERVERNOTICE)
963 // send server notices to all with +s
964 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
970 void ServerNoticeAll(char* text, ...)
975 char textbuffer[MAXBUF];
976 char formatbuffer[MAXBUF];
978 va_start (argsPtr, text);
979 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
982 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
984 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
986 userrec* t = (userrec*)(*i);
987 WriteServ_NoFormat(t->fd,formatbuffer);
991 void ServerPrivmsgAll(char* text, ...)
996 char textbuffer[MAXBUF];
997 char formatbuffer[MAXBUF];
999 va_start (argsPtr, text);
1000 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1003 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1005 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1007 userrec* t = (userrec*)(*i);
1008 WriteServ_NoFormat(t->fd,formatbuffer);
1012 void WriteMode(const char* modes, int flags, const char* text, ...)
1014 char textbuffer[MAXBUF];
1018 if ((!text) || (!modes) || (!flags))
1020 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1024 va_start(argsPtr, text);
1025 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1027 modelen = strlen(modes);
1029 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1031 userrec* t = (userrec*)(*i);
1032 bool send_to_user = false;
1034 if (flags == WM_AND)
1036 send_to_user = true;
1038 for (int n = 0; n < modelen; n++)
1040 if (!hasumode(t,modes[n]))
1042 send_to_user = false;
1047 else if (flags == WM_OR)
1049 send_to_user = false;
1051 for (int n = 0; n < modelen; n++)
1053 if (hasumode(t,modes[n]))
1055 send_to_user = true;
1063 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1068 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1070 char textbuffer[MAXBUF];
1071 char formatbuffer[MAXBUF];
1074 if ((!text) || (!source))
1076 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1080 va_start(argsPtr, text);
1081 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1084 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1086 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1088 userrec* t = (userrec*)(*i);
1089 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1094 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1096 char textbuffer[MAXBUF];
1097 char formatbuffer[MAXBUF];
1100 if ((!text) || (!source))
1102 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1106 va_start(argsPtr, text);
1107 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1110 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1112 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1114 userrec* t = (userrec*)(*i);
1116 if ((IS_LOCAL(t)) && (t->modebits & UM_WALLOPS))
1118 WriteTo_NoFormat(source,t,formatbuffer);
1123 /* convert a string to lowercase. Note following special circumstances
1124 * taken from RFC 1459. Many "official" server branches still hold to this
1125 * rule so i will too;
1127 * Because of IRC's scandanavian origin, the characters {}| are
1128 * considered to be the lower case equivalents of the characters []\,
1129 * respectively. This is a critical issue when determining the
1130 * equivalence of two nicknames.
1132 void strlower(char *n)
1136 for (char* t = n; *t; t++)
1137 *t = lowermap[(unsigned char)*t];
1141 /* Find a user record by nickname and return a pointer to it */
1143 userrec* Find(const std::string &nick)
1145 user_hash::iterator iter = clientlist.find(nick);
1147 if (iter == clientlist.end())
1148 /* Couldn't find it */
1151 return iter->second;
1154 userrec* Find(const char* nick)
1156 user_hash::iterator iter;
1161 iter = clientlist.find(nick);
1163 if (iter == clientlist.end())
1166 return iter->second;
1169 /* find a channel record by channel name and return a pointer to it */
1171 chanrec* FindChan(const char* chan)
1173 chan_hash::iterator iter;
1177 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1181 iter = chanlist.find(chan);
1183 if (iter == chanlist.end())
1184 /* Couldn't find it */
1187 return iter->second;
1191 long GetMaxBans(char* name)
1194 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1197 if (match(name,x.c_str()))
1205 void purge_empty_chans(userrec* u)
1207 std::vector<chanrec*> to_delete;
1209 // firstly decrement the count on each channel
1210 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1212 if (((ucrec*)(*f))->channel)
1214 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1216 /* No users left in here, mark it for deletion */
1217 to_delete.push_back(((ucrec*)(*f))->channel);
1218 ((ucrec*)(*f))->channel = NULL;
1223 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1225 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1227 chanrec* thischan = (chanrec*)*n;
1228 chan_hash::iterator i2 = chanlist.find(thischan->name);
1229 if (i2 != chanlist.end())
1231 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1242 char* chanmodes(chanrec *chan, bool showkey)
1244 static char scratch[MAXBUF];
1245 static char sparam[MAXBUF];
1246 char* offset = scratch;
1247 std::string extparam = "";
1251 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1259 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1260 for(int n = 0; n < 64; n++)
1269 extparam = (showkey ? chan->key : "<key>");
1272 extparam = ConvToStr(chan->limit);
1280 /* We know these have no parameters */
1283 extparam = chan->GetModeParameter(n+65);
1288 charlcat(sparam,' ',MAXBUF);
1289 strlcat(sparam,extparam.c_str(),MAXBUF);
1294 /* Null terminate scratch */
1296 strlcat(scratch,sparam,MAXMODES);
1301 /* compile a userlist of a channel into a string, each nick seperated by
1302 * spaces and op, voice etc status shown as @ and + */
1304 void userlist(userrec *user,chanrec *c)
1306 if ((!c) || (!user))
1308 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1313 size_t dlen, curlen;
1315 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1318 char* ptr = list + dlen;
1320 CUList *ulist= c->GetUsers();
1322 /* Improvement by Brain - this doesnt change in value, so why was it inside
1325 bool has_user = c->HasUser(user);
1327 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1329 if ((!has_user) && (i->second->modebits & UM_INVISIBLE))
1332 * user is +i, and source not on the channel, does not show
1333 * nick in NAMES list
1338 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1345 if (curlen > (480-NICKMAX))
1347 /* list overflowed into multiple numerics */
1348 WriteServ_NoFormat(user->fd,list);
1350 /* reset our lengths */
1351 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1359 /* if whats left in the list isnt empty, send it */
1362 WriteServ_NoFormat(user->fd,list);
1367 * return a count of the users on a specific channel accounting for
1368 * invisible users who won't increase the count. e.g. for /LIST
1370 int usercount_i(chanrec *c)
1377 CUList *ulist= c->GetUsers();
1378 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1380 if (!(i->second->modebits & UM_INVISIBLE))
1387 int usercount(chanrec *c)
1389 return (c ? c->GetUserCounter() : 0);
1393 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1394 ConnectClass GetClass(userrec *user)
1396 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1398 if (match(user->host,i->host.c_str()))
1404 return *(Config->Classes.begin());
1408 * sends out an error notice to all connected clients (not to be used
1411 void send_error(char *s)
1413 log(DEBUG,"send_error: %s",s);
1415 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1417 userrec* t = (userrec*)(*i);
1418 if (t->registered == 7)
1420 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1424 // fix - unregistered connections receive ERROR, not NOTICE
1425 Write(t->fd,"ERROR :%s",s);
1430 void Error(int status)
1436 signal(SIGALRM, SIG_IGN);
1437 signal(SIGPIPE, SIG_IGN);
1438 signal(SIGTERM, SIG_IGN);
1439 signal(SIGABRT, SIG_IGN);
1440 signal(SIGSEGV, SIG_IGN);
1441 signal(SIGURG, SIG_IGN);
1442 signal(SIGKILL, SIG_IGN);
1443 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1445 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
1446 size = backtrace(array, 30);
1447 strings = backtrace_symbols(array, size);
1448 for (size_t i = 0; i < size; i++) {
1449 log(DEFAULT,"[%d] %s", i, strings[i]);
1452 WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/");
1454 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
1456 send_error("Somebody screwed up... Whoops. IRC Server terminating.");
1457 signal(SIGSEGV, SIG_DFL);
1458 if (raise(SIGSEGV) == -1)
1460 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
1465 // this function counts all users connected, wether they are registered or NOT.
1468 return clientlist.size();
1471 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1472 int registered_usercount(void)
1476 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1478 if (i->second->registered == 7) c++;
1484 int usercount_invisible(void)
1488 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1490 if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE))
1497 int usercount_opers(void)
1501 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1503 if (*(i->second->oper))
1509 int usercount_unknown(void)
1513 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1515 userrec* t = (userrec*)(*i);
1516 if (t->registered != 7)
1523 long chancount(void)
1525 return chanlist.size();
1532 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1534 userrec* t = (userrec*)(*i);
1535 if (t->registered == 7)
1542 void ShowMOTD(userrec *user)
1544 static char mbuf[MAXBUF];
1545 static char crud[MAXBUF];
1546 std::string WholeMOTD = "";
1548 if (!Config->MOTD.size())
1550 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1554 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1555 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1556 WholeMOTD = WholeMOTD + mbuf;
1558 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1559 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1561 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1562 WholeMOTD = WholeMOTD + mbuf;
1564 // only one write operation
1565 if (Config->GetIOHook(user->port))
1569 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1571 catch (ModuleException& modexcept)
1573 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1578 user->AddWriteBuf(WholeMOTD);
1581 ServerInstance->stats->statsSent += WholeMOTD.length();
1584 void ShowRULES(userrec *user)
1586 if (!Config->RULES.size())
1588 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1591 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1593 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1594 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1596 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1599 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1600 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1601 // registration timeout maximum seconds)
1602 bool AllModulesReportReady(userrec* user)
1604 if (!Config->global_implementation[I_OnCheckReady])
1607 for (int i = 0; i <= MODCOUNT; i++)
1609 if (Config->implement_lists[i][I_OnCheckReady])
1611 int res = modules[i]->OnCheckReady(user);
1620 /* Make Sure Modules Are Avaliable!
1621 * (BugFix By Craig.. See? I do work! :p)
1622 * Modified by brain, requires const char*
1623 * to work with other API functions
1627 bool FileExists (const char* file)
1630 if ((input = fopen (file, "r")) == NULL)
1641 char* CleanFilename(char* name)
1643 char* p = name + strlen(name);
1644 while ((p != name) && (*p != '/')) p--;
1645 return (p != name ? ++p : p);
1648 bool DirValid(char* dirandfile)
1651 char buffer[MAXBUF];
1652 char otherdir[MAXBUF];
1655 strlcpy(work, dirandfile, MAXBUF);
1658 // we just want the dir
1670 // Get the current working directory
1671 if (getcwd(buffer, MAXBUF ) == NULL )
1676 if (getcwd(otherdir, MAXBUF ) == NULL )
1681 size_t t = strlen(work);
1683 if (strlen(otherdir) >= t)
1687 if (!strcmp(otherdir,work))
1700 std::string GetFullProgDir(char** argv, int argc)
1703 char buffer[MAXBUF];
1704 char otherdir[MAXBUF];
1707 strlcpy(work,argv[0],MAXBUF);
1710 // we just want the dir
1722 // Get the current working directory
1723 if (getcwd(buffer, MAXBUF) == NULL)
1728 if (getcwd(otherdir, MAXBUF) == NULL)
1735 int InsertMode(std::string &output, const char* mode, unsigned short section)
1737 unsigned short currsection = 1;
1738 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1740 if(section > 4 || section == 0)
1742 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1746 for(; pos < output.size(); pos++)
1748 if(section == currsection)
1751 if(output[pos] == ',')
1755 output.insert(pos, mode);
1759 bool IsValidChannelName(const char *chname)
1763 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1764 if (!chname || *chname != '#')
1769 c = (char *)chname + 1;
1783 /* too long a name - note funky pointer arithmetic here. */
1784 if ((c - chname) > CHANMAX)
1792 inline int charlcat(char* x,char y,int z)
1809 bool charremove(char* mp, char remove)
1812 bool shift_down = false;
1816 if (*mptr == remove)
1828 void OpenLog(char** argv, int argc)
1832 if (Config->logpath == "")
1834 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1839 Config->log_file = fopen(LOG_FILE,"a+");
1841 if (!Config->log_file)
1843 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1850 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1852 if (!Config->log_file)
1854 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1863 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1864 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1871 if (*Config->DieValue)
1873 printf("WARNING: %s\n\n",Config->DieValue);
1874 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1879 /* We must load the modules AFTER initializing the socket engine, now */
1880 void LoadAllModules(InspIRCd* ServerInstance)
1882 char configToken[MAXBUF];
1883 Config->module_names.clear();
1886 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1888 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1889 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1891 if (!ServerInstance->LoadModule(configToken))
1893 log(DEFAULT,"Exiting due to a module loader error.");
1894 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1899 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);