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 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 extern user_hash clientlist;
70 extern chan_hash chanlist;
71 extern command_table cmdlist;
72 extern file_cache MOTD;
73 extern file_cache RULES;
75 void log(int level,char *text, ...)
77 char textbuffer[MAXBUF];
81 if (level < Config->LogLevel)
85 timeinfo = localtime(&rawtime);
90 va_start (argsPtr, text);
91 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
93 strlcpy(b,asctime(timeinfo),MAXBUF);
94 b[24] = ':'; // we know this is the end of the time string
96 fprintf(Config->log_file,"%s %s\n",b,textbuffer);
99 // nofork enabled? display it on terminal too
100 printf("%s %s\n",b,textbuffer);
105 void readfile(file_cache &F, const char* fname)
108 char linebuf[MAXBUF];
110 log(DEBUG,"readfile: loading %s",fname);
112 file = fopen(fname,"r");
117 fgets(linebuf,sizeof(linebuf),file);
118 linebuf[strlen(linebuf)-1]='\0';
125 F.push_back(linebuf);
132 log(DEBUG,"readfile: failed to load file: %s",fname);
134 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
137 void Write(int sock,char *text, ...)
143 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
146 char textbuffer[MAXBUF];
150 va_start (argsPtr, text);
151 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
153 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
155 if (fd_ref_table[sock])
158 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
159 fd_ref_table[sock]->AddWriteBuf(tb);
160 stats->statsSent += bytes;
162 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
165 /* write a server formatted numeric response to a single socket */
167 void WriteServ(int sock, char* text, ...)
173 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
176 char textbuffer[MAXBUF],tb[MAXBUF];
178 va_start (argsPtr, text);
180 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
182 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
184 if (fd_ref_table[sock])
187 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
188 fd_ref_table[sock]->AddWriteBuf(tb);
189 stats->statsSent += bytes;
191 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
194 /* write text from an originating user to originating user */
196 void WriteFrom(int sock, userrec *user,char* text, ...)
200 if ((!text) || (!user))
202 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
205 char textbuffer[MAXBUF],tb[MAXBUF];
207 va_start (argsPtr, text);
209 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
211 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
213 if (fd_ref_table[sock])
216 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
217 fd_ref_table[sock]->AddWriteBuf(tb);
218 stats->statsSent += bytes;
220 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
223 /* write text to an destination user from a source user (e.g. user privmsg) */
225 void WriteTo(userrec *source, userrec *dest,char *data, ...)
227 if ((!dest) || (!data))
229 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
232 if (dest->fd == FD_MAGIC_NUMBER)
234 char textbuffer[MAXBUF],tb[MAXBUF];
236 va_start (argsPtr, data);
237 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
241 // if no source given send it from the server.
244 WriteServ(dest->fd,":%s %s",Config->ServerName,textbuffer);
248 WriteFrom(dest->fd,source,"%s",textbuffer);
252 /* write formatted text from a source user to all users on a channel
253 * including the sender (NOT for privmsg, notice etc!) */
255 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
257 if ((!Ptr) || (!user) || (!text))
259 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
262 char textbuffer[MAXBUF];
264 va_start (argsPtr, text);
265 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
268 std::vector<char*> *ulist = Ptr->GetUsers();
269 for (unsigned int j = 0; j < ulist->size(); j++)
271 char* o = (*ulist)[j];
272 userrec* otheruser = (userrec*)o;
273 if (otheruser->fd != FD_MAGIC_NUMBER)
274 WriteTo(user,otheruser,"%s",textbuffer);
278 /* write formatted text from a source user to all users on a channel
279 * including the sender (NOT for privmsg, notice etc!) doesnt send to
280 * users on remote servers */
282 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
284 if ((!Ptr) || (!text))
286 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
289 char textbuffer[MAXBUF];
291 va_start (argsPtr, text);
292 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
295 std::vector<char*> *ulist = Ptr->GetUsers();
296 for (unsigned int j = 0; j < ulist->size(); j++)
298 char* o = (*ulist)[j];
299 userrec* otheruser = (userrec*)o;
300 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
304 WriteServ(otheruser->fd,"%s",textbuffer);
308 WriteTo(user,otheruser,"%s",textbuffer);
314 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
316 if ((!Ptr) || (!text))
318 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
321 char textbuffer[MAXBUF];
323 va_start (argsPtr, text);
324 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
328 std::vector<char*> *ulist = Ptr->GetUsers();
329 for (unsigned int j = 0; j < ulist->size(); j++)
331 char* o = (*ulist)[j];
332 userrec* otheruser = (userrec*)o;
333 if (otheruser->fd != FD_MAGIC_NUMBER)
334 WriteServ(otheruser->fd,"%s",textbuffer);
338 /* write formatted text from a source user to all users on a channel except
339 * for the sender (for privmsg etc) */
341 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
343 if ((!Ptr) || (!user) || (!text))
345 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
348 char textbuffer[MAXBUF];
350 va_start (argsPtr, text);
351 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
354 std::vector<char*> *ulist = Ptr->GetUsers();
355 for (unsigned int j = 0; j < ulist->size(); j++)
357 char* o = (*ulist)[j];
358 userrec* otheruser = (userrec*)o;
359 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
360 WriteFrom(otheruser->fd,user,"%s",textbuffer);
364 std::string GetServerDescription(char* servername)
366 std::string description = "";
367 FOREACH_MOD OnGetServerDescription(servername,description);
368 if (description != "")
374 return Config->ServerDesc; // not a remote server that can be found, it must be me.
378 /* write a formatted string to all users who share at least one common
379 * channel, including the source user e.g. for use in NICK */
381 void WriteCommon(userrec *u, char* text, ...)
385 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
389 if (u->registered != 7) {
390 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
394 char textbuffer[MAXBUF];
396 va_start (argsPtr, text);
397 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
400 // FIX: Stops a message going to the same person more than once
401 memset(&already_sent,0,65536);
403 bool sent_to_at_least_one = false;
405 for (unsigned int i = 0; i < u->chans.size(); i++)
407 if (u->chans[i].channel)
409 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
410 for (unsigned int j = 0; j < ulist->size(); j++)
412 char* o = (*ulist)[j];
413 userrec* otheruser = (userrec*)o;
414 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
416 already_sent[otheruser->fd] = 1;
417 WriteFrom(otheruser->fd,u,"%s",textbuffer);
418 sent_to_at_least_one = true;
423 // if the user was not in any channels, no users will receive the text. Make sure the user
424 // receives their OWN message for WriteCommon
425 if (!sent_to_at_least_one)
427 WriteFrom(u->fd,u,"%s",textbuffer);
431 /* write a formatted string to all users who share at least one common
432 * channel, NOT including the source user e.g. for use in QUIT */
434 void WriteCommonExcept(userrec *u, char* text, ...)
438 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
442 if (u->registered != 7) {
443 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
447 char textbuffer[MAXBUF];
449 va_start (argsPtr, text);
450 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
453 memset(&already_sent,0,65536);
455 for (unsigned int i = 0; i < u->chans.size(); i++)
457 if (u->chans[i].channel)
459 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
460 for (unsigned int j = 0; j < ulist->size(); j++)
462 char* o = (*ulist)[j];
463 userrec* otheruser = (userrec*)o;
466 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
468 already_sent[otheruser->fd] = 1;
469 WriteFrom(otheruser->fd,u,"%s",textbuffer);
477 void WriteOpers(char* text, ...)
481 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
485 char textbuffer[MAXBUF];
487 va_start (argsPtr, text);
488 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
491 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
494 if ((a) && (a->fd != FD_MAGIC_NUMBER))
496 if (strchr(a->modes,'s'))
498 // send server notices to all with +s
499 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
505 void ServerNoticeAll(char* text, ...)
510 char textbuffer[MAXBUF];
512 va_start (argsPtr, text);
513 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
516 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
518 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
520 WriteServ(i->second->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
525 void ServerPrivmsgAll(char* text, ...)
530 char textbuffer[MAXBUF];
532 va_start (argsPtr, text);
533 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
536 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
538 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
540 WriteServ(i->second->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
545 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
547 if ((!text) || (!source))
549 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
553 char textbuffer[MAXBUF];
555 va_start (argsPtr, text);
556 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
559 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
562 if ((a) && (a->fd != FD_MAGIC_NUMBER))
564 if (strchr(a->modes,'s'))
566 // send server notices to all with +s
567 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
573 // returns TRUE of any users on channel C occupy server 'servername'.
575 bool ChanAnyOnThisServer(chanrec *c,char* servername)
577 log(DEBUG,"ChanAnyOnThisServer");
579 std::vector<char*> *ulist = c->GetUsers();
580 for (unsigned int j = 0; j < ulist->size(); j++)
582 char* o = (*ulist)[j];
583 userrec* user = (userrec*)o;
584 if (!strcasecmp(user->server,servername))
590 // returns true if user 'u' shares any common channels with any users on server 'servername'
592 bool CommonOnThisServer(userrec* u,const char* servername)
594 log(DEBUG,"ChanAnyOnThisServer");
596 for (unsigned int i = 0; i < u->chans.size(); i++)
598 if (u->chans[i].channel)
600 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
601 for (unsigned int j = 0; j < ulist->size(); j++)
603 char* o = (*ulist)[j];
604 userrec* user = (userrec*)o;
605 if (!strcasecmp(user->server,servername))
614 void WriteMode(const char* modes, int flags, const char* text, ...)
616 if ((!text) || (!modes) || (!flags))
618 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
622 char textbuffer[MAXBUF];
624 va_start (argsPtr, text);
625 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
627 int modelen = strlen(modes);
629 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
631 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
633 bool send_to_user = false;
638 for (int n = 0; n < modelen; n++)
640 if (!hasumode(i->second,modes[n]))
642 send_to_user = false;
647 else if (flags == WM_OR)
649 send_to_user = false;
650 for (int n = 0; n < modelen; n++)
652 if (hasumode(i->second,modes[n]))
662 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
668 void NoticeAll(userrec *source, bool local_only, char* text, ...)
670 if ((!text) || (!source))
672 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
676 char textbuffer[MAXBUF];
678 va_start (argsPtr, text);
679 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
682 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
684 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
686 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
693 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
695 if ((!text) || (!source))
697 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
701 char textbuffer[MAXBUF];
703 va_start (argsPtr, text);
704 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
707 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
709 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
711 if (strchr(i->second->modes,'w'))
713 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
719 /* convert a string to lowercase. Note following special circumstances
720 * taken from RFC 1459. Many "official" server branches still hold to this
721 * rule so i will too;
723 * Because of IRC's scandanavian origin, the characters {}| are
724 * considered to be the lower case equivalents of the characters []\,
725 * respectively. This is a critical issue when determining the
726 * equivalence of two nicknames.
729 void strlower(char *n)
733 for (char* t = n; *t; t++)
734 *t = lowermap[(unsigned)*t];
738 /* Find a user record by nickname and return a pointer to it */
740 userrec* Find(std::string nick)
742 user_hash::iterator iter = clientlist.find(nick);
744 if (iter == clientlist.end())
745 /* Couldn't find it */
751 /* find a channel record by channel name and return a pointer to it */
753 chanrec* FindChan(const char* chan)
757 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
761 chan_hash::iterator iter = chanlist.find(chan);
763 if (iter == chanlist.end())
764 /* Couldn't find it */
771 long GetMaxBans(char* name)
774 for (int count = 0; count < ConfValueEnum("banlist",&Config->config_f); count++)
776 ConfValue("banlist","chan",count,CM,&Config->config_f);
779 ConfValue("banlist","limit",count,CM,&Config->config_f);
786 void purge_empty_chans(userrec* u)
789 int go_again = 1, purge = 0;
791 // firstly decrement the count on each channel
792 for (unsigned int f = 0; f < u->chans.size(); f++)
794 if (u->chans[f].channel)
796 u->chans[f].channel->DelUser((char*)u);
800 for (unsigned int i = 0; i < u->chans.size(); i++)
802 if (u->chans[i].channel)
804 if (!usercount(u->chans[i].channel))
806 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
807 /* kill the record */
808 if (i2 != chanlist.end())
810 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
816 u->chans[i].channel = NULL;
821 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
825 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
831 char scratch[MAXBUF];
834 char* chanmodes(chanrec *chan)
838 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
845 if (chan->binarymodes & CM_NOEXTERNAL)
847 strlcat(scratch,"n",MAXMODES);
849 if (chan->binarymodes & CM_TOPICLOCK)
851 strlcat(scratch,"t",MAXMODES);
855 strlcat(scratch,"k",MAXMODES);
859 strlcat(scratch,"l",MAXMODES);
861 if (chan->binarymodes & CM_INVITEONLY)
863 strlcat(scratch,"i",MAXMODES);
865 if (chan->binarymodes & CM_MODERATED)
867 strlcat(scratch,"m",MAXMODES);
869 if (chan->binarymodes & CM_SECRET)
871 strlcat(scratch,"s",MAXMODES);
873 if (chan->binarymodes & CM_PRIVATE)
875 strlcat(scratch,"p",MAXMODES);
879 strlcat(sparam," ",MAXBUF);
880 strlcat(sparam,chan->key,MAXBUF);
885 sprintf(foo," %lu",(unsigned long)chan->limit);
886 strlcat(sparam,foo,MAXBUF);
888 if (*chan->custom_modes)
890 strlcat(scratch,chan->custom_modes,MAXMODES);
891 for (int z = 0; chan->custom_modes[z] != 0; z++)
893 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
896 strlcat(sparam," ",MAXBUF);
897 strlcat(sparam,extparam.c_str(),MAXBUF);
901 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
902 strlcat(scratch,sparam,MAXMODES);
907 /* compile a userlist of a channel into a string, each nick seperated by
908 * spaces and op, voice etc status shown as @ and + */
910 void userlist(userrec *user,chanrec *c)
914 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
918 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
920 std::vector<char*> *ulist = c->GetUsers();
921 for (unsigned int i = 0; i < ulist->size(); i++)
923 char* o = (*ulist)[i];
924 userrec* otheruser = (userrec*)o;
925 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
927 /* user is +i, and source not on the channel, does not show
928 * nick in NAMES list */
931 strlcat(list,cmode(otheruser,c),MAXBUF);
932 strlcat(list,otheruser->nick,MAXBUF);
933 strlcat(list," ",MAXBUF);
934 if (strlen(list)>(480-NICKMAX))
936 /* list overflowed into
937 * multiple numerics */
938 WriteServ(user->fd,"%s",list);
939 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
942 /* if whats left in the list isnt empty, send it */
943 if (list[strlen(list)-1] != ':')
945 WriteServ(user->fd,"%s",list);
949 /* return a count of the users on a specific channel accounting for
950 * invisible users who won't increase the count. e.g. for /LIST */
952 int usercount_i(chanrec *c)
958 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
962 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
966 if (has_channel(i->second,c))
968 if (isnick(i->second->nick))
970 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
972 /* user is +i, and source not on the channel, does not show
973 * nick in NAMES list */
981 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
986 int usercount(chanrec *c)
990 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
993 int count = c->GetUserCounter();
994 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
999 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1001 char* Passwd(userrec *user)
1003 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1005 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1013 bool IsDenied(userrec *user)
1015 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1017 if (match(user->host,i->host) && (i->type == CC_DENY))
1028 /* sends out an error notice to all connected clients (not to be used
1031 void send_error(char *s)
1033 log(DEBUG,"send_error: %s",s);
1034 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1036 if (isnick(i->second->nick))
1038 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1042 // fix - unregistered connections receive ERROR, not NOTICE
1043 Write(i->second->fd,"ERROR :%s",s);
1048 void Error(int status)
1050 signal (SIGALRM, SIG_IGN);
1051 signal (SIGPIPE, SIG_IGN);
1052 signal (SIGTERM, SIG_IGN);
1053 signal (SIGABRT, SIG_IGN);
1054 signal (SIGSEGV, SIG_IGN);
1055 signal (SIGURG, SIG_IGN);
1056 signal (SIGKILL, SIG_IGN);
1057 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1058 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1062 // this function counts all users connected, wether they are registered or NOT.
1065 return clientlist.size();
1068 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1069 int registered_usercount(void)
1072 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1074 if (i->second->registered == 7) c++;
1079 int usercount_invisible(void)
1082 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1084 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1089 int usercount_opers(void)
1092 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1094 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1099 int usercount_unknown(void)
1103 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1105 if ((i->second->fd > -1) && (i->second->registered != 7))
1111 long chancount(void)
1113 return chanlist.size();
1119 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1121 if ((isnick(i->second->nick)) && (i->second->fd > -1)) c++;
1126 void ShowMOTD(userrec *user)
1129 std::string WholeMOTD = "";
1132 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1135 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1136 WholeMOTD = WholeMOTD + buf;
1137 for (unsigned int i = 0; i != MOTD.size(); i++)
1139 snprintf(buf,65535,":%s 372 %s :- %s\r\n", Config->ServerName, user->nick, MOTD[i].c_str());
1140 WholeMOTD = WholeMOTD + buf;
1142 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1143 WholeMOTD = WholeMOTD + buf;
1144 // only one write operation
1145 user->AddWriteBuf(WholeMOTD);
1146 stats->statsSent += WholeMOTD.length();
1149 void ShowRULES(userrec *user)
1153 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1156 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1157 for (unsigned int i = 0; i != RULES.size(); i++)
1159 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1161 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1164 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1165 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1166 // registration timeout maximum seconds)
1167 bool AllModulesReportReady(userrec* user)
1169 for (int i = 0; i <= MODCOUNT; i++)
1171 int res = modules[i]->OnCheckReady(user);
1178 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1181 /* create the command and push it onto the table */
1182 strlcpy(comm.command,cmd,MAXBUF);
1183 strlcpy(comm.source,source,MAXBUF);
1184 comm.handler_function = f;
1185 comm.flags_needed = flags;
1186 comm.min_params = minparams;
1188 comm.total_bytes = 0;
1189 cmdlist.push_back(comm);
1190 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1194 void SetupCommandTable(void)
1196 createcommand("USER",handle_user,0,4,"<core>");
1197 createcommand("NICK",handle_nick,0,1,"<core>");
1198 createcommand("QUIT",handle_quit,0,0,"<core>");
1199 createcommand("VERSION",handle_version,0,0,"<core>");
1200 createcommand("PING",handle_ping,0,1,"<core>");
1201 createcommand("PONG",handle_pong,0,1,"<core>");
1202 createcommand("ADMIN",handle_admin,0,0,"<core>");
1203 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1204 createcommand("INFO",handle_info,0,0,"<core>");
1205 createcommand("TIME",handle_time,0,0,"<core>");
1206 createcommand("WHOIS",handle_whois,0,1,"<core>");
1207 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1208 createcommand("NOTICE",handle_notice,0,2,"<core>");
1209 createcommand("JOIN",handle_join,0,1,"<core>");
1210 createcommand("NAMES",handle_names,0,0,"<core>");
1211 createcommand("PART",handle_part,0,1,"<core>");
1212 createcommand("KICK",handle_kick,0,2,"<core>");
1213 createcommand("MODE",handle_mode,0,1,"<core>");
1214 createcommand("TOPIC",handle_topic,0,1,"<core>");
1215 createcommand("WHO",handle_who,0,1,"<core>");
1216 createcommand("MOTD",handle_motd,0,0,"<core>");
1217 createcommand("RULES",handle_rules,0,0,"<core>");
1218 createcommand("OPER",handle_oper,0,2,"<core>");
1219 createcommand("LIST",handle_list,0,0,"<core>");
1220 createcommand("DIE",handle_die,'o',1,"<core>");
1221 createcommand("RESTART",handle_restart,'o',1,"<core>");
1222 createcommand("KILL",handle_kill,'o',2,"<core>");
1223 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1224 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1225 createcommand("STATS",handle_stats,0,1,"<core>");
1226 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1227 createcommand("AWAY",handle_away,0,0,"<core>");
1228 createcommand("ISON",handle_ison,0,0,"<core>");
1229 createcommand("SUMMON",handle_summon,0,0,"<core>");
1230 createcommand("USERS",handle_users,0,0,"<core>");
1231 createcommand("INVITE",handle_invite,0,0,"<core>");
1232 createcommand("PASS",handle_pass,0,1,"<core>");
1233 createcommand("TRACE",handle_trace,'o',0,"<core>");
1234 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1235 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1236 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1237 createcommand("MODULES",handle_modules,0,0,"<core>");
1238 createcommand("LINKS",handle_links,0,0,"<core>");
1239 createcommand("MAP",handle_map,0,0,"<core>");
1240 createcommand("KLINE",handle_kline,'o',1,"<core>");
1241 createcommand("GLINE",handle_gline,'o',1,"<core>");
1242 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1243 createcommand("QLINE",handle_qline,'o',1,"<core>");
1244 createcommand("ELINE",handle_eline,'o',1,"<core>");
1245 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1246 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1247 createcommand("SERVER",handle_server,0,0,"<core>");
1248 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1251 bool DirValid(char* dirandfile)
1254 strlcpy(work,dirandfile,MAXBUF);
1255 int p = strlen(work);
1256 // we just want the dir
1257 while (strlen(work))
1266 char buffer[MAXBUF], otherdir[MAXBUF];
1267 // Get the current working directory
1268 if( getcwd( buffer, MAXBUF ) == NULL )
1271 if( getcwd( otherdir, MAXBUF ) == NULL )
1274 if (strlen(otherdir) >= strlen(work))
1276 otherdir[strlen(work)] = '\0';
1277 if (!strcmp(otherdir,work))
1286 std::string GetFullProgDir(char** argv, int argc)
1289 strlcpy(work,argv[0],MAXBUF);
1290 int p = strlen(work);
1291 // we just want the dir
1292 while (strlen(work))
1301 char buffer[MAXBUF], otherdir[MAXBUF];
1302 // Get the current working directory
1303 if( getcwd( buffer, MAXBUF ) == NULL )
1306 if( getcwd( otherdir, MAXBUF ) == NULL )