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 we were given -debug we output all messages, regardless of configured loglevel */
81 if ((level < Config->LogLevel) && !Config->forcedebug)
86 struct tm *timeinfo = localtime(&TIME);
88 strlcpy(TIMESTR,asctime(timeinfo),26);
95 va_start(argsPtr, text);
96 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
100 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
105 printf("%s %s\n", TIMESTR, textbuffer);
110 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
112 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
114 void readfile(file_cache &F, const char* fname)
117 char linebuf[MAXBUF];
119 log(DEBUG,"readfile: loading %s",fname);
121 file = fopen(fname,"r");
127 fgets(linebuf,sizeof(linebuf),file);
128 linebuf[strlen(linebuf)-1]='\0';
137 F.push_back(linebuf);
145 log(DEBUG,"readfile: failed to load file: %s",fname);
148 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
152 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
153 * is a valid entry in the local FD table.
155 void Write_NoFormat(int sock, const char *text)
160 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
163 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
166 if (fd_ref_table[sock])
168 if (Config->GetIOHook(fd_ref_table[sock]->port))
172 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
174 catch (ModuleException& modexcept)
176 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
181 fd_ref_table[sock]->AddWriteBuf(tb);
183 ServerInstance->stats->statsSent += bytes;
186 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
190 * Same as Write_NoFormat(), but formatted printf() style first.
192 void Write(int sock, char *text, ...)
195 char textbuffer[MAXBUF];
199 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
204 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
208 va_start(argsPtr, text);
209 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
211 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
214 if (fd_ref_table[sock])
216 if (Config->GetIOHook(fd_ref_table[sock]->port))
220 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
222 catch (ModuleException& modexcept)
224 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
229 fd_ref_table[sock]->AddWriteBuf(tb);
231 ServerInstance->stats->statsSent += bytes;
234 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
237 /** WriteServ_NoFormat()
238 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
240 void WriteServ_NoFormat(int sock, const char* text)
245 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
248 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
251 if (fd_ref_table[sock])
253 if (Config->GetIOHook(fd_ref_table[sock]->port))
257 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
259 catch (ModuleException& modexcept)
261 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
266 fd_ref_table[sock]->AddWriteBuf(tb);
268 ServerInstance->stats->statsSent += bytes;
271 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
275 * Same as Write(), except `text' is prefixed with `:server.name '.
277 void WriteServ(int sock, char* text, ...)
280 char textbuffer[MAXBUF];
284 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
289 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
293 va_start(argsPtr, text);
294 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
297 WriteServ_NoFormat(sock, textbuffer);
300 /** WriteFrom_NoFormat()
301 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
302 * the nick, user, and host of `user'.
304 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
309 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
312 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
315 if (fd_ref_table[sock])
317 if (Config->GetIOHook(fd_ref_table[sock]->port))
321 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
323 catch (ModuleException& modexcept)
325 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
330 fd_ref_table[sock]->AddWriteBuf(tb);
332 ServerInstance->stats->statsSent += bytes;
335 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
338 /* write text from an originating user to originating user */
340 void WriteFrom(int sock, userrec *user,char* text, ...)
343 char textbuffer[MAXBUF];
347 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
350 if ((!text) || (!user))
352 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
356 va_start(argsPtr, text);
357 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
359 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
362 if (fd_ref_table[sock])
364 if (Config->GetIOHook(fd_ref_table[sock]->port))
368 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
370 catch (ModuleException& modexcept)
372 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
377 fd_ref_table[sock]->AddWriteBuf(tb);
380 ServerInstance->stats->statsSent += bytes;
383 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
386 /* write text to an destination user from a source user (e.g. user privmsg) */
388 void WriteTo(userrec *source, userrec *dest,char *data, ...)
390 char textbuffer[MAXBUF];
393 if ((!dest) || (!data))
395 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
402 va_start(argsPtr, data);
403 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
407 // if no source given send it from the server.
410 WriteServ_NoFormat(dest->fd,textbuffer);
414 WriteFrom_NoFormat(dest->fd,source,textbuffer);
418 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
420 if ((!dest) || (!data))
425 WriteServ_NoFormat(dest->fd,data);
429 WriteFrom_NoFormat(dest->fd,source,data);
433 /* write formatted text from a source user to all users on a channel
434 * including the sender (NOT for privmsg, notice etc!) */
436 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
438 char textbuffer[MAXBUF];
442 if ((!Ptr) || (!user) || (!text))
444 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
448 va_start(argsPtr, text);
449 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
452 ulist = Ptr->GetUsers();
454 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
456 if (i->second->fd != FD_MAGIC_NUMBER)
457 WriteTo_NoFormat(user,i->second,textbuffer);
461 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
465 if ((!Ptr) || (!user) || (!text))
467 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
471 ulist = Ptr->GetUsers();
473 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
475 if (i->second->fd != FD_MAGIC_NUMBER)
476 WriteTo_NoFormat(user,i->second,text);
481 /* write formatted text from a source user to all users on a channel
482 * including the sender (NOT for privmsg, notice etc!) doesnt send to
483 * users on remote servers */
485 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
487 char textbuffer[MAXBUF];
491 if ((!Ptr) || (!text))
493 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
497 va_start(argsPtr, text);
498 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
501 ulist = Ptr->GetUsers();
503 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
505 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
509 WriteServ_NoFormat(i->second->fd,textbuffer);
513 WriteTo_NoFormat(user,i->second,textbuffer);
519 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
523 if ((!Ptr) || (!text))
525 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
529 ulist = Ptr->GetUsers();
531 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
533 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
537 WriteServ_NoFormat(i->second->fd,text);
541 WriteTo_NoFormat(user,i->second,text);
549 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
551 char textbuffer[MAXBUF];
555 if ((!Ptr) || (!text))
557 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
561 va_start(argsPtr, text);
562 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
565 ulist = Ptr->GetUsers();
567 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
569 if (IS_LOCAL(i->second))
570 WriteServ_NoFormat(i->second->fd,textbuffer);
574 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
578 if ((!Ptr) || (!text))
580 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
584 ulist = Ptr->GetUsers();
586 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
588 if (IS_LOCAL(i->second))
589 WriteServ_NoFormat(i->second->fd,text);
595 /* write formatted text from a source user to all users on a channel except
596 * for the sender (for privmsg etc) */
598 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
600 char textbuffer[MAXBUF];
604 if ((!Ptr) || (!user) || (!text))
606 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
610 va_start(argsPtr, text);
611 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
617 ulist = Ptr->GetOppedUsers();
620 ulist = Ptr->GetHalfoppedUsers();
623 ulist = Ptr->GetVoicedUsers();
626 ulist = Ptr->GetUsers();
630 log(DEBUG,"%d users to write to",ulist->size());
632 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
634 if ((IS_LOCAL(i->second)) && (user != i->second))
635 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
639 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
643 if ((!Ptr) || (!user) || (!text))
645 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
652 ulist = Ptr->GetOppedUsers();
655 ulist = Ptr->GetHalfoppedUsers();
658 ulist = Ptr->GetVoicedUsers();
661 ulist = Ptr->GetUsers();
665 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
667 if ((IS_LOCAL(i->second)) && (user != i->second))
668 WriteFrom_NoFormat(i->second->fd,user,text);
672 std::string GetServerDescription(char* servername)
674 std::string description = "";
676 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
678 if (description != "")
684 // not a remote server that can be found, it must be me.
685 return Config->ServerDesc;
689 /* write a formatted string to all users who share at least one common
690 * channel, including the source user e.g. for use in NICK */
692 void WriteCommon(userrec *u, char* text, ...)
694 char textbuffer[MAXBUF];
696 bool sent_to_at_least_one = false;
700 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
704 if (u->registered != 7)
706 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
710 va_start(argsPtr, text);
711 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
714 // FIX: Stops a message going to the same person more than once
715 memset(&already_sent,0,MAX_DESCRIPTORS);
717 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
719 if (((ucrec*)(*v))->channel)
721 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
723 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
725 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
727 already_sent[i->second->fd] = 1;
728 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
729 sent_to_at_least_one = true;
736 * if the user was not in any channels, no users will receive the text. Make sure the user
737 * receives their OWN message for WriteCommon
739 if (!sent_to_at_least_one)
741 WriteFrom_NoFormat(u->fd,u,textbuffer);
745 void WriteCommon_NoFormat(userrec *u, const char* text)
747 bool sent_to_at_least_one = false;
751 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
755 if (u->registered != 7)
757 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
761 // FIX: Stops a message going to the same person more than once
762 memset(&already_sent,0,MAX_DESCRIPTORS);
764 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
766 if (((ucrec*)(*v))->channel)
768 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
770 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
772 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
774 already_sent[i->second->fd] = 1;
775 WriteFrom_NoFormat(i->second->fd,u,text);
776 sent_to_at_least_one = true;
783 * if the user was not in any channels, no users will receive the text. Make sure the user
784 * receives their OWN message for WriteCommon
786 if (!sent_to_at_least_one)
788 WriteFrom_NoFormat(u->fd,u,text);
793 /* write a formatted string to all users who share at least one common
794 * channel, NOT including the source user e.g. for use in QUIT
797 void WriteCommonExcept(userrec *u, char* text, ...)
799 char textbuffer[MAXBUF];
800 char oper_quit[MAXBUF];
801 bool quit_munge = false;
807 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
811 if (u->registered != 7)
813 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
817 va_start(argsPtr, text);
818 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
821 if ((Config->HideSplits) && (total > 6))
823 /* Yeah yeah, this is ugly. But its fast, live with it. */
824 char* check = textbuffer;
826 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
828 std::stringstream split(check);
829 std::string server_one;
830 std::string server_two;
835 if ((FindServerName(server_one)) && (FindServerName(server_two)))
837 strlcpy(oper_quit,textbuffer,MAXQUIT);
838 strlcpy(check,"*.net *.split",MAXQUIT);
844 if ((Config->HideBans) && (total > 13) && (!quit_munge))
846 char* check = textbuffer;
849 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
853 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
855 strlcpy(oper_quit,textbuffer,MAXQUIT);
856 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
862 memset(&already_sent,0,MAX_DESCRIPTORS);
864 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
866 if (((ucrec*)(*v))->channel)
868 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
870 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
874 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
876 already_sent[i->second->fd] = 1;
880 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
883 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
891 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
895 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
899 if (u->registered != 7)
901 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
905 memset(&already_sent,0,MAX_DESCRIPTORS);
907 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
909 if (((ucrec*)(*v))->channel)
911 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
913 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
917 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
919 already_sent[i->second->fd] = 1;
920 WriteFrom_NoFormat(i->second->fd,u,text);
929 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
930 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
931 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
932 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
933 * uses the oper list, which means if you have 2000 users but only 5 opers,
934 * it iterates 5 times.
936 void WriteOpers(const char* text, ...)
938 char textbuffer[MAXBUF];
943 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
947 va_start(argsPtr, text);
948 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
951 WriteOpers_NoFormat(textbuffer);
954 void WriteOpers_NoFormat(const char* text)
958 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
962 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
968 if (a->modebits & UM_SERVERNOTICE)
970 // send server notices to all with +s
971 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
977 void ServerNoticeAll(char* text, ...)
982 char textbuffer[MAXBUF];
983 char formatbuffer[MAXBUF];
985 va_start (argsPtr, text);
986 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
989 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
991 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
993 userrec* t = (userrec*)(*i);
994 WriteServ_NoFormat(t->fd,formatbuffer);
998 void ServerPrivmsgAll(char* text, ...)
1003 char textbuffer[MAXBUF];
1004 char formatbuffer[MAXBUF];
1006 va_start (argsPtr, text);
1007 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1010 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1012 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1014 userrec* t = (userrec*)(*i);
1015 WriteServ_NoFormat(t->fd,formatbuffer);
1019 void WriteMode(const char* modes, int flags, const char* text, ...)
1021 char textbuffer[MAXBUF];
1025 if ((!text) || (!modes) || (!flags))
1027 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1031 va_start(argsPtr, text);
1032 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1034 modelen = strlen(modes);
1036 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1038 userrec* t = (userrec*)(*i);
1039 bool send_to_user = false;
1041 if (flags == WM_AND)
1043 send_to_user = true;
1045 for (int n = 0; n < modelen; n++)
1047 if (!hasumode(t,modes[n]))
1049 send_to_user = false;
1054 else if (flags == WM_OR)
1056 send_to_user = false;
1058 for (int n = 0; n < modelen; n++)
1060 if (hasumode(t,modes[n]))
1062 send_to_user = true;
1070 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1075 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1077 char textbuffer[MAXBUF];
1078 char formatbuffer[MAXBUF];
1081 if ((!text) || (!source))
1083 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1087 va_start(argsPtr, text);
1088 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1091 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1093 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1095 userrec* t = (userrec*)(*i);
1096 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1101 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1103 char textbuffer[MAXBUF];
1104 char formatbuffer[MAXBUF];
1107 if ((!text) || (!source))
1109 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1113 va_start(argsPtr, text);
1114 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1117 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1119 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1121 userrec* t = (userrec*)(*i);
1123 if ((IS_LOCAL(t)) && (t->modebits & UM_WALLOPS))
1125 WriteTo_NoFormat(source,t,formatbuffer);
1130 /* convert a string to lowercase. Note following special circumstances
1131 * taken from RFC 1459. Many "official" server branches still hold to this
1132 * rule so i will too;
1134 * Because of IRC's scandanavian origin, the characters {}| are
1135 * considered to be the lower case equivalents of the characters []\,
1136 * respectively. This is a critical issue when determining the
1137 * equivalence of two nicknames.
1139 void strlower(char *n)
1143 for (char* t = n; *t; t++)
1144 *t = lowermap[(unsigned char)*t];
1148 /* Find a user record by nickname and return a pointer to it */
1150 userrec* Find(const std::string &nick)
1152 user_hash::iterator iter = clientlist.find(nick);
1154 if (iter == clientlist.end())
1155 /* Couldn't find it */
1158 return iter->second;
1161 userrec* Find(const char* nick)
1163 user_hash::iterator iter;
1168 iter = clientlist.find(nick);
1170 if (iter == clientlist.end())
1173 return iter->second;
1176 /* find a channel record by channel name and return a pointer to it */
1178 chanrec* FindChan(const char* chan)
1180 chan_hash::iterator iter;
1184 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1188 iter = chanlist.find(chan);
1190 if (iter == chanlist.end())
1191 /* Couldn't find it */
1194 return iter->second;
1198 long GetMaxBans(char* name)
1201 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1204 if (match(name,x.c_str()))
1212 void purge_empty_chans(userrec* u)
1214 std::vector<chanrec*> to_delete;
1216 // firstly decrement the count on each channel
1217 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1219 if (((ucrec*)(*f))->channel)
1221 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1223 /* No users left in here, mark it for deletion */
1224 to_delete.push_back(((ucrec*)(*f))->channel);
1225 ((ucrec*)(*f))->channel = NULL;
1230 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1232 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1234 chanrec* thischan = (chanrec*)*n;
1235 chan_hash::iterator i2 = chanlist.find(thischan->name);
1236 if (i2 != chanlist.end())
1238 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1249 char* chanmodes(chanrec *chan, bool showkey)
1251 static char scratch[MAXBUF];
1252 static char sparam[MAXBUF];
1253 char* offset = scratch;
1254 std::string extparam = "";
1258 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1266 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1267 for(int n = 0; n < 64; n++)
1276 extparam = (showkey ? chan->key : "<key>");
1279 extparam = ConvToStr(chan->limit);
1287 /* We know these have no parameters */
1290 extparam = chan->GetModeParameter(n+65);
1295 charlcat(sparam,' ',MAXBUF);
1296 strlcat(sparam,extparam.c_str(),MAXBUF);
1301 /* Null terminate scratch */
1303 strlcat(scratch,sparam,MAXMODES);
1308 /* compile a userlist of a channel into a string, each nick seperated by
1309 * spaces and op, voice etc status shown as @ and + */
1311 void userlist(userrec *user,chanrec *c)
1313 if ((!c) || (!user))
1315 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1320 size_t dlen, curlen;
1322 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1325 char* ptr = list + dlen;
1327 CUList *ulist= c->GetUsers();
1329 /* Improvement by Brain - this doesnt change in value, so why was it inside
1332 bool has_user = c->HasUser(user);
1334 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1336 if ((!has_user) && (i->second->modebits & UM_INVISIBLE))
1339 * user is +i, and source not on the channel, does not show
1340 * nick in NAMES list
1345 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1352 if (curlen > (480-NICKMAX))
1354 /* list overflowed into multiple numerics */
1355 WriteServ_NoFormat(user->fd,list);
1357 /* reset our lengths */
1358 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1366 /* if whats left in the list isnt empty, send it */
1369 WriteServ_NoFormat(user->fd,list);
1374 * return a count of the users on a specific channel accounting for
1375 * invisible users who won't increase the count. e.g. for /LIST
1377 int usercount_i(chanrec *c)
1384 CUList *ulist= c->GetUsers();
1385 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1387 if (!(i->second->modebits & UM_INVISIBLE))
1394 int usercount(chanrec *c)
1396 return (c ? c->GetUserCounter() : 0);
1400 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1401 ConnectClass GetClass(userrec *user)
1403 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1405 if (match(user->host,i->host.c_str()))
1411 return *(Config->Classes.begin());
1415 * sends out an error notice to all connected clients (not to be used
1418 void send_error(char *s)
1420 log(DEBUG,"send_error: %s",s);
1422 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1424 userrec* t = (userrec*)(*i);
1425 if (t->registered == 7)
1427 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1431 // fix - unregistered connections receive ERROR, not NOTICE
1432 Write(t->fd,"ERROR :%s",s);
1437 void Error(int status)
1439 signal(SIGALRM, SIG_IGN);
1440 signal(SIGPIPE, SIG_IGN);
1441 signal(SIGTERM, SIG_IGN);
1442 signal(SIGABRT, SIG_IGN);
1443 signal(SIGSEGV, SIG_IGN);
1444 signal(SIGURG, SIG_IGN);
1445 signal(SIGKILL, SIG_IGN);
1446 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1447 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1451 // this function counts all users connected, wether they are registered or NOT.
1454 return clientlist.size();
1457 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1458 int registered_usercount(void)
1462 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1464 if (i->second->registered == 7) c++;
1470 int usercount_invisible(void)
1474 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1476 if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE))
1483 int usercount_opers(void)
1487 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1489 if (*(i->second->oper))
1495 int usercount_unknown(void)
1499 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1501 userrec* t = (userrec*)(*i);
1502 if (t->registered != 7)
1509 long chancount(void)
1511 return chanlist.size();
1518 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1520 userrec* t = (userrec*)(*i);
1521 if (t->registered == 7)
1528 void ShowMOTD(userrec *user)
1530 static char mbuf[MAXBUF];
1531 static char crud[MAXBUF];
1532 std::string WholeMOTD = "";
1534 if (!Config->MOTD.size())
1536 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1540 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1541 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1542 WholeMOTD = WholeMOTD + mbuf;
1544 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1545 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1547 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1548 WholeMOTD = WholeMOTD + mbuf;
1550 // only one write operation
1551 if (Config->GetIOHook(user->port))
1555 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1557 catch (ModuleException& modexcept)
1559 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1564 user->AddWriteBuf(WholeMOTD);
1567 ServerInstance->stats->statsSent += WholeMOTD.length();
1570 void ShowRULES(userrec *user)
1572 if (!Config->RULES.size())
1574 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1577 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1579 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1580 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1582 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1585 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1586 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1587 // registration timeout maximum seconds)
1588 bool AllModulesReportReady(userrec* user)
1590 if (!Config->global_implementation[I_OnCheckReady])
1593 for (int i = 0; i <= MODCOUNT; i++)
1595 if (Config->implement_lists[i][I_OnCheckReady])
1597 int res = modules[i]->OnCheckReady(user);
1606 bool DirValid(char* dirandfile)
1609 char buffer[MAXBUF];
1610 char otherdir[MAXBUF];
1613 strlcpy(work, dirandfile, MAXBUF);
1616 // we just want the dir
1628 // Get the current working directory
1629 if (getcwd(buffer, MAXBUF ) == NULL )
1634 if (getcwd(otherdir, MAXBUF ) == NULL )
1639 size_t t = strlen(work);
1641 if (strlen(otherdir) >= t)
1645 if (!strcmp(otherdir,work))
1658 std::string GetFullProgDir(char** argv, int argc)
1661 char buffer[MAXBUF];
1662 char otherdir[MAXBUF];
1665 strlcpy(work,argv[0],MAXBUF);
1668 // we just want the dir
1680 // Get the current working directory
1681 if (getcwd(buffer, MAXBUF) == NULL)
1686 if (getcwd(otherdir, MAXBUF) == NULL)
1693 int InsertMode(std::string &output, const char* mode, unsigned short section)
1695 unsigned short currsection = 1;
1696 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1698 if(section > 4 || section == 0)
1700 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1704 for(; pos < output.size(); pos++)
1706 if(section == currsection)
1709 if(output[pos] == ',')
1713 output.insert(pos, mode);
1717 bool IsValidChannelName(const char *chname)
1721 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1722 if (!chname || *chname != '#')
1727 c = (char *)chname + 1;
1741 /* too long a name - note funky pointer arithmetic here. */
1742 if ((c - chname) > CHANMAX)
1750 inline int charlcat(char* x,char y,int z)
1767 bool charremove(char* mp, char remove)
1770 bool shift_down = false;
1774 if (*mptr == remove)
1786 void OpenLog(char** argv, int argc)
1790 if (Config->logpath == "")
1792 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1797 Config->log_file = fopen(LOG_FILE,"a+");
1799 if (!Config->log_file)
1801 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1808 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1810 if (!Config->log_file)
1812 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1821 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1822 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1829 if (*Config->DieValue)
1831 printf("WARNING: %s\n\n",Config->DieValue);
1832 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1837 /* We must load the modules AFTER initializing the socket engine, now */
1838 void LoadAllModules(InspIRCd* ServerInstance)
1840 char configToken[MAXBUF];
1841 Config->module_names.clear();
1844 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1846 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1847 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1849 if (!ServerInstance->LoadModule(configToken))
1851 log(DEFAULT,"Exiting due to a module loader error.");
1852 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1857 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);