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;
70 void log(int level,char *text, ...)
74 if (level < Config->LogLevel)
77 char textbuffer[MAXBUF];
81 timeinfo = localtime(&TIME);
82 strlcpy(TIMESTR,asctime(timeinfo),26);
89 va_start (argsPtr, text);
90 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
94 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
97 // nofork enabled? display it on terminal too
98 printf("%s %s\n",TIMESTR,textbuffer);
103 void readfile(file_cache &F, const char* fname)
106 char linebuf[MAXBUF];
108 log(DEBUG,"readfile: loading %s",fname);
110 file = fopen(fname,"r");
115 fgets(linebuf,sizeof(linebuf),file);
116 linebuf[strlen(linebuf)-1]='\0';
123 F.push_back(linebuf);
130 log(DEBUG,"readfile: failed to load file: %s",fname);
132 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
135 void Write_NoFormat(int sock, const char *text)
137 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
141 int bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
143 if (fd_ref_table[sock])
145 if (Config->GetIOHook(fd_ref_table[sock]->port))
149 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
151 catch (ModuleException& modexcept)
153 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
158 fd_ref_table[sock]->AddWriteBuf(tb);
160 ServerInstance->stats->statsSent += bytes;
162 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
165 void Write(int sock,char *text, ...)
167 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
171 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
175 char textbuffer[MAXBUF],tb[MAXBUF];
176 va_start (argsPtr, text);
177 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
179 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
181 if (fd_ref_table[sock])
183 if (Config->GetIOHook(fd_ref_table[sock]->port))
187 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
189 catch (ModuleException& modexcept)
191 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
196 fd_ref_table[sock]->AddWriteBuf(tb);
198 ServerInstance->stats->statsSent += bytes;
200 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
203 void WriteServ_NoFormat(int sock, const char* text)
205 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
208 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
210 if (fd_ref_table[sock])
212 if (Config->GetIOHook(fd_ref_table[sock]->port))
216 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
218 catch (ModuleException& modexcept)
220 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
225 fd_ref_table[sock]->AddWriteBuf(tb);
227 ServerInstance->stats->statsSent += bytes;
229 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
232 /* write a server formatted numeric response to a single socket */
234 void WriteServ(int sock, char* text, ...)
236 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
240 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
244 va_start (argsPtr, text);
245 char textbuffer[MAXBUF],tb[MAXBUF];
246 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
248 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
250 if (fd_ref_table[sock])
252 if (Config->GetIOHook(fd_ref_table[sock]->port))
256 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
258 catch (ModuleException& modexcept)
260 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
265 fd_ref_table[sock]->AddWriteBuf(tb);
267 ServerInstance->stats->statsSent += bytes;
269 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
272 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
274 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
277 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
279 if (fd_ref_table[sock])
281 if (Config->GetIOHook(fd_ref_table[sock]->port))
285 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
287 catch (ModuleException& modexcept)
289 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
294 fd_ref_table[sock]->AddWriteBuf(tb);
296 ServerInstance->stats->statsSent += bytes;
298 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
301 /* write text from an originating user to originating user */
303 void WriteFrom(int sock, userrec *user,char* text, ...)
305 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
307 if ((!text) || (!user))
309 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
313 va_start (argsPtr, text);
314 char textbuffer[MAXBUF],tb[MAXBUF];
315 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
317 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
319 if (fd_ref_table[sock])
321 if (Config->GetIOHook(fd_ref_table[sock]->port))
325 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
327 catch (ModuleException& modexcept)
329 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
334 fd_ref_table[sock]->AddWriteBuf(tb);
336 ServerInstance->stats->statsSent += bytes;
338 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
341 /* write text to an destination user from a source user (e.g. user privmsg) */
343 void WriteTo(userrec *source, userrec *dest,char *data, ...)
345 if ((!dest) || (!data))
347 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
352 char textbuffer[MAXBUF];
354 va_start (argsPtr, data);
355 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
359 // if no source given send it from the server.
362 WriteServ_NoFormat(dest->fd,textbuffer);
366 WriteFrom_NoFormat(dest->fd,source,textbuffer);
370 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
372 if ((!dest) || (!data))
376 WriteServ_NoFormat(dest->fd,data);
380 WriteFrom_NoFormat(dest->fd,source,data);
384 /* write formatted text from a source user to all users on a channel
385 * including the sender (NOT for privmsg, notice etc!) */
387 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
389 if ((!Ptr) || (!user) || (!text))
391 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
394 char textbuffer[MAXBUF];
396 va_start (argsPtr, text);
397 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
400 std::map<char*,char*> *ulist= Ptr->GetUsers();
401 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
404 userrec* otheruser = (userrec*)o;
405 if (otheruser->fd != FD_MAGIC_NUMBER)
406 WriteTo_NoFormat(user,otheruser,textbuffer);
410 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
412 if ((!Ptr) || (!user) || (!text))
414 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
417 std::map<char*,char*> *ulist= Ptr->GetUsers();
418 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
421 userrec* otheruser = (userrec*)o;
422 if (otheruser->fd != FD_MAGIC_NUMBER)
423 WriteTo_NoFormat(user,otheruser,text);
428 /* write formatted text from a source user to all users on a channel
429 * including the sender (NOT for privmsg, notice etc!) doesnt send to
430 * users on remote servers */
432 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
434 if ((!Ptr) || (!text))
436 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
439 char textbuffer[MAXBUF];
441 va_start (argsPtr, text);
442 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
445 std::map<char*,char*> *ulist= Ptr->GetUsers();
446 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
449 userrec* otheruser = (userrec*)o;
450 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
454 WriteServ_NoFormat(otheruser->fd,textbuffer);
458 WriteTo_NoFormat(user,otheruser,textbuffer);
464 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
466 if ((!Ptr) || (!text))
468 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
471 std::map<char*,char*> *ulist= Ptr->GetUsers();
472 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
475 userrec* otheruser = (userrec*)o;
476 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
480 WriteServ_NoFormat(otheruser->fd,text);
484 WriteTo_NoFormat(user,otheruser,text);
492 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
494 if ((!Ptr) || (!text))
496 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
499 char textbuffer[MAXBUF];
501 va_start (argsPtr, text);
502 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
506 std::map<char*,char*> *ulist= Ptr->GetUsers();
507 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
510 userrec* otheruser = (userrec*)o;
511 if (IS_LOCAL(otheruser))
512 WriteServ_NoFormat(otheruser->fd,textbuffer);
516 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
518 if ((!Ptr) || (!text))
520 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
523 std::map<char*,char*> *ulist= Ptr->GetUsers();
524 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
527 userrec* otheruser = (userrec*)o;
528 if (IS_LOCAL(otheruser))
529 WriteServ_NoFormat(otheruser->fd,text);
535 /* write formatted text from a source user to all users on a channel except
536 * for the sender (for privmsg etc) */
538 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
540 if ((!Ptr) || (!user) || (!text))
542 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
545 char textbuffer[MAXBUF];
547 va_start (argsPtr, text);
548 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
551 std::map<char*,char*> *ulist;
555 ulist = Ptr->GetOppedUsers();
558 ulist = Ptr->GetHalfoppedUsers();
561 ulist = Ptr->GetVoicedUsers();
564 ulist = Ptr->GetUsers();
567 log(DEBUG,"%d users to write to",ulist->size());
568 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
571 userrec* otheruser = (userrec*)o;
572 if ((IS_LOCAL(otheruser)) && (user != otheruser))
573 WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
577 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
579 if ((!Ptr) || (!user) || (!text))
581 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
584 std::map<char*,char*> *ulist;
588 ulist = Ptr->GetOppedUsers();
591 ulist = Ptr->GetHalfoppedUsers();
594 ulist = Ptr->GetVoicedUsers();
597 ulist = Ptr->GetUsers();
600 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
603 userrec* otheruser = (userrec*)o;
604 if ((IS_LOCAL(otheruser)) && (user != otheruser))
605 WriteFrom_NoFormat(otheruser->fd,user,text);
609 std::string GetServerDescription(char* servername)
611 std::string description = "";
612 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
613 if (description != "")
619 return Config->ServerDesc; // not a remote server that can be found, it must be me.
623 /* write a formatted string to all users who share at least one common
624 * channel, including the source user e.g. for use in NICK */
626 void WriteCommon(userrec *u, char* text, ...)
630 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
634 if (u->registered != 7) {
635 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
639 char textbuffer[MAXBUF];
641 va_start (argsPtr, text);
642 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
645 // FIX: Stops a message going to the same person more than once
646 memset(&already_sent,0,MAX_DESCRIPTORS);
648 bool sent_to_at_least_one = false;
650 unsigned int y = u->chans.size();
651 for (unsigned int i = 0; i < y; i++)
653 if (u->chans[i].channel)
655 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
656 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
659 userrec* otheruser = (userrec*)o;
660 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
662 already_sent[otheruser->fd] = 1;
663 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
664 sent_to_at_least_one = true;
669 // if the user was not in any channels, no users will receive the text. Make sure the user
670 // receives their OWN message for WriteCommon
671 if (!sent_to_at_least_one)
673 WriteFrom_NoFormat(u->fd,u,textbuffer);
677 void WriteCommon_NoFormat(userrec *u, const char* text)
681 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
685 if (u->registered != 7) {
686 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
689 // FIX: Stops a message going to the same person more than once
690 memset(&already_sent,0,MAX_DESCRIPTORS);
692 bool sent_to_at_least_one = false;
694 unsigned int y = u->chans.size();
695 for (unsigned int i = 0; i < y; i++)
697 if (u->chans[i].channel)
699 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
700 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
703 userrec* otheruser = (userrec*)o;
704 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
706 already_sent[otheruser->fd] = 1;
707 WriteFrom_NoFormat(otheruser->fd,u,text);
708 sent_to_at_least_one = true;
713 // if the user was not in any channels, no users will receive the text. Make sure the user
714 // receives their OWN message for WriteCommon
715 if (!sent_to_at_least_one)
717 WriteFrom_NoFormat(u->fd,u,text);
722 /* write a formatted string to all users who share at least one common
723 * channel, NOT including the source user e.g. for use in QUIT */
725 void WriteCommonExcept(userrec *u, char* text, ...)
729 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
733 if (u->registered != 7) {
734 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
738 char textbuffer[MAXBUF];
739 char oper_quit[MAXBUF];
740 bool quit_munge = false;
743 va_start (argsPtr, text);
744 int total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
747 if ((Config->HideSplits) && (total > 6))
749 /* Yeah yeah, this is ugly. But its fast, live with it. */
750 char* check = textbuffer;
751 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
753 std::stringstream split(check);
754 std::string server_one;
755 std::string server_two;
758 if ((FindServerName(server_one)) && (FindServerName(server_two)))
760 strlcpy(oper_quit,textbuffer,MAXQUIT);
761 strlcpy(check,"*.net *.split",MAXQUIT);
766 if ((Config->HideBans) && (total > 13) && (!quit_munge))
768 char* check = textbuffer;
769 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
772 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
774 strlcpy(oper_quit,textbuffer,MAXQUIT);
775 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
781 memset(&already_sent,0,MAX_DESCRIPTORS);
783 unsigned int y = u->chans.size();
784 for (unsigned int i = 0; i < y; i++)
786 if (u->chans[i].channel)
788 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
789 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
792 userrec* otheruser = (userrec*)o;
795 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
797 already_sent[otheruser->fd] = 1;
800 WriteFrom_NoFormat(otheruser->fd,u,*otheruser->oper ? oper_quit : textbuffer);
802 else WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
810 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
814 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
818 if (u->registered != 7) {
819 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
823 memset(&already_sent,0,MAX_DESCRIPTORS);
825 unsigned int y = u->chans.size();
826 for (unsigned int i = 0; i < y; i++)
828 if (u->chans[i].channel)
830 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
831 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
834 userrec* otheruser = (userrec*)o;
837 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
839 already_sent[otheruser->fd] = 1;
840 WriteFrom_NoFormat(otheruser->fd,u,text);
850 void WriteOpers(char* text, ...)
854 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
858 char textbuffer[MAXBUF];
860 va_start (argsPtr, text);
861 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
864 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
869 if (strchr(a->modes,'s'))
871 // send server notices to all with +s
872 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
878 void ServerNoticeAll(char* text, ...)
883 char textbuffer[MAXBUF];
885 va_start (argsPtr, text);
886 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
889 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
891 userrec* t = (userrec*)(*i);
892 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
896 void ServerPrivmsgAll(char* text, ...)
901 char textbuffer[MAXBUF];
903 va_start (argsPtr, text);
904 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
907 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
909 userrec* t = (userrec*)(*i);
910 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
914 void WriteMode(const char* modes, int flags, const char* text, ...)
916 if ((!text) || (!modes) || (!flags))
918 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
922 char textbuffer[MAXBUF];
924 va_start (argsPtr, text);
925 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
927 int modelen = strlen(modes);
929 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
931 userrec* t = (userrec*)(*i);
932 bool send_to_user = false;
936 for (int n = 0; n < modelen; n++)
938 if (!hasumode(t,modes[n]))
940 send_to_user = false;
945 else if (flags == WM_OR)
947 send_to_user = false;
948 for (int n = 0; n < modelen; n++)
950 if (hasumode(t,modes[n]))
959 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
964 void NoticeAll(userrec *source, bool local_only, char* text, ...)
966 if ((!text) || (!source))
968 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
972 char textbuffer[MAXBUF];
974 va_start (argsPtr, text);
975 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
978 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
980 userrec* t = (userrec*)(*i);
981 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
987 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
989 if ((!text) || (!source))
991 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
995 char textbuffer[MAXBUF];
997 va_start (argsPtr, text);
998 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1001 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1003 userrec* t = (userrec*)(*i);
1004 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1006 WriteTo(source,t,"WALLOPS :%s",textbuffer);
1011 /* convert a string to lowercase. Note following special circumstances
1012 * taken from RFC 1459. Many "official" server branches still hold to this
1013 * rule so i will too;
1015 * Because of IRC's scandanavian origin, the characters {}| are
1016 * considered to be the lower case equivalents of the characters []\,
1017 * respectively. This is a critical issue when determining the
1018 * equivalence of two nicknames.
1021 void strlower(char *n)
1025 for (char* t = n; *t; t++)
1026 *t = lowermap[(unsigned)*t];
1030 /* Find a user record by nickname and return a pointer to it */
1032 userrec* Find(std::string nick)
1034 user_hash::iterator iter = clientlist.find(nick);
1036 if (iter == clientlist.end())
1037 /* Couldn't find it */
1040 return iter->second;
1043 userrec* Find(const char* nick)
1047 user_hash::iterator iter = clientlist.find(nick);
1049 if (iter == clientlist.end())
1052 return iter->second;
1055 /* find a channel record by channel name and return a pointer to it */
1057 chanrec* FindChan(const char* chan)
1061 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1065 chan_hash::iterator iter = chanlist.find(chan);
1067 if (iter == chanlist.end())
1068 /* Couldn't find it */
1071 return iter->second;
1075 long GetMaxBans(char* name)
1078 for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
1080 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
1083 Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
1090 void purge_empty_chans(userrec* u)
1095 // firstly decrement the count on each channel
1096 for (unsigned int f = 0; f < u->chans.size(); f++)
1098 if (u->chans[f].channel)
1100 u->chans[f].channel->DelUser((char*)u);
1104 for (unsigned int i = 0; i < u->chans.size(); i++)
1106 if (u->chans[i].channel)
1108 if (!usercount(u->chans[i].channel))
1110 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1111 /* kill the record */
1112 if (i2 != chanlist.end())
1114 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1117 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1122 u->chans[i].channel = NULL;
1127 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1131 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1137 char* chanmodes(chanrec *chan, bool showkey)
1139 static char scratch[MAXBUF];
1140 static char sparam[MAXBUF];
1141 char* offset = scratch;
1145 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1152 if (chan->binarymodes & CM_NOEXTERNAL)
1154 if (chan->binarymodes & CM_TOPICLOCK)
1160 if (chan->binarymodes & CM_INVITEONLY)
1162 if (chan->binarymodes & CM_MODERATED)
1164 if (chan->binarymodes & CM_SECRET)
1166 if (chan->binarymodes & CM_PRIVATE)
1170 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1175 sprintf(foo," %lu",(unsigned long)chan->limit);
1176 strlcat(sparam,foo,MAXBUF);
1178 if (*chan->custom_modes)
1180 for (char* t = chan->custom_modes; *t; t++)
1182 for (int z = 0; chan->custom_modes[z]; z++)
1184 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1187 charlcat(sparam,' ',MAXBUF);
1188 strlcat(sparam,extparam.c_str(),MAXBUF);
1192 /* Null terminate scratch */
1194 strlcat(scratch,sparam,MAXMODES);
1199 /* compile a userlist of a channel into a string, each nick seperated by
1200 * spaces and op, voice etc status shown as @ and + */
1202 void userlist(userrec *user,chanrec *c)
1204 if ((!c) || (!user))
1206 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1210 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1212 std::map<char*,char*> *ulist= c->GetUsers();
1213 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1215 char* o = i->second;
1216 userrec* otheruser = (userrec*)o;
1217 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1219 /* user is +i, and source not on the channel, does not show
1220 * nick in NAMES list */
1223 strlcat(list,cmode(otheruser,c),MAXBUF);
1224 strlcat(list,otheruser->nick,MAXBUF);
1225 charlcat(list,' ',MAXBUF);
1226 if (strlen(list)>(480-NICKMAX))
1228 /* list overflowed into
1229 * multiple numerics */
1230 WriteServ_NoFormat(user->fd,list);
1231 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1234 /* if whats left in the list isnt empty, send it */
1235 if (list[strlen(list)-1] != ':')
1237 WriteServ_NoFormat(user->fd,list);
1241 /* return a count of the users on a specific channel accounting for
1242 * invisible users who won't increase the count. e.g. for /LIST */
1244 int usercount_i(chanrec *c)
1250 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1254 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1256 if (has_channel(i->second,c))
1258 if (i->second->registered == 7)
1260 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1262 /* user is +i, and source not on the channel, does not show
1263 * nick in NAMES list */
1270 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1275 int usercount(chanrec *c)
1277 return (c ? c->GetUserCounter() : 0);
1281 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1283 ConnectClass GetClass(userrec *user)
1285 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1287 if (match(user->host,i->host.c_str()))
1292 return *(Config->Classes.begin());
1295 /* sends out an error notice to all connected clients (not to be used
1298 void send_error(char *s)
1300 log(DEBUG,"send_error: %s",s);
1301 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1303 userrec* t = (userrec*)(*i);
1304 if (t->registered == 7)
1306 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1310 // fix - unregistered connections receive ERROR, not NOTICE
1311 Write(t->fd,"ERROR :%s",s);
1316 void Error(int status)
1318 signal (SIGALRM, SIG_IGN);
1319 signal (SIGPIPE, SIG_IGN);
1320 signal (SIGTERM, SIG_IGN);
1321 signal (SIGABRT, SIG_IGN);
1322 signal (SIGSEGV, SIG_IGN);
1323 signal (SIGURG, SIG_IGN);
1324 signal (SIGKILL, SIG_IGN);
1325 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1326 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1330 // this function counts all users connected, wether they are registered or NOT.
1333 return clientlist.size();
1336 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1337 int registered_usercount(void)
1340 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1342 if (i->second->registered == 7) c++;
1347 int usercount_invisible(void)
1350 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1352 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1357 int usercount_opers(void)
1360 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1361 if (*i->second->oper) c++;
1365 int usercount_unknown(void)
1368 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1370 userrec* t = (userrec*)(*i);
1371 if (t->registered != 7)
1377 long chancount(void)
1379 return chanlist.size();
1385 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1387 userrec* t = (userrec*)(*i);
1388 if (t->registered == 7) c++;
1393 void ShowMOTD(userrec *user)
1395 static char mbuf[MAXBUF];
1396 static char crud[MAXBUF];
1397 std::string WholeMOTD = "";
1398 if (!Config->MOTD.size())
1400 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1403 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1404 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1405 WholeMOTD = WholeMOTD + mbuf;
1406 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1407 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1408 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1409 WholeMOTD = WholeMOTD + mbuf;
1410 // only one write operation
1411 if (Config->GetIOHook(user->port))
1415 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1417 catch (ModuleException& modexcept)
1419 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
1425 user->AddWriteBuf(WholeMOTD);
1427 ServerInstance->stats->statsSent += WholeMOTD.length();
1430 void ShowRULES(userrec *user)
1432 if (!Config->RULES.size())
1434 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1437 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1438 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1439 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1440 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1443 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1444 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1445 // registration timeout maximum seconds)
1446 bool AllModulesReportReady(userrec* user)
1448 if (!Config->global_implementation[I_OnCheckReady])
1450 for (int i = 0; i <= MODCOUNT; i++)
1452 if (Config->implement_lists[i][I_OnCheckReady])
1454 int res = modules[i]->OnCheckReady(user);
1462 bool DirValid(char* dirandfile)
1465 char buffer[MAXBUF], otherdir[MAXBUF];
1466 strlcpy(work,dirandfile,MAXBUF);
1467 int p = strlen(work);
1468 // we just want the dir
1478 // Get the current working directory
1479 if( getcwd( buffer, MAXBUF ) == NULL )
1482 if( getcwd( otherdir, MAXBUF ) == NULL )
1485 if (strlen(otherdir) >= strlen(work))
1487 otherdir[strlen(work)] = '\0';
1488 if (!strcmp(otherdir,work))
1497 std::string GetFullProgDir(char** argv, int argc)
1500 char buffer[MAXBUF], otherdir[MAXBUF];
1501 strlcpy(work,argv[0],MAXBUF);
1502 int p = strlen(work);
1503 // we just want the dir
1513 // Get the current working directory
1514 if( getcwd( buffer, MAXBUF ) == NULL )
1517 if( getcwd( otherdir, MAXBUF ) == NULL )
1523 int InsertMode(std::string &output, const char* mode, unsigned short section)
1525 unsigned short currsection = 1;
1526 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1528 if(section > 4 || section == 0)
1530 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1534 for(; pos < output.size(); pos++)
1536 if(section == currsection)
1539 if(output[pos] == ',')
1543 output.insert(pos, mode);
1547 bool IsValidChannelName(const char *chname)
1551 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1552 if (!chname || *chname != '#')
1557 c = (char *)chname + 1;
1571 /* too long a name - note funky pointer arithmetic here. */
1572 if ((c - chname) > CHANMAX)
1580 inline int charlcat(char* x,char y,int z)
1594 bool charremove(char* mp, char remove)
1597 bool shift_down = false;
1600 if (*mptr == remove)