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 extern std::vector<userrec*> local_users;
66 static char TIMESTR[26];
67 static time_t LAST = 0;
70 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
71 * is greater than the configured loglevel.
73 void log(int level, char *text, ...)
76 char textbuffer[MAXBUF];
78 if (level < Config->LogLevel)
83 struct tm *timeinfo = localtime(&TIME);
85 strlcpy(TIMESTR,asctime(timeinfo),26);
92 va_start(argsPtr, text);
93 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
97 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
101 printf("%s %s\n", TIMESTR, textbuffer);
107 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
109 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
111 void readfile(file_cache &F, const char* fname)
114 char linebuf[MAXBUF];
116 log(DEBUG,"readfile: loading %s",fname);
118 file = fopen(fname,"r");
124 fgets(linebuf,sizeof(linebuf),file);
125 linebuf[strlen(linebuf)-1]='\0';
134 F.push_back(linebuf);
142 log(DEBUG,"readfile: failed to load file: %s",fname);
145 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
149 * Writes a given string in `text' to the socket on fd `sock' - only if the socket
150 * is a valid entry in the local FD table.
152 void Write_NoFormat(int sock, const char *text)
157 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
160 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
163 if (fd_ref_table[sock])
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 va_start(argsPtr, text);
206 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
208 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
211 if (fd_ref_table[sock])
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 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
248 if (fd_ref_table[sock])
250 if (Config->GetIOHook(fd_ref_table[sock]->port))
254 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
256 catch (ModuleException& modexcept)
258 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
263 fd_ref_table[sock]->AddWriteBuf(tb);
265 ServerInstance->stats->statsSent += bytes;
268 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
272 * Same as Write(), except `text' is prefixed with `:server.name '.
274 void WriteServ(int sock, char* text, ...)
277 char textbuffer[MAXBUF];
281 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
286 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
290 va_start(argsPtr, text);
291 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
293 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
296 if (fd_ref_table[sock])
298 if (Config->GetIOHook(fd_ref_table[sock]->port))
302 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
304 catch (ModuleException& modexcept)
306 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
311 fd_ref_table[sock]->AddWriteBuf(tb);
314 ServerInstance->stats->statsSent += bytes;
317 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
320 /** WriteFrom_NoFormat()
321 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
322 * the nick, user, and host of `user'.
324 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
329 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
332 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
335 if (fd_ref_table[sock])
337 if (Config->GetIOHook(fd_ref_table[sock]->port))
341 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
343 catch (ModuleException& modexcept)
345 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
350 fd_ref_table[sock]->AddWriteBuf(tb);
352 ServerInstance->stats->statsSent += bytes;
355 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
358 /* write text from an originating user to originating user */
360 void WriteFrom(int sock, userrec *user,char* text, ...)
363 char textbuffer[MAXBUF];
367 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
370 if ((!text) || (!user))
372 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
376 va_start(argsPtr, text);
377 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
379 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
382 if (fd_ref_table[sock])
384 if (Config->GetIOHook(fd_ref_table[sock]->port))
388 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
390 catch (ModuleException& modexcept)
392 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
397 fd_ref_table[sock]->AddWriteBuf(tb);
400 ServerInstance->stats->statsSent += bytes;
403 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
406 /* write text to an destination user from a source user (e.g. user privmsg) */
408 void WriteTo(userrec *source, userrec *dest,char *data, ...)
410 char textbuffer[MAXBUF];
413 if ((!dest) || (!data))
415 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
422 va_start(argsPtr, data);
423 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
427 // if no source given send it from the server.
430 WriteServ_NoFormat(dest->fd,textbuffer);
434 WriteFrom_NoFormat(dest->fd,source,textbuffer);
438 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
440 if ((!dest) || (!data))
445 WriteServ_NoFormat(dest->fd,data);
449 WriteFrom_NoFormat(dest->fd,source,data);
453 /* write formatted text from a source user to all users on a channel
454 * including the sender (NOT for privmsg, notice etc!) */
456 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
458 char textbuffer[MAXBUF];
462 if ((!Ptr) || (!user) || (!text))
464 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
468 va_start(argsPtr, text);
469 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
472 ulist = Ptr->GetUsers();
474 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
476 if (i->second->fd != FD_MAGIC_NUMBER)
477 WriteTo_NoFormat(user,i->second,textbuffer);
481 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
485 if ((!Ptr) || (!user) || (!text))
487 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
491 ulist = Ptr->GetUsers();
493 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
495 if (i->second->fd != FD_MAGIC_NUMBER)
496 WriteTo_NoFormat(user,i->second,text);
501 /* write formatted text from a source user to all users on a channel
502 * including the sender (NOT for privmsg, notice etc!) doesnt send to
503 * users on remote servers */
505 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
507 char textbuffer[MAXBUF];
511 if ((!Ptr) || (!text))
513 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
517 va_start(argsPtr, text);
518 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
521 ulist = Ptr->GetUsers();
523 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
525 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
529 WriteServ_NoFormat(i->second->fd,textbuffer);
533 WriteTo_NoFormat(user,i->second,textbuffer);
539 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
543 if ((!Ptr) || (!text))
545 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
549 ulist = Ptr->GetUsers();
551 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
553 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
557 WriteServ_NoFormat(i->second->fd,text);
561 WriteTo_NoFormat(user,i->second,text);
569 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
571 char textbuffer[MAXBUF];
575 if ((!Ptr) || (!text))
577 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
581 va_start(argsPtr, text);
582 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
585 ulist = Ptr->GetUsers();
587 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
589 if (IS_LOCAL(i->second))
590 WriteServ_NoFormat(i->second->fd,textbuffer);
594 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
598 if ((!Ptr) || (!text))
600 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
604 ulist = Ptr->GetUsers();
606 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
608 if (IS_LOCAL(i->second))
609 WriteServ_NoFormat(i->second->fd,text);
615 /* write formatted text from a source user to all users on a channel except
616 * for the sender (for privmsg etc) */
618 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
620 char textbuffer[MAXBUF];
624 if ((!Ptr) || (!user) || (!text))
626 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
630 va_start(argsPtr, text);
631 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
637 ulist = Ptr->GetOppedUsers();
640 ulist = Ptr->GetHalfoppedUsers();
643 ulist = Ptr->GetVoicedUsers();
646 ulist = Ptr->GetUsers();
650 log(DEBUG,"%d users to write to",ulist->size());
652 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
654 if ((IS_LOCAL(i->second)) && (user != i->second))
655 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
659 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
663 if ((!Ptr) || (!user) || (!text))
665 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
672 ulist = Ptr->GetOppedUsers();
675 ulist = Ptr->GetHalfoppedUsers();
678 ulist = Ptr->GetVoicedUsers();
681 ulist = Ptr->GetUsers();
685 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
687 if ((IS_LOCAL(i->second)) && (user != i->second))
688 WriteFrom_NoFormat(i->second->fd,user,text);
692 std::string GetServerDescription(char* servername)
694 std::string description = "";
696 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
698 if (description != "")
704 // not a remote server that can be found, it must be me.
705 return Config->ServerDesc;
709 /* write a formatted string to all users who share at least one common
710 * channel, including the source user e.g. for use in NICK */
712 void WriteCommon(userrec *u, char* text, ...)
714 char textbuffer[MAXBUF];
716 bool sent_to_at_least_one = false;
720 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
724 if (u->registered != 7)
726 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
730 va_start(argsPtr, text);
731 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
734 // FIX: Stops a message going to the same person more than once
735 memset(&already_sent,0,MAX_DESCRIPTORS);
737 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
739 if (((ucrec*)(*v))->channel)
741 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
743 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
745 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
747 already_sent[i->second->fd] = 1;
748 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
749 sent_to_at_least_one = true;
756 * if the user was not in any channels, no users will receive the text. Make sure the user
757 * receives their OWN message for WriteCommon
759 if (!sent_to_at_least_one)
761 WriteFrom_NoFormat(u->fd,u,textbuffer);
765 void WriteCommon_NoFormat(userrec *u, const char* text)
767 bool sent_to_at_least_one = false;
771 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
775 if (u->registered != 7)
777 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
781 // FIX: Stops a message going to the same person more than once
782 memset(&already_sent,0,MAX_DESCRIPTORS);
784 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
786 if (((ucrec*)(*v))->channel)
788 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
790 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
792 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
794 already_sent[i->second->fd] = 1;
795 WriteFrom_NoFormat(i->second->fd,u,text);
796 sent_to_at_least_one = true;
803 * if the user was not in any channels, no users will receive the text. Make sure the user
804 * receives their OWN message for WriteCommon
806 if (!sent_to_at_least_one)
808 WriteFrom_NoFormat(u->fd,u,text);
813 /* write a formatted string to all users who share at least one common
814 * channel, NOT including the source user e.g. for use in QUIT
817 void WriteCommonExcept(userrec *u, char* text, ...)
819 char textbuffer[MAXBUF];
820 char oper_quit[MAXBUF];
821 bool quit_munge = false;
827 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
831 if (u->registered != 7)
833 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
837 va_start(argsPtr, text);
838 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
841 if ((Config->HideSplits) && (total > 6))
843 /* Yeah yeah, this is ugly. But its fast, live with it. */
844 char* check = textbuffer;
846 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
848 std::stringstream split(check);
849 std::string server_one;
850 std::string server_two;
855 if ((FindServerName(server_one)) && (FindServerName(server_two)))
857 strlcpy(oper_quit,textbuffer,MAXQUIT);
858 strlcpy(check,"*.net *.split",MAXQUIT);
864 if ((Config->HideBans) && (total > 13) && (!quit_munge))
866 char* check = textbuffer;
869 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
873 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
875 strlcpy(oper_quit,textbuffer,MAXQUIT);
876 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
882 memset(&already_sent,0,MAX_DESCRIPTORS);
884 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
886 if (((ucrec*)(*v))->channel)
888 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
890 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
894 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
896 already_sent[i->second->fd] = 1;
900 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
903 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
911 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
915 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
919 if (u->registered != 7)
921 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
925 memset(&already_sent,0,MAX_DESCRIPTORS);
927 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
929 if (((ucrec*)(*v))->channel)
931 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
933 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
937 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
939 already_sent[i->second->fd] = 1;
940 WriteFrom_NoFormat(i->second->fd,u,text);
949 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
950 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
951 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
952 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
953 * uses the oper list, which means if you have 2000 users but only 5 opers,
954 * it iterates 5 times.
956 void WriteOpers(char* text, ...)
958 char textbuffer[MAXBUF];
963 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
967 va_start(argsPtr, text);
968 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
971 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
977 if (a->modebits & UM_SERVERNOTICE)
979 // send server notices to all with +s
980 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
986 void ServerNoticeAll(char* text, ...)
991 char textbuffer[MAXBUF];
992 char formatbuffer[MAXBUF];
994 va_start (argsPtr, text);
995 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
998 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1000 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1002 userrec* t = (userrec*)(*i);
1003 WriteServ_NoFormat(t->fd,formatbuffer);
1007 void ServerPrivmsgAll(char* text, ...)
1012 char textbuffer[MAXBUF];
1013 char formatbuffer[MAXBUF];
1015 va_start (argsPtr, text);
1016 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1019 snprintf(formatbuffer,MAXBUF,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1021 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1023 userrec* t = (userrec*)(*i);
1024 WriteServ_NoFormat(t->fd,formatbuffer);
1028 void WriteMode(const char* modes, int flags, const char* text, ...)
1030 char textbuffer[MAXBUF];
1034 if ((!text) || (!modes) || (!flags))
1036 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1040 va_start(argsPtr, text);
1041 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1043 modelen = strlen(modes);
1045 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1047 userrec* t = (userrec*)(*i);
1048 bool send_to_user = false;
1050 if (flags == WM_AND)
1052 send_to_user = true;
1054 for (int n = 0; n < modelen; n++)
1056 if (!hasumode(t,modes[n]))
1058 send_to_user = false;
1063 else if (flags == WM_OR)
1065 send_to_user = false;
1067 for (int n = 0; n < modelen; n++)
1069 if (hasumode(t,modes[n]))
1071 send_to_user = true;
1079 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1084 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1086 char textbuffer[MAXBUF];
1087 char formatbuffer[MAXBUF];
1090 if ((!text) || (!source))
1092 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1096 va_start(argsPtr, text);
1097 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1100 snprintf(formatbuffer,MAXBUF,"NOTICE $* :%s",textbuffer);
1102 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1104 userrec* t = (userrec*)(*i);
1105 WriteFrom_NoFormat(t->fd,source,formatbuffer);
1110 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1112 char textbuffer[MAXBUF];
1113 char formatbuffer[MAXBUF];
1116 if ((!text) || (!source))
1118 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1122 va_start(argsPtr, text);
1123 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1126 snprintf(formatbuffer,MAXBUF,"WALLOPS :%s",textbuffer);
1128 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1130 userrec* t = (userrec*)(*i);
1132 if ((IS_LOCAL(t)) && (t->modebits & UM_WALLOPS))
1134 WriteTo_NoFormat(source,t,formatbuffer);
1139 /* convert a string to lowercase. Note following special circumstances
1140 * taken from RFC 1459. Many "official" server branches still hold to this
1141 * rule so i will too;
1143 * Because of IRC's scandanavian origin, the characters {}| are
1144 * considered to be the lower case equivalents of the characters []\,
1145 * respectively. This is a critical issue when determining the
1146 * equivalence of two nicknames.
1148 void strlower(char *n)
1152 for (char* t = n; *t; t++)
1153 *t = lowermap[(unsigned)*t];
1157 /* Find a user record by nickname and return a pointer to it */
1159 userrec* Find(const std::string &nick)
1161 user_hash::iterator iter = clientlist.find(nick);
1163 if (iter == clientlist.end())
1164 /* Couldn't find it */
1167 return iter->second;
1170 userrec* Find(const char* nick)
1172 user_hash::iterator iter;
1177 iter = clientlist.find(nick);
1179 if (iter == clientlist.end())
1182 return iter->second;
1185 /* find a channel record by channel name and return a pointer to it */
1187 chanrec* FindChan(const char* chan)
1189 chan_hash::iterator iter;
1193 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1197 iter = chanlist.find(chan);
1199 if (iter == chanlist.end())
1200 /* Couldn't find it */
1203 return iter->second;
1207 long GetMaxBans(char* name)
1210 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1213 if (match(name,x.c_str()))
1221 void purge_empty_chans(userrec* u)
1223 std::vector<chanrec*> to_delete;
1225 // firstly decrement the count on each channel
1226 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1228 if (((ucrec*)(*f))->channel)
1230 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1232 /* No users left in here, mark it for deletion */
1233 to_delete.push_back(((ucrec*)(*f))->channel);
1234 ((ucrec*)(*f))->channel = NULL;
1239 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1241 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1243 chanrec* thischan = (chanrec*)*n;
1244 chan_hash::iterator i2 = chanlist.find(thischan->name);
1245 if (i2 != chanlist.end())
1247 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1258 char* chanmodes(chanrec *chan, bool showkey)
1260 static char scratch[MAXBUF];
1261 static char sparam[MAXBUF];
1262 char* offset = scratch;
1266 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1274 if (chan->binarymodes & CM_NOEXTERNAL)
1276 if (chan->binarymodes & CM_TOPICLOCK)
1282 if (chan->binarymodes & CM_INVITEONLY)
1284 if (chan->binarymodes & CM_MODERATED)
1286 if (chan->binarymodes & CM_SECRET)
1288 if (chan->binarymodes & CM_PRIVATE)
1293 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1299 sprintf(foo," %lu",(unsigned long)chan->limit);
1300 strlcat(sparam,foo,MAXBUF);
1303 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1304 for(int n = 0; n < 64; n++)
1306 if(chan->custom_modes[n])
1309 std::string extparam = chan->GetModeParameter(n+65);
1313 charlcat(sparam,' ',MAXBUF);
1314 strlcat(sparam,extparam.c_str(),MAXBUF);
1319 /* Null terminate scratch */
1321 strlcat(scratch,sparam,MAXMODES);
1326 /* compile a userlist of a channel into a string, each nick seperated by
1327 * spaces and op, voice etc status shown as @ and + */
1329 void userlist(userrec *user,chanrec *c)
1331 if ((!c) || (!user))
1333 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1338 size_t dlen, curlen;
1340 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1343 char* ptr = list + dlen;
1345 CUList *ulist= c->GetUsers();
1347 /* Improvement by Brain - this doesnt change in value, so why was it inside
1350 bool has_user = c->HasUser(user);
1352 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1354 if ((!has_user) && (i->second->modebits & UM_INVISIBLE))
1357 * user is +i, and source not on the channel, does not show
1358 * nick in NAMES list
1363 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", cmode(i->second, c), i->second->nick);
1370 if (curlen > (480-NICKMAX))
1372 /* list overflowed into multiple numerics */
1373 WriteServ_NoFormat(user->fd,list);
1375 /* reset our lengths */
1376 dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1384 /* if whats left in the list isnt empty, send it */
1387 WriteServ_NoFormat(user->fd,list);
1392 * return a count of the users on a specific channel accounting for
1393 * invisible users who won't increase the count. e.g. for /LIST
1395 int usercount_i(chanrec *c)
1402 CUList *ulist= c->GetUsers();
1403 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1405 if (!(i->second->modebits & UM_INVISIBLE))
1412 int usercount(chanrec *c)
1414 return (c ? c->GetUserCounter() : 0);
1418 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1419 ConnectClass GetClass(userrec *user)
1421 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1423 if (match(user->host,i->host.c_str()))
1429 return *(Config->Classes.begin());
1433 * sends out an error notice to all connected clients (not to be used
1436 void send_error(char *s)
1438 log(DEBUG,"send_error: %s",s);
1440 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1442 userrec* t = (userrec*)(*i);
1443 if (t->registered == 7)
1445 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1449 // fix - unregistered connections receive ERROR, not NOTICE
1450 Write(t->fd,"ERROR :%s",s);
1455 void Error(int status)
1457 signal(SIGALRM, SIG_IGN);
1458 signal(SIGPIPE, SIG_IGN);
1459 signal(SIGTERM, SIG_IGN);
1460 signal(SIGABRT, SIG_IGN);
1461 signal(SIGSEGV, SIG_IGN);
1462 signal(SIGURG, SIG_IGN);
1463 signal(SIGKILL, SIG_IGN);
1464 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1465 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1469 // this function counts all users connected, wether they are registered or NOT.
1472 return clientlist.size();
1475 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1476 int registered_usercount(void)
1480 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1482 if (i->second->registered == 7) c++;
1488 int usercount_invisible(void)
1492 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1494 if ((i->second->registered == 7) && (i->second->modebits & UM_INVISIBLE))
1501 int usercount_opers(void)
1505 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1507 if (i->second->oper)
1513 int usercount_unknown(void)
1517 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1519 userrec* t = (userrec*)(*i);
1520 if (t->registered != 7)
1527 long chancount(void)
1529 return chanlist.size();
1536 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1538 userrec* t = (userrec*)(*i);
1539 if (t->registered == 7)
1546 void ShowMOTD(userrec *user)
1548 static char mbuf[MAXBUF];
1549 static char crud[MAXBUF];
1550 std::string WholeMOTD = "";
1552 if (!Config->MOTD.size())
1554 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1558 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1559 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1560 WholeMOTD = WholeMOTD + mbuf;
1562 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1563 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1565 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1566 WholeMOTD = WholeMOTD + mbuf;
1568 // only one write operation
1569 if (Config->GetIOHook(user->port))
1573 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1575 catch (ModuleException& modexcept)
1577 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1582 user->AddWriteBuf(WholeMOTD);
1585 ServerInstance->stats->statsSent += WholeMOTD.length();
1588 void ShowRULES(userrec *user)
1590 if (!Config->RULES.size())
1592 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1595 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1597 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1598 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1600 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1603 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1604 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1605 // registration timeout maximum seconds)
1606 bool AllModulesReportReady(userrec* user)
1608 if (!Config->global_implementation[I_OnCheckReady])
1611 for (int i = 0; i <= MODCOUNT; i++)
1613 if (Config->implement_lists[i][I_OnCheckReady])
1615 int res = modules[i]->OnCheckReady(user);
1624 bool DirValid(char* dirandfile)
1627 char buffer[MAXBUF];
1628 char otherdir[MAXBUF];
1631 strlcpy(work, dirandfile, MAXBUF);
1634 // we just want the dir
1646 // Get the current working directory
1647 if (getcwd(buffer, MAXBUF ) == NULL )
1652 if (getcwd(otherdir, MAXBUF ) == NULL )
1657 size_t t = strlen(work);
1659 if (strlen(otherdir) >= t)
1663 if (!strcmp(otherdir,work))
1676 std::string GetFullProgDir(char** argv, int argc)
1679 char buffer[MAXBUF];
1680 char otherdir[MAXBUF];
1683 strlcpy(work,argv[0],MAXBUF);
1686 // we just want the dir
1698 // Get the current working directory
1699 if (getcwd(buffer, MAXBUF) == NULL)
1704 if (getcwd(otherdir, MAXBUF) == NULL)
1711 int InsertMode(std::string &output, const char* mode, unsigned short section)
1713 unsigned short currsection = 1;
1714 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1716 if(section > 4 || section == 0)
1718 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1722 for(; pos < output.size(); pos++)
1724 if(section == currsection)
1727 if(output[pos] == ',')
1731 output.insert(pos, mode);
1735 bool IsValidChannelName(const char *chname)
1739 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1740 if (!chname || *chname != '#')
1745 c = (char *)chname + 1;
1759 /* too long a name - note funky pointer arithmetic here. */
1760 if ((c - chname) > CHANMAX)
1768 inline int charlcat(char* x,char y,int z)
1785 bool charremove(char* mp, char remove)
1788 bool shift_down = false;
1792 if (*mptr == remove)