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>
28 #include "connection.h"
39 #include "inspstring.h"
40 #include "helperfuncs.h"
45 extern ModuleList modules;
46 extern ServerConfig *Config;
47 extern InspIRCd* ServerInstance;
49 extern char lowermap[255];
50 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
51 static char already_sent[MAX_DESCRIPTORS];
52 extern std::vector<userrec*> all_opers;
53 extern user_hash clientlist;
54 extern chan_hash chanlist;
56 char LOG_FILE[MAXBUF];
58 extern std::vector<userrec*> local_users;
60 static char TIMESTR[26];
61 static time_t LAST = 0;
64 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
65 * is greater than the configured loglevel.
67 void log(int level, char *text, ...)
70 char textbuffer[MAXBUF];
72 /* If we were given -debug we output all messages, regardless of configured loglevel */
73 if ((level < Config->LogLevel) && !Config->forcedebug)
78 struct tm *timeinfo = localtime(&TIME);
80 strlcpy(TIMESTR,asctime(timeinfo),26);
87 va_start(argsPtr, text);
88 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
92 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
97 printf("%s %s\n", TIMESTR, textbuffer);
102 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
104 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
106 void readfile(file_cache &F, const char* fname)
109 char linebuf[MAXBUF];
111 log(DEBUG,"readfile: loading %s",fname);
113 file = fopen(fname,"r");
119 fgets(linebuf,sizeof(linebuf),file);
120 linebuf[strlen(linebuf)-1]='\0';
129 F.push_back(linebuf);
137 log(DEBUG,"readfile: failed to load file: %s",fname);
140 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
144 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
145 * is a valid entry in the local FD table.
147 void Write_NoFormat(int sock, const char *text)
152 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
155 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
158 if (fd_ref_table[sock])
160 if (Config->GetIOHook(fd_ref_table[sock]->port))
164 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
166 catch (ModuleException& modexcept)
168 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
173 fd_ref_table[sock]->AddWriteBuf(tb);
175 ServerInstance->stats->statsSent += bytes;
178 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
182 * Same as Write_NoFormat(), but formatted printf() style first.
184 void Write(int sock, char *text, ...)
187 char textbuffer[MAXBUF];
191 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
196 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
200 va_start(argsPtr, text);
201 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
203 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
206 if (fd_ref_table[sock])
208 if (Config->GetIOHook(fd_ref_table[sock]->port))
212 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
214 catch (ModuleException& modexcept)
216 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
221 fd_ref_table[sock]->AddWriteBuf(tb);
223 ServerInstance->stats->statsSent += bytes;
226 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
229 /** WriteServ_NoFormat()
230 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
232 void WriteServ_NoFormat(int sock, const char* text)
237 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
240 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
243 if (fd_ref_table[sock])
245 if (Config->GetIOHook(fd_ref_table[sock]->port))
249 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
251 catch (ModuleException& modexcept)
253 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
258 fd_ref_table[sock]->AddWriteBuf(tb);
260 ServerInstance->stats->statsSent += bytes;
263 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
267 * Same as Write(), except `text' is prefixed with `:server.name '.
269 void WriteServ(int sock, char* text, ...)
272 char textbuffer[MAXBUF];
274 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
279 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
283 va_start(argsPtr, text);
284 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
287 WriteServ_NoFormat(sock, textbuffer);
290 /** WriteFrom_NoFormat()
291 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
292 * the nick, user, and host of `user'.
294 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
299 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
302 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
305 if (fd_ref_table[sock])
307 if (Config->GetIOHook(fd_ref_table[sock]->port))
311 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
313 catch (ModuleException& modexcept)
315 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
320 fd_ref_table[sock]->AddWriteBuf(tb);
322 ServerInstance->stats->statsSent += bytes;
325 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
328 /* write text from an originating user to originating user */
330 void WriteFrom(int sock, userrec *user,char* text, ...)
333 char textbuffer[MAXBUF];
337 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
340 if ((!text) || (!user))
342 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
346 va_start(argsPtr, text);
347 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
349 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
352 if (fd_ref_table[sock])
354 if (Config->GetIOHook(fd_ref_table[sock]->port))
358 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
360 catch (ModuleException& modexcept)
362 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
367 fd_ref_table[sock]->AddWriteBuf(tb);
370 ServerInstance->stats->statsSent += bytes;
373 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
376 /* write text to an destination user from a source user (e.g. user privmsg) */
378 void WriteTo(userrec *source, userrec *dest,char *data, ...)
380 char textbuffer[MAXBUF];
383 if ((!dest) || (!data))
385 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
392 va_start(argsPtr, data);
393 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
397 // if no source given send it from the server.
400 WriteServ_NoFormat(dest->fd,textbuffer);
404 WriteFrom_NoFormat(dest->fd,source,textbuffer);
408 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
410 if ((!dest) || (!data))
415 WriteServ_NoFormat(dest->fd,data);
419 WriteFrom_NoFormat(dest->fd,source,data);
423 /* write formatted text from a source user to all users on a channel
424 * including the sender (NOT for privmsg, notice etc!) */
426 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
428 char textbuffer[MAXBUF];
432 if ((!Ptr) || (!user) || (!text))
434 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
438 va_start(argsPtr, text);
439 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
442 ulist = Ptr->GetUsers();
444 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
446 if (i->second->fd != FD_MAGIC_NUMBER)
447 WriteTo_NoFormat(user,i->second,textbuffer);
451 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
455 if ((!Ptr) || (!user) || (!text))
457 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
461 ulist = Ptr->GetUsers();
463 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
465 if (i->second->fd != FD_MAGIC_NUMBER)
466 WriteTo_NoFormat(user,i->second,text);
471 /* write formatted text from a source user to all users on a channel
472 * including the sender (NOT for privmsg, notice etc!) doesnt send to
473 * users on remote servers */
475 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
477 char textbuffer[MAXBUF];
481 if ((!Ptr) || (!text))
483 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
487 va_start(argsPtr, text);
488 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
491 ulist = Ptr->GetUsers();
493 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
495 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
499 WriteServ_NoFormat(i->second->fd,textbuffer);
503 WriteTo_NoFormat(user,i->second,textbuffer);
509 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
513 if ((!Ptr) || (!text))
515 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
519 ulist = Ptr->GetUsers();
521 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
523 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
527 WriteServ_NoFormat(i->second->fd,text);
531 WriteTo_NoFormat(user,i->second,text);
539 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
541 char textbuffer[MAXBUF];
545 if ((!Ptr) || (!text))
547 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
551 va_start(argsPtr, text);
552 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
555 ulist = Ptr->GetUsers();
557 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
559 if (IS_LOCAL(i->second))
560 WriteServ_NoFormat(i->second->fd,textbuffer);
564 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
568 if ((!Ptr) || (!text))
570 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
574 ulist = Ptr->GetUsers();
576 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
578 if (IS_LOCAL(i->second))
579 WriteServ_NoFormat(i->second->fd,text);
585 /* write formatted text from a source user to all users on a channel except
586 * for the sender (for privmsg etc) */
588 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
590 char textbuffer[MAXBUF];
594 if ((!Ptr) || (!user) || (!text))
596 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
600 va_start(argsPtr, text);
601 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
607 ulist = Ptr->GetOppedUsers();
610 ulist = Ptr->GetHalfoppedUsers();
613 ulist = Ptr->GetVoicedUsers();
616 ulist = Ptr->GetUsers();
620 log(DEBUG,"%d users to write to",ulist->size());
622 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
624 if ((IS_LOCAL(i->second)) && (user != i->second))
625 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
629 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
633 if ((!Ptr) || (!user) || (!text))
635 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
642 ulist = Ptr->GetOppedUsers();
645 ulist = Ptr->GetHalfoppedUsers();
648 ulist = Ptr->GetVoicedUsers();
651 ulist = Ptr->GetUsers();
655 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
657 if ((IS_LOCAL(i->second)) && (user != i->second))
658 WriteFrom_NoFormat(i->second->fd,user,text);
662 std::string GetServerDescription(char* servername)
664 std::string description = "";
666 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
668 if (description != "")
674 // not a remote server that can be found, it must be me.
675 return Config->ServerDesc;
679 /* write a formatted string to all users who share at least one common
680 * channel, including the source user e.g. for use in NICK */
682 void WriteCommon(userrec *u, char* text, ...)
684 char textbuffer[MAXBUF];
686 bool sent_to_at_least_one = false;
690 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
694 if (u->registered != 7)
696 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
700 va_start(argsPtr, text);
701 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
704 // FIX: Stops a message going to the same person more than once
705 memset(&already_sent,0,MAX_DESCRIPTORS);
707 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
709 if (((ucrec*)(*v))->channel)
711 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
713 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
715 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
717 already_sent[i->second->fd] = 1;
718 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
719 sent_to_at_least_one = true;
726 * if the user was not in any channels, no users will receive the text. Make sure the user
727 * receives their OWN message for WriteCommon
729 if (!sent_to_at_least_one)
731 WriteFrom_NoFormat(u->fd,u,textbuffer);
735 void WriteCommon_NoFormat(userrec *u, const char* text)
737 bool sent_to_at_least_one = false;
741 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
745 if (u->registered != 7)
747 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
751 // FIX: Stops a message going to the same person more than once
752 memset(&already_sent,0,MAX_DESCRIPTORS);
754 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
756 if (((ucrec*)(*v))->channel)
758 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
760 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
762 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
764 already_sent[i->second->fd] = 1;
765 WriteFrom_NoFormat(i->second->fd,u,text);
766 sent_to_at_least_one = true;
773 * if the user was not in any channels, no users will receive the text. Make sure the user
774 * receives their OWN message for WriteCommon
776 if (!sent_to_at_least_one)
778 WriteFrom_NoFormat(u->fd,u,text);
783 /* write a formatted string to all users who share at least one common
784 * channel, NOT including the source user e.g. for use in QUIT
787 void WriteCommonExcept(userrec *u, char* text, ...)
789 char textbuffer[MAXBUF];
790 char oper_quit[MAXBUF];
791 bool quit_munge = false;
797 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
801 if (u->registered != 7)
803 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
807 va_start(argsPtr, text);
808 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
811 if ((Config->HideSplits) && (total > 6))
813 /* Yeah yeah, this is ugly. But its fast, live with it. */
814 char* check = textbuffer;
816 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
818 std::stringstream split(check);
819 std::string server_one;
820 std::string server_two;
825 if ((FindServerName(server_one)) && (FindServerName(server_two)))
827 strlcpy(oper_quit,textbuffer,MAXQUIT);
828 strlcpy(check,"*.net *.split",MAXQUIT);
834 if ((Config->HideBans) && (total > 13) && (!quit_munge))
836 char* check = textbuffer;
839 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
843 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
845 strlcpy(oper_quit,textbuffer,MAXQUIT);
846 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
852 memset(&already_sent,0,MAX_DESCRIPTORS);
854 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
856 if (((ucrec*)(*v))->channel)
858 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
860 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
864 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
866 already_sent[i->second->fd] = 1;
870 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
873 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
881 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
885 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
889 if (u->registered != 7)
891 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
895 memset(&already_sent,0,MAX_DESCRIPTORS);
897 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
899 if (((ucrec*)(*v))->channel)
901 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
903 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
907 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
909 already_sent[i->second->fd] = 1;
910 WriteFrom_NoFormat(i->second->fd,u,text);
919 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
920 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
921 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
922 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
923 * uses the oper list, which means if you have 2000 users but only 5 opers,
924 * it iterates 5 times.
926 void WriteOpers(const char* text, ...)
928 char textbuffer[MAXBUF];
933 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
937 va_start(argsPtr, text);
938 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
941 WriteOpers_NoFormat(textbuffer);
944 void WriteOpers_NoFormat(const char* text)
948 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
952 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
958 if (a->modebits & UM_SERVERNOTICE)
960 // send server notices to all with +s
961 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
967 void ServerNoticeAll(char* text, ...)
972 char textbuffer[MAXBUF];
973 char formatbuffer[MAXBUF];
975 va_start (argsPtr, text);
976 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
979 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
981 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
983 userrec* t = (userrec*)(*i);
984 WriteServ_NoFormat(t->fd,formatbuffer);
988 void ServerPrivmsgAll(char* text, ...)
993 char textbuffer[MAXBUF];
994 char formatbuffer[MAXBUF];
996 va_start (argsPtr, text);
997 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1000 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1002 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1004 userrec* t = (userrec*)(*i);
1005 WriteServ_NoFormat(t->fd,formatbuffer);
1009 void WriteMode(const char* modes, int flags, const char* text, ...)
1011 char textbuffer[MAXBUF];
1015 if ((!text) || (!modes) || (!flags))
1017 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1021 va_start(argsPtr, text);
1022 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1024 modelen = strlen(modes);
1026 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1028 userrec* t = (userrec*)(*i);
1029 bool send_to_user = false;
1031 if (flags == WM_AND)
1033 send_to_user = true;
1035 for (int n = 0; n < modelen; n++)
1037 if (!hasumode(t,modes[n]))
1039 send_to_user = false;
1044 else if (flags == WM_OR)
1046 send_to_user = false;
1048 for (int n = 0; n < modelen; n++)
1050 if (hasumode(t,modes[n]))
1052 send_to_user = true;
1060 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1065 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1067 char textbuffer[MAXBUF];
1068 char formatbuffer[MAXBUF];
1071 if ((!text) || (!source))
1073 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1077 va_start(argsPtr, text);
1078 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1081 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1083 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1085 userrec* t = (userrec*)(*i);
1086 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1091 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1093 char textbuffer[MAXBUF];
1094 char formatbuffer[MAXBUF];
1097 if ((!text) || (!source))
1099 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1103 va_start(argsPtr, text);
1104 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1107 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1109 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1111 userrec* t = (userrec*)(*i);
1113 if ((IS_LOCAL(t)) && (t->modebits & UM_WALLOPS))
1115 WriteTo_NoFormat(source,t,formatbuffer);
1120 /* convert a string to lowercase. Note following special circumstances
1121 * taken from RFC 1459. Many "official" server branches still hold to this
1122 * rule so i will too;
1124 * Because of IRC's scandanavian origin, the characters {}| are
1125 * considered to be the lower case equivalents of the characters []\,
1126 * respectively. This is a critical issue when determining the
1127 * equivalence of two nicknames.
1129 void strlower(char *n)
1133 for (char* t = n; *t; t++)
1134 *t = lowermap[(unsigned char)*t];
1138 /* Find a user record by nickname and return a pointer to it */
1140 userrec* Find(const std::string &nick)
1142 user_hash::iterator iter = clientlist.find(nick);
1144 if (iter == clientlist.end())
1145 /* Couldn't find it */
1148 return iter->second;
1151 userrec* Find(const char* nick)
1153 user_hash::iterator iter;
1158 iter = clientlist.find(nick);
1160 if (iter == clientlist.end())
1163 return iter->second;
1166 /* find a channel record by channel name and return a pointer to it */
1168 chanrec* FindChan(const char* chan)
1170 chan_hash::iterator iter;
1174 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1178 iter = chanlist.find(chan);
1180 if (iter == chanlist.end())
1181 /* Couldn't find it */
1184 return iter->second;
1188 long GetMaxBans(char* name)
1191 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1194 if (match(name,x.c_str()))
1202 void purge_empty_chans(userrec* u)
1204 std::vector<chanrec*> to_delete;
1206 // firstly decrement the count on each channel
1207 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1209 if (((ucrec*)(*f))->channel)
1211 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1213 /* No users left in here, mark it for deletion */
1214 to_delete.push_back(((ucrec*)(*f))->channel);
1215 ((ucrec*)(*f))->channel = NULL;
1220 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1222 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1224 chanrec* thischan = (chanrec*)*n;
1225 chan_hash::iterator i2 = chanlist.find(thischan->name);
1226 if (i2 != chanlist.end())
1228 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1239 char* chanmodes(chanrec *chan, bool showkey)
1241 static char scratch[MAXBUF];
1242 static char sparam[MAXBUF];
1243 char* offset = scratch;
1244 std::string extparam = "";
1248 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1256 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1257 for(int n = 0; n < 64; n++)
1266 extparam = (showkey ? chan->key : "<key>");
1269 extparam = ConvToStr(chan->limit);
1277 /* We know these have no parameters */
1280 extparam = chan->GetModeParameter(n+65);
1285 charlcat(sparam,' ',MAXBUF);
1286 strlcat(sparam,extparam.c_str(),MAXBUF);
1291 /* Null terminate scratch */
1293 strlcat(scratch,sparam,MAXMODES);
1298 /* compile a userlist of a channel into a string, each nick seperated by
1299 * spaces and op, voice etc status shown as @ and + */
1301 void userlist(userrec *user,chanrec *c)
1303 if ((!c) || (!user))
1305 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1310 size_t dlen, curlen;
1312 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1315 char* ptr = list + dlen;
1317 CUList *ulist= c->GetUsers();
1319 /* Improvement by Brain - this doesnt change in value, so why was it inside
1322 bool has_user = c->HasUser(user);
1324 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1326 if ((!has_user) && (i->second->modebits & UM_INVISIBLE))
1329 * user is +i, and source not on the channel, does not show
1330 * nick in NAMES list
1335 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1342 if (curlen > (480-NICKMAX))
1344 /* list overflowed into multiple numerics */
1345 WriteServ_NoFormat(user->fd,list);
1347 /* reset our lengths */
1348 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1356 /* if whats left in the list isnt empty, send it */
1359 WriteServ_NoFormat(user->fd,list);
1364 * return a count of the users on a specific channel accounting for
1365 * invisible users who won't increase the count. e.g. for /LIST
1367 int usercount_i(chanrec *c)
1374 CUList *ulist= c->GetUsers();
1375 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1377 if (!(i->second->modebits & UM_INVISIBLE))
1384 int usercount(chanrec *c)
1386 return (c ? c->GetUserCounter() : 0);
1390 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1391 ConnectClass GetClass(userrec *user)
1393 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1395 if (match(user->host,i->host.c_str()))
1401 return *(Config->Classes.begin());
1405 * sends out an error notice to all connected clients (not to be used
1408 void send_error(char *s)
1410 log(DEBUG,"send_error: %s",s);
1412 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1414 userrec* t = (userrec*)(*i);
1415 if (t->registered == 7)
1417 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1421 // fix - unregistered connections receive ERROR, not NOTICE
1422 Write(t->fd,"ERROR :%s",s);
1427 void Error(int status)
1429 signal(SIGALRM, SIG_IGN);
1430 signal(SIGPIPE, SIG_IGN);
1431 signal(SIGTERM, SIG_IGN);
1432 signal(SIGABRT, SIG_IGN);
1433 signal(SIGSEGV, SIG_IGN);
1434 signal(SIGURG, SIG_IGN);
1435 signal(SIGKILL, SIG_IGN);
1436 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1437 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1441 // this function counts all users connected, wether they are registered or NOT.
1444 return clientlist.size();
1447 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1448 int registered_usercount(void)
1452 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1454 if (i->second->registered == 7) c++;
1460 int usercount_invisible(void)
1464 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1466 if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE))
1473 int usercount_opers(void)
1477 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1479 if (*(i->second->oper))
1485 int usercount_unknown(void)
1489 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1491 userrec* t = (userrec*)(*i);
1492 if (t->registered != 7)
1499 long chancount(void)
1501 return chanlist.size();
1508 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1510 userrec* t = (userrec*)(*i);
1511 if (t->registered == 7)
1518 void ShowMOTD(userrec *user)
1520 static char mbuf[MAXBUF];
1521 static char crud[MAXBUF];
1522 std::string WholeMOTD = "";
1524 if (!Config->MOTD.size())
1526 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1530 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1531 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1532 WholeMOTD = WholeMOTD + mbuf;
1534 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1535 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1537 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1538 WholeMOTD = WholeMOTD + mbuf;
1540 // only one write operation
1541 if (Config->GetIOHook(user->port))
1545 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1547 catch (ModuleException& modexcept)
1549 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1554 user->AddWriteBuf(WholeMOTD);
1557 ServerInstance->stats->statsSent += WholeMOTD.length();
1560 void ShowRULES(userrec *user)
1562 if (!Config->RULES.size())
1564 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1567 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1569 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1570 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1572 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1575 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1576 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1577 // registration timeout maximum seconds)
1578 bool AllModulesReportReady(userrec* user)
1580 if (!Config->global_implementation[I_OnCheckReady])
1583 for (int i = 0; i <= MODCOUNT; i++)
1585 if (Config->implement_lists[i][I_OnCheckReady])
1587 int res = modules[i]->OnCheckReady(user);
1596 /* Make Sure Modules Are Avaliable!
1597 * (BugFix By Craig.. See? I do work! :p)
1598 * Modified by brain, requires const char*
1599 * to work with other API functions
1603 bool FileExists (const char* file)
1606 if ((input = fopen (file, "r")) == NULL)
1617 char* CleanFilename(char* name)
1619 char* p = name + strlen(name);
1620 while ((p != name) && (*p != '/')) p--;
1621 return (p != name ? ++p : p);
1624 bool DirValid(char* dirandfile)
1627 char buffer[MAXBUF];
1628 char otherdir[MAXBUF];
1631 strlcpy(work, dirandfile, MAXBUF);
1634 // we just want the dir
1646 // Get the current working directory
1647 if (getcwd(buffer, MAXBUF ) == NULL )
1652 if (getcwd(otherdir, MAXBUF ) == NULL )
1657 size_t t = strlen(work);
1659 if (strlen(otherdir) >= t)
1663 if (!strcmp(otherdir,work))
1676 std::string GetFullProgDir(char** argv, int argc)
1679 char buffer[MAXBUF];
1680 char otherdir[MAXBUF];
1683 strlcpy(work,argv[0],MAXBUF);
1686 // we just want the dir
1698 // Get the current working directory
1699 if (getcwd(buffer, MAXBUF) == NULL)
1704 if (getcwd(otherdir, MAXBUF) == NULL)
1711 int InsertMode(std::string &output, const char* mode, unsigned short section)
1713 unsigned short currsection = 1;
1714 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1716 if(section > 4 || section == 0)
1718 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1722 for(; pos < output.size(); pos++)
1724 if(section == currsection)
1727 if(output[pos] == ',')
1731 output.insert(pos, mode);
1735 bool IsValidChannelName(const char *chname)
1739 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1740 if (!chname || *chname != '#')
1745 c = (char *)chname + 1;
1759 /* too long a name - note funky pointer arithmetic here. */
1760 if ((c - chname) > CHANMAX)
1768 inline int charlcat(char* x,char y,int z)
1785 bool charremove(char* mp, char remove)
1788 bool shift_down = false;
1792 if (*mptr == remove)
1804 void OpenLog(char** argv, int argc)
1808 if (Config->logpath == "")
1810 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1815 Config->log_file = fopen(LOG_FILE,"a+");
1817 if (!Config->log_file)
1819 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1826 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1828 if (!Config->log_file)
1830 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1839 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1840 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1847 if (*Config->DieValue)
1849 printf("WARNING: %s\n\n",Config->DieValue);
1850 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1855 /* We must load the modules AFTER initializing the socket engine, now */
1856 void LoadAllModules(InspIRCd* ServerInstance)
1858 char configToken[MAXBUF];
1859 Config->module_names.clear();
1862 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1864 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1865 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1867 if (!ServerInstance->LoadModule(configToken))
1869 log(DEFAULT,"Exiting due to a module loader error.");
1870 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1875 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);