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(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 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
979 if (a->modebits & UM_SERVERNOTICE)
981 // send server notices to all with +s
982 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
988 void ServerNoticeAll(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,"NOTICE $%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 ServerPrivmsgAll(char* text, ...)
1014 char textbuffer[MAXBUF];
1015 char formatbuffer[MAXBUF];
1017 va_start (argsPtr, text);
1018 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1021 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1023 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1025 userrec* t = (userrec*)(*i);
1026 WriteServ_NoFormat(t->fd,formatbuffer);
1030 void WriteMode(const char* modes, int flags, const char* text, ...)
1032 char textbuffer[MAXBUF];
1036 if ((!text) || (!modes) || (!flags))
1038 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1042 va_start(argsPtr, text);
1043 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1045 modelen = strlen(modes);
1047 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1049 userrec* t = (userrec*)(*i);
1050 bool send_to_user = false;
1052 if (flags == WM_AND)
1054 send_to_user = true;
1056 for (int n = 0; n < modelen; n++)
1058 if (!hasumode(t,modes[n]))
1060 send_to_user = false;
1065 else if (flags == WM_OR)
1067 send_to_user = false;
1069 for (int n = 0; n < modelen; n++)
1071 if (hasumode(t,modes[n]))
1073 send_to_user = true;
1081 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1086 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1088 char textbuffer[MAXBUF];
1089 char formatbuffer[MAXBUF];
1092 if ((!text) || (!source))
1094 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1098 va_start(argsPtr, text);
1099 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1102 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1104 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1106 userrec* t = (userrec*)(*i);
1107 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1112 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1114 char textbuffer[MAXBUF];
1115 char formatbuffer[MAXBUF];
1118 if ((!text) || (!source))
1120 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1124 va_start(argsPtr, text);
1125 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1128 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1130 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1132 userrec* t = (userrec*)(*i);
1134 if ((IS_LOCAL(t)) && (t->modebits & UM_WALLOPS))
1136 WriteTo_NoFormat(source,t,formatbuffer);
1141 /* convert a string to lowercase. Note following special circumstances
1142 * taken from RFC 1459. Many "official" server branches still hold to this
1143 * rule so i will too;
1145 * Because of IRC's scandanavian origin, the characters {}| are
1146 * considered to be the lower case equivalents of the characters []\,
1147 * respectively. This is a critical issue when determining the
1148 * equivalence of two nicknames.
1150 void strlower(char *n)
1154 for (char* t = n; *t; t++)
1155 *t = lowermap[(unsigned char)*t];
1159 /* Find a user record by nickname and return a pointer to it */
1161 userrec* Find(const std::string &nick)
1163 user_hash::iterator iter = clientlist.find(nick);
1165 if (iter == clientlist.end())
1166 /* Couldn't find it */
1169 return iter->second;
1172 userrec* Find(const char* nick)
1174 user_hash::iterator iter;
1179 iter = clientlist.find(nick);
1181 if (iter == clientlist.end())
1184 return iter->second;
1187 /* find a channel record by channel name and return a pointer to it */
1189 chanrec* FindChan(const char* chan)
1191 chan_hash::iterator iter;
1195 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1199 iter = chanlist.find(chan);
1201 if (iter == chanlist.end())
1202 /* Couldn't find it */
1205 return iter->second;
1209 long GetMaxBans(char* name)
1212 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1215 if (match(name,x.c_str()))
1223 void purge_empty_chans(userrec* u)
1225 std::vector<chanrec*> to_delete;
1227 // firstly decrement the count on each channel
1228 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1230 if (((ucrec*)(*f))->channel)
1232 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1234 /* No users left in here, mark it for deletion */
1235 to_delete.push_back(((ucrec*)(*f))->channel);
1236 ((ucrec*)(*f))->channel = NULL;
1241 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1243 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1245 chanrec* thischan = (chanrec*)*n;
1246 chan_hash::iterator i2 = chanlist.find(thischan->name);
1247 if (i2 != chanlist.end())
1249 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1260 char* chanmodes(chanrec *chan, bool showkey)
1262 static char scratch[MAXBUF];
1263 static char sparam[MAXBUF];
1264 char* offset = scratch;
1265 std::string extparam = "";
1269 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1277 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1278 for(int n = 0; n < 64; n++)
1287 extparam = (showkey ? chan->key : "<key>");
1290 extparam = ConvToStr(chan->limit);
1298 /* We know these have no parameters */
1301 extparam = chan->GetModeParameter(n+65);
1306 charlcat(sparam,' ',MAXBUF);
1307 strlcat(sparam,extparam.c_str(),MAXBUF);
1312 /* Null terminate scratch */
1314 strlcat(scratch,sparam,MAXMODES);
1319 /* compile a userlist of a channel into a string, each nick seperated by
1320 * spaces and op, voice etc status shown as @ and + */
1322 void userlist(userrec *user,chanrec *c)
1324 if ((!c) || (!user))
1326 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1331 size_t dlen, curlen;
1333 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1336 char* ptr = list + dlen;
1338 CUList *ulist= c->GetUsers();
1340 /* Improvement by Brain - this doesnt change in value, so why was it inside
1343 bool has_user = c->HasUser(user);
1345 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1347 if ((!has_user) && (i->second->modebits & UM_INVISIBLE))
1350 * user is +i, and source not on the channel, does not show
1351 * nick in NAMES list
1356 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1363 if (curlen > (480-NICKMAX))
1365 /* list overflowed into multiple numerics */
1366 WriteServ_NoFormat(user->fd,list);
1368 /* reset our lengths */
1369 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1377 /* if whats left in the list isnt empty, send it */
1380 WriteServ_NoFormat(user->fd,list);
1385 * return a count of the users on a specific channel accounting for
1386 * invisible users who won't increase the count. e.g. for /LIST
1388 int usercount_i(chanrec *c)
1395 CUList *ulist= c->GetUsers();
1396 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1398 if (!(i->second->modebits & UM_INVISIBLE))
1405 int usercount(chanrec *c)
1407 return (c ? c->GetUserCounter() : 0);
1411 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1412 ConnectClass GetClass(userrec *user)
1414 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1416 if (match(user->host,i->host.c_str()))
1422 return *(Config->Classes.begin());
1426 * sends out an error notice to all connected clients (not to be used
1429 void send_error(char *s)
1431 log(DEBUG,"send_error: %s",s);
1433 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1435 userrec* t = (userrec*)(*i);
1436 if (t->registered == 7)
1438 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1442 // fix - unregistered connections receive ERROR, not NOTICE
1443 Write(t->fd,"ERROR :%s",s);
1448 void Error(int status)
1450 signal(SIGALRM, SIG_IGN);
1451 signal(SIGPIPE, SIG_IGN);
1452 signal(SIGTERM, SIG_IGN);
1453 signal(SIGABRT, SIG_IGN);
1454 signal(SIGSEGV, SIG_IGN);
1455 signal(SIGURG, SIG_IGN);
1456 signal(SIGKILL, SIG_IGN);
1457 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1458 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1462 // this function counts all users connected, wether they are registered or NOT.
1465 return clientlist.size();
1468 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1469 int registered_usercount(void)
1473 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1475 if (i->second->registered == 7) c++;
1481 int usercount_invisible(void)
1485 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1487 if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE))
1494 int usercount_opers(void)
1498 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1500 if (*(i->second->oper))
1506 int usercount_unknown(void)
1510 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1512 userrec* t = (userrec*)(*i);
1513 if (t->registered != 7)
1520 long chancount(void)
1522 return chanlist.size();
1529 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1531 userrec* t = (userrec*)(*i);
1532 if (t->registered == 7)
1539 void ShowMOTD(userrec *user)
1541 static char mbuf[MAXBUF];
1542 static char crud[MAXBUF];
1543 std::string WholeMOTD = "";
1545 if (!Config->MOTD.size())
1547 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1551 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1552 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1553 WholeMOTD = WholeMOTD + mbuf;
1555 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1556 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1558 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1559 WholeMOTD = WholeMOTD + mbuf;
1561 // only one write operation
1562 if (Config->GetIOHook(user->port))
1566 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1568 catch (ModuleException& modexcept)
1570 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1575 user->AddWriteBuf(WholeMOTD);
1578 ServerInstance->stats->statsSent += WholeMOTD.length();
1581 void ShowRULES(userrec *user)
1583 if (!Config->RULES.size())
1585 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1588 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1590 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1591 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1593 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1596 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1597 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1598 // registration timeout maximum seconds)
1599 bool AllModulesReportReady(userrec* user)
1601 if (!Config->global_implementation[I_OnCheckReady])
1604 for (int i = 0; i <= MODCOUNT; i++)
1606 if (Config->implement_lists[i][I_OnCheckReady])
1608 int res = modules[i]->OnCheckReady(user);
1617 bool DirValid(char* dirandfile)
1620 char buffer[MAXBUF];
1621 char otherdir[MAXBUF];
1624 strlcpy(work, dirandfile, MAXBUF);
1627 // we just want the dir
1639 // Get the current working directory
1640 if (getcwd(buffer, MAXBUF ) == NULL )
1645 if (getcwd(otherdir, MAXBUF ) == NULL )
1650 size_t t = strlen(work);
1652 if (strlen(otherdir) >= t)
1656 if (!strcmp(otherdir,work))
1669 std::string GetFullProgDir(char** argv, int argc)
1672 char buffer[MAXBUF];
1673 char otherdir[MAXBUF];
1676 strlcpy(work,argv[0],MAXBUF);
1679 // we just want the dir
1691 // Get the current working directory
1692 if (getcwd(buffer, MAXBUF) == NULL)
1697 if (getcwd(otherdir, MAXBUF) == NULL)
1704 int InsertMode(std::string &output, const char* mode, unsigned short section)
1706 unsigned short currsection = 1;
1707 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1709 if(section > 4 || section == 0)
1711 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1715 for(; pos < output.size(); pos++)
1717 if(section == currsection)
1720 if(output[pos] == ',')
1724 output.insert(pos, mode);
1728 bool IsValidChannelName(const char *chname)
1732 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1733 if (!chname || *chname != '#')
1738 c = (char *)chname + 1;
1752 /* too long a name - note funky pointer arithmetic here. */
1753 if ((c - chname) > CHANMAX)
1761 inline int charlcat(char* x,char y,int z)
1778 bool charremove(char* mp, char remove)
1781 bool shift_down = false;
1785 if (*mptr == remove)
1797 void OpenLog(char** argv, int argc)
1801 if (Config->logpath == "")
1803 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1808 Config->log_file = fopen(LOG_FILE,"a+");
1810 if (!Config->log_file)
1812 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1819 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1821 if (!Config->log_file)
1823 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1832 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1833 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1840 if (*Config->DieValue)
1842 printf("WARNING: %s\n\n",Config->DieValue);
1843 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1848 /* We must load the modules AFTER initializing the socket engine, now */
1849 void LoadAllModules(InspIRCd* ServerInstance)
1851 char configToken[MAXBUF];
1852 Config->module_names.clear();
1855 for (int count = 0; count < Config->ConfValueEnum("module",&Config->config_f); count++)
1857 Config->ConfValue("module","name",count,configToken,&Config->config_f);
1858 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1860 if (!ServerInstance->LoadModule(configToken))
1862 log(DEFAULT,"Exiting due to a module loader error.");
1863 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
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);