1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2004 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"
22 #include "inspircd_util.h"
25 #include <sys/errno.h>
29 #include <ext/hash_map>
37 #include "connection.h"
48 #include "inspstring.h"
49 #include "helperfuncs.h"
54 extern std::vector<Module*> modules;
55 extern ServerConfig *Config;
58 extern char lowermap[255];
59 extern std::stringstream config_f;
60 static char list[MAXBUF];
61 extern userrec* fd_ref_table[65536];
63 extern serverstats* stats;
65 static char already_sent[65536];
66 extern std::vector<userrec*> all_opers;
68 extern ClassVector Classes;
70 /*typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
71 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
72 typedef std::deque<command_t> command_table;*/
74 extern user_hash clientlist;
75 extern chan_hash chanlist;
76 extern command_table cmdlist;
77 extern file_cache MOTD;
78 extern file_cache RULES;
80 void log(int level,char *text, ...)
82 char textbuffer[MAXBUF];
86 if (level < Config->LogLevel)
90 timeinfo = localtime(&rawtime);
95 va_start (argsPtr, text);
96 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
98 strlcpy(b,asctime(timeinfo),MAXBUF);
99 b[24] = ':'; // we know this is the end of the time string
100 if (Config->log_file)
101 fprintf(Config->log_file,"%s %s\n",b,textbuffer);
104 // nofork enabled? display it on terminal too
105 printf("%s %s\n",b,textbuffer);
110 void readfile(file_cache &F, const char* fname)
113 char linebuf[MAXBUF];
115 log(DEBUG,"readfile: loading %s",fname);
117 file = fopen(fname,"r");
122 fgets(linebuf,sizeof(linebuf),file);
123 linebuf[strlen(linebuf)-1]='\0';
130 F.push_back(linebuf);
137 log(DEBUG,"readfile: failed to load file: %s",fname);
139 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
142 void Write(int sock,char *text, ...)
148 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
151 char textbuffer[MAXBUF];
155 va_start (argsPtr, text);
156 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
158 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
160 if (fd_ref_table[sock])
163 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
164 fd_ref_table[sock]->AddWriteBuf(tb);
165 stats->statsSent += bytes;
167 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
170 /* write a server formatted numeric response to a single socket */
172 void WriteServ(int sock, char* text, ...)
178 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
181 char textbuffer[MAXBUF],tb[MAXBUF];
183 va_start (argsPtr, text);
185 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
187 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
189 if (fd_ref_table[sock])
192 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
193 fd_ref_table[sock]->AddWriteBuf(tb);
194 stats->statsSent += bytes;
196 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
199 /* write text from an originating user to originating user */
201 void WriteFrom(int sock, userrec *user,char* text, ...)
205 if ((!text) || (!user))
207 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
210 char textbuffer[MAXBUF],tb[MAXBUF];
212 va_start (argsPtr, text);
214 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
216 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
218 if (fd_ref_table[sock])
221 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
222 fd_ref_table[sock]->AddWriteBuf(tb);
223 stats->statsSent += bytes;
225 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
228 /* write text to an destination user from a source user (e.g. user privmsg) */
230 void WriteTo(userrec *source, userrec *dest,char *data, ...)
232 if ((!dest) || (!data))
234 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
237 if (dest->fd == FD_MAGIC_NUMBER)
239 char textbuffer[MAXBUF],tb[MAXBUF];
241 va_start (argsPtr, data);
242 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
246 // if no source given send it from the server.
249 WriteServ(dest->fd,":%s %s",Config->ServerName,textbuffer);
253 WriteFrom(dest->fd,source,"%s",textbuffer);
257 /* write formatted text from a source user to all users on a channel
258 * including the sender (NOT for privmsg, notice etc!) */
260 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
262 if ((!Ptr) || (!user) || (!text))
264 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
267 char textbuffer[MAXBUF];
269 va_start (argsPtr, text);
270 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
273 std::vector<char*> *ulist = Ptr->GetUsers();
274 for (unsigned int j = 0; j < ulist->size(); j++)
276 char* o = (*ulist)[j];
277 userrec* otheruser = (userrec*)o;
278 if (otheruser->fd != FD_MAGIC_NUMBER)
279 WriteTo(user,otheruser,"%s",textbuffer);
283 /* write formatted text from a source user to all users on a channel
284 * including the sender (NOT for privmsg, notice etc!) doesnt send to
285 * users on remote servers */
287 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
289 if ((!Ptr) || (!text))
291 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
294 char textbuffer[MAXBUF];
296 va_start (argsPtr, text);
297 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
300 std::vector<char*> *ulist = Ptr->GetUsers();
301 for (unsigned int j = 0; j < ulist->size(); j++)
303 char* o = (*ulist)[j];
304 userrec* otheruser = (userrec*)o;
305 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
309 WriteServ(otheruser->fd,"%s",textbuffer);
313 WriteTo(user,otheruser,"%s",textbuffer);
319 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
321 if ((!Ptr) || (!text))
323 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
326 char textbuffer[MAXBUF];
328 va_start (argsPtr, text);
329 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
333 std::vector<char*> *ulist = Ptr->GetUsers();
334 for (unsigned int j = 0; j < ulist->size(); j++)
336 char* o = (*ulist)[j];
337 userrec* otheruser = (userrec*)o;
338 if (otheruser->fd != FD_MAGIC_NUMBER)
339 WriteServ(otheruser->fd,"%s",textbuffer);
343 /* write formatted text from a source user to all users on a channel except
344 * for the sender (for privmsg etc) */
346 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
348 if ((!Ptr) || (!user) || (!text))
350 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
353 char textbuffer[MAXBUF];
355 va_start (argsPtr, text);
356 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
359 std::vector<char*> *ulist = Ptr->GetUsers();
360 for (unsigned int j = 0; j < ulist->size(); j++)
362 char* o = (*ulist)[j];
363 userrec* otheruser = (userrec*)o;
364 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
365 WriteFrom(otheruser->fd,user,"%s",textbuffer);
369 std::string GetServerDescription(char* servername)
371 std::string description = "";
372 FOREACH_MOD OnGetServerDescription(servername,description);
373 if (description != "")
379 return Config->ServerDesc; // not a remote server that can be found, it must be me.
383 /* write a formatted string to all users who share at least one common
384 * channel, including the source user e.g. for use in NICK */
386 void WriteCommon(userrec *u, char* text, ...)
390 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
394 if (u->registered != 7) {
395 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
399 char textbuffer[MAXBUF];
401 va_start (argsPtr, text);
402 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
405 // FIX: Stops a message going to the same person more than once
406 memset(&already_sent,0,65536);
408 bool sent_to_at_least_one = false;
410 for (unsigned int i = 0; i < u->chans.size(); i++)
412 if (u->chans[i].channel)
414 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
415 for (unsigned int j = 0; j < ulist->size(); j++)
417 char* o = (*ulist)[j];
418 userrec* otheruser = (userrec*)o;
419 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
421 already_sent[otheruser->fd] = 1;
422 WriteFrom(otheruser->fd,u,"%s",textbuffer);
423 sent_to_at_least_one = true;
428 // if the user was not in any channels, no users will receive the text. Make sure the user
429 // receives their OWN message for WriteCommon
430 if (!sent_to_at_least_one)
432 WriteFrom(u->fd,u,"%s",textbuffer);
436 /* write a formatted string to all users who share at least one common
437 * channel, NOT including the source user e.g. for use in QUIT */
439 void WriteCommonExcept(userrec *u, char* text, ...)
443 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
447 if (u->registered != 7) {
448 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
452 char textbuffer[MAXBUF];
454 va_start (argsPtr, text);
455 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
458 memset(&already_sent,0,65536);
460 for (unsigned int i = 0; i < u->chans.size(); i++)
462 if (u->chans[i].channel)
464 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
465 for (unsigned int j = 0; j < ulist->size(); j++)
467 char* o = (*ulist)[j];
468 userrec* otheruser = (userrec*)o;
471 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
473 already_sent[otheruser->fd] = 1;
474 WriteFrom(otheruser->fd,u,"%s",textbuffer);
482 void WriteOpers(char* text, ...)
486 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
490 char textbuffer[MAXBUF];
492 va_start (argsPtr, text);
493 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
496 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
499 if ((a) && (a->fd != FD_MAGIC_NUMBER))
501 if (strchr(a->modes,'s'))
503 // send server notices to all with +s
504 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
510 void ServerNoticeAll(char* text, ...)
515 char textbuffer[MAXBUF];
517 va_start (argsPtr, text);
518 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
521 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
523 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
525 WriteServ(i->second->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
530 void ServerPrivmsgAll(char* text, ...)
535 char textbuffer[MAXBUF];
537 va_start (argsPtr, text);
538 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
541 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
543 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
545 WriteServ(i->second->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
550 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
552 if ((!text) || (!source))
554 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
558 char textbuffer[MAXBUF];
560 va_start (argsPtr, text);
561 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
564 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
567 if ((a) && (a->fd != FD_MAGIC_NUMBER))
569 if (strchr(a->modes,'s'))
571 // send server notices to all with +s
572 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
578 // returns TRUE of any users on channel C occupy server 'servername'.
580 bool ChanAnyOnThisServer(chanrec *c,char* servername)
582 log(DEBUG,"ChanAnyOnThisServer");
584 std::vector<char*> *ulist = c->GetUsers();
585 for (unsigned int j = 0; j < ulist->size(); j++)
587 char* o = (*ulist)[j];
588 userrec* user = (userrec*)o;
589 if (!strcasecmp(user->server,servername))
595 // returns true if user 'u' shares any common channels with any users on server 'servername'
597 bool CommonOnThisServer(userrec* u,const char* servername)
599 log(DEBUG,"ChanAnyOnThisServer");
601 for (unsigned int i = 0; i < u->chans.size(); i++)
603 if (u->chans[i].channel)
605 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
606 for (unsigned int j = 0; j < ulist->size(); j++)
608 char* o = (*ulist)[j];
609 userrec* user = (userrec*)o;
610 if (!strcasecmp(user->server,servername))
619 void WriteMode(const char* modes, int flags, const char* text, ...)
621 if ((!text) || (!modes) || (!flags))
623 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
627 char textbuffer[MAXBUF];
629 va_start (argsPtr, text);
630 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
632 int modelen = strlen(modes);
634 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
636 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
638 bool send_to_user = false;
643 for (int n = 0; n < modelen; n++)
645 if (!hasumode(i->second,modes[n]))
647 send_to_user = false;
652 else if (flags == WM_OR)
654 send_to_user = false;
655 for (int n = 0; n < modelen; n++)
657 if (hasumode(i->second,modes[n]))
667 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
673 void NoticeAll(userrec *source, bool local_only, char* text, ...)
675 if ((!text) || (!source))
677 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
681 char textbuffer[MAXBUF];
683 va_start (argsPtr, text);
684 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
687 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
689 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
691 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
698 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
700 if ((!text) || (!source))
702 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
706 char textbuffer[MAXBUF];
708 va_start (argsPtr, text);
709 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
712 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
714 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
716 if (strchr(i->second->modes,'w'))
718 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
724 /* convert a string to lowercase. Note following special circumstances
725 * taken from RFC 1459. Many "official" server branches still hold to this
726 * rule so i will too;
728 * Because of IRC's scandanavian origin, the characters {}| are
729 * considered to be the lower case equivalents of the characters []\,
730 * respectively. This is a critical issue when determining the
731 * equivalence of two nicknames.
734 void strlower(char *n)
738 for (char* t = n; *t; t++)
739 *t = lowermap[(unsigned)*t];
743 /* Find a user record by nickname and return a pointer to it */
745 userrec* Find(std::string nick)
747 user_hash::iterator iter = clientlist.find(nick);
749 if (iter == clientlist.end())
750 /* Couldn't find it */
756 /* find a channel record by channel name and return a pointer to it */
758 chanrec* FindChan(const char* chan)
762 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
766 chan_hash::iterator iter = chanlist.find(chan);
768 if (iter == chanlist.end())
769 /* Couldn't find it */
776 long GetMaxBans(char* name)
779 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
781 ConfValue("banlist","chan",count,CM,&config_f);
784 ConfValue("banlist","limit",count,CM,&config_f);
791 void purge_empty_chans(userrec* u)
794 int go_again = 1, purge = 0;
796 // firstly decrement the count on each channel
797 for (unsigned int f = 0; f < u->chans.size(); f++)
799 if (u->chans[f].channel)
801 u->chans[f].channel->DelUser((char*)u);
805 for (unsigned int i = 0; i < u->chans.size(); i++)
807 if (u->chans[i].channel)
809 if (!usercount(u->chans[i].channel))
811 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
812 /* kill the record */
813 if (i2 != chanlist.end())
815 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
821 u->chans[i].channel = NULL;
826 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
830 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
836 char scratch[MAXBUF];
839 char* chanmodes(chanrec *chan)
843 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
850 if (chan->binarymodes & CM_NOEXTERNAL)
852 strlcat(scratch,"n",MAXMODES);
854 if (chan->binarymodes & CM_TOPICLOCK)
856 strlcat(scratch,"t",MAXMODES);
860 strlcat(scratch,"k",MAXMODES);
864 strlcat(scratch,"l",MAXMODES);
866 if (chan->binarymodes & CM_INVITEONLY)
868 strlcat(scratch,"i",MAXMODES);
870 if (chan->binarymodes & CM_MODERATED)
872 strlcat(scratch,"m",MAXMODES);
874 if (chan->binarymodes & CM_SECRET)
876 strlcat(scratch,"s",MAXMODES);
878 if (chan->binarymodes & CM_PRIVATE)
880 strlcat(scratch,"p",MAXMODES);
884 strlcat(sparam," ",MAXBUF);
885 strlcat(sparam,chan->key,MAXBUF);
890 sprintf(foo," %lu",(unsigned long)chan->limit);
891 strlcat(sparam,foo,MAXBUF);
893 if (*chan->custom_modes)
895 strlcat(scratch,chan->custom_modes,MAXMODES);
896 for (int z = 0; chan->custom_modes[z] != 0; z++)
898 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
901 strlcat(sparam," ",MAXBUF);
902 strlcat(sparam,extparam.c_str(),MAXBUF);
906 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
907 strlcat(scratch,sparam,MAXMODES);
912 /* compile a userlist of a channel into a string, each nick seperated by
913 * spaces and op, voice etc status shown as @ and + */
915 void userlist(userrec *user,chanrec *c)
919 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
923 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
925 std::vector<char*> *ulist = c->GetUsers();
926 for (unsigned int i = 0; i < ulist->size(); i++)
928 char* o = (*ulist)[i];
929 userrec* otheruser = (userrec*)o;
930 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
932 /* user is +i, and source not on the channel, does not show
933 * nick in NAMES list */
936 strlcat(list,cmode(otheruser,c),MAXBUF);
937 strlcat(list,otheruser->nick,MAXBUF);
938 strlcat(list," ",MAXBUF);
939 if (strlen(list)>(480-NICKMAX))
941 /* list overflowed into
942 * multiple numerics */
943 WriteServ(user->fd,"%s",list);
944 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
947 /* if whats left in the list isnt empty, send it */
948 if (list[strlen(list)-1] != ':')
950 WriteServ(user->fd,"%s",list);
954 /* return a count of the users on a specific channel accounting for
955 * invisible users who won't increase the count. e.g. for /LIST */
957 int usercount_i(chanrec *c)
963 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
967 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
971 if (has_channel(i->second,c))
973 if (isnick(i->second->nick))
975 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
977 /* user is +i, and source not on the channel, does not show
978 * nick in NAMES list */
986 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
991 int usercount(chanrec *c)
995 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
998 int count = c->GetUserCounter();
999 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
1004 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1006 char* Passwd(userrec *user)
1008 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1010 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1018 bool IsDenied(userrec *user)
1020 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1022 if (match(user->host,i->host) && (i->type == CC_DENY))
1033 /* sends out an error notice to all connected clients (not to be used
1036 void send_error(char *s)
1038 log(DEBUG,"send_error: %s",s);
1039 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1041 if (isnick(i->second->nick))
1043 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1047 // fix - unregistered connections receive ERROR, not NOTICE
1048 Write(i->second->fd,"ERROR :%s",s);
1053 void Error(int status)
1055 signal (SIGALRM, SIG_IGN);
1056 signal (SIGPIPE, SIG_IGN);
1057 signal (SIGTERM, SIG_IGN);
1058 signal (SIGABRT, SIG_IGN);
1059 signal (SIGSEGV, SIG_IGN);
1060 signal (SIGURG, SIG_IGN);
1061 signal (SIGKILL, SIG_IGN);
1062 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1063 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1067 // this function counts all users connected, wether they are registered or NOT.
1070 return clientlist.size();
1073 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1074 int registered_usercount(void)
1077 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1079 if (i->second->registered == 7) c++;
1084 int usercount_invisible(void)
1087 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1089 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1094 int usercount_opers(void)
1097 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1099 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1104 int usercount_unknown(void)
1108 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1110 if ((i->second->fd > -1) && (i->second->registered != 7))
1116 long chancount(void)
1118 return chanlist.size();
1124 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1126 if ((isnick(i->second->nick)) && (i->second->fd > -1)) c++;
1131 void ShowMOTD(userrec *user)
1134 std::string WholeMOTD = "";
1137 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1140 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1141 WholeMOTD = WholeMOTD + buf;
1142 for (unsigned int i = 0; i != MOTD.size(); i++)
1144 snprintf(buf,65535,":%s 372 %s :- %s\r\n", Config->ServerName, user->nick, MOTD[i].c_str());
1145 WholeMOTD = WholeMOTD + buf;
1147 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1148 WholeMOTD = WholeMOTD + buf;
1149 // only one write operation
1150 user->AddWriteBuf(WholeMOTD);
1151 stats->statsSent += WholeMOTD.length();
1154 void ShowRULES(userrec *user)
1158 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1161 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1162 for (unsigned int i = 0; i != RULES.size(); i++)
1164 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1166 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1169 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1170 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1171 // registration timeout maximum seconds)
1172 bool AllModulesReportReady(userrec* user)
1174 for (int i = 0; i <= MODCOUNT; i++)
1176 int res = modules[i]->OnCheckReady(user);
1183 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1186 /* create the command and push it onto the table */
1187 strlcpy(comm.command,cmd,MAXBUF);
1188 strlcpy(comm.source,source,MAXBUF);
1189 comm.handler_function = f;
1190 comm.flags_needed = flags;
1191 comm.min_params = minparams;
1193 comm.total_bytes = 0;
1194 cmdlist.push_back(comm);
1195 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1199 void SetupCommandTable(void)
1201 createcommand("USER",handle_user,0,4,"<core>");
1202 createcommand("NICK",handle_nick,0,1,"<core>");
1203 createcommand("QUIT",handle_quit,0,0,"<core>");
1204 createcommand("VERSION",handle_version,0,0,"<core>");
1205 createcommand("PING",handle_ping,0,1,"<core>");
1206 createcommand("PONG",handle_pong,0,1,"<core>");
1207 createcommand("ADMIN",handle_admin,0,0,"<core>");
1208 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1209 createcommand("INFO",handle_info,0,0,"<core>");
1210 createcommand("TIME",handle_time,0,0,"<core>");
1211 createcommand("WHOIS",handle_whois,0,1,"<core>");
1212 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1213 createcommand("NOTICE",handle_notice,0,2,"<core>");
1214 createcommand("JOIN",handle_join,0,1,"<core>");
1215 createcommand("NAMES",handle_names,0,0,"<core>");
1216 createcommand("PART",handle_part,0,1,"<core>");
1217 createcommand("KICK",handle_kick,0,2,"<core>");
1218 createcommand("MODE",handle_mode,0,1,"<core>");
1219 createcommand("TOPIC",handle_topic,0,1,"<core>");
1220 createcommand("WHO",handle_who,0,1,"<core>");
1221 createcommand("MOTD",handle_motd,0,0,"<core>");
1222 createcommand("RULES",handle_rules,0,0,"<core>");
1223 createcommand("OPER",handle_oper,0,2,"<core>");
1224 createcommand("LIST",handle_list,0,0,"<core>");
1225 createcommand("DIE",handle_die,'o',1,"<core>");
1226 createcommand("RESTART",handle_restart,'o',1,"<core>");
1227 createcommand("KILL",handle_kill,'o',2,"<core>");
1228 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1229 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1230 createcommand("STATS",handle_stats,0,1,"<core>");
1231 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1232 createcommand("AWAY",handle_away,0,0,"<core>");
1233 createcommand("ISON",handle_ison,0,0,"<core>");
1234 createcommand("SUMMON",handle_summon,0,0,"<core>");
1235 createcommand("USERS",handle_users,0,0,"<core>");
1236 createcommand("INVITE",handle_invite,0,0,"<core>");
1237 createcommand("PASS",handle_pass,0,1,"<core>");
1238 createcommand("TRACE",handle_trace,'o',0,"<core>");
1239 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1240 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1241 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1242 createcommand("MODULES",handle_modules,0,0,"<core>");
1243 createcommand("LINKS",handle_links,0,0,"<core>");
1244 createcommand("MAP",handle_map,0,0,"<core>");
1245 createcommand("KLINE",handle_kline,'o',1,"<core>");
1246 createcommand("GLINE",handle_gline,'o',1,"<core>");
1247 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1248 createcommand("QLINE",handle_qline,'o',1,"<core>");
1249 createcommand("ELINE",handle_eline,'o',1,"<core>");
1250 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1251 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1252 createcommand("SERVER",handle_server,0,0,"<core>");
1253 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1256 bool DirValid(char* dirandfile)
1259 strlcpy(work,dirandfile,MAXBUF);
1260 int p = strlen(work);
1261 // we just want the dir
1262 while (strlen(work))
1271 char buffer[MAXBUF], otherdir[MAXBUF];
1272 // Get the current working directory
1273 if( getcwd( buffer, MAXBUF ) == NULL )
1276 if( getcwd( otherdir, MAXBUF ) == NULL )
1279 if (strlen(otherdir) >= strlen(work))
1281 otherdir[strlen(work)] = '\0';
1282 if (!strcmp(otherdir,work))
1291 std::string GetFullProgDir(char** argv, int argc)
1294 strlcpy(work,argv[0],MAXBUF);
1295 int p = strlen(work);
1296 // we just want the dir
1297 while (strlen(work))
1306 char buffer[MAXBUF], otherdir[MAXBUF];
1307 // Get the current working directory
1308 if( getcwd( buffer, MAXBUF ) == NULL )
1311 if( getcwd( otherdir, MAXBUF ) == NULL )