1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
18 #include "inspircd_config.h"
20 #include "configreader.h"
23 #include <sys/errno.h>
31 #include "connection.h"
42 #include "inspstring.h"
43 #include "helperfuncs.h"
48 extern ModuleList modules;
49 extern ServerConfig *Config;
50 extern InspIRCd* ServerInstance;
52 extern char lowermap[255];
53 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
54 static char already_sent[MAX_DESCRIPTORS];
55 extern std::vector<userrec*> all_opers;
56 extern user_hash clientlist;
57 extern chan_hash chanlist;
59 char LOG_FILE[MAXBUF];
61 extern std::vector<userrec*> local_users;
63 static char TIMESTR[26];
64 static time_t LAST = 0;
67 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
68 * is greater than the configured loglevel.
70 void do_log(int level, const char *text, ...)
73 char textbuffer[MAXBUF];
75 /* If we were given -debug we output all messages, regardless of configured loglevel */
76 if ((level < Config->LogLevel) && !Config->forcedebug)
81 struct tm *timeinfo = localtime(&TIME);
83 strlcpy(TIMESTR,asctime(timeinfo),26);
90 va_start(argsPtr, text);
91 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
96 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
97 fflush(Config->log_file);
103 printf("%s %s\n", TIMESTR, textbuffer);
108 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
110 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
112 void readfile(file_cache &F, const char* fname)
115 char linebuf[MAXBUF];
117 log(DEBUG,"readfile: loading %s",fname);
119 file = fopen(fname,"r");
125 fgets(linebuf,sizeof(linebuf),file);
126 linebuf[strlen(linebuf)-1]='\0';
135 F.push_back(linebuf);
143 log(DEBUG,"readfile: failed to load file: %s",fname);
146 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
150 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
151 * is a valid entry in the local FD table.
153 void Write_NoFormat(int sock, const char *text)
158 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
161 if (fd_ref_table[sock])
163 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
165 if (Config->GetIOHook(fd_ref_table[sock]->port))
169 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
171 catch (ModuleException& modexcept)
173 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
178 fd_ref_table[sock]->AddWriteBuf(tb);
180 ServerInstance->stats->statsSent += bytes;
183 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
187 * Same as Write_NoFormat(), but formatted printf() style first.
189 void Write(int sock, char *text, ...)
192 char textbuffer[MAXBUF];
196 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
201 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
205 if (fd_ref_table[sock])
208 va_start(argsPtr, text);
209 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
211 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
213 if (Config->GetIOHook(fd_ref_table[sock]->port))
217 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
219 catch (ModuleException& modexcept)
221 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
226 fd_ref_table[sock]->AddWriteBuf(tb);
228 ServerInstance->stats->statsSent += bytes;
231 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
234 /** WriteServ_NoFormat()
235 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
237 void WriteServ_NoFormat(int sock, const char* text)
242 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
245 if (fd_ref_table[sock])
247 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
249 if (Config->GetIOHook(fd_ref_table[sock]->port))
253 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
255 catch (ModuleException& modexcept)
257 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
262 fd_ref_table[sock]->AddWriteBuf(tb);
264 ServerInstance->stats->statsSent += bytes;
267 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
271 * Same as Write(), except `text' is prefixed with `:server.name '.
273 void WriteServ(int sock, char* text, ...)
276 char textbuffer[MAXBUF];
278 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
283 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
287 if (!fd_ref_table[sock])
290 va_start(argsPtr, text);
291 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
294 WriteServ_NoFormat(sock, textbuffer);
297 /** WriteFrom_NoFormat()
298 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
299 * the nick, user, and host of `user'.
301 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
306 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
309 if (fd_ref_table[sock])
311 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
313 if (Config->GetIOHook(fd_ref_table[sock]->port))
317 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
319 catch (ModuleException& modexcept)
321 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
326 fd_ref_table[sock]->AddWriteBuf(tb);
328 ServerInstance->stats->statsSent += bytes;
331 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
334 /* write text from an originating user to originating user */
336 void WriteFrom(int sock, userrec *user,char* text, ...)
339 char textbuffer[MAXBUF];
343 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
346 if ((!text) || (!user))
348 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
352 if (fd_ref_table[sock])
355 va_start(argsPtr, text);
356 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
358 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
360 if (Config->GetIOHook(fd_ref_table[sock]->port))
364 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
366 catch (ModuleException& modexcept)
368 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
373 fd_ref_table[sock]->AddWriteBuf(tb);
376 ServerInstance->stats->statsSent += bytes;
379 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
382 /* write text to an destination user from a source user (e.g. user privmsg) */
384 void WriteTo(userrec *source, userrec *dest,char *data, ...)
386 char textbuffer[MAXBUF];
389 if ((!dest) || (!data))
391 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
398 va_start(argsPtr, data);
399 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
402 // if no source given send it from the server.
405 WriteServ_NoFormat(dest->fd,textbuffer);
409 WriteFrom_NoFormat(dest->fd,source,textbuffer);
413 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
415 if ((!dest) || (!data))
420 WriteServ_NoFormat(dest->fd,data);
424 WriteFrom_NoFormat(dest->fd,source,data);
428 /* write formatted text from a source user to all users on a channel
429 * including the sender (NOT for privmsg, notice etc!) */
431 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
433 char textbuffer[MAXBUF];
437 if ((!Ptr) || (!user) || (!text))
439 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
443 va_start(argsPtr, text);
444 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
447 ulist = Ptr->GetUsers();
449 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
451 if (i->second->fd != FD_MAGIC_NUMBER)
452 WriteTo_NoFormat(user,i->second,textbuffer);
456 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
460 if ((!Ptr) || (!user) || (!text))
462 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
466 ulist = Ptr->GetUsers();
468 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
470 if (i->second->fd != FD_MAGIC_NUMBER)
471 WriteTo_NoFormat(user,i->second,text);
476 /* write formatted text from a source user to all users on a channel
477 * including the sender (NOT for privmsg, notice etc!) doesnt send to
478 * users on remote servers */
480 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
482 char textbuffer[MAXBUF];
486 if ((!Ptr) || (!text))
488 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
492 va_start(argsPtr, text);
493 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
496 ulist = Ptr->GetUsers();
498 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
500 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
504 WriteServ_NoFormat(i->second->fd,textbuffer);
508 WriteTo_NoFormat(user,i->second,textbuffer);
514 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
518 if ((!Ptr) || (!text))
520 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
524 ulist = Ptr->GetUsers();
526 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
528 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
532 WriteServ_NoFormat(i->second->fd,text);
536 WriteTo_NoFormat(user,i->second,text);
544 void WriteChannelWithServ(const char* ServName, chanrec* Ptr, const char* text, ...)
546 char textbuffer[MAXBUF];
550 if ((!Ptr) || (!text))
552 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
556 va_start(argsPtr, text);
557 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
560 ulist = Ptr->GetUsers();
562 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
564 if (IS_LOCAL(i->second))
565 WriteServ_NoFormat(i->second->fd,textbuffer);
569 void WriteChannelWithServ_NoFormat(const char* ServName, chanrec* Ptr, const char* text)
573 if ((!Ptr) || (!text))
575 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
579 ulist = Ptr->GetUsers();
581 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
583 if (IS_LOCAL(i->second))
584 WriteServ_NoFormat(i->second->fd,text);
590 /* write formatted text from a source user to all users on a channel except
591 * for the sender (for privmsg etc) */
593 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
595 char textbuffer[MAXBUF];
599 if ((!Ptr) || (!user) || (!text))
601 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
605 va_start(argsPtr, text);
606 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
612 ulist = Ptr->GetOppedUsers();
615 ulist = Ptr->GetHalfoppedUsers();
618 ulist = Ptr->GetVoicedUsers();
621 ulist = Ptr->GetUsers();
625 log(DEBUG,"%d users to write to",ulist->size());
627 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
629 if ((IS_LOCAL(i->second)) && (user != i->second))
630 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
634 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
638 if ((!Ptr) || (!user) || (!text))
640 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
647 ulist = Ptr->GetOppedUsers();
650 ulist = Ptr->GetHalfoppedUsers();
653 ulist = Ptr->GetVoicedUsers();
656 ulist = Ptr->GetUsers();
660 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
662 if ((IS_LOCAL(i->second)) && (user != i->second))
663 WriteFrom_NoFormat(i->second->fd,user,text);
667 std::string GetServerDescription(const char* servername)
669 std::string description = "";
671 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
673 if (description != "")
679 // not a remote server that can be found, it must be me.
680 return Config->ServerDesc;
684 /* write a formatted string to all users who share at least one common
685 * channel, including the source user e.g. for use in NICK */
687 void WriteCommon(userrec *u, char* text, ...)
689 char textbuffer[MAXBUF];
691 bool sent_to_at_least_one = false;
695 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
699 if (u->registered != 7)
701 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
705 va_start(argsPtr, text);
706 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
709 // FIX: Stops a message going to the same person more than once
710 memset(&already_sent,0,MAX_DESCRIPTORS);
712 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
714 if (((ucrec*)(*v))->channel)
716 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
718 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
720 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
722 already_sent[i->second->fd] = 1;
723 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
724 sent_to_at_least_one = true;
731 * if the user was not in any channels, no users will receive the text. Make sure the user
732 * receives their OWN message for WriteCommon
734 if (!sent_to_at_least_one)
736 WriteFrom_NoFormat(u->fd,u,textbuffer);
740 void WriteCommon_NoFormat(userrec *u, const char* text)
742 bool sent_to_at_least_one = false;
746 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
750 if (u->registered != 7)
752 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
756 // FIX: Stops a message going to the same person more than once
757 memset(&already_sent,0,MAX_DESCRIPTORS);
759 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
761 if (((ucrec*)(*v))->channel)
763 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
765 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
767 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
769 already_sent[i->second->fd] = 1;
770 WriteFrom_NoFormat(i->second->fd,u,text);
771 sent_to_at_least_one = true;
778 * if the user was not in any channels, no users will receive the text. Make sure the user
779 * receives their OWN message for WriteCommon
781 if (!sent_to_at_least_one)
783 WriteFrom_NoFormat(u->fd,u,text);
788 /* write a formatted string to all users who share at least one common
789 * channel, NOT including the source user e.g. for use in QUIT
792 void WriteCommonExcept(userrec *u, char* text, ...)
794 char textbuffer[MAXBUF];
795 char oper_quit[MAXBUF];
796 bool quit_munge = false;
802 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
806 if (u->registered != 7)
808 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
812 va_start(argsPtr, text);
813 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
816 if ((Config->HideSplits) && (total > 6))
818 /* Yeah yeah, this is ugly. But its fast, live with it. */
819 char* check = textbuffer;
821 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
823 std::stringstream split(check);
824 std::string server_one;
825 std::string server_two;
830 if ((FindServerName(server_one)) && (FindServerName(server_two)))
832 strlcpy(oper_quit,textbuffer,MAXQUIT);
833 strlcpy(check,"*.net *.split",MAXQUIT);
839 if ((Config->HideBans) && (total > 13) && (!quit_munge))
841 char* check = textbuffer;
844 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
848 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
850 strlcpy(oper_quit,textbuffer,MAXQUIT);
851 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
857 memset(&already_sent,0,MAX_DESCRIPTORS);
859 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
861 if (((ucrec*)(*v))->channel)
863 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
865 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
869 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
871 already_sent[i->second->fd] = 1;
875 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
878 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
886 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
890 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
894 if (u->registered != 7)
896 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
900 memset(&already_sent,0,MAX_DESCRIPTORS);
902 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
904 if (((ucrec*)(*v))->channel)
906 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
908 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
912 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
914 already_sent[i->second->fd] = 1;
915 WriteFrom_NoFormat(i->second->fd,u,text);
924 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
925 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
926 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
927 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
928 * uses the oper list, which means if you have 2000 users but only 5 opers,
929 * it iterates 5 times.
931 void WriteOpers(const char* text, ...)
933 char textbuffer[MAXBUF];
938 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
942 va_start(argsPtr, text);
943 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
946 WriteOpers_NoFormat(textbuffer);
949 void WriteOpers_NoFormat(const char* text)
953 log(DEFAULT,"*** BUG *** WriteOpers_NoFormat was given an invalid parameter");
957 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
963 if (a->modes[UM_SERVERNOTICE])
965 // send server notices to all with +s
966 WriteServ(a->fd,"NOTICE %s :%s",a->nick,text);
972 void ServerNoticeAll(char* text, ...)
977 char textbuffer[MAXBUF];
978 char formatbuffer[MAXBUF];
980 va_start (argsPtr, text);
981 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
984 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
986 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
988 userrec* t = (userrec*)(*i);
989 WriteServ_NoFormat(t->fd,formatbuffer);
993 void ServerPrivmsgAll(char* text, ...)
998 char textbuffer[MAXBUF];
999 char formatbuffer[MAXBUF];
1001 va_start (argsPtr, text);
1002 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1005 snprintf(formatbuffer,MAXBUF,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1007 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1009 userrec* t = (userrec*)(*i);
1010 WriteServ_NoFormat(t->fd,formatbuffer);
1014 void WriteMode(const char* modes, int flags, const char* text, ...)
1016 char textbuffer[MAXBUF];
1020 if ((!text) || (!modes) || (!flags))
1022 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1026 va_start(argsPtr, text);
1027 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1029 modelen = strlen(modes);
1031 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1033 userrec* t = (userrec*)(*i);
1034 bool send_to_user = false;
1036 if (flags == WM_AND)
1038 send_to_user = true;
1040 for (int n = 0; n < modelen; n++)
1042 if (!t->modes[modes[n]-65])
1044 send_to_user = false;
1049 else if (flags == WM_OR)
1051 send_to_user = false;
1053 for (int n = 0; n < modelen; n++)
1055 if (t->modes[modes[n]-65])
1057 send_to_user = true;
1065 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1070 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1072 char textbuffer[MAXBUF];
1073 char formatbuffer[MAXBUF];
1076 if ((!text) || (!source))
1078 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1082 va_start(argsPtr, text);
1083 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1086 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1088 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1090 userrec* t = (userrec*)(*i);
1091 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1096 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1098 char textbuffer[MAXBUF];
1099 char formatbuffer[MAXBUF];
1102 if ((!text) || (!source))
1104 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1108 va_start(argsPtr, text);
1109 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1112 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1114 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1116 userrec* t = (userrec*)(*i);
1118 if ((IS_LOCAL(t)) && (t->modes[UM_WALLOPS]))
1120 WriteTo_NoFormat(source,t,formatbuffer);
1125 /* convert a string to lowercase. Note following special circumstances
1126 * taken from RFC 1459. Many "official" server branches still hold to this
1127 * rule so i will too;
1129 * Because of IRC's scandanavian origin, the characters {}| are
1130 * considered to be the lower case equivalents of the characters []\,
1131 * respectively. This is a critical issue when determining the
1132 * equivalence of two nicknames.
1134 void strlower(char *n)
1138 for (char* t = n; *t; t++)
1139 *t = lowermap[(unsigned char)*t];
1143 /* Find a user record by nickname and return a pointer to it */
1145 userrec* Find(const std::string &nick)
1147 user_hash::iterator iter = clientlist.find(nick);
1149 if (iter == clientlist.end())
1150 /* Couldn't find it */
1153 return iter->second;
1156 userrec* Find(const char* nick)
1158 user_hash::iterator iter;
1163 iter = clientlist.find(nick);
1165 if (iter == clientlist.end())
1168 return iter->second;
1171 /* find a channel record by channel name and return a pointer to it */
1173 chanrec* FindChan(const char* chan)
1175 chan_hash::iterator iter;
1179 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1183 iter = chanlist.find(chan);
1185 if (iter == chanlist.end())
1186 /* Couldn't find it */
1189 return iter->second;
1193 long GetMaxBans(char* name)
1196 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1199 if (match(name,x.c_str()))
1207 void purge_empty_chans(userrec* u)
1209 std::vector<chanrec*> to_delete;
1211 // firstly decrement the count on each channel
1212 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1214 if (((ucrec*)(*f))->channel)
1216 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1218 /* No users left in here, mark it for deletion */
1219 to_delete.push_back(((ucrec*)(*f))->channel);
1220 ((ucrec*)(*f))->channel = NULL;
1225 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1227 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1229 chanrec* thischan = (chanrec*)*n;
1230 chan_hash::iterator i2 = chanlist.find(thischan->name);
1231 if (i2 != chanlist.end())
1233 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1244 char* chanmodes(chanrec *chan, bool showkey)
1246 static char scratch[MAXBUF];
1247 static char sparam[MAXBUF];
1248 char* offset = scratch;
1249 std::string extparam = "";
1253 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1261 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1262 for(int n = 0; n < 64; n++)
1271 extparam = (showkey ? chan->key : "<key>");
1274 extparam = ConvToStr(chan->limit);
1282 /* We know these have no parameters */
1285 extparam = chan->GetModeParameter(n+65);
1290 charlcat(sparam,' ',MAXBUF);
1291 strlcat(sparam,extparam.c_str(),MAXBUF);
1296 /* Null terminate scratch */
1298 strlcat(scratch,sparam,MAXBUF);
1303 /* compile a userlist of a channel into a string, each nick seperated by
1304 * spaces and op, voice etc status shown as @ and + */
1306 void userlist(userrec *user,chanrec *c)
1308 if ((!c) || (!user))
1310 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1315 size_t dlen, curlen;
1317 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1320 char* ptr = list + dlen;
1322 CUList *ulist= c->GetUsers();
1324 /* Improvement by Brain - this doesnt change in value, so why was it inside
1327 bool has_user = c->HasUser(user);
1329 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1331 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
1334 * user is +i, and source not on the channel, does not show
1335 * nick in NAMES list
1340 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1347 if (curlen > (480-NICKMAX))
1349 /* list overflowed into multiple numerics */
1350 WriteServ_NoFormat(user->fd,list);
1352 /* reset our lengths */
1353 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1361 /* if whats left in the list isnt empty, send it */
1364 WriteServ_NoFormat(user->fd,list);
1369 * return a count of the users on a specific channel accounting for
1370 * invisible users who won't increase the count. e.g. for /LIST
1372 int usercount_i(chanrec *c)
1379 CUList *ulist= c->GetUsers();
1380 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1382 if (!(i->second->modes[UM_INVISIBLE]))
1389 int usercount(chanrec *c)
1391 return (c ? c->GetUserCounter() : 0);
1395 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1396 * NOTE: If the <ALLOW> or <DENY> tag specifies an ip, and this user resolves,
1397 * then their ip will be taken as 'priority' anyway, so for example,
1398 * <connect allow="127.0.0.1"> will match joe!bloggs@localhost
1400 ConnectClass GetClass(userrec *user)
1402 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1404 if ((match(inet_ntoa(user->ip4),i->host.c_str())) || (match(user->host,i->host.c_str())))
1410 return *(Config->Classes.begin());
1414 * sends out an error notice to all connected clients (not to be used
1417 void send_error(char *s)
1419 log(DEBUG,"send_error: %s",s);
1421 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1423 userrec* t = (userrec*)(*i);
1424 if (t->registered == 7)
1426 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1430 // fix - unregistered connections receive ERROR, not NOTICE
1431 Write(t->fd,"ERROR :%s",s);
1436 void Error(int status)
1442 signal(SIGALRM, SIG_IGN);
1443 signal(SIGPIPE, SIG_IGN);
1444 signal(SIGTERM, SIG_IGN);
1445 signal(SIGABRT, SIG_IGN);
1446 signal(SIGSEGV, SIG_IGN);
1447 signal(SIGURG, SIG_IGN);
1448 signal(SIGKILL, SIG_IGN);
1449 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1451 log(DEFAULT,"Please report the backtrace lines shown below with any bugreport to the bugtracker at http://www.inspircd.org/bugtrack/");
1452 size = backtrace(array, 30);
1453 strings = backtrace_symbols(array, size);
1454 for (size_t i = 0; i < size; i++) {
1455 log(DEFAULT,"[%d] %s", i, strings[i]);
1458 WriteOpers("*** SIGSEGV: Please see the ircd.log for backtrace and report the error to http://www.inspircd.org/bugtrack/");
1460 log(DEFAULT,"You do not have execinfo.h so i could not backtrace -- on FreeBSD, please install the libexecinfo port.");
1462 send_error("Somebody screwed up... Whoops. IRC Server terminating.");
1463 signal(SIGSEGV, SIG_DFL);
1464 if (raise(SIGSEGV) == -1)
1466 log(DEFAULT,"What the hell, i couldnt re-raise SIGSEGV! Error: %s",strerror(errno));
1471 // this function counts all users connected, wether they are registered or NOT.
1474 return clientlist.size();
1477 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1478 int registered_usercount(void)
1482 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1484 if (i->second->registered == 7) c++;
1490 int usercount_invisible(void)
1494 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1496 if ((i->second->registered == 7) && (i->second->modes[UM_INVISIBLE]))
1503 int usercount_opers(void)
1507 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1509 if (*(i->second->oper))
1515 int usercount_unknown(void)
1519 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1521 userrec* t = (userrec*)(*i);
1522 if (t->registered != 7)
1529 long chancount(void)
1531 return chanlist.size();
1538 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1540 userrec* t = (userrec*)(*i);
1541 if (t->registered == 7)
1548 void ShowMOTD(userrec *user)
1550 static char mbuf[MAXBUF];
1551 static char crud[MAXBUF];
1553 if (!Config->MOTD.size())
1555 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1558 WriteServ(user->fd,"375 %s :%s message of the day", user->nick, Config->ServerName);
1560 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1561 WriteServ(user->fd,"372 %s :- ",user->nick,Config->MOTD[i].c_str());
1563 WriteServ(user->fd,"376 %s :End of message of the day.", user->nick);
1566 void ShowRULES(userrec *user)
1568 if (!Config->RULES.size())
1570 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1573 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1575 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1576 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1578 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1581 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1582 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1583 // registration timeout maximum seconds)
1584 bool AllModulesReportReady(userrec* user)
1586 if (!Config->global_implementation[I_OnCheckReady])
1589 for (int i = 0; i <= MODCOUNT; i++)
1591 if (Config->implement_lists[i][I_OnCheckReady])
1593 int res = modules[i]->OnCheckReady(user);
1602 /* Make Sure Modules Are Avaliable!
1603 * (BugFix By Craig.. See? I do work! :p)
1604 * Modified by brain, requires const char*
1605 * to work with other API functions
1609 bool FileExists (const char* file)
1612 if ((input = fopen (file, "r")) == NULL)
1623 char* CleanFilename(char* name)
1625 char* p = name + strlen(name);
1626 while ((p != name) && (*p != '/')) p--;
1627 return (p != name ? ++p : p);
1630 bool DirValid(char* dirandfile)
1633 char buffer[MAXBUF];
1634 char otherdir[MAXBUF];
1637 strlcpy(work, dirandfile, MAXBUF);
1640 // we just want the dir
1652 // Get the current working directory
1653 if (getcwd(buffer, MAXBUF ) == NULL )
1658 if (getcwd(otherdir, MAXBUF ) == NULL )
1663 size_t t = strlen(work);
1665 if (strlen(otherdir) >= t)
1669 if (!strcmp(otherdir,work))
1682 std::string GetFullProgDir(char** argv, int argc)
1685 char buffer[MAXBUF];
1686 char otherdir[MAXBUF];
1689 strlcpy(work,argv[0],MAXBUF);
1692 // we just want the dir
1704 // Get the current working directory
1705 if (getcwd(buffer, MAXBUF) == NULL)
1710 if (getcwd(otherdir, MAXBUF) == NULL)
1717 int InsertMode(std::string &output, const char* mode, unsigned short section)
1719 unsigned short currsection = 1;
1720 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1722 if(section > 4 || section == 0)
1724 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1728 for(; pos < output.size(); pos++)
1730 if(section == currsection)
1733 if(output[pos] == ',')
1737 output.insert(pos, mode);
1741 bool IsValidChannelName(const char *chname)
1745 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1746 if (!chname || *chname != '#')
1751 c = (char *)chname + 1;
1765 /* too long a name - note funky pointer arithmetic here. */
1766 if ((c - chname) > CHANMAX)
1774 inline int charlcat(char* x,char y,int z)
1791 bool charremove(char* mp, char remove)
1794 bool shift_down = false;
1798 if (*mptr == remove)
1810 void OpenLog(char** argv, int argc)
1814 if (Config->logpath == "")
1816 Config->logpath = GetFullProgDir(argv,argc) + "/ircd.log";
1821 Config->log_file = fopen(LOG_FILE,"a+");
1823 if (!Config->log_file)
1825 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1832 Config->log_file = fopen(Config->logpath.c_str(),"a+");
1834 if (!Config->log_file)
1836 printf("ERROR: Could not write to logfile %s, bailing!\n\n",Config->logpath.c_str());
1845 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
1846 log(DEFAULT,"InspIRCd: startup: not starting with UID 0!");
1853 if (*Config->DieValue)
1855 printf("WARNING: %s\n\n",Config->DieValue);
1856 log(DEFAULT,"Uh-Oh, somebody didn't read their config file: '%s'",Config->DieValue);
1861 /* We must load the modules AFTER initializing the socket engine, now */
1862 void LoadAllModules(InspIRCd* ServerInstance)
1864 char configToken[MAXBUF];
1865 Config->module_names.clear();
1868 for (int count = 0; count < Config->ConfValueEnum(Config->config_data, "module"); count++)
1870 Config->ConfValue(Config->config_data, "module","name",count,configToken,MAXBUF);
1871 printf("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
1873 if (!ServerInstance->LoadModule(configToken))
1875 log(DEFAULT,"Exiting due to a module loader error.");
1876 printf("\nThere was an error loading a module: %s\n\n",ServerInstance->ModuleError());
1881 log(DEFAULT,"Total loaded modules: %lu",(unsigned long)MODCOUNT+1);