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;
721 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
725 if (u->registered != 7)
727 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
731 va_start(argsPtr, text);
732 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
735 // FIX: Stops a message going to the same person more than once
736 memset(&already_sent,0,MAX_DESCRIPTORS);
738 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
740 if (((ucrec*)(*v))->channel)
742 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
744 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
746 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
748 already_sent[i->second->fd] = 1;
749 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
750 sent_to_at_least_one = true;
757 * if the user was not in any channels, no users will receive the text. Make sure the user
758 * receives their OWN message for WriteCommon
760 if (!sent_to_at_least_one)
762 WriteFrom_NoFormat(u->fd,u,textbuffer);
766 void WriteCommon_NoFormat(userrec *u, const char* text)
768 bool sent_to_at_least_one = false;
772 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
776 if (u->registered != 7)
778 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
782 // FIX: Stops a message going to the same person more than once
783 memset(&already_sent,0,MAX_DESCRIPTORS);
785 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
787 if (((ucrec*)(*v))->channel)
789 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
791 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
793 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
795 already_sent[i->second->fd] = 1;
796 WriteFrom_NoFormat(i->second->fd,u,text);
797 sent_to_at_least_one = true;
804 * if the user was not in any channels, no users will receive the text. Make sure the user
805 * receives their OWN message for WriteCommon
807 if (!sent_to_at_least_one)
809 WriteFrom_NoFormat(u->fd,u,text);
814 /* write a formatted string to all users who share at least one common
815 * channel, NOT including the source user e.g. for use in QUIT
818 void WriteCommonExcept(userrec *u, char* text, ...)
820 char textbuffer[MAXBUF];
821 char oper_quit[MAXBUF];
822 bool quit_munge = false;
828 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
832 if (u->registered != 7)
834 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
838 va_start(argsPtr, text);
839 total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
842 if ((Config->HideSplits) && (total > 6))
844 /* Yeah yeah, this is ugly. But its fast, live with it. */
845 char* check = textbuffer;
847 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
849 std::stringstream split(check);
850 std::string server_one;
851 std::string server_two;
856 if ((FindServerName(server_one)) && (FindServerName(server_two)))
858 strlcpy(oper_quit,textbuffer,MAXQUIT);
859 strlcpy(check,"*.net *.split",MAXQUIT);
865 if ((Config->HideBans) && (total > 13) && (!quit_munge))
867 char* check = textbuffer;
870 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
874 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
876 strlcpy(oper_quit,textbuffer,MAXQUIT);
877 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
883 memset(&already_sent,0,MAX_DESCRIPTORS);
885 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
887 if (((ucrec*)(*v))->channel)
889 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
891 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
895 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
897 already_sent[i->second->fd] = 1;
901 WriteFrom_NoFormat(i->second->fd,u,*i->second->oper ? oper_quit : textbuffer);
904 WriteFrom_NoFormat(i->second->fd,u,textbuffer);
912 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
916 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
920 if (u->registered != 7)
922 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
926 memset(&already_sent,0,MAX_DESCRIPTORS);
928 for (std::vector<ucrec*>::const_iterator v = u->chans.begin(); v != u->chans.end(); v++)
930 if (((ucrec*)(*v))->channel)
932 CUList *ulist= ((ucrec*)(*v))->channel->GetUsers();
934 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
938 if ((i->second->fd > -1) && (!already_sent[i->second->fd]))
940 already_sent[i->second->fd] = 1;
941 WriteFrom_NoFormat(i->second->fd,u,text);
950 /* XXX - We don't use WriteMode for this because WriteMode is very slow and
951 * this isnt. Basically WriteMode has to iterate ALL the users 'n' times for
952 * the number of modes provided, e.g. if you send WriteMode 'og' to write to
953 * opers with globops, and you have 2000 users, thats 4000 iterations. WriteOpers
954 * uses the oper list, which means if you have 2000 users but only 5 opers,
955 * it iterates 5 times.
957 void WriteOpers(char* text, ...)
959 char textbuffer[MAXBUF];
964 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
968 va_start(argsPtr, text);
969 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
972 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
978 if (strchr(a->modes,'s'))
980 // send server notices to all with +s
981 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
987 void ServerNoticeAll(char* text, ...)
992 char textbuffer[MAXBUF];
994 va_start (argsPtr, text);
995 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
998 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1000 userrec* t = (userrec*)(*i);
1001 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
1005 void ServerPrivmsgAll(char* text, ...)
1010 char textbuffer[MAXBUF];
1012 va_start (argsPtr, text);
1013 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1016 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1018 userrec* t = (userrec*)(*i);
1019 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
1023 void WriteMode(const char* modes, int flags, const char* text, ...)
1025 char textbuffer[MAXBUF];
1029 if ((!text) || (!modes) || (!flags))
1031 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
1035 va_start(argsPtr, text);
1036 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1038 modelen = strlen(modes);
1040 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1042 userrec* t = (userrec*)(*i);
1043 bool send_to_user = false;
1045 if (flags == WM_AND)
1047 send_to_user = true;
1049 for (int n = 0; n < modelen; n++)
1051 if (!hasumode(t,modes[n]))
1053 send_to_user = false;
1058 else if (flags == WM_OR)
1060 send_to_user = false;
1062 for (int n = 0; n < modelen; n++)
1064 if (hasumode(t,modes[n]))
1066 send_to_user = true;
1074 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
1079 void NoticeAll(userrec *source, bool local_only, char* text, ...)
1081 char textbuffer[MAXBUF];
1084 if ((!text) || (!source))
1086 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
1090 va_start(argsPtr, text);
1091 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1094 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1096 userrec* t = (userrec*)(*i);
1097 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
1102 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1104 char textbuffer[MAXBUF];
1107 if ((!text) || (!source))
1109 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1113 va_start(argsPtr, text);
1114 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1117 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1119 userrec* t = (userrec*)(*i);
1121 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1123 WriteTo(source,t,"WALLOPS :%s",textbuffer);
1128 /* convert a string to lowercase. Note following special circumstances
1129 * taken from RFC 1459. Many "official" server branches still hold to this
1130 * rule so i will too;
1132 * Because of IRC's scandanavian origin, the characters {}| are
1133 * considered to be the lower case equivalents of the characters []\,
1134 * respectively. This is a critical issue when determining the
1135 * equivalence of two nicknames.
1137 void strlower(char *n)
1141 for (char* t = n; *t; t++)
1142 *t = lowermap[(unsigned)*t];
1146 /* Find a user record by nickname and return a pointer to it */
1148 userrec* Find(std::string nick)
1150 user_hash::iterator iter = clientlist.find(nick);
1152 if (iter == clientlist.end())
1153 /* Couldn't find it */
1156 return iter->second;
1159 userrec* Find(const char* nick)
1161 user_hash::iterator iter;
1166 iter = clientlist.find(nick);
1168 if (iter == clientlist.end())
1171 return iter->second;
1174 /* find a channel record by channel name and return a pointer to it */
1176 chanrec* FindChan(const char* chan)
1178 chan_hash::iterator iter;
1182 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1186 iter = chanlist.find(chan);
1188 if (iter == chanlist.end())
1189 /* Couldn't find it */
1192 return iter->second;
1196 long GetMaxBans(char* name)
1199 for (std::map<std::string,int>::iterator n = Config->maxbans.begin(); n != Config->maxbans.end(); n++)
1202 if (match(name,x.c_str()))
1210 void purge_empty_chans(userrec* u)
1212 std::vector<chanrec*> to_delete;
1214 // firstly decrement the count on each channel
1215 for (std::vector<ucrec*>::iterator f = u->chans.begin(); f != u->chans.end(); f++)
1217 if (((ucrec*)(*f))->channel)
1219 if (((ucrec*)(*f))->channel->DelUser(u) == 0)
1221 /* No users left in here, mark it for deletion */
1222 to_delete.push_back(((ucrec*)(*f))->channel);
1223 ((ucrec*)(*f))->channel = NULL;
1228 log(DEBUG,"purge_empty_chans: %d channels to delete",to_delete.size());
1230 for (std::vector<chanrec*>::iterator n = to_delete.begin(); n != to_delete.end(); n++)
1232 chanrec* thischan = (chanrec*)*n;
1233 chan_hash::iterator i2 = chanlist.find(thischan->name);
1234 if (i2 != chanlist.end())
1236 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1247 char* chanmodes(chanrec *chan, bool showkey)
1249 static char scratch[MAXBUF];
1250 static char sparam[MAXBUF];
1251 char* offset = scratch;
1255 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1263 if (chan->binarymodes & CM_NOEXTERNAL)
1265 if (chan->binarymodes & CM_TOPICLOCK)
1271 if (chan->binarymodes & CM_INVITEONLY)
1273 if (chan->binarymodes & CM_MODERATED)
1275 if (chan->binarymodes & CM_SECRET)
1277 if (chan->binarymodes & CM_PRIVATE)
1282 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1288 sprintf(foo," %lu",(unsigned long)chan->limit);
1289 strlcat(sparam,foo,MAXBUF);
1292 /* This was still iterating up to 190, chanrec::custom_modes is only 64 elements -- Om */
1293 for(int n = 0; n < 64; n++)
1295 if(chan->custom_modes[n])
1298 std::string extparam = chan->GetModeParameter(n+65);
1302 charlcat(sparam,' ',MAXBUF);
1303 strlcat(sparam,extparam.c_str(),MAXBUF);
1308 /* Null terminate scratch */
1310 strlcat(scratch,sparam,MAXMODES);
1315 /* compile a userlist of a channel into a string, each nick seperated by
1316 * spaces and op, voice etc status shown as @ and + */
1318 void userlist(userrec *user,chanrec *c)
1320 if ((!c) || (!user))
1322 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1326 size_t dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1327 size_t initial = dlen;
1329 CUList *ulist= c->GetUsers();
1331 /* Improvement by Brain - this doesnt change in value, so why was it inside
1334 bool has_user = c->HasUser(user);
1336 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1338 if ((!has_user) && (strchr(i->second->modes,'i')))
1341 * user is +i, and source not on the channel, does not show
1342 * nick in NAMES list
1347 dlen += strlcat(list,cmode(i->second,c),MAXBUF);
1348 dlen += strlcat(list,i->second->nick,MAXBUF);
1349 charlcat(list,' ',MAXBUF);
1352 if (dlen > (480-NICKMAX))
1354 /* list overflowed into multiple numerics */
1355 WriteServ_NoFormat(user->fd,list);
1356 dlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1360 /* if whats left in the list isnt empty, send it */
1361 if (dlen != initial)
1363 WriteServ_NoFormat(user->fd,list);
1368 * return a count of the users on a specific channel accounting for
1369 * invisible users who won't increase the count. e.g. for /LIST
1371 int usercount_i(chanrec *c)
1378 CUList *ulist= c->GetUsers();
1379 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
1381 if (!strchr(i->second->modes,'i'))
1388 int usercount(chanrec *c)
1390 return (c ? c->GetUserCounter() : 0);
1394 /* looks up a users password for their connection class (<ALLOW>/<DENY> tags) */
1395 ConnectClass GetClass(userrec *user)
1397 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1399 if (match(user->host,i->host.c_str()))
1405 return *(Config->Classes.begin());
1409 * sends out an error notice to all connected clients (not to be used
1412 void send_error(char *s)
1414 log(DEBUG,"send_error: %s",s);
1416 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1418 userrec* t = (userrec*)(*i);
1419 if (t->registered == 7)
1421 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1425 // fix - unregistered connections receive ERROR, not NOTICE
1426 Write(t->fd,"ERROR :%s",s);
1431 void Error(int status)
1433 signal(SIGALRM, SIG_IGN);
1434 signal(SIGPIPE, SIG_IGN);
1435 signal(SIGTERM, SIG_IGN);
1436 signal(SIGABRT, SIG_IGN);
1437 signal(SIGSEGV, SIG_IGN);
1438 signal(SIGURG, SIG_IGN);
1439 signal(SIGKILL, SIG_IGN);
1440 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1441 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1445 // this function counts all users connected, wether they are registered or NOT.
1448 return clientlist.size();
1451 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1452 int registered_usercount(void)
1456 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1458 if (i->second->registered == 7) c++;
1464 int usercount_invisible(void)
1468 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1470 if ((i->second->registered == 7) && (strchr(i->second->modes,'i')))
1477 int usercount_opers(void)
1481 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1483 if (i->second->oper)
1489 int usercount_unknown(void)
1493 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1495 userrec* t = (userrec*)(*i);
1496 if (t->registered != 7)
1503 long chancount(void)
1505 return chanlist.size();
1512 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1514 userrec* t = (userrec*)(*i);
1515 if (t->registered == 7)
1522 void ShowMOTD(userrec *user)
1524 static char mbuf[MAXBUF];
1525 static char crud[MAXBUF];
1526 std::string WholeMOTD = "";
1528 if (!Config->MOTD.size())
1530 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1534 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1535 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1536 WholeMOTD = WholeMOTD + mbuf;
1538 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1539 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1541 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1542 WholeMOTD = WholeMOTD + mbuf;
1544 // only one write operation
1545 if (Config->GetIOHook(user->port))
1549 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1551 catch (ModuleException& modexcept)
1553 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
1558 user->AddWriteBuf(WholeMOTD);
1561 ServerInstance->stats->statsSent += WholeMOTD.length();
1564 void ShowRULES(userrec *user)
1566 if (!Config->RULES.size())
1568 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1571 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1573 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1574 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1576 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1579 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1580 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1581 // registration timeout maximum seconds)
1582 bool AllModulesReportReady(userrec* user)
1584 if (!Config->global_implementation[I_OnCheckReady])
1587 for (int i = 0; i <= MODCOUNT; i++)
1589 if (Config->implement_lists[i][I_OnCheckReady])
1591 int res = modules[i]->OnCheckReady(user);
1600 bool DirValid(char* dirandfile)
1603 char buffer[MAXBUF];
1604 char otherdir[MAXBUF];
1607 strlcpy(work, dirandfile, MAXBUF);
1610 // we just want the dir
1622 // Get the current working directory
1623 if (getcwd(buffer, MAXBUF ) == NULL )
1628 if (getcwd(otherdir, MAXBUF ) == NULL )
1633 size_t t = strlen(work);
1635 if (strlen(otherdir) >= t)
1639 if (!strcmp(otherdir,work))
1652 std::string GetFullProgDir(char** argv, int argc)
1655 char buffer[MAXBUF];
1656 char otherdir[MAXBUF];
1659 strlcpy(work,argv[0],MAXBUF);
1662 // we just want the dir
1674 // Get the current working directory
1675 if (getcwd(buffer, MAXBUF) == NULL)
1680 if (getcwd(otherdir, MAXBUF) == NULL)
1687 int InsertMode(std::string &output, const char* mode, unsigned short section)
1689 unsigned short currsection = 1;
1690 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1692 if(section > 4 || section == 0)
1694 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1698 for(; pos < output.size(); pos++)
1700 if(section == currsection)
1703 if(output[pos] == ',')
1707 output.insert(pos, mode);
1711 bool IsValidChannelName(const char *chname)
1715 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1716 if (!chname || *chname != '#')
1721 c = (char *)chname + 1;
1735 /* too long a name - note funky pointer arithmetic here. */
1736 if ((c - chname) > CHANMAX)
1744 inline int charlcat(char* x,char y,int z)
1761 bool charremove(char* mp, char remove)
1764 bool shift_down = false;
1768 if (*mptr == remove)