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 * ---------------------------------------------------
19 #include "inspircd_config.h"
21 #include "inspircd_io.h"
24 #include <sys/errno.h>
28 #include <ext/hash_map>
36 #include "connection.h"
47 #include "inspstring.h"
48 #include "helperfuncs.h"
53 extern std::vector<Module*> modules;
54 extern ServerConfig *Config;
55 extern InspIRCd* ServerInstance;
57 extern char lowermap[255];
58 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
59 static char already_sent[MAX_DESCRIPTORS];
60 extern std::vector<userrec*> all_opers;
61 extern user_hash clientlist;
62 extern chan_hash chanlist;
64 char LOG_FILE[MAXBUF];
66 extern std::vector<userrec*> local_users;
68 static char TIMESTR[26];
69 static time_t LAST = 0;
72 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
73 * is greater than the configured loglevel.
75 void log(int level, char *text, ...)
78 char textbuffer[MAXBUF];
80 if (level < Config->LogLevel)
85 struct tm *timeinfo = localtime(&TIME);
87 strlcpy(TIMESTR,asctime(timeinfo),26);
94 va_start(argsPtr, text);
95 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
99 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
103 printf("%s %s\n", TIMESTR, textbuffer);
109 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
111 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
113 void readfile(file_cache &F, const char* fname)
116 char linebuf[MAXBUF];
118 log(DEBUG,"readfile: loading %s",fname);
120 file = fopen(fname,"r");
126 fgets(linebuf,sizeof(linebuf),file);
127 linebuf[strlen(linebuf)-1]='\0';
136 F.push_back(linebuf);
144 log(DEBUG,"readfile: failed to load file: %s",fname);
147 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
151 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
152 * is a valid entry in the local FD table.
154 void Write_NoFormat(int sock, const char *text)
159 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
162 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
165 if (fd_ref_table[sock])
167 if (Config->GetIOHook(fd_ref_table[sock]->port))
171 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
173 catch (ModuleException& modexcept)
175 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
180 fd_ref_table[sock]->AddWriteBuf(tb);
182 ServerInstance->stats->statsSent += bytes;
185 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
189 * Same as Write_NoFormat(), but formatted printf() style first.
191 void Write(int sock, char *text, ...)
194 char textbuffer[MAXBUF];
198 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
203 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
207 va_start(argsPtr, text);
208 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
210 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
213 if (fd_ref_table[sock])
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 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
250 if (fd_ref_table[sock])
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];
283 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
288 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
292 va_start(argsPtr, text);
293 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
295 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
298 if (fd_ref_table[sock])
300 if (Config->GetIOHook(fd_ref_table[sock]->port))
304 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
306 catch (ModuleException& modexcept)
308 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
313 fd_ref_table[sock]->AddWriteBuf(tb);
316 ServerInstance->stats->statsSent += bytes;
319 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
322 /** WriteFrom_NoFormat()
323 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
324 * the nick, user, and host of `user'.
326 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
331 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
334 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
337 if (fd_ref_table[sock])
339 if (Config->GetIOHook(fd_ref_table[sock]->port))
343 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
345 catch (ModuleException& modexcept)
347 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
352 fd_ref_table[sock]->AddWriteBuf(tb);
354 ServerInstance->stats->statsSent += bytes;
357 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
360 /* write text from an originating user to originating user */
362 void WriteFrom(int sock, userrec *user,char* text, ...)
365 char textbuffer[MAXBUF];
369 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
372 if ((!text) || (!user))
374 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
378 va_start(argsPtr, text);
379 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
381 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
384 if (fd_ref_table[sock])
386 if (Config->GetIOHook(fd_ref_table[sock]->port))
390 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
392 catch (ModuleException& modexcept)
394 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
399 fd_ref_table[sock]->AddWriteBuf(tb);
402 ServerInstance->stats->statsSent += bytes;
405 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
408 /* write text to an destination user from a source user (e.g. user privmsg) */
410 void WriteTo(userrec *source, userrec *dest,char *data, ...)
412 char textbuffer[MAXBUF];
415 if ((!dest) || (!data))
417 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
424 va_start(argsPtr, data);
425 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
429 // if no source given send it from the server.
432 WriteServ_NoFormat(dest->fd,textbuffer);
436 WriteFrom_NoFormat(dest->fd,source,textbuffer);
440 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
442 if ((!dest) || (!data))
447 WriteServ_NoFormat(dest->fd,data);
451 WriteFrom_NoFormat(dest->fd,source,data);
455 /* write formatted text from a source user to all users on a channel
456 * including the sender (NOT for privmsg, notice etc!) */
458 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
460 char textbuffer[MAXBUF];
464 if ((!Ptr) || (!user) || (!text))
466 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
470 va_start(argsPtr, text);
471 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
474 ulist = Ptr->GetUsers();
476 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
478 if (i->second->fd != FD_MAGIC_NUMBER)
479 WriteTo_NoFormat(user,i->second,textbuffer);
483 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
487 if ((!Ptr) || (!user) || (!text))
489 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
493 ulist = Ptr->GetUsers();
495 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
497 if (i->second->fd != FD_MAGIC_NUMBER)
498 WriteTo_NoFormat(user,i->second,text);
503 /* write formatted text from a source user to all users on a channel
504 * including the sender (NOT for privmsg, notice etc!) doesnt send to
505 * users on remote servers */
507 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
509 char textbuffer[MAXBUF];
513 if ((!Ptr) || (!text))
515 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
519 va_start(argsPtr, text);
520 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
523 ulist = Ptr->GetUsers();
525 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
527 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
531 WriteServ_NoFormat(i->second->fd,textbuffer);
535 WriteTo_NoFormat(user,i->second,textbuffer);
541 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
545 if ((!Ptr) || (!text))
547 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
551 ulist = Ptr->GetUsers();
553 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
555 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
559 WriteServ_NoFormat(i->second->fd,text);
563 WriteTo_NoFormat(user,i->second,text);
571 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
573 char textbuffer[MAXBUF];
577 if ((!Ptr) || (!text))
579 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
583 va_start(argsPtr, text);
584 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
587 ulist = Ptr->GetUsers();
589 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
591 if (IS_LOCAL(i->second))
592 WriteServ_NoFormat(i->second->fd,textbuffer);
596 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
600 if ((!Ptr) || (!text))
602 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
606 ulist = Ptr->GetUsers();
608 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
610 if (IS_LOCAL(i->second))
611 WriteServ_NoFormat(i->second->fd,text);
617 /* write formatted text from a source user to all users on a channel except
618 * for the sender (for privmsg etc) */
620 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
622 char textbuffer[MAXBUF];
626 if ((!Ptr) || (!user) || (!text))
628 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
632 va_start(argsPtr, text);
633 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
639 ulist = Ptr->GetOppedUsers();
642 ulist = Ptr->GetHalfoppedUsers();
645 ulist = Ptr->GetVoicedUsers();
648 ulist = Ptr->GetUsers();
652 log(DEBUG,"%d users to write to",ulist->size());
654 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
656 if ((IS_LOCAL(i->second)) && (user != i->second))
657 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
661 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
665 if ((!Ptr) || (!user) || (!text))
667 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
674 ulist = Ptr->GetOppedUsers();
677 ulist = Ptr->GetHalfoppedUsers();
680 ulist = Ptr->GetVoicedUsers();
683 ulist = Ptr->GetUsers();
687 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
689 if ((IS_LOCAL(i->second)) && (user != i->second))
690 WriteFrom_NoFormat(i->second->fd,user,text);
694 std::string GetServerDescription(char* servername)
696 std::string description = "";
698 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
700 if (description != "")
706 // not a remote server that can be found, it must be me.
707 return Config->ServerDesc;
711 /* write a formatted string to all users who share at least one common
712 * channel, including the source user e.g. for use in NICK */
714 void WriteCommon(userrec *u, char* text, ...)
716 char textbuffer[MAXBUF];
718 bool sent_to_at_least_one = false;
722 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
726 if (u->registered != 7)
728 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
732 va_start(argsPtr, text);
733 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
736 // FIX: Stops a message going to the same person more than once
737 memset(&already_sent,0,MAX_DESCRIPTORS);
739 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
741 if (((ucrec*)(*v))->channel)
743 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
745 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
747 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
749 already_sent[i->second->fd] = 1;
750 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
751 sent_to_at_least_one = true;
758 * if the user was not in any channels, no users will receive the text. Make sure the user
759 * receives their OWN message for WriteCommon
761 if (!sent_to_at_least_one)
763 WriteFrom_NoFormat(u->fd,u,textbuffer);
767 void WriteCommon_NoFormat(userrec *u, const char* text)
769 bool sent_to_at_least_one = false;
773 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
777 if (u->registered != 7)
779 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
783 // FIX: Stops a message going to the same person more than once
784 memset(&already_sent,0,MAX_DESCRIPTORS);
786 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
788 if (((ucrec*)(*v))->channel)
790 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
792 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
794 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
796 already_sent[i->second->fd] = 1;
797 WriteFrom_NoFormat(i->second->fd,u,text);
798 sent_to_at_least_one = true;
805 * if the user was not in any channels, no users will receive the text. Make sure the user
806 * receives their OWN message for WriteCommon
808 if (!sent_to_at_least_one)
810 WriteFrom_NoFormat(u->fd,u,text);
815 /* write a formatted string to all users who share at least one common
816 * channel, NOT including the source user e.g. for use in QUIT
819 void WriteCommonExcept(userrec *u, char* text, ...)
821 char textbuffer[MAXBUF];
822 char oper_quit[MAXBUF];
823 bool quit_munge = false;
829 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
833 if (u->registered != 7)
835 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
839 va_start(argsPtr, text);
840 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
843 if ((Config->HideSplits) && (total > 6))
845 /* Yeah yeah, this is ugly. But its fast, live with it. */
846 char* check = textbuffer;
848 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
850 std::stringstream split(check);
851 std::string server_one;
852 std::string server_two;
857 if ((FindServerName(server_one)) && (FindServerName(server_two)))
859 strlcpy(oper_quit,textbuffer,MAXQUIT);
860 strlcpy(check,"*.net *.split",MAXQUIT);
866 if ((Config->HideBans) && (total > 13) && (!quit_munge))
868 char* check = textbuffer;
871 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
875 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
877 strlcpy(oper_quit,textbuffer,MAXQUIT);
878 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
884 memset(&already_sent,0,MAX_DESCRIPTORS);
886 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
888 if (((ucrec*)(*v))->channel)
890 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
892 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
896 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
898 already_sent[i->second->fd] = 1;
902 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
905 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
913 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
917 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
921 if (u->registered != 7)
923 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
927 memset(&already_sent,0,MAX_DESCRIPTORS);
929 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
931 if (((ucrec*)(*v))->channel)
933 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
935 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
939 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
941 already_sent[i->second->fd] = 1;
942 WriteFrom_NoFormat(i->second->fd,u,text);
951 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
952 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
953 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
954 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
955 * uses the oper list, which means if you have 2000 users but only 5 opers,
956 * it iterates 5 times.
958 void WriteOpers(const char* text, ...)
960 char textbuffer[MAXBUF];
965 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
969 va_start(argsPtr, text);
970 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
973 WriteOpers_NoFormat(textbuffer);
976 void WriteOpers_NoFormat(const char* text)
980 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
984 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
990 if (a->modebits & UM_SERVERNOTICE)
992 // send server notices to all with +s
993 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
999 void ServerNoticeAll(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,"NOTICE $%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 ServerPrivmsgAll(char* text, ...)
1025 char textbuffer[MAXBUF];
1026 char formatbuffer[MAXBUF];
1028 va_start (argsPtr, text);
1029 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1032 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1034 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1036 userrec* t = (userrec*)(*i);
1037 WriteServ_NoFormat(t->fd,formatbuffer);
1041 void WriteMode(const char* modes, int flags, const char* text, ...)
1043 char textbuffer[MAXBUF];
1047 if ((!text) || (!modes) || (!flags))
1049 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1053 va_start(argsPtr, text);
1054 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1056 modelen = strlen(modes);
1058 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1060 userrec* t = (userrec*)(*i);
1061 bool send_to_user = false;
1063 if (flags == WM_AND)
1065 send_to_user = true;
1067 for (int n = 0; n < modelen; n++)
1069 if (!hasumode(t,modes[n]))
1071 send_to_user = false;
1076 else if (flags == WM_OR)
1078 send_to_user = false;
1080 for (int n = 0; n < modelen; n++)
1082 if (hasumode(t,modes[n]))
1084 send_to_user = true;
1092 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1097 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1099 char textbuffer[MAXBUF];
1100 char formatbuffer[MAXBUF];
1103 if ((!text) || (!source))
1105 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1109 va_start(argsPtr, text);
1110 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1113 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1115 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1117 userrec* t = (userrec*)(*i);
1118 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1123 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1125 char textbuffer[MAXBUF];
1126 char formatbuffer[MAXBUF];
1129 if ((!text) || (!source))
1131 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1135 va_start(argsPtr, text);
1136 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1139 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1141 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1143 userrec* t = (userrec*)(*i);
1145 if ((IS_LOCAL(t)) && (t->modebits & UM_WALLOPS))
1147 WriteTo_NoFormat(source,t,formatbuffer);
1152 /* convert a string to lowercase. Note following special circumstances
1153 * taken from RFC 1459. Many "official" server branches still hold to this
1154 * rule so i will too;
1156 * Because of IRC's scandanavian origin, the characters {}| are
1157 * considered to be the lower case equivalents of the characters []\,
1158 * respectively. This is a critical issue when determining the
1159 * equivalence of two nicknames.
1161 void strlower(char *n)
1165 for (char* t = n; *t; t++)
1166 *t = lowermap[(unsigned char)*t];
1170 /* Find a user record by nickname and return a pointer to it */
1172 userrec* Find(const std::string &nick)
1174 user_hash::iterator iter = clientlist.find(nick);
1176 if (iter == clientlist.end())
1177 /* Couldn't find it */
1180 return iter->second;
1183 userrec* Find(const char* nick)
1185 user_hash::iterator iter;
1190 iter = clientlist.find(nick);
1192 if (iter == clientlist.end())
1195 return iter->second;
1198 /* find a channel record by channel name and return a pointer to it */
1200 chanrec* FindChan(const char* chan)
1202 chan_hash::iterator iter;
1206 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1210 iter = chanlist.find(chan);
1212 if (iter == chanlist.end())
1213 /* Couldn't find it */
1216 return iter->second;
1220 long GetMaxBans(char* name)
1223 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1226 if (match(name,x.c_str()))
1234 void purge_empty_chans(userrec* u)
1236 std::vector<chanrec*> to_delete;
1238 // firstly decrement the count on each channel
1239 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1241 if (((ucrec*)(*f))->channel)
1243 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1245 /* No users left in here, mark it for deletion */
1246 to_delete.push_back(((ucrec*)(*f))->channel);
1247 ((ucrec*)(*f))->channel = NULL;
1252 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1254 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1256 chanrec* thischan = (chanrec*)*n;
1257 chan_hash::iterator i2 = chanlist.find(thischan->name);
1258 if (i2 != chanlist.end())
1260 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1271 char* chanmodes(chanrec *chan, bool showkey)
1273 static char scratch[MAXBUF];
1274 static char sparam[MAXBUF];
1275 char* offset = scratch;
1276 std::string extparam = "";
1280 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1288 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1289 for(int n = 0; n < 64; n++)
1298 extparam = (showkey ? chan->key : "<key>");
1301 extparam = ConvToStr(chan->limit);
1309 /* We know these have no parameters */
1312 extparam = chan->GetModeParameter(n+65);
1317 charlcat(sparam,' ',MAXBUF);
1318 strlcat(sparam,extparam.c_str(),MAXBUF);
1323 /* Null terminate scratch */
1325 strlcat(scratch,sparam,MAXMODES);
1330 /* compile a userlist of a channel into a string, each nick seperated by
1331 * spaces and op, voice etc status shown as @ and + */
1333 void userlist(userrec *user,chanrec *c)
1335 if ((!c) || (!user))
1337 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1342 size_t dlen, curlen;
1344 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1347 char* ptr = list + dlen;
1349 CUList *ulist= c->GetUsers();
1351 /* Improvement by Brain - this doesnt change in value, so why was it inside
1354 bool has_user = c->HasUser(user);
1356 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1358 if ((!has_user) && (i->second->modebits & UM_INVISIBLE))
1361 * user is +i, and source not on the channel, does not show
1362 * nick in NAMES list
1367 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1374 if (curlen > (480-NICKMAX))
1376 /* list overflowed into multiple numerics */
1377 WriteServ_NoFormat(user->fd,list);
1379 /* reset our lengths */
1380 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1388 /* if whats left in the list isnt empty, send it */
1391 WriteServ_NoFormat(user->fd,list);
1396 * return a count of the users on a specific channel accounting for
1397 * invisible users who won't increase the count. e.g. for /LIST
1399 int usercount_i(chanrec *c)
1406 CUList *ulist= c->GetUsers();
1407 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1409 if (!(i->second->modebits & UM_INVISIBLE))
1416 int usercount(chanrec *c)
1418 return (c ? c->GetUserCounter() : 0);
1422 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1423 ConnectClass GetClass(userrec *user)
1425 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1427 if (match(user->host,i->host.c_str()))
1433 return *(Config->Classes.begin());
1437 * sends out an error notice to all connected clients (not to be used
1440 void send_error(char *s)
1442 log(DEBUG,"send_error: %s",s);
1444 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1446 userrec* t = (userrec*)(*i);
1447 if (t->registered == 7)
1449 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1453 // fix - unregistered connections receive ERROR, not NOTICE
1454 Write(t->fd,"ERROR :%s",s);
1459 void Error(int status)
1461 signal(SIGALRM, SIG_IGN);
1462 signal(SIGPIPE, SIG_IGN);
1463 signal(SIGTERM, SIG_IGN);
1464 signal(SIGABRT, SIG_IGN);
1465 signal(SIGSEGV, SIG_IGN);
1466 signal(SIGURG, SIG_IGN);
1467 signal(SIGKILL, SIG_IGN);
1468 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1469 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1473 // this function counts all users connected, wether they are registered or NOT.
1476 return clientlist.size();
1479 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1480 int registered_usercount(void)
1484 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1486 if (i->second->registered == 7) c++;
1492 int usercount_invisible(void)
1496 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1498 if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE))
1505 int usercount_opers(void)
1509 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1511 if (*(i->second->oper))
1517 int usercount_unknown(void)
1521 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1523 userrec* t = (userrec*)(*i);
1524 if (t->registered != 7)
1531 long chancount(void)
1533 return chanlist.size();
1540 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1542 userrec* t = (userrec*)(*i);
1543 if (t->registered == 7)
1550 void ShowMOTD(userrec *user)
1552 static char mbuf[MAXBUF];
1553 static char crud[MAXBUF];
1554 std::string WholeMOTD = "";
1556 if (!Config->MOTD.size())
1558 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1562 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1563 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1564 WholeMOTD = WholeMOTD + mbuf;
1566 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1567 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1569 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1570 WholeMOTD = WholeMOTD + mbuf;
1572 // only one write operation
1573 if (Config->GetIOHook(user->port))
1577 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1579 catch (ModuleException& modexcept)
1581 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1586 user->AddWriteBuf(WholeMOTD);
1589 ServerInstance->stats->statsSent += WholeMOTD.length();
1592 void ShowRULES(userrec *user)
1594 if (!Config->RULES.size())
1596 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1599 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1601 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1602 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1604 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1607 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1608 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1609 // registration timeout maximum seconds)
1610 bool AllModulesReportReady(userrec* user)
1612 if (!Config->global_implementation[I_OnCheckReady])
1615 for (int i = 0; i <= MODCOUNT; i++)
1617 if (Config->implement_lists[i][I_OnCheckReady])
1619 int res = modules[i]->OnCheckReady(user);
1628 bool DirValid(char* dirandfile)
1631 char buffer[MAXBUF];
1632 char otherdir[MAXBUF];
1635 strlcpy(work, dirandfile, MAXBUF);
1638 // we just want the dir
1650 // Get the current working directory
1651 if (getcwd(buffer, MAXBUF ) == NULL )
1656 if (getcwd(otherdir, MAXBUF ) == NULL )
1661 size_t t = strlen(work);
1663 if (strlen(otherdir) >= t)
1667 if (!strcmp(otherdir,work))
1680 std::string GetFullProgDir(char** argv, int argc)
1683 char buffer[MAXBUF];
1684 char otherdir[MAXBUF];
1687 strlcpy(work,argv[0],MAXBUF);
1690 // we just want the dir
1702 // Get the current working directory
1703 if (getcwd(buffer, MAXBUF) == NULL)
1708 if (getcwd(otherdir, MAXBUF) == NULL)
1715 int InsertMode(std::string &output, const char* mode, unsigned short section)
1717 unsigned short currsection = 1;
1718 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1720 if(section > 4 || section == 0)
1722 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1726 for(; pos < output.size(); pos++)
1728 if(section == currsection)
1731 if(output[pos] == ',')
1735 output.insert(pos, mode);
1739 bool IsValidChannelName(const char *chname)
1743 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1744 if (!chname || *chname != '#')
1749 c = (char *)chname + 1;
1763 /* too long a name - note funky pointer arithmetic here. */
1764 if ((c - chname) > CHANMAX)
1772 inline int charlcat(char* x,char y,int z)
1789 bool charremove(char* mp, char remove)
1792 bool shift_down = false;
1796 if (*mptr == remove)
1808 void OpenLog(char** argv, int argc)
1812 if (Config->logpath == "")
1814 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1819 Config->log_file = fopen(LOG_FILE,"a+");
1821 if (!Config->log_file)
1823 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1830 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1832 if (!Config->log_file)
1834 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1843 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1844 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1851 if (*Config->DieValue)
1853 printf("WARNING: %s\n\n",Config->DieValue);
1854 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1859 /* We must load the modules AFTER initializing the socket engine, now */
1860 void LoadAllModules(InspIRCd* ServerInstance)
1862 char configToken[MAXBUF];
1863 Config->module_names.clear();
1866 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1868 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1869 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1871 if (!ServerInstance->LoadModule(configToken))
1873 log(DEFAULT,"Exiting due to a module loader error.");
1874 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1879 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);