2 #include "inspircd_io.h"
3 #include "inspircd_util.h"
4 #include "inspircd_config.h"
9 #include <sys/utsname.h>
11 #include <sys/types.h>
12 #include <sys/event.h>
19 #include <ext/hash_map>
31 #include "connection.h"
43 #include "inspstring.h"
44 #include "helperfuncs.h"
54 extern std::vector<Module*> modules;
55 extern std::vector<ircd_module*> factory;
61 extern char lowermap[255];
64 extern char ServerName[MAXBUF];
65 extern char Network[MAXBUF];
66 extern char ServerDesc[MAXBUF];
67 extern char AdminName[MAXBUF];
68 extern char AdminEmail[MAXBUF];
69 extern char AdminNick[MAXBUF];
70 extern char diepass[MAXBUF];
71 extern char restartpass[MAXBUF];
72 extern char motd[MAXBUF];
73 extern char rules[MAXBUF];
74 extern char list[MAXBUF];
75 extern char PrefixQuit[MAXBUF];
76 extern char DieValue[MAXBUF];
79 extern int WHOWAS_STALE;
80 extern int WHOWAS_MAX;
82 extern time_t startup_time;
83 extern int NetBufferSize;
84 extern int MaxWhoResults;
85 extern time_t nb_start;
87 extern std::vector<int> fd_reap;
88 extern std::vector<std::string> module_names;
90 extern int boundPortCount;
92 extern int SERVERportCount;
93 extern int ports[MAXSOCKS];
94 extern int defaultRoute;
96 extern std::vector<long> auth_cookies;
97 extern std::stringstream config_f;
99 extern serverrec* me[32];
101 extern FILE *log_file;
102 extern userrec* fd_ref_table[65536];
104 extern int statsAccept, statsRefused, statsUnknown, statsCollisions, statsDns, statsDnsGood, statsDnsBad, statsConnects, statsSent, statsRecv;
106 static char already_sent[65536];
107 extern std::vector<userrec*> all_opers;
109 extern ClassVector Classes;
112 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
113 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
114 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
115 typedef nspace::hash_map<std::string, WhoWasUser*, nspace::hash<string>, StrHashComp> whowas_hash;
116 typedef std::deque<command_t> command_table;
119 extern user_hash clientlist;
120 extern chan_hash chanlist;
121 extern whowas_hash whowas;
122 extern command_table cmdlist;
123 extern file_cache MOTD;
124 extern file_cache RULES;
125 extern address_cache IP;
127 void log(int level,char *text, ...)
129 char textbuffer[MAXBUF];
132 struct tm * timeinfo;
133 if (level < LogLevel)
137 timeinfo = localtime (&rawtime);
142 va_start (argsPtr, text);
143 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
145 strlcpy(b,asctime(timeinfo),MAXBUF);
146 b[24] = ':'; // we know this is the end of the time string
147 fprintf(log_file,"%s %s\n",b,textbuffer);
150 // nofork enabled? display it on terminal too
151 printf("%s %s\n",b,textbuffer);
156 void readfile(file_cache &F, const char* fname)
159 char linebuf[MAXBUF];
161 log(DEBUG,"readfile: loading %s",fname);
163 file = fopen(fname,"r");
168 fgets(linebuf,sizeof(linebuf),file);
169 linebuf[strlen(linebuf)-1]='\0';
176 F.push_back(linebuf);
183 log(DEBUG,"readfile: failed to load file: %s",fname);
185 log(DEBUG,"readfile: loaded %s, %lu lines",fname,(unsigned long)F.size());
188 void Write(int sock,char *text, ...)
194 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
197 char textbuffer[MAXBUF];
202 va_start (argsPtr, text);
203 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
205 int bytes = snprintf(tb,MAXBUF,"%s\r\n",textbuffer);
207 if (fd_ref_table[sock])
210 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
211 fd_ref_table[sock]->AddWriteBuf(tb);
214 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
217 /* write a server formatted numeric response to a single socket */
219 void WriteServ(int sock, char* text, ...)
225 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
228 char textbuffer[MAXBUF],tb[MAXBUF];
231 va_start (argsPtr, text);
233 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
235 int bytes = snprintf(tb,MAXBUF,":%s %s\r\n",ServerName,textbuffer);
237 if (fd_ref_table[sock])
240 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
241 fd_ref_table[sock]->AddWriteBuf(tb);
244 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
247 /* write text from an originating user to originating user */
249 void WriteFrom(int sock, userrec *user,char* text, ...)
253 if ((!text) || (!user))
255 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
258 char textbuffer[MAXBUF],tb[MAXBUF];
261 va_start (argsPtr, text);
263 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
265 int bytes = snprintf(tb,MAXBUF,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
267 if (fd_ref_table[sock])
270 FOREACH_RESULT(OnRawSocketWrite(sock,tb,bytes));
271 fd_ref_table[sock]->AddWriteBuf(tb);
274 else log(DEFAULT,"ERROR! attempted write to a user with no fd_ref_table entry!!!");
277 /* write text to an destination user from a source user (e.g. user privmsg) */
279 void WriteTo(userrec *source, userrec *dest,char *data, ...)
281 if ((!dest) || (!data))
283 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
286 if (dest->fd == FD_MAGIC_NUMBER)
288 char textbuffer[MAXBUF],tb[MAXBUF];
290 va_start (argsPtr, data);
291 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
295 // if no source given send it from the server.
298 WriteServ(dest->fd,":%s %s",ServerName,textbuffer);
302 WriteFrom(dest->fd,source,"%s",textbuffer);
306 /* write formatted text from a source user to all users on a channel
307 * including the sender (NOT for privmsg, notice etc!) */
309 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
311 if ((!Ptr) || (!user) || (!text))
313 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
316 char textbuffer[MAXBUF];
318 va_start (argsPtr, text);
319 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
322 std::vector<char*> *ulist = Ptr->GetUsers();
323 for (int j = 0; j < ulist->size(); j++)
325 char* o = (*ulist)[j];
326 userrec* otheruser = (userrec*)o;
327 if (otheruser->fd != FD_MAGIC_NUMBER)
328 WriteTo(user,otheruser,"%s",textbuffer);
332 /* write formatted text from a source user to all users on a channel
333 * including the sender (NOT for privmsg, notice etc!) doesnt send to
334 * users on remote servers */
336 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
338 if ((!Ptr) || (!text))
340 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
343 char textbuffer[MAXBUF];
345 va_start (argsPtr, text);
346 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
349 std::vector<char*> *ulist = Ptr->GetUsers();
350 for (int j = 0; j < ulist->size(); j++)
352 char* o = (*ulist)[j];
353 userrec* otheruser = (userrec*)o;
354 if ((otheruser->fd != FD_MAGIC_NUMBER) && (otheruser->fd != -1) && (otheruser != user))
358 WriteServ(otheruser->fd,"%s",textbuffer);
362 WriteTo(user,otheruser,"%s",textbuffer);
368 void WriteChannelWithServ(char* ServName, chanrec* Ptr, char* text, ...)
370 if ((!Ptr) || (!text))
372 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
375 char textbuffer[MAXBUF];
377 va_start (argsPtr, text);
378 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
382 std::vector<char*> *ulist = Ptr->GetUsers();
383 for (int j = 0; j < ulist->size(); j++)
385 char* o = (*ulist)[j];
386 userrec* otheruser = (userrec*)o;
387 if (otheruser->fd != FD_MAGIC_NUMBER)
388 WriteServ(otheruser->fd,"%s",textbuffer);
392 /* write formatted text from a source user to all users on a channel except
393 * for the sender (for privmsg etc) */
395 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
397 if ((!Ptr) || (!user) || (!text))
399 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
402 char textbuffer[MAXBUF];
404 va_start (argsPtr, text);
405 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
408 std::vector<char*> *ulist = Ptr->GetUsers();
409 for (int j = 0; j < ulist->size(); j++)
411 char* o = (*ulist)[j];
412 userrec* otheruser = (userrec*)o;
413 if ((otheruser->fd != FD_MAGIC_NUMBER) && (user != otheruser))
414 WriteFrom(otheruser->fd,user,"%s",textbuffer);
418 std::string GetServerDescription(char* servername)
420 for (int j = 0; j < 32; j++)
424 for (int k = 0; k < me[j]->connectors.size(); k++)
426 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),servername))
428 return me[j]->connectors[k].GetDescription();
432 return ServerDesc; // not a remote server that can be found, it must be me.
436 /* write a formatted string to all users who share at least one common
437 * channel, including the source user e.g. for use in NICK */
439 void WriteCommon(userrec *u, char* text, ...)
443 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
447 if (u->registered != 7) {
448 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
452 char textbuffer[MAXBUF];
454 va_start (argsPtr, text);
455 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
458 // FIX: Stops a message going to the same person more than once
459 bzero(&already_sent,65536);
461 bool sent_to_at_least_one = false;
463 for (int i = 0; i < MAXCHANS; i++)
465 if (u->chans[i].channel)
467 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
468 for (int j = 0; j < ulist->size(); j++)
470 char* o = (*ulist)[j];
471 userrec* otheruser = (userrec*)o;
472 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
474 already_sent[otheruser->fd] = 1;
475 WriteFrom(otheruser->fd,u,"%s",textbuffer);
476 sent_to_at_least_one = true;
481 // if the user was not in any channels, no users will receive the text. Make sure the user
482 // receives their OWN message for WriteCommon
483 if (!sent_to_at_least_one)
485 WriteFrom(u->fd,u,"%s",textbuffer);
489 /* write a formatted string to all users who share at least one common
490 * channel, NOT including the source user e.g. for use in QUIT */
492 void WriteCommonExcept(userrec *u, char* text, ...)
496 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
500 if (u->registered != 7) {
501 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
505 char textbuffer[MAXBUF];
507 va_start (argsPtr, text);
508 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
511 bzero(&already_sent,65536);
513 for (int i = 0; i < MAXCHANS; i++)
515 if (u->chans[i].channel)
517 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
518 for (int j = 0; j < ulist->size(); j++)
520 char* o = (*ulist)[j];
521 userrec* otheruser = (userrec*)o;
524 if ((otheruser->fd > 0) && (!already_sent[otheruser->fd]))
526 already_sent[otheruser->fd] = 1;
527 WriteFrom(otheruser->fd,u,"%s",textbuffer);
535 void WriteOpers(char* text, ...)
539 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
543 char textbuffer[MAXBUF];
545 va_start (argsPtr, text);
546 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
549 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
552 if ((a) && (a->fd != FD_MAGIC_NUMBER))
554 if (strchr(a->modes,'s'))
556 // send server notices to all with +s
557 WriteServ(a->fd,"NOTICE %s :%s",a->nick,textbuffer);
563 void NoticeAllOpers(userrec *source, bool local_only, char* text, ...)
565 if ((!text) || (!source))
567 log(DEFAULT,"*** BUG *** NoticeAllOpers was given an invalid parameter");
571 char textbuffer[MAXBUF];
573 va_start (argsPtr, text);
574 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
577 for (std::vector<userrec*>::iterator i = all_opers.begin(); i != all_opers.end(); i++)
580 if ((a) && (a->fd != FD_MAGIC_NUMBER))
582 if (strchr(a->modes,'s'))
584 // send server notices to all with +s
585 WriteServ(a->fd,"NOTICE %s :*** Notice From %s: %s",a->nick,source->nick,textbuffer);
593 snprintf(buffer,MAXBUF,"V %s @* :%s",source->nick,textbuffer);
594 NetSendToAll(buffer);
597 // returns TRUE of any users on channel C occupy server 'servername'.
599 bool ChanAnyOnThisServer(chanrec *c,char* servername)
601 log(DEBUG,"ChanAnyOnThisServer");
603 std::vector<char*> *ulist = c->GetUsers();
604 for (int j = 0; j < ulist->size(); j++)
606 char* o = (*ulist)[j];
607 userrec* user = (userrec*)o;
608 if (!strcasecmp(user->server,servername))
614 // returns true if user 'u' shares any common channels with any users on server 'servername'
616 bool CommonOnThisServer(userrec* u,const char* servername)
618 log(DEBUG,"ChanAnyOnThisServer");
620 for (int i = 0; i < MAXCHANS; i++)
622 if (u->chans[i].channel)
624 std::vector<char*> *ulist = u->chans[i].channel->GetUsers();
625 for (int j = 0; j < ulist->size(); j++)
627 char* o = (*ulist)[j];
628 userrec* user = (userrec*)o;
629 if (!strcasecmp(user->server,servername))
637 void NetSendToCommon(userrec* u, char* s)
640 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
642 log(DEBUG,"NetSendToCommon: '%s' '%s'",u->nick,s);
644 std::string msg = buffer;
645 FOREACH_MOD OnPacketTransmit(msg,s);
646 strlcpy(buffer,msg.c_str(),MAXBUF);
648 for (int j = 0; j < 32; j++)
652 for (int k = 0; k < me[j]->connectors.size(); k++)
654 if (CommonOnThisServer(u,me[j]->connectors[k].GetServerName().c_str()))
656 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
664 void NetSendToAll(char* s)
667 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
669 log(DEBUG,"NetSendToAll: '%s'",s);
671 std::string msg = buffer;
672 FOREACH_MOD OnPacketTransmit(msg,s);
673 strlcpy(buffer,msg.c_str(),MAXBUF);
675 for (int j = 0; j < 32; j++)
679 for (int k = 0; k < me[j]->connectors.size(); k++)
681 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
688 void NetSendToAll_WithSum(char* s,char* u)
691 snprintf(buffer,MAXBUF,":%s %s",u,s);
693 log(DEBUG,"NetSendToAll: '%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 (int k = 0; k < me[j]->connectors.size(); k++)
705 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
711 void NetSendToAllAlive(char* s)
714 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
716 log(DEBUG,"NetSendToAllAlive: '%s'",s);
718 std::string msg = buffer;
719 FOREACH_MOD OnPacketTransmit(msg,s);
720 strlcpy(buffer,msg.c_str(),MAXBUF);
722 for (int j = 0; j < 32; j++)
726 for (int k = 0; k < me[j]->connectors.size(); k++)
728 if (me[j]->connectors[k].GetState() != STATE_DISCONNECTED)
730 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
734 log(DEBUG,"%s is dead, not sending to it.",me[j]->connectors[k].GetServerName().c_str());
742 void NetSendToOne(char* target,char* s)
745 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
747 log(DEBUG,"NetSendToOne: '%s' '%s'",target,s);
749 std::string msg = buffer;
750 FOREACH_MOD OnPacketTransmit(msg,s);
751 strlcpy(buffer,msg.c_str(),MAXBUF);
753 for (int j = 0; j < 32; j++)
757 for (int k = 0; k < me[j]->connectors.size(); k++)
759 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
761 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
768 void NetSendToAllExcept(const char* target,char* s)
771 snprintf(buffer,MAXBUF,"%s %s",CreateSum().c_str(),s);
773 log(DEBUG,"NetSendToAllExcept: '%s' '%s'",target,s);
775 std::string msg = buffer;
776 FOREACH_MOD OnPacketTransmit(msg,s);
777 strlcpy(buffer,msg.c_str(),MAXBUF);
779 for (int j = 0; j < 32; j++)
783 for (int k = 0; k < me[j]->connectors.size(); k++)
785 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
787 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
794 void NetSendToAllExcept_WithSum(const char* target,char* s,char* u)
797 snprintf(buffer,MAXBUF,":%s %s",u,s);
799 log(DEBUG,"NetSendToAllExcept: '%s' '%s'",target,s);
801 std::string msg = buffer;
802 FOREACH_MOD OnPacketTransmit(msg,s);
803 strlcpy(buffer,msg.c_str(),MAXBUF);
805 for (int j = 0; j < 32; j++)
809 for (int k = 0; k < me[j]->connectors.size(); k++)
811 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),target))
813 me[j]->SendPacket(buffer,me[j]->connectors[k].GetServerName().c_str());
821 void WriteMode(const char* modes, int flags, const char* text, ...)
823 if ((!text) || (!modes) || (!flags))
825 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
829 char textbuffer[MAXBUF];
831 va_start (argsPtr, text);
832 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
834 int modelen = strlen(modes);
836 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
838 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
840 bool send_to_user = false;
845 for (int n = 0; n < modelen; n++)
847 if (!hasumode(i->second,modes[n]))
849 send_to_user = false;
854 else if (flags == WM_OR)
856 send_to_user = false;
857 for (int n = 0; n < modelen; n++)
859 if (hasumode(i->second,modes[n]))
869 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
875 void NoticeAll(userrec *source, bool local_only, char* text, ...)
877 if ((!text) || (!source))
879 log(DEFAULT,"*** BUG *** NoticeAll was given an invalid parameter");
883 char textbuffer[MAXBUF];
885 va_start (argsPtr, text);
886 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
889 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
891 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
893 WriteFrom(i->second->fd,source,"NOTICE $* :%s",textbuffer);
900 snprintf(buffer,MAXBUF,"V %s * :%s",source->nick,textbuffer);
901 NetSendToAll(buffer);
907 void WriteWallOps(userrec *source, bool local_only, char* text, ...)
909 if ((!text) || (!source))
911 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
915 char textbuffer[MAXBUF];
917 va_start (argsPtr, text);
918 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
921 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
923 if ((i->second) && (i->second->fd != FD_MAGIC_NUMBER))
925 if (strchr(i->second->modes,'w'))
927 WriteTo(source,i->second,"WALLOPS :%s",textbuffer);
935 snprintf(buffer,MAXBUF,"@ %s :%s",source->nick,textbuffer);
936 NetSendToAll(buffer);
940 /* convert a string to lowercase. Note following special circumstances
941 * taken from RFC 1459. Many "official" server branches still hold to this
942 * rule so i will too;
944 * Because of IRC's scandanavian origin, the characters {}| are
945 * considered to be the lower case equivalents of the characters []\,
946 * respectively. This is a critical issue when determining the
947 * equivalence of two nicknames.
950 void strlower(char *n)
954 for (char* t = n; *t; t++)
959 /* Find a user record by nickname and return a pointer to it */
961 userrec* Find(std::string nick)
963 user_hash::iterator iter = clientlist.find(nick);
965 if (iter == clientlist.end())
966 /* Couldn't find it */
972 /* find a channel record by channel name and return a pointer to it */
974 chanrec* FindChan(const char* chan)
978 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
982 chan_hash::iterator iter = chanlist.find(chan);
984 if (iter == chanlist.end())
985 /* Couldn't find it */
992 long GetMaxBans(char* name)
995 for (int count = 0; count < ConfValueEnum("banlist",&config_f); count++)
997 ConfValue("banlist","chan",count,CM,&config_f);
1000 ConfValue("banlist","limit",count,CM,&config_f);
1007 void purge_empty_chans(userrec* u)
1010 int go_again = 1, purge = 0;
1012 // firstly decrement the count on each channel
1013 for (int f = 0; f < MAXCHANS; f++)
1015 if (u->chans[f].channel)
1017 u->chans[f].channel->DelUser((char*)u);
1021 for (int i = 0; i < MAXCHANS; i++)
1023 if (u->chans[i].channel)
1025 if (!usercount(u->chans[i].channel))
1027 chan_hash::iterator i2 = chanlist.find(u->chans[i].channel->name);
1028 /* kill the record */
1029 if (i2 != chanlist.end())
1031 log(DEBUG,"del_channel: destroyed: %s",i2->second->name);
1037 u->chans[i].channel = NULL;
1042 log(DEBUG,"skipped purge for %s",u->chans[i].channel->name);
1046 log(DEBUG,"completed channel purge, killed %lu",(unsigned long)purge);
1052 char scratch[MAXBUF];
1053 char sparam[MAXBUF];
1055 char* chanmodes(chanrec *chan)
1059 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1066 if (chan->binarymodes & CM_NOEXTERNAL)
1068 strlcat(scratch,"n",MAXMODES);
1070 if (chan->binarymodes & CM_TOPICLOCK)
1072 strlcat(scratch,"t",MAXMODES);
1076 strlcat(scratch,"k",MAXMODES);
1080 strlcat(scratch,"l",MAXMODES);
1082 if (chan->binarymodes & CM_INVITEONLY)
1084 strlcat(scratch,"i",MAXMODES);
1086 if (chan->binarymodes & CM_MODERATED)
1088 strlcat(scratch,"m",MAXMODES);
1090 if (chan->binarymodes & CM_SECRET)
1092 strlcat(scratch,"s",MAXMODES);
1094 if (chan->binarymodes & CM_PRIVATE)
1096 strlcat(scratch,"p",MAXMODES);
1100 strlcat(sparam," ",MAXBUF);
1101 strlcat(sparam,chan->key,MAXBUF);
1106 sprintf(foo," %lu",(unsigned long)chan->limit);
1107 strlcat(sparam,foo,MAXBUF);
1109 if (*chan->custom_modes)
1111 strlcat(scratch,chan->custom_modes,MAXMODES);
1112 for (int z = 0; chan->custom_modes[z] != 0; z++)
1114 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1117 strlcat(sparam," ",MAXBUF);
1118 strlcat(sparam,extparam.c_str(),MAXBUF);
1122 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
1123 strlcat(scratch,sparam,MAXMODES);
1128 /* compile a userlist of a channel into a string, each nick seperated by
1129 * spaces and op, voice etc status shown as @ and + */
1131 void userlist(userrec *user,chanrec *c)
1133 if ((!c) || (!user))
1135 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1139 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1141 std::vector<char*> *ulist = c->GetUsers();
1142 for (int i = 0; i < ulist->size(); i++)
1144 char* o = (*ulist)[i];
1145 userrec* otheruser = (userrec*)o;
1146 if ((!has_channel(user,c)) && (strchr(otheruser->modes,'i')))
1148 /* user is +i, and source not on the channel, does not show
1149 * nick in NAMES list */
1152 strlcat(list,cmode(otheruser,c),MAXBUF);
1153 strlcat(list,otheruser->nick,MAXBUF);
1154 strlcat(list," ",MAXBUF);
1155 if (strlen(list)>(480-NICKMAX))
1157 /* list overflowed into
1158 * multiple numerics */
1159 WriteServ(user->fd,"%s",list);
1160 snprintf(list,MAXBUF,"353 %s = %s :", user->nick, c->name);
1163 /* if whats left in the list isnt empty, send it */
1164 if (list[strlen(list)-1] != ':')
1166 WriteServ(user->fd,"%s",list);
1170 /* return a count of the users on a specific channel accounting for
1171 * invisible users who won't increase the count. e.g. for /LIST */
1173 int usercount_i(chanrec *c)
1179 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1184 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1188 if (has_channel(i->second,c))
1190 if (isnick(i->second->nick))
1192 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1194 /* user is +i, and source not on the channel, does not show
1195 * nick in NAMES list */
1203 log(DEBUG,"usercount_i: %s %lu",c->name,(unsigned long)count);
1208 int usercount(chanrec *c)
1212 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
1215 int count = c->GetUserCounter();
1216 log(DEBUG,"usercount: %s %lu",c->name,(unsigned long)count);
1221 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
1223 char* Passwd(userrec *user)
1225 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1227 if (match(user->host,i->host) && (i->type == CC_ALLOW))
1235 bool IsDenied(userrec *user)
1237 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
1239 if (match(user->host,i->host) && (i->type == CC_DENY))
1250 /* sends out an error notice to all connected clients (not to be used
1253 void send_error(char *s)
1255 log(DEBUG,"send_error: %s",s);
1256 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1258 if (isnick(i->second->nick))
1260 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
1264 // fix - unregistered connections receive ERROR, not NOTICE
1265 Write(i->second->fd,"ERROR :%s",s);
1270 void Error(int status)
1272 signal (SIGALRM, SIG_IGN);
1273 signal (SIGPIPE, SIG_IGN);
1274 signal (SIGTERM, SIG_IGN);
1275 signal (SIGABRT, SIG_IGN);
1276 signal (SIGSEGV, SIG_IGN);
1277 signal (SIGURG, SIG_IGN);
1278 signal (SIGKILL, SIG_IGN);
1279 log(DEFAULT,"*** fell down a pothole in the road to perfection ***");
1280 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
1284 // this function counts all users connected, wether they are registered or NOT.
1287 return clientlist.size();
1290 // this counts only registered users, so that the percentages in /MAP don't mess up when users are sitting in an unregistered state
1291 int registered_usercount(void)
1294 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1296 if ((i->second->fd) && (isnick(i->second->nick))) c++;
1301 int usercount_invisible(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,'i'))) c++;
1312 int usercount_opers(void)
1316 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1318 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
1323 int usercount_unknown(void)
1327 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1329 if ((i->second->fd) && (i->second->registered != 7))
1335 long chancount(void)
1337 return chanlist.size();
1340 long count_servs(void)
1343 for (int i = 0; i < 32; i++)
1347 for (vector<ircd_connector>::iterator j = me[i]->connectors.begin(); j != me[i]->connectors.end(); j++)
1349 if (strcasecmp(j->GetServerName().c_str(),ServerName))
1359 long servercount(void)
1361 return count_servs()+1;
1367 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1369 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,ServerName))) c++;
1374 void ShowMOTD(userrec *user)
1377 std::string WholeMOTD = "";
1380 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
1383 snprintf(buf,65535,":%s 375 %s :- %s message of the day\r\n", ServerName, user->nick, ServerName);
1384 WholeMOTD = WholeMOTD + buf;
1385 for (int i = 0; i != MOTD.size(); i++)
1387 snprintf(buf,65535,":%s 372 %s :- %s\r\n", ServerName, user->nick, MOTD[i].c_str());
1388 WholeMOTD = WholeMOTD + buf;
1390 snprintf(buf,65535,":%s 376 %s :End of message of the day.\r\n", ServerName, user->nick);
1391 WholeMOTD = WholeMOTD + buf;
1392 // only one write operation
1393 user->AddWriteBuf(WholeMOTD);
1394 statsSent += WholeMOTD.length();
1397 void ShowRULES(userrec *user)
1401 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
1404 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
1405 for (int i = 0; i != RULES.size(); i++)
1407 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
1409 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
1412 // this returns 1 when all modules are satisfied that the user should be allowed onto the irc server
1413 // (until this returns true, a user will block in the waiting state, waiting to connect up to the
1414 // registration timeout maximum seconds)
1415 bool AllModulesReportReady(userrec* user)
1417 for (int i = 0; i <= MODCOUNT; i++)
1419 int res = modules[i]->OnCheckReady(user);
1426 char islast(const char* s)
1429 for (int j = 0; j < 32; j++)
1433 for (int k = 0; k < me[j]->connectors.size(); k++)
1435 if (strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
1439 if (!strcasecmp(me[j]->connectors[k].GetServerName().c_str(),s))
1449 long map_count(const char* s)
1452 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1454 if ((i->second->fd) && (isnick(i->second->nick)) && (!strcasecmp(i->second->server,s))) c++;
1459 void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
1462 /* create the command and push it onto the table */
1463 strlcpy(comm.command,cmd,MAXBUF);
1464 strlcpy(comm.source,source,MAXBUF);
1465 comm.handler_function = f;
1466 comm.flags_needed = flags;
1467 comm.min_params = minparams;
1469 comm.total_bytes = 0;
1470 cmdlist.push_back(comm);
1471 log(DEBUG,"Added command %s (%lu parameters)",cmd,(unsigned long)minparams);
1475 void SetupCommandTable(void)
1477 createcommand("USER",handle_user,0,4,"<core>");
1478 createcommand("NICK",handle_nick,0,1,"<core>");
1479 createcommand("QUIT",handle_quit,0,0,"<core>");
1480 createcommand("VERSION",handle_version,0,0,"<core>");
1481 createcommand("PING",handle_ping,0,1,"<core>");
1482 createcommand("PONG",handle_pong,0,1,"<core>");
1483 createcommand("ADMIN",handle_admin,0,0,"<core>");
1484 createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
1485 createcommand("INFO",handle_info,0,0,"<core>");
1486 createcommand("TIME",handle_time,0,0,"<core>");
1487 createcommand("WHOIS",handle_whois,0,1,"<core>");
1488 createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
1489 createcommand("NOTICE",handle_notice,0,2,"<core>");
1490 createcommand("JOIN",handle_join,0,1,"<core>");
1491 createcommand("NAMES",handle_names,0,0,"<core>");
1492 createcommand("PART",handle_part,0,1,"<core>");
1493 createcommand("KICK",handle_kick,0,2,"<core>");
1494 createcommand("MODE",handle_mode,0,1,"<core>");
1495 createcommand("TOPIC",handle_topic,0,1,"<core>");
1496 createcommand("WHO",handle_who,0,1,"<core>");
1497 createcommand("MOTD",handle_motd,0,0,"<core>");
1498 createcommand("RULES",handle_rules,0,0,"<core>");
1499 createcommand("OPER",handle_oper,0,2,"<core>");
1500 createcommand("LIST",handle_list,0,0,"<core>");
1501 createcommand("DIE",handle_die,'o',1,"<core>");
1502 createcommand("RESTART",handle_restart,'o',1,"<core>");
1503 createcommand("KILL",handle_kill,'o',2,"<core>");
1504 createcommand("REHASH",handle_rehash,'o',0,"<core>");
1505 createcommand("LUSERS",handle_lusers,0,0,"<core>");
1506 createcommand("STATS",handle_stats,0,1,"<core>");
1507 createcommand("USERHOST",handle_userhost,0,1,"<core>");
1508 createcommand("AWAY",handle_away,0,0,"<core>");
1509 createcommand("ISON",handle_ison,0,0,"<core>");
1510 createcommand("SUMMON",handle_summon,0,0,"<core>");
1511 createcommand("USERS",handle_users,0,0,"<core>");
1512 createcommand("INVITE",handle_invite,0,0,"<core>");
1513 createcommand("PASS",handle_pass,0,1,"<core>");
1514 createcommand("TRACE",handle_trace,'o',0,"<core>");
1515 createcommand("WHOWAS",handle_whowas,0,1,"<core>");
1516 createcommand("CONNECT",handle_connect,'o',1,"<core>");
1517 createcommand("SQUIT",handle_squit,'o',0,"<core>");
1518 createcommand("MODULES",handle_modules,0,0,"<core>");
1519 createcommand("LINKS",handle_links,0,0,"<core>");
1520 createcommand("MAP",handle_map,0,0,"<core>");
1521 createcommand("KLINE",handle_kline,'o',1,"<core>");
1522 createcommand("GLINE",handle_gline,'o',1,"<core>");
1523 createcommand("ZLINE",handle_zline,'o',1,"<core>");
1524 createcommand("QLINE",handle_qline,'o',1,"<core>");
1525 createcommand("ELINE",handle_eline,'o',1,"<core>");
1526 createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
1527 createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
1528 createcommand("SERVER",handle_server,0,0,"<core>");
1531 bool DirValid(char* dirandfile)
1534 strlcpy(work,dirandfile,MAXBUF);
1535 int p = strlen(work);
1536 // we just want the dir
1537 while (strlen(work))
1546 char buffer[MAXBUF], otherdir[MAXBUF];
1547 // Get the current working directory
1548 if( getcwd( buffer, MAXBUF ) == NULL )
1551 if( getcwd( otherdir, MAXBUF ) == NULL )
1554 if (strlen(otherdir) >= strlen(work))
1556 otherdir[strlen(work)] = '\0';
1557 if (!strcmp(otherdir,work))
1566 std::string GetFullProgDir(char** argv, int argc)
1569 strlcpy(work,argv[0],MAXBUF);
1570 int p = strlen(work);
1571 // we just want the dir
1572 while (strlen(work))
1581 char buffer[MAXBUF], otherdir[MAXBUF];
1582 // Get the current working directory
1583 if( getcwd( buffer, MAXBUF ) == NULL )
1586 if( getcwd( otherdir, MAXBUF ) == NULL )