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];
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 (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
477 if (i->second->fd != FD_MAGIC_NUMBER)
478 WriteTo_NoFormat(user,i->second,textbuffer);
482 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
486 if ((!Ptr) || (!user) || (!text))
488 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
492 ulist = Ptr->GetUsers();
494 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
496 if (i->second->fd != FD_MAGIC_NUMBER)
497 WriteTo_NoFormat(user,i->second,text);
502 /* write formatted text from a source user to all users on a channel
503 * including the sender (NOT for privmsg, notice etc!) doesnt send to
504 * users on remote servers */
506 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
508 char textbuffer[MAXBUF];
512 if ((!Ptr) || (!text))
514 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
518 va_start(argsPtr, text);
519 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
522 ulist = Ptr->GetUsers();
524 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
526 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
530 WriteServ_NoFormat(i->second->fd,textbuffer);
534 WriteTo_NoFormat(user,i->second,textbuffer);
540 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
544 if ((!Ptr) || (!text))
546 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
550 ulist = Ptr->GetUsers();
552 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
554 if ((i->second->fd != FD_MAGIC_NUMBER) && (i->second != user))
558 WriteServ_NoFormat(i->second->fd,text);
562 WriteTo_NoFormat(user,i->second,text);
570 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
572 char textbuffer[MAXBUF];
576 if ((!Ptr) || (!text))
578 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
582 va_start(argsPtr, text);
583 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
586 ulist = Ptr->GetUsers();
588 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
590 if (IS_LOCAL(i->second))
591 WriteServ_NoFormat(i->second->fd,textbuffer);
595 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
599 if ((!Ptr) || (!text))
601 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
605 ulist = Ptr->GetUsers();
607 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
609 if (IS_LOCAL(i->second))
610 WriteServ_NoFormat(i->second->fd,text);
616 /* write formatted text from a source user to all users on a channel except
617 * for the sender (for privmsg etc) */
619 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
621 char textbuffer[MAXBUF];
625 if ((!Ptr) || (!user) || (!text))
627 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
631 va_start(argsPtr, text);
632 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
638 ulist = Ptr->GetOppedUsers();
641 ulist = Ptr->GetHalfoppedUsers();
644 ulist = Ptr->GetVoicedUsers();
647 ulist = Ptr->GetUsers();
651 log(DEBUG,"%d users to write to",ulist->size());
653 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
655 if ((IS_LOCAL(i->second)) && (user != i->second))
656 WriteFrom_NoFormat(i->second->fd,user,textbuffer);
660 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
664 if ((!Ptr) || (!user) || (!text))
666 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
673 ulist = Ptr->GetOppedUsers();
676 ulist = Ptr->GetHalfoppedUsers();
679 ulist = Ptr->GetVoicedUsers();
682 ulist = Ptr->GetUsers();
686 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
688 if ((IS_LOCAL(i->second)) && (user != i->second))
689 WriteFrom_NoFormat(i->second->fd,user,text);
693 std::string GetServerDescription(char* servername)
695 std::string description = "";
697 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
699 if (description != "")
705 // not a remote server that can be found, it must be me.
706 return Config->ServerDesc;
710 /* write a formatted string to all users who share at least one common
711 * channel, including the source user e.g. for use in NICK */
713 void WriteCommon(userrec *u, char* text, ...)
715 char textbuffer[MAXBUF];
717 bool sent_to_at_least_one = false;
722 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
726 if (u->registered != 7)
728 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
732 va_start(argsPtr, text);
733 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
736 // FIX: Stops a message going to the same person more than once
737 memset(&already_sent,0,MAX_DESCRIPTORS);
740 for (unsigned int i = 0; i < y; i++)
742 if (u->chans[i].channel)
744 CUList *ulist= u->chans[i].channel->GetUsers();
746 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
748 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
750 already_sent[i->second->fd] = 1;
751 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
752 sent_to_at_least_one = true;
759 * if the user was not in any channels, no users will receive the text. Make sure the user
760 * receives their OWN message for WriteCommon
762 if (!sent_to_at_least_one)
764 WriteFrom_NoFormat(u->fd,u,textbuffer);
768 void WriteCommon_NoFormat(userrec *u, const char* text)
770 bool sent_to_at_least_one = false;
775 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
779 if (u->registered != 7)
781 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
785 // FIX: Stops a message going to the same person more than once
786 memset(&already_sent,0,MAX_DESCRIPTORS);
789 for (unsigned int i = 0; i < y; i++)
791 if (u->chans[i].channel)
793 CUList *ulist= u->chans[i].channel->GetUsers();
795 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
797 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
799 already_sent[i->second->fd] = 1;
800 WriteFrom_NoFormat(i->second->fd,u,text);
801 sent_to_at_least_one = true;
808 * if the user was not in any channels, no users will receive the text. Make sure the user
809 * receives their OWN message for WriteCommon
811 if (!sent_to_at_least_one)
813 WriteFrom_NoFormat(u->fd,u,text);
818 /* write a formatted string to all users who share at least one common
819 * channel, NOT including the source user e.g. for use in QUIT
822 void WriteCommonExcept(userrec *u, char* text, ...)
824 char textbuffer[MAXBUF];
825 char oper_quit[MAXBUF];
826 bool quit_munge = false;
833 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
837 if (u->registered != 7)
839 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
843 va_start(argsPtr, text);
844 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
847 if ((Config->HideSplits) && (total > 6))
849 /* Yeah yeah, this is ugly. But its fast, live with it. */
850 char* check = textbuffer;
852 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
854 std::stringstream split(check);
855 std::string server_one;
856 std::string server_two;
861 if ((FindServerName(server_one)) && (FindServerName(server_two)))
863 strlcpy(oper_quit,textbuffer,MAXQUIT);
864 strlcpy(check,"*.net *.split",MAXQUIT);
870 if ((Config->HideBans) && (total > 13) && (!quit_munge))
872 char* check = textbuffer;
875 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
879 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
881 strlcpy(oper_quit,textbuffer,MAXQUIT);
882 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
888 memset(&already_sent,0,MAX_DESCRIPTORS);
891 for (unsigned int i = 0; i < y; i++)
893 if (u->chans[i].channel)
895 CUList *ulist= u->chans[i].channel->GetUsers();
897 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
901 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
903 already_sent[i->second->fd] = 1;
907 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
910 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
918 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
924 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
928 if (u->registered != 7)
930 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
934 memset(&already_sent,0,MAX_DESCRIPTORS);
937 for (unsigned int i = 0; i < y; i++)
939 if (u->chans[i].channel)
941 CUList *ulist= u->chans[i].channel->GetUsers();
943 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
947 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
949 already_sent[i->second->fd] = 1;
950 WriteFrom_NoFormat(i->second->fd,u,text);
959 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
960 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
961 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
962 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
963 * uses the oper list, which means if you have 2000 users but only 5 opers,
964 * it iterates 5 times.
966 void WriteOpers(char* text, ...)
968 char textbuffer[MAXBUF];
973 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
977 va_start(argsPtr, text);
978 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
981 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
987 if (strchr(a->modes,'s'))
989 // send server notices to all with +s
990 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
996 void ServerNoticeAll(char* text, ...)
1001 char textbuffer[MAXBUF];
1003 va_start (argsPtr, text);
1004 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1007 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1009 userrec* t = (userrec*)(*i);
1010 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1014 void ServerPrivmsgAll(char* text, ...)
1019 char textbuffer[MAXBUF];
1021 va_start (argsPtr, text);
1022 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1025 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1027 userrec* t = (userrec*)(*i);
1028 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1032 void WriteMode(const char* modes, int flags, const char* text, ...)
1034 char textbuffer[MAXBUF];
1038 if ((!text) || (!modes) || (!flags))
1040 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1044 va_start(argsPtr, text);
1045 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1047 modelen = strlen(modes);
1049 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1051 userrec* t = (userrec*)(*i);
1052 bool send_to_user = false;
1054 if (flags == WM_AND)
1056 send_to_user = true;
1058 for (int n = 0; n < modelen; n++)
1060 if (!hasumode(t,modes[n]))
1062 send_to_user = false;
1067 else if (flags == WM_OR)
1069 send_to_user = false;
1071 for (int n = 0; n < modelen; n++)
1073 if (hasumode(t,modes[n]))
1075 send_to_user = true;
1083 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1088 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1090 char textbuffer[MAXBUF];
1093 if ((!text) || (!source))
1095 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1099 va_start(argsPtr, text);
1100 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1103 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1105 userrec* t = (userrec*)(*i);
1106 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
1111 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1113 char textbuffer[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 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1128 userrec* t = (userrec*)(*i);
1130 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1132 WriteTo(source,t,"WALLOPS :%s",textbuffer);
1137 /* convert a string to lowercase. Note following special circumstances
1138 * taken from RFC 1459. Many "official" server branches still hold to this
1139 * rule so i will too;
1141 * Because of IRC's scandanavian origin, the characters {}| are
1142 * considered to be the lower case equivalents of the characters []\,
1143 * respectively. This is a critical issue when determining the
1144 * equivalence of two nicknames.
1146 void strlower(char *n)
1150 for (char* t = n; *t; t++)
1151 *t = lowermap[(unsigned)*t];
1155 /* Find a user record by nickname and return a pointer to it */
1157 userrec* Find(std::string nick)
1159 user_hash::iterator iter = clientlist.find(nick);
1161 if (iter == clientlist.end())
1162 /* Couldn't find it */
1165 return iter->second;
1168 userrec* Find(const char* nick)
1170 user_hash::iterator iter;
1175 iter = clientlist.find(nick);
1177 if (iter == clientlist.end())
1180 return iter->second;
1183 /* find a channel record by channel name and return a pointer to it */
1185 chanrec* FindChan(const char* chan)
1187 chan_hash::iterator iter;
1191 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1195 iter = chanlist.find(chan);
1197 if (iter == chanlist.end())
1198 /* Couldn't find it */
1201 return iter->second;
1205 long GetMaxBans(char* name)
1208 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1211 if (match(name,x.c_str()))
1219 void purge_empty_chans(userrec* u)
1223 // firstly decrement the count on each channel
1224 for (unsigned int f = 0; f < u->chans.size(); f++)
1226 if (u->chans[f].channel)
1228 u->chans[f].channel->DelUser(u);
1232 for (unsigned int i = 0; i < u->chans.size(); i++)
1234 if (u->chans[i].channel)
1236 if (!usercount(u->chans[i].channel))
1238 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1240 /* kill the record */
1241 if (i2 != chanlist.end())
1243 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1247 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1253 u->chans[i].channel = NULL;
1258 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1263 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1268 char* chanmodes(chanrec *chan, bool showkey)
1270 static char scratch[MAXBUF];
1271 static char sparam[MAXBUF];
1272 char* offset = scratch;
1276 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1284 if (chan->binarymodes & CM_NOEXTERNAL)
1286 if (chan->binarymodes & CM_TOPICLOCK)
1292 if (chan->binarymodes & CM_INVITEONLY)
1294 if (chan->binarymodes & CM_MODERATED)
1296 if (chan->binarymodes & CM_SECRET)
1298 if (chan->binarymodes & CM_PRIVATE)
1303 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1309 sprintf(foo," %lu",(unsigned long)chan->limit);
1310 strlcat(sparam,foo,MAXBUF);
1313 for (int n = 0; n < 190; n++)
1315 if (chan->custom_modes[n])
1318 std::string extparam = chan->GetModeParameter(n+65);
1322 charlcat(sparam,' ',MAXBUF);
1323 strlcat(sparam,extparam.c_str(),MAXBUF);
1328 /* Null terminate scratch */
1330 strlcat(scratch,sparam,MAXMODES);
1335 /* compile a userlist of a channel into a string, each nick seperated by
1336 * spaces and op, voice etc status shown as @ and + */
1338 void userlist(userrec *user,chanrec *c)
1340 if ((!c) || (!user))
1342 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1346 size_t dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1347 size_t initial = dlen;
1349 CUList *ulist= c->GetUsers();
1351 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1353 if ((!has_channel(user,c)) && (strchr(i->second->modes,'i')))
1356 * user is +i, and source not on the channel, does not show
1357 * nick in NAMES list
1362 dlen += strlcat(list,cmode(i->second,c),MAXBUF);
1363 dlen += strlcat(list,i->second->nick,MAXBUF);
1364 charlcat(list,' ',MAXBUF);
1367 if (dlen > (480-NICKMAX))
1369 /* list overflowed into multiple numerics */
1370 WriteServ_NoFormat(user->fd,list);
1371 dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1375 /* if whats left in the list isnt empty, send it */
1376 if (dlen != initial)
1378 WriteServ_NoFormat(user->fd,list);
1383 * return a count of the users on a specific channel accounting for
1384 * invisible users who won't increase the count. e.g. for /LIST
1386 int usercount_i(chanrec *c)
1393 CUList *ulist= c->GetUsers();
1394 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1396 if (!strchr(i->second->modes,'i'))
1403 int usercount(chanrec *c)
1405 return (c ? c->GetUserCounter() : 0);
1409 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1410 ConnectClass GetClass(userrec *user)
1412 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1414 if (match(user->host,i->host.c_str()))
1420 return *(Config->Classes.begin());
1424 * sends out an error notice to all connected clients (not to be used
1427 void send_error(char *s)
1429 log(DEBUG,"send_error: %s",s);
1431 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1433 userrec* t = (userrec*)(*i);
1434 if (t->registered == 7)
1436 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1440 // fix - unregistered connections receive ERROR, not NOTICE
1441 Write(t->fd,"ERROR :%s",s);
1446 void Error(int status)
1448 signal(SIGALRM, SIG_IGN);
1449 signal(SIGPIPE, SIG_IGN);
1450 signal(SIGTERM, SIG_IGN);
1451 signal(SIGABRT, SIG_IGN);
1452 signal(SIGSEGV, SIG_IGN);
1453 signal(SIGURG, SIG_IGN);
1454 signal(SIGKILL, SIG_IGN);
1455 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1456 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1460 // this function counts all users connected, wether they are registered or NOT.
1463 return clientlist.size();
1466 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1467 int registered_usercount(void)
1471 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1473 if (i->second->registered == 7) c++;
1479 int usercount_invisible(void)
1483 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1485 if ((i->second->registered == 7) && (strchr(i->second->modes,'i')))
1492 int usercount_opers(void)
1496 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1498 if (i->second->oper)
1504 int usercount_unknown(void)
1508 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1510 userrec* t = (userrec*)(*i);
1511 if (t->registered != 7)
1518 long chancount(void)
1520 return chanlist.size();
1527 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1529 userrec* t = (userrec*)(*i);
1530 if (t->registered == 7)
1537 void ShowMOTD(userrec *user)
1539 static char mbuf[MAXBUF];
1540 static char crud[MAXBUF];
1541 std::string WholeMOTD = "";
1543 if (!Config->MOTD.size())
1545 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1549 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1550 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1551 WholeMOTD = WholeMOTD + mbuf;
1553 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1554 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1556 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1557 WholeMOTD = WholeMOTD + mbuf;
1559 // only one write operation
1560 if (Config->GetIOHook(user->port))
1564 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1566 catch (ModuleException& modexcept)
1568 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1573 user->AddWriteBuf(WholeMOTD);
1576 ServerInstance->stats->statsSent += WholeMOTD.length();
1579 void ShowRULES(userrec *user)
1581 if (!Config->RULES.size())
1583 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1586 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1588 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1589 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1591 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1594 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1595 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1596 // registration timeout maximum seconds)
1597 bool AllModulesReportReady(userrec* user)
1599 if (!Config->global_implementation[I_OnCheckReady])
1602 for (int i = 0; i <= MODCOUNT; i++)
1604 if (Config->implement_lists[i][I_OnCheckReady])
1606 int res = modules[i]->OnCheckReady(user);
1615 bool DirValid(char* dirandfile)
1618 char buffer[MAXBUF];
1619 char otherdir[MAXBUF];
1622 strlcpy(work, dirandfile, MAXBUF);
1625 // we just want the dir
1637 // Get the current working directory
1638 if (getcwd(buffer, MAXBUF ) == NULL )
1643 if (getcwd(otherdir, MAXBUF ) == NULL )
1648 size_t t = strlen(work);
1650 if (strlen(otherdir) >= t)
1654 if (!strcmp(otherdir,work))
1667 std::string GetFullProgDir(char** argv, int argc)
1670 char buffer[MAXBUF];
1671 char otherdir[MAXBUF];
1674 strlcpy(work,argv[0],MAXBUF);
1677 // we just want the dir
1689 // Get the current working directory
1690 if (getcwd(buffer, MAXBUF) == NULL)
1695 if (getcwd(otherdir, MAXBUF) == NULL)
1702 int InsertMode(std::string &output, const char* mode, unsigned short section)
1704 unsigned short currsection = 1;
1705 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1707 if(section > 4 || section == 0)
1709 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1713 for(; pos < output.size(); pos++)
1715 if(section == currsection)
1718 if(output[pos] == ',')
1722 output.insert(pos, mode);
1726 bool IsValidChannelName(const char *chname)
1730 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1731 if (!chname || *chname != '#')
1736 c = (char *)chname + 1;
1750 /* too long a name - note funky pointer arithmetic here. */
1751 if ((c - chname) > CHANMAX)
1759 inline int charlcat(char* x,char y,int z)
1776 bool charremove(char* mp, char remove)
1779 bool shift_down = false;
1783 if (*mptr == remove)