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 void log(int level,char *text, ...)
71 if (level < Config->LogLevel)
73 char textbuffer[MAXBUF];
74 timeinfo = localtime(&TIME);
79 va_start (argsPtr, text);
80 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
82 strlcpy(b,asctime(timeinfo),26);
83 b[24] = ':'; // we know this is the end of the time string
85 fprintf(Config->log_file,"%s %s\n",b,textbuffer);
88 // nofork enabled? display it on terminal too
89 printf("%s %s\n",b,textbuffer);
94 void readfile(file_cache &F, const char* fname)
99 log(DEBUG,"readfile: loading %s",fname);
101 file = fopen(fname,"r");
106 fgets(linebuf,sizeof(linebuf),file);
107 linebuf[strlen(linebuf)-1]='\0';
114 F.push_back(linebuf);
121 log(DEBUG,"readfile: failed to load file: %s",fname);
123 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
126 void Write_NoFormat(int sock, const char *text)
128 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
132 int bytes = snprintf(tb,MAXBUF,"%s\r\n",text);
134 if (fd_ref_table[sock])
136 if (Config->GetIOHook(fd_ref_table[sock]->port))
140 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
142 catch (ModuleException& modexcept)
144 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
149 fd_ref_table[sock]->AddWriteBuf(tb);
151 ServerInstance->stats->statsSent += bytes;
153 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
156 void Write(int sock,char *text, ...)
158 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
162 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
166 char textbuffer[MAXBUF],tb[MAXBUF];
167 va_start (argsPtr, text);
168 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
170 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
172 if (fd_ref_table[sock])
174 if (Config->GetIOHook(fd_ref_table[sock]->port))
178 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
180 catch (ModuleException& modexcept)
182 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
187 fd_ref_table[sock]->AddWriteBuf(tb);
189 ServerInstance->stats->statsSent += bytes;
191 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
194 void WriteServ_NoFormat(int sock, const char* text)
196 if ((sock < 0) || (!text) || (sock > MAX_DESCRIPTORS))
199 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,text);
201 if (fd_ref_table[sock])
203 if (Config->GetIOHook(fd_ref_table[sock]->port))
207 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
209 catch (ModuleException& modexcept)
211 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
216 fd_ref_table[sock]->AddWriteBuf(tb);
218 ServerInstance->stats->statsSent += bytes;
220 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
223 /* write a server formatted numeric response to a single socket */
225 void WriteServ(int sock, char* text, ...)
227 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
231 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
235 va_start (argsPtr, text);
236 char textbuffer[MAXBUF],tb[MAXBUF];
237 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
239 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
241 if (fd_ref_table[sock])
243 if (Config->GetIOHook(fd_ref_table[sock]->port))
247 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
249 catch (ModuleException& modexcept)
251 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
256 fd_ref_table[sock]->AddWriteBuf(tb);
258 ServerInstance->stats->statsSent += bytes;
260 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
263 void WriteFrom_NoFormat(int sock, userrec *user, const char* text)
265 if ((sock < 0) || (!text) || (!user) || (sock > MAX_DESCRIPTORS))
268 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),text);
270 if (fd_ref_table[sock])
272 if (Config->GetIOHook(fd_ref_table[sock]->port))
276 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
278 catch (ModuleException& modexcept)
280 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
285 fd_ref_table[sock]->AddWriteBuf(tb);
287 ServerInstance->stats->statsSent += bytes;
289 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
292 /* write text from an originating user to originating user */
294 void WriteFrom(int sock, userrec *user,char* text, ...)
296 if ((sock < 0) || (sock > MAX_DESCRIPTORS))
298 if ((!text) || (!user))
300 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
304 va_start (argsPtr, text);
305 char textbuffer[MAXBUF],tb[MAXBUF];
306 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
308 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",user->GetFullHost(),textbuffer);
310 if (fd_ref_table[sock])
312 if (Config->GetIOHook(fd_ref_table[sock]->port))
316 Config->GetIOHook(fd_ref_table[sock]->port)->OnRawSocketWrite(sock,tb,bytes);
318 catch (ModuleException& modexcept)
320 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
325 fd_ref_table[sock]->AddWriteBuf(tb);
327 ServerInstance->stats->statsSent += bytes;
329 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
332 /* write text to an destination user from a source user (e.g. user privmsg) */
334 void WriteTo(userrec *source, userrec *dest,char *data, ...)
336 if ((!dest) || (!data))
338 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
343 char textbuffer[MAXBUF];
345 va_start (argsPtr, data);
346 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
350 // if no source given send it from the server.
353 WriteServ_NoFormat(dest->fd,textbuffer);
357 WriteFrom_NoFormat(dest->fd,source,textbuffer);
361 void WriteTo_NoFormat(userrec *source, userrec *dest, const char *data)
363 if ((!dest) || (!data))
367 WriteServ_NoFormat(dest->fd,data);
371 WriteFrom_NoFormat(dest->fd,source,data);
375 /* write formatted text from a source user to all users on a channel
376 * including the sender (NOT for privmsg, notice etc!) */
378 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
380 if ((!Ptr) || (!user) || (!text))
382 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
385 char textbuffer[MAXBUF];
387 va_start (argsPtr, text);
388 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
391 std::map<char*,char*> *ulist= Ptr->GetUsers();
392 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
395 userrec* otheruser = (userrec*)o;
396 if (otheruser->fd != FD_MAGIC_NUMBER)
397 WriteTo_NoFormat(user,otheruser,textbuffer);
401 void WriteChannel_NoFormat(chanrec* Ptr, userrec* user, const char* text)
403 if ((!Ptr) || (!user) || (!text))
405 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
408 std::map<char*,char*> *ulist= Ptr->GetUsers();
409 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
412 userrec* otheruser = (userrec*)o;
413 if (otheruser->fd != FD_MAGIC_NUMBER)
414 WriteTo_NoFormat(user,otheruser,text);
419 /* write formatted text from a source user to all users on a channel
420 * including the sender (NOT for privmsg, notice etc!) doesnt send to
421 * users on remote servers */
423 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
425 if ((!Ptr) || (!text))
427 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
430 char textbuffer[MAXBUF];
432 va_start (argsPtr, text);
433 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
436 std::map<char*,char*> *ulist= Ptr->GetUsers();
437 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
440 userrec* otheruser = (userrec*)o;
441 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
445 WriteServ_NoFormat(otheruser->fd,textbuffer);
449 WriteTo_NoFormat(user,otheruser,textbuffer);
455 void WriteChannelLocal_NoFormat(chanrec* Ptr, userrec* user, const char* text)
457 if ((!Ptr) || (!text))
459 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
462 std::map<char*,char*> *ulist= Ptr->GetUsers();
463 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
466 userrec* otheruser = (userrec*)o;
467 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser != user))
471 WriteServ_NoFormat(otheruser->fd,text);
475 WriteTo_NoFormat(user,otheruser,text);
483 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
485 if ((!Ptr) || (!text))
487 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
490 char textbuffer[MAXBUF];
492 va_start (argsPtr, text);
493 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
497 std::map<char*,char*> *ulist= Ptr->GetUsers();
498 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
501 userrec* otheruser = (userrec*)o;
502 if (IS_LOCAL(otheruser))
503 WriteServ_NoFormat(otheruser->fd,textbuffer);
507 void WriteChannelWithServ_NoFormat(char* ServName, chanrec* Ptr, const char* text)
509 if ((!Ptr) || (!text))
511 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
514 std::map<char*,char*> *ulist= Ptr->GetUsers();
515 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
518 userrec* otheruser = (userrec*)o;
519 if (IS_LOCAL(otheruser))
520 WriteServ_NoFormat(otheruser->fd,text);
526 /* write formatted text from a source user to all users on a channel except
527 * for the sender (for privmsg etc) */
529 void ChanExceptSender(chanrec* Ptr, userrec* user, char status, char* text, ...)
531 if ((!Ptr) || (!user) || (!text))
533 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
536 char textbuffer[MAXBUF];
538 va_start (argsPtr, text);
539 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
542 std::map<char*,char*> *ulist;
546 ulist = Ptr->GetOppedUsers();
549 ulist = Ptr->GetHalfoppedUsers();
552 ulist = Ptr->GetVoicedUsers();
555 ulist = Ptr->GetUsers();
558 log(DEBUG,"%d users to write to",ulist->size());
559 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
562 userrec* otheruser = (userrec*)o;
563 if ((IS_LOCAL(otheruser)) && (user != otheruser))
564 WriteFrom_NoFormat(otheruser->fd,user,textbuffer);
568 void ChanExceptSender_NoFormat(chanrec* Ptr, userrec* user, char status, const char* text)
570 if ((!Ptr) || (!user) || (!text))
572 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
575 std::map<char*,char*> *ulist;
579 ulist = Ptr->GetOppedUsers();
582 ulist = Ptr->GetHalfoppedUsers();
585 ulist = Ptr->GetVoicedUsers();
588 ulist = Ptr->GetUsers();
591 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
594 userrec* otheruser = (userrec*)o;
595 if ((IS_LOCAL(otheruser)) && (user != otheruser))
596 WriteFrom_NoFormat(otheruser->fd,user,text);
600 std::string GetServerDescription(char* servername)
602 std::string description = "";
603 FOREACH_MOD(I_OnGetServerDescription,OnGetServerDescription(servername,description));
604 if (description != "")
610 return Config->ServerDesc; // not a remote server that can be found, it must be me.
614 /* write a formatted string to all users who share at least one common
615 * channel, including the source user e.g. for use in NICK */
617 void WriteCommon(userrec *u, char* text, ...)
621 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
625 if (u->registered != 7) {
626 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
630 char textbuffer[MAXBUF];
632 va_start (argsPtr, text);
633 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
636 // FIX: Stops a message going to the same person more than once
637 memset(&already_sent,0,MAX_DESCRIPTORS);
639 bool sent_to_at_least_one = false;
641 unsigned int y = u->chans.size();
642 for (unsigned int i = 0; i < y; i++)
644 if (u->chans[i].channel)
646 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
647 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
650 userrec* otheruser = (userrec*)o;
651 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
653 already_sent[otheruser->fd] = 1;
654 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
655 sent_to_at_least_one = true;
660 // if the user was not in any channels, no users will receive the text. Make sure the user
661 // receives their OWN message for WriteCommon
662 if (!sent_to_at_least_one)
664 WriteFrom_NoFormat(u->fd,u,textbuffer);
668 void WriteCommon_NoFormat(userrec *u, const char* text)
672 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
676 if (u->registered != 7) {
677 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
680 // FIX: Stops a message going to the same person more than once
681 memset(&already_sent,0,MAX_DESCRIPTORS);
683 bool sent_to_at_least_one = false;
685 unsigned int y = u->chans.size();
686 for (unsigned int i = 0; i < y; i++)
688 if (u->chans[i].channel)
690 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
691 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
694 userrec* otheruser = (userrec*)o;
695 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
697 already_sent[otheruser->fd] = 1;
698 WriteFrom_NoFormat(otheruser->fd,u,text);
699 sent_to_at_least_one = true;
704 // if the user was not in any channels, no users will receive the text. Make sure the user
705 // receives their OWN message for WriteCommon
706 if (!sent_to_at_least_one)
708 WriteFrom_NoFormat(u->fd,u,text);
713 /* write a formatted string to all users who share at least one common
714 * channel, NOT including the source user e.g. for use in QUIT */
716 void WriteCommonExcept(userrec *u, char* text, ...)
720 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
724 if (u->registered != 7) {
725 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
729 char textbuffer[MAXBUF];
730 char oper_quit[MAXBUF];
731 bool quit_munge = false;
734 va_start (argsPtr, text);
735 int total = vsnprintf(textbuffer, MAXBUF, text, argsPtr);
738 if ((Config->HideSplits) && (total > 6))
740 /* Yeah yeah, this is ugly. But its fast, live with it. */
741 char* check = textbuffer;
742 if ((*check++ == 'Q') && (*check++ == 'U') && (*check++ == 'I') && (*check++ == 'T') && (*check++ == ' ') && (*check++ == ':'))
744 std::stringstream split(check);
745 std::string server_one;
746 std::string server_two;
749 if ((FindServerName(server_one)) && (FindServerName(server_two)))
751 strlcpy(oper_quit,textbuffer,MAXBUF);
752 strlcpy(check,"*.net *.split",MAXQUIT);
758 memset(&already_sent,0,MAX_DESCRIPTORS);
760 unsigned int y = u->chans.size();
761 for (unsigned int i = 0; i < y; i++)
763 if (u->chans[i].channel)
765 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
766 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
769 userrec* otheruser = (userrec*)o;
772 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
774 already_sent[otheruser->fd] = 1;
777 if (*otheruser->oper)
779 WriteFrom_NoFormat(otheruser->fd,u,oper_quit);
783 WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
786 else WriteFrom_NoFormat(otheruser->fd,u,textbuffer);
794 void WriteCommonExcept_NoFormat(userrec *u, const char* text)
798 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
802 if (u->registered != 7) {
803 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
807 memset(&already_sent,0,MAX_DESCRIPTORS);
809 unsigned int y = u->chans.size();
810 for (unsigned int i = 0; i < y; i++)
812 if (u->chans[i].channel)
814 std::map<char*,char*> *ulist= u->chans[i].channel->GetUsers();
815 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
818 userrec* otheruser = (userrec*)o;
821 if ((otheruser->fd > -1) && (!already_sent[otheruser->fd]))
823 already_sent[otheruser->fd] = 1;
824 WriteFrom_NoFormat(otheruser->fd,u,text);
834 void WriteOpers(char* text, ...)
838 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
842 char textbuffer[MAXBUF];
844 va_start (argsPtr, text);
845 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
848 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
853 if (strchr(a->modes,'s'))
855 // send server notices to all with +s
856 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
862 void ServerNoticeAll(char* text, ...)
867 char textbuffer[MAXBUF];
869 va_start (argsPtr, text);
870 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
873 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
875 userrec* t = (userrec*)(*i);
876 WriteServ(t->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
880 void ServerPrivmsgAll(char* text, ...)
885 char textbuffer[MAXBUF];
887 va_start (argsPtr, text);
888 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
891 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
893 userrec* t = (userrec*)(*i);
894 WriteServ(t->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
898 void WriteMode(const char* modes, int flags, const char* text, ...)
900 if ((!text) || (!modes) || (!flags))
902 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
906 char textbuffer[MAXBUF];
908 va_start (argsPtr, text);
909 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
911 int modelen = strlen(modes);
913 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
915 userrec* t = (userrec*)(*i);
916 bool send_to_user = false;
920 for (int n = 0; n < modelen; n++)
922 if (!hasumode(t,modes[n]))
924 send_to_user = false;
929 else if (flags == WM_OR)
931 send_to_user = false;
932 for (int n = 0; n < modelen; n++)
934 if (hasumode(t,modes[n]))
943 WriteServ(t->fd,"NOTICE %s :%s",t->nick,textbuffer);
948 void NoticeAll(userrec *source, bool local_only, char* text, ...)
950 if ((!text) || (!source))
952 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
956 char textbuffer[MAXBUF];
958 va_start (argsPtr, text);
959 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
962 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
964 userrec* t = (userrec*)(*i);
965 WriteFrom(t->fd,source,"NOTICE $* :%s",textbuffer);
971 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
973 if ((!text) || (!source))
975 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
979 char textbuffer[MAXBUF];
981 va_start (argsPtr, text);
982 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
985 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
987 userrec* t = (userrec*)(*i);
988 if ((IS_LOCAL(t)) && (strchr(t->modes,'w')))
990 WriteTo(source,t,"WALLOPS :%s",textbuffer);
995 /* convert a string to lowercase. Note following special circumstances
996 * taken from RFC 1459. Many "official" server branches still hold to this
997 * rule so i will too;
999 * Because of IRC's scandanavian origin, the characters {}| are
1000 * considered to be the lower case equivalents of the characters []\,
1001 * respectively. This is a critical issue when determining the
1002 * equivalence of two nicknames.
1005 void strlower(char *n)
1009 for (char* t = n; *t; t++)
1010 *t = lowermap[(unsigned)*t];
1014 /* Find a user record by nickname and return a pointer to it */
1016 userrec* Find(std::string nick)
1018 user_hash::iterator iter = clientlist.find(nick);
1020 if (iter == clientlist.end())
1021 /* Couldn't find it */
1024 return iter->second;
1027 userrec* Find(const char* nick)
1031 user_hash::iterator iter = clientlist.find(nick);
1033 if (iter == clientlist.end())
1036 return iter->second;
1039 /* find a channel record by channel name and return a pointer to it */
1041 chanrec* FindChan(const char* chan)
1045 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1049 chan_hash::iterator iter = chanlist.find(chan);
1051 if (iter == chanlist.end())
1052 /* Couldn't find it */
1055 return iter->second;
1059 long GetMaxBans(char* name)
1062 for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
1064 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
1067 Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
1074 void purge_empty_chans(userrec* u)
1079 // firstly decrement the count on each channel
1080 for (unsigned int f = 0; f < u->chans.size(); f++)
1082 if (u->chans[f].channel)
1084 u->chans[f].channel->DelUser((char*)u);
1088 for (unsigned int i = 0; i < u->chans.size(); i++)
1090 if (u->chans[i].channel)
1092 if (!usercount(u->chans[i].channel))
1094 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1095 /* kill the record */
1096 if (i2 != chanlist.end())
1098 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1101 FOREACH_MOD(I_OnChannelDelete,OnChannelDelete(i2->second));
1106 u->chans[i].channel = NULL;
1111 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1115 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1121 char* chanmodes(chanrec *chan, bool showkey)
1123 static char scratch[MAXBUF];
1124 static char sparam[MAXBUF];
1125 char* offset = scratch;
1129 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1136 if (chan->binarymodes & CM_NOEXTERNAL)
1138 if (chan->binarymodes & CM_TOPICLOCK)
1144 if (chan->binarymodes & CM_INVITEONLY)
1146 if (chan->binarymodes & CM_MODERATED)
1148 if (chan->binarymodes & CM_SECRET)
1150 if (chan->binarymodes & CM_PRIVATE)
1154 snprintf(sparam,MAXBUF," %s",showkey ? chan->key : "<key>");
1159 sprintf(foo," %lu",(unsigned long)chan->limit);
1160 strlcat(sparam,foo,MAXBUF);
1162 if (*chan->custom_modes)
1164 for (char* t = chan->custom_modes; *t; t++)
1166 for (int z = 0; chan->custom_modes[z]; z++)
1168 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1171 strlcat(sparam," ",MAXBUF);
1172 strlcat(sparam,extparam.c_str(),MAXBUF);
1176 /* Null terminate scratch */
1178 strlcat(scratch,sparam,MAXMODES);
1183 /* compile a userlist of a channel into a string, each nick seperated by
1184 * spaces and op, voice etc status shown as @ and + */
1186 void userlist(userrec *user,chanrec *c)
1188 if ((!c) || (!user))
1190 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1194 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1196 std::map<char*,char*> *ulist= c->GetUsers();
1197 for (std::map<char*,char*>::iterator i = ulist->begin(); i != ulist->end(); i++)
1199 char* o = i->second;
1200 userrec* otheruser = (userrec*)o;
1201 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1203 /* user is +i, and source not on the channel, does not show
1204 * nick in NAMES list */
1207 strlcat(list,cmode(otheruser,c),MAXBUF);
1208 strlcat(list,otheruser->nick,MAXBUF);
1209 strlcat(list," ",MAXBUF);
1210 if (strlen(list)>(480-NICKMAX))
1212 /* list overflowed into
1213 * multiple numerics */
1214 WriteServ_NoFormat(user->fd,list);
1215 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1218 /* if whats left in the list isnt empty, send it */
1219 if (list[strlen(list)-1] != ':')
1221 WriteServ_NoFormat(user->fd,list);
1225 /* return a count of the users on a specific channel accounting for
1226 * invisible users who won't increase the count. e.g. for /LIST */
1228 int usercount_i(chanrec *c)
1234 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1238 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1240 if (has_channel(i->second,c))
1242 if (i->second->registered == 7)
1244 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1246 /* user is +i, and source not on the channel, does not show
1247 * nick in NAMES list */
1254 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1259 int usercount(chanrec *c)
1261 return (c ? c->GetUserCounter() : 0);
1265 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1267 ConnectClass GetClass(userrec *user)
1269 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1271 if (match(user->host,i->host.c_str()))
1276 return *(Config->Classes.begin());
1279 /* sends out an error notice to all connected clients (not to be used
1282 void send_error(char *s)
1284 log(DEBUG,"send_error: %s",s);
1285 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1287 userrec* t = (userrec*)(*i);
1288 if (t->registered == 7)
1290 WriteServ(t->fd,"NOTICE %s :%s",t->nick,s);
1294 // fix - unregistered connections receive ERROR, not NOTICE
1295 Write(t->fd,"ERROR :%s",s);
1300 void Error(int status)
1302 signal (SIGALRM, SIG_IGN);
1303 signal (SIGPIPE, SIG_IGN);
1304 signal (SIGTERM, SIG_IGN);
1305 signal (SIGABRT, SIG_IGN);
1306 signal (SIGSEGV, SIG_IGN);
1307 signal (SIGURG, SIG_IGN);
1308 signal (SIGKILL, SIG_IGN);
1309 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1310 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1314 // this function counts all users connected, wether they are registered or NOT.
1317 return clientlist.size();
1320 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1321 int registered_usercount(void)
1324 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1326 if (i->second->registered == 7) c++;
1331 int usercount_invisible(void)
1334 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1336 if ((i->second->registered == 7) && (strchr(i->second->modes,'i'))) c++;
1341 int usercount_opers(void)
1344 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1345 if (*i->second->oper) c++;
1349 int usercount_unknown(void)
1352 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1354 userrec* t = (userrec*)(*i);
1355 if (t->registered != 7)
1361 long chancount(void)
1363 return chanlist.size();
1369 for (std::vector<userrec*>::const_iterator i = local_users.begin(); i != local_users.end(); i++)
1371 userrec* t = (userrec*)(*i);
1372 if (t->registered == 7) c++;
1377 void ShowMOTD(userrec *user)
1379 static char mbuf[MAXBUF];
1380 static char crud[MAXBUF];
1381 std::string WholeMOTD = "";
1382 if (!Config->MOTD.size())
1384 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1387 snprintf(crud,MAXBUF,":%s 372 %s :- ", Config->ServerName, user->nick);
1388 snprintf(mbuf,MAXBUF,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1389 WholeMOTD = WholeMOTD + mbuf;
1390 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1391 WholeMOTD = WholeMOTD + std::string(crud) + Config->MOTD[i].c_str() + std::string("\r\n");
1392 snprintf(mbuf,MAXBUF,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1393 WholeMOTD = WholeMOTD + mbuf;
1394 // only one write operation
1395 if (Config->GetIOHook(user->port))
1399 Config->GetIOHook(user->port)->OnRawSocketWrite(user->fd,(char*)WholeMOTD.c_str(),WholeMOTD.length());
1401 catch (ModuleException& modexcept)
1403 log(DEBUG,"Module exception cought: %s",modexcept.GetReason()); \
1409 user->AddWriteBuf(WholeMOTD);
1411 ServerInstance->stats->statsSent += WholeMOTD.length();
1414 void ShowRULES(userrec *user)
1416 if (!Config->RULES.size())
1418 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1421 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1422 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1423 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1424 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1427 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1428 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1429 // registration timeout maximum seconds)
1430 bool AllModulesReportReady(userrec* user)
1432 if (!Config->global_implementation[I_OnCheckReady])
1434 for (int i = 0; i <= MODCOUNT; i++)
1436 if (Config->implement_lists[i][I_OnCheckReady])
1438 int res = modules[i]->OnCheckReady(user);
1446 bool DirValid(char* dirandfile)
1449 char buffer[MAXBUF], otherdir[MAXBUF];
1450 strlcpy(work,dirandfile,MAXBUF);
1451 int p = strlen(work);
1452 // we just want the dir
1462 // Get the current working directory
1463 if( getcwd( buffer, MAXBUF ) == NULL )
1466 if( getcwd( otherdir, MAXBUF ) == NULL )
1469 if (strlen(otherdir) >= strlen(work))
1471 otherdir[strlen(work)] = '\0';
1472 if (!strcmp(otherdir,work))
1481 std::string GetFullProgDir(char** argv, int argc)
1484 char buffer[MAXBUF], otherdir[MAXBUF];
1485 strlcpy(work,argv[0],MAXBUF);
1486 int p = strlen(work);
1487 // we just want the dir
1497 // Get the current working directory
1498 if( getcwd( buffer, MAXBUF ) == NULL )
1501 if( getcwd( otherdir, MAXBUF ) == NULL )
1507 int InsertMode(std::string &output, const char* mode, unsigned short section)
1509 unsigned short currsection = 1;
1510 unsigned int pos = output.find("CHANMODES=", 0) + 10; // +10 for the length of "CHANMODES="
1512 if(section > 4 || section == 0)
1514 log(DEBUG, "InsertMode: CHANMODES doesn't have a section %dh :/", section);
1518 for(; pos < output.size(); pos++)
1520 if(section == currsection)
1523 if(output[pos] == ',')
1527 output.insert(pos, mode);
1531 bool IsValidChannelName(const char *chname)
1535 /* check for no name - don't check for !*chname, as if it is empty, it won't be '#'! */
1536 if (!chname || *chname != '#')
1541 c = (char *)chname + 1;
1555 /* too long a name - note funky pointer arithmetic here. */
1556 if ((c - chname) > CHANMAX)