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 user_hash clientlist;
64 extern chan_hash chanlist;
65 extern command_table cmdlist;
67 void log(int level,char *text, ...)
69 char textbuffer[MAXBUF];
73 if (level < Config->LogLevel)
77 timeinfo = localtime(&rawtime);
82 va_start (argsPtr, text);
83 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
85 strlcpy(b,asctime(timeinfo),MAXBUF);
86 b[24] = ':'; // we know this is the end of the time string
88 fprintf(Config->log_file,"%s %s\n",b,textbuffer);
91 // nofork enabled? display it on terminal too
92 printf("%s %s\n",b,textbuffer);
97 void readfile(file_cache &F, const char* fname)
100 char linebuf[MAXBUF];
102 log(DEBUG,"readfile: loading %s",fname);
104 file = fopen(fname,"r");
109 fgets(linebuf,sizeof(linebuf),file);
110 linebuf[strlen(linebuf)-1]='\0';
117 F.push_back(linebuf);
124 log(DEBUG,"readfile: failed to load file: %s",fname);
126 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
129 void Write(int sock,char *text, ...)
135 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
138 char textbuffer[MAXBUF];
142 va_start (argsPtr, text);
143 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
145 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
147 if (fd_ref_table[sock])
150 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
151 fd_ref_table[sock]->AddWriteBuf(tb);
152 stats->statsSent += bytes;
154 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
157 /* write a server formatted numeric response to a single socket */
159 void WriteServ(int sock, char* text, ...)
165 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
168 char textbuffer[MAXBUF],tb[MAXBUF];
170 va_start (argsPtr, text);
172 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
174 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",Config->ServerName,textbuffer);
176 if (fd_ref_table[sock])
179 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
180 fd_ref_table[sock]->AddWriteBuf(tb);
181 stats->statsSent += bytes;
183 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
186 /* write text from an originating user to originating user */
188 void WriteFrom(int sock, userrec *user,char* text, ...)
192 if ((!text) || (!user))
194 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
197 char textbuffer[MAXBUF],tb[MAXBUF];
199 va_start (argsPtr, text);
201 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
203 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
205 if (fd_ref_table[sock])
208 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
209 fd_ref_table[sock]->AddWriteBuf(tb);
210 stats->statsSent += bytes;
212 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
215 /* write text to an destination user from a source user (e.g. user privmsg) */
217 void WriteTo(userrec *source, userrec *dest,char *data, ...)
219 if ((!dest) || (!data))
221 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
224 if (dest->fd == FD_MAGIC_NUMBER)
226 char textbuffer[MAXBUF],tb[MAXBUF];
228 va_start (argsPtr, data);
229 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
233 // if no source given send it from the server.
236 WriteServ(dest->fd,":%s %s",Config->ServerName,textbuffer);
240 WriteFrom(dest->fd,source,"%s",textbuffer);
244 /* write formatted text from a source user to all users on a channel
245 * including the sender (NOT for privmsg, notice etc!) */
247 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
249 if ((!Ptr) || (!user) || (!text))
251 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
254 char textbuffer[MAXBUF];
256 va_start (argsPtr, text);
257 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
260 std::vector<char*> *ulist = Ptr->GetUsers();
261 for (unsigned int j = 0; j < ulist->size(); j++)
263 char* o = (*ulist)[j];
264 userrec* otheruser = (userrec*)o;
265 if (otheruser->fd != FD_MAGIC_NUMBER)
266 WriteTo(user,otheruser,"%s",textbuffer);
270 /* write formatted text from a source user to all users on a channel
271 * including the sender (NOT for privmsg, notice etc!) doesnt send to
272 * users on remote servers */
274 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
276 if ((!Ptr) || (!text))
278 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
281 char textbuffer[MAXBUF];
283 va_start (argsPtr, text);
284 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
287 std::vector<char*> *ulist = Ptr->GetUsers();
288 for (unsigned int j = 0; j < ulist->size(); j++)
290 char* o = (*ulist)[j];
291 userrec* otheruser = (userrec*)o;
292 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
296 WriteServ(otheruser->fd,"%s",textbuffer);
300 WriteTo(user,otheruser,"%s",textbuffer);
306 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
308 if ((!Ptr) || (!text))
310 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
313 char textbuffer[MAXBUF];
315 va_start (argsPtr, text);
316 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
320 std::vector<char*> *ulist = Ptr->GetUsers();
321 for (unsigned int j = 0; j < ulist->size(); j++)
323 char* o = (*ulist)[j];
324 userrec* otheruser = (userrec*)o;
325 if (otheruser->fd != FD_MAGIC_NUMBER)
326 WriteServ(otheruser->fd,"%s",textbuffer);
330 /* write formatted text from a source user to all users on a channel except
331 * for the sender (for privmsg etc) */
333 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
335 if ((!Ptr) || (!user) || (!text))
337 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
340 char textbuffer[MAXBUF];
342 va_start (argsPtr, text);
343 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
346 std::vector<char*> *ulist = Ptr->GetUsers();
347 for (unsigned int j = 0; j < ulist->size(); j++)
349 char* o = (*ulist)[j];
350 userrec* otheruser = (userrec*)o;
351 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
352 WriteFrom(otheruser->fd,user,"%s",textbuffer);
356 std::string GetServerDescription(char* servername)
358 std::string description = "";
359 FOREACH_MOD OnGetServerDescription(servername,description);
360 if (description != "")
366 return Config->ServerDesc; // not a remote server that can be found, it must be me.
370 /* write a formatted string to all users who share at least one common
371 * channel, including the source user e.g. for use in NICK */
373 void WriteCommon(userrec *u, char* text, ...)
377 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
381 if (u->registered != 7) {
382 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
386 char textbuffer[MAXBUF];
388 va_start (argsPtr, text);
389 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
392 // FIX: Stops a message going to the same person more than once
393 memset(&already_sent,0,65536);
395 bool sent_to_at_least_one = false;
397 for (unsigned int i = 0; i < u->chans.size(); i++)
399 if (u->chans[i].channel)
401 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
402 for (unsigned int j = 0; j < ulist->size(); j++)
404 char* o = (*ulist)[j];
405 userrec* otheruser = (userrec*)o;
406 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
408 already_sent[otheruser->fd] = 1;
409 WriteFrom(otheruser->fd,u,"%s",textbuffer);
410 sent_to_at_least_one = true;
415 // if the user was not in any channels, no users will receive the text. Make sure the user
416 // receives their OWN message for WriteCommon
417 if (!sent_to_at_least_one)
419 WriteFrom(u->fd,u,"%s",textbuffer);
423 /* write a formatted string to all users who share at least one common
424 * channel, NOT including the source user e.g. for use in QUIT */
426 void WriteCommonExcept(userrec *u, char* text, ...)
430 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
434 if (u->registered != 7) {
435 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
439 char textbuffer[MAXBUF];
441 va_start (argsPtr, text);
442 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
445 memset(&already_sent,0,65536);
447 for (unsigned int i = 0; i < u->chans.size(); i++)
449 if (u->chans[i].channel)
451 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
452 for (unsigned int j = 0; j < ulist->size(); j++)
454 char* o = (*ulist)[j];
455 userrec* otheruser = (userrec*)o;
458 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
460 already_sent[otheruser->fd] = 1;
461 WriteFrom(otheruser->fd,u,"%s",textbuffer);
469 void WriteOpers(char* text, ...)
473 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
477 char textbuffer[MAXBUF];
479 va_start (argsPtr, text);
480 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
483 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
486 if ((a) && (a->fd != FD_MAGIC_NUMBER))
488 if (strchr(a->modes,'s'))
490 // send server notices to all with +s
491 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
497 void ServerNoticeAll(char* text, ...)
502 char textbuffer[MAXBUF];
504 va_start (argsPtr, text);
505 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
508 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
510 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
512 WriteServ(i->second->fd,"NOTICE $%s :%s",Config->ServerName,textbuffer);
517 void ServerPrivmsgAll(char* text, ...)
522 char textbuffer[MAXBUF];
524 va_start (argsPtr, text);
525 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
528 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
530 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
532 WriteServ(i->second->fd,"PRIVMSG $%s :%s",Config->ServerName,textbuffer);
537 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
539 if ((!text) || (!source))
541 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
545 char textbuffer[MAXBUF];
547 va_start (argsPtr, text);
548 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
551 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
554 if ((a) && (a->fd != FD_MAGIC_NUMBER))
556 if (strchr(a->modes,'s'))
558 // send server notices to all with +s
559 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
565 // returns TRUE of any users on channel C occupy server 'servername'.
567 bool ChanAnyOnThisServer(chanrec *c,char* servername)
569 log(DEBUG,"ChanAnyOnThisServer");
571 std::vector<char*> *ulist = c->GetUsers();
572 for (unsigned int j = 0; j < ulist->size(); j++)
574 char* o = (*ulist)[j];
575 userrec* user = (userrec*)o;
576 if (!strcasecmp(user->server,servername))
582 // returns true if user 'u' shares any common channels with any users on server 'servername'
584 bool CommonOnThisServer(userrec* u,const char* servername)
586 log(DEBUG,"ChanAnyOnThisServer");
588 for (unsigned int i = 0; i < u->chans.size(); i++)
590 if (u->chans[i].channel)
592 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
593 for (unsigned int j = 0; j < ulist->size(); j++)
595 char* o = (*ulist)[j];
596 userrec* user = (userrec*)o;
597 if (!strcasecmp(user->server,servername))
606 void WriteMode(const char* modes, int flags, const char* text, ...)
608 if ((!text) || (!modes) || (!flags))
610 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
614 char textbuffer[MAXBUF];
616 va_start (argsPtr, text);
617 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
619 int modelen = strlen(modes);
621 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
623 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
625 bool send_to_user = false;
630 for (int n = 0; n < modelen; n++)
632 if (!hasumode(i->second,modes[n]))
634 send_to_user = false;
639 else if (flags == WM_OR)
641 send_to_user = false;
642 for (int n = 0; n < modelen; n++)
644 if (hasumode(i->second,modes[n]))
654 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
660 void NoticeAll(userrec *source, bool local_only, char* text, ...)
662 if ((!text) || (!source))
664 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
668 char textbuffer[MAXBUF];
670 va_start (argsPtr, text);
671 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
674 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
676 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
678 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
685 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
687 if ((!text) || (!source))
689 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
693 char textbuffer[MAXBUF];
695 va_start (argsPtr, text);
696 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
699 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
701 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
703 if (strchr(i->second->modes,'w'))
705 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
711 /* convert a string to lowercase. Note following special circumstances
712 * taken from RFC 1459. Many "official" server branches still hold to this
713 * rule so i will too;
715 * Because of IRC's scandanavian origin, the characters {}| are
716 * considered to be the lower case equivalents of the characters []\,
717 * respectively. This is a critical issue when determining the
718 * equivalence of two nicknames.
721 void strlower(char *n)
725 for (char* t = n; *t; t++)
726 *t = lowermap[(unsigned)*t];
730 /* Find a user record by nickname and return a pointer to it */
732 userrec* Find(std::string nick)
734 user_hash::iterator iter = clientlist.find(nick);
736 if (iter == clientlist.end())
737 /* Couldn't find it */
743 /* find a channel record by channel name and return a pointer to it */
745 chanrec* FindChan(const char* chan)
749 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
753 chan_hash::iterator iter = chanlist.find(chan);
755 if (iter == chanlist.end())
756 /* Couldn't find it */
763 long GetMaxBans(char* name)
766 for (int count = 0; count < Config->ConfValueEnum("banlist",&Config->config_f); count++)
768 Config->ConfValue("banlist","chan",count,CM,&Config->config_f);
771 Config->ConfValue("banlist","limit",count,CM,&Config->config_f);
778 void purge_empty_chans(userrec* u)
781 int go_again = 1, purge = 0;
783 // firstly decrement the count on each channel
784 for (unsigned int f = 0; f < u->chans.size(); f++)
786 if (u->chans[f].channel)
788 u->chans[f].channel->DelUser((char*)u);
792 for (unsigned int i = 0; i < u->chans.size(); i++)
794 if (u->chans[i].channel)
796 if (!usercount(u->chans[i].channel))
798 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
799 /* kill the record */
800 if (i2 != chanlist.end())
802 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
808 u->chans[i].channel = NULL;
813 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
817 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
823 char scratch[MAXBUF];
826 char* chanmodes(chanrec *chan)
830 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
837 if (chan->binarymodes & CM_NOEXTERNAL)
839 strlcat(scratch,"n",MAXMODES);
841 if (chan->binarymodes & CM_TOPICLOCK)
843 strlcat(scratch,"t",MAXMODES);
847 strlcat(scratch,"k",MAXMODES);
851 strlcat(scratch,"l",MAXMODES);
853 if (chan->binarymodes & CM_INVITEONLY)
855 strlcat(scratch,"i",MAXMODES);
857 if (chan->binarymodes & CM_MODERATED)
859 strlcat(scratch,"m",MAXMODES);
861 if (chan->binarymodes & CM_SECRET)
863 strlcat(scratch,"s",MAXMODES);
865 if (chan->binarymodes & CM_PRIVATE)
867 strlcat(scratch,"p",MAXMODES);
871 strlcat(sparam," ",MAXBUF);
872 strlcat(sparam,chan->key,MAXBUF);
877 sprintf(foo," %lu",(unsigned long)chan->limit);
878 strlcat(sparam,foo,MAXBUF);
880 if (*chan->custom_modes)
882 strlcat(scratch,chan->custom_modes,MAXMODES);
883 for (int z = 0; chan->custom_modes[z] != 0; z++)
885 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
888 strlcat(sparam," ",MAXBUF);
889 strlcat(sparam,extparam.c_str(),MAXBUF);
893 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
894 strlcat(scratch,sparam,MAXMODES);
899 /* compile a userlist of a channel into a string, each nick seperated by
900 * spaces and op, voice etc status shown as @ and + */
902 void userlist(userrec *user,chanrec *c)
906 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
910 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
912 std::vector<char*> *ulist = c->GetUsers();
913 for (unsigned int i = 0; i < ulist->size(); i++)
915 char* o = (*ulist)[i];
916 userrec* otheruser = (userrec*)o;
917 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
919 /* user is +i, and source not on the channel, does not show
920 * nick in NAMES list */
923 strlcat(list,cmode(otheruser,c),MAXBUF);
924 strlcat(list,otheruser->nick,MAXBUF);
925 strlcat(list," ",MAXBUF);
926 if (strlen(list)>(480-NICKMAX))
928 /* list overflowed into
929 * multiple numerics */
930 WriteServ(user->fd,"%s",list);
931 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
934 /* if whats left in the list isnt empty, send it */
935 if (list[strlen(list)-1] != ':')
937 WriteServ(user->fd,"%s",list);
941 /* return a count of the users on a specific channel accounting for
942 * invisible users who won't increase the count. e.g. for /LIST */
944 int usercount_i(chanrec *c)
950 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
954 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
958 if (has_channel(i->second,c))
960 if (isnick(i->second->nick))
962 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
964 /* user is +i, and source not on the channel, does not show
965 * nick in NAMES list */
973 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
978 int usercount(chanrec *c)
982 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
985 int count = c->GetUserCounter();
986 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
991 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
993 char* Passwd(userrec *user)
995 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
997 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1005 bool IsDenied(userrec *user)
1007 for (ClassVector::iterator i = Config->Classes.begin(); i != Config->Classes.end(); i++)
1009 if (match(user->host,i->host) && (i->type == CC_DENY))
1020 /* sends out an error notice to all connected clients (not to be used
1023 void send_error(char *s)
1025 log(DEBUG,"send_error: %s",s);
1026 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1028 if (isnick(i->second->nick))
1030 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1034 // fix - unregistered connections receive ERROR, not NOTICE
1035 Write(i->second->fd,"ERROR :%s",s);
1040 void Error(int status)
1042 signal (SIGALRM, SIG_IGN);
1043 signal (SIGPIPE, SIG_IGN);
1044 signal (SIGTERM, SIG_IGN);
1045 signal (SIGABRT, SIG_IGN);
1046 signal (SIGSEGV, SIG_IGN);
1047 signal (SIGURG, SIG_IGN);
1048 signal (SIGKILL, SIG_IGN);
1049 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1050 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1054 // this function counts all users connected, wether they are registered or NOT.
1057 return clientlist.size();
1060 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1061 int registered_usercount(void)
1064 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1066 if (i->second->registered == 7) c++;
1071 int usercount_invisible(void)
1074 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1076 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1081 int usercount_opers(void)
1084 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1086 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1091 int usercount_unknown(void)
1095 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1097 if ((i->second->fd > -1) && (i->second->registered != 7))
1103 long chancount(void)
1105 return chanlist.size();
1111 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1113 if ((isnick(i->second->nick)) && (i->second->fd > -1)) c++;
1118 void ShowMOTD(userrec *user)
1121 std::string WholeMOTD = "";
1122 if (!Config->MOTD.size())
1124 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1127 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", Config->ServerName, user->nick, Config->ServerName);
1128 WholeMOTD = WholeMOTD + buf;
1129 for (unsigned int i = 0; i < Config->MOTD.size(); i++)
1131 snprintf(buf,65535,":%s 372 %s :- %s\r\n", Config->ServerName, user->nick, Config->MOTD[i].c_str());
1132 WholeMOTD = WholeMOTD + buf;
1134 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", Config->ServerName, user->nick);
1135 WholeMOTD = WholeMOTD + buf;
1136 // only one write operation
1137 user->AddWriteBuf(WholeMOTD);
1138 stats->statsSent += WholeMOTD.length();
1141 void ShowRULES(userrec *user)
1143 if (!Config->RULES.size())
1145 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1148 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,Config->ServerName);
1149 for (unsigned int i = 0; i < Config->RULES.size(); i++)
1151 WriteServ(user->fd,"NOTICE %s :%s",user->nick,Config->RULES[i].c_str());
1153 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,Config->ServerName);
1156 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1157 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1158 // registration timeout maximum seconds)
1159 bool AllModulesReportReady(userrec* user)
1161 for (int i = 0; i <= MODCOUNT; i++)
1163 int res = modules[i]->OnCheckReady(user);
1170 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1173 /* create the command and push it onto the table */
1174 strlcpy(comm.command,cmd,MAXBUF);
1175 strlcpy(comm.source,source,MAXBUF);
1176 comm.handler_function = f;
1177 comm.flags_needed = flags;
1178 comm.min_params = minparams;
1180 comm.total_bytes = 0;
1181 cmdlist.push_back(comm);
1182 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1186 void SetupCommandTable(void)
1188 createcommand("USER",handle_user,0,4,"<core>");
1189 createcommand("NICK",handle_nick,0,1,"<core>");
1190 createcommand("QUIT",handle_quit,0,0,"<core>");
1191 createcommand("VERSION",handle_version,0,0,"<core>");
1192 createcommand("PING",handle_ping,0,1,"<core>");
1193 createcommand("PONG",handle_pong,0,1,"<core>");
1194 createcommand("ADMIN",handle_admin,0,0,"<core>");
1195 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1196 createcommand("INFO",handle_info,0,0,"<core>");
1197 createcommand("TIME",handle_time,0,0,"<core>");
1198 createcommand("WHOIS",handle_whois,0,1,"<core>");
1199 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1200 createcommand("NOTICE",handle_notice,0,2,"<core>");
1201 createcommand("JOIN",handle_join,0,1,"<core>");
1202 createcommand("NAMES",handle_names,0,0,"<core>");
1203 createcommand("PART",handle_part,0,1,"<core>");
1204 createcommand("KICK",handle_kick,0,2,"<core>");
1205 createcommand("MODE",handle_mode,0,1,"<core>");
1206 createcommand("TOPIC",handle_topic,0,1,"<core>");
1207 createcommand("WHO",handle_who,0,1,"<core>");
1208 createcommand("MOTD",handle_motd,0,0,"<core>");
1209 createcommand("RULES",handle_rules,0,0,"<core>");
1210 createcommand("OPER",handle_oper,0,2,"<core>");
1211 createcommand("LIST",handle_list,0,0,"<core>");
1212 createcommand("DIE",handle_die,'o',1,"<core>");
1213 createcommand("RESTART",handle_restart,'o',1,"<core>");
1214 createcommand("KILL",handle_kill,'o',2,"<core>");
1215 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1216 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1217 createcommand("STATS",handle_stats,0,1,"<core>");
1218 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1219 createcommand("AWAY",handle_away,0,0,"<core>");
1220 createcommand("ISON",handle_ison,0,0,"<core>");
1221 createcommand("SUMMON",handle_summon,0,0,"<core>");
1222 createcommand("USERS",handle_users,0,0,"<core>");
1223 createcommand("INVITE",handle_invite,0,0,"<core>");
1224 createcommand("PASS",handle_pass,0,1,"<core>");
1225 createcommand("TRACE",handle_trace,'o',0,"<core>");
1226 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1227 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1228 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1229 createcommand("MODULES",handle_modules,0,0,"<core>");
1230 createcommand("LINKS",handle_links,0,0,"<core>");
1231 createcommand("MAP",handle_map,0,0,"<core>");
1232 createcommand("KLINE",handle_kline,'o',1,"<core>");
1233 createcommand("GLINE",handle_gline,'o',1,"<core>");
1234 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1235 createcommand("QLINE",handle_qline,'o',1,"<core>");
1236 createcommand("ELINE",handle_eline,'o',1,"<core>");
1237 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1238 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1239 createcommand("SERVER",handle_server,0,0,"<core>");
1240 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1243 bool DirValid(char* dirandfile)
1246 strlcpy(work,dirandfile,MAXBUF);
1247 int p = strlen(work);
1248 // we just want the dir
1249 while (strlen(work))
1258 char buffer[MAXBUF], otherdir[MAXBUF];
1259 // Get the current working directory
1260 if( getcwd( buffer, MAXBUF ) == NULL )
1263 if( getcwd( otherdir, MAXBUF ) == NULL )
1266 if (strlen(otherdir) >= strlen(work))
1268 otherdir[strlen(work)] = '\0';
1269 if (!strcmp(otherdir,work))
1278 std::string GetFullProgDir(char** argv, int argc)
1281 strlcpy(work,argv[0],MAXBUF);
1282 int p = strlen(work);
1283 // we just want the dir
1284 while (strlen(work))
1293 char buffer[MAXBUF], otherdir[MAXBUF];
1294 // Get the current working directory
1295 if( getcwd( buffer, MAXBUF ) == NULL )
1298 if( getcwd( otherdir, MAXBUF ) == NULL )