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"
49 #include "inspstring.h"
50 #include "helperfuncs.h"
54 extern std::vector<Module*> modules;
58 extern char lowermap[255];
59 extern char ServerName[MAXBUF];
60 extern char Network[MAXBUF];
61 extern char ServerDesc[MAXBUF];
62 extern char list[MAXBUF];
67 extern std::stringstream config_f;
69 extern serverrec* me[32];
71 extern FILE *log_file;
72 extern userrec* fd_ref_table[65536];
74 extern int statsAccept, statsRefused, statsUnknown, statsCollisions, statsDns, statsDnsGood, statsDnsBad, statsConnects, statsSent, statsRecv;
76 static char already_sent[65536];
77 extern std::vector<userrec*> all_opers;
79 extern ClassVector Classes;
81 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
82 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
83 typedef std::deque<command_t> command_table;
85 extern user_hash clientlist;
86 extern chan_hash chanlist;
87 extern command_table cmdlist;
88 extern file_cache MOTD;
89 extern file_cache RULES;
91 void log(int level,char *text, ...)
93 char textbuffer[MAXBUF];
101 timeinfo = localtime (&rawtime);
106 va_start (argsPtr, text);
107 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
109 strlcpy(b,asctime(timeinfo),MAXBUF);
110 b[24] = ':'; // we know this is the end of the time string
111 fprintf(log_file,"%s %s\n",b,textbuffer);
114 // nofork enabled? display it on terminal too
115 printf("%s %s\n",b,textbuffer);
120 void readfile(file_cache &F, const char* fname)
123 char linebuf[MAXBUF];
125 log(DEBUG,"readfile: loading %s",fname);
127 file = fopen(fname,"r");
132 fgets(linebuf,sizeof(linebuf),file);
133 linebuf[strlen(linebuf)-1]='\0';
140 F.push_back(linebuf);
147 log(DEBUG,"readfile: failed to load file: %s",fname);
149 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
152 void Write(int sock,char *text, ...)
158 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
161 char textbuffer[MAXBUF];
165 va_start (argsPtr, text);
166 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
168 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
170 if (fd_ref_table[sock])
173 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
174 fd_ref_table[sock]->AddWriteBuf(tb);
177 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
180 /* write a server formatted numeric response to a single socket */
182 void WriteServ(int sock, char* text, ...)
188 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
191 char textbuffer[MAXBUF],tb[MAXBUF];
193 va_start (argsPtr, text);
195 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
197 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",ServerName,textbuffer);
199 if (fd_ref_table[sock])
202 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
203 fd_ref_table[sock]->AddWriteBuf(tb);
206 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
209 /* write text from an originating user to originating user */
211 void WriteFrom(int sock, userrec *user,char* text, ...)
215 if ((!text) || (!user))
217 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
220 char textbuffer[MAXBUF],tb[MAXBUF];
222 va_start (argsPtr, text);
224 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
226 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
228 if (fd_ref_table[sock])
231 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
232 fd_ref_table[sock]->AddWriteBuf(tb);
235 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
238 /* write text to an destination user from a source user (e.g. user privmsg) */
240 void WriteTo(userrec *source, userrec *dest,char *data, ...)
242 if ((!dest) || (!data))
244 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
247 if (dest->fd == FD_MAGIC_NUMBER)
249 char textbuffer[MAXBUF],tb[MAXBUF];
251 va_start (argsPtr, data);
252 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
256 // if no source given send it from the server.
259 WriteServ(dest->fd,":%s %s",ServerName,textbuffer);
263 WriteFrom(dest->fd,source,"%s",textbuffer);
267 /* write formatted text from a source user to all users on a channel
268 * including the sender (NOT for privmsg, notice etc!) */
270 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
272 if ((!Ptr) || (!user) || (!text))
274 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
277 char textbuffer[MAXBUF];
279 va_start (argsPtr, text);
280 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
283 std::vector<char*> *ulist = Ptr->GetUsers();
284 for (unsigned int j = 0; j < ulist->size(); j++)
286 char* o = (*ulist)[j];
287 userrec* otheruser = (userrec*)o;
288 if (otheruser->fd != FD_MAGIC_NUMBER)
289 WriteTo(user,otheruser,"%s",textbuffer);
293 /* write formatted text from a source user to all users on a channel
294 * including the sender (NOT for privmsg, notice etc!) doesnt send to
295 * users on remote servers */
297 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
299 if ((!Ptr) || (!text))
301 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
304 char textbuffer[MAXBUF];
306 va_start (argsPtr, text);
307 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
310 std::vector<char*> *ulist = Ptr->GetUsers();
311 for (unsigned int j = 0; j < ulist->size(); j++)
313 char* o = (*ulist)[j];
314 userrec* otheruser = (userrec*)o;
315 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
319 WriteServ(otheruser->fd,"%s",textbuffer);
323 WriteTo(user,otheruser,"%s",textbuffer);
329 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
331 if ((!Ptr) || (!text))
333 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
336 char textbuffer[MAXBUF];
338 va_start (argsPtr, text);
339 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
343 std::vector<char*> *ulist = Ptr->GetUsers();
344 for (unsigned int j = 0; j < ulist->size(); j++)
346 char* o = (*ulist)[j];
347 userrec* otheruser = (userrec*)o;
348 if (otheruser->fd != FD_MAGIC_NUMBER)
349 WriteServ(otheruser->fd,"%s",textbuffer);
353 /* write formatted text from a source user to all users on a channel except
354 * for the sender (for privmsg etc) */
356 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
358 if ((!Ptr) || (!user) || (!text))
360 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
363 char textbuffer[MAXBUF];
365 va_start (argsPtr, text);
366 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
369 std::vector<char*> *ulist = Ptr->GetUsers();
370 for (unsigned int j = 0; j < ulist->size(); j++)
372 char* o = (*ulist)[j];
373 userrec* otheruser = (userrec*)o;
374 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
375 WriteFrom(otheruser->fd,user,"%s",textbuffer);
379 std::string GetServerDescription(char* servername)
381 for (int j = 0; j < 32; j++)
385 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
387 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),servername))
389 return me[j]->connectors[k].GetDescription();
394 return ServerDesc; // not a remote server that can be found, it must be me.
397 /* write a formatted string to all users who share at least one common
398 * channel, including the source user e.g. for use in NICK */
400 void WriteCommon(userrec *u, char* text, ...)
404 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
408 if (u->registered != 7) {
409 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
413 char textbuffer[MAXBUF];
415 va_start (argsPtr, text);
416 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
419 // FIX: Stops a message going to the same person more than once
420 memset(&already_sent,0,65536);
422 bool sent_to_at_least_one = false;
424 for (int i = 0; i < MAXCHANS; i++)
426 if (u->chans[i].channel)
428 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
429 for (unsigned int j = 0; j < ulist->size(); j++)
431 char* o = (*ulist)[j];
432 userrec* otheruser = (userrec*)o;
433 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
435 already_sent[otheruser->fd] = 1;
436 WriteFrom(otheruser->fd,u,"%s",textbuffer);
437 sent_to_at_least_one = true;
442 // if the user was not in any channels, no users will receive the text. Make sure the user
443 // receives their OWN message for WriteCommon
444 if (!sent_to_at_least_one)
446 WriteFrom(u->fd,u,"%s",textbuffer);
450 /* write a formatted string to all users who share at least one common
451 * channel, NOT including the source user e.g. for use in QUIT */
453 void WriteCommonExcept(userrec *u, char* text, ...)
457 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
461 if (u->registered != 7) {
462 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
466 char textbuffer[MAXBUF];
468 va_start (argsPtr, text);
469 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
472 memset(&already_sent,0,65536);
474 for (int i = 0; i < MAXCHANS; i++)
476 if (u->chans[i].channel)
478 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
479 for (unsigned int j = 0; j < ulist->size(); j++)
481 char* o = (*ulist)[j];
482 userrec* otheruser = (userrec*)o;
485 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
487 already_sent[otheruser->fd] = 1;
488 WriteFrom(otheruser->fd,u,"%s",textbuffer);
496 void WriteOpers(char* text, ...)
500 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
504 char textbuffer[MAXBUF];
506 va_start (argsPtr, text);
507 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
510 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
513 if ((a) && (a->fd != FD_MAGIC_NUMBER))
515 if (strchr(a->modes,'s'))
517 // send server notices to all with +s
518 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
524 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
526 if ((!text) || (!source))
528 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
532 char textbuffer[MAXBUF];
534 va_start (argsPtr, text);
535 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
538 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
541 if ((a) && (a->fd != FD_MAGIC_NUMBER))
543 if (strchr(a->modes,'s'))
545 // send server notices to all with +s
546 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
552 // returns TRUE of any users on channel C occupy server 'servername'.
554 bool ChanAnyOnThisServer(chanrec *c,char* servername)
556 log(DEBUG,"ChanAnyOnThisServer");
558 std::vector<char*> *ulist = c->GetUsers();
559 for (unsigned int j = 0; j < ulist->size(); j++)
561 char* o = (*ulist)[j];
562 userrec* user = (userrec*)o;
563 if (!strcasecmp(user->server,servername))
569 // returns true if user 'u' shares any common channels with any users on server 'servername'
571 bool CommonOnThisServer(userrec* u,const char* servername)
573 log(DEBUG,"ChanAnyOnThisServer");
575 for (int i = 0; i < MAXCHANS; i++)
577 if (u->chans[i].channel)
579 std::vector<char*> *ulist = u->chans[i].channel->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))
593 void WriteMode(const char* modes, int flags, const char* text, ...)
595 if ((!text) || (!modes) || (!flags))
597 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
601 char textbuffer[MAXBUF];
603 va_start (argsPtr, text);
604 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
606 int modelen = strlen(modes);
608 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
610 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
612 bool send_to_user = false;
617 for (int n = 0; n < modelen; n++)
619 if (!hasumode(i->second,modes[n]))
621 send_to_user = false;
626 else if (flags == WM_OR)
628 send_to_user = false;
629 for (int n = 0; n < modelen; n++)
631 if (hasumode(i->second,modes[n]))
641 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
647 void NoticeAll(userrec *source, bool local_only, char* text, ...)
649 if ((!text) || (!source))
651 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
655 char textbuffer[MAXBUF];
657 va_start (argsPtr, text);
658 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
661 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
663 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
665 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
672 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
674 if ((!text) || (!source))
676 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
680 char textbuffer[MAXBUF];
682 va_start (argsPtr, text);
683 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
686 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
688 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
690 if (strchr(i->second->modes,'w'))
692 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
698 /* convert a string to lowercase. Note following special circumstances
699 * taken from RFC 1459. Many "official" server branches still hold to this
700 * rule so i will too;
702 * Because of IRC's scandanavian origin, the characters {}| are
703 * considered to be the lower case equivalents of the characters []\,
704 * respectively. This is a critical issue when determining the
705 * equivalence of two nicknames.
708 void strlower(char *n)
712 for (char* t = n; *t; t++)
713 *t = lowermap[(unsigned)*t];
717 /* Find a user record by nickname and return a pointer to it */
719 userrec* Find(std::string nick)
721 user_hash::iterator iter = clientlist.find(nick);
723 if (iter == clientlist.end())
724 /* Couldn't find it */
730 /* find a channel record by channel name and return a pointer to it */
732 chanrec* FindChan(const char* chan)
736 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
740 chan_hash::iterator iter = chanlist.find(chan);
742 if (iter == chanlist.end())
743 /* Couldn't find it */
750 long GetMaxBans(char* name)
753 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
755 ConfValue("banlist","chan",count,CM,&config_f);
758 ConfValue("banlist","limit",count,CM,&config_f);
765 void purge_empty_chans(userrec* u)
768 int go_again = 1, purge = 0;
770 // firstly decrement the count on each channel
771 for (int f = 0; f < MAXCHANS; f++)
773 if (u->chans[f].channel)
775 u->chans[f].channel->DelUser((char*)u);
779 for (int i = 0; i < MAXCHANS; i++)
781 if (u->chans[i].channel)
783 if (!usercount(u->chans[i].channel))
785 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
786 /* kill the record */
787 if (i2 != chanlist.end())
789 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
795 u->chans[i].channel = NULL;
800 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
804 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
810 char scratch[MAXBUF];
813 char* chanmodes(chanrec *chan)
817 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
824 if (chan->binarymodes & CM_NOEXTERNAL)
826 strlcat(scratch,"n",MAXMODES);
828 if (chan->binarymodes & CM_TOPICLOCK)
830 strlcat(scratch,"t",MAXMODES);
834 strlcat(scratch,"k",MAXMODES);
838 strlcat(scratch,"l",MAXMODES);
840 if (chan->binarymodes & CM_INVITEONLY)
842 strlcat(scratch,"i",MAXMODES);
844 if (chan->binarymodes & CM_MODERATED)
846 strlcat(scratch,"m",MAXMODES);
848 if (chan->binarymodes & CM_SECRET)
850 strlcat(scratch,"s",MAXMODES);
852 if (chan->binarymodes & CM_PRIVATE)
854 strlcat(scratch,"p",MAXMODES);
858 strlcat(sparam," ",MAXBUF);
859 strlcat(sparam,chan->key,MAXBUF);
864 sprintf(foo," %lu",(unsigned long)chan->limit);
865 strlcat(sparam,foo,MAXBUF);
867 if (*chan->custom_modes)
869 strlcat(scratch,chan->custom_modes,MAXMODES);
870 for (int z = 0; chan->custom_modes[z] != 0; z++)
872 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
875 strlcat(sparam," ",MAXBUF);
876 strlcat(sparam,extparam.c_str(),MAXBUF);
880 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
881 strlcat(scratch,sparam,MAXMODES);
886 /* compile a userlist of a channel into a string, each nick seperated by
887 * spaces and op, voice etc status shown as @ and + */
889 void userlist(userrec *user,chanrec *c)
893 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
897 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
899 std::vector<char*> *ulist = c->GetUsers();
900 for (unsigned int i = 0; i < ulist->size(); i++)
902 char* o = (*ulist)[i];
903 userrec* otheruser = (userrec*)o;
904 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
906 /* user is +i, and source not on the channel, does not show
907 * nick in NAMES list */
910 strlcat(list,cmode(otheruser,c),MAXBUF);
911 strlcat(list,otheruser->nick,MAXBUF);
912 strlcat(list," ",MAXBUF);
913 if (strlen(list)>(480-NICKMAX))
915 /* list overflowed into
916 * multiple numerics */
917 WriteServ(user->fd,"%s",list);
918 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
921 /* if whats left in the list isnt empty, send it */
922 if (list[strlen(list)-1] != ':')
924 WriteServ(user->fd,"%s",list);
928 /* return a count of the users on a specific channel accounting for
929 * invisible users who won't increase the count. e.g. for /LIST */
931 int usercount_i(chanrec *c)
937 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
942 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
946 if (has_channel(i->second,c))
948 if (isnick(i->second->nick))
950 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
952 /* user is +i, and source not on the channel, does not show
953 * nick in NAMES list */
961 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
966 int usercount(chanrec *c)
970 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
973 int count = c->GetUserCounter();
974 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
979 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
981 char* Passwd(userrec *user)
983 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
985 if (match(user->host,i->host) && (i->type == CC_ALLOW))
993 bool IsDenied(userrec *user)
995 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
997 if (match(user->host,i->host) && (i->type == CC_DENY))
1008 /* sends out an error notice to all connected clients (not to be used
1011 void send_error(char *s)
1013 log(DEBUG,"send_error: %s",s);
1014 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1016 if (isnick(i->second->nick))
1018 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1022 // fix - unregistered connections receive ERROR, not NOTICE
1023 Write(i->second->fd,"ERROR :%s",s);
1028 void Error(int status)
1030 signal (SIGALRM, SIG_IGN);
1031 signal (SIGPIPE, SIG_IGN);
1032 signal (SIGTERM, SIG_IGN);
1033 signal (SIGABRT, SIG_IGN);
1034 signal (SIGSEGV, SIG_IGN);
1035 signal (SIGURG, SIG_IGN);
1036 signal (SIGKILL, SIG_IGN);
1037 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1038 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1042 // this function counts all users connected, wether they are registered or NOT.
1045 return clientlist.size();
1048 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1049 int registered_usercount(void)
1052 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1054 if ((i->second->fd) && (isnick(i->second->nick))) c++;
1059 int usercount_invisible(void)
1063 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1065 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1070 int usercount_opers(void)
1074 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1076 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1081 int usercount_unknown(void)
1085 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1087 if ((i->second->fd) && (i->second->registered != 7))
1093 long chancount(void)
1095 return chanlist.size();
1098 long count_servs(void)
1101 for (int i = 0; i < 32; i++)
1105 for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
1107 if (strcasecmp(j->GetServerName().c_str(),ServerName))
1117 long servercount(void)
1119 return count_servs()+1;
1125 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1127 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,ServerName))) c++;
1132 void ShowMOTD(userrec *user)
1135 std::string WholeMOTD = "";
1138 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1141 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", ServerName, user->nick, ServerName);
1142 WholeMOTD = WholeMOTD + buf;
1143 for (unsigned int i = 0; i != MOTD.size(); i++)
1145 snprintf(buf,65535,":%s 372 %s :- %s\r\n", ServerName, user->nick, MOTD[i].c_str());
1146 WholeMOTD = WholeMOTD + buf;
1148 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", ServerName, user->nick);
1149 WholeMOTD = WholeMOTD + buf;
1150 // only one write operation
1151 user->AddWriteBuf(WholeMOTD);
1152 statsSent += WholeMOTD.length();
1155 void ShowRULES(userrec *user)
1159 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1162 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
1163 for (unsigned int i = 0; i != RULES.size(); i++)
1165 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1167 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
1170 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1171 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1172 // registration timeout maximum seconds)
1173 bool AllModulesReportReady(userrec* user)
1175 for (int i = 0; i <= MODCOUNT; i++)
1177 int res = modules[i]->OnCheckReady(user);
1184 char islast(const char* s)
1187 for (int j = 0; j < 32; j++)
1191 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
1193 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
1197 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
1207 long map_count(const char* s)
1210 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1212 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,s))) c++;
1217 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1220 /* create the command and push it onto the table */
1221 strlcpy(comm.command,cmd,MAXBUF);
1222 strlcpy(comm.source,source,MAXBUF);
1223 comm.handler_function = f;
1224 comm.flags_needed = flags;
1225 comm.min_params = minparams;
1227 comm.total_bytes = 0;
1228 cmdlist.push_back(comm);
1229 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1233 void SetupCommandTable(void)
1235 createcommand("USER",handle_user,0,4,"<core>");
1236 createcommand("NICK",handle_nick,0,1,"<core>");
1237 createcommand("QUIT",handle_quit,0,0,"<core>");
1238 createcommand("VERSION",handle_version,0,0,"<core>");
1239 createcommand("PING",handle_ping,0,1,"<core>");
1240 createcommand("PONG",handle_pong,0,1,"<core>");
1241 createcommand("ADMIN",handle_admin,0,0,"<core>");
1242 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1243 createcommand("INFO",handle_info,0,0,"<core>");
1244 createcommand("TIME",handle_time,0,0,"<core>");
1245 createcommand("WHOIS",handle_whois,0,1,"<core>");
1246 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1247 createcommand("NOTICE",handle_notice,0,2,"<core>");
1248 createcommand("JOIN",handle_join,0,1,"<core>");
1249 createcommand("NAMES",handle_names,0,0,"<core>");
1250 createcommand("PART",handle_part,0,1,"<core>");
1251 createcommand("KICK",handle_kick,0,2,"<core>");
1252 createcommand("MODE",handle_mode,0,1,"<core>");
1253 createcommand("TOPIC",handle_topic,0,1,"<core>");
1254 createcommand("WHO",handle_who,0,1,"<core>");
1255 createcommand("MOTD",handle_motd,0,0,"<core>");
1256 createcommand("RULES",handle_rules,0,0,"<core>");
1257 createcommand("OPER",handle_oper,0,2,"<core>");
1258 createcommand("LIST",handle_list,0,0,"<core>");
1259 createcommand("DIE",handle_die,'o',1,"<core>");
1260 createcommand("RESTART",handle_restart,'o',1,"<core>");
1261 createcommand("KILL",handle_kill,'o',2,"<core>");
1262 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1263 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1264 createcommand("STATS",handle_stats,0,1,"<core>");
1265 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1266 createcommand("AWAY",handle_away,0,0,"<core>");
1267 createcommand("ISON",handle_ison,0,0,"<core>");
1268 createcommand("SUMMON",handle_summon,0,0,"<core>");
1269 createcommand("USERS",handle_users,0,0,"<core>");
1270 createcommand("INVITE",handle_invite,0,0,"<core>");
1271 createcommand("PASS",handle_pass,0,1,"<core>");
1272 createcommand("TRACE",handle_trace,'o',0,"<core>");
1273 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1274 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1275 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1276 createcommand("MODULES",handle_modules,0,0,"<core>");
1277 createcommand("LINKS",handle_links,0,0,"<core>");
1278 createcommand("MAP",handle_map,0,0,"<core>");
1279 createcommand("KLINE",handle_kline,'o',1,"<core>");
1280 createcommand("GLINE",handle_gline,'o',1,"<core>");
1281 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1282 createcommand("QLINE",handle_qline,'o',1,"<core>");
1283 createcommand("ELINE",handle_eline,'o',1,"<core>");
1284 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1285 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1286 createcommand("SERVER",handle_server,0,0,"<core>");
1287 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1290 bool DirValid(char* dirandfile)
1293 strlcpy(work,dirandfile,MAXBUF);
1294 int p = strlen(work);
1295 // we just want the dir
1296 while (strlen(work))
1305 char buffer[MAXBUF], otherdir[MAXBUF];
1306 // Get the current working directory
1307 if( getcwd( buffer, MAXBUF ) == NULL )
1310 if( getcwd( otherdir, MAXBUF ) == NULL )
1313 if (strlen(otherdir) >= strlen(work))
1315 otherdir[strlen(work)] = '\0';
1316 if (!strcmp(otherdir,work))
1325 std::string GetFullProgDir(char** argv, int argc)
1328 strlcpy(work,argv[0],MAXBUF);
1329 int p = strlen(work);
1330 // we just want the dir
1331 while (strlen(work))
1340 char buffer[MAXBUF], otherdir[MAXBUF];
1341 // Get the current working directory
1342 if( getcwd( buffer, MAXBUF ) == NULL )
1345 if( getcwd( otherdir, MAXBUF ) == NULL )