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;
57 extern char lowermap[255];
58 static char list[MAXBUF];
59 extern userrec* fd_ref_table[65536];
60 extern serverstats* stats;
61 static char already_sent[65536];
62 extern std::vector<userrec*> all_opers;
63 extern ClassVector Classes;
64 extern user_hash clientlist;
65 extern chan_hash chanlist;
66 extern command_table cmdlist;
68 void log(int level,char *text, ...)
70 char textbuffer[MAXBUF];
74 if (level < Config->LogLevel)
78 timeinfo = localtime(&rawtime);
83 va_start (argsPtr, text);
84 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
86 strlcpy(b,asctime(timeinfo),MAXBUF);
87 b[24] = ':'; // we know this is the end of the time string
89 fprintf(Config->log_file,"%s %s\n",b,textbuffer);
92 // nofork enabled? display it on terminal too
93 printf("%s %s\n",b,textbuffer);
98 void readfile(file_cache &F, const char* fname)
101 char linebuf[MAXBUF];
103 log(DEBUG,"readfile: loading %s",fname);
105 file = fopen(fname,"r");
110 fgets(linebuf,sizeof(linebuf),file);
111 linebuf[strlen(linebuf)-1]='\0';
118 F.push_back(linebuf);
125 log(DEBUG,"readfile: failed to load file: %s",fname);
127 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
130 void Write(int sock,char *text, ...)
136 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
139 char textbuffer[MAXBUF];
143 va_start (argsPtr, text);
144 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
146 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
148 if (fd_ref_table[sock])
151 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
152 fd_ref_table[sock]->AddWriteBuf(tb);
153 stats->statsSent += bytes;
155 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
158 /* write a server formatted numeric response to a single socket */
160 void WriteServ(int sock, char* text, ...)
166 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
169 char textbuffer[MAXBUF],tb[MAXBUF];
171 va_start (argsPtr, text);
173 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
175 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
177 if (fd_ref_table[sock])
180 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
181 fd_ref_table[sock]->AddWriteBuf(tb);
182 stats->statsSent += bytes;
184 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
187 /* write text from an originating user to originating user */
189 void WriteFrom(int sock, userrec *user,char* text, ...)
193 if ((!text) || (!user))
195 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
198 char textbuffer[MAXBUF],tb[MAXBUF];
200 va_start (argsPtr, text);
202 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
204 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
206 if (fd_ref_table[sock])
209 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
210 fd_ref_table[sock]->AddWriteBuf(tb);
211 stats->statsSent += bytes;
213 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
216 /* write text to an destination user from a source user (e.g. user privmsg) */
218 void WriteTo(userrec *source, userrec *dest,char *data, ...)
220 if ((!dest) || (!data))
222 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
225 if (dest->fd == FD_MAGIC_NUMBER)
227 char textbuffer[MAXBUF],tb[MAXBUF];
229 va_start (argsPtr, data);
230 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
234 // if no source given send it from the server.
237 WriteServ(dest->fd,":%s %s",Config->ServerName,textbuffer);
241 WriteFrom(dest->fd,source,"%s",textbuffer);
245 /* write formatted text from a source user to all users on a channel
246 * including the sender (NOT for privmsg, notice etc!) */
248 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
250 if ((!Ptr) || (!user) || (!text))
252 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
255 char textbuffer[MAXBUF];
257 va_start (argsPtr, text);
258 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
261 std::vector<char*> *ulist = Ptr->GetUsers();
262 for (unsigned int j = 0; j < ulist->size(); j++)
264 char* o = (*ulist)[j];
265 userrec* otheruser = (userrec*)o;
266 if (otheruser->fd != FD_MAGIC_NUMBER)
267 WriteTo(user,otheruser,"%s",textbuffer);
271 /* write formatted text from a source user to all users on a channel
272 * including the sender (NOT for privmsg, notice etc!) doesnt send to
273 * users on remote servers */
275 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
277 if ((!Ptr) || (!text))
279 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
282 char textbuffer[MAXBUF];
284 va_start (argsPtr, text);
285 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
288 std::vector<char*> *ulist = Ptr->GetUsers();
289 for (unsigned int j = 0; j < ulist->size(); j++)
291 char* o = (*ulist)[j];
292 userrec* otheruser = (userrec*)o;
293 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
297 WriteServ(otheruser->fd,"%s",textbuffer);
301 WriteTo(user,otheruser,"%s",textbuffer);
307 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
309 if ((!Ptr) || (!text))
311 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
314 char textbuffer[MAXBUF];
316 va_start (argsPtr, text);
317 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
321 std::vector<char*> *ulist = Ptr->GetUsers();
322 for (unsigned int j = 0; j < ulist->size(); j++)
324 char* o = (*ulist)[j];
325 userrec* otheruser = (userrec*)o;
326 if (otheruser->fd != FD_MAGIC_NUMBER)
327 WriteServ(otheruser->fd,"%s",textbuffer);
331 /* write formatted text from a source user to all users on a channel except
332 * for the sender (for privmsg etc) */
334 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
336 if ((!Ptr) || (!user) || (!text))
338 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
341 char textbuffer[MAXBUF];
343 va_start (argsPtr, text);
344 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
347 std::vector<char*> *ulist = Ptr->GetUsers();
348 for (unsigned int j = 0; j < ulist->size(); j++)
350 char* o = (*ulist)[j];
351 userrec* otheruser = (userrec*)o;
352 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
353 WriteFrom(otheruser->fd,user,"%s",textbuffer);
357 std::string GetServerDescription(char* servername)
359 std::string description = "";
360 FOREACH_MOD OnGetServerDescription(servername,description);
361 if (description != "")
367 return Config->ServerDesc; // not a remote server that can be found, it must be me.
371 /* write a formatted string to all users who share at least one common
372 * channel, including the source user e.g. for use in NICK */
374 void WriteCommon(userrec *u, char* text, ...)
378 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
382 if (u->registered != 7) {
383 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
387 char textbuffer[MAXBUF];
389 va_start (argsPtr, text);
390 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
393 // FIX: Stops a message going to the same person more than once
394 memset(&already_sent,0,65536);
396 bool sent_to_at_least_one = false;
398 for (unsigned int i = 0; i < u->chans.size(); i++)
400 if (u->chans[i].channel)
402 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
403 for (unsigned int j = 0; j < ulist->size(); j++)
405 char* o = (*ulist)[j];
406 userrec* otheruser = (userrec*)o;
407 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
409 already_sent[otheruser->fd] = 1;
410 WriteFrom(otheruser->fd,u,"%s",textbuffer);
411 sent_to_at_least_one = true;
416 // if the user was not in any channels, no users will receive the text. Make sure the user
417 // receives their OWN message for WriteCommon
418 if (!sent_to_at_least_one)
420 WriteFrom(u->fd,u,"%s",textbuffer);
424 /* write a formatted string to all users who share at least one common
425 * channel, NOT including the source user e.g. for use in QUIT */
427 void WriteCommonExcept(userrec *u, char* text, ...)
431 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
435 if (u->registered != 7) {
436 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
440 char textbuffer[MAXBUF];
442 va_start (argsPtr, text);
443 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
446 memset(&already_sent,0,65536);
448 for (unsigned int i = 0; i < u->chans.size(); i++)
450 if (u->chans[i].channel)
452 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
453 for (unsigned int j = 0; j < ulist->size(); j++)
455 char* o = (*ulist)[j];
456 userrec* otheruser = (userrec*)o;
459 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
461 already_sent[otheruser->fd] = 1;
462 WriteFrom(otheruser->fd,u,"%s",textbuffer);
470 void WriteOpers(char* text, ...)
474 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
478 char textbuffer[MAXBUF];
480 va_start (argsPtr, text);
481 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
484 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
487 if ((a) && (a->fd != FD_MAGIC_NUMBER))
489 if (strchr(a->modes,'s'))
491 // send server notices to all with +s
492 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
498 void ServerNoticeAll(char* text, ...)
503 char textbuffer[MAXBUF];
505 va_start (argsPtr, text);
506 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
509 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
511 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
513 WriteServ(i->second->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
518 void ServerPrivmsgAll(char* text, ...)
523 char textbuffer[MAXBUF];
525 va_start (argsPtr, text);
526 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
529 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
531 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
533 WriteServ(i->second->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
538 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
540 if ((!text) || (!source))
542 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
546 char textbuffer[MAXBUF];
548 va_start (argsPtr, text);
549 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
552 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
555 if ((a) && (a->fd != FD_MAGIC_NUMBER))
557 if (strchr(a->modes,'s'))
559 // send server notices to all with +s
560 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
566 // returns TRUE of any users on channel C occupy server 'servername'.
568 bool ChanAnyOnThisServer(chanrec *c,char* servername)
570 log(DEBUG,"ChanAnyOnThisServer");
572 std::vector<char*> *ulist = c->GetUsers();
573 for (unsigned int j = 0; j < ulist->size(); j++)
575 char* o = (*ulist)[j];
576 userrec* user = (userrec*)o;
577 if (!strcasecmp(user->server,servername))
583 // returns true if user 'u' shares any common channels with any users on server 'servername'
585 bool CommonOnThisServer(userrec* u,const char* servername)
587 log(DEBUG,"ChanAnyOnThisServer");
589 for (unsigned int i = 0; i < u->chans.size(); i++)
591 if (u->chans[i].channel)
593 std::vector<char*> *ulist = u->chans[i].channel->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))
607 void WriteMode(const char* modes, int flags, const char* text, ...)
609 if ((!text) || (!modes) || (!flags))
611 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
615 char textbuffer[MAXBUF];
617 va_start (argsPtr, text);
618 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
620 int modelen = strlen(modes);
622 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
624 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
626 bool send_to_user = false;
631 for (int n = 0; n < modelen; n++)
633 if (!hasumode(i->second,modes[n]))
635 send_to_user = false;
640 else if (flags == WM_OR)
642 send_to_user = false;
643 for (int n = 0; n < modelen; n++)
645 if (hasumode(i->second,modes[n]))
655 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
661 void NoticeAll(userrec *source, bool local_only, char* text, ...)
663 if ((!text) || (!source))
665 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
669 char textbuffer[MAXBUF];
671 va_start (argsPtr, text);
672 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
675 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
677 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
679 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
686 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
688 if ((!text) || (!source))
690 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
694 char textbuffer[MAXBUF];
696 va_start (argsPtr, text);
697 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
700 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
702 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
704 if (strchr(i->second->modes,'w'))
706 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
712 /* convert a string to lowercase. Note following special circumstances
713 * taken from RFC 1459. Many "official" server branches still hold to this
714 * rule so i will too;
716 * Because of IRC's scandanavian origin, the characters {}| are
717 * considered to be the lower case equivalents of the characters []\,
718 * respectively. This is a critical issue when determining the
719 * equivalence of two nicknames.
722 void strlower(char *n)
726 for (char* t = n; *t; t++)
727 *t = lowermap[(unsigned)*t];
731 /* Find a user record by nickname and return a pointer to it */
733 userrec* Find(std::string nick)
735 user_hash::iterator iter = clientlist.find(nick);
737 if (iter == clientlist.end())
738 /* Couldn't find it */
744 /* find a channel record by channel name and return a pointer to it */
746 chanrec* FindChan(const char* chan)
750 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
754 chan_hash::iterator iter = chanlist.find(chan);
756 if (iter == chanlist.end())
757 /* Couldn't find it */
764 long GetMaxBans(char* name)
767 for (int count = 0; count < ConfValueEnum("banlist",&Config->config_f); count++)
769 ConfValue("banlist","chan",count,CM,&Config->config_f);
772 ConfValue("banlist","limit",count,CM,&Config->config_f);
779 void purge_empty_chans(userrec* u)
782 int go_again = 1, purge = 0;
784 // firstly decrement the count on each channel
785 for (unsigned int f = 0; f < u->chans.size(); f++)
787 if (u->chans[f].channel)
789 u->chans[f].channel->DelUser((char*)u);
793 for (unsigned int i = 0; i < u->chans.size(); i++)
795 if (u->chans[i].channel)
797 if (!usercount(u->chans[i].channel))
799 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
800 /* kill the record */
801 if (i2 != chanlist.end())
803 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
809 u->chans[i].channel = NULL;
814 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
818 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
824 char scratch[MAXBUF];
827 char* chanmodes(chanrec *chan)
831 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
838 if (chan->binarymodes & CM_NOEXTERNAL)
840 strlcat(scratch,"n",MAXMODES);
842 if (chan->binarymodes & CM_TOPICLOCK)
844 strlcat(scratch,"t",MAXMODES);
848 strlcat(scratch,"k",MAXMODES);
852 strlcat(scratch,"l",MAXMODES);
854 if (chan->binarymodes & CM_INVITEONLY)
856 strlcat(scratch,"i",MAXMODES);
858 if (chan->binarymodes & CM_MODERATED)
860 strlcat(scratch,"m",MAXMODES);
862 if (chan->binarymodes & CM_SECRET)
864 strlcat(scratch,"s",MAXMODES);
866 if (chan->binarymodes & CM_PRIVATE)
868 strlcat(scratch,"p",MAXMODES);
872 strlcat(sparam," ",MAXBUF);
873 strlcat(sparam,chan->key,MAXBUF);
878 sprintf(foo," %lu",(unsigned long)chan->limit);
879 strlcat(sparam,foo,MAXBUF);
881 if (*chan->custom_modes)
883 strlcat(scratch,chan->custom_modes,MAXMODES);
884 for (int z = 0; chan->custom_modes[z] != 0; z++)
886 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
889 strlcat(sparam," ",MAXBUF);
890 strlcat(sparam,extparam.c_str(),MAXBUF);
894 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
895 strlcat(scratch,sparam,MAXMODES);
900 /* compile a userlist of a channel into a string, each nick seperated by
901 * spaces and op, voice etc status shown as @ and + */
903 void userlist(userrec *user,chanrec *c)
907 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
911 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
913 std::vector<char*> *ulist = c->GetUsers();
914 for (unsigned int i = 0; i < ulist->size(); i++)
916 char* o = (*ulist)[i];
917 userrec* otheruser = (userrec*)o;
918 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
920 /* user is +i, and source not on the channel, does not show
921 * nick in NAMES list */
924 strlcat(list,cmode(otheruser,c),MAXBUF);
925 strlcat(list,otheruser->nick,MAXBUF);
926 strlcat(list," ",MAXBUF);
927 if (strlen(list)>(480-NICKMAX))
929 /* list overflowed into
930 * multiple numerics */
931 WriteServ(user->fd,"%s",list);
932 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
935 /* if whats left in the list isnt empty, send it */
936 if (list[strlen(list)-1] != ':')
938 WriteServ(user->fd,"%s",list);
942 /* return a count of the users on a specific channel accounting for
943 * invisible users who won't increase the count. e.g. for /LIST */
945 int usercount_i(chanrec *c)
951 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
955 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
959 if (has_channel(i->second,c))
961 if (isnick(i->second->nick))
963 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
965 /* user is +i, and source not on the channel, does not show
966 * nick in NAMES list */
974 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
979 int usercount(chanrec *c)
983 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
986 int count = c->GetUserCounter();
987 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
992 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
994 char* Passwd(userrec *user)
996 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
998 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1006 bool IsDenied(userrec *user)
1008 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1010 if (match(user->host,i->host) && (i->type == CC_DENY))
1021 /* sends out an error notice to all connected clients (not to be used
1024 void send_error(char *s)
1026 log(DEBUG,"send_error: %s",s);
1027 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1029 if (isnick(i->second->nick))
1031 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1035 // fix - unregistered connections receive ERROR, not NOTICE
1036 Write(i->second->fd,"ERROR :%s",s);
1041 void Error(int status)
1043 signal (SIGALRM, SIG_IGN);
1044 signal (SIGPIPE, SIG_IGN);
1045 signal (SIGTERM, SIG_IGN);
1046 signal (SIGABRT, SIG_IGN);
1047 signal (SIGSEGV, SIG_IGN);
1048 signal (SIGURG, SIG_IGN);
1049 signal (SIGKILL, SIG_IGN);
1050 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1051 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1055 // this function counts all users connected, wether they are registered or NOT.
1058 return clientlist.size();
1061 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1062 int registered_usercount(void)
1065 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1067 if (i->second->registered == 7) c++;
1072 int usercount_invisible(void)
1075 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1077 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1082 int usercount_opers(void)
1085 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1087 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1092 int usercount_unknown(void)
1096 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1098 if ((i->second->fd > -1) && (i->second->registered != 7))
1104 long chancount(void)
1106 return chanlist.size();
1112 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1114 if ((isnick(i->second->nick)) && (i->second->fd > -1)) c++;
1119 void ShowMOTD(userrec *user)
1122 std::string WholeMOTD = "";
1123 if (!Config->MOTD.size())
1125 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1128 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1129 WholeMOTD = WholeMOTD + buf;
1130 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1132 snprintf(buf,65535,":%s 372 %s :- %s\r\n", Config->ServerName, user->nick, Config->MOTD[i].c_str());
1133 WholeMOTD = WholeMOTD + buf;
1135 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1136 WholeMOTD = WholeMOTD + buf;
1137 // only one write operation
1138 user->AddWriteBuf(WholeMOTD);
1139 stats->statsSent += WholeMOTD.length();
1142 void ShowRULES(userrec *user)
1144 if (!Config->RULES.size())
1146 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1149 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1150 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1152 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1154 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1157 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1158 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1159 // registration timeout maximum seconds)
1160 bool AllModulesReportReady(userrec* user)
1162 for (int i = 0; i <= MODCOUNT; i++)
1164 int res = modules[i]->OnCheckReady(user);
1171 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1174 /* create the command and push it onto the table */
1175 strlcpy(comm.command,cmd,MAXBUF);
1176 strlcpy(comm.source,source,MAXBUF);
1177 comm.handler_function = f;
1178 comm.flags_needed = flags;
1179 comm.min_params = minparams;
1181 comm.total_bytes = 0;
1182 cmdlist.push_back(comm);
1183 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1187 void SetupCommandTable(void)
1189 createcommand("USER",handle_user,0,4,"<core>");
1190 createcommand("NICK",handle_nick,0,1,"<core>");
1191 createcommand("QUIT",handle_quit,0,0,"<core>");
1192 createcommand("VERSION",handle_version,0,0,"<core>");
1193 createcommand("PING",handle_ping,0,1,"<core>");
1194 createcommand("PONG",handle_pong,0,1,"<core>");
1195 createcommand("ADMIN",handle_admin,0,0,"<core>");
1196 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1197 createcommand("INFO",handle_info,0,0,"<core>");
1198 createcommand("TIME",handle_time,0,0,"<core>");
1199 createcommand("WHOIS",handle_whois,0,1,"<core>");
1200 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1201 createcommand("NOTICE",handle_notice,0,2,"<core>");
1202 createcommand("JOIN",handle_join,0,1,"<core>");
1203 createcommand("NAMES",handle_names,0,0,"<core>");
1204 createcommand("PART",handle_part,0,1,"<core>");
1205 createcommand("KICK",handle_kick,0,2,"<core>");
1206 createcommand("MODE",handle_mode,0,1,"<core>");
1207 createcommand("TOPIC",handle_topic,0,1,"<core>");
1208 createcommand("WHO",handle_who,0,1,"<core>");
1209 createcommand("MOTD",handle_motd,0,0,"<core>");
1210 createcommand("RULES",handle_rules,0,0,"<core>");
1211 createcommand("OPER",handle_oper,0,2,"<core>");
1212 createcommand("LIST",handle_list,0,0,"<core>");
1213 createcommand("DIE",handle_die,'o',1,"<core>");
1214 createcommand("RESTART",handle_restart,'o',1,"<core>");
1215 createcommand("KILL",handle_kill,'o',2,"<core>");
1216 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1217 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1218 createcommand("STATS",handle_stats,0,1,"<core>");
1219 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1220 createcommand("AWAY",handle_away,0,0,"<core>");
1221 createcommand("ISON",handle_ison,0,0,"<core>");
1222 createcommand("SUMMON",handle_summon,0,0,"<core>");
1223 createcommand("USERS",handle_users,0,0,"<core>");
1224 createcommand("INVITE",handle_invite,0,0,"<core>");
1225 createcommand("PASS",handle_pass,0,1,"<core>");
1226 createcommand("TRACE",handle_trace,'o',0,"<core>");
1227 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1228 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1229 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1230 createcommand("MODULES",handle_modules,0,0,"<core>");
1231 createcommand("LINKS",handle_links,0,0,"<core>");
1232 createcommand("MAP",handle_map,0,0,"<core>");
1233 createcommand("KLINE",handle_kline,'o',1,"<core>");
1234 createcommand("GLINE",handle_gline,'o',1,"<core>");
1235 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1236 createcommand("QLINE",handle_qline,'o',1,"<core>");
1237 createcommand("ELINE",handle_eline,'o',1,"<core>");
1238 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1239 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1240 createcommand("SERVER",handle_server,0,0,"<core>");
1241 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1244 bool DirValid(char* dirandfile)
1247 strlcpy(work,dirandfile,MAXBUF);
1248 int p = strlen(work);
1249 // we just want the dir
1250 while (strlen(work))
1259 char buffer[MAXBUF], otherdir[MAXBUF];
1260 // Get the current working directory
1261 if( getcwd( buffer, MAXBUF ) == NULL )
1264 if( getcwd( otherdir, MAXBUF ) == NULL )
1267 if (strlen(otherdir) >= strlen(work))
1269 otherdir[strlen(work)] = '\0';
1270 if (!strcmp(otherdir,work))
1279 std::string GetFullProgDir(char** argv, int argc)
1282 strlcpy(work,argv[0],MAXBUF);
1283 int p = strlen(work);
1284 // we just want the dir
1285 while (strlen(work))
1294 char buffer[MAXBUF], otherdir[MAXBUF];
1295 // Get the current working directory
1296 if( getcwd( buffer, MAXBUF ) == NULL )
1299 if( getcwd( otherdir, MAXBUF ) == NULL )