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 /* XXX - WHAT THE HELL are we doing here? :P --w00t */
479 userrec* otheruser = (userrec*)o;
481 if (otheruser->fd != FD_MAGIC_NUMBER)
482 WriteTo_NoFormat(user,otheruser,textbuffer);
486 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
488 std::map<char*,char*> *ulist;
490 if ((!Ptr) || (!user) || (!text))
492 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
496 ulist = Ptr->GetUsers();
498 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
500 /* XXX - .....and here! --w00t */
502 userrec* otheruser = (userrec*)o;
504 if (otheruser->fd != FD_MAGIC_NUMBER)
505 WriteTo_NoFormat(user,otheruser,text);
510 /* write formatted text from a source user to all users on a channel
511 * including the sender (NOT for privmsg, notice etc!) doesnt send to
512 * users on remote servers */
514 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
516 char textbuffer[MAXBUF];
518 std::map<char*,char*> *ulist;
520 if ((!Ptr) || (!text))
522 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
526 va_start(argsPtr, text);
527 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
530 ulist = Ptr->GetUsers();
532 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
536 userrec* otheruser = (userrec*)o;
538 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
542 WriteServ_NoFormat(otheruser->fd,textbuffer);
546 WriteTo_NoFormat(user,otheruser,textbuffer);
552 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
554 std::map<char*,char*> *ulist;
556 if ((!Ptr) || (!text))
558 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
562 ulist = Ptr->GetUsers();
564 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
568 userrec* otheruser = (userrec*)o;
570 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
574 WriteServ_NoFormat(otheruser->fd,text);
578 WriteTo_NoFormat(user,otheruser,text);
586 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
588 char textbuffer[MAXBUF];
590 std::map<char*,char*> *ulist;
592 if ((!Ptr) || (!text))
594 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
598 va_start(argsPtr, text);
599 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
602 ulist = Ptr->GetUsers();
604 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
608 userrec* otheruser = (userrec*)o;
610 if (IS_LOCAL(otheruser))
611 WriteServ_NoFormat(otheruser->fd,textbuffer);
615 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
617 std::map<char*,char*> *ulist;
619 if ((!Ptr) || (!text))
621 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
625 ulist = Ptr->GetUsers();
627 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
631 userrec* otheruser = (userrec*)o;
633 if (IS_LOCAL(otheruser))
634 WriteServ_NoFormat(otheruser->fd,text);
640 /* write formatted text from a source user to all users on a channel except
641 * for the sender (for privmsg etc) */
643 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
645 char textbuffer[MAXBUF];
647 std::map<char*,char*> *ulist;
649 if ((!Ptr) || (!user) || (!text))
651 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
655 va_start(argsPtr, text);
656 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
662 ulist = Ptr->GetOppedUsers();
665 ulist = Ptr->GetHalfoppedUsers();
668 ulist = Ptr->GetVoicedUsers();
671 ulist = Ptr->GetUsers();
675 log(DEBUG,"%d users to write to",ulist->size());
677 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
681 userrec* otheruser = (userrec*)o;
683 if ((IS_LOCAL(otheruser)) && (user != otheruser))
684 WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
688 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
690 std::map<char*,char*> *ulist;
692 if ((!Ptr) || (!user) || (!text))
694 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
701 ulist = Ptr->GetOppedUsers();
704 ulist = Ptr->GetHalfoppedUsers();
707 ulist = Ptr->GetVoicedUsers();
710 ulist = Ptr->GetUsers();
714 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
718 userrec* otheruser = (userrec*)o;
720 if ((IS_LOCAL(otheruser)) && (user != otheruser))
721 WriteFrom_NoFormat(otheruser->fd,user,text);
725 std::string GetServerDescription(char* servername)
727 std::string description = "";
729 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
731 if (description != "")
737 // not a remote server that can be found, it must be me.
738 return Config->ServerDesc;
742 /* write a formatted string to all users who share at least one common
743 * channel, including the source user e.g. for use in NICK */
745 void WriteCommon(userrec *u, char* text, ...)
747 char textbuffer[MAXBUF];
749 bool sent_to_at_least_one = false;
754 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
758 if (u->registered != 7)
760 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
764 va_start(argsPtr, text);
765 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
768 // FIX: Stops a message going to the same person more than once
769 memset(&already_sent,0,MAX_DESCRIPTORS);
772 for (unsigned int i = 0; i < y; i++)
774 if (u->chans[i].channel)
776 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
778 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
781 userrec* otheruser = (userrec*)o;
783 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
785 already_sent[otheruser->fd] = 1;
786 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
787 sent_to_at_least_one = true;
794 * if the user was not in any channels, no users will receive the text. Make sure the user
795 * receives their OWN message for WriteCommon
797 if (!sent_to_at_least_one)
799 WriteFrom_NoFormat(u->fd,u,textbuffer);
803 void WriteCommon_NoFormat(userrec *u, const char* text)
805 bool sent_to_at_least_one = false;
810 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
814 if (u->registered != 7)
816 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
820 // FIX: Stops a message going to the same person more than once
821 memset(&already_sent,0,MAX_DESCRIPTORS);
824 for (unsigned int i = 0; i < y; i++)
826 if (u->chans[i].channel)
828 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
830 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
833 userrec* otheruser = (userrec*)o;
835 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
837 already_sent[otheruser->fd] = 1;
838 WriteFrom_NoFormat(otheruser->fd,u,text);
839 sent_to_at_least_one = true;
846 * if the user was not in any channels, no users will receive the text. Make sure the user
847 * receives their OWN message for WriteCommon
849 if (!sent_to_at_least_one)
851 WriteFrom_NoFormat(u->fd,u,text);
856 /* write a formatted string to all users who share at least one common
857 * channel, NOT including the source user e.g. for use in QUIT
860 void WriteCommonExcept(userrec *u, char* text, ...)
862 char textbuffer[MAXBUF];
863 char oper_quit[MAXBUF];
864 bool quit_munge = false;
871 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
875 if (u->registered != 7)
877 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
881 va_start(argsPtr, text);
882 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
885 if ((Config->HideSplits) && (total > 6))
887 /* Yeah yeah, this is ugly. But its fast, live with it. */
888 char* check = textbuffer;
890 /* XXX - don't mean to be picky, but I think this could be done better */
891 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
893 std::stringstream split(check);
894 std::string server_one;
895 std::string server_two;
900 if ((FindServerName(server_one)) && (FindServerName(server_two)))
902 strlcpy(oper_quit,textbuffer,MAXQUIT);
903 strlcpy(check,"*.net *.split",MAXQUIT);
909 if ((Config->HideBans) && (total > 13) && (!quit_munge))
911 char* check = textbuffer;
914 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
918 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
920 strlcpy(oper_quit,textbuffer,MAXQUIT);
921 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
927 memset(&already_sent,0,MAX_DESCRIPTORS);
930 for (unsigned int i = 0; i < y; i++)
932 if (u->chans[i].channel)
934 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
936 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
939 userrec* otheruser = (userrec*)o;
943 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
945 already_sent[otheruser->fd] = 1;
949 WriteFrom_NoFormat(otheruser->fd,u,*otheruser->oper ? oper_quit : textbuffer);
952 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
960 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
966 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
970 if (u->registered != 7)
972 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
976 memset(&already_sent,0,MAX_DESCRIPTORS);
979 for (unsigned int i = 0; i < y; i++)
981 if (u->chans[i].channel)
983 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
985 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
988 userrec* otheruser = (userrec*)o;
992 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
994 already_sent[otheruser->fd] = 1;
995 WriteFrom_NoFormat(otheruser->fd,u,text);
1004 /* XXX - replace with a call to WriteMode() ? -- w00t */
1005 void WriteOpers(char* text, ...)
1007 char textbuffer[MAXBUF];
1012 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1016 va_start(argsPtr, text);
1017 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1020 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
1026 if (strchr(a->modes,'s'))
1028 // send server notices to all with +s
1029 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
1035 void ServerNoticeAll(char* text, ...)
1040 char textbuffer[MAXBUF];
1042 va_start (argsPtr, text);
1043 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1046 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1048 userrec* t = (userrec*)(*i);
1049 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1053 void ServerPrivmsgAll(char* text, ...)
1058 char textbuffer[MAXBUF];
1060 va_start (argsPtr, text);
1061 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1064 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1066 userrec* t = (userrec*)(*i);
1067 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1071 void WriteMode(const char* modes, int flags, const char* text, ...)
1073 char textbuffer[MAXBUF];
1077 if ((!text) || (!modes) || (!flags))
1079 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1083 va_start(argsPtr, text);
1084 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1086 modelen = strlen(modes);
1088 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1090 userrec* t = (userrec*)(*i);
1091 bool send_to_user = false;
1093 if (flags == WM_AND)
1095 send_to_user = true;
1097 for (int n = 0; n < modelen; n++)
1099 if (!hasumode(t,modes[n]))
1101 send_to_user = false;
1106 else if (flags == WM_OR)
1108 send_to_user = false;
1110 for (int n = 0; n < modelen; n++)
1112 if (hasumode(t,modes[n]))
1114 send_to_user = true;
1122 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1127 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1129 char textbuffer[MAXBUF];
1132 if ((!text) || (!source))
1134 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1138 va_start(argsPtr, text);
1139 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1142 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1144 userrec* t = (userrec*)(*i);
1145 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
1150 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1152 char textbuffer[MAXBUF];
1155 if ((!text) || (!source))
1157 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1161 va_start(argsPtr, text);
1162 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1165 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1167 userrec* t = (userrec*)(*i);
1169 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1171 WriteTo(source,t,"WALLOPS :%s",textbuffer);
1176 /* convert a string to lowercase. Note following special circumstances
1177 * taken from RFC 1459. Many "official" server branches still hold to this
1178 * rule so i will too;
1180 * Because of IRC's scandanavian origin, the characters {}| are
1181 * considered to be the lower case equivalents of the characters []\,
1182 * respectively. This is a critical issue when determining the
1183 * equivalence of two nicknames.
1185 void strlower(char *n)
1189 for (char* t = n; *t; t++)
1190 *t = lowermap[(unsigned)*t];
1194 /* Find a user record by nickname and return a pointer to it */
1196 userrec* Find(std::string nick)
1198 user_hash::iterator iter = clientlist.find(nick);
1200 if (iter == clientlist.end())
1201 /* Couldn't find it */
1204 return iter->second;
1207 userrec* Find(const char* nick)
1209 user_hash::iterator iter;
1214 iter = clientlist.find(nick);
1216 if (iter == clientlist.end())
1219 return iter->second;
1222 /* find a channel record by channel name and return a pointer to it */
1224 chanrec* FindChan(const char* chan)
1226 chan_hash::iterator iter;
1230 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1234 iter = chanlist.find(chan);
1236 if (iter == chanlist.end())
1237 /* Couldn't find it */
1240 return iter->second;
1244 long GetMaxBans(char* name)
1247 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1250 if (match(name,x.c_str()))
1258 void purge_empty_chans(userrec* u)
1262 // firstly decrement the count on each channel
1263 for (unsigned int f = 0; f < u->chans.size(); f++)
1265 if (u->chans[f].channel)
1267 u->chans[f].channel->DelUser((char*)u);
1271 for (unsigned int i = 0; i < u->chans.size(); i++)
1273 if (u->chans[i].channel)
1275 if (!usercount(u->chans[i].channel))
1277 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1279 /* kill the record */
1280 if (i2 != chanlist.end())
1282 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1286 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1292 u->chans[i].channel = NULL;
1297 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1302 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1307 char* chanmodes(chanrec *chan, bool showkey)
1309 static char scratch[MAXBUF];
1310 static char sparam[MAXBUF];
1311 char* offset = scratch;
1315 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1323 if (chan->binarymodes & CM_NOEXTERNAL)
1325 if (chan->binarymodes & CM_TOPICLOCK)
1331 if (chan->binarymodes & CM_INVITEONLY)
1333 if (chan->binarymodes & CM_MODERATED)
1335 if (chan->binarymodes & CM_SECRET)
1337 if (chan->binarymodes & CM_PRIVATE)
1342 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1348 sprintf(foo," %lu",(unsigned long)chan->limit);
1349 strlcat(sparam,foo,MAXBUF);
1352 for (int n = 0; n < 190; n++)
1354 if (chan->custom_modes[n])
1357 std::string extparam = chan->GetModeParameter(n+65);
1361 charlcat(sparam,' ',MAXBUF);
1362 strlcat(sparam,extparam.c_str(),MAXBUF);
1367 /* Null terminate scratch */
1369 strlcat(scratch,sparam,MAXMODES);
1374 /* compile a userlist of a channel into a string, each nick seperated by
1375 * spaces and op, voice etc status shown as @ and + */
1377 void userlist(userrec *user,chanrec *c)
1379 if ((!c) || (!user))
1381 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1385 size_t dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1386 size_t initial = dlen;
1388 std::map<char*,char*> *ulist= c->GetUsers();
1390 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1392 char* o = i->second;
1393 userrec* otheruser = (userrec*)o;
1395 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1398 * user is +i, and source not on the channel, does not show
1399 * nick in NAMES list
1404 dlen += strlcat(list,cmode(otheruser,c),MAXBUF);
1405 dlen += strlcat(list,otheruser->nick,MAXBUF);
1406 charlcat(list,' ',MAXBUF);
1409 if (dlen > (480-NICKMAX))
1411 /* list overflowed into multiple numerics */
1412 WriteServ_NoFormat(user->fd,list);
1413 dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1417 /* if whats left in the list isnt empty, send it */
1418 if (dlen != initial)
1420 WriteServ_NoFormat(user->fd,list);
1425 * return a count of the users on a specific channel accounting for
1426 * invisible users who won't increase the count. e.g. for /LIST
1428 int usercount_i(chanrec *c)
1435 std::map<char*,char*> *ulist= c->GetUsers();
1436 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1438 char* o = i->second;
1439 userrec* user = (userrec*)o;
1440 if (!strchr(user->modes,'i'))
1447 int usercount(chanrec *c)
1449 return (c ? c->GetUserCounter() : 0);
1453 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1454 ConnectClass GetClass(userrec *user)
1456 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1458 if (match(user->host,i->host.c_str()))
1464 return *(Config->Classes.begin());
1468 * sends out an error notice to all connected clients (not to be used
1471 void send_error(char *s)
1473 log(DEBUG,"send_error: %s",s);
1475 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1477 userrec* t = (userrec*)(*i);
1478 if (t->registered == 7)
1480 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1484 // fix - unregistered connections receive ERROR, not NOTICE
1485 Write(t->fd,"ERROR :%s",s);
1490 void Error(int status)
1492 signal(SIGALRM, SIG_IGN);
1493 signal(SIGPIPE, SIG_IGN);
1494 signal(SIGTERM, SIG_IGN);
1495 signal(SIGABRT, SIG_IGN);
1496 signal(SIGSEGV, SIG_IGN);
1497 signal(SIGURG, SIG_IGN);
1498 signal(SIGKILL, SIG_IGN);
1499 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1500 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1504 // this function counts all users connected, wether they are registered or NOT.
1507 return clientlist.size();
1510 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1511 int registered_usercount(void)
1515 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1517 if (i->second->registered == 7) c++;
1523 int usercount_invisible(void)
1527 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1529 if ((i->second->registered == 7) && (strchr(i->second->modes,'i')))
1536 int usercount_opers(void)
1540 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1542 if (i->second->oper)
1548 int usercount_unknown(void)
1552 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1554 userrec* t = (userrec*)(*i);
1555 if (t->registered != 7)
1562 long chancount(void)
1564 return chanlist.size();
1571 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1573 userrec* t = (userrec*)(*i);
1574 if (t->registered == 7)
1581 void ShowMOTD(userrec *user)
1583 static char mbuf[MAXBUF];
1584 static char crud[MAXBUF];
1585 std::string WholeMOTD = "";
1587 if (!Config->MOTD.size())
1589 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1593 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1594 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1595 WholeMOTD = WholeMOTD + mbuf;
1597 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1598 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1600 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1601 WholeMOTD = WholeMOTD + mbuf;
1603 // only one write operation
1604 if (Config->GetIOHook(user->port))
1608 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1610 catch (ModuleException& modexcept)
1612 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1617 user->AddWriteBuf(WholeMOTD);
1620 ServerInstance->stats->statsSent += WholeMOTD.length();
1623 void ShowRULES(userrec *user)
1625 if (!Config->RULES.size())
1627 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1630 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1632 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1633 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1635 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1638 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1639 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1640 // registration timeout maximum seconds)
1641 bool AllModulesReportReady(userrec* user)
1643 if (!Config->global_implementation[I_OnCheckReady])
1646 for (int i = 0; i <= MODCOUNT; i++)
1648 if (Config->implement_lists[i][I_OnCheckReady])
1650 int res = modules[i]->OnCheckReady(user);
1659 bool DirValid(char* dirandfile)
1662 char buffer[MAXBUF];
1663 char otherdir[MAXBUF];
1666 strlcpy(work, dirandfile, MAXBUF);
1669 // we just want the dir
1681 // Get the current working directory
1682 if (getcwd(buffer, MAXBUF ) == NULL )
1687 if (getcwd(otherdir, MAXBUF ) == NULL )
1692 size_t t = strlen(work);
1694 if (strlen(otherdir) >= t)
1698 if (!strcmp(otherdir,work))
1711 std::string GetFullProgDir(char** argv, int argc)
1714 char buffer[MAXBUF];
1715 char otherdir[MAXBUF];
1718 strlcpy(work,argv[0],MAXBUF);
1721 // we just want the dir
1733 // Get the current working directory
1734 if (getcwd(buffer, MAXBUF) == NULL)
1739 if (getcwd(otherdir, MAXBUF) == NULL)
1746 int InsertMode(std::string &output, const char* mode, unsigned short section)
1748 unsigned short currsection = 1;
1749 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1751 if(section > 4 || section == 0)
1753 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1757 for(; pos < output.size(); pos++)
1759 if(section == currsection)
1762 if(output[pos] == ',')
1766 output.insert(pos, mode);
1770 bool IsValidChannelName(const char *chname)
1774 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1775 if (!chname || *chname != '#')
1780 c = (char *)chname + 1;
1794 /* too long a name - note funky pointer arithmetic here. */
1795 if ((c - chname) > CHANMAX)
1803 inline int charlcat(char* x,char y,int z)
1820 bool charremove(char* mp, char remove)
1823 bool shift_down = false;
1827 if (*mptr == remove)