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 /** Writes information about events to a file.
72 * Write a line of text `text' to the logfile (and stdout, if in nofork) if the level `level'
73 * is greater than the configured loglevel.
75 void log(int level, char *text, ...)
79 if (level < Config->LogLevel)
82 char textbuffer[MAXBUF];
86 timeinfo = localtime(&TIME);
87 strlcpy(TIMESTR,asctime(timeinfo),26);
94 va_start (argsPtr, text);
95 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
99 fprintf(Config->log_file,"%s %s\n",TIMESTR,textbuffer);
102 // nofork enabled? display it on terminal too
103 printf("%s %s\n",TIMESTR,textbuffer);
110 * Read the contents of a file located by `fname' into a file_cache pointed at by `F'.
112 * XXX - we may want to consider returning a file_cache or pointer to one, less confusing.
114 void readfile(file_cache &F, const char* fname)
117 char linebuf[MAXBUF];
119 log(DEBUG,"readfile: loading %s",fname);
121 file = fopen(fname,"r");
126 fgets(linebuf,sizeof(linebuf),file);
127 linebuf[strlen(linebuf)-1]='\0';
134 F.push_back(linebuf);
141 log(DEBUG,"readfile: failed to load file: %s",fname);
143 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
146 void Write_NoFormat(int sock, const char *text)
148 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
152 int bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
154 if (fd_ref_table[sock])
156 if (Config->GetIOHook(fd_ref_table[sock]->port))
160 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
162 catch (ModuleException& modexcept)
164 log(DEBUG,"Module exception caught: %s",modexcept.GetReason());
169 fd_ref_table[sock]->AddWriteBuf(tb);
171 ServerInstance->stats->statsSent += bytes;
173 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
176 void Write(int sock,char *text, ...)
178 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
182 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
186 char textbuffer[MAXBUF],tb[MAXBUF];
187 va_start (argsPtr, text);
188 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
190 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
192 if (fd_ref_table[sock])
194 if (Config->GetIOHook(fd_ref_table[sock]->port))
198 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
200 catch (ModuleException& modexcept)
202 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
207 fd_ref_table[sock]->AddWriteBuf(tb);
209 ServerInstance->stats->statsSent += bytes;
211 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
214 void WriteServ_NoFormat(int sock, const char* text)
216 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
219 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
221 if (fd_ref_table[sock])
223 if (Config->GetIOHook(fd_ref_table[sock]->port))
227 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
229 catch (ModuleException& modexcept)
231 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
236 fd_ref_table[sock]->AddWriteBuf(tb);
238 ServerInstance->stats->statsSent += bytes;
240 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
243 /* write a server formatted numeric response to a single socket */
245 void WriteServ(int sock, char* text, ...)
247 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
251 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
255 va_start (argsPtr, text);
256 char textbuffer[MAXBUF],tb[MAXBUF];
257 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
259 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
261 if (fd_ref_table[sock])
263 if (Config->GetIOHook(fd_ref_table[sock]->port))
267 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
269 catch (ModuleException& modexcept)
271 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
276 fd_ref_table[sock]->AddWriteBuf(tb);
278 ServerInstance->stats->statsSent += bytes;
280 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
283 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
285 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
288 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
290 if (fd_ref_table[sock])
292 if (Config->GetIOHook(fd_ref_table[sock]->port))
296 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
298 catch (ModuleException& modexcept)
300 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
305 fd_ref_table[sock]->AddWriteBuf(tb);
307 ServerInstance->stats->statsSent += bytes;
309 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
312 /* write text from an originating user to originating user */
314 void WriteFrom(int sock, userrec *user,char* text, ...)
316 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
318 if ((!text) || (!user))
320 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
324 va_start (argsPtr, text);
325 char textbuffer[MAXBUF],tb[MAXBUF];
326 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
328 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
330 if (fd_ref_table[sock])
332 if (Config->GetIOHook(fd_ref_table[sock]->port))
336 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
338 catch (ModuleException& modexcept)
340 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
345 fd_ref_table[sock]->AddWriteBuf(tb);
347 ServerInstance->stats->statsSent += bytes;
349 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
352 /* write text to an destination user from a source user (e.g. user privmsg) */
354 void WriteTo(userrec *source, userrec *dest,char *data, ...)
356 if ((!dest) || (!data))
358 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
363 char textbuffer[MAXBUF];
365 va_start (argsPtr, data);
366 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
370 // if no source given send it from the server.
373 WriteServ_NoFormat(dest->fd,textbuffer);
377 WriteFrom_NoFormat(dest->fd,source,textbuffer);
381 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
383 if ((!dest) || (!data))
387 WriteServ_NoFormat(dest->fd,data);
391 WriteFrom_NoFormat(dest->fd,source,data);
395 /* write formatted text from a source user to all users on a channel
396 * including the sender (NOT for privmsg, notice etc!) */
398 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
400 if ((!Ptr) || (!user) || (!text))
402 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
405 char textbuffer[MAXBUF];
407 va_start (argsPtr, text);
408 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
411 std::map<char*,char*> *ulist= Ptr->GetUsers();
412 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
415 userrec* otheruser = (userrec*)o;
416 if (otheruser->fd != FD_MAGIC_NUMBER)
417 WriteTo_NoFormat(user,otheruser,textbuffer);
421 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
423 if ((!Ptr) || (!user) || (!text))
425 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
428 std::map<char*,char*> *ulist= Ptr->GetUsers();
429 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
432 userrec* otheruser = (userrec*)o;
433 if (otheruser->fd != FD_MAGIC_NUMBER)
434 WriteTo_NoFormat(user,otheruser,text);
439 /* write formatted text from a source user to all users on a channel
440 * including the sender (NOT for privmsg, notice etc!) doesnt send to
441 * users on remote servers */
443 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
445 if ((!Ptr) || (!text))
447 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
450 char textbuffer[MAXBUF];
452 va_start (argsPtr, text);
453 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
456 std::map<char*,char*> *ulist= Ptr->GetUsers();
457 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
460 userrec* otheruser = (userrec*)o;
461 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
465 WriteServ_NoFormat(otheruser->fd,textbuffer);
469 WriteTo_NoFormat(user,otheruser,textbuffer);
475 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
477 if ((!Ptr) || (!text))
479 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
482 std::map<char*,char*> *ulist= Ptr->GetUsers();
483 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
486 userrec* otheruser = (userrec*)o;
487 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
491 WriteServ_NoFormat(otheruser->fd,text);
495 WriteTo_NoFormat(user,otheruser,text);
503 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
505 if ((!Ptr) || (!text))
507 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
510 char textbuffer[MAXBUF];
512 va_start (argsPtr, text);
513 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
517 std::map<char*,char*> *ulist= Ptr->GetUsers();
518 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
521 userrec* otheruser = (userrec*)o;
522 if (IS_LOCAL(otheruser))
523 WriteServ_NoFormat(otheruser->fd,textbuffer);
527 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
529 if ((!Ptr) || (!text))
531 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
534 std::map<char*,char*> *ulist= Ptr->GetUsers();
535 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
538 userrec* otheruser = (userrec*)o;
539 if (IS_LOCAL(otheruser))
540 WriteServ_NoFormat(otheruser->fd,text);
546 /* write formatted text from a source user to all users on a channel except
547 * for the sender (for privmsg etc) */
549 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
551 if ((!Ptr) || (!user) || (!text))
553 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
556 char textbuffer[MAXBUF];
558 va_start (argsPtr, text);
559 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
562 std::map<char*,char*> *ulist;
566 ulist = Ptr->GetOppedUsers();
569 ulist = Ptr->GetHalfoppedUsers();
572 ulist = Ptr->GetVoicedUsers();
575 ulist = Ptr->GetUsers();
578 log(DEBUG,"%d users to write to",ulist->size());
579 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
582 userrec* otheruser = (userrec*)o;
583 if ((IS_LOCAL(otheruser)) && (user != otheruser))
584 WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
588 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
590 if ((!Ptr) || (!user) || (!text))
592 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
595 std::map<char*,char*> *ulist;
599 ulist = Ptr->GetOppedUsers();
602 ulist = Ptr->GetHalfoppedUsers();
605 ulist = Ptr->GetVoicedUsers();
608 ulist = Ptr->GetUsers();
611 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
614 userrec* otheruser = (userrec*)o;
615 if ((IS_LOCAL(otheruser)) && (user != otheruser))
616 WriteFrom_NoFormat(otheruser->fd,user,text);
620 std::string GetServerDescription(char* servername)
622 std::string description = "";
623 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
624 if (description != "")
630 return Config->ServerDesc; // not a remote server that can be found, it must be me.
634 /* write a formatted string to all users who share at least one common
635 * channel, including the source user e.g. for use in NICK */
637 void WriteCommon(userrec *u, char* text, ...)
641 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
645 if (u->registered != 7) {
646 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
650 char textbuffer[MAXBUF];
652 va_start (argsPtr, text);
653 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
656 // FIX: Stops a message going to the same person more than once
657 memset(&already_sent,0,MAX_DESCRIPTORS);
659 bool sent_to_at_least_one = false;
661 unsigned int y = u->chans.size();
662 for (unsigned int i = 0; i < y; i++)
664 if (u->chans[i].channel)
666 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
667 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
670 userrec* otheruser = (userrec*)o;
671 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
673 already_sent[otheruser->fd] = 1;
674 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
675 sent_to_at_least_one = true;
680 // if the user was not in any channels, no users will receive the text. Make sure the user
681 // receives their OWN message for WriteCommon
682 if (!sent_to_at_least_one)
684 WriteFrom_NoFormat(u->fd,u,textbuffer);
688 void WriteCommon_NoFormat(userrec *u, const char* text)
692 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
696 if (u->registered != 7) {
697 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
700 // FIX: Stops a message going to the same person more than once
701 memset(&already_sent,0,MAX_DESCRIPTORS);
703 bool sent_to_at_least_one = false;
705 unsigned int y = u->chans.size();
706 for (unsigned int i = 0; i < y; i++)
708 if (u->chans[i].channel)
710 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
711 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
714 userrec* otheruser = (userrec*)o;
715 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
717 already_sent[otheruser->fd] = 1;
718 WriteFrom_NoFormat(otheruser->fd,u,text);
719 sent_to_at_least_one = true;
724 // if the user was not in any channels, no users will receive the text. Make sure the user
725 // receives their OWN message for WriteCommon
726 if (!sent_to_at_least_one)
728 WriteFrom_NoFormat(u->fd,u,text);
733 /* write a formatted string to all users who share at least one common
734 * channel, NOT including the source user e.g. for use in QUIT */
736 void WriteCommonExcept(userrec *u, char* text, ...)
740 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
744 if (u->registered != 7) {
745 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
749 char textbuffer[MAXBUF];
750 char oper_quit[MAXBUF];
751 bool quit_munge = false;
754 va_start (argsPtr, text);
755 int total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
758 if ((Config->HideSplits) && (total > 6))
760 /* Yeah yeah, this is ugly. But its fast, live with it. */
761 char* check = textbuffer;
762 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
764 std::stringstream split(check);
765 std::string server_one;
766 std::string server_two;
769 if ((FindServerName(server_one)) && (FindServerName(server_two)))
771 strlcpy(oper_quit,textbuffer,MAXQUIT);
772 strlcpy(check,"*.net *.split",MAXQUIT);
777 if ((Config->HideBans) && (total > 13) && (!quit_munge))
779 char* check = textbuffer;
780 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
783 if ((*check++ == '-') && (*check++ == 'L') && (*check++ == 'i') && (*check++ == 'n') && (*check++ == 'e') && (*check++ == 'd') && (*check++ == ':'))
785 strlcpy(oper_quit,textbuffer,MAXQUIT);
786 *(--check) = 0; // We don't need to strlcpy, we just chop it from the :
792 memset(&already_sent,0,MAX_DESCRIPTORS);
794 unsigned int y = u->chans.size();
795 for (unsigned int i = 0; i < y; i++)
797 if (u->chans[i].channel)
799 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
800 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
803 userrec* otheruser = (userrec*)o;
806 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
808 already_sent[otheruser->fd] = 1;
811 WriteFrom_NoFormat(otheruser->fd,u,*otheruser->oper ? oper_quit : textbuffer);
813 else WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
821 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
825 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
829 if (u->registered != 7) {
830 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
834 memset(&already_sent,0,MAX_DESCRIPTORS);
836 unsigned int y = u->chans.size();
837 for (unsigned int i = 0; i < y; i++)
839 if (u->chans[i].channel)
841 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
842 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
845 userrec* otheruser = (userrec*)o;
848 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
850 already_sent[otheruser->fd] = 1;
851 WriteFrom_NoFormat(otheruser->fd,u,text);
861 void WriteOpers(char* text, ...)
865 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
869 char textbuffer[MAXBUF];
871 va_start (argsPtr, text);
872 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
875 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
880 if (strchr(a->modes,'s'))
882 // send server notices to all with +s
883 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
889 void ServerNoticeAll(char* text, ...)
894 char textbuffer[MAXBUF];
896 va_start (argsPtr, text);
897 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
900 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
902 userrec* t = (userrec*)(*i);
903 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
907 void ServerPrivmsgAll(char* text, ...)
912 char textbuffer[MAXBUF];
914 va_start (argsPtr, text);
915 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
918 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
920 userrec* t = (userrec*)(*i);
921 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
925 void WriteMode(const char* modes, int flags, const char* text, ...)
927 if ((!text) || (!modes) || (!flags))
929 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
933 char textbuffer[MAXBUF];
935 va_start (argsPtr, text);
936 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
938 int modelen = strlen(modes);
940 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
942 userrec* t = (userrec*)(*i);
943 bool send_to_user = false;
947 for (int n = 0; n < modelen; n++)
949 if (!hasumode(t,modes[n]))
951 send_to_user = false;
956 else if (flags == WM_OR)
958 send_to_user = false;
959 for (int n = 0; n < modelen; n++)
961 if (hasumode(t,modes[n]))
970 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
975 void NoticeAll(userrec *source, bool local_only, char* text, ...)
977 if ((!text) || (!source))
979 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
983 char textbuffer[MAXBUF];
985 va_start (argsPtr, text);
986 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
989 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
991 userrec* t = (userrec*)(*i);
992 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
998 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
1000 if ((!text) || (!source))
1002 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
1006 char textbuffer[MAXBUF];
1008 va_start (argsPtr, text);
1009 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
1012 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1014 userrec* t = (userrec*)(*i);
1015 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
1017 WriteTo(source,t,"WALLOPS :%s",textbuffer);
1022 /* convert a string to lowercase. Note following special circumstances
1023 * taken from RFC 1459. Many "official" server branches still hold to this
1024 * rule so i will too;
1026 * Because of IRC's scandanavian origin, the characters {}| are
1027 * considered to be the lower case equivalents of the characters []\,
1028 * respectively. This is a critical issue when determining the
1029 * equivalence of two nicknames.
1032 void strlower(char *n)
1036 for (char* t = n; *t; t++)
1037 *t = lowermap[(unsigned)*t];
1041 /* Find a user record by nickname and return a pointer to it */
1043 userrec* Find(std::string nick)
1045 user_hash::iterator iter = clientlist.find(nick);
1047 if (iter == clientlist.end())
1048 /* Couldn't find it */
1051 return iter->second;
1054 userrec* Find(const char* nick)
1058 user_hash::iterator iter = clientlist.find(nick);
1060 if (iter == clientlist.end())
1063 return iter->second;
1066 /* find a channel record by channel name and return a pointer to it */
1068 chanrec* FindChan(const char* chan)
1072 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1076 chan_hash::iterator iter = chanlist.find(chan);
1078 if (iter == chanlist.end())
1079 /* Couldn't find it */
1082 return iter->second;
1086 long GetMaxBans(char* name)
1089 for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
1091 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
1094 Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
1101 void purge_empty_chans(userrec* u)
1106 // firstly decrement the count on each channel
1107 for (unsigned int f = 0; f < u->chans.size(); f++)
1109 if (u->chans[f].channel)
1111 u->chans[f].channel->DelUser((char*)u);
1115 for (unsigned int i = 0; i < u->chans.size(); i++)
1117 if (u->chans[i].channel)
1119 if (!usercount(u->chans[i].channel))
1121 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1122 /* kill the record */
1123 if (i2 != chanlist.end())
1125 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1128 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1133 u->chans[i].channel = NULL;
1138 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1142 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1148 char* chanmodes(chanrec *chan, bool showkey)
1150 static char scratch[MAXBUF];
1151 static char sparam[MAXBUF];
1152 char* offset = scratch;
1156 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1163 if (chan->binarymodes & CM_NOEXTERNAL)
1165 if (chan->binarymodes & CM_TOPICLOCK)
1171 if (chan->binarymodes & CM_INVITEONLY)
1173 if (chan->binarymodes & CM_MODERATED)
1175 if (chan->binarymodes & CM_SECRET)
1177 if (chan->binarymodes & CM_PRIVATE)
1181 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1186 sprintf(foo," %lu",(unsigned long)chan->limit);
1187 strlcat(sparam,foo,MAXBUF);
1189 if (*chan->custom_modes)
1191 for (char* t = chan->custom_modes; *t; t++)
1193 for (int z = 0; chan->custom_modes[z]; z++)
1195 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1198 charlcat(sparam,' ',MAXBUF);
1199 strlcat(sparam,extparam.c_str(),MAXBUF);
1203 /* Null terminate scratch */
1205 strlcat(scratch,sparam,MAXMODES);
1210 /* compile a userlist of a channel into a string, each nick seperated by
1211 * spaces and op, voice etc status shown as @ and + */
1213 void userlist(userrec *user,chanrec *c)
1215 if ((!c) || (!user))
1217 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1221 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1223 std::map<char*,char*> *ulist= c->GetUsers();
1224 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1226 char* o = i->second;
1227 userrec* otheruser = (userrec*)o;
1228 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1230 /* user is +i, and source not on the channel, does not show
1231 * nick in NAMES list */
1234 strlcat(list,cmode(otheruser,c),MAXBUF);
1235 strlcat(list,otheruser->nick,MAXBUF);
1236 charlcat(list,' ',MAXBUF);
1237 if (strlen(list)>(480-NICKMAX))
1239 /* list overflowed into
1240 * multiple numerics */
1241 WriteServ_NoFormat(user->fd,list);
1242 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1245 /* if whats left in the list isnt empty, send it */
1246 if (list[strlen(list)-1] != ':')
1248 WriteServ_NoFormat(user->fd,list);
1252 /* return a count of the users on a specific channel accounting for
1253 * invisible users who won't increase the count. e.g. for /LIST */
1255 int usercount_i(chanrec *c)
1261 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1265 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1267 if (has_channel(i->second,c))
1269 if (i->second->registered == 7)
1271 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1273 /* user is +i, and source not on the channel, does not show
1274 * nick in NAMES list */
1281 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1286 int usercount(chanrec *c)
1288 return (c ? c->GetUserCounter() : 0);
1292 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1294 ConnectClass GetClass(userrec *user)
1296 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1298 if (match(user->host,i->host.c_str()))
1303 return *(Config->Classes.begin());
1306 /* sends out an error notice to all connected clients (not to be used
1309 void send_error(char *s)
1311 log(DEBUG,"send_error: %s",s);
1312 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1314 userrec* t = (userrec*)(*i);
1315 if (t->registered == 7)
1317 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1321 // fix - unregistered connections receive ERROR, not NOTICE
1322 Write(t->fd,"ERROR :%s",s);
1327 void Error(int status)
1329 signal (SIGALRM, SIG_IGN);
1330 signal (SIGPIPE, SIG_IGN);
1331 signal (SIGTERM, SIG_IGN);
1332 signal (SIGABRT, SIG_IGN);
1333 signal (SIGSEGV, SIG_IGN);
1334 signal (SIGURG, SIG_IGN);
1335 signal (SIGKILL, SIG_IGN);
1336 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1337 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1341 // this function counts all users connected, wether they are registered or NOT.
1344 return clientlist.size();
1347 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1348 int registered_usercount(void)
1351 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1353 if (i->second->registered == 7) c++;
1358 int usercount_invisible(void)
1361 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1363 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1368 int usercount_opers(void)
1371 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1372 if (*i->second->oper) c++;
1376 int usercount_unknown(void)
1379 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1381 userrec* t = (userrec*)(*i);
1382 if (t->registered != 7)
1388 long chancount(void)
1390 return chanlist.size();
1396 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1398 userrec* t = (userrec*)(*i);
1399 if (t->registered == 7) c++;
1404 void ShowMOTD(userrec *user)
1406 static char mbuf[MAXBUF];
1407 static char crud[MAXBUF];
1408 std::string WholeMOTD = "";
1409 if (!Config->MOTD.size())
1411 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1414 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1415 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1416 WholeMOTD = WholeMOTD + mbuf;
1417 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1418 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1419 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1420 WholeMOTD = WholeMOTD + mbuf;
1421 // only one write operation
1422 if (Config->GetIOHook(user->port))
1426 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1428 catch (ModuleException& modexcept)
1430 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
1436 user->AddWriteBuf(WholeMOTD);
1438 ServerInstance->stats->statsSent += WholeMOTD.length();
1441 void ShowRULES(userrec *user)
1443 if (!Config->RULES.size())
1445 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1448 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1449 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1450 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1451 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1454 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1455 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1456 // registration timeout maximum seconds)
1457 bool AllModulesReportReady(userrec* user)
1459 if (!Config->global_implementation[I_OnCheckReady])
1461 for (int i = 0; i <= MODCOUNT; i++)
1463 if (Config->implement_lists[i][I_OnCheckReady])
1465 int res = modules[i]->OnCheckReady(user);
1473 bool DirValid(char* dirandfile)
1476 char buffer[MAXBUF], otherdir[MAXBUF];
1477 strlcpy(work,dirandfile,MAXBUF);
1478 int p = strlen(work);
1479 // we just want the dir
1489 // Get the current working directory
1490 if( getcwd( buffer, MAXBUF ) == NULL )
1493 if( getcwd( otherdir, MAXBUF ) == NULL )
1496 if (strlen(otherdir) >= strlen(work))
1498 otherdir[strlen(work)] = '\0';
1499 if (!strcmp(otherdir,work))
1508 std::string GetFullProgDir(char** argv, int argc)
1511 char buffer[MAXBUF], otherdir[MAXBUF];
1512 strlcpy(work,argv[0],MAXBUF);
1513 int p = strlen(work);
1514 // we just want the dir
1524 // Get the current working directory
1525 if( getcwd( buffer, MAXBUF ) == NULL )
1528 if( getcwd( otherdir, MAXBUF ) == NULL )
1534 int InsertMode(std::string &output, const char* mode, unsigned short section)
1536 unsigned short currsection = 1;
1537 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1539 if(section > 4 || section == 0)
1541 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1545 for(; pos < output.size(); pos++)
1547 if(section == currsection)
1550 if(output[pos] == ',')
1554 output.insert(pos, mode);
1558 bool IsValidChannelName(const char *chname)
1562 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1563 if (!chname || *chname != '#')
1568 c = (char *)chname + 1;
1582 /* too long a name - note funky pointer arithmetic here. */
1583 if ((c - chname) > CHANMAX)
1591 inline int charlcat(char* x,char y,int z)
1605 bool charremove(char* mp, char remove)
1608 bool shift_down = false;
1611 if (*mptr == remove)