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 * ---------------------------------------------------
20 #include "inspircd_io.h"
21 #include "inspircd_util.h"
22 #include "inspircd_config.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];
166 va_start (argsPtr, text);
167 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
169 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
171 if (fd_ref_table[sock])
174 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
175 fd_ref_table[sock]->AddWriteBuf(tb);
178 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
181 /* write a server formatted numeric response to a single socket */
183 void WriteServ(int sock, char* text, ...)
189 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
192 char textbuffer[MAXBUF],tb[MAXBUF];
195 va_start (argsPtr, text);
197 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
199 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",ServerName,textbuffer);
201 if (fd_ref_table[sock])
204 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
205 fd_ref_table[sock]->AddWriteBuf(tb);
208 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
211 /* write text from an originating user to originating user */
213 void WriteFrom(int sock, userrec *user,char* text, ...)
217 if ((!text) || (!user))
219 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
222 char textbuffer[MAXBUF],tb[MAXBUF];
225 va_start (argsPtr, text);
227 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
229 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
231 if (fd_ref_table[sock])
234 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
235 fd_ref_table[sock]->AddWriteBuf(tb);
238 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
241 /* write text to an destination user from a source user (e.g. user privmsg) */
243 void WriteTo(userrec *source, userrec *dest,char *data, ...)
245 if ((!dest) || (!data))
247 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
250 if (dest->fd == FD_MAGIC_NUMBER)
252 char textbuffer[MAXBUF],tb[MAXBUF];
254 va_start (argsPtr, data);
255 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
259 // if no source given send it from the server.
262 WriteServ(dest->fd,":%s %s",ServerName,textbuffer);
266 WriteFrom(dest->fd,source,"%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!) */
273 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
275 if ((!Ptr) || (!user) || (!text))
277 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
280 char textbuffer[MAXBUF];
282 va_start (argsPtr, text);
283 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
286 std::vector<char*> *ulist = Ptr->GetUsers();
287 for (int j = 0; j < ulist->size(); j++)
289 char* o = (*ulist)[j];
290 userrec* otheruser = (userrec*)o;
291 if (otheruser->fd != FD_MAGIC_NUMBER)
292 WriteTo(user,otheruser,"%s",textbuffer);
296 /* write formatted text from a source user to all users on a channel
297 * including the sender (NOT for privmsg, notice etc!) doesnt send to
298 * users on remote servers */
300 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
302 if ((!Ptr) || (!text))
304 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
307 char textbuffer[MAXBUF];
309 va_start (argsPtr, text);
310 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
313 std::vector<char*> *ulist = Ptr->GetUsers();
314 for (int j = 0; j < ulist->size(); j++)
316 char* o = (*ulist)[j];
317 userrec* otheruser = (userrec*)o;
318 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
322 WriteServ(otheruser->fd,"%s",textbuffer);
326 WriteTo(user,otheruser,"%s",textbuffer);
332 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
334 if ((!Ptr) || (!text))
336 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
339 char textbuffer[MAXBUF];
341 va_start (argsPtr, text);
342 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
346 std::vector<char*> *ulist = Ptr->GetUsers();
347 for (int j = 0; j < ulist->size(); j++)
349 char* o = (*ulist)[j];
350 userrec* otheruser = (userrec*)o;
351 if (otheruser->fd != FD_MAGIC_NUMBER)
352 WriteServ(otheruser->fd,"%s",textbuffer);
356 /* write formatted text from a source user to all users on a channel except
357 * for the sender (for privmsg etc) */
359 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
361 if ((!Ptr) || (!user) || (!text))
363 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
366 char textbuffer[MAXBUF];
368 va_start (argsPtr, text);
369 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
372 std::vector<char*> *ulist = Ptr->GetUsers();
373 for (int j = 0; j < ulist->size(); j++)
375 char* o = (*ulist)[j];
376 userrec* otheruser = (userrec*)o;
377 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
378 WriteFrom(otheruser->fd,user,"%s",textbuffer);
382 std::string GetServerDescription(char* servername)
384 for (int j = 0; j < 32; j++)
388 for (int k = 0; k < me[j]->connectors.size(); k++)
390 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),servername))
392 return me[j]->connectors[k].GetDescription();
396 return ServerDesc; // not a remote server that can be found, it must be me.
400 /* write a formatted string to all users who share at least one common
401 * channel, including the source user e.g. for use in NICK */
403 void WriteCommon(userrec *u, char* text, ...)
407 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
411 if (u->registered != 7) {
412 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
416 char textbuffer[MAXBUF];
418 va_start (argsPtr, text);
419 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
422 // FIX: Stops a message going to the same person more than once
423 bzero(&already_sent,65536);
425 bool sent_to_at_least_one = false;
427 for (int i = 0; i < MAXCHANS; i++)
429 if (u->chans[i].channel)
431 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
432 for (int j = 0; j < ulist->size(); j++)
434 char* o = (*ulist)[j];
435 userrec* otheruser = (userrec*)o;
436 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
438 already_sent[otheruser->fd] = 1;
439 WriteFrom(otheruser->fd,u,"%s",textbuffer);
440 sent_to_at_least_one = true;
445 // if the user was not in any channels, no users will receive the text. Make sure the user
446 // receives their OWN message for WriteCommon
447 if (!sent_to_at_least_one)
449 WriteFrom(u->fd,u,"%s",textbuffer);
453 /* write a formatted string to all users who share at least one common
454 * channel, NOT including the source user e.g. for use in QUIT */
456 void WriteCommonExcept(userrec *u, char* text, ...)
460 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
464 if (u->registered != 7) {
465 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
469 char textbuffer[MAXBUF];
471 va_start (argsPtr, text);
472 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
475 bzero(&already_sent,65536);
477 for (int i = 0; i < MAXCHANS; i++)
479 if (u->chans[i].channel)
481 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
482 for (int j = 0; j < ulist->size(); j++)
484 char* o = (*ulist)[j];
485 userrec* otheruser = (userrec*)o;
488 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
490 already_sent[otheruser->fd] = 1;
491 WriteFrom(otheruser->fd,u,"%s",textbuffer);
499 void WriteOpers(char* text, ...)
503 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
507 char textbuffer[MAXBUF];
509 va_start (argsPtr, text);
510 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
513 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
516 if ((a) && (a->fd != FD_MAGIC_NUMBER))
518 if (strchr(a->modes,'s'))
520 // send server notices to all with +s
521 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
527 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
529 if ((!text) || (!source))
531 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
535 char textbuffer[MAXBUF];
537 va_start (argsPtr, text);
538 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
541 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
544 if ((a) && (a->fd != FD_MAGIC_NUMBER))
546 if (strchr(a->modes,'s'))
548 // send server notices to all with +s
549 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
557 snprintf(buffer,MAXBUF,"V %s @* :%s",source->nick,textbuffer);
558 NetSendToAll(buffer);
561 // returns TRUE of any users on channel C occupy server 'servername'.
563 bool ChanAnyOnThisServer(chanrec *c,char* servername)
565 log(DEBUG,"ChanAnyOnThisServer");
567 std::vector<char*> *ulist = c->GetUsers();
568 for (int j = 0; j < ulist->size(); j++)
570 char* o = (*ulist)[j];
571 userrec* user = (userrec*)o;
572 if (!strcasecmp(user->server,servername))
578 // returns true if user 'u' shares any common channels with any users on server 'servername'
580 bool CommonOnThisServer(userrec* u,const char* servername)
582 log(DEBUG,"ChanAnyOnThisServer");
584 for (int i = 0; i < MAXCHANS; i++)
586 if (u->chans[i].channel)
588 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
589 for (int j = 0; j < ulist->size(); j++)
591 char* o = (*ulist)[j];
592 userrec* user = (userrec*)o;
593 if (!strcasecmp(user->server,servername))
601 void NetSendToCommon(userrec* u, char* s)
604 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
606 log(DEBUG,"NetSendToCommon: '%s' '%s'",u->nick,s);
608 std::string msg = buffer;
609 FOREACH_MOD OnPacketTransmit(msg,s);
610 strlcpy(buffer,msg.c_str(),MAXBUF);
612 for (int j = 0; j < 32; j++)
616 for (int k = 0; k < me[j]->connectors.size(); k++)
618 if (CommonOnThisServer(u,me[j]->connectors[k].GetServerName().c_str()))
620 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
628 void NetSendToAll(char* s)
631 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
633 log(DEBUG,"NetSendToAll: '%s'",s);
635 std::string msg = buffer;
636 FOREACH_MOD OnPacketTransmit(msg,s);
637 strlcpy(buffer,msg.c_str(),MAXBUF);
639 for (int j = 0; j < 32; j++)
643 for (int k = 0; k < me[j]->connectors.size(); k++)
645 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
652 void NetSendToAll_WithSum(char* s,char* u)
655 snprintf(buffer,MAXBUF,":%s %s",u,s);
657 log(DEBUG,"NetSendToAll: '%s'",s);
659 std::string msg = buffer;
660 FOREACH_MOD OnPacketTransmit(msg,s);
661 strlcpy(buffer,msg.c_str(),MAXBUF);
663 for (int j = 0; j < 32; j++)
667 for (int k = 0; k < me[j]->connectors.size(); k++)
669 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
675 void NetSendToAllAlive(char* s)
678 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
680 log(DEBUG,"NetSendToAllAlive: '%s'",s);
682 std::string msg = buffer;
683 FOREACH_MOD OnPacketTransmit(msg,s);
684 strlcpy(buffer,msg.c_str(),MAXBUF);
686 for (int j = 0; j < 32; j++)
690 for (int k = 0; k < me[j]->connectors.size(); k++)
692 if (me[j]->connectors[k].GetState() != STATE_DISCONNECTED)
694 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
698 log(DEBUG,"%s is dead, not sending to it.",me[j]->connectors[k].GetServerName().c_str());
706 void NetSendToOne(char* target,char* s)
709 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
711 log(DEBUG,"NetSendToOne: '%s' '%s'",target,s);
713 std::string msg = buffer;
714 FOREACH_MOD OnPacketTransmit(msg,s);
715 strlcpy(buffer,msg.c_str(),MAXBUF);
717 for (int j = 0; j < 32; j++)
721 for (int k = 0; k < me[j]->connectors.size(); k++)
723 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
725 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
732 void NetSendToAllExcept(const char* target,char* s)
735 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
737 log(DEBUG,"NetSendToAllExcept: '%s' '%s'",target,s);
739 std::string msg = buffer;
740 FOREACH_MOD OnPacketTransmit(msg,s);
741 strlcpy(buffer,msg.c_str(),MAXBUF);
743 for (int j = 0; j < 32; j++)
747 for (int k = 0; k < me[j]->connectors.size(); k++)
749 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
751 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
758 void NetSendToAllExcept_WithSum(const char* target,char* s,char* u)
761 snprintf(buffer,MAXBUF,":%s %s",u,s);
763 log(DEBUG,"NetSendToAllExcept: '%s' '%s'",target,s);
765 std::string msg = buffer;
766 FOREACH_MOD OnPacketTransmit(msg,s);
767 strlcpy(buffer,msg.c_str(),MAXBUF);
769 for (int j = 0; j < 32; j++)
773 for (int k = 0; k < me[j]->connectors.size(); k++)
775 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
777 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
785 void WriteMode(const char* modes, int flags, const char* text, ...)
787 if ((!text) || (!modes) || (!flags))
789 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
793 char textbuffer[MAXBUF];
795 va_start (argsPtr, text);
796 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
798 int modelen = strlen(modes);
800 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
802 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
804 bool send_to_user = false;
809 for (int n = 0; n < modelen; n++)
811 if (!hasumode(i->second,modes[n]))
813 send_to_user = false;
818 else if (flags == WM_OR)
820 send_to_user = false;
821 for (int n = 0; n < modelen; n++)
823 if (hasumode(i->second,modes[n]))
833 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
839 void NoticeAll(userrec *source, bool local_only, char* text, ...)
841 if ((!text) || (!source))
843 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
847 char textbuffer[MAXBUF];
849 va_start (argsPtr, text);
850 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
853 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
855 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
857 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
864 snprintf(buffer,MAXBUF,"V %s * :%s",source->nick,textbuffer);
865 NetSendToAll(buffer);
871 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
873 if ((!text) || (!source))
875 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
879 char textbuffer[MAXBUF];
881 va_start (argsPtr, text);
882 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
885 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
887 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
889 if (strchr(i->second->modes,'w'))
891 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
899 snprintf(buffer,MAXBUF,"@ %s :%s",source->nick,textbuffer);
900 NetSendToAll(buffer);
904 /* convert a string to lowercase. Note following special circumstances
905 * taken from RFC 1459. Many "official" server branches still hold to this
906 * rule so i will too;
908 * Because of IRC's scandanavian origin, the characters {}| are
909 * considered to be the lower case equivalents of the characters []\,
910 * respectively. This is a critical issue when determining the
911 * equivalence of two nicknames.
914 void strlower(char *n)
918 for (char* t = n; *t; t++)
923 /* Find a user record by nickname and return a pointer to it */
925 userrec* Find(std::string nick)
927 user_hash::iterator iter = clientlist.find(nick);
929 if (iter == clientlist.end())
930 /* Couldn't find it */
936 /* find a channel record by channel name and return a pointer to it */
938 chanrec* FindChan(const char* chan)
942 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
946 chan_hash::iterator iter = chanlist.find(chan);
948 if (iter == chanlist.end())
949 /* Couldn't find it */
956 long GetMaxBans(char* name)
959 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
961 ConfValue("banlist","chan",count,CM,&config_f);
964 ConfValue("banlist","limit",count,CM,&config_f);
971 void purge_empty_chans(userrec* u)
974 int go_again = 1, purge = 0;
976 // firstly decrement the count on each channel
977 for (int f = 0; f < MAXCHANS; f++)
979 if (u->chans[f].channel)
981 u->chans[f].channel->DelUser((char*)u);
985 for (int i = 0; i < MAXCHANS; i++)
987 if (u->chans[i].channel)
989 if (!usercount(u->chans[i].channel))
991 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
992 /* kill the record */
993 if (i2 != chanlist.end())
995 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1001 u->chans[i].channel = NULL;
1006 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1010 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1016 char scratch[MAXBUF];
1017 char sparam[MAXBUF];
1019 char* chanmodes(chanrec *chan)
1023 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1030 if (chan->binarymodes & CM_NOEXTERNAL)
1032 strlcat(scratch,"n",MAXMODES);
1034 if (chan->binarymodes & CM_TOPICLOCK)
1036 strlcat(scratch,"t",MAXMODES);
1040 strlcat(scratch,"k",MAXMODES);
1044 strlcat(scratch,"l",MAXMODES);
1046 if (chan->binarymodes & CM_INVITEONLY)
1048 strlcat(scratch,"i",MAXMODES);
1050 if (chan->binarymodes & CM_MODERATED)
1052 strlcat(scratch,"m",MAXMODES);
1054 if (chan->binarymodes & CM_SECRET)
1056 strlcat(scratch,"s",MAXMODES);
1058 if (chan->binarymodes & CM_PRIVATE)
1060 strlcat(scratch,"p",MAXMODES);
1064 strlcat(sparam," ",MAXBUF);
1065 strlcat(sparam,chan->key,MAXBUF);
1070 sprintf(foo," %lu",(unsigned long)chan->limit);
1071 strlcat(sparam,foo,MAXBUF);
1073 if (*chan->custom_modes)
1075 strlcat(scratch,chan->custom_modes,MAXMODES);
1076 for (int z = 0; chan->custom_modes[z] != 0; z++)
1078 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1081 strlcat(sparam," ",MAXBUF);
1082 strlcat(sparam,extparam.c_str(),MAXBUF);
1086 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
1087 strlcat(scratch,sparam,MAXMODES);
1092 /* compile a userlist of a channel into a string, each nick seperated by
1093 * spaces and op, voice etc status shown as @ and + */
1095 void userlist(userrec *user,chanrec *c)
1097 if ((!c) || (!user))
1099 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1103 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1105 std::vector<char*> *ulist = c->GetUsers();
1106 for (int i = 0; i < ulist->size(); i++)
1108 char* o = (*ulist)[i];
1109 userrec* otheruser = (userrec*)o;
1110 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1112 /* user is +i, and source not on the channel, does not show
1113 * nick in NAMES list */
1116 strlcat(list,cmode(otheruser,c),MAXBUF);
1117 strlcat(list,otheruser->nick,MAXBUF);
1118 strlcat(list," ",MAXBUF);
1119 if (strlen(list)>(480-NICKMAX))
1121 /* list overflowed into
1122 * multiple numerics */
1123 WriteServ(user->fd,"%s",list);
1124 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1127 /* if whats left in the list isnt empty, send it */
1128 if (list[strlen(list)-1] != ':')
1130 WriteServ(user->fd,"%s",list);
1134 /* return a count of the users on a specific channel accounting for
1135 * invisible users who won't increase the count. e.g. for /LIST */
1137 int usercount_i(chanrec *c)
1143 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1148 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1152 if (has_channel(i->second,c))
1154 if (isnick(i->second->nick))
1156 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1158 /* user is +i, and source not on the channel, does not show
1159 * nick in NAMES list */
1167 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1172 int usercount(chanrec *c)
1176 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
1179 int count = c->GetUserCounter();
1180 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
1185 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1187 char* Passwd(userrec *user)
1189 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1191 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1199 bool IsDenied(userrec *user)
1201 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1203 if (match(user->host,i->host) && (i->type == CC_DENY))
1214 /* sends out an error notice to all connected clients (not to be used
1217 void send_error(char *s)
1219 log(DEBUG,"send_error: %s",s);
1220 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1222 if (isnick(i->second->nick))
1224 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1228 // fix - unregistered connections receive ERROR, not NOTICE
1229 Write(i->second->fd,"ERROR :%s",s);
1234 void Error(int status)
1236 signal (SIGALRM, SIG_IGN);
1237 signal (SIGPIPE, SIG_IGN);
1238 signal (SIGTERM, SIG_IGN);
1239 signal (SIGABRT, SIG_IGN);
1240 signal (SIGSEGV, SIG_IGN);
1241 signal (SIGURG, SIG_IGN);
1242 signal (SIGKILL, SIG_IGN);
1243 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1244 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1248 // this function counts all users connected, wether they are registered or NOT.
1251 return clientlist.size();
1254 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1255 int registered_usercount(void)
1258 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1260 if ((i->second->fd) && (isnick(i->second->nick))) c++;
1265 int usercount_invisible(void)
1269 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1271 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
1276 int usercount_opers(void)
1280 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1282 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1287 int usercount_unknown(void)
1291 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1293 if ((i->second->fd) && (i->second->registered != 7))
1299 long chancount(void)
1301 return chanlist.size();
1304 long count_servs(void)
1307 for (int i = 0; i < 32; i++)
1311 for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
1313 if (strcasecmp(j->GetServerName().c_str(),ServerName))
1323 long servercount(void)
1325 return count_servs()+1;
1331 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1333 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,ServerName))) c++;
1338 void ShowMOTD(userrec *user)
1341 std::string WholeMOTD = "";
1344 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1347 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", ServerName, user->nick, ServerName);
1348 WholeMOTD = WholeMOTD + buf;
1349 for (int i = 0; i != MOTD.size(); i++)
1351 snprintf(buf,65535,":%s 372 %s :- %s\r\n", ServerName, user->nick, MOTD[i].c_str());
1352 WholeMOTD = WholeMOTD + buf;
1354 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", ServerName, user->nick);
1355 WholeMOTD = WholeMOTD + buf;
1356 // only one write operation
1357 user->AddWriteBuf(WholeMOTD);
1358 statsSent += WholeMOTD.length();
1361 void ShowRULES(userrec *user)
1365 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1368 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
1369 for (int i = 0; i != RULES.size(); i++)
1371 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1373 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
1376 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1377 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1378 // registration timeout maximum seconds)
1379 bool AllModulesReportReady(userrec* user)
1381 for (int i = 0; i <= MODCOUNT; i++)
1383 int res = modules[i]->OnCheckReady(user);
1390 char islast(const char* s)
1393 for (int j = 0; j < 32; j++)
1397 for (int k = 0; k < me[j]->connectors.size(); k++)
1399 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
1403 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
1413 long map_count(const char* s)
1416 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1418 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,s))) c++;
1423 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1426 /* create the command and push it onto the table */
1427 strlcpy(comm.command,cmd,MAXBUF);
1428 strlcpy(comm.source,source,MAXBUF);
1429 comm.handler_function = f;
1430 comm.flags_needed = flags;
1431 comm.min_params = minparams;
1433 comm.total_bytes = 0;
1434 cmdlist.push_back(comm);
1435 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1439 void SetupCommandTable(void)
1441 createcommand("USER",handle_user,0,4,"<core>");
1442 createcommand("NICK",handle_nick,0,1,"<core>");
1443 createcommand("QUIT",handle_quit,0,0,"<core>");
1444 createcommand("VERSION",handle_version,0,0,"<core>");
1445 createcommand("PING",handle_ping,0,1,"<core>");
1446 createcommand("PONG",handle_pong,0,1,"<core>");
1447 createcommand("ADMIN",handle_admin,0,0,"<core>");
1448 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1449 createcommand("INFO",handle_info,0,0,"<core>");
1450 createcommand("TIME",handle_time,0,0,"<core>");
1451 createcommand("WHOIS",handle_whois,0,1,"<core>");
1452 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1453 createcommand("NOTICE",handle_notice,0,2,"<core>");
1454 createcommand("JOIN",handle_join,0,1,"<core>");
1455 createcommand("NAMES",handle_names,0,0,"<core>");
1456 createcommand("PART",handle_part,0,1,"<core>");
1457 createcommand("KICK",handle_kick,0,2,"<core>");
1458 createcommand("MODE",handle_mode,0,1,"<core>");
1459 createcommand("TOPIC",handle_topic,0,1,"<core>");
1460 createcommand("WHO",handle_who,0,1,"<core>");
1461 createcommand("MOTD",handle_motd,0,0,"<core>");
1462 createcommand("RULES",handle_rules,0,0,"<core>");
1463 createcommand("OPER",handle_oper,0,2,"<core>");
1464 createcommand("LIST",handle_list,0,0,"<core>");
1465 createcommand("DIE",handle_die,'o',1,"<core>");
1466 createcommand("RESTART",handle_restart,'o',1,"<core>");
1467 createcommand("KILL",handle_kill,'o',2,"<core>");
1468 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1469 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1470 createcommand("STATS",handle_stats,0,1,"<core>");
1471 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1472 createcommand("AWAY",handle_away,0,0,"<core>");
1473 createcommand("ISON",handle_ison,0,0,"<core>");
1474 createcommand("SUMMON",handle_summon,0,0,"<core>");
1475 createcommand("USERS",handle_users,0,0,"<core>");
1476 createcommand("INVITE",handle_invite,0,0,"<core>");
1477 createcommand("PASS",handle_pass,0,1,"<core>");
1478 createcommand("TRACE",handle_trace,'o',0,"<core>");
1479 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1480 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1481 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1482 createcommand("MODULES",handle_modules,0,0,"<core>");
1483 createcommand("LINKS",handle_links,0,0,"<core>");
1484 createcommand("MAP",handle_map,0,0,"<core>");
1485 createcommand("KLINE",handle_kline,'o',1,"<core>");
1486 createcommand("GLINE",handle_gline,'o',1,"<core>");
1487 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1488 createcommand("QLINE",handle_qline,'o',1,"<core>");
1489 createcommand("ELINE",handle_eline,'o',1,"<core>");
1490 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1491 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1492 createcommand("SERVER",handle_server,0,0,"<core>");
1495 bool DirValid(char* dirandfile)
1498 strlcpy(work,dirandfile,MAXBUF);
1499 int p = strlen(work);
1500 // we just want the dir
1501 while (strlen(work))
1510 char buffer[MAXBUF], otherdir[MAXBUF];
1511 // Get the current working directory
1512 if( getcwd( buffer, MAXBUF ) == NULL )
1515 if( getcwd( otherdir, MAXBUF ) == NULL )
1518 if (strlen(otherdir) >= strlen(work))
1520 otherdir[strlen(work)] = '\0';
1521 if (!strcmp(otherdir,work))
1530 std::string GetFullProgDir(char** argv, int argc)
1533 strlcpy(work,argv[0],MAXBUF);
1534 int p = strlen(work);
1535 // we just want the dir
1536 while (strlen(work))
1545 char buffer[MAXBUF], otherdir[MAXBUF];
1546 // Get the current working directory
1547 if( getcwd( buffer, MAXBUF ) == NULL )
1550 if( getcwd( otherdir, MAXBUF ) == NULL )