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);
554 snprintf(buffer,MAXBUF,"V %s @* :%s",source->nick,textbuffer);
555 NetSendToAll(buffer);
558 // returns TRUE of any users on channel C occupy server 'servername'.
560 bool ChanAnyOnThisServer(chanrec *c,char* servername)
562 log(DEBUG,"ChanAnyOnThisServer");
564 std::vector<char*> *ulist = c->GetUsers();
565 for (unsigned int j = 0; j < ulist->size(); j++)
567 char* o = (*ulist)[j];
568 userrec* user = (userrec*)o;
569 if (!strcasecmp(user->server,servername))
575 // returns true if user 'u' shares any common channels with any users on server 'servername'
577 bool CommonOnThisServer(userrec* u,const char* servername)
579 log(DEBUG,"ChanAnyOnThisServer");
581 for (int i = 0; i < MAXCHANS; i++)
583 if (u->chans[i].channel)
585 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
586 for (unsigned int j = 0; j < ulist->size(); j++)
588 char* o = (*ulist)[j];
589 userrec* user = (userrec*)o;
590 if (!strcasecmp(user->server,servername))
598 void NetSendToCommon(userrec* u, char* s)
603 snprintf(buffer,MAXBUF,"%s",s);
605 else snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
607 log(DEBUG,"NetSendToCommon: '%s' '%s'",u->nick,s);
609 std::string msg = buffer;
610 FOREACH_MOD OnPacketTransmit(msg,s);
611 strlcpy(buffer,msg.c_str(),MAXBUF);
613 for (int j = 0; j < 32; j++)
617 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
619 if (CommonOnThisServer(u,me[j]->connectors[k].GetServerName().c_str()))
621 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
629 void NetSendToAll(char* s)
634 snprintf(buffer,MAXBUF,"%s",s);
636 else snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
638 log(DEBUG,"NetSendToAll: '%s'",s);
640 std::string msg = buffer;
641 FOREACH_MOD OnPacketTransmit(msg,s);
642 strlcpy(buffer,msg.c_str(),MAXBUF);
644 for (int j = 0; j < 32; j++)
648 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
650 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
657 void NetSendToAll_WithSum(char* s,char* u)
662 snprintf(buffer,MAXBUF,"%s",s);
664 else snprintf(buffer,MAXBUF,":%s %s",u,s);
666 log(DEBUG,"NetSendToAll: '%s'",s);
668 std::string msg = buffer;
669 FOREACH_MOD OnPacketTransmit(msg,s);
670 strlcpy(buffer,msg.c_str(),MAXBUF);
672 for (int j = 0; j < 32; j++)
676 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
678 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
684 void NetSendToAllAlive(char* s)
689 snprintf(buffer,MAXBUF,"%s",s);
691 else snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
693 log(DEBUG,"NetSendToAllAlive: '%s'",s);
695 std::string msg = buffer;
696 FOREACH_MOD OnPacketTransmit(msg,s);
697 strlcpy(buffer,msg.c_str(),MAXBUF);
699 for (int j = 0; j < 32; j++)
703 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
705 if (me[j]->connectors[k].GetState() != STATE_DISCONNECTED)
707 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
711 log(DEBUG,"%s is dead, not sending to it.",me[j]->connectors[k].GetServerName().c_str());
719 void NetSendToOne(char* target,char* s)
724 snprintf(buffer,MAXBUF,"%s",s);
726 else snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
728 log(DEBUG,"NetSendToOne: '%s' '%s'",target,s);
730 std::string msg = buffer;
731 FOREACH_MOD OnPacketTransmit(msg,s);
732 strlcpy(buffer,msg.c_str(),MAXBUF);
734 for (int j = 0; j < 32; j++)
738 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
740 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
742 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
749 void NetSendToAllExcept(const char* target,char* s)
754 snprintf(buffer,MAXBUF,"%s",s);
756 else snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
758 log(DEBUG,"NetSendToAllExcept: '%s' '%s'",target,s);
760 std::string msg = buffer;
761 FOREACH_MOD OnPacketTransmit(msg,s);
762 strlcpy(buffer,msg.c_str(),MAXBUF);
764 for (int j = 0; j < 32; j++)
768 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
770 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
772 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
779 void NetSendToAllExcept_WithSum(const char* target,char* s,char* u)
784 snprintf(buffer,MAXBUF,"%s",s);
786 else snprintf(buffer,MAXBUF,":%s %s",u,s);
788 log(DEBUG,"NetSendToAllExcept: '%s' '%s'",target,s);
790 std::string msg = buffer;
791 FOREACH_MOD OnPacketTransmit(msg,s);
792 strlcpy(buffer,msg.c_str(),MAXBUF);
794 for (int j = 0; j < 32; j++)
798 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
800 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
802 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
810 void WriteMode(const char* modes, int flags, const char* text, ...)
812 if ((!text) || (!modes) || (!flags))
814 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
818 char textbuffer[MAXBUF];
820 va_start (argsPtr, text);
821 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
823 int modelen = strlen(modes);
825 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
827 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
829 bool send_to_user = false;
834 for (int n = 0; n < modelen; n++)
836 if (!hasumode(i->second,modes[n]))
838 send_to_user = false;
843 else if (flags == WM_OR)
845 send_to_user = false;
846 for (int n = 0; n < modelen; n++)
848 if (hasumode(i->second,modes[n]))
858 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
864 void NoticeAll(userrec *source, bool local_only, char* text, ...)
866 if ((!text) || (!source))
868 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
872 char textbuffer[MAXBUF];
874 va_start (argsPtr, text);
875 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
878 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
880 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
882 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
889 snprintf(buffer,MAXBUF,"V %s * :%s",source->nick,textbuffer);
890 NetSendToAll(buffer);
896 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
898 if ((!text) || (!source))
900 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
904 char textbuffer[MAXBUF];
906 va_start (argsPtr, text);
907 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
910 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
912 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
914 if (strchr(i->second->modes,'w'))
916 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
924 snprintf(buffer,MAXBUF,"@ %s :%s",source->nick,textbuffer);
925 NetSendToAll(buffer);
929 /* convert a string to lowercase. Note following special circumstances
930 * taken from RFC 1459. Many "official" server branches still hold to this
931 * rule so i will too;
933 * Because of IRC's scandanavian origin, the characters {}| are
934 * considered to be the lower case equivalents of the characters []\,
935 * respectively. This is a critical issue when determining the
936 * equivalence of two nicknames.
939 void strlower(char *n)
943 for (char* t = n; *t; t++)
944 *t = lowermap[(unsigned)*t];
948 /* Find a user record by nickname and return a pointer to it */
950 userrec* Find(std::string nick)
952 user_hash::iterator iter = clientlist.find(nick);
954 if (iter == clientlist.end())
955 /* Couldn't find it */
961 /* find a channel record by channel name and return a pointer to it */
963 chanrec* FindChan(const char* chan)
967 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
971 chan_hash::iterator iter = chanlist.find(chan);
973 if (iter == chanlist.end())
974 /* Couldn't find it */
981 long GetMaxBans(char* name)
984 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
986 ConfValue("banlist","chan",count,CM,&config_f);
989 ConfValue("banlist","limit",count,CM,&config_f);
996 void purge_empty_chans(userrec* u)
999 int go_again = 1, purge = 0;
1001 // firstly decrement the count on each channel
1002 for (int f = 0; f < MAXCHANS; f++)
1004 if (u->chans[f].channel)
1006 u->chans[f].channel->DelUser((char*)u);
1010 for (int i = 0; i < MAXCHANS; i++)
1012 if (u->chans[i].channel)
1014 if (!usercount(u->chans[i].channel))
1016 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1017 /* kill the record */
1018 if (i2 != chanlist.end())
1020 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1026 u->chans[i].channel = NULL;
1031 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1035 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1041 char scratch[MAXBUF];
1042 char sparam[MAXBUF];
1044 char* chanmodes(chanrec *chan)
1048 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1055 if (chan->binarymodes & CM_NOEXTERNAL)
1057 strlcat(scratch,"n",MAXMODES);
1059 if (chan->binarymodes & CM_TOPICLOCK)
1061 strlcat(scratch,"t",MAXMODES);
1065 strlcat(scratch,"k",MAXMODES);
1069 strlcat(scratch,"l",MAXMODES);
1071 if (chan->binarymodes & CM_INVITEONLY)
1073 strlcat(scratch,"i",MAXMODES);
1075 if (chan->binarymodes & CM_MODERATED)
1077 strlcat(scratch,"m",MAXMODES);
1079 if (chan->binarymodes & CM_SECRET)
1081 strlcat(scratch,"s",MAXMODES);
1083 if (chan->binarymodes & CM_PRIVATE)
1085 strlcat(scratch,"p",MAXMODES);
1089 strlcat(sparam," ",MAXBUF);
1090 strlcat(sparam,chan->key,MAXBUF);
1095 sprintf(foo," %lu",(unsigned long)chan->limit);
1096 strlcat(sparam,foo,MAXBUF);
1098 if (*chan->custom_modes)
1100 strlcat(scratch,chan->custom_modes,MAXMODES);
1101 for (int z = 0; chan->custom_modes[z] != 0; z++)
1103 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1106 strlcat(sparam," ",MAXBUF);
1107 strlcat(sparam,extparam.c_str(),MAXBUF);
1111 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
1112 strlcat(scratch,sparam,MAXMODES);
1117 /* compile a userlist of a channel into a string, each nick seperated by
1118 * spaces and op, voice etc status shown as @ and + */
1120 void userlist(userrec *user,chanrec *c)
1122 if ((!c) || (!user))
1124 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1128 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1130 std::vector<char*> *ulist = c->GetUsers();
1131 for (unsigned int i = 0; i < ulist->size(); i++)
1133 char* o = (*ulist)[i];
1134 userrec* otheruser = (userrec*)o;
1135 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1137 /* user is +i, and source not on the channel, does not show
1138 * nick in NAMES list */
1141 strlcat(list,cmode(otheruser,c),MAXBUF);
1142 strlcat(list,otheruser->nick,MAXBUF);
1143 strlcat(list," ",MAXBUF);
1144 if (strlen(list)>(480-NICKMAX))
1146 /* list overflowed into
1147 * multiple numerics */
1148 WriteServ(user->fd,"%s",list);
1149 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1152 /* if whats left in the list isnt empty, send it */
1153 if (list[strlen(list)-1] != ':')
1155 WriteServ(user->fd,"%s",list);
1159 /* return a count of the users on a specific channel accounting for
1160 * invisible users who won't increase the count. e.g. for /LIST */
1162 int usercount_i(chanrec *c)
1168 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1173 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1177 if (has_channel(i->second,c))
1179 if (isnick(i->second->nick))
1181 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1183 /* user is +i, and source not on the channel, does not show
1184 * nick in NAMES list */
1192 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1197 int usercount(chanrec *c)
1201 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
1204 int count = c->GetUserCounter();
1205 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
1210 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1212 char* Passwd(userrec *user)
1214 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1216 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1224 bool IsDenied(userrec *user)
1226 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1228 if (match(user->host,i->host) && (i->type == CC_DENY))
1239 /* sends out an error notice to all connected clients (not to be used
1242 void send_error(char *s)
1244 log(DEBUG,"send_error: %s",s);
1245 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1247 if (isnick(i->second->nick))
1249 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1253 // fix - unregistered connections receive ERROR, not NOTICE
1254 Write(i->second->fd,"ERROR :%s",s);
1259 void Error(int status)
1261 signal (SIGALRM, SIG_IGN);
1262 signal (SIGPIPE, SIG_IGN);
1263 signal (SIGTERM, SIG_IGN);
1264 signal (SIGABRT, SIG_IGN);
1265 signal (SIGSEGV, SIG_IGN);
1266 signal (SIGURG, SIG_IGN);
1267 signal (SIGKILL, SIG_IGN);
1268 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1269 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1273 // this function counts all users connected, wether they are registered or NOT.
1276 return clientlist.size();
1279 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1280 int registered_usercount(void)
1283 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1285 if ((i->second->fd) && (isnick(i->second->nick))) c++;
1290 int usercount_invisible(void)
1294 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1296 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1301 int usercount_opers(void)
1305 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1307 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1312 int usercount_unknown(void)
1316 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1318 if ((i->second->fd) && (i->second->registered != 7))
1324 long chancount(void)
1326 return chanlist.size();
1329 long count_servs(void)
1332 for (int i = 0; i < 32; i++)
1336 for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
1338 if (strcasecmp(j->GetServerName().c_str(),ServerName))
1348 long servercount(void)
1350 return count_servs()+1;
1356 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1358 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,ServerName))) c++;
1363 void ShowMOTD(userrec *user)
1366 std::string WholeMOTD = "";
1369 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1372 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", ServerName, user->nick, ServerName);
1373 WholeMOTD = WholeMOTD + buf;
1374 for (unsigned int i = 0; i != MOTD.size(); i++)
1376 snprintf(buf,65535,":%s 372 %s :- %s\r\n", ServerName, user->nick, MOTD[i].c_str());
1377 WholeMOTD = WholeMOTD + buf;
1379 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", ServerName, user->nick);
1380 WholeMOTD = WholeMOTD + buf;
1381 // only one write operation
1382 user->AddWriteBuf(WholeMOTD);
1383 statsSent += WholeMOTD.length();
1386 void ShowRULES(userrec *user)
1390 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1393 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
1394 for (unsigned int i = 0; i != RULES.size(); i++)
1396 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1398 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
1401 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1402 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1403 // registration timeout maximum seconds)
1404 bool AllModulesReportReady(userrec* user)
1406 for (int i = 0; i <= MODCOUNT; i++)
1408 int res = modules[i]->OnCheckReady(user);
1415 char islast(const char* s)
1418 for (int j = 0; j < 32; j++)
1422 for (unsigned int k = 0; k < me[j]->connectors.size(); k++)
1424 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
1428 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
1438 long map_count(const char* s)
1441 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1443 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,s))) c++;
1448 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1451 /* create the command and push it onto the table */
1452 strlcpy(comm.command,cmd,MAXBUF);
1453 strlcpy(comm.source,source,MAXBUF);
1454 comm.handler_function = f;
1455 comm.flags_needed = flags;
1456 comm.min_params = minparams;
1458 comm.total_bytes = 0;
1459 cmdlist.push_back(comm);
1460 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1464 void SetupCommandTable(void)
1466 createcommand("USER",handle_user,0,4,"<core>");
1467 createcommand("NICK",handle_nick,0,1,"<core>");
1468 createcommand("QUIT",handle_quit,0,0,"<core>");
1469 createcommand("VERSION",handle_version,0,0,"<core>");
1470 createcommand("PING",handle_ping,0,1,"<core>");
1471 createcommand("PONG",handle_pong,0,1,"<core>");
1472 createcommand("ADMIN",handle_admin,0,0,"<core>");
1473 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1474 createcommand("INFO",handle_info,0,0,"<core>");
1475 createcommand("TIME",handle_time,0,0,"<core>");
1476 createcommand("WHOIS",handle_whois,0,1,"<core>");
1477 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1478 createcommand("NOTICE",handle_notice,0,2,"<core>");
1479 createcommand("JOIN",handle_join,0,1,"<core>");
1480 createcommand("NAMES",handle_names,0,0,"<core>");
1481 createcommand("PART",handle_part,0,1,"<core>");
1482 createcommand("KICK",handle_kick,0,2,"<core>");
1483 createcommand("MODE",handle_mode,0,1,"<core>");
1484 createcommand("TOPIC",handle_topic,0,1,"<core>");
1485 createcommand("WHO",handle_who,0,1,"<core>");
1486 createcommand("MOTD",handle_motd,0,0,"<core>");
1487 createcommand("RULES",handle_rules,0,0,"<core>");
1488 createcommand("OPER",handle_oper,0,2,"<core>");
1489 createcommand("LIST",handle_list,0,0,"<core>");
1490 createcommand("DIE",handle_die,'o',1,"<core>");
1491 createcommand("RESTART",handle_restart,'o',1,"<core>");
1492 createcommand("KILL",handle_kill,'o',2,"<core>");
1493 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1494 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1495 createcommand("STATS",handle_stats,0,1,"<core>");
1496 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1497 createcommand("AWAY",handle_away,0,0,"<core>");
1498 createcommand("ISON",handle_ison,0,0,"<core>");
1499 createcommand("SUMMON",handle_summon,0,0,"<core>");
1500 createcommand("USERS",handle_users,0,0,"<core>");
1501 createcommand("INVITE",handle_invite,0,0,"<core>");
1502 createcommand("PASS",handle_pass,0,1,"<core>");
1503 createcommand("TRACE",handle_trace,'o',0,"<core>");
1504 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1505 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1506 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1507 createcommand("MODULES",handle_modules,0,0,"<core>");
1508 createcommand("LINKS",handle_links,0,0,"<core>");
1509 createcommand("MAP",handle_map,0,0,"<core>");
1510 createcommand("KLINE",handle_kline,'o',1,"<core>");
1511 createcommand("GLINE",handle_gline,'o',1,"<core>");
1512 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1513 createcommand("QLINE",handle_qline,'o',1,"<core>");
1514 createcommand("ELINE",handle_eline,'o',1,"<core>");
1515 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1516 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1517 createcommand("SERVER",handle_server,0,0,"<core>");
1518 createcommand("COMMANDS",handle_commands,0,0,"<core>");
1521 bool DirValid(char* dirandfile)
1524 strlcpy(work,dirandfile,MAXBUF);
1525 int p = strlen(work);
1526 // we just want the dir
1527 while (strlen(work))
1536 char buffer[MAXBUF], otherdir[MAXBUF];
1537 // Get the current working directory
1538 if( getcwd( buffer, MAXBUF ) == NULL )
1541 if( getcwd( otherdir, MAXBUF ) == NULL )
1544 if (strlen(otherdir) >= strlen(work))
1546 otherdir[strlen(work)] = '\0';
1547 if (!strcmp(otherdir,work))
1556 std::string GetFullProgDir(char** argv, int argc)
1559 strlcpy(work,argv[0],MAXBUF);
1560 int p = strlen(work);
1561 // we just want the dir
1562 while (strlen(work))
1571 char buffer[MAXBUF], otherdir[MAXBUF];
1572 // Get the current working directory
1573 if( getcwd( buffer, MAXBUF ) == NULL )
1576 if( getcwd( otherdir, MAXBUF ) == NULL )