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"
53 extern std::vector<Module*> modules;
57 extern char lowermap[255];
58 extern char ServerName[MAXBUF];
59 extern char Network[MAXBUF];
60 extern char ServerDesc[MAXBUF];
61 extern char list[MAXBUF];
66 extern std::stringstream config_f;
70 extern FILE *log_file;
71 extern userrec* fd_ref_table[65536];
73 extern int statsAccept, statsRefused, statsUnknown, statsCollisions, statsDns, statsDnsGood, statsDnsBad, statsConnects, statsSent, statsRecv;
75 static char already_sent[65536];
76 extern std::vector<userrec*> all_opers;
78 extern ClassVector Classes;
80 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
81 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
82 typedef std::deque<command_t> command_table;
84 extern user_hash clientlist;
85 extern chan_hash chanlist;
86 extern command_table cmdlist;
87 extern file_cache MOTD;
88 extern file_cache RULES;
90 void log(int level,char *text, ...)
92 char textbuffer[MAXBUF];
100 timeinfo = localtime (&rawtime);
105 va_start (argsPtr, text);
106 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
108 strlcpy(b,asctime(timeinfo),MAXBUF);
109 b[24] = ':'; // we know this is the end of the time string
110 fprintf(log_file,"%s %s\n",b,textbuffer);
113 // nofork enabled? display it on terminal too
114 printf("%s %s\n",b,textbuffer);
119 void readfile(file_cache &F, const char* fname)
122 char linebuf[MAXBUF];
124 log(DEBUG,"readfile: loading %s",fname);
126 file = fopen(fname,"r");
131 fgets(linebuf,sizeof(linebuf),file);
132 linebuf[strlen(linebuf)-1]='\0';
139 F.push_back(linebuf);
146 log(DEBUG,"readfile: failed to load file: %s",fname);
148 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
151 void Write(int sock,char *text, ...)
157 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
160 char textbuffer[MAXBUF];
164 va_start (argsPtr, text);
165 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
167 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
169 if (fd_ref_table[sock])
172 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
173 fd_ref_table[sock]->AddWriteBuf(tb);
176 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
179 /* write a server formatted numeric response to a single socket */
181 void WriteServ(int sock, char* text, ...)
187 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
190 char textbuffer[MAXBUF],tb[MAXBUF];
192 va_start (argsPtr, text);
194 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
196 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",ServerName,textbuffer);
198 if (fd_ref_table[sock])
201 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
202 fd_ref_table[sock]->AddWriteBuf(tb);
205 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
208 /* write text from an originating user to originating user */
210 void WriteFrom(int sock, userrec *user,char* text, ...)
214 if ((!text) || (!user))
216 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
219 char textbuffer[MAXBUF],tb[MAXBUF];
221 va_start (argsPtr, text);
223 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
225 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
227 if (fd_ref_table[sock])
230 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
231 fd_ref_table[sock]->AddWriteBuf(tb);
234 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
237 /* write text to an destination user from a source user (e.g. user privmsg) */
239 void WriteTo(userrec *source, userrec *dest,char *data, ...)
241 if ((!dest) || (!data))
243 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
246 if (dest->fd == FD_MAGIC_NUMBER)
248 char textbuffer[MAXBUF],tb[MAXBUF];
250 va_start (argsPtr, data);
251 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
255 // if no source given send it from the server.
258 WriteServ(dest->fd,":%s %s",ServerName,textbuffer);
262 WriteFrom(dest->fd,source,"%s",textbuffer);
266 /* write formatted text from a source user to all users on a channel
267 * including the sender (NOT for privmsg, notice etc!) */
269 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
271 if ((!Ptr) || (!user) || (!text))
273 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
276 char textbuffer[MAXBUF];
278 va_start (argsPtr, text);
279 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
282 std::vector<char*> *ulist = Ptr->GetUsers();
283 for (unsigned int j = 0; j < ulist->size(); j++)
285 char* o = (*ulist)[j];
286 userrec* otheruser = (userrec*)o;
287 if (otheruser->fd != FD_MAGIC_NUMBER)
288 WriteTo(user,otheruser,"%s",textbuffer);
292 /* write formatted text from a source user to all users on a channel
293 * including the sender (NOT for privmsg, notice etc!) doesnt send to
294 * users on remote servers */
296 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
298 if ((!Ptr) || (!text))
300 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
303 char textbuffer[MAXBUF];
305 va_start (argsPtr, text);
306 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
309 std::vector<char*> *ulist = Ptr->GetUsers();
310 for (unsigned int j = 0; j < ulist->size(); j++)
312 char* o = (*ulist)[j];
313 userrec* otheruser = (userrec*)o;
314 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
318 WriteServ(otheruser->fd,"%s",textbuffer);
322 WriteTo(user,otheruser,"%s",textbuffer);
328 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
330 if ((!Ptr) || (!text))
332 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
335 char textbuffer[MAXBUF];
337 va_start (argsPtr, text);
338 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
342 std::vector<char*> *ulist = Ptr->GetUsers();
343 for (unsigned int j = 0; j < ulist->size(); j++)
345 char* o = (*ulist)[j];
346 userrec* otheruser = (userrec*)o;
347 if (otheruser->fd != FD_MAGIC_NUMBER)
348 WriteServ(otheruser->fd,"%s",textbuffer);
352 /* write formatted text from a source user to all users on a channel except
353 * for the sender (for privmsg etc) */
355 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
357 if ((!Ptr) || (!user) || (!text))
359 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
362 char textbuffer[MAXBUF];
364 va_start (argsPtr, text);
365 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
368 std::vector<char*> *ulist = Ptr->GetUsers();
369 for (unsigned int j = 0; j < ulist->size(); j++)
371 char* o = (*ulist)[j];
372 userrec* otheruser = (userrec*)o;
373 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
374 WriteFrom(otheruser->fd,user,"%s",textbuffer);
378 std::string GetServerDescription(char* servername)
380 std::string description = "";
381 FOREACH_MOD OnGetServerDescription(servername,description);
382 if (description != "")
388 return ServerDesc; // not a remote server that can be found, it must be me.
392 /* write a formatted string to all users who share at least one common
393 * channel, including the source user e.g. for use in NICK */
395 void WriteCommon(userrec *u, char* text, ...)
399 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
403 if (u->registered != 7) {
404 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
408 char textbuffer[MAXBUF];
410 va_start (argsPtr, text);
411 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
414 // FIX: Stops a message going to the same person more than once
415 memset(&already_sent,0,65536);
417 bool sent_to_at_least_one = false;
419 for (int i = 0; i < MAXCHANS; i++)
421 if (u->chans[i].channel)
423 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
424 for (unsigned int j = 0; j < ulist->size(); j++)
426 char* o = (*ulist)[j];
427 userrec* otheruser = (userrec*)o;
428 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
430 already_sent[otheruser->fd] = 1;
431 WriteFrom(otheruser->fd,u,"%s",textbuffer);
432 sent_to_at_least_one = true;
437 // if the user was not in any channels, no users will receive the text. Make sure the user
438 // receives their OWN message for WriteCommon
439 if (!sent_to_at_least_one)
441 WriteFrom(u->fd,u,"%s",textbuffer);
445 /* write a formatted string to all users who share at least one common
446 * channel, NOT including the source user e.g. for use in QUIT */
448 void WriteCommonExcept(userrec *u, char* text, ...)
452 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
456 if (u->registered != 7) {
457 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
461 char textbuffer[MAXBUF];
463 va_start (argsPtr, text);
464 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
467 memset(&already_sent,0,65536);
469 for (int i = 0; i < MAXCHANS; i++)
471 if (u->chans[i].channel)
473 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
474 for (unsigned int j = 0; j < ulist->size(); j++)
476 char* o = (*ulist)[j];
477 userrec* otheruser = (userrec*)o;
480 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
482 already_sent[otheruser->fd] = 1;
483 WriteFrom(otheruser->fd,u,"%s",textbuffer);
491 void WriteOpers(char* text, ...)
495 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
499 char textbuffer[MAXBUF];
501 va_start (argsPtr, text);
502 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
505 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
508 if ((a) && (a->fd != FD_MAGIC_NUMBER))
510 if (strchr(a->modes,'s'))
512 // send server notices to all with +s
513 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
519 void ServerNoticeAll(char* text, ...)
524 char textbuffer[MAXBUF];
526 va_start (argsPtr, text);
527 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
530 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
532 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
534 WriteServ(i->second->fd,"NOTICE $%s :%s",ServerName,textbuffer);
539 void ServerPrivmsgAll(char* text, ...)
544 char textbuffer[MAXBUF];
546 va_start (argsPtr, text);
547 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
550 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
552 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
554 WriteServ(i->second->fd,"PRIVMSG $%s :%s",ServerName,textbuffer);
559 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
561 if ((!text) || (!source))
563 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
567 char textbuffer[MAXBUF];
569 va_start (argsPtr, text);
570 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
573 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
576 if ((a) && (a->fd != FD_MAGIC_NUMBER))
578 if (strchr(a->modes,'s'))
580 // send server notices to all with +s
581 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
587 // returns TRUE of any users on channel C occupy server 'servername'.
589 bool ChanAnyOnThisServer(chanrec *c,char* servername)
591 log(DEBUG,"ChanAnyOnThisServer");
593 std::vector<char*> *ulist = c->GetUsers();
594 for (unsigned int j = 0; j < ulist->size(); j++)
596 char* o = (*ulist)[j];
597 userrec* user = (userrec*)o;
598 if (!strcasecmp(user->server,servername))
604 // returns true if user 'u' shares any common channels with any users on server 'servername'
606 bool CommonOnThisServer(userrec* u,const char* servername)
608 log(DEBUG,"ChanAnyOnThisServer");
610 for (int i = 0; i < MAXCHANS; i++)
612 if (u->chans[i].channel)
614 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
615 for (unsigned int j = 0; j < ulist->size(); j++)
617 char* o = (*ulist)[j];
618 userrec* user = (userrec*)o;
619 if (!strcasecmp(user->server,servername))
628 void WriteMode(const char* modes, int flags, const char* text, ...)
630 if ((!text) || (!modes) || (!flags))
632 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
636 char textbuffer[MAXBUF];
638 va_start (argsPtr, text);
639 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
641 int modelen = strlen(modes);
643 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
645 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
647 bool send_to_user = false;
652 for (int n = 0; n < modelen; n++)
654 if (!hasumode(i->second,modes[n]))
656 send_to_user = false;
661 else if (flags == WM_OR)
663 send_to_user = false;
664 for (int n = 0; n < modelen; n++)
666 if (hasumode(i->second,modes[n]))
676 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
682 void NoticeAll(userrec *source, bool local_only, char* text, ...)
684 if ((!text) || (!source))
686 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
690 char textbuffer[MAXBUF];
692 va_start (argsPtr, text);
693 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
696 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
698 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
700 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
707 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
709 if ((!text) || (!source))
711 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
715 char textbuffer[MAXBUF];
717 va_start (argsPtr, text);
718 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
721 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
723 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
725 if (strchr(i->second->modes,'w'))
727 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
733 /* convert a string to lowercase. Note following special circumstances
734 * taken from RFC 1459. Many "official" server branches still hold to this
735 * rule so i will too;
737 * Because of IRC's scandanavian origin, the characters {}| are
738 * considered to be the lower case equivalents of the characters []\,
739 * respectively. This is a critical issue when determining the
740 * equivalence of two nicknames.
743 void strlower(char *n)
747 for (char* t = n; *t; t++)
748 *t = lowermap[(unsigned)*t];
752 /* Find a user record by nickname and return a pointer to it */
754 userrec* Find(std::string nick)
756 user_hash::iterator iter = clientlist.find(nick);
758 if (iter == clientlist.end())
759 /* Couldn't find it */
765 /* find a channel record by channel name and return a pointer to it */
767 chanrec* FindChan(const char* chan)
771 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
775 chan_hash::iterator iter = chanlist.find(chan);
777 if (iter == chanlist.end())
778 /* Couldn't find it */
785 long GetMaxBans(char* name)
788 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
790 ConfValue("banlist","chan",count,CM,&config_f);
793 ConfValue("banlist","limit",count,CM,&config_f);
800 void purge_empty_chans(userrec* u)
803 int go_again = 1, purge = 0;
805 // firstly decrement the count on each channel
806 for (int f = 0; f < MAXCHANS; f++)
808 if (u->chans[f].channel)
810 u->chans[f].channel->DelUser((char*)u);
814 for (int i = 0; i < MAXCHANS; i++)
816 if (u->chans[i].channel)
818 if (!usercount(u->chans[i].channel))
820 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
821 /* kill the record */
822 if (i2 != chanlist.end())
824 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
830 u->chans[i].channel = NULL;
835 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
839 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
845 char scratch[MAXBUF];
848 char* chanmodes(chanrec *chan)
852 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
859 if (chan->binarymodes & CM_NOEXTERNAL)
861 strlcat(scratch,"n",MAXMODES);
863 if (chan->binarymodes & CM_TOPICLOCK)
865 strlcat(scratch,"t",MAXMODES);
869 strlcat(scratch,"k",MAXMODES);
873 strlcat(scratch,"l",MAXMODES);
875 if (chan->binarymodes & CM_INVITEONLY)
877 strlcat(scratch,"i",MAXMODES);
879 if (chan->binarymodes & CM_MODERATED)
881 strlcat(scratch,"m",MAXMODES);
883 if (chan->binarymodes & CM_SECRET)
885 strlcat(scratch,"s",MAXMODES);
887 if (chan->binarymodes & CM_PRIVATE)
889 strlcat(scratch,"p",MAXMODES);
893 strlcat(sparam," ",MAXBUF);
894 strlcat(sparam,chan->key,MAXBUF);
899 sprintf(foo," %lu",(unsigned long)chan->limit);
900 strlcat(sparam,foo,MAXBUF);
902 if (*chan->custom_modes)
904 strlcat(scratch,chan->custom_modes,MAXMODES);
905 for (int z = 0; chan->custom_modes[z] != 0; z++)
907 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
910 strlcat(sparam," ",MAXBUF);
911 strlcat(sparam,extparam.c_str(),MAXBUF);
915 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
916 strlcat(scratch,sparam,MAXMODES);
921 /* compile a userlist of a channel into a string, each nick seperated by
922 * spaces and op, voice etc status shown as @ and + */
924 void userlist(userrec *user,chanrec *c)
928 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
932 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
934 std::vector<char*> *ulist = c->GetUsers();
935 for (unsigned int i = 0; i < ulist->size(); i++)
937 char* o = (*ulist)[i];
938 userrec* otheruser = (userrec*)o;
939 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
941 /* user is +i, and source not on the channel, does not show
942 * nick in NAMES list */
945 strlcat(list,cmode(otheruser,c),MAXBUF);
946 strlcat(list,otheruser->nick,MAXBUF);
947 strlcat(list," ",MAXBUF);
948 if (strlen(list)>(480-NICKMAX))
950 /* list overflowed into
951 * multiple numerics */
952 WriteServ(user->fd,"%s",list);
953 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
956 /* if whats left in the list isnt empty, send it */
957 if (list[strlen(list)-1] != ':')
959 WriteServ(user->fd,"%s",list);
963 /* return a count of the users on a specific channel accounting for
964 * invisible users who won't increase the count. e.g. for /LIST */
966 int usercount_i(chanrec *c)
972 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
977 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
981 if (has_channel(i->second,c))
983 if (isnick(i->second->nick))
985 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
987 /* user is +i, and source not on the channel, does not show
988 * nick in NAMES list */
996 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1001 int usercount(chanrec *c)
1005 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
1008 int count = c->GetUserCounter();
1009 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
1014 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1016 char* Passwd(userrec *user)
1018 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1020 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1028 bool IsDenied(userrec *user)
1030 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1032 if (match(user->host,i->host) && (i->type == CC_DENY))
1043 /* sends out an error notice to all connected clients (not to be used
1046 void send_error(char *s)
1048 log(DEBUG,"send_error: %s",s);
1049 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1051 if (isnick(i->second->nick))
1053 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1057 // fix - unregistered connections receive ERROR, not NOTICE
1058 Write(i->second->fd,"ERROR :%s",s);
1063 void Error(int status)
1065 signal (SIGALRM, SIG_IGN);
1066 signal (SIGPIPE, SIG_IGN);
1067 signal (SIGTERM, SIG_IGN);
1068 signal (SIGABRT, SIG_IGN);
1069 signal (SIGSEGV, SIG_IGN);
1070 signal (SIGURG, SIG_IGN);
1071 signal (SIGKILL, SIG_IGN);
1072 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1073 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1077 // this function counts all users connected, wether they are registered or NOT.
1080 return clientlist.size();
1083 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1084 int registered_usercount(void)
1087 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1089 if (i->second->registered == 7) c++;
1094 int usercount_invisible(void)
1097 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1099 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1104 int usercount_opers(void)
1107 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1109 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1114 int usercount_unknown(void)
1118 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1120 if ((i->second->fd) && (i->second->registered != 7))
1126 long chancount(void)
1128 return chanlist.size();
1131 long count_servs(void)
1136 long servercount(void)
1138 return count_servs()+1;
1144 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1146 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,ServerName))) c++;
1151 void ShowMOTD(userrec *user)
1154 std::string WholeMOTD = "";
1157 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1160 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", ServerName, user->nick, ServerName);
1161 WholeMOTD = WholeMOTD + buf;
1162 for (unsigned int i = 0; i != MOTD.size(); i++)
1164 snprintf(buf,65535,":%s 372 %s :- %s\r\n", ServerName, user->nick, MOTD[i].c_str());
1165 WholeMOTD = WholeMOTD + buf;
1167 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", ServerName, user->nick);
1168 WholeMOTD = WholeMOTD + buf;
1169 // only one write operation
1170 user->AddWriteBuf(WholeMOTD);
1171 statsSent += WholeMOTD.length();
1174 void ShowRULES(userrec *user)
1178 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1181 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
1182 for (unsigned int i = 0; i != RULES.size(); i++)
1184 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1186 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
1189 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1190 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1191 // registration timeout maximum seconds)
1192 bool AllModulesReportReady(userrec* user)
1194 for (int i = 0; i <= MODCOUNT; i++)
1196 int res = modules[i]->OnCheckReady(user);
1203 char islast(const char* s)
1208 long map_count(const char* s)
1211 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1213 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,s))) c++;
1218 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1221 /* create the command and push it onto the table */
1222 strlcpy(comm.command,cmd,MAXBUF);
1223 strlcpy(comm.source,source,MAXBUF);
1224 comm.handler_function = f;
1225 comm.flags_needed = flags;
1226 comm.min_params = minparams;
1228 comm.total_bytes = 0;
1229 cmdlist.push_back(comm);
1230 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1234 void SetupCommandTable(void)
1236 createcommand("USER",handle_user,0,4,"<core>");
1237 createcommand("NICK",handle_nick,0,1,"<core>");
1238 createcommand("QUIT",handle_quit,0,0,"<core>");
1239 createcommand("VERSION",handle_version,0,0,"<core>");
1240 createcommand("PING",handle_ping,0,1,"<core>");
1241 createcommand("PONG",handle_pong,0,1,"<core>");
1242 createcommand("ADMIN",handle_admin,0,0,"<core>");
1243 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1244 createcommand("INFO",handle_info,0,0,"<core>");
1245 createcommand("TIME",handle_time,0,0,"<core>");
1246 createcommand("WHOIS",handle_whois,0,1,"<core>");
1247 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1248 createcommand("NOTICE",handle_notice,0,2,"<core>");
1249 createcommand("JOIN",handle_join,0,1,"<core>");
1250 createcommand("NAMES",handle_names,0,0,"<core>");
1251 createcommand("PART",handle_part,0,1,"<core>");
1252 createcommand("KICK",handle_kick,0,2,"<core>");
1253 createcommand("MODE",handle_mode,0,1,"<core>");
1254 createcommand("TOPIC",handle_topic,0,1,"<core>");
1255 createcommand("WHO",handle_who,0,1,"<core>");
1256 createcommand("MOTD",handle_motd,0,0,"<core>");
1257 createcommand("RULES",handle_rules,0,0,"<core>");
1258 createcommand("OPER",handle_oper,0,2,"<core>");
1259 createcommand("LIST",handle_list,0,0,"<core>");
1260 createcommand("DIE",handle_die,'o',1,"<core>");
1261 createcommand("RESTART",handle_restart,'o',1,"<core>");
1262 createcommand("KILL",handle_kill,'o',2,"<core>");
1263 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1264 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1265 createcommand("STATS",handle_stats,0,1,"<core>");
1266 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1267 createcommand("AWAY",handle_away,0,0,"<core>");
1268 createcommand("ISON",handle_ison,0,0,"<core>");
1269 createcommand("SUMMON",handle_summon,0,0,"<core>");
1270 createcommand("USERS",handle_users,0,0,"<core>");
1271 createcommand("INVITE",handle_invite,0,0,"<core>");
1272 createcommand("PASS",handle_pass,0,1,"<core>");
1273 createcommand("TRACE",handle_trace,'o',0,"<core>");
1274 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1275 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1276 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1277 createcommand("MODULES",handle_modules,0,0,"<core>");
1278 createcommand("LINKS",handle_links,0,0,"<core>");
1279 createcommand("MAP",handle_map,0,0,"<core>");
1280 createcommand("KLINE",handle_kline,'o',1,"<core>");
1281 createcommand("GLINE",handle_gline,'o',1,"<core>");
1282 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1283 createcommand("QLINE",handle_qline,'o',1,"<core>");
1284 createcommand("ELINE",handle_eline,'o',1,"<core>");
1285 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1286 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1287 createcommand("SERVER",handle_server,0,0,"<core>");
1288 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1291 bool DirValid(char* dirandfile)
1294 strlcpy(work,dirandfile,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 )
1314 if (strlen(otherdir) >= strlen(work))
1316 otherdir[strlen(work)] = '\0';
1317 if (!strcmp(otherdir,work))
1326 std::string GetFullProgDir(char** argv, int argc)
1329 strlcpy(work,argv[0],MAXBUF);
1330 int p = strlen(work);
1331 // we just want the dir
1332 while (strlen(work))
1341 char buffer[MAXBUF], otherdir[MAXBUF];
1342 // Get the current working directory
1343 if( getcwd( buffer, MAXBUF ) == NULL )
1346 if( getcwd( otherdir, MAXBUF ) == NULL )