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 static char list[MAXBUF];
59 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
60 static char already_sent[MAX_DESCRIPTORS];
61 extern std::vector<userrec*> all_opers;
62 extern user_hash clientlist;
63 extern chan_hash chanlist;
65 extern std::vector<userrec*> local_users;
67 static char TIMESTR[26];
68 static time_t LAST = 0;
71 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
72 * is greater than the configured loglevel.
74 void log(int level, char *text, ...)
77 char textbuffer[MAXBUF];
79 if (level < Config->LogLevel)
84 struct tm *timeinfo = localtime(&TIME);
86 strlcpy(TIMESTR,asctime(timeinfo),26);
93 va_start(argsPtr, text);
94 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
98 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
102 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 bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
164 if (fd_ref_table[sock])
166 if (Config->GetIOHook(fd_ref_table[sock]->port))
170 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
172 catch (ModuleException& modexcept)
174 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
179 fd_ref_table[sock]->AddWriteBuf(tb);
181 ServerInstance->stats->statsSent += bytes;
184 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
188 * Same as Write_NoFormat(), but formatted printf() style first.
190 void Write(int sock, char *text, ...)
193 char textbuffer[MAXBUF];
197 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
202 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
206 va_start(argsPtr, text);
207 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
209 bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
212 if (fd_ref_table[sock])
214 if (Config->GetIOHook(fd_ref_table[sock]->port))
218 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
220 catch (ModuleException& modexcept)
222 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
227 fd_ref_table[sock]->AddWriteBuf(tb);
229 ServerInstance->stats->statsSent += bytes;
232 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
235 /** WriteServ_NoFormat()
236 * Same as Write_NoFormat(), except prefixes `text' with `:server.name '.
238 void WriteServ_NoFormat(int sock, const char* text)
243 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
246 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
249 if (fd_ref_table[sock])
251 if (Config->GetIOHook(fd_ref_table[sock]->port))
255 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
257 catch (ModuleException& modexcept)
259 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
264 fd_ref_table[sock]->AddWriteBuf(tb);
266 ServerInstance->stats->statsSent += bytes;
269 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
273 * Same as Write(), except `text' is prefixed with `:server.name '.
275 void WriteServ(int sock, char* text, ...)
278 char textbuffer[MAXBUF];
282 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
287 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
291 va_start(argsPtr, text);
292 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
294 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
297 if (fd_ref_table[sock])
299 if (Config->GetIOHook(fd_ref_table[sock]->port))
303 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
305 catch (ModuleException& modexcept)
307 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
312 fd_ref_table[sock]->AddWriteBuf(tb);
315 ServerInstance->stats->statsSent += bytes;
318 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
321 /** WriteFrom_NoFormat()
322 * Write `text' to a socket with fd `sock' prefixed with `:n!u@h' - taken from
323 * the nick, user, and host of `user'.
325 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
330 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
333 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
336 if (fd_ref_table[sock])
338 if (Config->GetIOHook(fd_ref_table[sock]->port))
342 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
344 catch (ModuleException& modexcept)
346 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
351 fd_ref_table[sock]->AddWriteBuf(tb);
353 ServerInstance->stats->statsSent += bytes;
356 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
359 /* write text from an originating user to originating user */
361 void WriteFrom(int sock, userrec *user,char* text, ...)
364 char textbuffer[MAXBUF];
368 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
371 if ((!text) || (!user))
373 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
377 va_start(argsPtr, text);
378 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
380 bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
383 if (fd_ref_table[sock])
385 if (Config->GetIOHook(fd_ref_table[sock]->port))
389 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
391 catch (ModuleException& modexcept)
393 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
398 fd_ref_table[sock]->AddWriteBuf(tb);
401 ServerInstance->stats->statsSent += bytes;
404 log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
407 /* write text to an destination user from a source user (e.g. user privmsg) */
409 void WriteTo(userrec *source, userrec *dest,char *data, ...)
411 char textbuffer[MAXBUF];
414 if ((!dest) || (!data))
416 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
423 va_start(argsPtr, data);
424 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
428 // if no source given send it from the server.
431 WriteServ_NoFormat(dest->fd,textbuffer);
435 WriteFrom_NoFormat(dest->fd,source,textbuffer);
439 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
441 if ((!dest) || (!data))
446 WriteServ_NoFormat(dest->fd,data);
450 WriteFrom_NoFormat(dest->fd,source,data);
454 /* write formatted text from a source user to all users on a channel
455 * including the sender (NOT for privmsg, notice etc!) */
457 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
459 char textbuffer[MAXBUF];
461 std::map<char*,char*> *ulist;
463 if ((!Ptr) || (!user) || (!text))
465 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
469 va_start(argsPtr, text);
470 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
473 ulist = Ptr->GetUsers();
475 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
477 userrec* otheruser = (userrec*)i->second;
479 if (otheruser->fd != FD_MAGIC_NUMBER)
480 WriteTo_NoFormat(user,otheruser,textbuffer);
484 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
486 std::map<char*,char*> *ulist;
488 if ((!Ptr) || (!user) || (!text))
490 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
494 ulist = Ptr->GetUsers();
496 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
498 userrec* otheruser = (userrec*)i->second;
500 if (otheruser->fd != FD_MAGIC_NUMBER)
501 WriteTo_NoFormat(user,otheruser,text);
506 /* write formatted text from a source user to all users on a channel
507 * including the sender (NOT for privmsg, notice etc!) doesnt send to
508 * users on remote servers */
510 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
512 char textbuffer[MAXBUF];
514 std::map<char*,char*> *ulist;
516 if ((!Ptr) || (!text))
518 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
522 va_start(argsPtr, text);
523 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
526 ulist = Ptr->GetUsers();
528 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
530 userrec* otheruser = (userrec*)i->second;
532 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
536 WriteServ_NoFormat(otheruser->fd,textbuffer);
540 WriteTo_NoFormat(user,otheruser,textbuffer);
546 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
548 std::map<char*,char*> *ulist;
550 if ((!Ptr) || (!text))
552 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
556 ulist = Ptr->GetUsers();
558 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
560 userrec* otheruser = (userrec*)i->second;
562 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
566 WriteServ_NoFormat(otheruser->fd,text);
570 WriteTo_NoFormat(user,otheruser,text);
578 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
580 char textbuffer[MAXBUF];
582 std::map<char*,char*> *ulist;
584 if ((!Ptr) || (!text))
586 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
590 va_start(argsPtr, text);
591 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
594 ulist = Ptr->GetUsers();
596 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
598 userrec* otheruser = (userrec*)i->second;
600 if (IS_LOCAL(otheruser))
601 WriteServ_NoFormat(otheruser->fd,textbuffer);
605 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
607 std::map<char*,char*> *ulist;
609 if ((!Ptr) || (!text))
611 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
615 ulist = Ptr->GetUsers();
617 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
619 userrec* otheruser = (userrec*)i->second;
621 if (IS_LOCAL(otheruser))
622 WriteServ_NoFormat(otheruser->fd,text);
628 /* write formatted text from a source user to all users on a channel except
629 * for the sender (for privmsg etc) */
631 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
633 char textbuffer[MAXBUF];
635 std::map<char*,char*> *ulist;
637 if ((!Ptr) || (!user) || (!text))
639 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
643 va_start(argsPtr, text);
644 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
650 ulist = Ptr->GetOppedUsers();
653 ulist = Ptr->GetHalfoppedUsers();
656 ulist = Ptr->GetVoicedUsers();
659 ulist = Ptr->GetUsers();
663 log(DEBUG,"%d users to write to",ulist->size());
665 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
667 userrec* otheruser = (userrec*)i->second;
669 if ((IS_LOCAL(otheruser)) && (user != otheruser))
670 WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
674 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
676 std::map<char*,char*> *ulist;
678 if ((!Ptr) || (!user) || (!text))
680 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
687 ulist = Ptr->GetOppedUsers();
690 ulist = Ptr->GetHalfoppedUsers();
693 ulist = Ptr->GetVoicedUsers();
696 ulist = Ptr->GetUsers();
700 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
702 userrec* otheruser = (userrec*)i->second;
704 if ((IS_LOCAL(otheruser)) && (user != otheruser))
705 WriteFrom_NoFormat(otheruser->fd,user,text);
709 std::string GetServerDescription(char* servername)
711 std::string description = "";
713 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
715 if (description != "")
721 // not a remote server that can be found, it must be me.
722 return Config->ServerDesc;
726 /* write a formatted string to all users who share at least one common
727 * channel, including the source user e.g. for use in NICK */
729 void WriteCommon(userrec *u, char* text, ...)
731 char textbuffer[MAXBUF];
733 bool sent_to_at_least_one = false;
738 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
742 if (u->registered != 7)
744 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
748 va_start(argsPtr, text);
749 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
752 // FIX: Stops a message going to the same person more than once
753 memset(&already_sent,0,MAX_DESCRIPTORS);
756 for (unsigned int i = 0; i < y; i++)
758 if (u->chans[i].channel)
760 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
762 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
764 userrec* otheruser = (userrec*)i->second;
766 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
768 already_sent[otheruser->fd] = 1;
769 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
770 sent_to_at_least_one = true;
777 * if the user was not in any channels, no users will receive the text. Make sure the user
778 * receives their OWN message for WriteCommon
780 if (!sent_to_at_least_one)
782 WriteFrom_NoFormat(u->fd,u,textbuffer);
786 void WriteCommon_NoFormat(userrec *u, const char* text)
788 bool sent_to_at_least_one = false;
793 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
797 if (u->registered != 7)
799 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
803 // FIX: Stops a message going to the same person more than once
804 memset(&already_sent,0,MAX_DESCRIPTORS);
807 for (unsigned int i = 0; i < y; i++)
809 if (u->chans[i].channel)
811 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
813 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
815 userrec* otheruser = (userrec*)i->second;
817 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
819 already_sent[otheruser->fd] = 1;
820 WriteFrom_NoFormat(otheruser->fd,u,text);
821 sent_to_at_least_one = true;
828 * if the user was not in any channels, no users will receive the text. Make sure the user
829 * receives their OWN message for WriteCommon
831 if (!sent_to_at_least_one)
833 WriteFrom_NoFormat(u->fd,u,text);
838 /* write a formatted string to all users who share at least one common
839 * channel, NOT including the source user e.g. for use in QUIT
842 void WriteCommonExcept(userrec *u, char* text, ...)
844 char textbuffer[MAXBUF];
845 char oper_quit[MAXBUF];
846 bool quit_munge = false;
853 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
857 if (u->registered != 7)
859 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
863 va_start(argsPtr, text);
864 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
867 if ((Config->HideSplits) && (total > 6))
869 /* Yeah yeah, this is ugly. But its fast, live with it. */
870 char* check = textbuffer;
872 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
874 std::stringstream split(check);
875 std::string server_one;
876 std::string server_two;
881 if ((FindServerName(server_one)) && (FindServerName(server_two)))
883 strlcpy(oper_quit,textbuffer,MAXQUIT);
884 strlcpy(check,"*.net *.split",MAXQUIT);
890 if ((Config->HideBans) && (total > 13) && (!quit_munge))
892 char* check = textbuffer;
895 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
899 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
901 strlcpy(oper_quit,textbuffer,MAXQUIT);
902 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
908 memset(&already_sent,0,MAX_DESCRIPTORS);
911 for (unsigned int i = 0; i < y; i++)
913 if (u->chans[i].channel)
915 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
917 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
919 userrec* otheruser = (userrec*)i->second;
923 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
925 already_sent[otheruser->fd] = 1;
929 WriteFrom_NoFormat(otheruser->fd,u,*otheruser->oper ? oper_quit : textbuffer);
932 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
940 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
946 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
950 if (u->registered != 7)
952 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
956 memset(&already_sent,0,MAX_DESCRIPTORS);
959 for (unsigned int i = 0; i < y; i++)
961 if (u->chans[i].channel)
963 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
965 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
967 userrec* otheruser = (userrec*)i->second;
971 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
973 already_sent[otheruser->fd] = 1;
974 WriteFrom_NoFormat(otheruser->fd,u,text);
983 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
984 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
985 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
986 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
987 * uses the oper list, which means if you have 2000 users but only 5 opers,
988 * it iterates 5 times.
990 void WriteOpers(char* text, ...)
992 char textbuffer[MAXBUF];
997 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1001 va_start(argsPtr, text);
1002 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1005 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
1011 if (strchr(a->modes,'s'))
1013 // send server notices to all with +s
1014 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
1020 void ServerNoticeAll(char* text, ...)
1025 char textbuffer[MAXBUF];
1027 va_start (argsPtr, text);
1028 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1031 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1033 userrec* t = (userrec*)(*i);
1034 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1038 void ServerPrivmsgAll(char* text, ...)
1043 char textbuffer[MAXBUF];
1045 va_start (argsPtr, text);
1046 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1049 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1051 userrec* t = (userrec*)(*i);
1052 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1056 void WriteMode(const char* modes, int flags, const char* text, ...)
1058 char textbuffer[MAXBUF];
1062 if ((!text) || (!modes) || (!flags))
1064 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1068 va_start(argsPtr, text);
1069 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1071 modelen = strlen(modes);
1073 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1075 userrec* t = (userrec*)(*i);
1076 bool send_to_user = false;
1078 if (flags == WM_AND)
1080 send_to_user = true;
1082 for (int n = 0; n < modelen; n++)
1084 if (!hasumode(t,modes[n]))
1086 send_to_user = false;
1091 else if (flags == WM_OR)
1093 send_to_user = false;
1095 for (int n = 0; n < modelen; n++)
1097 if (hasumode(t,modes[n]))
1099 send_to_user = true;
1107 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1112 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1114 char textbuffer[MAXBUF];
1117 if ((!text) || (!source))
1119 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1123 va_start(argsPtr, text);
1124 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1127 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1129 userrec* t = (userrec*)(*i);
1130 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
1135 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1137 char textbuffer[MAXBUF];
1140 if ((!text) || (!source))
1142 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1146 va_start(argsPtr, text);
1147 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1150 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1152 userrec* t = (userrec*)(*i);
1154 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1156 WriteTo(source,t,"WALLOPS :%s",textbuffer);
1161 /* convert a string to lowercase. Note following special circumstances
1162 * taken from RFC 1459. Many "official" server branches still hold to this
1163 * rule so i will too;
1165 * Because of IRC's scandanavian origin, the characters {}| are
1166 * considered to be the lower case equivalents of the characters []\,
1167 * respectively. This is a critical issue when determining the
1168 * equivalence of two nicknames.
1170 void strlower(char *n)
1174 for (char* t = n; *t; t++)
1175 *t = lowermap[(unsigned)*t];
1179 /* Find a user record by nickname and return a pointer to it */
1181 userrec* Find(std::string nick)
1183 user_hash::iterator iter = clientlist.find(nick);
1185 if (iter == clientlist.end())
1186 /* Couldn't find it */
1189 return iter->second;
1192 userrec* Find(const char* nick)
1194 user_hash::iterator iter;
1199 iter = clientlist.find(nick);
1201 if (iter == clientlist.end())
1204 return iter->second;
1207 /* find a channel record by channel name and return a pointer to it */
1209 chanrec* FindChan(const char* chan)
1211 chan_hash::iterator iter;
1215 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1219 iter = chanlist.find(chan);
1221 if (iter == chanlist.end())
1222 /* Couldn't find it */
1225 return iter->second;
1229 long GetMaxBans(char* name)
1232 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1235 if (match(name,x.c_str()))
1243 void purge_empty_chans(userrec* u)
1247 // firstly decrement the count on each channel
1248 for (unsigned int f = 0; f < u->chans.size(); f++)
1250 if (u->chans[f].channel)
1252 u->chans[f].channel->DelUser((char*)u);
1256 for (unsigned int i = 0; i < u->chans.size(); i++)
1258 if (u->chans[i].channel)
1260 if (!usercount(u->chans[i].channel))
1262 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1264 /* kill the record */
1265 if (i2 != chanlist.end())
1267 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1271 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1277 u->chans[i].channel = NULL;
1282 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1287 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1292 char* chanmodes(chanrec *chan, bool showkey)
1294 static char scratch[MAXBUF];
1295 static char sparam[MAXBUF];
1296 char* offset = scratch;
1300 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1308 if (chan->binarymodes & CM_NOEXTERNAL)
1310 if (chan->binarymodes & CM_TOPICLOCK)
1316 if (chan->binarymodes & CM_INVITEONLY)
1318 if (chan->binarymodes & CM_MODERATED)
1320 if (chan->binarymodes & CM_SECRET)
1322 if (chan->binarymodes & CM_PRIVATE)
1327 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1333 sprintf(foo," %lu",(unsigned long)chan->limit);
1334 strlcat(sparam,foo,MAXBUF);
1337 for (int n = 0; n < 190; n++)
1339 if (chan->custom_modes[n])
1342 std::string extparam = chan->GetModeParameter(n+65);
1346 charlcat(sparam,' ',MAXBUF);
1347 strlcat(sparam,extparam.c_str(),MAXBUF);
1352 /* Null terminate scratch */
1354 strlcat(scratch,sparam,MAXMODES);
1359 /* compile a userlist of a channel into a string, each nick seperated by
1360 * spaces and op, voice etc status shown as @ and + */
1362 void userlist(userrec *user,chanrec *c)
1364 if ((!c) || (!user))
1366 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1370 size_t dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1371 size_t initial = dlen;
1373 std::map<char*,char*> *ulist= c->GetUsers();
1375 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1377 userrec* otheruser = (userrec*)i->second;
1379 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1382 * user is +i, and source not on the channel, does not show
1383 * nick in NAMES list
1388 dlen += strlcat(list,cmode(otheruser,c),MAXBUF);
1389 dlen += strlcat(list,otheruser->nick,MAXBUF);
1390 charlcat(list,' ',MAXBUF);
1393 if (dlen > (480-NICKMAX))
1395 /* list overflowed into multiple numerics */
1396 WriteServ_NoFormat(user->fd,list);
1397 dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1401 /* if whats left in the list isnt empty, send it */
1402 if (dlen != initial)
1404 WriteServ_NoFormat(user->fd,list);
1409 * return a count of the users on a specific channel accounting for
1410 * invisible users who won't increase the count. e.g. for /LIST
1412 int usercount_i(chanrec *c)
1419 std::map<char*,char*> *ulist= c->GetUsers();
1420 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1422 userrec* user = (userrec*)i->second;
1423 if (!strchr(user->modes,'i'))
1430 int usercount(chanrec *c)
1432 return (c ? c->GetUserCounter() : 0);
1436 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1437 ConnectClass GetClass(userrec *user)
1439 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1441 if (match(user->host,i->host.c_str()))
1447 return *(Config->Classes.begin());
1451 * sends out an error notice to all connected clients (not to be used
1454 void send_error(char *s)
1456 log(DEBUG,"send_error: %s",s);
1458 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1460 userrec* t = (userrec*)(*i);
1461 if (t->registered == 7)
1463 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1467 // fix - unregistered connections receive ERROR, not NOTICE
1468 Write(t->fd,"ERROR :%s",s);
1473 void Error(int status)
1475 signal(SIGALRM, SIG_IGN);
1476 signal(SIGPIPE, SIG_IGN);
1477 signal(SIGTERM, SIG_IGN);
1478 signal(SIGABRT, SIG_IGN);
1479 signal(SIGSEGV, SIG_IGN);
1480 signal(SIGURG, SIG_IGN);
1481 signal(SIGKILL, SIG_IGN);
1482 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1483 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1487 // this function counts all users connected, wether they are registered or NOT.
1490 return clientlist.size();
1493 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1494 int registered_usercount(void)
1498 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1500 if (i->second->registered == 7) c++;
1506 int usercount_invisible(void)
1510 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1512 if ((i->second->registered == 7) && (strchr(i->second->modes,'i')))
1519 int usercount_opers(void)
1523 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1525 if (i->second->oper)
1531 int usercount_unknown(void)
1535 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1537 userrec* t = (userrec*)(*i);
1538 if (t->registered != 7)
1545 long chancount(void)
1547 return chanlist.size();
1554 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1556 userrec* t = (userrec*)(*i);
1557 if (t->registered == 7)
1564 void ShowMOTD(userrec *user)
1566 static char mbuf[MAXBUF];
1567 static char crud[MAXBUF];
1568 std::string WholeMOTD = "";
1570 if (!Config->MOTD.size())
1572 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1576 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1577 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1578 WholeMOTD = WholeMOTD + mbuf;
1580 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1581 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1583 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1584 WholeMOTD = WholeMOTD + mbuf;
1586 // only one write operation
1587 if (Config->GetIOHook(user->port))
1591 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1593 catch (ModuleException& modexcept)
1595 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1600 user->AddWriteBuf(WholeMOTD);
1603 ServerInstance->stats->statsSent += WholeMOTD.length();
1606 void ShowRULES(userrec *user)
1608 if (!Config->RULES.size())
1610 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1613 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1615 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1616 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1618 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1621 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1622 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1623 // registration timeout maximum seconds)
1624 bool AllModulesReportReady(userrec* user)
1626 if (!Config->global_implementation[I_OnCheckReady])
1629 for (int i = 0; i <= MODCOUNT; i++)
1631 if (Config->implement_lists[i][I_OnCheckReady])
1633 int res = modules[i]->OnCheckReady(user);
1642 bool DirValid(char* dirandfile)
1645 char buffer[MAXBUF];
1646 char otherdir[MAXBUF];
1649 strlcpy(work, dirandfile, MAXBUF);
1652 // we just want the dir
1664 // Get the current working directory
1665 if (getcwd(buffer, MAXBUF ) == NULL )
1670 if (getcwd(otherdir, MAXBUF ) == NULL )
1675 size_t t = strlen(work);
1677 if (strlen(otherdir) >= t)
1681 if (!strcmp(otherdir,work))
1694 std::string GetFullProgDir(char** argv, int argc)
1697 char buffer[MAXBUF];
1698 char otherdir[MAXBUF];
1701 strlcpy(work,argv[0],MAXBUF);
1704 // we just want the dir
1716 // Get the current working directory
1717 if (getcwd(buffer, MAXBUF) == NULL)
1722 if (getcwd(otherdir, MAXBUF) == NULL)
1729 int InsertMode(std::string &output, const char* mode, unsigned short section)
1731 unsigned short currsection = 1;
1732 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1734 if(section > 4 || section == 0)
1736 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1740 for(; pos < output.size(); pos++)
1742 if(section == currsection)
1745 if(output[pos] == ',')
1749 output.insert(pos, mode);
1753 bool IsValidChannelName(const char *chname)
1757 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1758 if (!chname || *chname != '#')
1763 c = (char *)chname + 1;
1777 /* too long a name - note funky pointer arithmetic here. */
1778 if ((c - chname) > CHANMAX)
1786 inline int charlcat(char* x,char y,int z)
1803 bool charremove(char* mp, char remove)
1806 bool shift_down = false;
1810 if (*mptr == remove)