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;
54 extern ServerConfig *Config;
57 extern char lowermap[255];
58 extern std::stringstream config_f;
59 static char list[MAXBUF];
60 extern userrec* fd_ref_table[65536];
62 extern serverstats* stats;
64 static char already_sent[65536];
65 extern std::vector<userrec*> all_opers;
67 extern ClassVector Classes;
69 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
70 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
71 typedef std::deque<command_t> command_table;
73 extern user_hash clientlist;
74 extern chan_hash chanlist;
75 extern command_table cmdlist;
76 extern file_cache MOTD;
77 extern file_cache RULES;
79 void log(int level,char *text, ...)
81 char textbuffer[MAXBUF];
85 if (level < Config->LogLevel)
89 timeinfo = localtime(&rawtime);
94 va_start (argsPtr, text);
95 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
97 strlcpy(b,asctime(timeinfo),MAXBUF);
98 b[24] = ':'; // we know this is the end of the time string
100 fprintf(Config->log_file,"%s %s\n",b,textbuffer);
103 // nofork enabled? display it on terminal too
104 printf("%s %s\n",b,textbuffer);
109 void readfile(file_cache &F, const char* fname)
112 char linebuf[MAXBUF];
114 log(DEBUG,"readfile: loading %s",fname);
116 file = fopen(fname,"r");
121 fgets(linebuf,sizeof(linebuf),file);
122 linebuf[strlen(linebuf)-1]='\0';
129 F.push_back(linebuf);
136 log(DEBUG,"readfile: failed to load file: %s",fname);
138 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
141 void Write(int sock,char *text, ...)
147 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
150 char textbuffer[MAXBUF];
154 va_start (argsPtr, text);
155 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
157 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
159 if (fd_ref_table[sock])
162 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
163 fd_ref_table[sock]->AddWriteBuf(tb);
164 stats->statsSent += bytes;
166 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
169 /* write a server formatted numeric response to a single socket */
171 void WriteServ(int sock, char* text, ...)
177 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
180 char textbuffer[MAXBUF],tb[MAXBUF];
182 va_start (argsPtr, text);
184 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
186 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
188 if (fd_ref_table[sock])
191 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
192 fd_ref_table[sock]->AddWriteBuf(tb);
193 stats->statsSent += bytes;
195 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
198 /* write text from an originating user to originating user */
200 void WriteFrom(int sock, userrec *user,char* text, ...)
204 if ((!text) || (!user))
206 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
209 char textbuffer[MAXBUF],tb[MAXBUF];
211 va_start (argsPtr, text);
213 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
215 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
217 if (fd_ref_table[sock])
220 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
221 fd_ref_table[sock]->AddWriteBuf(tb);
222 stats->statsSent += bytes;
224 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
227 /* write text to an destination user from a source user (e.g. user privmsg) */
229 void WriteTo(userrec *source, userrec *dest,char *data, ...)
231 if ((!dest) || (!data))
233 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
236 if (dest->fd == FD_MAGIC_NUMBER)
238 char textbuffer[MAXBUF],tb[MAXBUF];
240 va_start (argsPtr, data);
241 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
245 // if no source given send it from the server.
248 WriteServ(dest->fd,":%s %s",Config->ServerName,textbuffer);
252 WriteFrom(dest->fd,source,"%s",textbuffer);
256 /* write formatted text from a source user to all users on a channel
257 * including the sender (NOT for privmsg, notice etc!) */
259 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
261 if ((!Ptr) || (!user) || (!text))
263 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
266 char textbuffer[MAXBUF];
268 va_start (argsPtr, text);
269 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
272 std::vector<char*> *ulist = Ptr->GetUsers();
273 for (unsigned int j = 0; j < ulist->size(); j++)
275 char* o = (*ulist)[j];
276 userrec* otheruser = (userrec*)o;
277 if (otheruser->fd != FD_MAGIC_NUMBER)
278 WriteTo(user,otheruser,"%s",textbuffer);
282 /* write formatted text from a source user to all users on a channel
283 * including the sender (NOT for privmsg, notice etc!) doesnt send to
284 * users on remote servers */
286 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
288 if ((!Ptr) || (!text))
290 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
293 char textbuffer[MAXBUF];
295 va_start (argsPtr, text);
296 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
299 std::vector<char*> *ulist = Ptr->GetUsers();
300 for (unsigned int j = 0; j < ulist->size(); j++)
302 char* o = (*ulist)[j];
303 userrec* otheruser = (userrec*)o;
304 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
308 WriteServ(otheruser->fd,"%s",textbuffer);
312 WriteTo(user,otheruser,"%s",textbuffer);
318 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
320 if ((!Ptr) || (!text))
322 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
325 char textbuffer[MAXBUF];
327 va_start (argsPtr, text);
328 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
332 std::vector<char*> *ulist = Ptr->GetUsers();
333 for (unsigned int j = 0; j < ulist->size(); j++)
335 char* o = (*ulist)[j];
336 userrec* otheruser = (userrec*)o;
337 if (otheruser->fd != FD_MAGIC_NUMBER)
338 WriteServ(otheruser->fd,"%s",textbuffer);
342 /* write formatted text from a source user to all users on a channel except
343 * for the sender (for privmsg etc) */
345 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
347 if ((!Ptr) || (!user) || (!text))
349 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
352 char textbuffer[MAXBUF];
354 va_start (argsPtr, text);
355 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
358 std::vector<char*> *ulist = Ptr->GetUsers();
359 for (unsigned int j = 0; j < ulist->size(); j++)
361 char* o = (*ulist)[j];
362 userrec* otheruser = (userrec*)o;
363 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
364 WriteFrom(otheruser->fd,user,"%s",textbuffer);
368 std::string GetServerDescription(char* servername)
370 std::string description = "";
371 FOREACH_MOD OnGetServerDescription(servername,description);
372 if (description != "")
378 return Config->ServerDesc; // not a remote server that can be found, it must be me.
382 /* write a formatted string to all users who share at least one common
383 * channel, including the source user e.g. for use in NICK */
385 void WriteCommon(userrec *u, char* text, ...)
389 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
393 if (u->registered != 7) {
394 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
398 char textbuffer[MAXBUF];
400 va_start (argsPtr, text);
401 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
404 // FIX: Stops a message going to the same person more than once
405 memset(&already_sent,0,65536);
407 bool sent_to_at_least_one = false;
409 for (unsigned int i = 0; i < u->chans.size(); i++)
411 if (u->chans[i].channel)
413 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
414 for (unsigned int j = 0; j < ulist->size(); j++)
416 char* o = (*ulist)[j];
417 userrec* otheruser = (userrec*)o;
418 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
420 already_sent[otheruser->fd] = 1;
421 WriteFrom(otheruser->fd,u,"%s",textbuffer);
422 sent_to_at_least_one = true;
427 // if the user was not in any channels, no users will receive the text. Make sure the user
428 // receives their OWN message for WriteCommon
429 if (!sent_to_at_least_one)
431 WriteFrom(u->fd,u,"%s",textbuffer);
435 /* write a formatted string to all users who share at least one common
436 * channel, NOT including the source user e.g. for use in QUIT */
438 void WriteCommonExcept(userrec *u, char* text, ...)
442 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
446 if (u->registered != 7) {
447 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
451 char textbuffer[MAXBUF];
453 va_start (argsPtr, text);
454 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
457 memset(&already_sent,0,65536);
459 for (unsigned int i = 0; i < u->chans.size(); i++)
461 if (u->chans[i].channel)
463 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
464 for (unsigned int j = 0; j < ulist->size(); j++)
466 char* o = (*ulist)[j];
467 userrec* otheruser = (userrec*)o;
470 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
472 already_sent[otheruser->fd] = 1;
473 WriteFrom(otheruser->fd,u,"%s",textbuffer);
481 void WriteOpers(char* text, ...)
485 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
489 char textbuffer[MAXBUF];
491 va_start (argsPtr, text);
492 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
495 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
498 if ((a) && (a->fd != FD_MAGIC_NUMBER))
500 if (strchr(a->modes,'s'))
502 // send server notices to all with +s
503 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
509 void ServerNoticeAll(char* text, ...)
514 char textbuffer[MAXBUF];
516 va_start (argsPtr, text);
517 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
520 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
522 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
524 WriteServ(i->second->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
529 void ServerPrivmsgAll(char* text, ...)
534 char textbuffer[MAXBUF];
536 va_start (argsPtr, text);
537 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
540 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
542 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
544 WriteServ(i->second->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
549 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
551 if ((!text) || (!source))
553 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
557 char textbuffer[MAXBUF];
559 va_start (argsPtr, text);
560 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
563 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
566 if ((a) && (a->fd != FD_MAGIC_NUMBER))
568 if (strchr(a->modes,'s'))
570 // send server notices to all with +s
571 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
577 // returns TRUE of any users on channel C occupy server 'servername'.
579 bool ChanAnyOnThisServer(chanrec *c,char* servername)
581 log(DEBUG,"ChanAnyOnThisServer");
583 std::vector<char*> *ulist = c->GetUsers();
584 for (unsigned int j = 0; j < ulist->size(); j++)
586 char* o = (*ulist)[j];
587 userrec* user = (userrec*)o;
588 if (!strcasecmp(user->server,servername))
594 // returns true if user 'u' shares any common channels with any users on server 'servername'
596 bool CommonOnThisServer(userrec* u,const char* servername)
598 log(DEBUG,"ChanAnyOnThisServer");
600 for (unsigned int i = 0; i < u->chans.size(); i++)
602 if (u->chans[i].channel)
604 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
605 for (unsigned int j = 0; j < ulist->size(); j++)
607 char* o = (*ulist)[j];
608 userrec* user = (userrec*)o;
609 if (!strcasecmp(user->server,servername))
618 void WriteMode(const char* modes, int flags, const char* text, ...)
620 if ((!text) || (!modes) || (!flags))
622 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
626 char textbuffer[MAXBUF];
628 va_start (argsPtr, text);
629 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
631 int modelen = strlen(modes);
633 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
635 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
637 bool send_to_user = false;
642 for (int n = 0; n < modelen; n++)
644 if (!hasumode(i->second,modes[n]))
646 send_to_user = false;
651 else if (flags == WM_OR)
653 send_to_user = false;
654 for (int n = 0; n < modelen; n++)
656 if (hasumode(i->second,modes[n]))
666 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
672 void NoticeAll(userrec *source, bool local_only, char* text, ...)
674 if ((!text) || (!source))
676 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
680 char textbuffer[MAXBUF];
682 va_start (argsPtr, text);
683 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
686 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
688 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
690 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
697 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
699 if ((!text) || (!source))
701 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
705 char textbuffer[MAXBUF];
707 va_start (argsPtr, text);
708 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
711 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
713 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
715 if (strchr(i->second->modes,'w'))
717 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
723 /* convert a string to lowercase. Note following special circumstances
724 * taken from RFC 1459. Many "official" server branches still hold to this
725 * rule so i will too;
727 * Because of IRC's scandanavian origin, the characters {}| are
728 * considered to be the lower case equivalents of the characters []\,
729 * respectively. This is a critical issue when determining the
730 * equivalence of two nicknames.
733 void strlower(char *n)
737 for (char* t = n; *t; t++)
738 *t = lowermap[(unsigned)*t];
742 /* Find a user record by nickname and return a pointer to it */
744 userrec* Find(std::string nick)
746 user_hash::iterator iter = clientlist.find(nick);
748 if (iter == clientlist.end())
749 /* Couldn't find it */
755 /* find a channel record by channel name and return a pointer to it */
757 chanrec* FindChan(const char* chan)
761 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
765 chan_hash::iterator iter = chanlist.find(chan);
767 if (iter == chanlist.end())
768 /* Couldn't find it */
775 long GetMaxBans(char* name)
778 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
780 ConfValue("banlist","chan",count,CM,&config_f);
783 ConfValue("banlist","limit",count,CM,&config_f);
790 void purge_empty_chans(userrec* u)
793 int go_again = 1, purge = 0;
795 // firstly decrement the count on each channel
796 for (unsigned int f = 0; f < u->chans.size(); f++)
798 if (u->chans[f].channel)
800 u->chans[f].channel->DelUser((char*)u);
804 for (unsigned int i = 0; i < u->chans.size(); i++)
806 if (u->chans[i].channel)
808 if (!usercount(u->chans[i].channel))
810 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
811 /* kill the record */
812 if (i2 != chanlist.end())
814 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
820 u->chans[i].channel = NULL;
825 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
829 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
835 char scratch[MAXBUF];
838 char* chanmodes(chanrec *chan)
842 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
849 if (chan->binarymodes & CM_NOEXTERNAL)
851 strlcat(scratch,"n",MAXMODES);
853 if (chan->binarymodes & CM_TOPICLOCK)
855 strlcat(scratch,"t",MAXMODES);
859 strlcat(scratch,"k",MAXMODES);
863 strlcat(scratch,"l",MAXMODES);
865 if (chan->binarymodes & CM_INVITEONLY)
867 strlcat(scratch,"i",MAXMODES);
869 if (chan->binarymodes & CM_MODERATED)
871 strlcat(scratch,"m",MAXMODES);
873 if (chan->binarymodes & CM_SECRET)
875 strlcat(scratch,"s",MAXMODES);
877 if (chan->binarymodes & CM_PRIVATE)
879 strlcat(scratch,"p",MAXMODES);
883 strlcat(sparam," ",MAXBUF);
884 strlcat(sparam,chan->key,MAXBUF);
889 sprintf(foo," %lu",(unsigned long)chan->limit);
890 strlcat(sparam,foo,MAXBUF);
892 if (*chan->custom_modes)
894 strlcat(scratch,chan->custom_modes,MAXMODES);
895 for (int z = 0; chan->custom_modes[z] != 0; z++)
897 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
900 strlcat(sparam," ",MAXBUF);
901 strlcat(sparam,extparam.c_str(),MAXBUF);
905 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
906 strlcat(scratch,sparam,MAXMODES);
911 /* compile a userlist of a channel into a string, each nick seperated by
912 * spaces and op, voice etc status shown as @ and + */
914 void userlist(userrec *user,chanrec *c)
918 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
922 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
924 std::vector<char*> *ulist = c->GetUsers();
925 for (unsigned int i = 0; i < ulist->size(); i++)
927 char* o = (*ulist)[i];
928 userrec* otheruser = (userrec*)o;
929 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
931 /* user is +i, and source not on the channel, does not show
932 * nick in NAMES list */
935 strlcat(list,cmode(otheruser,c),MAXBUF);
936 strlcat(list,otheruser->nick,MAXBUF);
937 strlcat(list," ",MAXBUF);
938 if (strlen(list)>(480-NICKMAX))
940 /* list overflowed into
941 * multiple numerics */
942 WriteServ(user->fd,"%s",list);
943 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
946 /* if whats left in the list isnt empty, send it */
947 if (list[strlen(list)-1] != ':')
949 WriteServ(user->fd,"%s",list);
953 /* return a count of the users on a specific channel accounting for
954 * invisible users who won't increase the count. e.g. for /LIST */
956 int usercount_i(chanrec *c)
962 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
966 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
970 if (has_channel(i->second,c))
972 if (isnick(i->second->nick))
974 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
976 /* user is +i, and source not on the channel, does not show
977 * nick in NAMES list */
985 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
990 int usercount(chanrec *c)
994 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
997 int count = c->GetUserCounter();
998 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
1003 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1005 char* Passwd(userrec *user)
1007 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1009 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1017 bool IsDenied(userrec *user)
1019 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1021 if (match(user->host,i->host) && (i->type == CC_DENY))
1032 /* sends out an error notice to all connected clients (not to be used
1035 void send_error(char *s)
1037 log(DEBUG,"send_error: %s",s);
1038 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1040 if (isnick(i->second->nick))
1042 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1046 // fix - unregistered connections receive ERROR, not NOTICE
1047 Write(i->second->fd,"ERROR :%s",s);
1052 void Error(int status)
1054 signal (SIGALRM, SIG_IGN);
1055 signal (SIGPIPE, SIG_IGN);
1056 signal (SIGTERM, SIG_IGN);
1057 signal (SIGABRT, SIG_IGN);
1058 signal (SIGSEGV, SIG_IGN);
1059 signal (SIGURG, SIG_IGN);
1060 signal (SIGKILL, SIG_IGN);
1061 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1062 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1066 // this function counts all users connected, wether they are registered or NOT.
1069 return clientlist.size();
1072 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1073 int registered_usercount(void)
1076 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1078 if (i->second->registered == 7) c++;
1083 int usercount_invisible(void)
1086 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1088 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1093 int usercount_opers(void)
1096 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1098 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1103 int usercount_unknown(void)
1107 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1109 if ((i->second->fd > -1) && (i->second->registered != 7))
1115 long chancount(void)
1117 return chanlist.size();
1123 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1125 if ((isnick(i->second->nick)) && (i->second->fd > -1)) c++;
1130 void ShowMOTD(userrec *user)
1133 std::string WholeMOTD = "";
1136 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1139 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1140 WholeMOTD = WholeMOTD + buf;
1141 for (unsigned int i = 0; i != MOTD.size(); i++)
1143 snprintf(buf,65535,":%s 372 %s :- %s\r\n", Config->ServerName, user->nick, MOTD[i].c_str());
1144 WholeMOTD = WholeMOTD + buf;
1146 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1147 WholeMOTD = WholeMOTD + buf;
1148 // only one write operation
1149 user->AddWriteBuf(WholeMOTD);
1150 stats->statsSent += WholeMOTD.length();
1153 void ShowRULES(userrec *user)
1157 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1160 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1161 for (unsigned int i = 0; i != RULES.size(); i++)
1163 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1165 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1168 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1169 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1170 // registration timeout maximum seconds)
1171 bool AllModulesReportReady(userrec* user)
1173 for (int i = 0; i <= MODCOUNT; i++)
1175 int res = modules[i]->OnCheckReady(user);
1182 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1185 /* create the command and push it onto the table */
1186 strlcpy(comm.command,cmd,MAXBUF);
1187 strlcpy(comm.source,source,MAXBUF);
1188 comm.handler_function = f;
1189 comm.flags_needed = flags;
1190 comm.min_params = minparams;
1192 comm.total_bytes = 0;
1193 cmdlist.push_back(comm);
1194 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1198 void SetupCommandTable(void)
1200 createcommand("USER",handle_user,0,4,"<core>");
1201 createcommand("NICK",handle_nick,0,1,"<core>");
1202 createcommand("QUIT",handle_quit,0,0,"<core>");
1203 createcommand("VERSION",handle_version,0,0,"<core>");
1204 createcommand("PING",handle_ping,0,1,"<core>");
1205 createcommand("PONG",handle_pong,0,1,"<core>");
1206 createcommand("ADMIN",handle_admin,0,0,"<core>");
1207 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1208 createcommand("INFO",handle_info,0,0,"<core>");
1209 createcommand("TIME",handle_time,0,0,"<core>");
1210 createcommand("WHOIS",handle_whois,0,1,"<core>");
1211 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1212 createcommand("NOTICE",handle_notice,0,2,"<core>");
1213 createcommand("JOIN",handle_join,0,1,"<core>");
1214 createcommand("NAMES",handle_names,0,0,"<core>");
1215 createcommand("PART",handle_part,0,1,"<core>");
1216 createcommand("KICK",handle_kick,0,2,"<core>");
1217 createcommand("MODE",handle_mode,0,1,"<core>");
1218 createcommand("TOPIC",handle_topic,0,1,"<core>");
1219 createcommand("WHO",handle_who,0,1,"<core>");
1220 createcommand("MOTD",handle_motd,0,0,"<core>");
1221 createcommand("RULES",handle_rules,0,0,"<core>");
1222 createcommand("OPER",handle_oper,0,2,"<core>");
1223 createcommand("LIST",handle_list,0,0,"<core>");
1224 createcommand("DIE",handle_die,'o',1,"<core>");
1225 createcommand("RESTART",handle_restart,'o',1,"<core>");
1226 createcommand("KILL",handle_kill,'o',2,"<core>");
1227 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1228 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1229 createcommand("STATS",handle_stats,0,1,"<core>");
1230 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1231 createcommand("AWAY",handle_away,0,0,"<core>");
1232 createcommand("ISON",handle_ison,0,0,"<core>");
1233 createcommand("SUMMON",handle_summon,0,0,"<core>");
1234 createcommand("USERS",handle_users,0,0,"<core>");
1235 createcommand("INVITE",handle_invite,0,0,"<core>");
1236 createcommand("PASS",handle_pass,0,1,"<core>");
1237 createcommand("TRACE",handle_trace,'o',0,"<core>");
1238 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1239 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1240 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1241 createcommand("MODULES",handle_modules,0,0,"<core>");
1242 createcommand("LINKS",handle_links,0,0,"<core>");
1243 createcommand("MAP",handle_map,0,0,"<core>");
1244 createcommand("KLINE",handle_kline,'o',1,"<core>");
1245 createcommand("GLINE",handle_gline,'o',1,"<core>");
1246 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1247 createcommand("QLINE",handle_qline,'o',1,"<core>");
1248 createcommand("ELINE",handle_eline,'o',1,"<core>");
1249 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1250 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1251 createcommand("SERVER",handle_server,0,0,"<core>");
1252 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1255 bool DirValid(char* dirandfile)
1258 strlcpy(work,dirandfile,MAXBUF);
1259 int p = strlen(work);
1260 // we just want the dir
1261 while (strlen(work))
1270 char buffer[MAXBUF], otherdir[MAXBUF];
1271 // Get the current working directory
1272 if( getcwd( buffer, MAXBUF ) == NULL )
1275 if( getcwd( otherdir, MAXBUF ) == NULL )
1278 if (strlen(otherdir) >= strlen(work))
1280 otherdir[strlen(work)] = '\0';
1281 if (!strcmp(otherdir,work))
1290 std::string GetFullProgDir(char** argv, int argc)
1293 strlcpy(work,argv[0],MAXBUF);
1294 int p = strlen(work);
1295 // we just want the dir
1296 while (strlen(work))
1305 char buffer[MAXBUF], otherdir[MAXBUF];
1306 // Get the current working directory
1307 if( getcwd( buffer, MAXBUF ) == NULL )
1310 if( getcwd( otherdir, MAXBUF ) == NULL )