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 extern user_hash clientlist;
71 extern chan_hash chanlist;
72 extern command_table cmdlist;
73 extern file_cache MOTD;
74 extern file_cache RULES;
76 void log(int level,char *text, ...)
78 char textbuffer[MAXBUF];
82 if (level < Config->LogLevel)
86 timeinfo = localtime(&rawtime);
91 va_start (argsPtr, text);
92 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
94 strlcpy(b,asctime(timeinfo),MAXBUF);
95 b[24] = ':'; // we know this is the end of the time string
97 fprintf(Config->log_file,"%s %s\n",b,textbuffer);
100 // nofork enabled? display it on terminal too
101 printf("%s %s\n",b,textbuffer);
106 void readfile(file_cache &F, const char* fname)
109 char linebuf[MAXBUF];
111 log(DEBUG,"readfile: loading %s",fname);
113 file = fopen(fname,"r");
118 fgets(linebuf,sizeof(linebuf),file);
119 linebuf[strlen(linebuf)-1]='\0';
126 F.push_back(linebuf);
133 log(DEBUG,"readfile: failed to load file: %s",fname);
135 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
138 void Write(int sock,char *text, ...)
144 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
147 char textbuffer[MAXBUF];
151 va_start (argsPtr, text);
152 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
154 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
156 if (fd_ref_table[sock])
159 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
160 fd_ref_table[sock]->AddWriteBuf(tb);
161 stats->statsSent += bytes;
163 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
166 /* write a server formatted numeric response to a single socket */
168 void WriteServ(int sock, char* text, ...)
174 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
177 char textbuffer[MAXBUF],tb[MAXBUF];
179 va_start (argsPtr, text);
181 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
183 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
185 if (fd_ref_table[sock])
188 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
189 fd_ref_table[sock]->AddWriteBuf(tb);
190 stats->statsSent += bytes;
192 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
195 /* write text from an originating user to originating user */
197 void WriteFrom(int sock, userrec *user,char* text, ...)
201 if ((!text) || (!user))
203 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
206 char textbuffer[MAXBUF],tb[MAXBUF];
208 va_start (argsPtr, text);
210 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
212 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
214 if (fd_ref_table[sock])
217 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
218 fd_ref_table[sock]->AddWriteBuf(tb);
219 stats->statsSent += bytes;
221 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
224 /* write text to an destination user from a source user (e.g. user privmsg) */
226 void WriteTo(userrec *source, userrec *dest,char *data, ...)
228 if ((!dest) || (!data))
230 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
233 if (dest->fd == FD_MAGIC_NUMBER)
235 char textbuffer[MAXBUF],tb[MAXBUF];
237 va_start (argsPtr, data);
238 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
242 // if no source given send it from the server.
245 WriteServ(dest->fd,":%s %s",Config->ServerName,textbuffer);
249 WriteFrom(dest->fd,source,"%s",textbuffer);
253 /* write formatted text from a source user to all users on a channel
254 * including the sender (NOT for privmsg, notice etc!) */
256 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
258 if ((!Ptr) || (!user) || (!text))
260 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
263 char textbuffer[MAXBUF];
265 va_start (argsPtr, text);
266 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
269 std::vector<char*> *ulist = Ptr->GetUsers();
270 for (unsigned int j = 0; j < ulist->size(); j++)
272 char* o = (*ulist)[j];
273 userrec* otheruser = (userrec*)o;
274 if (otheruser->fd != FD_MAGIC_NUMBER)
275 WriteTo(user,otheruser,"%s",textbuffer);
279 /* write formatted text from a source user to all users on a channel
280 * including the sender (NOT for privmsg, notice etc!) doesnt send to
281 * users on remote servers */
283 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
285 if ((!Ptr) || (!text))
287 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
290 char textbuffer[MAXBUF];
292 va_start (argsPtr, text);
293 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
296 std::vector<char*> *ulist = Ptr->GetUsers();
297 for (unsigned int j = 0; j < ulist->size(); j++)
299 char* o = (*ulist)[j];
300 userrec* otheruser = (userrec*)o;
301 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
305 WriteServ(otheruser->fd,"%s",textbuffer);
309 WriteTo(user,otheruser,"%s",textbuffer);
315 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
317 if ((!Ptr) || (!text))
319 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
322 char textbuffer[MAXBUF];
324 va_start (argsPtr, text);
325 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
329 std::vector<char*> *ulist = Ptr->GetUsers();
330 for (unsigned int j = 0; j < ulist->size(); j++)
332 char* o = (*ulist)[j];
333 userrec* otheruser = (userrec*)o;
334 if (otheruser->fd != FD_MAGIC_NUMBER)
335 WriteServ(otheruser->fd,"%s",textbuffer);
339 /* write formatted text from a source user to all users on a channel except
340 * for the sender (for privmsg etc) */
342 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
344 if ((!Ptr) || (!user) || (!text))
346 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
349 char textbuffer[MAXBUF];
351 va_start (argsPtr, text);
352 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
355 std::vector<char*> *ulist = Ptr->GetUsers();
356 for (unsigned int j = 0; j < ulist->size(); j++)
358 char* o = (*ulist)[j];
359 userrec* otheruser = (userrec*)o;
360 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
361 WriteFrom(otheruser->fd,user,"%s",textbuffer);
365 std::string GetServerDescription(char* servername)
367 std::string description = "";
368 FOREACH_MOD OnGetServerDescription(servername,description);
369 if (description != "")
375 return Config->ServerDesc; // not a remote server that can be found, it must be me.
379 /* write a formatted string to all users who share at least one common
380 * channel, including the source user e.g. for use in NICK */
382 void WriteCommon(userrec *u, char* text, ...)
386 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
390 if (u->registered != 7) {
391 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
395 char textbuffer[MAXBUF];
397 va_start (argsPtr, text);
398 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
401 // FIX: Stops a message going to the same person more than once
402 memset(&already_sent,0,65536);
404 bool sent_to_at_least_one = false;
406 for (unsigned int i = 0; i < u->chans.size(); i++)
408 if (u->chans[i].channel)
410 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
411 for (unsigned int j = 0; j < ulist->size(); j++)
413 char* o = (*ulist)[j];
414 userrec* otheruser = (userrec*)o;
415 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
417 already_sent[otheruser->fd] = 1;
418 WriteFrom(otheruser->fd,u,"%s",textbuffer);
419 sent_to_at_least_one = true;
424 // if the user was not in any channels, no users will receive the text. Make sure the user
425 // receives their OWN message for WriteCommon
426 if (!sent_to_at_least_one)
428 WriteFrom(u->fd,u,"%s",textbuffer);
432 /* write a formatted string to all users who share at least one common
433 * channel, NOT including the source user e.g. for use in QUIT */
435 void WriteCommonExcept(userrec *u, char* text, ...)
439 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
443 if (u->registered != 7) {
444 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
448 char textbuffer[MAXBUF];
450 va_start (argsPtr, text);
451 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
454 memset(&already_sent,0,65536);
456 for (unsigned int i = 0; i < u->chans.size(); i++)
458 if (u->chans[i].channel)
460 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
461 for (unsigned int j = 0; j < ulist->size(); j++)
463 char* o = (*ulist)[j];
464 userrec* otheruser = (userrec*)o;
467 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
469 already_sent[otheruser->fd] = 1;
470 WriteFrom(otheruser->fd,u,"%s",textbuffer);
478 void WriteOpers(char* text, ...)
482 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
486 char textbuffer[MAXBUF];
488 va_start (argsPtr, text);
489 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
492 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
495 if ((a) && (a->fd != FD_MAGIC_NUMBER))
497 if (strchr(a->modes,'s'))
499 // send server notices to all with +s
500 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
506 void ServerNoticeAll(char* text, ...)
511 char textbuffer[MAXBUF];
513 va_start (argsPtr, text);
514 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
517 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
519 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
521 WriteServ(i->second->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
526 void ServerPrivmsgAll(char* text, ...)
531 char textbuffer[MAXBUF];
533 va_start (argsPtr, text);
534 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
537 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
539 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
541 WriteServ(i->second->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
546 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
548 if ((!text) || (!source))
550 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
554 char textbuffer[MAXBUF];
556 va_start (argsPtr, text);
557 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
560 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
563 if ((a) && (a->fd != FD_MAGIC_NUMBER))
565 if (strchr(a->modes,'s'))
567 // send server notices to all with +s
568 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
574 // returns TRUE of any users on channel C occupy server 'servername'.
576 bool ChanAnyOnThisServer(chanrec *c,char* servername)
578 log(DEBUG,"ChanAnyOnThisServer");
580 std::vector<char*> *ulist = c->GetUsers();
581 for (unsigned int j = 0; j < ulist->size(); j++)
583 char* o = (*ulist)[j];
584 userrec* user = (userrec*)o;
585 if (!strcasecmp(user->server,servername))
591 // returns true if user 'u' shares any common channels with any users on server 'servername'
593 bool CommonOnThisServer(userrec* u,const char* servername)
595 log(DEBUG,"ChanAnyOnThisServer");
597 for (unsigned int i = 0; i < u->chans.size(); i++)
599 if (u->chans[i].channel)
601 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
602 for (unsigned int j = 0; j < ulist->size(); j++)
604 char* o = (*ulist)[j];
605 userrec* user = (userrec*)o;
606 if (!strcasecmp(user->server,servername))
615 void WriteMode(const char* modes, int flags, const char* text, ...)
617 if ((!text) || (!modes) || (!flags))
619 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
623 char textbuffer[MAXBUF];
625 va_start (argsPtr, text);
626 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
628 int modelen = strlen(modes);
630 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
632 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
634 bool send_to_user = false;
639 for (int n = 0; n < modelen; n++)
641 if (!hasumode(i->second,modes[n]))
643 send_to_user = false;
648 else if (flags == WM_OR)
650 send_to_user = false;
651 for (int n = 0; n < modelen; n++)
653 if (hasumode(i->second,modes[n]))
663 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
669 void NoticeAll(userrec *source, bool local_only, char* text, ...)
671 if ((!text) || (!source))
673 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
677 char textbuffer[MAXBUF];
679 va_start (argsPtr, text);
680 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
683 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
685 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
687 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
694 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
696 if ((!text) || (!source))
698 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
702 char textbuffer[MAXBUF];
704 va_start (argsPtr, text);
705 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
708 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
710 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
712 if (strchr(i->second->modes,'w'))
714 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
720 /* convert a string to lowercase. Note following special circumstances
721 * taken from RFC 1459. Many "official" server branches still hold to this
722 * rule so i will too;
724 * Because of IRC's scandanavian origin, the characters {}| are
725 * considered to be the lower case equivalents of the characters []\,
726 * respectively. This is a critical issue when determining the
727 * equivalence of two nicknames.
730 void strlower(char *n)
734 for (char* t = n; *t; t++)
735 *t = lowermap[(unsigned)*t];
739 /* Find a user record by nickname and return a pointer to it */
741 userrec* Find(std::string nick)
743 user_hash::iterator iter = clientlist.find(nick);
745 if (iter == clientlist.end())
746 /* Couldn't find it */
752 /* find a channel record by channel name and return a pointer to it */
754 chanrec* FindChan(const char* chan)
758 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
762 chan_hash::iterator iter = chanlist.find(chan);
764 if (iter == chanlist.end())
765 /* Couldn't find it */
772 long GetMaxBans(char* name)
775 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
777 ConfValue("banlist","chan",count,CM,&config_f);
780 ConfValue("banlist","limit",count,CM,&config_f);
787 void purge_empty_chans(userrec* u)
790 int go_again = 1, purge = 0;
792 // firstly decrement the count on each channel
793 for (unsigned int f = 0; f < u->chans.size(); f++)
795 if (u->chans[f].channel)
797 u->chans[f].channel->DelUser((char*)u);
801 for (unsigned int i = 0; i < u->chans.size(); i++)
803 if (u->chans[i].channel)
805 if (!usercount(u->chans[i].channel))
807 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
808 /* kill the record */
809 if (i2 != chanlist.end())
811 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
817 u->chans[i].channel = NULL;
822 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
826 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
832 char scratch[MAXBUF];
835 char* chanmodes(chanrec *chan)
839 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
846 if (chan->binarymodes & CM_NOEXTERNAL)
848 strlcat(scratch,"n",MAXMODES);
850 if (chan->binarymodes & CM_TOPICLOCK)
852 strlcat(scratch,"t",MAXMODES);
856 strlcat(scratch,"k",MAXMODES);
860 strlcat(scratch,"l",MAXMODES);
862 if (chan->binarymodes & CM_INVITEONLY)
864 strlcat(scratch,"i",MAXMODES);
866 if (chan->binarymodes & CM_MODERATED)
868 strlcat(scratch,"m",MAXMODES);
870 if (chan->binarymodes & CM_SECRET)
872 strlcat(scratch,"s",MAXMODES);
874 if (chan->binarymodes & CM_PRIVATE)
876 strlcat(scratch,"p",MAXMODES);
880 strlcat(sparam," ",MAXBUF);
881 strlcat(sparam,chan->key,MAXBUF);
886 sprintf(foo," %lu",(unsigned long)chan->limit);
887 strlcat(sparam,foo,MAXBUF);
889 if (*chan->custom_modes)
891 strlcat(scratch,chan->custom_modes,MAXMODES);
892 for (int z = 0; chan->custom_modes[z] != 0; z++)
894 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
897 strlcat(sparam," ",MAXBUF);
898 strlcat(sparam,extparam.c_str(),MAXBUF);
902 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
903 strlcat(scratch,sparam,MAXMODES);
908 /* compile a userlist of a channel into a string, each nick seperated by
909 * spaces and op, voice etc status shown as @ and + */
911 void userlist(userrec *user,chanrec *c)
915 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
919 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
921 std::vector<char*> *ulist = c->GetUsers();
922 for (unsigned int i = 0; i < ulist->size(); i++)
924 char* o = (*ulist)[i];
925 userrec* otheruser = (userrec*)o;
926 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
928 /* user is +i, and source not on the channel, does not show
929 * nick in NAMES list */
932 strlcat(list,cmode(otheruser,c),MAXBUF);
933 strlcat(list,otheruser->nick,MAXBUF);
934 strlcat(list," ",MAXBUF);
935 if (strlen(list)>(480-NICKMAX))
937 /* list overflowed into
938 * multiple numerics */
939 WriteServ(user->fd,"%s",list);
940 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
943 /* if whats left in the list isnt empty, send it */
944 if (list[strlen(list)-1] != ':')
946 WriteServ(user->fd,"%s",list);
950 /* return a count of the users on a specific channel accounting for
951 * invisible users who won't increase the count. e.g. for /LIST */
953 int usercount_i(chanrec *c)
959 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
963 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
967 if (has_channel(i->second,c))
969 if (isnick(i->second->nick))
971 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
973 /* user is +i, and source not on the channel, does not show
974 * nick in NAMES list */
982 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
987 int usercount(chanrec *c)
991 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
994 int count = c->GetUserCounter();
995 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
1000 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1002 char* Passwd(userrec *user)
1004 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1006 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1014 bool IsDenied(userrec *user)
1016 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1018 if (match(user->host,i->host) && (i->type == CC_DENY))
1029 /* sends out an error notice to all connected clients (not to be used
1032 void send_error(char *s)
1034 log(DEBUG,"send_error: %s",s);
1035 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1037 if (isnick(i->second->nick))
1039 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1043 // fix - unregistered connections receive ERROR, not NOTICE
1044 Write(i->second->fd,"ERROR :%s",s);
1049 void Error(int status)
1051 signal (SIGALRM, SIG_IGN);
1052 signal (SIGPIPE, SIG_IGN);
1053 signal (SIGTERM, SIG_IGN);
1054 signal (SIGABRT, SIG_IGN);
1055 signal (SIGSEGV, SIG_IGN);
1056 signal (SIGURG, SIG_IGN);
1057 signal (SIGKILL, SIG_IGN);
1058 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1059 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1063 // this function counts all users connected, wether they are registered or NOT.
1066 return clientlist.size();
1069 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1070 int registered_usercount(void)
1073 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1075 if (i->second->registered == 7) c++;
1080 int usercount_invisible(void)
1083 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1085 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1090 int usercount_opers(void)
1093 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1095 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1100 int usercount_unknown(void)
1104 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1106 if ((i->second->fd > -1) && (i->second->registered != 7))
1112 long chancount(void)
1114 return chanlist.size();
1120 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1122 if ((isnick(i->second->nick)) && (i->second->fd > -1)) c++;
1127 void ShowMOTD(userrec *user)
1130 std::string WholeMOTD = "";
1133 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1136 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1137 WholeMOTD = WholeMOTD + buf;
1138 for (unsigned int i = 0; i != MOTD.size(); i++)
1140 snprintf(buf,65535,":%s 372 %s :- %s\r\n", Config->ServerName, user->nick, MOTD[i].c_str());
1141 WholeMOTD = WholeMOTD + buf;
1143 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1144 WholeMOTD = WholeMOTD + buf;
1145 // only one write operation
1146 user->AddWriteBuf(WholeMOTD);
1147 stats->statsSent += WholeMOTD.length();
1150 void ShowRULES(userrec *user)
1154 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1157 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1158 for (unsigned int i = 0; i != RULES.size(); i++)
1160 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1162 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1165 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1166 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1167 // registration timeout maximum seconds)
1168 bool AllModulesReportReady(userrec* user)
1170 for (int i = 0; i <= MODCOUNT; i++)
1172 int res = modules[i]->OnCheckReady(user);
1179 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1182 /* create the command and push it onto the table */
1183 strlcpy(comm.command,cmd,MAXBUF);
1184 strlcpy(comm.source,source,MAXBUF);
1185 comm.handler_function = f;
1186 comm.flags_needed = flags;
1187 comm.min_params = minparams;
1189 comm.total_bytes = 0;
1190 cmdlist.push_back(comm);
1191 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1195 void SetupCommandTable(void)
1197 createcommand("USER",handle_user,0,4,"<core>");
1198 createcommand("NICK",handle_nick,0,1,"<core>");
1199 createcommand("QUIT",handle_quit,0,0,"<core>");
1200 createcommand("VERSION",handle_version,0,0,"<core>");
1201 createcommand("PING",handle_ping,0,1,"<core>");
1202 createcommand("PONG",handle_pong,0,1,"<core>");
1203 createcommand("ADMIN",handle_admin,0,0,"<core>");
1204 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1205 createcommand("INFO",handle_info,0,0,"<core>");
1206 createcommand("TIME",handle_time,0,0,"<core>");
1207 createcommand("WHOIS",handle_whois,0,1,"<core>");
1208 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1209 createcommand("NOTICE",handle_notice,0,2,"<core>");
1210 createcommand("JOIN",handle_join,0,1,"<core>");
1211 createcommand("NAMES",handle_names,0,0,"<core>");
1212 createcommand("PART",handle_part,0,1,"<core>");
1213 createcommand("KICK",handle_kick,0,2,"<core>");
1214 createcommand("MODE",handle_mode,0,1,"<core>");
1215 createcommand("TOPIC",handle_topic,0,1,"<core>");
1216 createcommand("WHO",handle_who,0,1,"<core>");
1217 createcommand("MOTD",handle_motd,0,0,"<core>");
1218 createcommand("RULES",handle_rules,0,0,"<core>");
1219 createcommand("OPER",handle_oper,0,2,"<core>");
1220 createcommand("LIST",handle_list,0,0,"<core>");
1221 createcommand("DIE",handle_die,'o',1,"<core>");
1222 createcommand("RESTART",handle_restart,'o',1,"<core>");
1223 createcommand("KILL",handle_kill,'o',2,"<core>");
1224 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1225 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1226 createcommand("STATS",handle_stats,0,1,"<core>");
1227 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1228 createcommand("AWAY",handle_away,0,0,"<core>");
1229 createcommand("ISON",handle_ison,0,0,"<core>");
1230 createcommand("SUMMON",handle_summon,0,0,"<core>");
1231 createcommand("USERS",handle_users,0,0,"<core>");
1232 createcommand("INVITE",handle_invite,0,0,"<core>");
1233 createcommand("PASS",handle_pass,0,1,"<core>");
1234 createcommand("TRACE",handle_trace,'o',0,"<core>");
1235 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1236 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1237 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1238 createcommand("MODULES",handle_modules,0,0,"<core>");
1239 createcommand("LINKS",handle_links,0,0,"<core>");
1240 createcommand("MAP",handle_map,0,0,"<core>");
1241 createcommand("KLINE",handle_kline,'o',1,"<core>");
1242 createcommand("GLINE",handle_gline,'o',1,"<core>");
1243 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1244 createcommand("QLINE",handle_qline,'o',1,"<core>");
1245 createcommand("ELINE",handle_eline,'o',1,"<core>");
1246 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1247 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1248 createcommand("SERVER",handle_server,0,0,"<core>");
1249 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1252 bool DirValid(char* dirandfile)
1255 strlcpy(work,dirandfile,MAXBUF);
1256 int p = strlen(work);
1257 // we just want the dir
1258 while (strlen(work))
1267 char buffer[MAXBUF], otherdir[MAXBUF];
1268 // Get the current working directory
1269 if( getcwd( buffer, MAXBUF ) == NULL )
1272 if( getcwd( otherdir, MAXBUF ) == NULL )
1275 if (strlen(otherdir) >= strlen(work))
1277 otherdir[strlen(work)] = '\0';
1278 if (!strcmp(otherdir,work))
1287 std::string GetFullProgDir(char** argv, int argc)
1290 strlcpy(work,argv[0],MAXBUF);
1291 int p = strlen(work);
1292 // we just want the dir
1293 while (strlen(work))
1302 char buffer[MAXBUF], otherdir[MAXBUF];
1303 // Get the current working directory
1304 if( getcwd( buffer, MAXBUF ) == NULL )
1307 if( getcwd( otherdir, MAXBUF ) == NULL )