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 * ---------------------------------------------------
17 /* Now with added unF! ;) */
22 #include "inspircd_io.h"
23 #include "inspircd_util.h"
24 #include "inspircd_config.h"
27 #include <sys/errno.h>
28 #include <sys/ioctl.h>
29 #include <sys/utsname.h>
34 #include <ext/hash_map>
44 #include "connection.h"
54 #define nspace __gnu_cxx
59 int LogLevel = DEFAULT;
60 char ServerName[MAXBUF];
62 char ServerDesc[MAXBUF];
63 char AdminName[MAXBUF];
64 char AdminEmail[MAXBUF];
65 char AdminNick[MAXBUF];
67 char restartpass[MAXBUF];
71 char PrefixQuit[MAXBUF];
72 char DieValue[MAXBUF];
74 int WHOWAS_STALE = 48; // default WHOWAS Entries last 2 days before they go 'stale'
75 int WHOWAS_MAX = 100; // default 100 people maximum in the WHOWAS list
77 time_t startup_time = time(NULL);
78 int NetBufferSize = 10240; // NetBufferSize used as the buffer size for all read() ops
80 extern vector<Module*> modules;
81 std::vector<std::string> module_names;
82 extern vector<ircd_module*> factory;
83 std::vector<int> fd_reap;
93 template<> struct nspace::hash<in_addr>
95 size_t operator()(const struct in_addr &a) const
98 memcpy(&q,&a,sizeof(size_t));
103 template<> struct nspace::hash<string>
105 size_t operator()(const string &s) const
108 static struct hash<const char *> strhash;
120 bool operator()(const string& s1, const string& s2) const
122 char a[MAXBUF],b[MAXBUF];
123 strcpy(a,s1.c_str());
124 strcpy(b,s2.c_str());
125 return (strcasecmp(a,b) == 0);
130 struct InAddr_HashComp
133 bool operator()(const in_addr &s1, const in_addr &s2) const
138 memcpy(&q,&s1,sizeof(size_t));
139 memcpy(&p,&s2,sizeof(size_t));
147 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
148 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
149 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
150 typedef std::deque<command_t> command_table;
153 serverrec* servers[255];
157 user_hash clientlist;
160 command_table cmdlist;
167 struct linger linger = { 0 };
168 char bannerBuffer[MAXBUF];
169 int boundPortCount = 0;
170 int portCount = 0, UDPportCount = 0, ports[MAXSOCKS];
171 int defaultRoute = 0;
175 long MyKey = C.GenKey();
179 int has_channel(userrec *u, chanrec *c);
180 int usercount(chanrec *c);
181 int usercount_i(chanrec *c);
182 void update_stats_l(int fd,int data_out);
183 char* Passwd(userrec *user);
184 bool IsDenied(userrec *user);
185 void AddWhoWas(userrec* u);
187 std::stringstream config_f(stringstream::in | stringstream::out);
189 void safedelete(userrec *p)
193 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
194 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
200 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
204 void safedelete(chanrec *p)
210 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
214 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
219 void tidystring(char* str)
221 // strips out double spaces before a : parameter
224 bool go_again = true;
231 while ((str[0] == ' ') && (strlen(str)>0))
238 bool noparse = false;
241 while (a < strlen(str))
243 if ((a<strlen(str)-1) && (noparse==false))
245 if ((str[a] == ' ') && (str[a+1] == ' '))
247 log(DEBUG,"Tidied extra space out of string: %s",str);
255 if ((str[a] == ' ') && (str[a+1] == ':'))
261 temp[t++] = str[a++];
264 strncpy(str,temp,MAXBUF);
268 /* chop a string down to 512 characters and preserve linefeed (irc max
275 log(DEBUG,"ERROR! Null string passed to chop()!");
279 FOREACH_MOD OnServerRaw(temp,false);
280 const char* str2 = temp.c_str();
281 sprintf(str,"%s",str2);
284 if (strlen(str) >= 512)
293 std::string getservername()
298 std::string getserverdesc()
303 std::string getnetworkname()
308 std::string getadminname()
313 std::string getadminemail()
318 std::string getadminnick()
323 void log(int level,char *text, ...)
325 char textbuffer[MAXBUF];
328 struct tm * timeinfo;
329 if (level < LogLevel)
333 timeinfo = localtime (&rawtime);
338 va_start (argsPtr, text);
339 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
341 strcpy(b,asctime(timeinfo));
342 b[strlen(b)-1] = ':';
343 fprintf(log_file,"%s %s\n",b,textbuffer);
346 // nofork enabled? display it on terminal too
347 printf("%s %s\n",b,textbuffer);
352 void readfile(file_cache &F, const char* fname)
355 char linebuf[MAXBUF];
357 log(DEBUG,"readfile: loading %s",fname);
359 file = fopen(fname,"r");
364 fgets(linebuf,sizeof(linebuf),file);
365 linebuf[strlen(linebuf)-1]='\0';
366 if (!strcmp(linebuf,""))
372 F.push_back(linebuf);
379 log(DEBUG,"readfile: failed to load file: %s",fname);
381 log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
384 void ReadConfig(void)
386 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF];
389 LoadConf(CONFIG_FILE,&config_f);
391 ConfValue("server","name",0,ServerName,&config_f);
392 ConfValue("server","description",0,ServerDesc,&config_f);
393 ConfValue("server","network",0,Network,&config_f);
394 ConfValue("admin","name",0,AdminName,&config_f);
395 ConfValue("admin","email",0,AdminEmail,&config_f);
396 ConfValue("admin","nick",0,AdminNick,&config_f);
397 ConfValue("files","motd",0,motd,&config_f);
398 ConfValue("files","rules",0,rules,&config_f);
399 ConfValue("power","diepass",0,diepass,&config_f);
400 ConfValue("power","pause",0,pauseval,&config_f);
401 ConfValue("power","restartpass",0,restartpass,&config_f);
402 ConfValue("options","prefixquit",0,PrefixQuit,&config_f);
403 ConfValue("die","value",0,DieValue,&config_f);
404 ConfValue("options","loglevel",0,dbg,&config_f);
405 ConfValue("options","netbuffersize",0,NB,&config_f);
406 NetBufferSize = atoi(NB);
407 if ((!NetBufferSize) || (NetBufferSize > 65535) || (NetBufferSize < 1024))
409 log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
410 NetBufferSize = 10240;
412 if (!strcmp(dbg,"debug"))
414 if (!strcmp(dbg,"verbose"))
416 if (!strcmp(dbg,"default"))
418 if (!strcmp(dbg,"sparse"))
420 if (!strcmp(dbg,"none"))
423 log(DEBUG,"Reading message of the day");
424 readfile(RULES,rules);
425 log(DEBUG,"Reading connect classes");
427 for (int i = 0; i < ConfValueEnum("connect",&config_f); i++)
430 ConfValue("connect","allow",i,Value,&config_f);
431 ConfValue("connect","timeout",i,timeout,&config_f);
432 ConfValue("connect","flood",i,flood,&config_f);
433 if (strcmp(Value,""))
435 strcpy(c.host,Value);
438 ConfValue("connect","password",i,Value,&config_f);
439 strcpy(c.pass,Value);
440 c.registration_timeout = 90; // default is 2 minutes
441 c.flood = atoi(flood);
444 c.registration_timeout = atoi(timeout);
446 Classes.push_back(c);
447 log(DEBUG,"Read connect class type ALLOW, host=%s password=%s timeout=%d flood=%d",c.host,c.pass,c.registration_timeout,c.flood);
451 ConfValue("connect","deny",i,Value,&config_f);
452 strcpy(c.host,Value);
454 Classes.push_back(c);
455 log(DEBUG,"Read connect class type DENY, host=%s",c.host);
464 log(DEBUG,"Blocking: %d",s);
465 flags = fcntl(s, F_GETFL, 0);
466 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
469 void NonBlocking(int s)
472 log(DEBUG,"NonBlocking: %d",s);
473 flags = fcntl(s, F_GETFL, 0);
474 //fcntl(s, F_SETFL, O_NONBLOCK);
475 fcntl(s, F_SETFL, flags | O_NONBLOCK);
479 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
481 struct hostent *hostPtr = NULL;
484 memset (resolvedHost, '\0',MAXBUF);
485 if(unresolvedHost == NULL)
487 if ((inet_aton(unresolvedHost,&addr)) == 0)
489 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
491 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
493 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
497 /* write formatted text to a socket, in same format as printf */
499 void Write(int sock,char *text, ...)
503 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
506 char textbuffer[MAXBUF];
510 va_start (argsPtr, text);
511 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
513 sprintf(tb,"%s\r\n",textbuffer);
517 write(sock,tb,strlen(tb));
518 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
522 /* write a server formatted numeric response to a single socket */
524 void WriteServ(int sock, char* text, ...)
528 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
531 char textbuffer[MAXBUF],tb[MAXBUF];
533 va_start (argsPtr, text);
535 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
537 sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
541 write(sock,tb,strlen(tb));
542 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
546 /* write text from an originating user to originating user */
548 void WriteFrom(int sock, userrec *user,char* text, ...)
550 if ((!text) || (!user))
552 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
555 char textbuffer[MAXBUF],tb[MAXBUF];
557 va_start (argsPtr, text);
559 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
561 sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
565 write(sock,tb,strlen(tb));
566 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
570 /* write text to an destination user from a source user (e.g. user privmsg) */
572 void WriteTo(userrec *source, userrec *dest,char *data, ...)
574 if ((!source) || (!dest) || (!data))
576 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
579 char textbuffer[MAXBUF],tb[MAXBUF];
581 va_start (argsPtr, data);
582 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
585 WriteFrom(dest->fd,source,"%s",textbuffer);
588 /* write formatted text from a source user to all users on a channel
589 * including the sender (NOT for privmsg, notice etc!) */
591 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
593 if ((!Ptr) || (!user) || (!text))
595 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
598 char textbuffer[MAXBUF];
600 va_start (argsPtr, text);
601 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
603 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
605 if (has_channel(i->second,Ptr))
607 WriteTo(user,i->second,"%s",textbuffer);
612 /* write formatted text from a source user to all users on a channel
613 * including the sender (NOT for privmsg, notice etc!) doesnt send to
614 * users on remote servers */
616 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
618 if ((!Ptr) || (!text))
620 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
623 char textbuffer[MAXBUF];
625 va_start (argsPtr, text);
626 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
628 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
630 if (has_channel(i->second,Ptr))
632 if (i->second->fd != -1)
636 WriteServ(i->second->fd,"%s",textbuffer);
640 WriteTo(user,i->second,"%s",textbuffer);
648 void WriteChannelWithServ(char* ServerName, chanrec* Ptr, userrec* user, char* text, ...)
650 if ((!Ptr) || (!user) || (!text))
652 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
655 char textbuffer[MAXBUF];
657 va_start (argsPtr, text);
658 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
660 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
664 if (has_channel(i->second,Ptr))
666 WriteServ(i->second->fd,"%s",textbuffer);
673 /* write formatted text from a source user to all users on a channel except
674 * for the sender (for privmsg etc) */
676 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
678 if ((!Ptr) || (!user) || (!text))
680 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
683 char textbuffer[MAXBUF];
685 va_start (argsPtr, text);
686 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
689 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
693 if (has_channel(i->second,Ptr) && (user != i->second))
695 WriteTo(user,i->second,"%s",textbuffer);
701 int c_count(userrec* u)
704 for (int i =0; i != MAXCHANS; i++)
705 if (u->chans[i].channel != NULL)
711 /* return 0 or 1 depending if users u and u2 share one or more common channels
712 * (used by QUIT, NICK etc which arent channel specific notices) */
714 int common_channels(userrec *u, userrec *u2)
721 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
724 for (i = 0; i != MAXCHANS; i++)
726 for (z = 0; z != MAXCHANS; z++)
728 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
730 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
732 if ((c_count(u)) && (c_count(u2)))
743 /* write a formatted string to all users who share at least one common
744 * channel, including the source user e.g. for use in NICK */
746 void WriteCommon(userrec *u, char* text, ...)
750 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
754 if (u->registered != 7) {
755 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
759 char textbuffer[MAXBUF];
761 va_start (argsPtr, text);
762 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
765 WriteFrom(u->fd,u,"%s",textbuffer);
767 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
771 if (common_channels(u,i->second) && (i->second != u))
773 WriteFrom(i->second->fd,u,"%s",textbuffer);
779 /* write a formatted string to all users who share at least one common
780 * channel, NOT including the source user e.g. for use in QUIT */
782 void WriteCommonExcept(userrec *u, char* text, ...)
786 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
790 if (u->registered != 7) {
791 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
795 char textbuffer[MAXBUF];
797 va_start (argsPtr, text);
798 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
801 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
805 if ((common_channels(u,i->second)) && (u != i->second))
807 WriteFrom(i->second->fd,u,"%s",textbuffer);
813 void WriteOpers(char* text, ...)
817 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
821 char textbuffer[MAXBUF];
823 va_start (argsPtr, text);
824 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
827 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
831 if (strchr(i->second->modes,'o'))
833 if (strchr(i->second->modes,'s'))
835 // send server notices to all with +s
836 // (TODO: needs SNOMASKs)
837 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
844 bool hasumode(userrec* user, char mode)
848 return (strchr(user->modes,mode)>0);
853 void WriteMode(const char* modes, int flags, const char* text, ...)
855 if ((!text) || (!modes) || (!flags))
857 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
861 char textbuffer[MAXBUF];
863 va_start (argsPtr, text);
864 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
867 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
871 bool send_to_user = false;
876 for (int n = 0; n < strlen(modes); n++)
878 if (!hasumode(i->second,modes[n]))
880 send_to_user = false;
885 else if (flags == WM_OR)
887 send_to_user = false;
888 for (int n = 0; n < strlen(modes); n++)
890 if (hasumode(i->second,modes[n]))
900 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
906 void WriteWallOps(userrec *source, char* text, ...)
908 if ((!text) || (!source))
910 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++)
925 if (strchr(i->second->modes,'w'))
927 WriteTo(source,i->second,"WALLOPS %s",textbuffer);
933 /* convert a string to lowercase. Note following special circumstances
934 * taken from RFC 1459. Many "official" server branches still hold to this
935 * rule so i will too;
937 * Because of IRC's scandanavian origin, the characters {}| are
938 * considered to be the lower case equivalents of the characters []\,
939 * respectively. This is a critical issue when determining the
940 * equivalence of two nicknames.
943 void strlower(char *n)
949 for (int i = 0; i != strlen(n); i++)
951 n[i] = tolower(n[i]);
961 /* verify that a user's ident and nickname is valid */
963 int isident(const char* n)
976 for (i = 0; i != strlen(n); i++)
978 if ((n[i] < 33) || (n[i] > 125))
982 /* can't occur ANYWHERE in an Ident! */
983 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
992 int isnick(const char* n)
1004 if (strlen(n) > NICKMAX-1)
1008 for (i = 0; i != strlen(n); i++)
1010 if ((n[i] < 33) || (n[i] > 125))
1014 /* can't occur ANYWHERE in a nickname! */
1015 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
1019 /* can't occur as the first char of a nickname... */
1020 if ((strchr("0123456789",n[i])) && (!i))
1028 /* Find a user record by nickname and return a pointer to it */
1030 userrec* Find(string nick)
1032 user_hash::iterator iter = clientlist.find(nick);
1034 if (iter == clientlist.end())
1035 /* Couldn't find it */
1038 return iter->second;
1041 void update_stats_l(int fd,int data_out) /* add one line-out to stats L for this fd */
1043 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1047 if (i->second->fd == fd)
1049 i->second->bytes_out+=data_out;
1050 i->second->cmds_out++;
1057 /* find a channel record by channel name and return a pointer to it */
1059 chanrec* FindChan(const char* chan)
1063 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1067 chan_hash::iterator iter = chanlist.find(chan);
1069 if (iter == chanlist.end())
1070 /* Couldn't find it */
1073 return iter->second;
1077 void purge_empty_chans(void)
1079 int go_again = 1, purge = 0;
1084 for (chan_hash::iterator i = chanlist.begin(); i != chanlist.end(); i++)
1087 if (!usercount(i->second))
1089 /* kill the record */
1090 if (i != chanlist.end())
1092 log(DEBUG,"del_channel: destroyed: %s",i->second->name);
1102 log(DEBUG,"skipped purge for %s",i->second->name);
1107 log(DEBUG,"completed channel purge, killed %d",purge);
1110 /* returns the status character for a given user on a channel, e.g. @ for op,
1111 * % for halfop etc. If the user has several modes set, the highest mode
1112 * the user has must be returned. */
1114 char* cmode(userrec *user, chanrec *chan)
1116 if ((!user) || (!chan))
1118 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
1123 for (i = 0; i != MAXCHANS; i++)
1125 if ((user->chans[i].channel == chan) && (chan != NULL))
1127 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
1131 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
1135 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
1144 char scratch[MAXBUF];
1145 char sparam[MAXBUF];
1147 char* chanmodes(chanrec *chan)
1151 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1158 if (chan->noexternal)
1160 strncat(scratch,"n",MAXMODES);
1162 if (chan->topiclock)
1164 strncat(scratch,"t",MAXMODES);
1166 if (strcmp(chan->key,""))
1168 strncat(scratch,"k",MAXMODES);
1172 strncat(scratch,"l",MAXMODES);
1174 if (chan->inviteonly)
1176 strncat(scratch,"i",MAXMODES);
1178 if (chan->moderated)
1180 strncat(scratch,"m",MAXMODES);
1184 strncat(scratch,"s",MAXMODES);
1186 if (chan->c_private)
1188 strncat(scratch,"p",MAXMODES);
1190 if (strcmp(chan->key,""))
1192 strncat(sparam,chan->key,MAXBUF);
1197 sprintf(foo," %d",chan->limit);
1198 strncat(sparam,foo,MAXBUF);
1200 if (strlen(chan->custom_modes))
1202 strncat(scratch,chan->custom_modes,MAXMODES);
1203 for (int z = 0; z < strlen(chan->custom_modes); z++)
1205 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1208 strncat(sparam," ",MAXBUF);
1209 strncat(sparam,extparam.c_str(),MAXBUF);
1213 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
1214 strncat(scratch,sparam,MAXMODES);
1218 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
1219 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
1220 * highest mode the user has must be returned. */
1222 int cstatus(userrec *user, chanrec *chan)
1224 if ((!chan) || (!user))
1226 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
1231 for (i = 0; i != MAXCHANS; i++)
1233 if ((user->chans[i].channel == chan) && (chan != NULL))
1235 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
1239 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
1243 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
1245 return STATUS_VOICE;
1247 return STATUS_NORMAL;
1253 /* compile a userlist of a channel into a string, each nick seperated by
1254 * spaces and op, voice etc status shown as @ and + */
1256 void userlist(userrec *user,chanrec *c)
1258 if ((!c) || (!user))
1260 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1264 sprintf(list,"353 %s = %s :", user->nick, c->name);
1265 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1267 if (has_channel(i->second,c))
1269 if (isnick(i->second->nick))
1271 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1273 /* user is +i, and source not on the channel, does not show
1274 * nick in NAMES list */
1277 strcat(list,cmode(i->second,c));
1278 strcat(list,i->second->nick);
1280 if (strlen(list)>(480-NICKMAX))
1282 /* list overflowed into
1283 * multiple numerics */
1284 WriteServ(user->fd,list);
1285 sprintf(list,"353 %s = %s :", user->nick, c->name);
1290 /* if whats left in the list isnt empty, send it */
1291 if (list[strlen(list)-1] != ':')
1293 WriteServ(user->fd,list);
1297 /* return a count of the users on a specific channel accounting for
1298 * invisible users who won't increase the count. e.g. for /LIST */
1300 int usercount_i(chanrec *c)
1307 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1312 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1316 if (has_channel(i->second,c))
1318 if (isnick(i->second->nick))
1320 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1322 /* user is +i, and source not on the channel, does not show
1323 * nick in NAMES list */
1331 log(DEBUG,"usercount_i: %s %d",c->name,count);
1336 int usercount(chanrec *c)
1343 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
1348 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1352 if (has_channel(i->second,c))
1354 if ((isnick(i->second->nick)) && (i->second->registered == 7))
1361 log(DEBUG,"usercount: %s %d",c->name,count);
1366 /* add a channel to a user, creating the record for it if needed and linking
1367 * it to the user record */
1369 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
1371 if ((!user) || (!cn))
1373 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
1382 strncpy(cname,cn,MAXBUF);
1384 // we MUST declare this wherever we use FOREACH_RESULT
1387 if ((!cname) || (!user))
1391 if (strlen(cname) > CHANMAX-1)
1393 cname[CHANMAX-1] = '\0';
1396 log(DEBUG,"add_channel: %s %s",user->nick,cname);
1398 if ((FindChan(cname)) && (has_channel(user,FindChan(cname))))
1400 return NULL; // already on the channel!
1404 if (!FindChan(cname))
1406 FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
1411 /* create a new one */
1412 log(DEBUG,"add_channel: creating: %s",cname);
1414 chanlist[cname] = new chanrec();
1416 strcpy(chanlist[cname]->name, cname);
1417 chanlist[cname]->topiclock = 1;
1418 chanlist[cname]->noexternal = 1;
1419 chanlist[cname]->created = time(NULL);
1420 strcpy(chanlist[cname]->topic, "");
1421 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
1422 chanlist[cname]->topicset = 0;
1423 Ptr = chanlist[cname];
1424 log(DEBUG,"add_channel: created: %s",cname);
1425 /* set created to 2 to indicate user
1426 * is the first in the channel
1427 * and should be given ops */
1433 /* channel exists, just fish out a pointer to its struct */
1434 Ptr = FindChan(cname);
1437 FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
1442 log(DEBUG,"add_channel: joining to: %s",Ptr->name);
1444 // the override flag allows us to bypass channel modes
1445 // and bans (used by servers)
1449 if (strcmp(Ptr->key,""))
1451 log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
1454 log(DEBUG,"add_channel: no key given in JOIN");
1455 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
1460 log(DEBUG,"key at %p is %s",key,key);
1461 if (strcasecmp(key,Ptr->key))
1463 log(DEBUG,"add_channel: bad key given in JOIN");
1464 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
1469 log(DEBUG,"add_channel: no key");
1471 if (Ptr->inviteonly)
1473 log(DEBUG,"add_channel: channel is +i");
1474 if (user->IsInvited(Ptr->name))
1476 /* user was invited to channel */
1477 /* there may be an optional channel NOTICE here */
1481 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
1485 log(DEBUG,"add_channel: channel is not +i");
1489 if (usercount(Ptr) == Ptr->limit)
1491 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
1496 log(DEBUG,"add_channel: about to walk banlist");
1498 /* check user against the channel banlist */
1499 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1501 if (match(user->GetFullHost(),i->data))
1503 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
1510 log(DEBUG,"add_channel: bans checked");
1512 user->RemoveInvite(Ptr->name);
1514 log(DEBUG,"add_channel: invites removed");
1520 log(DEBUG,"Passed channel checks");
1522 for (i =0; i != MAXCHANS; i++)
1524 if (user->chans[i].channel == NULL)
1526 log(DEBUG,"Adding into their channel list");
1530 /* first user in is given ops */
1531 user->chans[i].uc_modes = UCMODE_OP;
1535 user->chans[i].uc_modes = 0;
1537 user->chans[i].channel = Ptr;
1538 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
1540 log(DEBUG,"Sent JOIN to client");
1544 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
1545 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
1548 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
1549 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name,chanmodes(Ptr));
1550 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1551 FOREACH_MOD OnUserJoin(user,Ptr);
1555 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
1556 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
1560 /* remove a channel from a users record, and remove the record from memory
1561 * if the channel has become empty */
1563 chanrec* del_channel(userrec *user, const char* cname, const char* reason)
1565 if ((!user) || (!cname))
1567 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
1575 if ((!cname) || (!user))
1580 Ptr = FindChan(cname);
1587 FOREACH_MOD OnUserPart(user,Ptr);
1588 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
1590 for (i =0; i != MAXCHANS; i++)
1592 /* zap it from the channel list of the user */
1593 if (user->chans[i].channel == Ptr)
1597 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
1601 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
1603 user->chans[i].uc_modes = 0;
1604 user->chans[i].channel = NULL;
1605 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1610 /* if there are no users left on the channel */
1611 if (!usercount(Ptr))
1613 chan_hash::iterator iter = chanlist.find(Ptr->name);
1615 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1617 /* kill the record */
1618 if (iter != chanlist.end())
1620 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1621 delete iter->second;
1622 chanlist.erase(iter);
1628 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
1630 if ((!src) || (!user) || (!Ptr) || (!reason))
1632 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
1639 if ((!Ptr) || (!user) || (!src))
1644 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
1646 if (!has_channel(user,Ptr))
1648 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
1651 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
1653 if (cstatus(src,Ptr) == STATUS_HOP)
1655 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
1659 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
1665 for (i =0; i != MAXCHANS; i++)
1667 /* zap it from the channel list of the user */
1668 if (user->chans[i].channel == Ptr)
1670 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
1671 user->chans[i].uc_modes = 0;
1672 user->chans[i].channel = NULL;
1673 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1678 /* if there are no users left on the channel */
1679 if (!usercount(Ptr))
1681 chan_hash::iterator iter = chanlist.find(Ptr->name);
1683 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1685 /* kill the record */
1686 if (iter != chanlist.end())
1688 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1689 delete iter->second;
1690 chanlist.erase(iter);
1696 /* returns 1 if user u has channel c in their record, 0 if not */
1698 int has_channel(userrec *u, chanrec *c)
1704 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
1707 for (i =0; i != MAXCHANS; i++)
1709 if (u->chans[i].channel == c)
1717 int give_ops(userrec *user,char *dest,chanrec *chan,int status)
1722 if ((!user) || (!dest) || (!chan))
1724 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
1727 if (status != STATUS_OP)
1729 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1736 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1742 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1747 for (i = 0; i != MAXCHANS; i++)
1749 if ((d->chans[i].channel == chan) && (chan != NULL))
1751 if (d->chans[i].uc_modes & UCMODE_OP)
1753 /* mode already set on user, dont allow multiple */
1756 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
1757 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
1765 int give_hops(userrec *user,char *dest,chanrec *chan,int status)
1770 if ((!user) || (!dest) || (!chan))
1772 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
1775 if (status != STATUS_OP)
1777 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1785 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1790 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1795 for (i = 0; i != MAXCHANS; i++)
1797 if ((d->chans[i].channel == chan) && (chan != NULL))
1799 if (d->chans[i].uc_modes & UCMODE_HOP)
1801 /* mode already set on user, dont allow multiple */
1804 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
1805 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
1813 int give_voice(userrec *user,char *dest,chanrec *chan,int status)
1818 if ((!user) || (!dest) || (!chan))
1820 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
1823 if (status < STATUS_HOP)
1825 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
1833 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1838 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1843 for (i = 0; i != MAXCHANS; i++)
1845 if ((d->chans[i].channel == chan) && (chan != NULL))
1847 if (d->chans[i].uc_modes & UCMODE_VOICE)
1849 /* mode already set on user, dont allow multiple */
1852 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
1853 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
1861 int take_ops(userrec *user,char *dest,chanrec *chan,int status)
1866 if ((!user) || (!dest) || (!chan))
1868 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
1871 if (status != STATUS_OP)
1873 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1881 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1886 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1891 for (i = 0; i != MAXCHANS; i++)
1893 if ((d->chans[i].channel == chan) && (chan != NULL))
1895 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
1897 /* mode already set on user, dont allow multiple */
1900 d->chans[i].uc_modes ^= UCMODE_OP;
1901 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
1909 int take_hops(userrec *user,char *dest,chanrec *chan,int status)
1914 if ((!user) || (!dest) || (!chan))
1916 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
1919 if (status != STATUS_OP)
1921 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1929 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1934 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1939 for (i = 0; i != MAXCHANS; i++)
1941 if ((d->chans[i].channel == chan) && (chan != NULL))
1943 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
1945 /* mode already set on user, dont allow multiple */
1948 d->chans[i].uc_modes ^= UCMODE_HOP;
1949 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
1957 int take_voice(userrec *user,char *dest,chanrec *chan,int status)
1962 if ((!user) || (!dest) || (!chan))
1964 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
1967 if (status < STATUS_HOP)
1969 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
1977 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1982 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1987 for (i = 0; i != MAXCHANS; i++)
1989 if ((d->chans[i].channel == chan) && (chan != NULL))
1991 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
1993 /* mode already set on user, dont allow multiple */
1996 d->chans[i].uc_modes ^= UCMODE_VOICE;
1997 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
2005 void TidyBan(char *ban)
2008 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
2012 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
2016 char* pos_of_pling = strchr(temp,'!');
2017 char* pos_of_at = strchr(temp,'@');
2019 pos_of_pling[0] = '\0';
2020 pos_of_at[0] = '\0';
2024 strncpy(NICK,temp,NICKMAX);
2025 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
2026 strncpy(HOST,pos_of_at,160);
2028 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
2031 int add_ban(userrec *user,char *dest,chanrec *chan,int status)
2033 if ((!user) || (!dest) || (!chan)) {
2034 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
2039 if ((!user) || (!dest) || (!chan))
2041 if (strchr(dest,'!')==0)
2043 if (strchr(dest,'@')==0)
2045 for (int i = 0; i < strlen(dest); i++)
2048 for (int i = 0; i < strlen(dest); i++)
2052 for (int i = 0; i < strlen(dest); i++)
2058 for (int i = 0; i < strlen(dest); i++)
2063 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
2066 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
2068 if (!strcasecmp(i->data,dest))
2070 // dont allow a user to set the same ban twice
2075 b.set_time = time(NULL);
2076 strncpy(b.data,dest,MAXBUF);
2077 strncpy(b.set_by,user->nick,NICKMAX);
2078 chan->bans.push_back(b);
2082 int take_ban(userrec *user,char *dest,chanrec *chan,int status)
2084 if ((!user) || (!dest) || (!chan)) {
2085 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
2089 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
2090 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
2092 if (!strcasecmp(i->data,dest))
2094 chan->bans.erase(i);
2101 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode)
2103 if ((!parameters) || (!user)) {
2104 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
2109 char modelist[MAXBUF];
2110 char outlist[MAXBUF];
2111 char outstr[MAXBUF];
2112 char outpars[32][MAXBUF];
2118 bool k_set = false, l_set = false;
2125 log(DEBUG,"process_modes: start");
2127 strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
2128 /* parameters[2] onwards are parameters for
2129 * modes that require them :) */
2130 strcpy(outlist,"+");
2133 log(DEBUG,"process_modes: modelist: %s",modelist);
2135 for (ptr = 0; ptr < strlen(modelist); ptr++)
2140 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
2141 char modechar = modelist[ptr];
2142 switch (modelist[ptr])
2147 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
2149 outlist[strlen(outlist)-1] = '-';
2153 strcat(outlist,"-");
2163 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
2165 outlist[strlen(outlist)-1] = '+';
2169 strcat(outlist,"+");
2176 if ((param >= pcnt)) break;
2179 r = give_ops(user,parameters[param++],chan,status);
2183 r = take_ops(user,parameters[param++],chan,status);
2187 strcat(outlist,"o");
2188 strcpy(outpars[pc++],parameters[param-1]);
2193 if ((param >= pcnt)) break;
2196 r = give_hops(user,parameters[param++],chan,status);
2200 r = take_hops(user,parameters[param++],chan,status);
2204 strcat(outlist,"h");
2205 strcpy(outpars[pc++],parameters[param-1]);
2211 if ((param >= pcnt)) break;
2214 r = give_voice(user,parameters[param++],chan,status);
2218 r = take_voice(user,parameters[param++],chan,status);
2222 strcat(outlist,"v");
2223 strcpy(outpars[pc++],parameters[param-1]);
2228 if ((param >= pcnt)) break;
2231 r = add_ban(user,parameters[param++],chan,status);
2235 r = take_ban(user,parameters[param++],chan,status);
2239 strcat(outlist,"b");
2240 strcpy(outpars[pc++],parameters[param-1]);
2246 if ((param >= pcnt))
2254 if (!strcmp(chan->key,""))
2256 strcat(outlist,"k");
2258 strcpy(key,parameters[param++]);
2259 if (strlen(key)>32) {
2262 strcpy(outpars[pc++],key);
2263 strcpy(chan->key,key);
2269 /* checks on -k are case sensitive and only accurate to the
2270 first 32 characters */
2272 strcpy(key,parameters[param++]);
2273 if (strlen(key)>32) {
2276 /* only allow -k if correct key given */
2277 if (!strcmp(chan->key,key))
2279 strcat(outlist,"k");
2280 strcpy(chan->key,"");
2281 strcpy(outpars[pc++],key);
2291 strcat(outlist,"l");
2296 if ((param >= pcnt)) break;
2302 bool invalid = false;
2303 for (int i = 0; i < strlen(parameters[param]); i++)
2305 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
2310 if (atoi(parameters[param]) < 1)
2318 chan->limit = atoi(parameters[param]);
2321 strcat(outlist,"l");
2322 strcpy(outpars[pc++],parameters[param++]);
2329 if (chan->inviteonly != mdir)
2331 strcat(outlist,"i");
2333 chan->inviteonly = mdir;
2337 if (chan->topiclock != mdir)
2339 strcat(outlist,"t");
2341 chan->topiclock = mdir;
2345 if (chan->noexternal != mdir)
2347 strcat(outlist,"n");
2349 chan->noexternal = mdir;
2353 if (chan->moderated != mdir)
2355 strcat(outlist,"m");
2357 chan->moderated = mdir;
2361 if (chan->secret != mdir)
2363 strcat(outlist,"s");
2364 if (chan->c_private)
2366 chan->c_private = 0;
2369 strcat(outlist,"-p+");
2373 strcat(outlist,"+p-");
2377 chan->secret = mdir;
2381 if (chan->c_private != mdir)
2383 strcat(outlist,"p");
2389 strcat(outlist,"-s+");
2393 strcat(outlist,"+s-");
2397 chan->c_private = mdir;
2401 log(DEBUG,"Preprocessing custom mode %c",modechar);
2404 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
2406 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
2409 if (ModeDefined(modechar,MT_CHANNEL))
2411 log(DEBUG,"A module has claimed this mode");
2414 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
2416 p.push_back(parameters[param]);
2418 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
2420 p.push_back(parameters[param]);
2423 bool handled = false;
2426 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
2427 // we're supposed to have a parameter, but none was given... so dont handle the mode.
2428 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
2434 for (int i = 0; i <= MODCOUNT; i++)
2438 if (modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p))
2440 log(DEBUG,"OnExtendedMode returned nonzero for a module");
2441 char app[] = {modechar, 0};
2444 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
2446 strcat(outlist, app);
2448 else if (!strchr(outlist,modechar))
2450 strcat(outlist, app);
2453 chan->SetCustomMode(modechar,mdir);
2454 // include parameters in output if mode has them
2455 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
2457 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
2458 strcpy(outpars[pc++],parameters[param++]);
2460 // break, because only one module can handle the mode.
2472 /* this ensures only the *valid* modes are sent out onto the network */
2473 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
2475 outlist[strlen(outlist)-1] = '\0';
2477 if (strcmp(outlist,""))
2479 strcpy(outstr,outlist);
2480 for (ptr = 0; ptr < pc; ptr++)
2483 strcat(outstr,outpars[ptr]);
2487 WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
2491 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
2496 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
2498 bool allowed_umode(char umode, char* sourcemodes,bool adding)
2500 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
2501 // RFC1459 specified modes
2502 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
2504 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
2508 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
2509 if ((strchr(sourcemodes,'o')) && (!adding))
2511 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
2514 else if (umode == 'o')
2516 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
2520 // process any module-defined modes that need oper
2521 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
2523 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
2527 if (ModeDefined(umode,MT_CLIENT))
2529 // process any module-defined modes that don't need oper
2530 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
2531 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
2533 // no, this mode needs oper, and this user 'aint got what it takes!
2539 // anything else - return false.
2540 log(DEBUG,"umode %c not known by any ruleset",umode);
2544 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
2548 if (ModeDefined(umode,MT_CLIENT))
2550 for (int i = 0; i <= MODCOUNT; i++)
2552 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
2554 log(DEBUG,"Module claims umode %c",umode);
2558 log(DEBUG,"No module claims umode %c",umode);
2563 log(DEBUG,"*** BUG *** Non-module umode passed to process_module_umode!");
2568 void handle_mode(char **parameters, int pcnt, userrec *user)
2574 char outpars[MAXBUF];
2576 dest = Find(parameters[0]);
2578 if ((dest) && (pcnt == 1))
2580 WriteServ(user->fd,"221 %s :+%s",user->nick,user->modes);
2584 if ((dest) && (pcnt > 1))
2586 char dmodes[MAXBUF];
2587 strncpy(dmodes,dest->modes,MAXBUF);
2588 log(DEBUG,"pulled up dest user modes: %s",dmodes);
2593 if (strchr(user->modes,'o'))
2604 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
2608 strcpy(outpars,"+");
2611 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
2614 for (i = 0; i < strlen(parameters[1]); i++)
2616 if (parameters[1][i] == '+')
2620 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2622 outpars[strlen(outpars)-1] = '+';
2626 strcat(outpars,"+");
2632 if (parameters[1][i] == '-')
2636 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2638 outpars[strlen(outpars)-1] = '-';
2642 strcat(outpars,"-");
2650 if (strchr(user->modes,'o'))
2656 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
2665 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2667 char umode = parameters[1][i];
2668 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2670 dmodes[strlen(dmodes)+1]='\0';
2671 dmodes[strlen(dmodes)] = parameters[1][i];
2672 outpars[strlen(outpars)+1]='\0';
2673 outpars[strlen(outpars)] = parameters[1][i];
2679 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2681 char umode = parameters[1][i];
2682 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2688 outpars[strlen(outpars)+1]='\0';
2689 outpars[strlen(outpars)] = parameters[1][i];
2692 for (q = 0; q < strlen(dmodes); q++)
2694 if (dmodes[q] != parameters[1][i])
2701 strcpy(dmodes,temp);
2708 if (strlen(outpars))
2714 while (i < strlen (outpars))
2716 b[z++] = outpars[i++];
2718 if (i<strlen(outpars)-1)
2720 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2722 // someones playing silly buggers and trying
2723 // to put a +- or -+ into the line...
2727 if (i == strlen(outpars)-1)
2729 if ((outpars[i] == '-') || (outpars[i] == '+'))
2737 if ((b[z] == '-') || (b[z] == '+'))
2740 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2743 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2745 if (strlen(dmodes)>MAXMODES)
2747 dmodes[MAXMODES-1] = '\0';
2749 log(DEBUG,"Stripped mode line");
2750 log(DEBUG,"Line dest is now %s",dmodes);
2751 strncpy(dest->modes,dmodes,MAXMODES);
2758 Ptr = FindChan(parameters[0]);
2763 /* just /modes #channel */
2764 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
2765 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
2771 if ((!strcmp(parameters[1],"+b")) || (!strcmp(parameters[1],"b")))
2774 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
2776 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
2778 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
2782 if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
2784 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
2788 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false);
2792 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2799 void server_mode(char **parameters, int pcnt, userrec *user)
2805 char outpars[MAXBUF];
2807 dest = Find(parameters[0]);
2809 // fix: ChroNiCk found this - we cant use this as debug if its null!
2812 log(DEBUG,"server_mode on %s",dest->nick);
2815 if ((dest) && (pcnt > 1))
2817 log(DEBUG,"params > 1");
2819 char dmodes[MAXBUF];
2820 strncpy(dmodes,dest->modes,MAXBUF);
2822 strcpy(outpars,"+");
2825 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
2828 for (i = 0; i < strlen(parameters[1]); i++)
2830 if (parameters[1][i] == '+')
2834 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2836 outpars[strlen(outpars)-1] = '+';
2840 strcat(outpars,"+");
2846 if (parameters[1][i] == '-')
2850 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2852 outpars[strlen(outpars)-1] = '-';
2856 strcat(outpars,"-");
2863 log(DEBUG,"begin mode processing entry");
2869 log(DEBUG,"umode %c being added",parameters[1][i]);
2870 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2872 char umode = parameters[1][i];
2873 log(DEBUG,"umode %c is an allowed umode",umode);
2874 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2876 dmodes[strlen(dmodes)+1]='\0';
2877 dmodes[strlen(dmodes)] = parameters[1][i];
2878 outpars[strlen(outpars)+1]='\0';
2879 outpars[strlen(outpars)] = parameters[1][i];
2885 // can only remove a mode they already have
2886 log(DEBUG,"umode %c being removed",parameters[1][i]);
2887 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2889 char umode = parameters[1][i];
2890 log(DEBUG,"umode %c is an allowed umode",umode);
2891 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2897 outpars[strlen(outpars)+1]='\0';
2898 outpars[strlen(outpars)] = parameters[1][i];
2901 for (q = 0; q < strlen(dmodes); q++)
2903 if (dmodes[q] != parameters[1][i])
2910 strcpy(dmodes,temp);
2917 if (strlen(outpars))
2923 while (i < strlen (outpars))
2925 b[z++] = outpars[i++];
2927 if (i<strlen(outpars)-1)
2929 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2931 // someones playing silly buggers and trying
2932 // to put a +- or -+ into the line...
2936 if (i == strlen(outpars)-1)
2938 if ((outpars[i] == '-') || (outpars[i] == '+'))
2946 if ((b[z] == '-') || (b[z] == '+'))
2949 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2952 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2954 if (strlen(dmodes)>MAXMODES)
2956 dmodes[MAXMODES-1] = '\0';
2958 log(DEBUG,"Stripped mode line");
2959 log(DEBUG,"Line dest is now %s",dmodes);
2960 strncpy(dest->modes,dmodes,MAXMODES);
2967 Ptr = FindChan(parameters[0]);
2970 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true);
2974 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2979 /* This function pokes and hacks at a parameter list like the following:
2981 * PART #winbot, #darkgalaxy :m00!
2983 * to turn it into a series of individual calls like this:
2985 * PART #winbot :m00!
2986 * PART #darkgalaxy :m00!
2988 * The seperate calls are sent to a callback function provided by the caller
2989 * (the caller will usually call itself recursively). The callback function
2990 * must be a command handler. Calling this function on a line with no list causes
2991 * no action to be taken. You must provide a starting and ending parameter number
2992 * where the range of the list can be found, useful if you have a terminating
2993 * parameter as above which is actually not part of the list, or parameters
2994 * before the actual list as well. This code is used by many functions which
2995 * can function as "one to list" (see the RFC) */
2997 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
3002 char blog[32][MAXBUF];
3003 char blog2[32][MAXBUF];
3004 int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
3005 char keystr[MAXBUF];
3008 for (i = 0; i <32; i++)
3011 for (i = 0; i <32; i++)
3012 strcpy(blog2[i],"");
3015 for (i = 0; i <10; i++)
3019 parameters[i] = moo;
3024 if (pcnt > 1) /* we have a key to copy */
3026 strcpy(keystr,parameters[1]);
3030 if (!parameters[start])
3034 if (!strchr(parameters[start],','))
3039 for (i = start; i <= end; i++)
3043 strcat(plist,parameters[i]);
3051 for (i = 0; i < t; i++)
3053 if (plist[i] == ',')
3056 strcpy(blog[j++],param);
3060 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
3065 strcpy(blog[j++],param);
3068 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
3073 if ((joins) && (keystr))
3075 if (strchr(keystr,','))
3079 t2 = strlen(keystr);
3080 for (i = 0; i < t2; i++)
3082 if (keystr[i] == ',')
3085 strcpy(blog2[j++],param);
3089 strcpy(blog2[j++],param);
3094 for (j = 0; j < total; j++)
3100 for (q = end; q < pcnt-1; q++)
3102 if (parameters[q+1])
3104 pars[q-end+1] = parameters[q+1];
3107 if ((joins) && (parameters[1]))
3118 /* repeatedly call the function with the hacked parameter list */
3119 if ((joins) && (pcnt > 1))
3123 // pars[1] already set up and containing key from blog2[j]
3128 pars[1] = parameters[1];
3134 fn(pars,pcnt-(end-start),u);
3142 void handle_join(char **parameters, int pcnt, userrec *user)
3147 if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
3149 if (parameters[0][0] == '#')
3151 Ptr = add_channel(user,parameters[0],parameters[1],false);
3156 void handle_part(char **parameters, int pcnt, userrec *user)
3162 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
3164 del_channel(user,parameters[0],parameters[1]);
3168 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
3170 del_channel(user,parameters[0],NULL);
3174 void handle_kick(char **parameters, int pcnt, userrec *user)
3176 chanrec* Ptr = FindChan(parameters[0]);
3177 userrec* u = Find(parameters[1]);
3181 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3185 if (!has_channel(u,Ptr))
3187 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
3193 char reason[MAXBUF];
3194 strncpy(reason,parameters[2],MAXBUF);
3195 if (strlen(reason)>MAXKICK)
3197 reason[MAXKICK-1] = '\0';
3200 kick_channel(user,u,Ptr,reason);
3204 kick_channel(user,u,Ptr,user->nick);
3209 void handle_die(char **parameters, int pcnt, userrec *user)
3211 log(DEBUG,"die: %s",user->nick);
3212 if (!strcmp(parameters[0],diepass))
3214 WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
3220 WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
3224 void handle_restart(char **parameters, int pcnt, userrec *user)
3226 log(DEBUG,"restart: %s",user->nick);
3227 if (!strcmp(parameters[0],restartpass))
3229 WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
3232 /* Will finish this later when i can be arsed :) */
3236 WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
3241 void kill_link(userrec *user,const char* r)
3243 user_hash::iterator iter = clientlist.find(user->nick);
3245 char reason[MAXBUF];
3247 strncpy(reason,r,MAXBUF);
3249 if (strlen(reason)>MAXQUIT)
3251 reason[MAXQUIT-1] = '\0';
3254 log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
3255 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
3256 log(DEBUG,"closing fd %d",user->fd);
3258 /* bugfix, cant close() a nonblocking socket (sux!) */
3259 if (user->registered == 7) {
3260 FOREACH_MOD OnUserQuit(user);
3261 WriteCommonExcept(user,"QUIT :%s",reason);
3264 /* push the socket on a stack of sockets due to be closed at the next opportunity
3265 * 'Client exited' is an exception to this as it means the client side has already
3266 * closed the socket, we don't need to do it.
3268 fd_reap.push_back(user->fd);
3270 bool do_purge = false;
3272 if (user->registered == 7) {
3273 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
3277 if (iter != clientlist.end())
3279 log(DEBUG,"deleting user hash value %d",iter->second);
3280 if ((iter->second) && (user->registered == 7)) {
3281 delete iter->second;
3283 clientlist.erase(iter);
3286 if (user->registered == 7) {
3287 purge_empty_chans();
3294 void handle_kill(char **parameters, int pcnt, userrec *user)
3296 userrec *u = Find(parameters[0]);
3297 char killreason[MAXBUF];
3299 log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
3302 WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
3303 // :Brain!brain@NetAdmin.chatspike.net KILL [Brain] :homer!NetAdmin.chatspike.net!Brain (test kill)
3304 WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
3305 sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
3306 kill_link(u,killreason);
3310 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3314 void handle_summon(char **parameters, int pcnt, userrec *user)
3316 WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
3319 void handle_users(char **parameters, int pcnt, userrec *user)
3321 WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
3325 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
3327 char* Passwd(userrec *user)
3329 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3331 if (match(user->host,i->host) && (i->type == CC_ALLOW))
3339 bool IsDenied(userrec *user)
3341 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3343 if (match(user->host,i->host) && (i->type == CC_DENY))
3353 void handle_pass(char **parameters, int pcnt, userrec *user)
3355 // Check to make sure they havnt registered -- Fix by FCS
3356 if (user->registered == 7)
3358 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
3361 if (!strcasecmp(parameters[0],Passwd(user)))
3363 user->haspassed = true;
3367 void handle_invite(char **parameters, int pcnt, userrec *user)
3369 userrec* u = Find(parameters[0]);
3370 chanrec* c = FindChan(parameters[1]);
3376 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[1]);
3382 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
3391 if (cstatus(user,c) < STATUS_HOP)
3393 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, c->name);
3397 if (has_channel(u,c))
3399 WriteServ(user->fd,"443 %s %s %s :Is already on channel %s",user->nick,u->nick,c->name,c->name);
3402 if (!has_channel(user,c))
3404 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, c->name);
3407 u->InviteTo(c->name);
3408 WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
3409 WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
3412 void handle_topic(char **parameters, int pcnt, userrec *user)
3418 if (strlen(parameters[0]) <= CHANMAX)
3420 Ptr = FindChan(parameters[0]);
3425 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
3426 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
3430 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
3435 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3442 if (strlen(parameters[0]) <= CHANMAX)
3444 Ptr = FindChan(parameters[0]);
3447 if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
3449 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel", user->nick, Ptr->name);
3454 strncpy(topic,parameters[1],MAXBUF);
3455 if (strlen(topic)>MAXTOPIC)
3457 topic[MAXTOPIC-1] = '\0';
3460 strcpy(Ptr->topic,topic);
3461 strcpy(Ptr->setby,user->nick);
3462 Ptr->topicset = time(NULL);
3463 WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
3467 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3473 /* sends out an error notice to all connected clients (not to be used
3476 void send_error(char *s)
3478 log(DEBUG,"send_error: %s",s);
3479 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3481 if (isnick(i->second->nick))
3483 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
3487 // fix - unregistered connections receive ERROR, not NOTICE
3488 Write(i->second->fd,"ERROR :%s",s);
3493 void Error(int status)
3495 signal (SIGALRM, SIG_IGN);
3496 signal (SIGPIPE, SIG_IGN);
3497 signal (SIGTERM, SIG_IGN);
3498 signal (SIGABRT, SIG_IGN);
3499 signal (SIGSEGV, SIG_IGN);
3500 signal (SIGURG, SIG_IGN);
3501 signal (SIGKILL, SIG_IGN);
3502 log(DEBUG,"*** fell down a pothole in the road to perfection ***");
3503 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
3508 int main (int argc, char *argv[])
3511 log(DEBUG,"*** InspIRCd starting up!");
3512 if (!FileExists(CONFIG_FILE))
3514 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
3515 log(DEBUG,"main: no config");
3516 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
3520 if (!strcmp(argv[1],"-nofork")) {
3524 if (InspIRCd() == ERROR)
3526 log(DEBUG,"main: daemon function bailed");
3527 printf("ERROR: could not initialise. Shutting down.\n");
3534 template<typename T> inline string ConvToStr(const T &in)
3537 if (!(tmp << in)) return string();
3541 /* re-allocates a nick in the user_hash after they change nicknames,
3542 * returns a pointer to the new user as it may have moved */
3544 userrec* ReHashNick(char* Old, char* New)
3546 user_hash::iterator newnick;
3547 user_hash::iterator oldnick = clientlist.find(Old);
3549 log(DEBUG,"ReHashNick: %s %s",Old,New);
3551 if (!strcasecmp(Old,New))
3553 log(DEBUG,"old nick is new nick, skipping");
3554 return oldnick->second;
3557 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
3559 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
3561 clientlist[New] = new userrec();
3562 clientlist[New] = oldnick->second;
3563 /*delete oldnick->second; */
3564 clientlist.erase(oldnick);
3566 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
3568 return clientlist[New];
3571 /* adds or updates an entry in the whowas list */
3572 void AddWhoWas(userrec* u)
3574 user_hash::iterator iter = whowas.find(u->nick);
3575 userrec *a = new userrec();
3576 strcpy(a->nick,u->nick);
3577 strcpy(a->ident,u->ident);
3578 strcpy(a->dhost,u->dhost);
3579 strcpy(a->host,u->host);
3580 strcpy(a->fullname,u->fullname);
3581 strcpy(a->server,u->server);
3582 a->signon = u->signon;
3584 /* MAX_WHOWAS: max number of /WHOWAS items
3585 * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
3586 * can be replaced by a newer one
3589 if (iter == whowas.end())
3591 if (whowas.size() == WHOWAS_MAX)
3593 for (user_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
3595 // 3600 seconds in an hour ;)
3596 if ((i->second->signon)<(time(NULL)-(WHOWAS_STALE*3600)))
3600 log(DEBUG,"added WHOWAS entry, purged an old record");
3607 log(DEBUG,"added fresh WHOWAS entry");
3608 whowas[a->nick] = a;
3613 log(DEBUG,"updated WHOWAS entry");
3614 delete iter->second;
3620 /* add a client connection to the sockets list */
3621 void AddClient(int socket, char* host, int port, bool iscached)
3625 char resolved[MAXBUF];
3628 user_hash::iterator iter;
3630 tempnick = ConvToStr(socket) + "-unknown";
3631 sprintf(tn2,"%d-unknown",socket);
3633 iter = clientlist.find(tempnick);
3635 if (iter != clientlist.end()) return;
3638 * It is OK to access the value here this way since we know
3639 * it exists, we just created it above.
3641 * At NO other time should you access a value in a map or a
3642 * hash_map this way.
3644 clientlist[tempnick] = new userrec();
3646 NonBlocking(socket);
3647 log(DEBUG,"AddClient: %d %s %d",socket,host,port);
3649 clientlist[tempnick]->fd = socket;
3650 strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
3651 strncpy(clientlist[tempnick]->host, host,160);
3652 strncpy(clientlist[tempnick]->dhost, host,160);
3653 strncpy(clientlist[tempnick]->server, ServerName,256);
3654 strncpy(clientlist[tempnick]->ident, "unknown",9);
3655 clientlist[tempnick]->registered = 0;
3656 clientlist[tempnick]->signon = time(NULL);
3657 clientlist[tempnick]->nping = time(NULL)+240;
3658 clientlist[tempnick]->lastping = 1;
3659 clientlist[tempnick]->port = port;
3663 WriteServ(socket,"NOTICE Auth :Found your hostname (cached)...");
3667 WriteServ(socket,"NOTICE Auth :Looking up your hostname...");
3670 // set the registration timeout for this user
3671 unsigned long class_regtimeout = 90;
3672 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3674 if (match(clientlist[tempnick]->host,i->host) && (i->type == CC_ALLOW))
3676 class_regtimeout = (unsigned long)i->registration_timeout;
3681 int class_flood = 0;
3682 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3684 if (match(clientlist[tempnick]->host,i->host) && (i->type == CC_ALLOW))
3686 class_flood = i->flood;
3691 clientlist[tempnick]->timeout = time(NULL)+class_regtimeout;
3692 clientlist[tempnick]->flood = class_flood;
3694 if (clientlist.size() == MAXCLIENTS)
3695 kill_link(clientlist[tempnick],"No more connections allowed in this class");
3698 void handle_names(char **parameters, int pcnt, userrec *user)
3702 if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
3704 c = FindChan(parameters[0]);
3707 /*WriteServ(user->fd,"353 %s = %s :%s", user->nick, c->name,*/
3709 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
3713 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3718 void handle_privmsg(char **parameters, int pcnt, userrec *user)
3723 user->idle_lastmsg = time(NULL);
3725 if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
3727 if (parameters[0][0] == '#')
3729 chan = FindChan(parameters[0]);
3732 if ((chan->noexternal) && (!has_channel(user,chan)))
3734 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
3737 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
3739 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
3745 FOREACH_RESULT(OnUserPreMessage(user,chan,TYPE_CHANNEL,std::string(parameters[1])));
3750 ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
3754 /* no such nick/channel */
3755 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3760 dest = Find(parameters[0]);
3763 if (strcmp(dest->awaymsg,""))
3765 /* auto respond with aweh msg */
3766 WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
3771 FOREACH_RESULT(OnUserPreMessage(user,dest,TYPE_USER,std::string(parameters[1])));
3776 WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
3780 /* no such nick/channel */
3781 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3785 void handle_notice(char **parameters, int pcnt, userrec *user)
3790 user->idle_lastmsg = time(NULL);
3792 if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
3794 if (parameters[0][0] == '#')
3796 chan = FindChan(parameters[0]);
3799 if ((chan->noexternal) && (!has_channel(user,chan)))
3801 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
3804 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
3806 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
3812 FOREACH_RESULT(OnUserPreNotice(user,chan,TYPE_CHANNEL,std::string(parameters[1])));
3817 WriteChannel(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
3821 /* no such nick/channel */
3822 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3827 dest = Find(parameters[0]);
3832 FOREACH_RESULT(OnUserPreNotice(user,dest,TYPE_USER,std::string(parameters[1])));
3837 WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
3841 /* no such nick/channel */
3842 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3848 char* chlist(userrec *user)
3853 log(DEBUG,"chlist: %s",user->nick);
3859 for (i = 0; i != MAXCHANS; i++)
3861 if (user->chans[i].channel != NULL)
3863 if (user->chans[i].channel->name)
3865 strcpy(cmp,user->chans[i].channel->name);
3867 if (!strstr(lst,cmp))
3869 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
3871 strcat(lst,cmode(user,user->chans[i].channel));
3872 strcat(lst,user->chans[i].channel->name);
3881 lst[strlen(lst)-1] = '\0'; // chop trailing space
3886 void handle_info(char **parameters, int pcnt, userrec *user)
3888 WriteServ(user->fd,"371 %s :The Inspire IRCd Project Has been brought to you by the following people..",user->nick);
3889 WriteServ(user->fd,"371 %s :Craig Edwards, Craig McLure, and Others..",user->nick);
3890 WriteServ(user->fd,"371 %s :Will finish this later when i can be arsed :p",user->nick);
3891 FOREACH_MOD OnInfo(user);
3892 WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
3895 void handle_time(char **parameters, int pcnt, userrec *user)
3898 struct tm * timeinfo;
3901 timeinfo = localtime ( &rawtime );
3902 WriteServ(user->fd,"391 %s %s :%s",user->nick,ServerName, asctime (timeinfo) );
3906 void handle_whois(char **parameters, int pcnt, userrec *user)
3911 if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
3913 dest = Find(parameters[0]);
3916 // bug found by phidjit - were able to whois an incomplete connection if it had sent a NICK or USER
3917 if (dest->registered == 7)
3919 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
3920 if ((user == dest) || (strchr(user->modes,'o')))
3922 WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
3924 if (strcmp(chlist(dest),""))
3926 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
3928 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
3929 if (strcmp(dest->awaymsg,""))
3931 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
3933 if (strchr(dest->modes,'o'))
3935 WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
3937 FOREACH_MOD OnWhois(user,dest);
3938 //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
3939 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
3941 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
3945 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3950 /* no such nick/channel */
3951 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3955 void handle_quit(char **parameters, int pcnt, userrec *user)
3957 user_hash::iterator iter = clientlist.find(user->nick);
3960 if (user->registered == 7)
3962 /* theres more to do here, but for now just close the socket */
3965 if (parameters[0][0] == ':')
3969 reason = parameters[0];
3971 if (strlen(reason)>MAXQUIT)
3973 reason[MAXQUIT-1] = '\0';
3976 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
3977 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
3978 WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
3982 Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
3983 WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
3984 WriteCommonExcept(user,"QUIT :Client exited");
3986 FOREACH_MOD OnUserQuit(user);
3990 /* push the socket on a stack of sockets due to be closed at the next opportunity */
3991 fd_reap.push_back(user->fd);
3993 if (iter != clientlist.end())
3995 log(DEBUG,"deleting user hash value %d",iter->second);
3996 if ((iter->second) && (user->registered == 7)) {
3997 delete iter->second;
3999 clientlist.erase(iter);
4002 if (user->registered == 7) {
4003 purge_empty_chans();
4009 void handle_who(char **parameters, int pcnt, userrec *user)
4011 chanrec* Ptr = NULL;
4013 /* theres more to do here, but for now just close the socket */
4016 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
4018 if (user->chans[0].channel)
4020 Ptr = user->chans[0].channel;
4021 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4023 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
4025 WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, i->second->server, i->second->nick, i->second->fullname);
4031 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
4035 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, user->nick);
4039 if (parameters[0][0] == '#')
4041 Ptr = FindChan(parameters[0]);
4044 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4046 if ((has_channel(i->second,Ptr)) && (isnick(i->second->nick)))
4048 WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, i->second->server, i->second->nick, i->second->fullname);
4051 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
4055 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4060 userrec* u = Find(parameters[0]);
4061 WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, u->nick, u->ident, u->dhost, u->server, u->nick, u->fullname);
4066 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
4068 Ptr = user->chans[0].channel;
4069 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4071 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
4073 if (strchr(i->second->modes,'o'))
4075 WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, i->second->server, i->second->nick, i->second->fullname);
4079 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
4085 void handle_wallops(char **parameters, int pcnt, userrec *user)
4087 WriteWallOps(user,"%s",parameters[0]);
4090 void handle_list(char **parameters, int pcnt, userrec *user)
4094 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
4095 for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
4097 if ((!i->second->c_private) && (!i->second->secret))
4099 WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
4102 WriteServ(user->fd,"323 %s :End of channel list.",user->nick);
4106 void handle_rehash(char **parameters, int pcnt, userrec *user)
4108 WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CleanFilename(CONFIG_FILE));
4110 FOREACH_MOD OnRehash();
4111 WriteOpers("%s is rehashing config file %s",user->nick,CleanFilename(CONFIG_FILE));
4117 return clientlist.size();
4121 int usercount_invisible(void)
4125 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4127 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
4132 int usercount_opers(void)
4136 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4138 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
4143 int usercount_unknown(void)
4147 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4149 if ((i->second->fd) && (i->second->registered != 7))
4157 return chanlist.size();
4160 int servercount(void)
4165 void handle_lusers(char **parameters, int pcnt, userrec *user)
4167 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),servercount());
4168 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
4169 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
4170 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
4171 WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,usercnt());
4174 void handle_admin(char **parameters, int pcnt, userrec *user)
4176 WriteServ(user->fd,"256 %s :Administrative info for %s",user->nick,ServerName);
4177 WriteServ(user->fd,"257 %s :Name - %s",user->nick,AdminName);
4178 WriteServ(user->fd,"258 %s :Nickname - %s",user->nick,AdminNick);
4179 WriteServ(user->fd,"258 %s :E-Mail - %s",user->nick,AdminEmail);
4182 void ShowMOTD(userrec *user)
4186 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
4189 WriteServ(user->fd,"375 %s :- %s message of the day",user->nick,ServerName);
4190 for (int i = 0; i != MOTD.size(); i++)
4192 WriteServ(user->fd,"372 %s :- %s",user->nick,MOTD[i].c_str());
4194 WriteServ(user->fd,"376 %s :End of %s message of the day.",user->nick,ServerName);
4197 void ShowRULES(userrec *user)
4201 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
4204 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
4205 for (int i = 0; i != RULES.size(); i++)
4207 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
4209 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
4212 /* shows the message of the day, and any other on-logon stuff */
4213 void ConnectUser(userrec *user)
4215 user->registered = 7;
4216 user->idle_lastmsg = time(NULL);
4217 log(DEBUG,"ConnectUser: %s",user->nick);
4219 if (strcmp(Passwd(user),"") && (!user->haspassed))
4221 kill_link(user,"Invalid password");
4226 kill_link(user,"Unauthorised connection");
4230 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
4231 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
4232 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
4233 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
4234 WriteServ(user->fd,"004 %s :%s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
4235 WriteServ(user->fd,"005 %s :MAP KNOCK SAFELIST HCN MAXCHANNELS=20 MAXBANS=60 NICKLEN=30 TOPICLEN=307 KICKLEN=307 MAXTARGETS=20 AWAYLEN=307 :are supported by this server",user->nick);
4236 WriteServ(user->fd,"005 %s :WALLCHOPS WATCH=128 SILENCE=5 MODES=13 CHANTYPES=# PREFIX=(ohv)@%c+ CHANMODES=ohvbeqa,kfL,l,psmntirRcOAQKVHGCuzN NETWORK=%s :are supported by this server",user->nick,'%',Network);
4238 FOREACH_MOD OnUserConnect(user);
4239 WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
4242 void handle_version(char **parameters, int pcnt, userrec *user)
4244 WriteServ(user->fd,"351 %s :%s %s %s :%s",user->nick,VERSION,"$Revision$ $Date$",ServerName,SYSTEM);
4247 void handle_ping(char **parameters, int pcnt, userrec *user)
4249 WriteServ(user->fd,"PONG %s :%s",ServerName,parameters[0]);
4252 void handle_pong(char **parameters, int pcnt, userrec *user)
4254 // set the user as alive so they survive to next ping
4258 void handle_motd(char **parameters, int pcnt, userrec *user)
4263 void handle_rules(char **parameters, int pcnt, userrec *user)
4268 void handle_user(char **parameters, int pcnt, userrec *user)
4270 if (user->registered < 3)
4272 if (isident(parameters[0]) == 0) {
4273 // This kinda Sucks, According to the RFC thou, its either this,
4274 // or "You have already registered" :p -- Craig
4275 WriteServ(user->fd,"461 %s USER :Not enough parameters",user->nick);
4278 WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
4279 strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
4280 strncat(user->ident,parameters[0],IDENTMAX);
4281 strncpy(user->fullname,parameters[3],128);
4282 user->registered = (user->registered | 1);
4287 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
4290 /* parameters 2 and 3 are local and remote hosts, ignored when sent by client connection */
4291 if (user->registered == 3)
4293 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
4298 void handle_userhost(char **parameters, int pcnt, userrec *user)
4300 char Return[MAXBUF],junk[MAXBUF];
4301 sprintf(Return,"302 %s :",user->nick);
4302 for (int i = 0; i < pcnt; i++)
4304 userrec *u = Find(parameters[i]);
4307 if (strchr(u->modes,'o'))
4309 sprintf(junk,"%s*=+%s@%s ",u->nick,u->ident,u->host);
4310 strcat(Return,junk);
4314 sprintf(junk,"%s=+%s@%s ",u->nick,u->ident,u->host);
4315 strcat(Return,junk);
4319 WriteServ(user->fd,Return);
4323 void handle_ison(char **parameters, int pcnt, userrec *user)
4325 char Return[MAXBUF];
4326 sprintf(Return,"303 %s :",user->nick);
4327 for (int i = 0; i < pcnt; i++)
4329 userrec *u = Find(parameters[i]);
4332 strcat(Return,u->nick);
4336 WriteServ(user->fd,Return);
4340 void handle_away(char **parameters, int pcnt, userrec *user)
4344 strcpy(user->awaymsg,parameters[0]);
4345 WriteServ(user->fd,"306 %s :You have been marked as being away",user->nick);
4349 strcpy(user->awaymsg,"");
4350 WriteServ(user->fd,"305 %s :You are no longer marked as being away",user->nick);
4354 void handle_whowas(char **parameters, int pcnt, userrec* user)
4356 user_hash::iterator i = whowas.find(parameters[0]);
4358 if (i == whowas.end())
4360 WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
4361 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
4365 time_t rawtime = i->second->signon;
4369 timeinfo = localtime(&rawtime);
4370 strcpy(b,asctime(timeinfo));
4371 b[strlen(b)-1] = '\0';
4373 WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
4374 WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
4375 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
4380 void handle_trace(char **parameters, int pcnt, userrec *user)
4382 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
4386 if (isnick(i->second->nick))
4388 if (strchr(i->second->modes,'o'))
4390 WriteServ(user->fd,"205 %s :Oper 0 %s",user->nick,i->second->nick);
4394 WriteServ(user->fd,"204 %s :User 0 %s",user->nick,i->second->nick);
4399 WriteServ(user->fd,"203 %s :???? 0 [%s]",user->nick,i->second->host);
4405 void handle_modules(char **parameters, int pcnt, userrec *user)
4407 for (int i = 0; i < module_names.size(); i++)
4409 Version V = modules[i]->GetVersion();
4410 char modulename[MAXBUF];
4411 strncpy(modulename,module_names[i].c_str(),256);
4412 WriteServ(user->fd,"900 %s :0x%08lx %d.%d.%d.%d %s",user->nick,modules[i],V.Major,V.Minor,V.Revision,V.Build,CleanFilename(modulename));
4416 void handle_stats(char **parameters, int pcnt, userrec *user)
4422 if (strlen(parameters[0])>1)
4424 /* make the stats query 1 character long */
4425 parameters[0][1] = '\0';
4428 /* stats m (list number of times each command has been used, plus bytecount) */
4429 if (!strcasecmp(parameters[0],"m"))
4431 for (int i = 0; i < cmdlist.size(); i++)
4433 if (cmdlist[i].handler_function)
4435 if (cmdlist[i].use_count)
4437 /* RPL_STATSCOMMANDS */
4438 WriteServ(user->fd,"212 %s %s %d %d",user->nick,cmdlist[i].command,cmdlist[i].use_count,cmdlist[i].total_bytes);
4445 /* stats z (debug and memory info) */
4446 if (!strcasecmp(parameters[0],"z"))
4448 WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
4449 WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
4450 WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
4451 WriteServ(user->fd,"249 %s :MOTD(VECTOR) %d, RULES(VECTOR) %d",user->nick,MOTD.size(),RULES.size());
4452 WriteServ(user->fd,"249 %s :address_cache(HASH_MAP) %d (%d buckets)",user->nick,IP.size(),IP.bucket_count());
4453 WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
4454 WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
4455 WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
4459 if (!strcasecmp(parameters[0],"o"))
4461 for (int i = 0; i < ConfValueEnum("oper",&config_f); i++)
4463 char LoginName[MAXBUF];
4464 char HostName[MAXBUF];
4465 char OperType[MAXBUF];
4466 ConfValue("oper","name",i,LoginName,&config_f);
4467 ConfValue("oper","host",i,HostName,&config_f);
4468 ConfValue("oper","type",i,OperType,&config_f);
4469 WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
4473 /* stats l (show user I/O stats) */
4474 if (!strcasecmp(parameters[0],"l"))
4476 WriteServ(user->fd,"211 %s :server:port nick bytes_in cmds_in bytes_out cmds_out",user->nick);
4477 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
4479 if (isnick(i->second->nick))
4481 WriteServ(user->fd,"211 %s :%s:%d %s %d %d %d %d",user->nick,ServerName,i->second->port,i->second->nick,i->second->bytes_in,i->second->cmds_in,i->second->bytes_out,i->second->cmds_out);
4485 WriteServ(user->fd,"211 %s :%s:%d (unknown@%d) %d %d %d %d",user->nick,ServerName,i->second->port,i->second->fd,i->second->bytes_in,i->second->cmds_in,i->second->bytes_out,i->second->cmds_out);
4491 /* stats u (show server uptime) */
4492 if (!strcasecmp(parameters[0],"u"))
4494 time_t current_time = 0;
4495 current_time = time(NULL);
4496 time_t server_uptime = current_time - startup_time;
4498 stime = gmtime(&server_uptime);
4499 /* i dont know who the hell would have an ircd running for over a year nonstop, but
4500 * Craig suggested this, and it seemed a good idea so in it went */
4501 if (stime->tm_year > 70)
4503 WriteServ(user->fd,"242 %s :Server up %d years, %d days, %.2d:%.2d:%.2d",user->nick,(stime->tm_year-70),stime->tm_yday,stime->tm_hour,stime->tm_min,stime->tm_sec);
4507 WriteServ(user->fd,"242 %s :Server up %d days, %.2d:%.2d:%.2d",user->nick,stime->tm_yday,stime->tm_hour,stime->tm_min,stime->tm_sec);
4511 WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
4512 WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
4516 void handle_connect(char **parameters, int pcnt, userrec *user)
4518 char Link_ServerName[1024];
4519 char Link_IPAddr[1024];
4520 char Link_Port[1024];
4521 char Link_Pass[1024];
4525 for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
4527 ConfValue("link","name",i,Link_ServerName,&config_f);
4528 ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
4529 ConfValue("link","port",i,Link_Port,&config_f);
4530 ConfValue("link","sendpass",i,Link_Pass,&config_f);
4531 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
4532 LinkPort = atoi(Link_Port);
4533 if (match(Link_ServerName,parameters[0])) {
4539 WriteServ(user->fd,"NOTICE %s :*** Failed to connect to %s: No servers matching this pattern are configured for linking.",user->nick,parameters[0]);
4543 // TODO: Perform a check here to stop a server being linked twice!
4545 WriteServ(user->fd,"NOTICE %s :*** Connecting to %s (%s) port %s...",user->nick,Link_ServerName,Link_IPAddr,Link_Port);
4547 if (me[defaultRoute])
4550 // at this point parameters[0] is an ip in a string.
4551 // TODO: Look this up from the <link> blocks instead!
4552 for (int j = 0; j < 255; j++) {
4553 if (servers[j] == NULL) {
4554 servers[j] = new serverrec;
4555 strcpy(servers[j]->internal_addr,Link_IPAddr);
4556 strcpy(servers[j]->name,Link_ServerName);
4557 log(DEBUG,"Allocated new serverrec");
4558 if (!me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass))
4560 WriteServ(user->fd,"NOTICE %s :*** Failed to send auth packet to %s!",user->nick,Link_IPAddr);
4565 WriteServ(user->fd,"NOTICE %s :*** Failed to create server record for address %s!",user->nick,Link_IPAddr);
4569 WriteServ(user->fd,"NOTICE %s :No default route is defined for server connections on this server. You must define a server connection to be default route so that sockets can be bound to it.",user->nick);
4573 void handle_squit(char **parameters, int pcnt, userrec *user)
4575 // send out an squit across the mesh and then clear the server list (for local squit)
4578 void handle_oper(char **parameters, int pcnt, userrec *user)
4580 char LoginName[MAXBUF];
4581 char Password[MAXBUF];
4582 char OperType[MAXBUF];
4583 char TypeName[MAXBUF];
4584 char Hostname[MAXBUF];
4587 for (i = 0; i < ConfValueEnum("oper",&config_f); i++)
4589 ConfValue("oper","name",i,LoginName,&config_f);
4590 ConfValue("oper","password",i,Password,&config_f);
4591 if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
4593 /* correct oper credentials */
4594 ConfValue("oper","type",i,OperType,&config_f);
4595 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
4596 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
4597 WriteServ(user->fd,"MODE %s :+o",user->nick);
4598 for (j =0; j < ConfValueEnum("type",&config_f); j++)
4600 ConfValue("type","name",j,TypeName,&config_f);
4601 if (!strcmp(TypeName,OperType))
4603 /* found this oper's opertype */
4604 ConfValue("type","host",j,Hostname,&config_f);
4605 strncpy(user->dhost,Hostname,256);
4608 if (!strchr(user->modes,'o'))
4610 strcat(user->modes,"o");
4612 FOREACH_MOD OnOper(user);
4617 WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
4618 WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
4621 void handle_nick(char **parameters, int pcnt, userrec *user)
4625 log(DEBUG,"not enough params for handle_nick");
4630 log(DEBUG,"invalid parameter passed to handle_nick");
4633 if (!strlen(parameters[0]))
4635 log(DEBUG,"zero length new nick passed to handle_nick");
4640 log(DEBUG,"invalid user passed to handle_nick");
4645 log(DEBUG,"invalid old nick passed to handle_nick");
4648 if (!strcasecmp(user->nick,parameters[0]))
4650 log(DEBUG,"old nick is new nick, skipping");
4655 if (strlen(parameters[0]) > 1)
4657 if (parameters[0][0] == ':')
4662 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
4664 WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
4668 if (isnick(parameters[0]) == 0)
4670 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
4674 if (user->registered == 7)
4676 WriteCommon(user,"NICK %s",parameters[0]);
4679 /* change the nick of the user in the users_hash */
4680 user = ReHashNick(user->nick, parameters[0]);
4681 /* actually change the nick within the record */
4683 if (!user->nick) return;
4685 strncpy(user->nick, parameters[0],NICKMAX);
4687 log(DEBUG,"new nick set: %s",user->nick);
4689 if (user->registered < 3)
4690 user->registered = (user->registered | 2);
4691 if (user->registered == 3)
4693 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
4696 log(DEBUG,"exit nickchange: %s",user->nick);
4699 void force_nickchange(userrec* user,const char* newnick)
4708 strncpy(nick,newnick,MAXBUF);
4710 if (user->registered == 7)
4714 handle_nick(pars,1,user);
4720 int process_parameters(char **command_p,char *parameters)
4725 q = strlen(parameters);
4728 /* no parameters, command_p invalid! */
4731 if (parameters[0] == ':')
4733 command_p[0] = parameters+1;
4738 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
4740 /* only one parameter */
4741 command_p[0] = parameters;
4742 if (parameters[0] == ':')
4744 if (strchr(parameters,' ') != NULL)
4752 command_p[j++] = parameters;
4753 for (i = 0; i <= q; i++)
4755 if (parameters[i] == ' ')
4757 command_p[j++] = parameters+i+1;
4758 parameters[i] = '\0';
4759 if (command_p[j-1][0] == ':')
4761 *command_p[j-1]++; /* remove dodgy ":" */
4763 /* parameter like this marks end of the sequence */
4767 return j; /* returns total number of items in the list */
4770 void process_command(userrec *user, char* cmd)
4774 char *command_p[127];
4775 char p[MAXBUF], temp[MAXBUF];
4776 int i, j, items, cmd_found;
4778 for (int i = 0; i < 127; i++)
4779 command_p[i] = NULL;
4789 if (!strcmp(cmd,""))
4794 int total_params = 0;
4797 for (int q = 0; q < strlen(cmd)-1; q++)
4799 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
4802 // found a 'trailing', we dont count them after this.
4810 // another phidjit bug...
4811 if (total_params > 126)
4813 kill_link(user,"Protocol violation (1)");
4820 FOREACH_MOD OnServerRaw(tmp,true);
4821 const char* cmd2 = tmp.c_str();
4822 snprintf(cmd,512,"%s",cmd2);
4824 if (!strchr(cmd,' '))
4826 /* no parameters, lets skip the formalities and not chop up
4828 log(DEBUG,"About to preprocess command with no params");
4830 command_p[0] = NULL;
4832 for (int i = 0; i <= strlen(cmd); i++)
4834 cmd[i] = toupper(cmd[i]);
4836 log(DEBUG,"Preprocess done length=%d",strlen(cmd));
4843 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
4844 for (i = 0; i < strlen(temp); i++)
4846 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
4852 /* split the full string into a command plus parameters */
4856 if (strchr(cmd,' '))
4858 for (i = 0; i <= strlen(cmd); i++)
4860 /* capitalise the command ONLY, leave params intact */
4861 cmd[i] = toupper(cmd[i]);
4862 /* are we nearly there yet?! :P */
4866 parameters = cmd+i+1;
4874 for (i = 0; i <= strlen(cmd); i++)
4876 cmd[i] = toupper(cmd[i]);
4883 if (strlen(command)>MAXCOMMAND)
4885 kill_link(user,"Protocol violation (2)");
4889 for (int x = 0; x < strlen(command); x++)
4891 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
4893 if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
4895 if (strchr("@!\"$%^&*(){}[]_-=+;:'#~,.<>/?\\|`",command[x]))
4897 kill_link(user,"Protocol violation (3)");
4904 for (i = 0; i != cmdlist.size(); i++)
4906 if (strcmp(cmdlist[i].command,""))
4908 if (strlen(command)>=(strlen(cmdlist[i].command))) if (!strncmp(command, cmdlist[i].command,MAXCOMMAND))
4910 log(DEBUG,"Found matching command");
4914 if (strcmp(parameters,""))
4916 items = process_parameters(command_p,parameters);
4921 command_p[0] = NULL;
4927 command_p[0] = NULL;
4932 log(DEBUG,"Processing command");
4934 /* activity resets the ping pending timer */
4935 user->nping = time(NULL) + 120;
4936 if ((items) < cmdlist[i].min_params)
4938 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
4939 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
4942 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
4944 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
4945 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
4949 /* if the command isnt USER, PASS, or NICK, and nick is empty,
4951 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
4953 if ((!isnick(user->nick)) || (user->registered != 7))
4955 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
4956 WriteServ(user->fd,"451 %s :You have not registered",command);
4960 if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
4962 log(DEBUG,"process_command: handler: %s %s %d",user->nick,command,items);
4963 if (cmdlist[i].handler_function)
4965 /* ikky /stats counters */
4970 user->bytes_in += strlen(temp);
4973 cmdlist[i].use_count++;
4974 cmdlist[i].total_bytes+=strlen(temp);
4977 /* WARNING: nothing may come after the
4978 * command handler call, as the handler
4979 * may free the user structure! */
4981 cmdlist[i].handler_function(command_p,items,user);
4987 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
4988 WriteServ(user->fd,"451 %s :You have not registered",command);
4996 if ((!cmd_found) && (user))
4998 log(DEBUG,"process_command: not in table: %s %s",user->nick,command);
4999 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
5004 void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
5007 /* create the command and push it onto the table */
5008 strcpy(comm.command,cmd);
5009 comm.handler_function = f;
5010 comm.flags_needed = flags;
5011 comm.min_params = minparams;
5013 comm.total_bytes = 0;
5014 cmdlist.push_back(comm);
5015 log(DEBUG,"Added command %s (%d parameters)",cmd,minparams);
5018 void SetupCommandTable(void)
5020 createcommand("USER",handle_user,0,4);
5021 createcommand("NICK",handle_nick,0,1);
5022 createcommand("QUIT",handle_quit,0,0);
5023 createcommand("VERSION",handle_version,0,0);
5024 createcommand("PING",handle_ping,0,1);
5025 createcommand("PONG",handle_pong,0,1);
5026 createcommand("ADMIN",handle_admin,0,0);
5027 createcommand("PRIVMSG",handle_privmsg,0,2);
5028 createcommand("INFO",handle_info,0,0);
5029 createcommand("TIME",handle_time,0,0);
5030 createcommand("WHOIS",handle_whois,0,1);
5031 createcommand("WALLOPS",handle_wallops,'o',1);
5032 createcommand("NOTICE",handle_notice,0,2);
5033 createcommand("JOIN",handle_join,0,1);
5034 createcommand("NAMES",handle_names,0,1);
5035 createcommand("PART",handle_part,0,1);
5036 createcommand("KICK",handle_kick,0,2);
5037 createcommand("MODE",handle_mode,0,1);
5038 createcommand("TOPIC",handle_topic,0,1);
5039 createcommand("WHO",handle_who,0,1);
5040 createcommand("MOTD",handle_motd,0,0);
5041 createcommand("RULES",handle_rules,0,0);
5042 createcommand("OPER",handle_oper,0,2);
5043 createcommand("LIST",handle_list,0,0);
5044 createcommand("DIE",handle_die,'o',1);
5045 createcommand("RESTART",handle_restart,'o',1);
5046 createcommand("KILL",handle_kill,'o',2);
5047 createcommand("REHASH",handle_rehash,'o',0);
5048 createcommand("LUSERS",handle_lusers,0,0);
5049 createcommand("STATS",handle_stats,0,1);
5050 createcommand("USERHOST",handle_userhost,0,1);
5051 createcommand("AWAY",handle_away,0,0);
5052 createcommand("ISON",handle_ison,0,0);
5053 createcommand("SUMMON",handle_summon,0,0);
5054 createcommand("USERS",handle_users,0,0);
5055 createcommand("INVITE",handle_invite,0,2);
5056 createcommand("PASS",handle_pass,0,1);
5057 createcommand("TRACE",handle_trace,'o',0);
5058 createcommand("WHOWAS",handle_whowas,0,1);
5059 createcommand("CONNECT",handle_connect,'o',1);
5060 createcommand("SQUIT",handle_squit,'o',1);
5061 createcommand("MODULES",handle_modules,'o',0);
5064 void process_buffer(const char* cmdbuf,userrec *user)
5068 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
5075 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
5078 if (!strcmp(cmdbuf,""))
5082 strncpy(cmd,cmdbuf,MAXBUF);
5083 if (!strcmp(cmd,""))
5087 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
5089 cmd[strlen(cmd)-1] = '\0';
5091 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
5093 cmd[strlen(cmd)-1] = '\0';
5095 if (!strcmp(cmd,""))
5099 log(DEBUG,"InspIRCd: processing: %s %s",user->nick,cmd);
5101 if ((user) && (cmd))
5103 process_command(user,cmd);
5107 void DoSync(serverrec* serv, char* udp_host,int udp_port, long MyKey)
5110 // send start of sync marker: Y <timestamp>
5111 snprintf(data,MAXBUF,"Y %d",time(NULL));
5112 serv->SendPacket(data,udp_host,udp_port,MyKey);
5113 // send users and channels
5114 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
5116 snprintf(data,MAXBUF,"N %d %s %s %s %s %s %s :%s",u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,u->second->server,u->second->fullname);
5117 serv->SendPacket(data,udp_host,udp_port,MyKey);
5118 if (strcmp(chlist(u->second),""))
5120 snprintf(data,MAXBUF,"J %s %s",u->second->nick,chlist(u->second));
5121 serv->SendPacket(data,udp_host,udp_port,MyKey);
5124 // send channel modes, topics etc...
5125 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
5127 snprintf(data,MAXBUF,"C %s %d %s %s",c->second->name,c->second->created,c->second->setby,chanmodes(c->second));
5128 serv->SendPacket(data,udp_host,udp_port,MyKey);
5129 if (strcmp(c->second->topic,""))
5131 snprintf(data,MAXBUF,"T %d %s %s",c->second->topicset,c->second->name,c->second->topic);
5133 serv->SendPacket(data,udp_host,udp_port,MyKey);
5134 // send current banlist
5136 for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
5138 snprintf(data,MAXBUF,"M %d %s +b %s",b->set_time,c->second->name,b->data);
5139 serv->SendPacket(data,udp_host,udp_port,MyKey);
5142 // send end of sync marker: E <timestamp>
5143 snprintf(data,MAXBUF,"F %d",time(NULL));
5144 serv->SendPacket(data,udp_host,udp_port,MyKey);
5148 void handle_N(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
5150 // %d %s %s %s %s %s %s :%s
5151 char* tm = strtok(params," ");
5152 char* nick = strtok(NULL," ");
5153 char* host = strtok(NULL," ");
5154 char* dhost = strtok(NULL," ");
5155 char* ident = strtok(NULL," ");
5156 char* modes = strtok(NULL," ");
5157 char* server = strtok(NULL," ");
5158 char* gecos = strtok(NULL," ");
5159 time_t TS = atoi(tm);
5160 user_hash::iterator iter = clientlist.find(nick);
5161 if (iter != clientlist.end())
5164 WriteOpers("Nickname collision: %s@%s != %s@%s",nick,server,iter->second->nick,iter->second->server);
5165 if (TS >= iter->second->age)
5168 snprintf(str,MAXBUF,"Killed (Nick Collision (%s@%s < %s@%s))",nick,server,iter->second->nick,iter->second->server);
5169 WriteServ(iter->second->fd, "KILL %s :%s",iter->second->nick,str);
5170 // client on remote server is older than the local user, kill the local user
5171 kill_link(iter->second,str);
5174 clientlist[nick] = new userrec();
5175 // remote users have an fd of -1. This is so that our Write abstraction
5176 // routines know to route any messages to this record away to whatever server
5178 clientlist[nick]->fd = -1;
5179 strncpy(clientlist[nick]->nick, nick,NICKMAX);
5180 strncpy(clientlist[nick]->host, host,160);
5181 strncpy(clientlist[nick]->dhost, dhost,160);
5182 strncpy(clientlist[nick]->server, server,256);
5183 strncpy(clientlist[nick]->ident, ident,10); // +1 char to compensate for '~'
5184 //strncpy(clientlist[nick]->fullname, gecos,128);
5185 clientlist[nick]->signon = TS;
5186 clientlist[nick]->nping = 0; // this is ignored for a remote user anyway.
5187 clientlist[nick]->lastping = 1;
5188 clientlist[nick]->port = 0; // so is this...
5189 clientlist[nick]->registered = 7; // this however we need to set for them to receive messages and appear online
5190 clientlist[nick]->idle_lastmsg = time(NULL); // this is unrealiable and wont actually be used locally
5193 void handle_J(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
5196 // The J token currently has no timestamp - this needs looking at
5197 // because it will allow splitriding.
5198 char* nick = strtok(params," ");
5199 char* channel = strtok(NULL," ");
5200 userrec* user = Find(nick);
5203 if ((user != NULL) && (strcmp(channel,"")))
5205 char privilage = '\0';
5206 if (channel[0] != '#')
5208 privilage = channel[0];
5211 add_channel(user,channel,"",true);
5213 // now work out the privilages they should have on each channel
5214 // and send the appropriate servermodes.
5215 for (int i = 0; i != MAXCHANS; i++)
5217 if (user->chans[i].channel)
5219 if (!strcasecmp(user->chans[i].channel->name,channel))
5221 if (privilage == '@')
5223 user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_OP;
5224 WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +o %s",channel,user->nick);
5226 if (privilage == '%')
5228 user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_HOP;
5229 WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +h %s",channel,user->nick);
5231 if (privilage == '+')
5233 user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_VOICE;
5234 WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +o %s",channel,user->nick);
5241 channel = strtok(NULL," ");
5245 void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
5253 // N <TS> <NICK> <HOST> <DHOST> <IDENT> <MODES> <SERVER> :<GECOS>
5254 // introduce remote client
5256 handle_N(token,params,source,reply,udp_host,udp_port);
5258 // J <NICK> <CHANLIST>
5259 // Join user to channel list, merge channel permissions
5261 handle_J(token,params,source,reply,udp_host,udp_port);
5263 // C <CHANNEL> <TS> <TOPICSETTER> <MODES>
5264 // initialise channel (netburst only)
5267 // T <TS> <CHANNEL> <TOPIC>
5268 // change channel topic (netburst only)
5271 // M <TS> <TARGET> <MODES> [MODE-PARAMETERS]
5272 // Set modes on an object
5281 WriteOpers("WARNING! Unknown datagram type '%c'",token);
5287 void handle_link_packet(long theirkey, char* udp_msg, char* udp_host, int udp_port, serverrec *serv)
5289 char response[10240];
5290 char token = udp_msg[0];
5291 char* params = udp_msg + 2;
5292 char finalparam[1024];
5293 strcpy(finalparam," :xxxx");
5294 if (strstr(params," :")) {
5295 strncpy(finalparam,strstr(params," :"),1024);
5298 // S test.chatspike.net password :ChatSpike InspIRCd test server
5299 char* servername = strtok(params," ");
5300 char* password = strtok(NULL," ");
5301 char* serverdesc = finalparam+2;
5302 WriteOpers("CONNECT from %s (%s)",servername,udp_host,password,serverdesc);
5305 char Link_ServerName[1024];
5306 char Link_IPAddr[1024];
5307 char Link_Port[1024];
5308 char Link_Pass[1024];
5309 char Link_SendPass[1024];
5312 // search for a corresponding <link> block in the config files
5313 for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
5315 ConfValue("link","name",i,Link_ServerName,&config_f);
5316 ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
5317 ConfValue("link","port",i,Link_Port,&config_f);
5318 ConfValue("link","recvpass",i,Link_Pass,&config_f);
5319 ConfValue("link","sendpass",i,Link_SendPass,&config_f);
5320 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
5321 LinkPort = atoi(Link_Port);
5322 if (!strcasecmp(Link_ServerName,servername)) {
5323 if (!strcasecmp(Link_IPAddr,udp_host)) {
5324 if (LinkPort == udp_port) {
5325 // we have a matching link line -
5326 // send a 'diminutive' server message back...
5327 snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
5328 serv->SendPacket(response,udp_host,udp_port,0);
5329 WriteOpers("CONNECT from %s accepted, authenticating",servername);
5330 for (int j = 0; j < 255; j++) {
5331 if (servers[j] == NULL) {
5332 servers[j] = new serverrec;
5333 strcpy(servers[j]->internal_addr,udp_host);
5334 strcpy(servers[j]->name,servername);
5335 // create a server record for this server
5336 snprintf(response,10240,"O %d",MyKey);
5337 serv->SendPacket(response,udp_host,udp_port,0);
5341 WriteOpers("Internal error connecting to %s, failed to create server record!",servername);
5345 log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
5349 log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
5353 log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
5356 serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
5357 WriteOpers("CONNECT from %s denied, no matching link block",servername);
5362 // if this is received, this means the server-ip that sent it said "OK" to credentials.
5363 // only when a server says this do we exchange keys. The server MUST have an entry in the servers
5364 // array, which is only added by an 'S' packet or BeginLink().
5365 for (int i = 0; i < 255; i++) {
5366 if (servers[i] != NULL) {
5367 if (!strcasecmp(servers[i]->internal_addr,udp_host)) {
5368 servers[i]->key = atoi(params);
5369 log(DEBUG,"Key for this server is now %d",servers[i]->key);
5370 DoSync(serv,udp_host,udp_port,MyKey);
5375 WriteOpers("\2WARNING!\2 Server ip %s attempted a key exchange, but is not in the authentication state! Possible intrusion attempt!",udp_host);
5379 // S test.chatspike.net password :ChatSpike InspIRCd test server
5380 char* servername = strtok(params," ");
5381 char* password = strtok(NULL," ");
5382 char* serverdesc = finalparam+2;
5384 // TODO: we should do a check here to ensure that this server is one we recently initiated a
5385 // link with, and didnt hear an 's' or 'E' back from yet (these are the only two valid responses
5386 // to an 'S' command. If we didn't recently send an 'S' to this server, theyre trying to spoof
5387 // a connect, so put out an oper alert!
5392 // for now, just accept all, we'll fix that later.
5393 WriteOpers("%s accepted our link credentials ",servername);
5395 char Link_ServerName[1024];
5396 char Link_IPAddr[1024];
5397 char Link_Port[1024];
5398 char Link_Pass[1024];
5399 char Link_SendPass[1024];
5402 // search for a corresponding <link> block in the config files
5403 for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
5405 ConfValue("link","name",i,Link_ServerName,&config_f);
5406 ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
5407 ConfValue("link","port",i,Link_Port,&config_f);
5408 ConfValue("link","recvpass",i,Link_Pass,&config_f);
5409 ConfValue("link","sendpass",i,Link_SendPass,&config_f);
5410 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
5411 LinkPort = atoi(Link_Port);
5412 if (!strcasecmp(Link_ServerName,servername)) {
5413 if (!strcasecmp(Link_IPAddr,udp_host)) {
5414 if (LinkPort == udp_port) {
5415 // matching link at this end too, we're all done!
5416 // at this point we must begin key exchange and insert this
5417 // server into our 'active' table.
5418 for (int j = 0; j < 255; j++) {
5419 if (servers[j] != NULL) {
5420 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
5421 WriteOpers("Server %s authenticated, exchanging server keys...",servername);
5422 snprintf(response,10240,"O %d",MyKey);
5423 serv->SendPacket(response,udp_host,udp_port,0);
5428 WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right noe! Possible intrusion attempt!",udp_host);
5433 log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
5437 log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
5441 log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
5444 serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
5445 WriteOpers("CONNECT from %s denied, no matching link block",servername);
5450 char* error_message = finalparam+2;
5451 WriteOpers("ERROR from %s: %s",udp_host,error_message);
5452 // remove this server from any lists
5453 for (int j = 0; j < 255; j++) {
5454 if (servers[j] != NULL) {
5455 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
5465 serverrec* source_server = NULL;
5467 for (int j = 0; j < 255; j++) {
5468 if (servers[j] != NULL) {
5469 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
5470 if (servers[j]->key == theirkey) {
5471 // found a valid key for this server, can process restricted stuff here
5472 process_restricted_commands(token,params,servers[j],serv,udp_host,udp_port);
5473 log(DEBUG,"Sync: exit 2");
5480 log(DEBUG,"Unrecognised token or unauthenticated host in datagram from %s:%d: %c",udp_host,udp_port,token);
5484 int reap_counter = 0;
5488 struct sockaddr_in client, server;
5489 char addrs[MAXBUF][255];
5490 int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
5492 int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
5493 int selectResult = 0, selectResult2 = 0;
5494 char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
5495 char resolvedHost[MAXBUF];
5499 log_file = fopen("ircd.log","a+");
5502 printf("ERROR: Could not write to logfile ircd.log, bailing!\n\n");
5506 log(DEBUG,"InspIRCd: startup: begin");
5510 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
5512 log(DEBUG,"InspIRCd: startup: not starting with UID 0!");
5514 SetupCommandTable();
5515 log(DEBUG,"InspIRCd: startup: default command table set up");
5518 if (strcmp(DieValue,""))
5520 printf("WARNING: %s\n\n",DieValue);
5523 log(DEBUG,"InspIRCd: startup: read config");
5525 int count2 = 0, count3 = 0;
5527 for (count = 0; count < ConfValueEnum("bind",&config_f); count++)
5529 ConfValue("bind","port",count,configToken,&config_f);
5530 ConfValue("bind","address",count,Addr,&config_f);
5531 ConfValue("bind","type",count,Type,&config_f);
5532 if (!strcmp(Type,"servers"))
5534 char Default[MAXBUF];
5535 strcpy(Default,"no");
5536 ConfValue("bind","default",count,Default,&config_f);
5537 if (strchr(Default,'y'))
5539 defaultRoute = count3;
5540 log(DEBUG,"InspIRCd: startup: binding '%s:%s' is default server route",Addr,configToken);
5542 me[count3] = new serverrec(ServerName,100L,false);
5543 me[count3]->CreateListener(Addr,atoi(configToken));
5548 ports[count2] = atoi(configToken);
5549 strcpy(addrs[count2],Addr);
5552 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
5555 UDPportCount = count3;
5557 log(DEBUG,"InspIRCd: startup: read %d total client ports and %d total server ports",portCount,UDPportCount);
5559 log(DEBUG,"InspIRCd: startup: InspIRCd is now running!");
5563 /* BugFix By Craig! :p */
5565 for (count2 = 0; count2 < ConfValueEnum("module",&config_f); count2++)
5567 char modfile[MAXBUF];
5568 ConfValue("module","name",count2,configToken,&config_f);
5569 sprintf(modfile,"%s/%s",MOD_PATH,configToken,&config_f);
5570 printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
5571 log(DEBUG,"InspIRCd: startup: Loading module: %s",modfile);
5572 /* If The File Doesnt exist, Trying to load it
5573 * Will Segfault the IRCd.. So, check to see if
5574 * it Exists, Before Proceeding. */
5575 if (FileExists(modfile))
5577 factory[count] = new ircd_module(modfile);
5578 if (factory[count]->LastError())
5580 log(DEBUG,"Unable to load %s: %s",modfile,factory[count]->LastError());
5581 sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
5584 if (factory[count]->factory)
5586 modules[count] = factory[count]->factory->CreateModule();
5587 /* save the module and the module's classfactory, if
5588 * this isnt done, random crashes can occur :/ */
5589 module_names.push_back(modfile);
5593 log(DEBUG,"Unable to load %s",modfile);
5594 sprintf("Unable to load %s\nExiting...\n",modfile);
5597 /* Increase the Count */
5602 log(DEBUG,"InspIRCd: startup: Module Not Found %s",modfile);
5603 printf("Module Not Found: \033[1;37m%s\033[0;37m, Skipping\n",modfile);
5606 MODCOUNT = count - 1;
5607 log(DEBUG,"Total loaded modules: %d",MODCOUNT+1);
5609 printf("\nInspIRCd is now running!\n");
5611 startup_time = time(NULL);
5615 log(VERBOSE,"Not forking as -nofork was specified");
5619 if (DaemonSeed() == ERROR)
5621 log(DEBUG,"InspIRCd: startup: can't daemonise");
5622 printf("ERROR: could not go into daemon mode. Shutting down.\n");
5628 /* setup select call */
5629 FD_ZERO(&selectFds);
5630 log(DEBUG,"InspIRCd: startup: zero selects");
5631 log(VERBOSE,"InspIRCd: startup: portCount = %d", portCount);
5633 for (count = 0; count < portCount; count++)
5635 if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
5637 log(DEBUG,"InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
5640 if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
5642 log(DEBUG,"InspIRCd: startup: failed to bind port %d",ports[count]);
5644 else /* well we at least bound to one socket so we'll continue */
5650 log(DEBUG,"InspIRCd: startup: total bound ports %d",boundPortCount);
5652 /* if we didn't bind to anything then abort */
5653 if (boundPortCount == 0)
5655 log(DEBUG,"InspIRCd: startup: no ports bound, bailing!");
5659 length = sizeof (client);
5660 int flip_flop = 0, udp_port = 0;
5661 char udp_msg[MAXBUF], udp_host[MAXBUF];
5663 /* main loop, this never returns */
5668 struct timeval tval;
5671 user_hash::iterator count2 = clientlist.begin();
5673 // *FIX* Instead of closing sockets in kill_link when they receive the ERROR :blah line, we should queue
5674 // them in a list, then reap the list every second or so.
5675 if (reap_counter>5000)
5677 if (fd_reap.size() > 0)
5679 for( int n = 0; n < fd_reap.size(); n++)
5681 Blocking(fd_reap[n]);
5683 NonBlocking(fd_reap[n]);
5691 for (int x = 0; x != UDPportCount; x++)
5694 if (me[x]->RecvPacket(udp_msg, udp_host, udp_port, theirkey))
5696 if (strlen(udp_msg)<1) {
5697 log(DEBUG,"Invalid datagram from %s:%d:%d [route%d]",udp_host,udp_port,me[x]->port,x);
5701 FOREACH_MOD OnPacketReceive(udp_msg);
5702 // Packets must go back via the route they arrived on :)
5703 handle_link_packet(theirkey, udp_msg, udp_host, udp_port, me[x]);
5704 // link packets can manipulate the usertable so beware of
5705 // any loops here watching the user or channels hash
5706 log(DEBUG,"Sync: exit 3");
5713 while (count2 != clientlist.end())
5716 tval.tv_usec = tval.tv_sec = 0;
5718 int total_in_this_set = 0;
5720 user_hash::iterator xcount = count2;
5721 user_hash::iterator endingiter = count2;
5723 if (!count2->second) break;
5726 if (count2->second->fd != 0)
5728 // assemble up to 64 sockets into an fd_set
5729 // to implement a pooling mechanism.
5731 // This should be up to 64x faster than the
5732 // old implementation.
5733 while (total_in_this_set < 64)
5735 if (count2 != clientlist.end())
5737 // we don't check the state of remote users.
5738 if (count2->second->fd > 0)
5740 FD_SET (count2->second->fd, &sfd);
5742 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
5743 // their connection class.
5744 if ((time(NULL) > count2->second->timeout) && (count2->second->registered != 7))
5746 log(DEBUG,"InspIRCd: registration timeout: %s",count2->second->nick);
5747 kill_link(count2->second,"Registration timeout");
5750 if (((time(NULL)) > count2->second->nping) && (isnick(count2->second->nick)) && (count2->second->registered == 7))
5752 if ((!count2->second->lastping) && (count2->second->registered == 7))
5754 log(DEBUG,"InspIRCd: ping timeout: %s",count2->second->nick);
5755 kill_link(count2->second,"Ping timeout");
5758 Write(count2->second->fd,"PING :%s",ServerName);
5759 log(DEBUG,"InspIRCd: pinging: %s",count2->second->nick);
5760 count2->second->lastping = 0;
5761 count2->second->nping = time(NULL)+120;
5765 total_in_this_set++;
5770 endingiter = count2;
5771 count2 = xcount; // roll back to where we were
5777 selectResult2 = select(65535, &sfd, NULL, NULL, &tval);
5779 // now loop through all of the items in this pool if any are waiting
5780 //if (selectResult2 > 0)
5781 for (user_hash::iterator count2a = xcount; count2a != endingiter; count2a++)
5784 if ((count2a->second->fd != -1) && (FD_ISSET (count2a->second->fd, &sfd)))
5786 log(DEBUG,"Reading fd %d",count2a->second->fd);
5787 memset(data, 0, 10240);
5788 result = read(count2a->second->fd, data, 10240);
5792 log(DEBUG,"Read %d characters from socket",result);
5793 userrec* current = count2a->second;
5794 int currfd = current->fd;
5795 char* l = strtok(data,"\n");
5800 if ((floodlines > current->flood) && (current->flood != 0))
5802 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
5803 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
5804 kill_link(current,"Excess flood");
5807 char sanitized[NetBufferSize];
5808 memset(sanitized, 0, NetBufferSize);
5810 for (int pt = 0; pt < strlen(l); pt++)
5814 sanitized[ptt++] = l[pt];
5817 sanitized[ptt] = '\0';
5818 if (strlen(sanitized))
5822 // we're gonna re-scan to check if the nick is gone, after every
5823 // command - if it has, we're gonna bail
5824 bool find_again = false;
5825 process_buffer(sanitized,current);
5827 // look for the user's record in case it's changed
5828 for (user_hash::iterator c2 = clientlist.begin(); c2 != clientlist.end(); c2++)
5830 if (c2->second->fd == currfd)
5832 // found again, update pointer
5833 current == c2->second;
5842 l = strtok(NULL,"\n");
5847 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
5849 log(DEBUG,"killing: %s",count2a->second->nick);
5850 kill_link(count2a->second,strerror(errno));
5854 // result EAGAIN means nothing read
5855 if (result == EAGAIN)
5863 log(DEBUG,"InspIRCd: Exited: %s",count2a->second->nick);
5864 kill_link(count2a->second,"Client exited");
5865 // must bail here? kill_link removes the hash, corrupting the iterator
5866 log(DEBUG,"Bailing from client exit");
5870 else if (result > 0)
5875 for (int q = 0; q < total_in_this_set; q++)
5877 // there is no iterator += operator :(
5878 //if (count2 != clientlist.end())
5885 // set up select call
5886 for (count = 0; count < boundPortCount; count++)
5888 FD_SET (openSockfd[count], &selectFds);
5891 /* added timeout! select was waiting forever... wank... :/ */
5903 selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
5905 /* select is reporting a waiting socket. Poll them all to find out which */
5906 if (selectResult > 0)
5908 char target[MAXBUF], resolved[MAXBUF];
5909 for (count = 0; count < boundPortCount; count++)
5911 if (FD_ISSET (openSockfd[count], &selectFds))
5913 incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
5915 address_cache::iterator iter = IP.find(client.sin_addr);
5916 bool iscached = false;
5917 if (iter == IP.end())
5919 /* ip isn't in cache, add it */
5920 strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
5921 if(CleanAndResolve(resolved, target) != TRUE)
5923 strncpy(resolved,target,MAXBUF);
5925 /* hostname now in 'target' */
5926 IP[client.sin_addr] = new string(resolved);
5927 /* hostname in cache */
5931 /* found ip (cached) */
5932 strncpy(resolved, iter->second->c_str(), MAXBUF);
5936 if (incomingSockfd < 0)
5938 WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
5939 log(DEBUG,"InspIRCd: accept failed: %d",ports[count]);
5943 AddClient(incomingSockfd, resolved, ports[count], iscached);
5944 log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
5951 if(0) {}; // "Label must be followed by a statement"... so i gave it one.
5954 close (incomingSockfd);