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"
53 extern std::vector<Module*> modules;
54 extern ServerConfig *Config;
57 extern char lowermap[255];
58 extern std::stringstream config_f;
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 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, irc::StrHashComp> user_hash;
70 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, irc::StrHashComp> chan_hash;
71 typedef std::deque<command_t> command_table;
73 extern user_hash clientlist;
74 extern chan_hash chanlist;
75 extern command_table cmdlist;
76 extern file_cache MOTD;
77 extern file_cache RULES;
79 void log(int level,char *text, ...)
81 char textbuffer[MAXBUF];
89 timeinfo = localtime (&rawtime);
94 va_start (argsPtr, text);
95 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
97 strlcpy(b,asctime(timeinfo),MAXBUF);
98 b[24] = ':'; // we know this is the end of the time string
99 fprintf(log_file,"%s %s\n",b,textbuffer);
102 // nofork enabled? display it on terminal too
103 printf("%s %s\n",b,textbuffer);
108 void readfile(file_cache &F, const char* fname)
111 char linebuf[MAXBUF];
113 log(DEBUG,"readfile: loading %s",fname);
115 file = fopen(fname,"r");
120 fgets(linebuf,sizeof(linebuf),file);
121 linebuf[strlen(linebuf)-1]='\0';
128 F.push_back(linebuf);
135 log(DEBUG,"readfile: failed to load file: %s",fname);
137 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
140 void Write(int sock,char *text, ...)
146 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
149 char textbuffer[MAXBUF];
153 va_start (argsPtr, text);
154 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
156 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
158 if (fd_ref_table[sock])
161 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
162 fd_ref_table[sock]->AddWriteBuf(tb);
165 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
168 /* write a server formatted numeric response to a single socket */
170 void WriteServ(int sock, char* text, ...)
176 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
179 char textbuffer[MAXBUF],tb[MAXBUF];
181 va_start (argsPtr, text);
183 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
185 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",ServerName,textbuffer);
187 if (fd_ref_table[sock])
190 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
191 fd_ref_table[sock]->AddWriteBuf(tb);
194 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
197 /* write text from an originating user to originating user */
199 void WriteFrom(int sock, userrec *user,char* text, ...)
203 if ((!text) || (!user))
205 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
208 char textbuffer[MAXBUF],tb[MAXBUF];
210 va_start (argsPtr, text);
212 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
214 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
216 if (fd_ref_table[sock])
219 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
220 fd_ref_table[sock]->AddWriteBuf(tb);
223 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
226 /* write text to an destination user from a source user (e.g. user privmsg) */
228 void WriteTo(userrec *source, userrec *dest,char *data, ...)
230 if ((!dest) || (!data))
232 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
235 if (dest->fd == FD_MAGIC_NUMBER)
237 char textbuffer[MAXBUF],tb[MAXBUF];
239 va_start (argsPtr, data);
240 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
244 // if no source given send it from the server.
247 WriteServ(dest->fd,":%s %s",ServerName,textbuffer);
251 WriteFrom(dest->fd,source,"%s",textbuffer);
255 /* write formatted text from a source user to all users on a channel
256 * including the sender (NOT for privmsg, notice etc!) */
258 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
260 if ((!Ptr) || (!user) || (!text))
262 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
265 char textbuffer[MAXBUF];
267 va_start (argsPtr, text);
268 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
271 std::vector<char*> *ulist = Ptr->GetUsers();
272 for (unsigned int j = 0; j < ulist->size(); j++)
274 char* o = (*ulist)[j];
275 userrec* otheruser = (userrec*)o;
276 if (otheruser->fd != FD_MAGIC_NUMBER)
277 WriteTo(user,otheruser,"%s",textbuffer);
281 /* write formatted text from a source user to all users on a channel
282 * including the sender (NOT for privmsg, notice etc!) doesnt send to
283 * users on remote servers */
285 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
287 if ((!Ptr) || (!text))
289 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
292 char textbuffer[MAXBUF];
294 va_start (argsPtr, text);
295 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
298 std::vector<char*> *ulist = Ptr->GetUsers();
299 for (unsigned int j = 0; j < ulist->size(); j++)
301 char* o = (*ulist)[j];
302 userrec* otheruser = (userrec*)o;
303 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
307 WriteServ(otheruser->fd,"%s",textbuffer);
311 WriteTo(user,otheruser,"%s",textbuffer);
317 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
319 if ((!Ptr) || (!text))
321 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
324 char textbuffer[MAXBUF];
326 va_start (argsPtr, text);
327 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
331 std::vector<char*> *ulist = Ptr->GetUsers();
332 for (unsigned int j = 0; j < ulist->size(); j++)
334 char* o = (*ulist)[j];
335 userrec* otheruser = (userrec*)o;
336 if (otheruser->fd != FD_MAGIC_NUMBER)
337 WriteServ(otheruser->fd,"%s",textbuffer);
341 /* write formatted text from a source user to all users on a channel except
342 * for the sender (for privmsg etc) */
344 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
346 if ((!Ptr) || (!user) || (!text))
348 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
351 char textbuffer[MAXBUF];
353 va_start (argsPtr, text);
354 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
357 std::vector<char*> *ulist = Ptr->GetUsers();
358 for (unsigned int j = 0; j < ulist->size(); j++)
360 char* o = (*ulist)[j];
361 userrec* otheruser = (userrec*)o;
362 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
363 WriteFrom(otheruser->fd,user,"%s",textbuffer);
367 std::string GetServerDescription(char* servername)
369 std::string description = "";
370 FOREACH_MOD OnGetServerDescription(servername,description);
371 if (description != "")
377 return ServerDesc; // not a remote server that can be found, it must be me.
381 /* write a formatted string to all users who share at least one common
382 * channel, including the source user e.g. for use in NICK */
384 void WriteCommon(userrec *u, char* text, ...)
388 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
392 if (u->registered != 7) {
393 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
397 char textbuffer[MAXBUF];
399 va_start (argsPtr, text);
400 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
403 // FIX: Stops a message going to the same person more than once
404 memset(&already_sent,0,65536);
406 bool sent_to_at_least_one = false;
408 for (unsigned int i = 0; i < u->chans.size(); i++)
410 if (u->chans[i].channel)
412 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
413 for (unsigned int j = 0; j < ulist->size(); j++)
415 char* o = (*ulist)[j];
416 userrec* otheruser = (userrec*)o;
417 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
419 already_sent[otheruser->fd] = 1;
420 WriteFrom(otheruser->fd,u,"%s",textbuffer);
421 sent_to_at_least_one = true;
426 // if the user was not in any channels, no users will receive the text. Make sure the user
427 // receives their OWN message for WriteCommon
428 if (!sent_to_at_least_one)
430 WriteFrom(u->fd,u,"%s",textbuffer);
434 /* write a formatted string to all users who share at least one common
435 * channel, NOT including the source user e.g. for use in QUIT */
437 void WriteCommonExcept(userrec *u, char* text, ...)
441 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
445 if (u->registered != 7) {
446 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
450 char textbuffer[MAXBUF];
452 va_start (argsPtr, text);
453 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
456 memset(&already_sent,0,65536);
458 for (unsigned int i = 0; i < u->chans.size(); i++)
460 if (u->chans[i].channel)
462 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
463 for (unsigned int j = 0; j < ulist->size(); j++)
465 char* o = (*ulist)[j];
466 userrec* otheruser = (userrec*)o;
469 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
471 already_sent[otheruser->fd] = 1;
472 WriteFrom(otheruser->fd,u,"%s",textbuffer);
480 void WriteOpers(char* text, ...)
484 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
488 char textbuffer[MAXBUF];
490 va_start (argsPtr, text);
491 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
494 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
497 if ((a) && (a->fd != FD_MAGIC_NUMBER))
499 if (strchr(a->modes,'s'))
501 // send server notices to all with +s
502 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
508 void ServerNoticeAll(char* text, ...)
513 char textbuffer[MAXBUF];
515 va_start (argsPtr, text);
516 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
519 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
521 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
523 WriteServ(i->second->fd,"NOTICE $%s :%s",ServerName,textbuffer);
528 void ServerPrivmsgAll(char* text, ...)
533 char textbuffer[MAXBUF];
535 va_start (argsPtr, text);
536 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
539 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
541 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
543 WriteServ(i->second->fd,"PRIVMSG $%s :%s",ServerName,textbuffer);
548 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
550 if ((!text) || (!source))
552 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
556 char textbuffer[MAXBUF];
558 va_start (argsPtr, text);
559 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
562 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
565 if ((a) && (a->fd != FD_MAGIC_NUMBER))
567 if (strchr(a->modes,'s'))
569 // send server notices to all with +s
570 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
576 // returns TRUE of any users on channel C occupy server 'servername'.
578 bool ChanAnyOnThisServer(chanrec *c,char* servername)
580 log(DEBUG,"ChanAnyOnThisServer");
582 std::vector<char*> *ulist = c->GetUsers();
583 for (unsigned int j = 0; j < ulist->size(); j++)
585 char* o = (*ulist)[j];
586 userrec* user = (userrec*)o;
587 if (!strcasecmp(user->server,servername))
593 // returns true if user 'u' shares any common channels with any users on server 'servername'
595 bool CommonOnThisServer(userrec* u,const char* servername)
597 log(DEBUG,"ChanAnyOnThisServer");
599 for (unsigned int i = 0; i < u->chans.size(); i++)
601 if (u->chans[i].channel)
603 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
604 for (unsigned int j = 0; j < ulist->size(); j++)
606 char* o = (*ulist)[j];
607 userrec* user = (userrec*)o;
608 if (!strcasecmp(user->server,servername))
617 void WriteMode(const char* modes, int flags, const char* text, ...)
619 if ((!text) || (!modes) || (!flags))
621 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
625 char textbuffer[MAXBUF];
627 va_start (argsPtr, text);
628 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
630 int modelen = strlen(modes);
632 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
634 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
636 bool send_to_user = false;
641 for (int n = 0; n < modelen; n++)
643 if (!hasumode(i->second,modes[n]))
645 send_to_user = false;
650 else if (flags == WM_OR)
652 send_to_user = false;
653 for (int n = 0; n < modelen; n++)
655 if (hasumode(i->second,modes[n]))
665 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
671 void NoticeAll(userrec *source, bool local_only, char* text, ...)
673 if ((!text) || (!source))
675 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
679 char textbuffer[MAXBUF];
681 va_start (argsPtr, text);
682 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
685 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
687 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
689 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
696 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
698 if ((!text) || (!source))
700 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
704 char textbuffer[MAXBUF];
706 va_start (argsPtr, text);
707 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
710 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
712 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
714 if (strchr(i->second->modes,'w'))
716 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
722 /* convert a string to lowercase. Note following special circumstances
723 * taken from RFC 1459. Many "official" server branches still hold to this
724 * rule so i will too;
726 * Because of IRC's scandanavian origin, the characters {}| are
727 * considered to be the lower case equivalents of the characters []\,
728 * respectively. This is a critical issue when determining the
729 * equivalence of two nicknames.
732 void strlower(char *n)
736 for (char* t = n; *t; t++)
737 *t = lowermap[(unsigned)*t];
741 /* Find a user record by nickname and return a pointer to it */
743 userrec* Find(std::string nick)
745 user_hash::iterator iter = clientlist.find(nick);
747 if (iter == clientlist.end())
748 /* Couldn't find it */
754 /* find a channel record by channel name and return a pointer to it */
756 chanrec* FindChan(const char* chan)
760 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
764 chan_hash::iterator iter = chanlist.find(chan);
766 if (iter == chanlist.end())
767 /* Couldn't find it */
774 long GetMaxBans(char* name)
777 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
779 ConfValue("banlist","chan",count,CM,&config_f);
782 ConfValue("banlist","limit",count,CM,&config_f);
789 void purge_empty_chans(userrec* u)
792 int go_again = 1, purge = 0;
794 // firstly decrement the count on each channel
795 for (unsigned int f = 0; f < u->chans.size(); f++)
797 if (u->chans[f].channel)
799 u->chans[f].channel->DelUser((char*)u);
803 for (unsigned int i = 0; i < u->chans.size(); i++)
805 if (u->chans[i].channel)
807 if (!usercount(u->chans[i].channel))
809 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
810 /* kill the record */
811 if (i2 != chanlist.end())
813 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
819 u->chans[i].channel = NULL;
824 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
828 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
834 char scratch[MAXBUF];
837 char* chanmodes(chanrec *chan)
841 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
848 if (chan->binarymodes & CM_NOEXTERNAL)
850 strlcat(scratch,"n",MAXMODES);
852 if (chan->binarymodes & CM_TOPICLOCK)
854 strlcat(scratch,"t",MAXMODES);
858 strlcat(scratch,"k",MAXMODES);
862 strlcat(scratch,"l",MAXMODES);
864 if (chan->binarymodes & CM_INVITEONLY)
866 strlcat(scratch,"i",MAXMODES);
868 if (chan->binarymodes & CM_MODERATED)
870 strlcat(scratch,"m",MAXMODES);
872 if (chan->binarymodes & CM_SECRET)
874 strlcat(scratch,"s",MAXMODES);
876 if (chan->binarymodes & CM_PRIVATE)
878 strlcat(scratch,"p",MAXMODES);
882 strlcat(sparam," ",MAXBUF);
883 strlcat(sparam,chan->key,MAXBUF);
888 sprintf(foo," %lu",(unsigned long)chan->limit);
889 strlcat(sparam,foo,MAXBUF);
891 if (*chan->custom_modes)
893 strlcat(scratch,chan->custom_modes,MAXMODES);
894 for (int z = 0; chan->custom_modes[z] != 0; z++)
896 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
899 strlcat(sparam," ",MAXBUF);
900 strlcat(sparam,extparam.c_str(),MAXBUF);
904 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
905 strlcat(scratch,sparam,MAXMODES);
910 /* compile a userlist of a channel into a string, each nick seperated by
911 * spaces and op, voice etc status shown as @ and + */
913 void userlist(userrec *user,chanrec *c)
917 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
921 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
923 std::vector<char*> *ulist = c->GetUsers();
924 for (unsigned int i = 0; i < ulist->size(); i++)
926 char* o = (*ulist)[i];
927 userrec* otheruser = (userrec*)o;
928 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
930 /* user is +i, and source not on the channel, does not show
931 * nick in NAMES list */
934 strlcat(list,cmode(otheruser,c),MAXBUF);
935 strlcat(list,otheruser->nick,MAXBUF);
936 strlcat(list," ",MAXBUF);
937 if (strlen(list)>(480-NICKMAX))
939 /* list overflowed into
940 * multiple numerics */
941 WriteServ(user->fd,"%s",list);
942 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
945 /* if whats left in the list isnt empty, send it */
946 if (list[strlen(list)-1] != ':')
948 WriteServ(user->fd,"%s",list);
952 /* return a count of the users on a specific channel accounting for
953 * invisible users who won't increase the count. e.g. for /LIST */
955 int usercount_i(chanrec *c)
961 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
966 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
970 if (has_channel(i->second,c))
972 if (isnick(i->second->nick))
974 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
976 /* user is +i, and source not on the channel, does not show
977 * nick in NAMES list */
985 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
990 int usercount(chanrec *c)
994 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
997 int count = c->GetUserCounter();
998 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
1003 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1005 char* Passwd(userrec *user)
1007 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1009 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1017 bool IsDenied(userrec *user)
1019 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1021 if (match(user->host,i->host) && (i->type == CC_DENY))
1032 /* sends out an error notice to all connected clients (not to be used
1035 void send_error(char *s)
1037 log(DEBUG,"send_error: %s",s);
1038 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1040 if (isnick(i->second->nick))
1042 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1046 // fix - unregistered connections receive ERROR, not NOTICE
1047 Write(i->second->fd,"ERROR :%s",s);
1052 void Error(int status)
1054 signal (SIGALRM, SIG_IGN);
1055 signal (SIGPIPE, SIG_IGN);
1056 signal (SIGTERM, SIG_IGN);
1057 signal (SIGABRT, SIG_IGN);
1058 signal (SIGSEGV, SIG_IGN);
1059 signal (SIGURG, SIG_IGN);
1060 signal (SIGKILL, SIG_IGN);
1061 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1062 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1066 // this function counts all users connected, wether they are registered or NOT.
1069 return clientlist.size();
1072 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1073 int registered_usercount(void)
1076 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1078 if (i->second->registered == 7) c++;
1083 int usercount_invisible(void)
1086 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1088 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1093 int usercount_opers(void)
1096 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1098 if ((isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1103 int usercount_unknown(void)
1107 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1109 if ((i->second->fd > -1) && (i->second->registered != 7))
1115 long chancount(void)
1117 return chanlist.size();
1123 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1125 if ((isnick(i->second->nick)) && (i->second->fd > -1)) c++;
1130 void ShowMOTD(userrec *user)
1133 std::string WholeMOTD = "";
1136 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1139 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", ServerName, user->nick, ServerName);
1140 WholeMOTD = WholeMOTD + buf;
1141 for (unsigned int i = 0; i != MOTD.size(); i++)
1143 snprintf(buf,65535,":%s 372 %s :- %s\r\n", ServerName, user->nick, MOTD[i].c_str());
1144 WholeMOTD = WholeMOTD + buf;
1146 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", ServerName, user->nick);
1147 WholeMOTD = WholeMOTD + buf;
1148 // only one write operation
1149 user->AddWriteBuf(WholeMOTD);
1150 statsSent += WholeMOTD.length();
1153 void ShowRULES(userrec *user)
1157 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1160 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
1161 for (unsigned int i = 0; i != RULES.size(); i++)
1163 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1165 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
1168 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1169 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1170 // registration timeout maximum seconds)
1171 bool AllModulesReportReady(userrec* user)
1173 for (int i = 0; i <= MODCOUNT; i++)
1175 int res = modules[i]->OnCheckReady(user);
1182 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1185 /* create the command and push it onto the table */
1186 strlcpy(comm.command,cmd,MAXBUF);
1187 strlcpy(comm.source,source,MAXBUF);
1188 comm.handler_function = f;
1189 comm.flags_needed = flags;
1190 comm.min_params = minparams;
1192 comm.total_bytes = 0;
1193 cmdlist.push_back(comm);
1194 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1198 void SetupCommandTable(void)
1200 createcommand("USER",handle_user,0,4,"<core>");
1201 createcommand("NICK",handle_nick,0,1,"<core>");
1202 createcommand("QUIT",handle_quit,0,0,"<core>");
1203 createcommand("VERSION",handle_version,0,0,"<core>");
1204 createcommand("PING",handle_ping,0,1,"<core>");
1205 createcommand("PONG",handle_pong,0,1,"<core>");
1206 createcommand("ADMIN",handle_admin,0,0,"<core>");
1207 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1208 createcommand("INFO",handle_info,0,0,"<core>");
1209 createcommand("TIME",handle_time,0,0,"<core>");
1210 createcommand("WHOIS",handle_whois,0,1,"<core>");
1211 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1212 createcommand("NOTICE",handle_notice,0,2,"<core>");
1213 createcommand("JOIN",handle_join,0,1,"<core>");
1214 createcommand("NAMES",handle_names,0,0,"<core>");
1215 createcommand("PART",handle_part,0,1,"<core>");
1216 createcommand("KICK",handle_kick,0,2,"<core>");
1217 createcommand("MODE",handle_mode,0,1,"<core>");
1218 createcommand("TOPIC",handle_topic,0,1,"<core>");
1219 createcommand("WHO",handle_who,0,1,"<core>");
1220 createcommand("MOTD",handle_motd,0,0,"<core>");
1221 createcommand("RULES",handle_rules,0,0,"<core>");
1222 createcommand("OPER",handle_oper,0,2,"<core>");
1223 createcommand("LIST",handle_list,0,0,"<core>");
1224 createcommand("DIE",handle_die,'o',1,"<core>");
1225 createcommand("RESTART",handle_restart,'o',1,"<core>");
1226 createcommand("KILL",handle_kill,'o',2,"<core>");
1227 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1228 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1229 createcommand("STATS",handle_stats,0,1,"<core>");
1230 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1231 createcommand("AWAY",handle_away,0,0,"<core>");
1232 createcommand("ISON",handle_ison,0,0,"<core>");
1233 createcommand("SUMMON",handle_summon,0,0,"<core>");
1234 createcommand("USERS",handle_users,0,0,"<core>");
1235 createcommand("INVITE",handle_invite,0,0,"<core>");
1236 createcommand("PASS",handle_pass,0,1,"<core>");
1237 createcommand("TRACE",handle_trace,'o',0,"<core>");
1238 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1239 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1240 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1241 createcommand("MODULES",handle_modules,0,0,"<core>");
1242 createcommand("LINKS",handle_links,0,0,"<core>");
1243 createcommand("MAP",handle_map,0,0,"<core>");
1244 createcommand("KLINE",handle_kline,'o',1,"<core>");
1245 createcommand("GLINE",handle_gline,'o',1,"<core>");
1246 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1247 createcommand("QLINE",handle_qline,'o',1,"<core>");
1248 createcommand("ELINE",handle_eline,'o',1,"<core>");
1249 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1250 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1251 createcommand("SERVER",handle_server,0,0,"<core>");
1252 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1255 bool DirValid(char* dirandfile)
1258 strlcpy(work,dirandfile,MAXBUF);
1259 int p = strlen(work);
1260 // we just want the dir
1261 while (strlen(work))
1270 char buffer[MAXBUF], otherdir[MAXBUF];
1271 // Get the current working directory
1272 if( getcwd( buffer, MAXBUF ) == NULL )
1275 if( getcwd( otherdir, MAXBUF ) == NULL )
1278 if (strlen(otherdir) >= strlen(work))
1280 otherdir[strlen(work)] = '\0';
1281 if (!strcmp(otherdir,work))
1290 std::string GetFullProgDir(char** argv, int argc)
1293 strlcpy(work,argv[0],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 )