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
79 time_t nb_start = time(NULL);
81 extern vector<Module*> modules;
82 std::vector<std::string> module_names;
83 extern vector<ircd_module*> factory;
84 std::vector<int> fd_reap;
92 template<> struct nspace::hash<in_addr>
94 size_t operator()(const struct in_addr &a) const
97 memcpy(&q,&a,sizeof(size_t));
102 template<> struct nspace::hash<string>
104 size_t operator()(const string &s) const
107 static struct hash<const char *> strhash;
119 bool operator()(const string& s1, const string& s2) const
121 char a[MAXBUF],b[MAXBUF];
122 strcpy(a,s1.c_str());
123 strcpy(b,s2.c_str());
124 return (strcasecmp(a,b) == 0);
129 struct InAddr_HashComp
132 bool operator()(const in_addr &s1, const in_addr &s2) const
137 memcpy(&q,&s1,sizeof(size_t));
138 memcpy(&p,&s2,sizeof(size_t));
146 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
147 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
148 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
149 typedef std::deque<command_t> command_table;
152 serverrec* servers[255];
156 user_hash clientlist;
159 command_table cmdlist;
166 struct linger linger = { 0 };
167 char bannerBuffer[MAXBUF];
168 int boundPortCount = 0;
169 int portCount = 0, UDPportCount = 0, ports[MAXSOCKS];
170 int defaultRoute = 0;
174 long MyKey = C.GenKey();
178 int has_channel(userrec *u, chanrec *c);
179 int usercount(chanrec *c);
180 int usercount_i(chanrec *c);
181 void update_stats_l(int fd,int data_out);
182 char* Passwd(userrec *user);
183 bool IsDenied(userrec *user);
184 void AddWhoWas(userrec* u);
186 std::stringstream config_f(stringstream::in | stringstream::out);
188 void safedelete(userrec *p)
192 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
193 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
199 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
203 void safedelete(chanrec *p)
209 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
213 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
218 void tidystring(char* str)
220 // strips out double spaces before a : parameter
223 bool go_again = true;
230 while ((str[0] == ' ') && (strlen(str)>0))
237 bool noparse = false;
240 while (a < strlen(str))
242 if ((a<strlen(str)-1) && (noparse==false))
244 if ((str[a] == ' ') && (str[a+1] == ' '))
246 log(DEBUG,"Tidied extra space out of string: %s",str);
254 if ((str[a] == ' ') && (str[a+1] == ':'))
260 temp[t++] = str[a++];
263 strncpy(str,temp,MAXBUF);
267 /* chop a string down to 512 characters and preserve linefeed (irc max
274 log(DEBUG,"ERROR! Null string passed to chop()!");
278 FOREACH_MOD OnServerRaw(temp,false);
279 const char* str2 = temp.c_str();
280 sprintf(str,"%s",str2);
283 if (strlen(str) >= 512)
292 std::string getservername()
297 std::string getserverdesc()
302 std::string getnetworkname()
307 std::string getadminname()
312 std::string getadminemail()
317 std::string getadminnick()
322 void log(int level,char *text, ...)
324 char textbuffer[MAXBUF];
327 struct tm * timeinfo;
328 if (level < LogLevel)
332 timeinfo = localtime (&rawtime);
337 va_start (argsPtr, text);
338 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
340 strcpy(b,asctime(timeinfo));
341 b[strlen(b)-1] = ':';
342 fprintf(log_file,"%s %s\n",b,textbuffer);
345 // nofork enabled? display it on terminal too
346 printf("%s %s\n",b,textbuffer);
351 void readfile(file_cache &F, const char* fname)
354 char linebuf[MAXBUF];
356 log(DEBUG,"readfile: loading %s",fname);
358 file = fopen(fname,"r");
363 fgets(linebuf,sizeof(linebuf),file);
364 linebuf[strlen(linebuf)-1]='\0';
365 if (!strcmp(linebuf,""))
371 F.push_back(linebuf);
378 log(DEBUG,"readfile: failed to load file: %s",fname);
380 log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
383 void ReadConfig(void)
385 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF];
388 LoadConf(CONFIG_FILE,&config_f);
390 ConfValue("server","name",0,ServerName,&config_f);
391 ConfValue("server","description",0,ServerDesc,&config_f);
392 ConfValue("server","network",0,Network,&config_f);
393 ConfValue("admin","name",0,AdminName,&config_f);
394 ConfValue("admin","email",0,AdminEmail,&config_f);
395 ConfValue("admin","nick",0,AdminNick,&config_f);
396 ConfValue("files","motd",0,motd,&config_f);
397 ConfValue("files","rules",0,rules,&config_f);
398 ConfValue("power","diepass",0,diepass,&config_f);
399 ConfValue("power","pause",0,pauseval,&config_f);
400 ConfValue("power","restartpass",0,restartpass,&config_f);
401 ConfValue("options","prefixquit",0,PrefixQuit,&config_f);
402 ConfValue("die","value",0,DieValue,&config_f);
403 ConfValue("options","loglevel",0,dbg,&config_f);
404 ConfValue("options","netbuffersize",0,NB,&config_f);
405 NetBufferSize = atoi(NB);
406 if ((!NetBufferSize) || (NetBufferSize > 65535) || (NetBufferSize < 1024))
408 log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
409 NetBufferSize = 10240;
411 if (!strcmp(dbg,"debug"))
413 if (!strcmp(dbg,"verbose"))
415 if (!strcmp(dbg,"default"))
417 if (!strcmp(dbg,"sparse"))
419 if (!strcmp(dbg,"none"))
422 log(DEBUG,"Reading message of the day");
423 readfile(RULES,rules);
424 log(DEBUG,"Reading connect classes");
426 for (int i = 0; i < ConfValueEnum("connect",&config_f); i++)
429 ConfValue("connect","allow",i,Value,&config_f);
430 ConfValue("connect","timeout",i,timeout,&config_f);
431 ConfValue("connect","flood",i,flood,&config_f);
432 if (strcmp(Value,""))
434 strcpy(c.host,Value);
437 ConfValue("connect","password",i,Value,&config_f);
438 strcpy(c.pass,Value);
439 c.registration_timeout = 90; // default is 2 minutes
440 c.flood = atoi(flood);
443 c.registration_timeout = atoi(timeout);
445 Classes.push_back(c);
446 log(DEBUG,"Read connect class type ALLOW, host=%s password=%s timeout=%d flood=%d",c.host,c.pass,c.registration_timeout,c.flood);
450 ConfValue("connect","deny",i,Value,&config_f);
451 strcpy(c.host,Value);
453 Classes.push_back(c);
454 log(DEBUG,"Read connect class type DENY, host=%s",c.host);
463 log(DEBUG,"Blocking: %d",s);
464 flags = fcntl(s, F_GETFL, 0);
465 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
468 void NonBlocking(int s)
471 log(DEBUG,"NonBlocking: %d",s);
472 flags = fcntl(s, F_GETFL, 0);
473 //fcntl(s, F_SETFL, O_NONBLOCK);
474 fcntl(s, F_SETFL, flags | O_NONBLOCK);
478 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
480 struct hostent *hostPtr = NULL;
483 memset (resolvedHost, '\0',MAXBUF);
484 if(unresolvedHost == NULL)
486 if ((inet_aton(unresolvedHost,&addr)) == 0)
488 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
490 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
492 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
496 /* write formatted text to a socket, in same format as printf */
498 void Write(int sock,char *text, ...)
502 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
505 char textbuffer[MAXBUF];
509 va_start (argsPtr, text);
510 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
512 sprintf(tb,"%s\r\n",textbuffer);
516 write(sock,tb,strlen(tb));
517 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
521 /* write a server formatted numeric response to a single socket */
523 void WriteServ(int sock, char* text, ...)
527 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
530 char textbuffer[MAXBUF],tb[MAXBUF];
532 va_start (argsPtr, text);
534 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
536 sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
540 write(sock,tb,strlen(tb));
541 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
545 /* write text from an originating user to originating user */
547 void WriteFrom(int sock, userrec *user,char* text, ...)
549 if ((!text) || (!user))
551 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
554 char textbuffer[MAXBUF],tb[MAXBUF];
556 va_start (argsPtr, text);
558 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
560 sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
564 write(sock,tb,strlen(tb));
565 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
569 /* write text to an destination user from a source user (e.g. user privmsg) */
571 void WriteTo(userrec *source, userrec *dest,char *data, ...)
573 if ((!dest) || (!data))
575 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
578 char textbuffer[MAXBUF],tb[MAXBUF];
580 va_start (argsPtr, data);
581 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
585 // if no source given send it from the server.
588 WriteServ(dest->fd,":%s %s",ServerName,textbuffer);
592 WriteFrom(dest->fd,source,"%s",textbuffer);
596 /* write formatted text from a source user to all users on a channel
597 * including the sender (NOT for privmsg, notice etc!) */
599 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
601 if ((!Ptr) || (!user) || (!text))
603 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
606 char textbuffer[MAXBUF];
608 va_start (argsPtr, text);
609 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
611 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
613 if (has_channel(i->second,Ptr))
615 WriteTo(user,i->second,"%s",textbuffer);
620 /* write formatted text from a source user to all users on a channel
621 * including the sender (NOT for privmsg, notice etc!) doesnt send to
622 * users on remote servers */
624 void WriteChannelLocal(chanrec* Ptr, userrec* user, char* text, ...)
626 if ((!Ptr) || (!text))
628 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
631 char textbuffer[MAXBUF];
633 va_start (argsPtr, text);
634 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
636 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
638 if (has_channel(i->second,Ptr))
640 if (i->second->fd != -1)
644 WriteServ(i->second->fd,"%s",textbuffer);
648 WriteTo(user,i->second,"%s",textbuffer);
656 void WriteChannelWithServ(char* ServerName, chanrec* Ptr, userrec* user, char* text, ...)
658 if ((!Ptr) || (!user) || (!text))
660 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
663 char textbuffer[MAXBUF];
665 va_start (argsPtr, text);
666 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
668 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
672 if (has_channel(i->second,Ptr))
674 WriteServ(i->second->fd,"%s",textbuffer);
681 /* write formatted text from a source user to all users on a channel except
682 * for the sender (for privmsg etc) */
684 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
686 if ((!Ptr) || (!user) || (!text))
688 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
691 char textbuffer[MAXBUF];
693 va_start (argsPtr, text);
694 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
697 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
701 if (has_channel(i->second,Ptr) && (user != i->second))
703 WriteTo(user,i->second,"%s",textbuffer);
709 int c_count(userrec* u)
712 for (int i =0; i != MAXCHANS; i++)
713 if (u->chans[i].channel != NULL)
719 /* return 0 or 1 depending if users u and u2 share one or more common channels
720 * (used by QUIT, NICK etc which arent channel specific notices) */
722 int common_channels(userrec *u, userrec *u2)
729 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
732 for (int i = 0; i != MAXCHANS; i++)
734 for (z = 0; z != MAXCHANS; z++)
736 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
738 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
740 if ((c_count(u)) && (c_count(u2)))
751 /* write a formatted string to all users who share at least one common
752 * channel, including the source user e.g. for use in NICK */
754 void WriteCommon(userrec *u, char* text, ...)
758 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
762 if (u->registered != 7) {
763 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
767 char textbuffer[MAXBUF];
769 va_start (argsPtr, text);
770 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
773 WriteFrom(u->fd,u,"%s",textbuffer);
775 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
779 if (common_channels(u,i->second) && (i->second != u))
781 WriteFrom(i->second->fd,u,"%s",textbuffer);
787 /* write a formatted string to all users who share at least one common
788 * channel, NOT including the source user e.g. for use in QUIT */
790 void WriteCommonExcept(userrec *u, char* text, ...)
794 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
798 if (u->registered != 7) {
799 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
803 char textbuffer[MAXBUF];
805 va_start (argsPtr, text);
806 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
809 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
813 if ((common_channels(u,i->second)) && (u != i->second))
815 WriteFrom(i->second->fd,u,"%s",textbuffer);
821 void WriteOpers(char* text, ...)
825 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
829 char textbuffer[MAXBUF];
831 va_start (argsPtr, text);
832 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
835 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
839 if (strchr(i->second->modes,'o'))
841 if (strchr(i->second->modes,'s'))
843 // send server notices to all with +s
844 // (TODO: needs SNOMASKs)
845 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
852 // returns TRUE of any users on channel C occupy server 'servername'.
854 bool ChanAnyOnThisServer(chanrec *c,char* servername)
856 log(DEBUG,"ChanAnyOnThisServer");
857 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
859 if (has_channel(i->second,c))
861 if (!strcasecmp(i->second->server,servername))
870 // returns true if user 'u' shares any common channels with any users on server 'servername'
872 bool CommonOnThisServer(userrec* u,char* servername)
874 log(DEBUG,"ChanAnyOnThisServer");
875 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
877 if ((common_channels(u,i->second)) && (u != i->second))
879 if (!strcasecmp(i->second->server,servername))
890 bool hasumode(userrec* user, char mode)
894 return (strchr(user->modes,mode)>0);
899 void WriteMode(const char* modes, int flags, const char* text, ...)
901 if ((!text) || (!modes) || (!flags))
903 log(DEFAULT,"*** BUG *** WriteMode was given an invalid parameter");
907 char textbuffer[MAXBUF];
909 va_start (argsPtr, text);
910 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
913 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
917 bool send_to_user = false;
922 for (int n = 0; n < strlen(modes); n++)
924 if (!hasumode(i->second,modes[n]))
926 send_to_user = false;
931 else if (flags == WM_OR)
933 send_to_user = false;
934 for (int n = 0; n < strlen(modes); n++)
936 if (hasumode(i->second,modes[n]))
946 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
952 void WriteWallOps(userrec *source, char* text, ...)
954 if ((!text) || (!source))
956 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
961 char textbuffer[MAXBUF];
963 va_start (argsPtr, text);
964 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
967 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
971 if (strchr(i->second->modes,'w'))
973 WriteTo(source,i->second,"WALLOPS %s",textbuffer);
979 /* convert a string to lowercase. Note following special circumstances
980 * taken from RFC 1459. Many "official" server branches still hold to this
981 * rule so i will too;
983 * Because of IRC's scandanavian origin, the characters {}| are
984 * considered to be the lower case equivalents of the characters []\,
985 * respectively. This is a critical issue when determining the
986 * equivalence of two nicknames.
989 void strlower(char *n)
995 for (int i = 0; i != strlen(n); i++)
997 n[i] = tolower(n[i]);
1007 /* verify that a user's ident and nickname is valid */
1009 int isident(const char* n)
1021 for (int i = 0; i != strlen(n); i++)
1023 if ((n[i] < 33) || (n[i] > 125))
1027 /* can't occur ANYWHERE in an Ident! */
1028 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
1037 int isnick(const char* n)
1049 if (strlen(n) > NICKMAX-1)
1053 for (int i = 0; i != strlen(n); i++)
1055 if ((n[i] < 33) || (n[i] > 125))
1059 /* can't occur ANYWHERE in a nickname! */
1060 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
1064 /* can't occur as the first char of a nickname... */
1065 if ((strchr("0123456789",n[i])) && (!i))
1073 /* Find a user record by nickname and return a pointer to it */
1075 userrec* Find(string nick)
1077 user_hash::iterator iter = clientlist.find(nick);
1079 if (iter == clientlist.end())
1080 /* Couldn't find it */
1083 return iter->second;
1086 void update_stats_l(int fd,int data_out) /* add one line-out to stats L for this fd */
1088 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1092 if (i->second->fd == fd)
1094 i->second->bytes_out+=data_out;
1095 i->second->cmds_out++;
1102 /* find a channel record by channel name and return a pointer to it */
1104 chanrec* FindChan(const char* chan)
1108 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
1112 chan_hash::iterator iter = chanlist.find(chan);
1114 if (iter == chanlist.end())
1115 /* Couldn't find it */
1118 return iter->second;
1122 void purge_empty_chans(void)
1124 int go_again = 1, purge = 0;
1129 for (chan_hash::iterator i = chanlist.begin(); i != chanlist.end(); i++)
1132 if (!usercount(i->second))
1134 /* kill the record */
1135 if (i != chanlist.end())
1137 log(DEBUG,"del_channel: destroyed: %s",i->second->name);
1147 log(DEBUG,"skipped purge for %s",i->second->name);
1152 log(DEBUG,"completed channel purge, killed %d",purge);
1155 /* returns the status character for a given user on a channel, e.g. @ for op,
1156 * % for halfop etc. If the user has several modes set, the highest mode
1157 * the user has must be returned. */
1159 char* cmode(userrec *user, chanrec *chan)
1161 if ((!user) || (!chan))
1163 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
1168 for (int i = 0; i != MAXCHANS; i++)
1170 if ((user->chans[i].channel == chan) && (chan != NULL))
1172 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
1176 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
1180 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
1189 char scratch[MAXBUF];
1190 char sparam[MAXBUF];
1192 char* chanmodes(chanrec *chan)
1196 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1203 if (chan->noexternal)
1205 strncat(scratch,"n",MAXMODES);
1207 if (chan->topiclock)
1209 strncat(scratch,"t",MAXMODES);
1211 if (strcmp(chan->key,""))
1213 strncat(scratch,"k",MAXMODES);
1217 strncat(scratch,"l",MAXMODES);
1219 if (chan->inviteonly)
1221 strncat(scratch,"i",MAXMODES);
1223 if (chan->moderated)
1225 strncat(scratch,"m",MAXMODES);
1229 strncat(scratch,"s",MAXMODES);
1231 if (chan->c_private)
1233 strncat(scratch,"p",MAXMODES);
1235 if (strcmp(chan->key,""))
1237 strncat(sparam,chan->key,MAXBUF);
1242 sprintf(foo," %d",chan->limit);
1243 strncat(sparam,foo,MAXBUF);
1245 if (strlen(chan->custom_modes))
1247 strncat(scratch,chan->custom_modes,MAXMODES);
1248 for (int z = 0; z < strlen(chan->custom_modes); z++)
1250 std::string extparam = chan->GetModeParameter(chan->custom_modes[z]);
1253 strncat(sparam," ",MAXBUF);
1254 strncat(sparam,extparam.c_str(),MAXBUF);
1258 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
1259 strncat(scratch,sparam,MAXMODES);
1263 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
1264 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
1265 * highest mode the user has must be returned. */
1267 int cstatus(userrec *user, chanrec *chan)
1269 if ((!chan) || (!user))
1271 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
1275 for (int i = 0; i != MAXCHANS; i++)
1277 if ((user->chans[i].channel == chan) && (chan != NULL))
1279 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
1283 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
1287 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
1289 return STATUS_VOICE;
1291 return STATUS_NORMAL;
1297 /* compile a userlist of a channel into a string, each nick seperated by
1298 * spaces and op, voice etc status shown as @ and + */
1300 void userlist(userrec *user,chanrec *c)
1302 if ((!c) || (!user))
1304 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1308 sprintf(list,"353 %s = %s :", user->nick, c->name);
1309 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1311 if (has_channel(i->second,c))
1313 if (isnick(i->second->nick))
1315 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1317 /* user is +i, and source not on the channel, does not show
1318 * nick in NAMES list */
1321 strcat(list,cmode(i->second,c));
1322 strcat(list,i->second->nick);
1324 if (strlen(list)>(480-NICKMAX))
1326 /* list overflowed into
1327 * multiple numerics */
1328 WriteServ(user->fd,list);
1329 sprintf(list,"353 %s = %s :", user->nick, c->name);
1334 /* if whats left in the list isnt empty, send it */
1335 if (list[strlen(list)-1] != ':')
1337 WriteServ(user->fd,list);
1341 /* return a count of the users on a specific channel accounting for
1342 * invisible users who won't increase the count. e.g. for /LIST */
1344 int usercount_i(chanrec *c)
1351 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1356 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1360 if (has_channel(i->second,c))
1362 if (isnick(i->second->nick))
1364 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1366 /* user is +i, and source not on the channel, does not show
1367 * nick in NAMES list */
1375 log(DEBUG,"usercount_i: %s %d",c->name,count);
1380 int usercount(chanrec *c)
1387 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
1392 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1396 if (has_channel(i->second,c))
1398 if ((isnick(i->second->nick)) && (i->second->registered == 7))
1405 log(DEBUG,"usercount: %s %d",c->name,count);
1410 /* add a channel to a user, creating the record for it if needed and linking
1411 * it to the user record */
1413 chanrec* add_channel(userrec *user, const char* cn, const char* key, bool override)
1415 if ((!user) || (!cn))
1417 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
1426 strncpy(cname,cn,MAXBUF);
1428 // we MUST declare this wherever we use FOREACH_RESULT
1431 if (strlen(cname) > CHANMAX-1)
1433 cname[CHANMAX-1] = '\0';
1436 log(DEBUG,"add_channel: %s %s",user->nick,cname);
1438 if ((FindChan(cname)) && (has_channel(user,FindChan(cname))))
1440 return NULL; // already on the channel!
1444 if (!FindChan(cname))
1446 FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
1451 /* create a new one */
1452 log(DEBUG,"add_channel: creating: %s",cname);
1454 chanlist[cname] = new chanrec();
1456 strcpy(chanlist[cname]->name, cname);
1457 chanlist[cname]->topiclock = 1;
1458 chanlist[cname]->noexternal = 1;
1459 chanlist[cname]->created = time(NULL);
1460 strcpy(chanlist[cname]->topic, "");
1461 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
1462 chanlist[cname]->topicset = 0;
1463 Ptr = chanlist[cname];
1464 log(DEBUG,"add_channel: created: %s",cname);
1465 /* set created to 2 to indicate user
1466 * is the first in the channel
1467 * and should be given ops */
1473 /* channel exists, just fish out a pointer to its struct */
1474 Ptr = FindChan(cname);
1477 log(DEBUG,"add_channel: joining to: %s",Ptr->name);
1479 // the override flag allows us to bypass channel modes
1480 // and bans (used by servers)
1483 FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
1488 if (strcmp(Ptr->key,""))
1490 log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
1493 log(DEBUG,"add_channel: no key given in JOIN");
1494 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
1499 log(DEBUG,"key at %p is %s",key,key);
1500 if (strcasecmp(key,Ptr->key))
1502 log(DEBUG,"add_channel: bad key given in JOIN");
1503 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
1508 log(DEBUG,"add_channel: no key");
1510 if (Ptr->inviteonly)
1512 log(DEBUG,"add_channel: channel is +i");
1513 if (user->IsInvited(Ptr->name))
1515 /* user was invited to channel */
1516 /* there may be an optional channel NOTICE here */
1520 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
1524 log(DEBUG,"add_channel: channel is not +i");
1528 if (usercount(Ptr) == Ptr->limit)
1530 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
1535 log(DEBUG,"add_channel: about to walk banlist");
1537 /* check user against the channel banlist */
1540 if (Ptr->bans.size())
1542 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1544 if (match(user->GetFullHost(),i->data))
1546 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
1553 log(DEBUG,"add_channel: bans checked");
1556 if ((Ptr) && (user))
1558 user->RemoveInvite(Ptr->name);
1561 log(DEBUG,"add_channel: invites removed");
1566 log(DEBUG,"Overridden checks");
1574 log(DEBUG,"Passed channel checks");
1576 for (int i =0; i != MAXCHANS; i++)
1578 log(DEBUG,"Check location %d",i);
1579 if (user->chans[i].channel == NULL)
1581 log(DEBUG,"Adding into their channel list at location %d",i);
1585 /* first user in is given ops */
1586 user->chans[i].uc_modes = UCMODE_OP;
1590 user->chans[i].uc_modes = 0;
1592 user->chans[i].channel = Ptr;
1593 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
1595 if (!override) // we're not overriding... so this isnt part of a netburst, broadcast it.
1597 // use the stamdard J token with no privilages.
1598 char buffer[MAXBUF];
1599 snprintf(buffer,MAXBUF,"J %s :%s",user->nick,Ptr->name);
1600 for (int j = 0; j < 255; j++)
1602 if (servers[j] != NULL)
1604 if (ChanAnyOnThisServer(Ptr,servers[j]->name))
1606 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
1607 log(DEBUG,"Sent J token");
1613 log(DEBUG,"Sent JOIN to client");
1617 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
1618 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
1621 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
1622 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name,chanmodes(Ptr));
1623 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1624 FOREACH_MOD OnUserJoin(user,Ptr);
1628 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
1629 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
1633 /* remove a channel from a users record, and remove the record from memory
1634 * if the channel has become empty */
1636 chanrec* del_channel(userrec *user, const char* cname, const char* reason, bool local)
1638 if ((!user) || (!cname))
1640 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
1647 if ((!cname) || (!user))
1652 Ptr = FindChan(cname);
1659 FOREACH_MOD OnUserPart(user,Ptr);
1660 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
1662 for (int i =0; i != MAXCHANS; i++)
1664 /* zap it from the channel list of the user */
1665 if (user->chans[i].channel == Ptr)
1669 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
1673 char buffer[MAXBUF];
1674 snprintf(buffer,MAXBUF,"L %s %s :%s",user->nick,Ptr->name,reason);
1675 for (int j = 0; j < 255; j++)
1677 if (servers[j] != NULL)
1679 if (ChanAnyOnThisServer(Ptr,servers[j]->name))
1681 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
1682 log(DEBUG,"Sent L token (with reason)");
1694 char buffer[MAXBUF];
1695 snprintf(buffer,MAXBUF,"L %s %s :",user->nick,Ptr->name);
1696 for (int j = 0; j < 255; j++)
1698 if (servers[j] != NULL)
1700 if (ChanAnyOnThisServer(Ptr,servers[j]->name))
1702 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
1703 log(DEBUG,"Sent L token (no reason)");
1709 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
1711 user->chans[i].uc_modes = 0;
1712 user->chans[i].channel = NULL;
1713 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1718 /* if there are no users left on the channel */
1719 if (!usercount(Ptr))
1721 chan_hash::iterator iter = chanlist.find(Ptr->name);
1723 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1725 /* kill the record */
1726 if (iter != chanlist.end())
1728 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1729 delete iter->second;
1730 chanlist.erase(iter);
1736 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
1738 if ((!src) || (!user) || (!Ptr) || (!reason))
1740 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
1747 if ((!Ptr) || (!user) || (!src))
1752 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
1754 if (!has_channel(user,Ptr))
1756 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
1759 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
1761 if (cstatus(src,Ptr) == STATUS_HOP)
1763 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
1767 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
1773 for (int i =0; i != MAXCHANS; i++)
1775 /* zap it from the channel list of the user */
1776 if (user->chans[i].channel == Ptr)
1778 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
1779 user->chans[i].uc_modes = 0;
1780 user->chans[i].channel = NULL;
1781 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1786 /* if there are no users left on the channel */
1787 if (!usercount(Ptr))
1789 chan_hash::iterator iter = chanlist.find(Ptr->name);
1791 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1793 /* kill the record */
1794 if (iter != chanlist.end())
1796 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1797 delete iter->second;
1798 chanlist.erase(iter);
1804 /* returns 1 if user u has channel c in their record, 0 if not */
1806 int has_channel(userrec *u, chanrec *c)
1810 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
1813 for (int i =0; i != MAXCHANS; i++)
1815 if (u->chans[i].channel == c)
1823 int give_ops(userrec *user,char *dest,chanrec *chan,int status)
1828 if ((!user) || (!dest) || (!chan))
1830 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
1833 if (status != STATUS_OP)
1835 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1842 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1848 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1853 for (int i = 0; i != MAXCHANS; i++)
1855 if ((d->chans[i].channel == chan) && (chan != NULL))
1857 if (d->chans[i].uc_modes & UCMODE_OP)
1859 /* mode already set on user, dont allow multiple */
1862 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
1863 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
1871 int give_hops(userrec *user,char *dest,chanrec *chan,int status)
1876 if ((!user) || (!dest) || (!chan))
1878 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
1881 if (status != STATUS_OP)
1883 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1891 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1896 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1901 for (int i = 0; i != MAXCHANS; i++)
1903 if ((d->chans[i].channel == chan) && (chan != NULL))
1905 if (d->chans[i].uc_modes & UCMODE_HOP)
1907 /* mode already set on user, dont allow multiple */
1910 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
1911 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
1919 int give_voice(userrec *user,char *dest,chanrec *chan,int status)
1924 if ((!user) || (!dest) || (!chan))
1926 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
1929 if (status < STATUS_HOP)
1931 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
1939 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1944 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1949 for (int i = 0; i != MAXCHANS; i++)
1951 if ((d->chans[i].channel == chan) && (chan != NULL))
1953 if (d->chans[i].uc_modes & UCMODE_VOICE)
1955 /* mode already set on user, dont allow multiple */
1958 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
1959 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
1967 int take_ops(userrec *user,char *dest,chanrec *chan,int status)
1972 if ((!user) || (!dest) || (!chan))
1974 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
1977 if (status != STATUS_OP)
1979 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1987 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1992 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1997 for (int i = 0; i != MAXCHANS; i++)
1999 if ((d->chans[i].channel == chan) && (chan != NULL))
2001 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
2003 /* mode already set on user, dont allow multiple */
2006 d->chans[i].uc_modes ^= UCMODE_OP;
2007 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
2015 int take_hops(userrec *user,char *dest,chanrec *chan,int status)
2020 if ((!user) || (!dest) || (!chan))
2022 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
2025 if (status != STATUS_OP)
2027 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
2035 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
2040 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
2045 for (int i = 0; i != MAXCHANS; i++)
2047 if ((d->chans[i].channel == chan) && (chan != NULL))
2049 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
2051 /* mode already set on user, dont allow multiple */
2054 d->chans[i].uc_modes ^= UCMODE_HOP;
2055 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
2063 int take_voice(userrec *user,char *dest,chanrec *chan,int status)
2068 if ((!user) || (!dest) || (!chan))
2070 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
2073 if (status < STATUS_HOP)
2075 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, chan->name);
2083 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
2088 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
2093 for (int i = 0; i != MAXCHANS; i++)
2095 if ((d->chans[i].channel == chan) && (chan != NULL))
2097 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
2099 /* mode already set on user, dont allow multiple */
2102 d->chans[i].uc_modes ^= UCMODE_VOICE;
2103 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
2111 void TidyBan(char *ban)
2114 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
2118 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
2122 char* pos_of_pling = strchr(temp,'!');
2123 char* pos_of_at = strchr(temp,'@');
2125 pos_of_pling[0] = '\0';
2126 pos_of_at[0] = '\0';
2130 strncpy(NICK,temp,NICKMAX);
2131 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
2132 strncpy(HOST,pos_of_at,160);
2134 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
2137 int add_ban(userrec *user,char *dest,chanrec *chan,int status)
2139 if ((!user) || (!dest) || (!chan)) {
2140 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
2145 if ((!user) || (!dest) || (!chan))
2147 if (strchr(dest,'!')==0)
2149 if (strchr(dest,'@')==0)
2151 for (int i = 0; i < strlen(dest); i++)
2154 for (int i = 0; i < strlen(dest); i++)
2158 for (int i = 0; i < strlen(dest); i++)
2164 for (int i = 0; i < strlen(dest); i++)
2169 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
2172 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
2174 if (!strcasecmp(i->data,dest))
2176 // dont allow a user to set the same ban twice
2181 b.set_time = time(NULL);
2182 strncpy(b.data,dest,MAXBUF);
2183 strncpy(b.set_by,user->nick,NICKMAX);
2184 chan->bans.push_back(b);
2188 int take_ban(userrec *user,char *dest,chanrec *chan,int status)
2190 if ((!user) || (!dest) || (!chan)) {
2191 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
2195 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
2196 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
2198 if (!strcasecmp(i->data,dest))
2200 chan->bans.erase(i);
2207 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode, bool silent, bool local)
2210 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
2214 char modelist[MAXBUF];
2215 char outlist[MAXBUF];
2216 char outstr[MAXBUF];
2217 char outpars[32][MAXBUF];
2223 bool k_set = false, l_set = false;
2230 log(DEBUG,"process_modes: start");
2232 strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
2233 /* parameters[2] onwards are parameters for
2234 * modes that require them :) */
2235 strcpy(outlist,"+");
2238 log(DEBUG,"process_modes: modelist: %s",modelist);
2240 for (ptr = 0; ptr < strlen(modelist); ptr++)
2245 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
2246 char modechar = modelist[ptr];
2247 switch (modelist[ptr])
2252 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
2254 outlist[strlen(outlist)-1] = '-';
2258 strcat(outlist,"-");
2268 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
2270 outlist[strlen(outlist)-1] = '+';
2274 strcat(outlist,"+");
2281 if ((param >= pcnt)) break;
2284 r = give_ops(user,parameters[param++],chan,status);
2288 r = take_ops(user,parameters[param++],chan,status);
2292 strcat(outlist,"o");
2293 strcpy(outpars[pc++],parameters[param-1]);
2298 if ((param >= pcnt)) break;
2301 r = give_hops(user,parameters[param++],chan,status);
2305 r = take_hops(user,parameters[param++],chan,status);
2309 strcat(outlist,"h");
2310 strcpy(outpars[pc++],parameters[param-1]);
2316 if ((param >= pcnt)) break;
2319 r = give_voice(user,parameters[param++],chan,status);
2323 r = take_voice(user,parameters[param++],chan,status);
2327 strcat(outlist,"v");
2328 strcpy(outpars[pc++],parameters[param-1]);
2333 if ((param >= pcnt)) break;
2336 r = add_ban(user,parameters[param++],chan,status);
2340 r = take_ban(user,parameters[param++],chan,status);
2344 strcat(outlist,"b");
2345 strcpy(outpars[pc++],parameters[param-1]);
2351 if ((param >= pcnt))
2359 if (!strcmp(chan->key,""))
2361 strcat(outlist,"k");
2363 strcpy(key,parameters[param++]);
2364 if (strlen(key)>32) {
2367 strcpy(outpars[pc++],key);
2368 strcpy(chan->key,key);
2374 /* checks on -k are case sensitive and only accurate to the
2375 first 32 characters */
2377 strcpy(key,parameters[param++]);
2378 if (strlen(key)>32) {
2381 /* only allow -k if correct key given */
2382 if (!strcmp(chan->key,key))
2384 strcat(outlist,"k");
2385 strcpy(chan->key,"");
2386 strcpy(outpars[pc++],key);
2396 strcat(outlist,"l");
2401 if ((param >= pcnt)) break;
2407 bool invalid = false;
2408 for (int i = 0; i < strlen(parameters[param]); i++)
2410 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
2415 if (atoi(parameters[param]) < 1)
2423 chan->limit = atoi(parameters[param]);
2426 strcat(outlist,"l");
2427 strcpy(outpars[pc++],parameters[param++]);
2434 if (chan->inviteonly != mdir)
2436 strcat(outlist,"i");
2438 chan->inviteonly = mdir;
2442 if (chan->topiclock != mdir)
2444 strcat(outlist,"t");
2446 chan->topiclock = mdir;
2450 if (chan->noexternal != mdir)
2452 strcat(outlist,"n");
2454 chan->noexternal = mdir;
2458 if (chan->moderated != mdir)
2460 strcat(outlist,"m");
2462 chan->moderated = mdir;
2466 if (chan->secret != mdir)
2468 strcat(outlist,"s");
2469 if (chan->c_private)
2471 chan->c_private = 0;
2474 strcat(outlist,"-p+");
2478 strcat(outlist,"+p-");
2482 chan->secret = mdir;
2486 if (chan->c_private != mdir)
2488 strcat(outlist,"p");
2494 strcat(outlist,"-s+");
2498 strcat(outlist,"+s-");
2502 chan->c_private = mdir;
2506 log(DEBUG,"Preprocessing custom mode %c",modechar);
2509 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
2511 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
2514 if (ModeDefined(modechar,MT_CHANNEL))
2516 log(DEBUG,"A module has claimed this mode");
2519 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
2521 p.push_back(parameters[param]);
2523 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
2525 p.push_back(parameters[param]);
2528 bool handled = false;
2531 log(DEBUG,"Not enough parameters for module-mode %c",modechar);
2532 // we're supposed to have a parameter, but none was given... so dont handle the mode.
2533 if (((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir)) || ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir)))
2539 for (int i = 0; i <= MODCOUNT; i++)
2543 if (modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p))
2545 log(DEBUG,"OnExtendedMode returned nonzero for a module");
2546 char app[] = {modechar, 0};
2549 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
2551 strcat(outlist, app);
2553 else if (!strchr(outlist,modechar))
2555 strcat(outlist, app);
2558 chan->SetCustomMode(modechar,mdir);
2559 // include parameters in output if mode has them
2560 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
2562 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
2563 strcpy(outpars[pc++],parameters[param++]);
2565 // break, because only one module can handle the mode.
2577 /* this ensures only the *valid* modes are sent out onto the network */
2578 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
2580 outlist[strlen(outlist)-1] = '\0';
2582 if (strcmp(outlist,""))
2584 strcpy(outstr,outlist);
2585 for (ptr = 0; ptr < pc; ptr++)
2588 strcat(outstr,outpars[ptr]);
2592 log(DEBUG,"Local mode change");
2593 WriteChannelLocal(chan, user, "MODE %s",outstr);
2601 WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
2602 // M token for a usermode must go to all servers
2603 char buffer[MAXBUF];
2604 snprintf(buffer,MAXBUF,"M %s %s",ServerName,chan->name, outstr);
2605 for (int j = 0; j < 255; j++)
2607 if (servers[j] != NULL)
2609 if (strcmp(servers[j]->name,ServerName))
2611 if (ChanAnyOnThisServer(chan,servers[j]->name))
2613 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
2614 log(DEBUG,"Sent M token");
2626 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
2627 // M token for a usermode must go to all servers
2628 char buffer[MAXBUF];
2629 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick,chan->name, outstr);
2630 for (int j = 0; j < 255; j++)
2632 if (servers[j] != NULL)
2634 if (strcmp(servers[j]->name,ServerName))
2636 if (ChanAnyOnThisServer(chan,servers[j]->name))
2638 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
2639 log(DEBUG,"Sent m token");
2650 // based on sourcemodes, return true or false to determine if umode is a valid mode a user may set on themselves or others.
2652 bool allowed_umode(char umode, char* sourcemodes,bool adding)
2654 log(DEBUG,"Allowed_umode: %c %s",umode,sourcemodes);
2655 // RFC1459 specified modes
2656 if ((umode == 'w') || (umode == 's') || (umode == 'i'))
2658 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
2662 // user may not +o themselves or others, but an oper may de-oper other opers or themselves
2663 if ((strchr(sourcemodes,'o')) && (!adding))
2665 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
2668 else if (umode == 'o')
2670 log(DEBUG,"umode %c allowed by RFC1459 scemantics",umode);
2674 // process any module-defined modes that need oper
2675 if ((ModeDefinedOper(umode,MT_CLIENT)) && (strchr(sourcemodes,'o')))
2677 log(DEBUG,"umode %c allowed by module handler (oper only mode)",umode);
2681 if (ModeDefined(umode,MT_CLIENT))
2683 // process any module-defined modes that don't need oper
2684 log(DEBUG,"umode %c allowed by module handler (non-oper mode)",umode);
2685 if ((ModeDefinedOper(umode,MT_CLIENT)) && (!strchr(sourcemodes,'o')))
2687 // no, this mode needs oper, and this user 'aint got what it takes!
2693 // anything else - return false.
2694 log(DEBUG,"umode %c not known by any ruleset",umode);
2698 bool process_module_umode(char umode, userrec* source, void* dest, bool adding)
2705 strncpy(s2->nick,ServerName,NICKMAX);
2706 strcpy(s2->modes,"o");
2713 if (ModeDefined(umode,MT_CLIENT))
2715 for (int i = 0; i <= MODCOUNT; i++)
2717 if (modules[i]->OnExtendedMode(source,(void*)dest,umode,MT_CLIENT,adding,p))
2719 log(DEBUG,"Module claims umode %c",umode);
2723 log(DEBUG,"No module claims umode %c",umode);
2733 log(DEBUG,"*** BUG *** Non-module umode passed to process_module_umode!");
2743 void handle_mode(char **parameters, int pcnt, userrec *user)
2749 char outpars[MAXBUF];
2751 dest = Find(parameters[0]);
2758 if ((dest) && (pcnt == 1))
2760 WriteServ(user->fd,"221 %s :+%s",user->nick,user->modes);
2764 if ((dest) && (pcnt > 1))
2766 char dmodes[MAXBUF];
2767 strncpy(dmodes,dest->modes,MAXBUF);
2768 log(DEBUG,"pulled up dest user modes: %s",dmodes);
2773 if (strchr(user->modes,'o'))
2784 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
2788 strcpy(outpars,"+");
2791 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
2794 for (int i = 0; i < strlen(parameters[1]); i++)
2796 if (parameters[1][i] == '+')
2800 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2802 outpars[strlen(outpars)-1] = '+';
2806 strcat(outpars,"+");
2812 if (parameters[1][i] == '-')
2816 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2818 outpars[strlen(outpars)-1] = '-';
2822 strcat(outpars,"-");
2830 if (strchr(user->modes,'o'))
2836 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's') || (allowed_umode(parameters[1][i],user->modes,direction)))
2845 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
2847 char umode = parameters[1][i];
2848 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2850 dmodes[strlen(dmodes)+1]='\0';
2851 dmodes[strlen(dmodes)] = parameters[1][i];
2852 outpars[strlen(outpars)+1]='\0';
2853 outpars[strlen(outpars)] = parameters[1][i];
2859 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
2861 char umode = parameters[1][i];
2862 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
2868 outpars[strlen(outpars)+1]='\0';
2869 outpars[strlen(outpars)] = parameters[1][i];
2872 for (q = 0; q < strlen(dmodes); q++)
2874 if (dmodes[q] != parameters[1][i])
2881 strcpy(dmodes,temp);
2888 if (strlen(outpars))
2894 while (i < strlen (outpars))
2896 b[z++] = outpars[i++];
2898 if (i<strlen(outpars)-1)
2900 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2902 // someones playing silly buggers and trying
2903 // to put a +- or -+ into the line...
2907 if (i == strlen(outpars)-1)
2909 if ((outpars[i] == '-') || (outpars[i] == '+'))
2917 if ((b[z] == '-') || (b[z] == '+'))
2920 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2923 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2925 // M token for a usermode must go to all servers
2926 char buffer[MAXBUF];
2927 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
2928 for (int j = 0; j < 255; j++)
2930 if (servers[j] != NULL)
2932 if (strcmp(servers[j]->name,ServerName))
2934 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
2935 log(DEBUG,"Sent m token");
2940 if (strlen(dmodes)>MAXMODES)
2942 dmodes[MAXMODES-1] = '\0';
2944 log(DEBUG,"Stripped mode line");
2945 log(DEBUG,"Line dest is now %s",dmodes);
2946 strncpy(dest->modes,dmodes,MAXMODES);
2953 Ptr = FindChan(parameters[0]);
2958 /* just /modes #channel */
2959 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
2960 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
2966 if ((!strcmp(parameters[1],"+b")) || (!strcmp(parameters[1],"b")))
2969 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
2971 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
2973 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
2977 if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
2979 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, Ptr->name);
2983 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,false,false);
2987 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2994 void server_mode(char **parameters, int pcnt, userrec *user)
3000 char outpars[MAXBUF];
3002 dest = Find(parameters[0]);
3004 // fix: ChroNiCk found this - we cant use this as debug if its null!
3007 log(DEBUG,"server_mode on %s",dest->nick);
3010 if ((dest) && (pcnt > 1))
3012 log(DEBUG,"params > 1");
3014 char dmodes[MAXBUF];
3015 strncpy(dmodes,dest->modes,MAXBUF);
3017 strcpy(outpars,"+");
3020 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
3023 for (int i = 0; i < strlen(parameters[1]); i++)
3025 if (parameters[1][i] == '+')
3029 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
3031 outpars[strlen(outpars)-1] = '+';
3035 strcat(outpars,"+");
3041 if (parameters[1][i] == '-')
3045 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
3047 outpars[strlen(outpars)-1] = '-';
3051 strcat(outpars,"-");
3058 log(DEBUG,"begin mode processing entry");
3064 log(DEBUG,"umode %c being added",parameters[1][i]);
3065 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
3067 char umode = parameters[1][i];
3068 log(DEBUG,"umode %c is an allowed umode",umode);
3069 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
3071 dmodes[strlen(dmodes)+1]='\0';
3072 dmodes[strlen(dmodes)] = parameters[1][i];
3073 outpars[strlen(outpars)+1]='\0';
3074 outpars[strlen(outpars)] = parameters[1][i];
3080 // can only remove a mode they already have
3081 log(DEBUG,"umode %c being removed",parameters[1][i]);
3082 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
3084 char umode = parameters[1][i];
3085 log(DEBUG,"umode %c is an allowed umode",umode);
3086 if ((process_module_umode(umode, user, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
3092 outpars[strlen(outpars)+1]='\0';
3093 outpars[strlen(outpars)] = parameters[1][i];
3096 for (q = 0; q < strlen(dmodes); q++)
3098 if (dmodes[q] != parameters[1][i])
3105 strcpy(dmodes,temp);
3112 if (strlen(outpars))
3118 while (i < strlen (outpars))
3120 b[z++] = outpars[i++];
3122 if (i<strlen(outpars)-1)
3124 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
3126 // someones playing silly buggers and trying
3127 // to put a +- or -+ into the line...
3131 if (i == strlen(outpars)-1)
3133 if ((outpars[i] == '-') || (outpars[i] == '+'))
3141 if ((b[z] == '-') || (b[z] == '+'))
3144 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
3147 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
3149 // M token for a usermode must go to all servers
3150 char buffer[MAXBUF];
3151 snprintf(buffer,MAXBUF,"m %s %s %s",user->nick, dest->nick, b);
3152 for (int j = 0; j < 255; j++)
3154 if (servers[j] != NULL)
3156 if (strcmp(servers[j]->name,ServerName))
3158 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
3159 log(DEBUG,"Sent m token");
3164 if (strlen(dmodes)>MAXMODES)
3166 dmodes[MAXMODES-1] = '\0';
3168 log(DEBUG,"Stripped mode line");
3169 log(DEBUG,"Line dest is now %s",dmodes);
3170 strncpy(dest->modes,dmodes,MAXMODES);
3177 Ptr = FindChan(parameters[0]);
3180 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true,false,false);
3184 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3190 void merge_mode(char **parameters, int pcnt)
3196 char outpars[MAXBUF];
3198 dest = Find(parameters[0]);
3200 // fix: ChroNiCk found this - we cant use this as debug if its null!
3203 log(DEBUG,"merge_mode on %s",dest->nick);
3206 if ((dest) && (pcnt > 1))
3208 log(DEBUG,"params > 1");
3210 char dmodes[MAXBUF];
3211 strncpy(dmodes,dest->modes,MAXBUF);
3213 strcpy(outpars,"+");
3216 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
3219 for (int i = 0; i < strlen(parameters[1]); i++)
3221 if (parameters[1][i] == '+')
3225 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
3227 outpars[strlen(outpars)-1] = '+';
3231 strcat(outpars,"+");
3237 if (parameters[1][i] == '-')
3241 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
3243 outpars[strlen(outpars)-1] = '-';
3247 strcat(outpars,"-");
3254 log(DEBUG,"begin mode processing entry");
3260 log(DEBUG,"umode %c being added",parameters[1][i]);
3261 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],"o",true)))
3263 char umode = parameters[1][i];
3264 log(DEBUG,"umode %c is an allowed umode",umode);
3265 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
3267 dmodes[strlen(dmodes)+1]='\0';
3268 dmodes[strlen(dmodes)] = parameters[1][i];
3269 outpars[strlen(outpars)+1]='\0';
3270 outpars[strlen(outpars)] = parameters[1][i];
3276 // can only remove a mode they already have
3277 log(DEBUG,"umode %c being removed",parameters[1][i]);
3278 if ((allowed_umode(parameters[1][i],"o",false)) && (strchr(dmodes,parameters[1][i])))
3280 char umode = parameters[1][i];
3281 log(DEBUG,"umode %c is an allowed umode",umode);
3282 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
3288 outpars[strlen(outpars)+1]='\0';
3289 outpars[strlen(outpars)] = parameters[1][i];
3292 for (q = 0; q < strlen(dmodes); q++)
3294 if (dmodes[q] != parameters[1][i])
3301 strcpy(dmodes,temp);
3308 if (strlen(outpars))
3314 while (i < strlen (outpars))
3316 b[z++] = outpars[i++];
3318 if (i<strlen(outpars)-1)
3320 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
3322 // someones playing silly buggers and trying
3323 // to put a +- or -+ into the line...
3327 if (i == strlen(outpars)-1)
3329 if ((outpars[i] == '-') || (outpars[i] == '+'))
3337 if ((b[z] == '-') || (b[z] == '+'))
3340 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
3343 if (strlen(dmodes)>MAXMODES)
3345 dmodes[MAXMODES-1] = '\0';
3347 log(DEBUG,"Stripped mode line");
3348 log(DEBUG,"Line dest is now %s",dmodes);
3349 strncpy(dest->modes,dmodes,MAXMODES);
3356 Ptr = FindChan(parameters[0]);
3360 strncpy(s2.nick,ServerName,NICKMAX);
3361 strcpy(s2.modes,"o");
3363 process_modes(parameters,&s2,Ptr,STATUS_OP,pcnt,true,true,false);
3368 void merge_mode2(char **parameters, int pcnt, userrec* user)
3374 char outpars[MAXBUF];
3376 dest = Find(parameters[0]);
3378 // fix: ChroNiCk found this - we cant use this as debug if its null!
3381 log(DEBUG,"merge_mode on %s",dest->nick);
3384 if ((dest) && (pcnt > 1))
3386 log(DEBUG,"params > 1");
3388 char dmodes[MAXBUF];
3389 strncpy(dmodes,dest->modes,MAXBUF);
3391 strcpy(outpars,"+");
3394 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
3397 for (int i = 0; i < strlen(parameters[1]); i++)
3399 if (parameters[1][i] == '+')
3403 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
3405 outpars[strlen(outpars)-1] = '+';
3409 strcat(outpars,"+");
3415 if (parameters[1][i] == '-')
3419 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
3421 outpars[strlen(outpars)-1] = '-';
3425 strcat(outpars,"-");
3432 log(DEBUG,"begin mode processing entry");
3438 log(DEBUG,"umode %c being added",parameters[1][i]);
3439 if ((!strchr(dmodes,parameters[1][i])) && (allowed_umode(parameters[1][i],user->modes,true)))
3441 char umode = parameters[1][i];
3442 log(DEBUG,"umode %c is an allowed umode",umode);
3443 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
3445 dmodes[strlen(dmodes)+1]='\0';
3446 dmodes[strlen(dmodes)] = parameters[1][i];
3447 outpars[strlen(outpars)+1]='\0';
3448 outpars[strlen(outpars)] = parameters[1][i];
3454 // can only remove a mode they already have
3455 log(DEBUG,"umode %c being removed",parameters[1][i]);
3456 if ((allowed_umode(parameters[1][i],user->modes,false)) && (strchr(dmodes,parameters[1][i])))
3458 char umode = parameters[1][i];
3459 log(DEBUG,"umode %c is an allowed umode",umode);
3460 if ((process_module_umode(umode, NULL, dest, direction)) || (umode == 'i') || (umode == 's') || (umode == 'w') || (umode == 'o'))
3466 outpars[strlen(outpars)+1]='\0';
3467 outpars[strlen(outpars)] = parameters[1][i];
3470 for (q = 0; q < strlen(dmodes); q++)
3472 if (dmodes[q] != parameters[1][i])
3479 strcpy(dmodes,temp);
3486 if (strlen(outpars))
3492 while (i < strlen (outpars))
3494 b[z++] = outpars[i++];
3496 if (i<strlen(outpars)-1)
3498 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
3500 // someones playing silly buggers and trying
3501 // to put a +- or -+ into the line...
3505 if (i == strlen(outpars)-1)
3507 if ((outpars[i] == '-') || (outpars[i] == '+'))
3515 if ((b[z] == '-') || (b[z] == '+'))
3518 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
3521 WriteTo(user,dest,"MODE :%s",b);
3523 if (strlen(dmodes)>MAXMODES)
3525 dmodes[MAXMODES-1] = '\0';
3527 log(DEBUG,"Stripped mode line");
3528 log(DEBUG,"Line dest is now %s",dmodes);
3529 strncpy(dest->modes,dmodes,MAXMODES);
3536 Ptr = FindChan(parameters[0]);
3539 if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
3544 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false,true,true);
3550 /* This function pokes and hacks at a parameter list like the following:
3552 * PART #winbot, #darkgalaxy :m00!
3554 * to turn it into a series of individual calls like this:
3556 * PART #winbot :m00!
3557 * PART #darkgalaxy :m00!
3559 * The seperate calls are sent to a callback function provided by the caller
3560 * (the caller will usually call itself recursively). The callback function
3561 * must be a command handler. Calling this function on a line with no list causes
3562 * no action to be taken. You must provide a starting and ending parameter number
3563 * where the range of the list can be found, useful if you have a terminating
3564 * parameter as above which is actually not part of the list, or parameters
3565 * before the actual list as well. This code is used by many functions which
3566 * can function as "one to list" (see the RFC) */
3568 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
3573 char blog[32][MAXBUF];
3574 char blog2[32][MAXBUF];
3575 int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
3576 char keystr[MAXBUF];
3579 for (int i = 0; i <32; i++)
3582 for (int i = 0; i <32; i++)
3583 strcpy(blog2[i],"");
3586 for (int i = 0; i <10; i++)
3590 parameters[i] = moo;
3595 if (pcnt > 1) /* we have a key to copy */
3597 strcpy(keystr,parameters[1]);
3601 if (!parameters[start])
3605 if (!strchr(parameters[start],','))
3610 for (int i = start; i <= end; i++)
3614 strcat(plist,parameters[i]);
3622 for (int i = 0; i < t; i++)
3624 if (plist[i] == ',')
3627 strcpy(blog[j++],param);
3631 WriteServ(u->fd,"407 %s %s :Too many targets in list, message not delivered.",u->nick,blog[j-1]);
3636 strcpy(blog[j++],param);
3639 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
3644 if ((joins) && (keystr))
3646 if (strchr(keystr,','))
3650 t2 = strlen(keystr);
3651 for (int i = 0; i < t2; i++)
3653 if (keystr[i] == ',')
3656 strcpy(blog2[j++],param);
3660 strcpy(blog2[j++],param);
3665 for (j = 0; j < total; j++)
3671 for (q = end; q < pcnt-1; q++)
3673 if (parameters[q+1])
3675 pars[q-end+1] = parameters[q+1];
3678 if ((joins) && (parameters[1]))
3689 /* repeatedly call the function with the hacked parameter list */
3690 if ((joins) && (pcnt > 1))
3694 // pars[1] already set up and containing key from blog2[j]
3699 pars[1] = parameters[1];
3705 fn(pars,pcnt-(end-start),u);
3713 void handle_join(char **parameters, int pcnt, userrec *user)
3718 if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
3720 if (parameters[0][0] == '#')
3722 Ptr = add_channel(user,parameters[0],parameters[1],false);
3727 void handle_part(char **parameters, int pcnt, userrec *user)
3733 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
3735 del_channel(user,parameters[0],parameters[1],false);
3739 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
3741 del_channel(user,parameters[0],NULL,false);
3745 void handle_kick(char **parameters, int pcnt, userrec *user)
3747 chanrec* Ptr = FindChan(parameters[0]);
3748 userrec* u = Find(parameters[1]);
3752 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3756 if (!has_channel(u,Ptr))
3758 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
3764 char reason[MAXBUF];
3765 strncpy(reason,parameters[2],MAXBUF);
3766 if (strlen(reason)>MAXKICK)
3768 reason[MAXKICK-1] = '\0';
3771 kick_channel(user,u,Ptr,reason);
3775 kick_channel(user,u,Ptr,user->nick);
3780 void handle_die(char **parameters, int pcnt, userrec *user)
3782 log(DEBUG,"die: %s",user->nick);
3783 if (!strcmp(parameters[0],diepass))
3785 WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
3791 WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
3795 void handle_restart(char **parameters, int pcnt, userrec *user)
3797 log(DEBUG,"restart: %s",user->nick);
3798 if (!strcmp(parameters[0],restartpass))
3800 WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
3803 /* Will finish this later when i can be arsed :) */
3807 WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
3812 void kill_link(userrec *user,const char* r)
3814 user_hash::iterator iter = clientlist.find(user->nick);
3816 char reason[MAXBUF];
3818 strncpy(reason,r,MAXBUF);
3820 if (strlen(reason)>MAXQUIT)
3822 reason[MAXQUIT-1] = '\0';
3825 log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
3826 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
3827 log(DEBUG,"closing fd %d",user->fd);
3829 /* bugfix, cant close() a nonblocking socket (sux!) */
3830 if (user->registered == 7) {
3831 FOREACH_MOD OnUserQuit(user);
3832 WriteCommonExcept(user,"QUIT :%s",reason);
3834 // Q token must go to ALL servers!!!
3835 char buffer[MAXBUF];
3836 snprintf(buffer,MAXBUF,"Q %s :%s",user->nick,reason);
3837 for (int j = 0; j < 255; j++)
3839 if (servers[j] != NULL)
3841 if (strcmp(servers[j]->name,ServerName))
3843 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
3844 log(DEBUG,"Sent Q token");
3850 /* push the socket on a stack of sockets due to be closed at the next opportunity
3851 * 'Client exited' is an exception to this as it means the client side has already
3852 * closed the socket, we don't need to do it.
3854 fd_reap.push_back(user->fd);
3856 bool do_purge = false;
3858 if (user->registered == 7) {
3859 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
3863 if (iter != clientlist.end())
3865 log(DEBUG,"deleting user hash value %d",iter->second);
3866 if ((iter->second) && (user->registered == 7)) {
3867 delete iter->second;
3869 clientlist.erase(iter);
3872 if (user->registered == 7) {
3873 purge_empty_chans();
3878 void handle_kill(char **parameters, int pcnt, userrec *user)
3880 userrec *u = Find(parameters[0]);
3881 char killreason[MAXBUF];
3883 log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
3886 WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
3887 // :Brain!brain@NetAdmin.chatspike.net KILL [Brain] :homer!NetAdmin.chatspike.net!Brain (test kill)
3888 WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
3889 sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
3890 kill_link(u,killreason);
3894 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3898 void handle_summon(char **parameters, int pcnt, userrec *user)
3900 WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
3903 void handle_users(char **parameters, int pcnt, userrec *user)
3905 WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
3909 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
3911 char* Passwd(userrec *user)
3913 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3915 if (match(user->host,i->host) && (i->type == CC_ALLOW))
3923 bool IsDenied(userrec *user)
3925 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3927 if (match(user->host,i->host) && (i->type == CC_DENY))
3937 void handle_pass(char **parameters, int pcnt, userrec *user)
3939 // Check to make sure they havnt registered -- Fix by FCS
3940 if (user->registered == 7)
3942 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
3945 if (!strcasecmp(parameters[0],Passwd(user)))
3947 user->haspassed = true;
3951 void handle_invite(char **parameters, int pcnt, userrec *user)
3953 userrec* u = Find(parameters[0]);
3954 chanrec* c = FindChan(parameters[1]);
3960 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[1]);
3966 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
3975 if (cstatus(user,c) < STATUS_HOP)
3977 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",user->nick, c->name);
3981 if (has_channel(u,c))
3983 WriteServ(user->fd,"443 %s %s %s :Is already on channel %s",user->nick,u->nick,c->name,c->name);
3986 if (!has_channel(user,c))
3988 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, c->name);
3991 u->InviteTo(c->name);
3992 WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
3993 WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
3996 void handle_topic(char **parameters, int pcnt, userrec *user)
4002 if (strlen(parameters[0]) <= CHANMAX)
4004 Ptr = FindChan(parameters[0]);
4009 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
4010 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
4014 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
4019 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4026 if (strlen(parameters[0]) <= CHANMAX)
4028 Ptr = FindChan(parameters[0]);
4031 if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
4033 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel", user->nick, Ptr->name);
4038 strncpy(topic,parameters[1],MAXBUF);
4039 if (strlen(topic)>MAXTOPIC)
4041 topic[MAXTOPIC-1] = '\0';
4044 strcpy(Ptr->topic,topic);
4045 strcpy(Ptr->setby,user->nick);
4046 Ptr->topicset = time(NULL);
4047 WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
4051 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4057 /* sends out an error notice to all connected clients (not to be used
4060 void send_error(char *s)
4062 log(DEBUG,"send_error: %s",s);
4063 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4065 if (isnick(i->second->nick))
4067 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
4071 // fix - unregistered connections receive ERROR, not NOTICE
4072 Write(i->second->fd,"ERROR :%s",s);
4077 void Error(int status)
4079 signal (SIGALRM, SIG_IGN);
4080 signal (SIGPIPE, SIG_IGN);
4081 signal (SIGTERM, SIG_IGN);
4082 signal (SIGABRT, SIG_IGN);
4083 signal (SIGSEGV, SIG_IGN);
4084 signal (SIGURG, SIG_IGN);
4085 signal (SIGKILL, SIG_IGN);
4086 log(DEBUG,"*** fell down a pothole in the road to perfection ***");
4087 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
4092 int main (int argc, char *argv[])
4095 log(DEBUG,"*** InspIRCd starting up!");
4096 if (!FileExists(CONFIG_FILE))
4098 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
4099 log(DEBUG,"main: no config");
4100 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
4104 if (!strcmp(argv[1],"-nofork")) {
4108 if (InspIRCd() == ERROR)
4110 log(DEBUG,"main: daemon function bailed");
4111 printf("ERROR: could not initialise. Shutting down.\n");
4118 template<typename T> inline string ConvToStr(const T &in)
4121 if (!(tmp << in)) return string();
4125 /* re-allocates a nick in the user_hash after they change nicknames,
4126 * returns a pointer to the new user as it may have moved */
4128 userrec* ReHashNick(char* Old, char* New)
4130 user_hash::iterator newnick;
4131 user_hash::iterator oldnick = clientlist.find(Old);
4133 log(DEBUG,"ReHashNick: %s %s",Old,New);
4135 if (!strcasecmp(Old,New))
4137 log(DEBUG,"old nick is new nick, skipping");
4138 return oldnick->second;
4141 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
4143 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
4145 clientlist[New] = new userrec();
4146 clientlist[New] = oldnick->second;
4147 /*delete oldnick->second; */
4148 clientlist.erase(oldnick);
4150 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
4152 return clientlist[New];
4155 /* adds or updates an entry in the whowas list */
4156 void AddWhoWas(userrec* u)
4158 user_hash::iterator iter = whowas.find(u->nick);
4159 userrec *a = new userrec();
4160 strcpy(a->nick,u->nick);
4161 strcpy(a->ident,u->ident);
4162 strcpy(a->dhost,u->dhost);
4163 strcpy(a->host,u->host);
4164 strcpy(a->fullname,u->fullname);
4165 strcpy(a->server,u->server);
4166 a->signon = u->signon;
4168 /* MAX_WHOWAS: max number of /WHOWAS items
4169 * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
4170 * can be replaced by a newer one
4173 if (iter == whowas.end())
4175 if (whowas.size() == WHOWAS_MAX)
4177 for (user_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
4179 // 3600 seconds in an hour ;)
4180 if ((i->second->signon)<(time(NULL)-(WHOWAS_STALE*3600)))
4184 log(DEBUG,"added WHOWAS entry, purged an old record");
4191 log(DEBUG,"added fresh WHOWAS entry");
4192 whowas[a->nick] = a;
4197 log(DEBUG,"updated WHOWAS entry");
4198 delete iter->second;
4204 /* add a client connection to the sockets list */
4205 void AddClient(int socket, char* host, int port, bool iscached)
4209 char resolved[MAXBUF];
4212 user_hash::iterator iter;
4214 tempnick = ConvToStr(socket) + "-unknown";
4215 sprintf(tn2,"%d-unknown",socket);
4217 iter = clientlist.find(tempnick);
4219 if (iter != clientlist.end()) return;
4222 * It is OK to access the value here this way since we know
4223 * it exists, we just created it above.
4225 * At NO other time should you access a value in a map or a
4226 * hash_map this way.
4228 clientlist[tempnick] = new userrec();
4230 NonBlocking(socket);
4231 log(DEBUG,"AddClient: %d %s %d",socket,host,port);
4233 clientlist[tempnick]->fd = socket;
4234 strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
4235 strncpy(clientlist[tempnick]->host, host,160);
4236 strncpy(clientlist[tempnick]->dhost, host,160);
4237 strncpy(clientlist[tempnick]->server, ServerName,256);
4238 strncpy(clientlist[tempnick]->ident, "unknown",9);
4239 clientlist[tempnick]->registered = 0;
4240 clientlist[tempnick]->signon = time(NULL);
4241 clientlist[tempnick]->nping = time(NULL)+240;
4242 clientlist[tempnick]->lastping = 1;
4243 clientlist[tempnick]->port = port;
4247 WriteServ(socket,"NOTICE Auth :Found your hostname (cached)...");
4251 WriteServ(socket,"NOTICE Auth :Looking up your hostname...");
4254 // set the registration timeout for this user
4255 unsigned long class_regtimeout = 90;
4256 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
4258 if (match(clientlist[tempnick]->host,i->host) && (i->type == CC_ALLOW))
4260 class_regtimeout = (unsigned long)i->registration_timeout;
4265 int class_flood = 0;
4266 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
4268 if (match(clientlist[tempnick]->host,i->host) && (i->type == CC_ALLOW))
4270 class_flood = i->flood;
4275 clientlist[tempnick]->timeout = time(NULL)+class_regtimeout;
4276 clientlist[tempnick]->flood = class_flood;
4278 for (int i = 0; i < MAXCHANS; i++)
4280 clientlist[tempnick]->chans[i].channel = NULL;
4281 clientlist[tempnick]->chans[i].uc_modes = 0;
4284 if (clientlist.size() == MAXCLIENTS)
4285 kill_link(clientlist[tempnick],"No more connections allowed in this class");
4288 void handle_names(char **parameters, int pcnt, userrec *user)
4292 if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
4294 c = FindChan(parameters[0]);
4297 /*WriteServ(user->fd,"353 %s = %s :%s", user->nick, c->name,*/
4299 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
4303 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4307 void handle_privmsg(char **parameters, int pcnt, userrec *user)
4312 user->idle_lastmsg = time(NULL);
4314 if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
4316 if (parameters[0][0] == '#')
4318 chan = FindChan(parameters[0]);
4321 if ((chan->noexternal) && (!has_channel(user,chan)))
4323 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
4326 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
4328 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
4334 FOREACH_RESULT(OnUserPreMessage(user,chan,TYPE_CHANNEL,std::string(parameters[1])));
4339 ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
4341 // if any users of this channel are on remote servers, broadcast the packet
4342 char buffer[MAXBUF];
4343 snprintf(buffer,MAXBUF,"P %s %s :%s",user->nick,chan->name,parameters[1]);
4344 for (int j = 0; j < 255; j++)
4346 if (servers[j] != NULL)
4348 if (ChanAnyOnThisServer(chan,servers[j]->name))
4350 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
4357 /* no such nick/channel */
4358 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4363 dest = Find(parameters[0]);
4366 if (strcmp(dest->awaymsg,""))
4368 /* auto respond with aweh msg */
4369 WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
4374 FOREACH_RESULT(OnUserPreMessage(user,dest,TYPE_USER,std::string(parameters[1])));
4381 if (!strcmp(dest->server,user->server))
4383 // direct write, same server
4384 WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
4388 for (int j = 0; j < 255; j++)
4390 if (servers[j] != NULL)
4392 if (!strcasecmp(servers[j]->name,dest->server))
4394 // direct write, same server
4395 char buffer[MAXBUF];
4396 snprintf(buffer,MAXBUF,"P %s %s :%s",user->nick,dest->nick,parameters[1]);
4397 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
4405 /* no such nick/channel */
4406 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4410 void handle_notice(char **parameters, int pcnt, userrec *user)
4415 user->idle_lastmsg = time(NULL);
4417 if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
4419 if (parameters[0][0] == '#')
4421 chan = FindChan(parameters[0]);
4424 if ((chan->noexternal) && (!has_channel(user,chan)))
4426 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
4429 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
4431 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
4437 FOREACH_RESULT(OnUserPreNotice(user,chan,TYPE_CHANNEL,std::string(parameters[1])));
4442 ChanExceptSender(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
4444 // if any users of this channel are on remote servers, broadcast the packet
4445 char buffer[MAXBUF];
4446 snprintf(buffer,MAXBUF,"V %s %s :%s",user->nick,chan->name,parameters[1]);
4447 for (int j = 0; j < 255; j++)
4449 if (servers[j] != NULL)
4451 if (ChanAnyOnThisServer(chan,servers[j]->name))
4453 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
4460 /* no such nick/channel */
4461 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4466 dest = Find(parameters[0]);
4471 FOREACH_RESULT(OnUserPreNotice(user,dest,TYPE_USER,std::string(parameters[1])));
4476 if (!strcmp(dest->server,user->server))
4478 // direct write, same server
4479 WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
4483 for (int j = 0; j < 255; j++)
4485 if (servers[j] != NULL)
4487 if (!strcasecmp(servers[j]->name,dest->server))
4489 // direct write, same server
4490 char buffer[MAXBUF];
4491 snprintf(buffer,MAXBUF,"V %s %s :%s",user->nick,dest->nick,parameters[1]);
4492 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
4500 /* no such nick/channel */
4501 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4507 char* chlist(userrec *user)
4512 log(DEBUG,"chlist: %s",user->nick);
4518 for (int i = 0; i != MAXCHANS; i++)
4520 if (user->chans[i].channel != NULL)
4522 if (user->chans[i].channel->name)
4524 strcpy(cmp,user->chans[i].channel->name);
4526 if (!strstr(lst,cmp))
4528 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
4530 strcat(lst,cmode(user,user->chans[i].channel));
4531 strcat(lst,user->chans[i].channel->name);
4540 lst[strlen(lst)-1] = '\0'; // chop trailing space
4545 void handle_info(char **parameters, int pcnt, userrec *user)
4547 WriteServ(user->fd,"371 %s :The Inspire IRCd Project Has been brought to you by the following people..",user->nick);
4548 WriteServ(user->fd,"371 %s :Craig Edwards, Craig McLure, and Others..",user->nick);
4549 WriteServ(user->fd,"371 %s :Will finish this later when i can be arsed :p",user->nick);
4550 FOREACH_MOD OnInfo(user);
4551 WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
4554 void handle_time(char **parameters, int pcnt, userrec *user)
4557 struct tm * timeinfo;
4560 timeinfo = localtime ( &rawtime );
4561 WriteServ(user->fd,"391 %s %s :%s",user->nick,ServerName, asctime (timeinfo) );
4565 void handle_whois(char **parameters, int pcnt, userrec *user)
4570 if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
4572 dest = Find(parameters[0]);
4575 // bug found by phidjit - were able to whois an incomplete connection if it had sent a NICK or USER
4576 if (dest->registered == 7)
4578 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
4579 if ((user == dest) || (strchr(user->modes,'o')))
4581 WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
4583 if (strcmp(chlist(dest),""))
4585 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
4587 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
4588 if (strcmp(dest->awaymsg,""))
4590 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
4592 if (strchr(dest->modes,'o'))
4594 WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
4596 FOREACH_MOD OnWhois(user,dest);
4597 //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
4598 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
4600 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
4604 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4609 /* no such nick/channel */
4610 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4614 void handle_quit(char **parameters, int pcnt, userrec *user)
4616 user_hash::iterator iter = clientlist.find(user->nick);
4619 if (user->registered == 7)
4621 /* theres more to do here, but for now just close the socket */
4624 if (parameters[0][0] == ':')
4628 reason = parameters[0];
4630 if (strlen(reason)>MAXQUIT)
4632 reason[MAXQUIT-1] = '\0';
4635 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
4636 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
4637 WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
4639 char buffer[MAXBUF];
4640 snprintf(buffer,MAXBUF,"Q %s :%s%s",user->nick,PrefixQuit,parameters[0]);
4641 for (int j = 0; j < 255; j++)
4643 if (servers[j] != NULL)
4645 if (CommonOnThisServer(user,servers[j]->name))
4647 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
4648 log(DEBUG,"Sent Q token");
4655 Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
4656 WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
4657 WriteCommonExcept(user,"QUIT :Client exited");
4659 char buffer[MAXBUF];
4660 snprintf(buffer,MAXBUF,"Q %s :Client exited",user->nick);
4661 for (int j = 0; j < 255; j++)
4663 if (servers[j] != NULL)
4665 if (CommonOnThisServer(user,servers[j]->name))
4667 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
4668 log(DEBUG,"Sent Q token");
4673 FOREACH_MOD OnUserQuit(user);
4677 /* push the socket on a stack of sockets due to be closed at the next opportunity */
4678 fd_reap.push_back(user->fd);
4680 if (iter != clientlist.end())
4682 clientlist.erase(iter);
4683 log(DEBUG,"deleting user hash value %d",iter->second);
4684 //if ((user) && (user->registered == 7)) {
4689 if (user->registered == 7) {
4690 purge_empty_chans();
4694 void handle_who(char **parameters, int pcnt, userrec *user)
4696 chanrec* Ptr = NULL;
4698 /* theres more to do here, but for now just close the socket */
4701 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
4703 if (user->chans[0].channel)
4705 Ptr = user->chans[0].channel;
4706 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4708 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
4710 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);
4716 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
4720 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, user->nick);
4724 if (parameters[0][0] == '#')
4726 Ptr = FindChan(parameters[0]);
4729 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4731 if ((has_channel(i->second,Ptr)) && (isnick(i->second->nick)))
4733 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);
4736 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
4740 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
4745 userrec* u = Find(parameters[0]);
4746 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);
4751 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
4753 Ptr = user->chans[0].channel;
4754 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4756 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
4758 if (strchr(i->second->modes,'o'))
4760 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);
4764 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
4770 void handle_wallops(char **parameters, int pcnt, userrec *user)
4772 WriteWallOps(user,"%s",parameters[0]);
4775 void handle_list(char **parameters, int pcnt, userrec *user)
4779 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
4780 for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
4782 if ((!i->second->c_private) && (!i->second->secret))
4784 WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
4787 WriteServ(user->fd,"323 %s :End of channel list.",user->nick);
4791 void handle_rehash(char **parameters, int pcnt, userrec *user)
4793 WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CleanFilename(CONFIG_FILE));
4795 FOREACH_MOD OnRehash();
4796 WriteOpers("%s is rehashing config file %s",user->nick,CleanFilename(CONFIG_FILE));
4802 return clientlist.size();
4806 int usercount_invisible(void)
4810 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4812 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
4817 int usercount_opers(void)
4821 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4823 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
4828 int usercount_unknown(void)
4832 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
4834 if ((i->second->fd) && (i->second->registered != 7))
4842 return chanlist.size();
4845 int servercount(void)
4850 void handle_lusers(char **parameters, int pcnt, userrec *user)
4852 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),servercount());
4853 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
4854 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
4855 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
4856 WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,usercnt());
4859 void handle_admin(char **parameters, int pcnt, userrec *user)
4861 WriteServ(user->fd,"256 %s :Administrative info for %s",user->nick,ServerName);
4862 WriteServ(user->fd,"257 %s :Name - %s",user->nick,AdminName);
4863 WriteServ(user->fd,"258 %s :Nickname - %s",user->nick,AdminNick);
4864 WriteServ(user->fd,"258 %s :E-Mail - %s",user->nick,AdminEmail);
4867 void ShowMOTD(userrec *user)
4871 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
4874 WriteServ(user->fd,"375 %s :- %s message of the day",user->nick,ServerName);
4875 for (int i = 0; i != MOTD.size(); i++)
4877 WriteServ(user->fd,"372 %s :- %s",user->nick,MOTD[i].c_str());
4879 WriteServ(user->fd,"376 %s :End of %s message of the day.",user->nick,ServerName);
4882 void ShowRULES(userrec *user)
4886 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
4889 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
4890 for (int i = 0; i != RULES.size(); i++)
4892 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
4894 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
4897 /* shows the message of the day, and any other on-logon stuff */
4898 void ConnectUser(userrec *user)
4900 user->registered = 7;
4901 user->idle_lastmsg = time(NULL);
4902 log(DEBUG,"ConnectUser: %s",user->nick);
4904 if (strcmp(Passwd(user),"") && (!user->haspassed))
4906 kill_link(user,"Invalid password");
4911 kill_link(user,"Unauthorised connection");
4915 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
4916 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
4917 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
4918 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
4919 WriteServ(user->fd,"004 %s :%s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
4920 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);
4921 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);
4923 FOREACH_MOD OnUserConnect(user);
4924 WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
4926 char buffer[MAXBUF];
4927 snprintf(buffer,MAXBUF,"N %d %s %s %s %s +%s %s :%s",user->age,user->nick,user->host,user->dhost,user->ident,user->modes,ServerName,user->fullname);
4928 for (int j = 0; j < 255; j++)
4930 if (servers[j] != NULL)
4932 if (strcmp(servers[j]->name,ServerName))
4934 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
4935 log(DEBUG,"Sent N token");
4942 void handle_version(char **parameters, int pcnt, userrec *user)
4944 WriteServ(user->fd,"351 %s :%s %s %s :%s",user->nick,VERSION,"$Revision$ $Date$",ServerName,SYSTEM);
4947 void handle_ping(char **parameters, int pcnt, userrec *user)
4949 WriteServ(user->fd,"PONG %s :%s",ServerName,parameters[0]);
4952 void handle_pong(char **parameters, int pcnt, userrec *user)
4954 // set the user as alive so they survive to next ping
4958 void handle_motd(char **parameters, int pcnt, userrec *user)
4963 void handle_rules(char **parameters, int pcnt, userrec *user)
4968 void handle_user(char **parameters, int pcnt, userrec *user)
4970 if (user->registered < 3)
4972 if (isident(parameters[0]) == 0) {
4973 // This kinda Sucks, According to the RFC thou, its either this,
4974 // or "You have already registered" :p -- Craig
4975 WriteServ(user->fd,"461 %s USER :Not enough parameters",user->nick);
4978 WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
4979 strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
4980 strncat(user->ident,parameters[0],IDENTMAX);
4981 strncpy(user->fullname,parameters[3],128);
4982 user->registered = (user->registered | 1);
4987 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
4990 /* parameters 2 and 3 are local and remote hosts, ignored when sent by client connection */
4991 if (user->registered == 3)
4993 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
4998 void handle_userhost(char **parameters, int pcnt, userrec *user)
5000 char Return[MAXBUF],junk[MAXBUF];
5001 sprintf(Return,"302 %s :",user->nick);
5002 for (int i = 0; i < pcnt; i++)
5004 userrec *u = Find(parameters[i]);
5007 if (strchr(u->modes,'o'))
5009 sprintf(junk,"%s*=+%s@%s ",u->nick,u->ident,u->host);
5010 strcat(Return,junk);
5014 sprintf(junk,"%s=+%s@%s ",u->nick,u->ident,u->host);
5015 strcat(Return,junk);
5019 WriteServ(user->fd,Return);
5023 void handle_ison(char **parameters, int pcnt, userrec *user)
5025 char Return[MAXBUF];
5026 sprintf(Return,"303 %s :",user->nick);
5027 for (int i = 0; i < pcnt; i++)
5029 userrec *u = Find(parameters[i]);
5032 strcat(Return,u->nick);
5036 WriteServ(user->fd,Return);
5040 void handle_away(char **parameters, int pcnt, userrec *user)
5044 strcpy(user->awaymsg,parameters[0]);
5045 WriteServ(user->fd,"306 %s :You have been marked as being away",user->nick);
5049 strcpy(user->awaymsg,"");
5050 WriteServ(user->fd,"305 %s :You are no longer marked as being away",user->nick);
5054 void handle_whowas(char **parameters, int pcnt, userrec* user)
5056 user_hash::iterator i = whowas.find(parameters[0]);
5058 if (i == whowas.end())
5060 WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
5061 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
5065 time_t rawtime = i->second->signon;
5069 timeinfo = localtime(&rawtime);
5070 strcpy(b,asctime(timeinfo));
5071 b[strlen(b)-1] = '\0';
5073 WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
5074 WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
5075 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
5080 void handle_trace(char **parameters, int pcnt, userrec *user)
5082 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
5086 if (isnick(i->second->nick))
5088 if (strchr(i->second->modes,'o'))
5090 WriteServ(user->fd,"205 %s :Oper 0 %s",user->nick,i->second->nick);
5094 WriteServ(user->fd,"204 %s :User 0 %s",user->nick,i->second->nick);
5099 WriteServ(user->fd,"203 %s :???? 0 [%s]",user->nick,i->second->host);
5105 void handle_modules(char **parameters, int pcnt, userrec *user)
5107 for (int i = 0; i < module_names.size(); i++)
5109 Version V = modules[i]->GetVersion();
5110 char modulename[MAXBUF];
5111 strncpy(modulename,module_names[i].c_str(),256);
5112 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));
5116 void handle_stats(char **parameters, int pcnt, userrec *user)
5122 if (strlen(parameters[0])>1)
5124 /* make the stats query 1 character long */
5125 parameters[0][1] = '\0';
5128 /* stats m (list number of times each command has been used, plus bytecount) */
5129 if (!strcasecmp(parameters[0],"m"))
5131 for (int i = 0; i < cmdlist.size(); i++)
5133 if (cmdlist[i].handler_function)
5135 if (cmdlist[i].use_count)
5137 /* RPL_STATSCOMMANDS */
5138 WriteServ(user->fd,"212 %s %s %d %d",user->nick,cmdlist[i].command,cmdlist[i].use_count,cmdlist[i].total_bytes);
5145 /* stats z (debug and memory info) */
5146 if (!strcasecmp(parameters[0],"z"))
5148 WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
5149 WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
5150 WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
5151 WriteServ(user->fd,"249 %s :MOTD(VECTOR) %d, RULES(VECTOR) %d",user->nick,MOTD.size(),RULES.size());
5152 WriteServ(user->fd,"249 %s :address_cache(HASH_MAP) %d (%d buckets)",user->nick,IP.size(),IP.bucket_count());
5153 WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
5154 WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
5155 WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
5159 if (!strcasecmp(parameters[0],"o"))
5161 for (int i = 0; i < ConfValueEnum("oper",&config_f); i++)
5163 char LoginName[MAXBUF];
5164 char HostName[MAXBUF];
5165 char OperType[MAXBUF];
5166 ConfValue("oper","name",i,LoginName,&config_f);
5167 ConfValue("oper","host",i,HostName,&config_f);
5168 ConfValue("oper","type",i,OperType,&config_f);
5169 WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
5173 /* stats l (show user I/O stats) */
5174 if (!strcasecmp(parameters[0],"l"))
5176 WriteServ(user->fd,"211 %s :server:port nick bytes_in cmds_in bytes_out cmds_out",user->nick);
5177 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
5179 if (isnick(i->second->nick))
5181 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);
5185 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);
5191 /* stats u (show server uptime) */
5192 if (!strcasecmp(parameters[0],"u"))
5194 time_t current_time = 0;
5195 current_time = time(NULL);
5196 time_t server_uptime = current_time - startup_time;
5198 stime = gmtime(&server_uptime);
5199 /* i dont know who the hell would have an ircd running for over a year nonstop, but
5200 * Craig suggested this, and it seemed a good idea so in it went */
5201 if (stime->tm_year > 70)
5203 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);
5207 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);
5211 WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
5212 WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
5216 void handle_connect(char **parameters, int pcnt, userrec *user)
5218 char Link_ServerName[1024];
5219 char Link_IPAddr[1024];
5220 char Link_Port[1024];
5221 char Link_Pass[1024];
5225 for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
5227 ConfValue("link","name",i,Link_ServerName,&config_f);
5228 ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
5229 ConfValue("link","port",i,Link_Port,&config_f);
5230 ConfValue("link","sendpass",i,Link_Pass,&config_f);
5231 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
5232 LinkPort = atoi(Link_Port);
5233 if (match(Link_ServerName,parameters[0])) {
5239 WriteServ(user->fd,"NOTICE %s :*** Failed to connect to %s: No servers matching this pattern are configured for linking.",user->nick,parameters[0]);
5243 // TODO: Perform a check here to stop a server being linked twice!
5245 WriteServ(user->fd,"NOTICE %s :*** Connecting to %s (%s) port %s...",user->nick,Link_ServerName,Link_IPAddr,Link_Port);
5247 if (me[defaultRoute])
5250 // at this point parameters[0] is an ip in a string.
5251 // TODO: Look this up from the <link> blocks instead!
5252 for (int j = 0; j < 255; j++) {
5253 if (servers[j] == NULL) {
5254 servers[j] = new serverrec;
5255 strcpy(servers[j]->internal_addr,Link_IPAddr);
5256 servers[j]->internal_port = LinkPort;
5257 strcpy(servers[j]->name,Link_ServerName);
5258 log(DEBUG,"Allocated new serverrec");
5259 if (!me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass))
5261 WriteServ(user->fd,"NOTICE %s :*** Failed to send auth packet to %s!",user->nick,Link_IPAddr);
5266 WriteServ(user->fd,"NOTICE %s :*** Failed to create server record for address %s!",user->nick,Link_IPAddr);
5270 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);
5274 void handle_squit(char **parameters, int pcnt, userrec *user)
5276 // send out an squit across the mesh and then clear the server list (for local squit)
5279 char islast(serverrec* s)
5282 for (int j = 0; j < 255; j++)
5284 if (servers[j] != NULL)
5288 if (servers[j] == s)
5296 void handle_links(char **parameters, int pcnt, userrec *user)
5298 WriteServ(user->fd,"364 %s %s %s :0 %s",user->nick,ServerName,ServerName,ServerDesc);
5299 for (int j = 0; j < 255; j++)
5301 if (servers[j] != NULL)
5303 WriteServ(user->fd,"364 %s %s %s :1 %s",user->nick,servers[j]->name,ServerName,servers[j]->description);
5306 WriteServ(user->fd,"365 %s * :End of /LINKS list.",user->nick);
5309 void handle_map(char **parameters, int pcnt, userrec *user)
5311 WriteServ(user->fd,"006 %s :%s",user->nick,ServerName);
5312 for (int j = 0; j < 255; j++)
5314 if (servers[j] != NULL)
5316 WriteServ(user->fd,"006 %s :%c-%s",user->nick,islast(servers[j]),servers[j]->name);
5319 WriteServ(user->fd,"007 %s :End of /MAP",user->nick);
5323 void handle_oper(char **parameters, int pcnt, userrec *user)
5325 char LoginName[MAXBUF];
5326 char Password[MAXBUF];
5327 char OperType[MAXBUF];
5328 char TypeName[MAXBUF];
5329 char Hostname[MAXBUF];
5332 for (int i = 0; i < ConfValueEnum("oper",&config_f); i++)
5334 ConfValue("oper","name",i,LoginName,&config_f);
5335 ConfValue("oper","password",i,Password,&config_f);
5336 if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
5338 /* correct oper credentials */
5339 ConfValue("oper","type",i,OperType,&config_f);
5340 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
5341 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
5342 WriteServ(user->fd,"MODE %s :+o",user->nick);
5343 for (j =0; j < ConfValueEnum("type",&config_f); j++)
5345 ConfValue("type","name",j,TypeName,&config_f);
5346 if (!strcmp(TypeName,OperType))
5348 /* found this oper's opertype */
5349 ConfValue("type","host",j,Hostname,&config_f);
5350 strncpy(user->dhost,Hostname,256);
5353 if (!strchr(user->modes,'o'))
5355 strcat(user->modes,"o");
5357 FOREACH_MOD OnOper(user);
5362 WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
5363 WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
5366 void handle_nick(char **parameters, int pcnt, userrec *user)
5370 log(DEBUG,"not enough params for handle_nick");
5375 log(DEBUG,"invalid parameter passed to handle_nick");
5378 if (!strlen(parameters[0]))
5380 log(DEBUG,"zero length new nick passed to handle_nick");
5385 log(DEBUG,"invalid user passed to handle_nick");
5390 log(DEBUG,"invalid old nick passed to handle_nick");
5393 if (!strcasecmp(user->nick,parameters[0]))
5395 log(DEBUG,"old nick is new nick, skipping");
5400 if (strlen(parameters[0]) > 1)
5402 if (parameters[0][0] == ':')
5407 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
5409 WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
5413 if (isnick(parameters[0]) == 0)
5415 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
5419 if (user->registered == 7)
5421 WriteCommon(user,"NICK %s",parameters[0]);
5423 // Q token must go to ALL servers!!!
5424 char buffer[MAXBUF];
5425 snprintf(buffer,MAXBUF,"n %s %s",user->nick,parameters[0]);
5426 for (int j = 0; j < 255; j++)
5428 if (servers[j] != NULL)
5430 if (strcmp(servers[j]->name,ServerName))
5432 me[defaultRoute]->SendPacket(buffer,servers[j]->internal_addr,servers[j]->internal_port,MyKey);
5433 log(DEBUG,"Sent n token");
5440 /* change the nick of the user in the users_hash */
5441 user = ReHashNick(user->nick, parameters[0]);
5442 /* actually change the nick within the record */
5444 if (!user->nick) return;
5446 strncpy(user->nick, parameters[0],NICKMAX);
5448 log(DEBUG,"new nick set: %s",user->nick);
5450 if (user->registered < 3)
5451 user->registered = (user->registered | 2);
5452 if (user->registered == 3)
5454 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
5457 log(DEBUG,"exit nickchange: %s",user->nick);
5460 void force_nickchange(userrec* user,const char* newnick)
5469 strncpy(nick,newnick,MAXBUF);
5471 if (user->registered == 7)
5475 handle_nick(pars,1,user);
5481 int process_parameters(char **command_p,char *parameters)
5486 q = strlen(parameters);
5489 /* no parameters, command_p invalid! */
5492 if (parameters[0] == ':')
5494 command_p[0] = parameters+1;
5499 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
5501 /* only one parameter */
5502 command_p[0] = parameters;
5503 if (parameters[0] == ':')
5505 if (strchr(parameters,' ') != NULL)
5513 command_p[j++] = parameters;
5514 for (int i = 0; i <= q; i++)
5516 if (parameters[i] == ' ')
5518 command_p[j++] = parameters+i+1;
5519 parameters[i] = '\0';
5520 if (command_p[j-1][0] == ':')
5522 *command_p[j-1]++; /* remove dodgy ":" */
5524 /* parameter like this marks end of the sequence */
5528 return j; /* returns total number of items in the list */
5531 void process_command(userrec *user, char* cmd)
5535 char *command_p[127];
5536 char p[MAXBUF], temp[MAXBUF];
5537 int i, j, items, cmd_found;
5539 for (int i = 0; i < 127; i++)
5540 command_p[i] = NULL;
5550 if (!strcmp(cmd,""))
5555 int total_params = 0;
5558 for (int q = 0; q < strlen(cmd)-1; q++)
5560 if ((cmd[q] == ' ') && (cmd[q+1] == ':'))
5563 // found a 'trailing', we dont count them after this.
5571 // another phidjit bug...
5572 if (total_params > 126)
5574 kill_link(user,"Protocol violation (1)");
5581 FOREACH_MOD OnServerRaw(tmp,true);
5582 const char* cmd2 = tmp.c_str();
5583 snprintf(cmd,512,"%s",cmd2);
5585 if (!strchr(cmd,' '))
5587 /* no parameters, lets skip the formalities and not chop up
5589 log(DEBUG,"About to preprocess command with no params");
5591 command_p[0] = NULL;
5593 for (int i = 0; i <= strlen(cmd); i++)
5595 cmd[i] = toupper(cmd[i]);
5597 log(DEBUG,"Preprocess done length=%d",strlen(cmd));
5604 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
5605 for (int i = 0; i < strlen(temp); i++)
5607 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
5613 /* split the full string into a command plus parameters */
5617 if (strchr(cmd,' '))
5619 for (int i = 0; i <= strlen(cmd); i++)
5621 /* capitalise the command ONLY, leave params intact */
5622 cmd[i] = toupper(cmd[i]);
5623 /* are we nearly there yet?! :P */
5627 parameters = cmd+i+1;
5635 for (int i = 0; i <= strlen(cmd); i++)
5637 cmd[i] = toupper(cmd[i]);
5644 if (strlen(command)>MAXCOMMAND)
5646 kill_link(user,"Protocol violation (2)");
5650 for (int x = 0; x < strlen(command); x++)
5652 if (((command[x] < 'A') || (command[x] > 'Z')) && (command[x] != '.'))
5654 if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
5656 if (strchr("@!\"$%^&*(){}[]_-=+;:'#~,.<>/?\\|`",command[x]))
5658 kill_link(user,"Protocol violation (3)");
5665 for (int i = 0; i != cmdlist.size(); i++)
5667 if (strcmp(cmdlist[i].command,""))
5669 if (strlen(command)>=(strlen(cmdlist[i].command))) if (!strncmp(command, cmdlist[i].command,MAXCOMMAND))
5671 log(DEBUG,"Found matching command");
5675 if (strcmp(parameters,""))
5677 items = process_parameters(command_p,parameters);
5682 command_p[0] = NULL;
5688 command_p[0] = NULL;
5693 log(DEBUG,"Processing command");
5695 /* activity resets the ping pending timer */
5696 user->nping = time(NULL) + 120;
5697 if ((items) < cmdlist[i].min_params)
5699 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
5700 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
5703 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
5705 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
5706 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
5710 /* if the command isnt USER, PASS, or NICK, and nick is empty,
5712 if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
5714 if ((!isnick(user->nick)) || (user->registered != 7))
5716 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
5717 WriteServ(user->fd,"451 %s :You have not registered",command);
5721 if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
5723 log(DEBUG,"process_command: handler: %s %s %d",user->nick,command,items);
5724 if (cmdlist[i].handler_function)
5726 /* ikky /stats counters */
5731 user->bytes_in += strlen(temp);
5734 cmdlist[i].use_count++;
5735 cmdlist[i].total_bytes+=strlen(temp);
5738 /* WARNING: nothing may come after the
5739 * command handler call, as the handler
5740 * may free the user structure! */
5742 cmdlist[i].handler_function(command_p,items,user);
5748 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
5749 WriteServ(user->fd,"451 %s :You have not registered",command);
5757 if ((!cmd_found) && (user))
5759 log(DEBUG,"process_command: not in table: %s %s",user->nick,command);
5760 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
5765 void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
5768 /* create the command and push it onto the table */
5769 strcpy(comm.command,cmd);
5770 comm.handler_function = f;
5771 comm.flags_needed = flags;
5772 comm.min_params = minparams;
5774 comm.total_bytes = 0;
5775 cmdlist.push_back(comm);
5776 log(DEBUG,"Added command %s (%d parameters)",cmd,minparams);
5779 void SetupCommandTable(void)
5781 createcommand("USER",handle_user,0,4);
5782 createcommand("NICK",handle_nick,0,1);
5783 createcommand("QUIT",handle_quit,0,0);
5784 createcommand("VERSION",handle_version,0,0);
5785 createcommand("PING",handle_ping,0,1);
5786 createcommand("PONG",handle_pong,0,1);
5787 createcommand("ADMIN",handle_admin,0,0);
5788 createcommand("PRIVMSG",handle_privmsg,0,2);
5789 createcommand("INFO",handle_info,0,0);
5790 createcommand("TIME",handle_time,0,0);
5791 createcommand("WHOIS",handle_whois,0,1);
5792 createcommand("WALLOPS",handle_wallops,'o',1);
5793 createcommand("NOTICE",handle_notice,0,2);
5794 createcommand("JOIN",handle_join,0,1);
5795 createcommand("NAMES",handle_names,0,1);
5796 createcommand("PART",handle_part,0,1);
5797 createcommand("KICK",handle_kick,0,2);
5798 createcommand("MODE",handle_mode,0,1);
5799 createcommand("TOPIC",handle_topic,0,1);
5800 createcommand("WHO",handle_who,0,1);
5801 createcommand("MOTD",handle_motd,0,0);
5802 createcommand("RULES",handle_rules,0,0);
5803 createcommand("OPER",handle_oper,0,2);
5804 createcommand("LIST",handle_list,0,0);
5805 createcommand("DIE",handle_die,'o',1);
5806 createcommand("RESTART",handle_restart,'o',1);
5807 createcommand("KILL",handle_kill,'o',2);
5808 createcommand("REHASH",handle_rehash,'o',0);
5809 createcommand("LUSERS",handle_lusers,0,0);
5810 createcommand("STATS",handle_stats,0,1);
5811 createcommand("USERHOST",handle_userhost,0,1);
5812 createcommand("AWAY",handle_away,0,0);
5813 createcommand("ISON",handle_ison,0,0);
5814 createcommand("SUMMON",handle_summon,0,0);
5815 createcommand("USERS",handle_users,0,0);
5816 createcommand("INVITE",handle_invite,0,2);
5817 createcommand("PASS",handle_pass,0,1);
5818 createcommand("TRACE",handle_trace,'o',0);
5819 createcommand("WHOWAS",handle_whowas,0,1);
5820 createcommand("CONNECT",handle_connect,'o',1);
5821 createcommand("SQUIT",handle_squit,'o',1);
5822 createcommand("MODULES",handle_modules,'o',0);
5823 createcommand("LINKS",handle_links,0,0);
5824 createcommand("MAP",handle_map,0,0);
5827 void process_buffer(const char* cmdbuf,userrec *user)
5831 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
5838 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
5841 if (!strcmp(cmdbuf,""))
5845 strncpy(cmd,cmdbuf,MAXBUF);
5846 if (!strcmp(cmd,""))
5850 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
5852 cmd[strlen(cmd)-1] = '\0';
5854 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
5856 cmd[strlen(cmd)-1] = '\0';
5858 if (!strcmp(cmd,""))
5862 log(DEBUG,"InspIRCd: processing: %s %s",user->nick,cmd);
5864 if ((user) && (cmd))
5866 process_command(user,cmd);
5870 void DoSync(serverrec* serv, char* udp_host,int udp_port, long MyKey)
5873 // send start of sync marker: Y <timestamp>
5874 // at this point the ircd receiving it starts broadcasting this netburst to all ircds
5875 // except the ones its receiving it from.
5876 snprintf(data,MAXBUF,"Y %d",time(NULL));
5877 serv->SendPacket(data,udp_host,udp_port,MyKey);
5878 // send users and channels
5879 for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
5881 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);
5882 serv->SendPacket(data,udp_host,udp_port,MyKey);
5883 if (strcmp(chlist(u->second),""))
5885 snprintf(data,MAXBUF,"J %s %s",u->second->nick,chlist(u->second));
5886 serv->SendPacket(data,udp_host,udp_port,MyKey);
5889 // send channel modes, topics etc...
5890 for (chan_hash::iterator c = chanlist.begin(); c != chanlist.end(); c++)
5892 snprintf(data,MAXBUF,"M %s +%s",c->second->name,chanmodes(c->second));
5893 serv->SendPacket(data,udp_host,udp_port,MyKey);
5894 if (strcmp(c->second->topic,""))
5896 snprintf(data,MAXBUF,"T %d %s %s :%s",c->second->topicset,c->second->setby,c->second->name,c->second->topic);
5897 serv->SendPacket(data,udp_host,udp_port,MyKey);
5899 // send current banlist
5901 for (BanList::iterator b = c->second->bans.begin(); b != c->second->bans.end(); b++)
5903 snprintf(data,MAXBUF,"M %s +b %s",b->set_time,c->second->name,b->data);
5904 serv->SendPacket(data,udp_host,udp_port,MyKey);
5907 // send end of sync marker: E <timestamp>
5908 snprintf(data,MAXBUF,"F %d",time(NULL));
5909 serv->SendPacket(data,udp_host,udp_port,MyKey);
5910 // ircd sends its serverlist after the end of sync here
5914 void handle_V(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
5916 char* src = strtok(params," ");
5917 char* dest = strtok(NULL," :");
5918 char* text = strtok(NULL,"\r\n");
5921 userrec* user = Find(src);
5924 userrec* dst = Find(dest);
5928 WriteTo(user, dst, "NOTICE %s :%s", dst->nick, text);
5932 chanrec* d = FindChan(dest);
5935 ChanExceptSender(d, user, "NOTICE %s :%s", d->name, text);
5943 void handle_P(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
5945 char* src = strtok(params," ");
5946 char* dest = strtok(NULL," :");
5947 char* text = strtok(NULL,"\r\n");
5950 userrec* user = Find(src);
5953 userrec* dst = Find(dest);
5957 WriteTo(user, dst, "PRIVMSG %s :%s", dst->nick, text);
5961 chanrec* d = FindChan(dest);
5964 ChanExceptSender(d, user, "PRIVMSG %s :%s", d->name, text);
5972 void handle_T(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
5974 char* tm = strtok(params," ");
5975 char* setby = strtok(NULL," ");
5976 char* channel = strtok(NULL," :");
5977 char* topic = strtok(NULL,"\r\n");
5979 time_t TS = atoi(tm);
5980 chanrec* c = FindChan(channel);
5983 // in the case of topics and TS, the *NEWER*
5984 if (TS <= c->topicset)
5986 WriteChannelLocal(c,NULL,"TOPIC %s :%s",c->name,topic);
5987 strncpy(c->topic,topic,MAXTOPIC);
5988 strncpy(c->setby,setby,NICKMAX);
5993 void handle_M(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
5996 char original[MAXBUF],target[MAXBUF];
5997 strncpy(original,params,MAXBUF);
5999 char* parameter = strtok(params," ");
6000 strncpy(target,parameter,MAXBUF);
6003 pars[index++] = parameter;
6004 parameter = strtok(NULL," ");
6006 merge_mode(pars,--index);
6007 if (FindChan(target))
6009 WriteChannelLocal(FindChan(target), NULL, "MODE %s",original);
6013 WriteTo(NULL,Find(target),"MODE %s",original);
6017 // m is modes set by users only (not servers) valid targets are channels or users.
6019 void handle_m(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
6021 // m blah #chatspike +b *!test@*4
6023 char original[MAXBUF];
6024 strncpy(original,params,MAXBUF);
6026 if (!strchr(params,' '))
6028 WriteOpers("WARNING! 'm' token in data stream without any parameters! Something fishy is going on!");
6039 char* src = strtok(params," ");
6040 userrec* user = Find(src);
6044 log(DEBUG,"Found user: %s",user->nick);
6045 char* parameter = strtok(NULL," ");
6048 pars[index++] = parameter;
6049 parameter = strtok(NULL," ");
6052 log(DEBUG,"Calling merge_mode2");
6053 merge_mode2(pars,--index,user);
6058 void handle_L(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
6060 char* nick = strtok(params," ");
6061 char* channel = strtok(NULL," :");
6062 char* reason = strtok(NULL,"\r\n");
6063 userrec* user = Find(nick);
6067 if (strcmp(reason,""))
6069 del_channel(user,channel,reason,true);
6073 del_channel(user,channel,NULL,true);
6078 void handle_Q(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
6080 char* nick = strtok(params," :");
6081 char* reason = strtok(NULL,"\r\n");
6084 userrec* user = Find(nick);
6088 if (strlen(reason)>MAXQUIT)
6090 reason[MAXQUIT-1] = '\0';
6094 WriteCommonExcept(user,"QUIT :%s",reason);
6096 user_hash::iterator iter = clientlist.find(user->nick);
6098 if (iter != clientlist.end())
6100 log(DEBUG,"deleting user hash value %d",iter->second);
6101 if ((iter->second) && (user->registered == 7)) {
6102 delete iter->second;
6104 clientlist.erase(iter);
6107 purge_empty_chans();
6111 void handle_n(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
6113 char* oldnick = strtok(params," ");
6114 char* newnick = strtok(NULL," ");
6116 userrec* user = Find(oldnick);
6120 WriteCommon(user,"NICK %s",newnick);
6121 user = ReHashNick(user->nick, newnick);
6123 if (!user->nick) return;
6124 strncpy(user->nick, newnick,NICKMAX);
6125 log(DEBUG,"new nick set: %s",user->nick);
6129 void handle_N(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
6131 char* tm = strtok(params," ");
6132 char* nick = strtok(NULL," ");
6133 char* host = strtok(NULL," ");
6134 char* dhost = strtok(NULL," ");
6135 char* ident = strtok(NULL," ");
6136 char* modes = strtok(NULL," ");
6137 char* server = strtok(NULL," :");
6138 char* gecos = strtok(NULL,"\r\n");
6141 time_t TS = atoi(tm);
6142 user_hash::iterator iter = clientlist.find(nick);
6143 if (iter != clientlist.end())
6146 WriteOpers("Nickname collision: %s@%s != %s@%s",nick,server,iter->second->nick,iter->second->server);
6147 if (TS >= iter->second->age)
6150 snprintf(str,MAXBUF,"Killed (Nick Collision (%s@%s < %s@%s))",nick,server,iter->second->nick,iter->second->server);
6151 WriteServ(iter->second->fd, "KILL %s :%s",iter->second->nick,str);
6152 // client on remote server is older than the local user, kill the local user
6153 kill_link(iter->second,str);
6156 clientlist[nick] = new userrec();
6157 // remote users have an fd of -1. This is so that our Write abstraction
6158 // routines know to route any messages to this record away to whatever server
6160 clientlist[nick]->fd = -1;
6161 strncpy(clientlist[nick]->nick, nick,NICKMAX);
6162 strncpy(clientlist[nick]->host, host,160);
6163 strncpy(clientlist[nick]->dhost, dhost,160);
6164 strncpy(clientlist[nick]->server, server,256);
6165 strncpy(clientlist[nick]->ident, ident,10); // +1 char to compensate for '~'
6166 strncpy(clientlist[nick]->fullname, gecos,128);
6167 clientlist[nick]->signon = TS;
6168 clientlist[nick]->nping = 0; // this is ignored for a remote user anyway.
6169 clientlist[nick]->lastping = 1;
6170 clientlist[nick]->port = 0; // so is this...
6171 clientlist[nick]->registered = 7; // this however we need to set for them to receive messages and appear online
6172 clientlist[nick]->idle_lastmsg = time(NULL); // this is unrealiable and wont actually be used locally
6173 for (int i = 0; i < MAXCHANS; i++)
6175 clientlist[nick]->chans[i].channel = NULL;
6176 clientlist[nick]->chans[i].uc_modes = 0;
6180 void handle_F(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
6182 long tdiff = time(NULL) - atoi(params);
6184 WriteOpers("TS split for %s -> %s: %d",source->name,reply->name,tdiff);
6187 void handle_J(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
6190 // The J token currently has no timestamp - this needs looking at
6191 // because it will allow splitriding.
6192 char* nick = strtok(params," ");
6193 char* channel = strtok(NULL," ");
6194 userrec* user = Find(nick);
6197 if ((user != NULL) && (strcmp(channel,"")))
6199 char privilage = '\0';
6200 if (channel[0] != '#')
6202 privilage = channel[0];
6205 add_channel(user,channel,"",true);
6207 // now work out the privilages they should have on each channel
6208 // and send the appropriate servermodes.
6209 for (int i = 0; i != MAXCHANS; i++)
6211 if (user->chans[i].channel)
6213 if (!strcasecmp(user->chans[i].channel->name,channel))
6215 if (privilage == '@')
6217 user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_OP;
6218 WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +o %s",channel,user->nick);
6220 if (privilage == '%')
6222 user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_HOP;
6223 WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +h %s",channel,user->nick);
6225 if (privilage == '+')
6227 user->chans[i].uc_modes = user->chans[i].uc_modes | UCMODE_VOICE;
6228 WriteChannelLocal(user->chans[i].channel, NULL, "MODE %s +o %s",channel,user->nick);
6235 channel = strtok(NULL," ");
6239 void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
6246 nb_start = time(NULL);
6247 WriteOpers("Server %s is starting netburst.",source->name);
6249 // N <TS> <NICK> <HOST> <DHOST> <IDENT> <MODES> <SERVER> :<GECOS>
6250 // introduce remote client
6252 handle_N(token,params,source,reply,udp_host,udp_port);
6254 // n <NICK> <NEWNICK>
6255 // change nickname of client -- a server should only be able to
6256 // change the nicknames of clients that reside on it unless
6259 handle_n(token,params,source,reply,udp_host,udp_port);
6261 // J <NICK> <CHANLIST>
6262 // Join user to channel list, merge channel permissions
6264 handle_J(token,params,source,reply,udp_host,udp_port);
6266 // T <TS> <CHANNEL> <TOPICSETTER> :<TOPIC>
6267 // change channel topic (netburst only)
6269 handle_T(token,params,source,reply,udp_host,udp_port);
6271 // M <TARGET> <MODES> [MODE-PARAMETERS]
6272 // Server setting modes on an object
6274 handle_M(token,params,source,reply,udp_host,udp_port);
6276 // m <SOURCE> <TARGET> <MODES> [MODE-PARAMETERS]
6277 // User setting modes on an object
6279 handle_m(token,params,source,reply,udp_host,udp_port);
6281 // P <SOURCE> <TARGET> :<TEXT>
6282 // Send a private/channel message
6284 handle_P(token,params,source,reply,udp_host,udp_port);
6286 // V <SOURCE> <TARGET> :<TEXT>
6287 // Send a private/channel notice
6289 handle_V(token,params,source,reply,udp_host,udp_port);
6291 // L <SOURCE> <CHANNEL> :<REASON>
6292 // User parting a channel
6294 handle_L(token,params,source,reply,udp_host,udp_port);
6296 // Q <SOURCE> :<REASON>
6299 handle_Q(token,params,source,reply,udp_host,udp_port);
6304 WriteOpers("Server %s has completed netburst. (%d secs)",source->name,time(NULL)-nb_start);
6305 handle_F(token,params,source,reply,udp_host,udp_port);
6309 WriteOpers("WARNING! Unknown datagram type '%c'",token);
6315 void handle_link_packet(long theirkey, char* udp_msg, char* udp_host, int udp_port, serverrec *serv)
6317 char response[10240];
6318 char token = udp_msg[0];
6319 char* params = udp_msg + 2;
6320 char finalparam[1024];
6321 strcpy(finalparam," :xxxx");
6322 if (strstr(params," :")) {
6323 strncpy(finalparam,strstr(params," :"),1024);
6326 // S test.chatspike.net password :ChatSpike InspIRCd test server
6327 char* servername = strtok(params," ");
6328 char* password = strtok(NULL," ");
6329 char* serverdesc = finalparam+2;
6330 WriteOpers("CONNECT from %s (%s)",servername,udp_host);
6333 char Link_ServerName[1024];
6334 char Link_IPAddr[1024];
6335 char Link_Port[1024];
6336 char Link_Pass[1024];
6337 char Link_SendPass[1024];
6340 // search for a corresponding <link> block in the config files
6341 for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
6343 ConfValue("link","name",i,Link_ServerName,&config_f);
6344 ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
6345 ConfValue("link","port",i,Link_Port,&config_f);
6346 ConfValue("link","recvpass",i,Link_Pass,&config_f);
6347 ConfValue("link","sendpass",i,Link_SendPass,&config_f);
6348 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
6349 LinkPort = atoi(Link_Port);
6350 if (!strcasecmp(Link_ServerName,servername)) {
6351 if (!strcasecmp(Link_IPAddr,udp_host)) {
6352 if (LinkPort == udp_port) {
6353 // we have a matching link line -
6354 // send a 'diminutive' server message back...
6355 snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
6356 serv->SendPacket(response,udp_host,udp_port,0);
6357 WriteOpers("CONNECT from %s accepted, authenticating",servername);
6358 for (int j = 0; j < 255; j++) {
6359 if (servers[j] == NULL) {
6360 servers[j] = new serverrec;
6361 strcpy(servers[j]->internal_addr,udp_host);
6362 strcpy(servers[j]->name,servername);
6363 strcpy(servers[j]->description,serverdesc);
6364 servers[j]->internal_port = udp_port;
6365 // create a server record for this server
6366 snprintf(response,10240,"O %d",MyKey);
6367 serv->SendPacket(response,udp_host,udp_port,0);
6371 WriteOpers("Internal error connecting to %s, failed to create server record!",servername);
6375 log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
6379 log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
6383 log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
6386 serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
6387 WriteOpers("CONNECT from %s denied, no matching link block",servername);
6392 // if this is received, this means the server-ip that sent it said "OK" to credentials.
6393 // only when a server says this do we exchange keys. The server MUST have an entry in the servers
6394 // array, which is only added by an 'S' packet or BeginLink().
6395 for (int i = 0; i < 255; i++) {
6396 if (servers[i] != NULL) {
6397 if (!strcasecmp(servers[i]->internal_addr,udp_host)) {
6398 servers[i]->key = atoi(params);
6399 log(DEBUG,"Key for this server is now %d",servers[i]->key);
6400 DoSync(serv,udp_host,udp_port,MyKey);
6405 WriteOpers("\2WARNING!\2 Server ip %s attempted a key exchange, but is not in the authentication state! Possible intrusion attempt!",udp_host);
6409 // S test.chatspike.net password :ChatSpike InspIRCd test server
6410 char* servername = strtok(params," ");
6411 char* password = strtok(NULL," ");
6412 char* serverdesc = finalparam+2;
6414 // TODO: we should do a check here to ensure that this server is one we recently initiated a
6415 // link with, and didnt hear an 's' or 'E' back from yet (these are the only two valid responses
6416 // to an 'S' command. If we didn't recently send an 'S' to this server, theyre trying to spoof
6417 // a connect, so put out an oper alert!
6422 // for now, just accept all, we'll fix that later.
6423 WriteOpers("%s accepted our link credentials ",servername);
6425 char Link_ServerName[1024];
6426 char Link_IPAddr[1024];
6427 char Link_Port[1024];
6428 char Link_Pass[1024];
6429 char Link_SendPass[1024];
6432 // search for a corresponding <link> block in the config files
6433 for (int i = 0; i < ConfValueEnum("link",&config_f); i++)
6435 ConfValue("link","name",i,Link_ServerName,&config_f);
6436 ConfValue("link","ipaddr",i,Link_IPAddr,&config_f);
6437 ConfValue("link","port",i,Link_Port,&config_f);
6438 ConfValue("link","recvpass",i,Link_Pass,&config_f);
6439 ConfValue("link","sendpass",i,Link_SendPass,&config_f);
6440 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
6441 LinkPort = atoi(Link_Port);
6442 if (!strcasecmp(Link_ServerName,servername)) {
6443 if (!strcasecmp(Link_IPAddr,udp_host)) {
6444 if (LinkPort == udp_port) {
6445 // matching link at this end too, we're all done!
6446 // at this point we must begin key exchange and insert this
6447 // server into our 'active' table.
6448 for (int j = 0; j < 255; j++) {
6449 if (servers[j] != NULL) {
6450 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
6451 strcpy(servers[j]->description,serverdesc);
6452 WriteOpers("Server %s authenticated, exchanging server keys...",servername);
6453 snprintf(response,10240,"O %d",MyKey);
6454 serv->SendPacket(response,udp_host,udp_port,0);
6459 WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right noe! Possible intrusion attempt!",udp_host);
6464 log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
6468 log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
6472 log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
6475 serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
6476 WriteOpers("CONNECT from %s denied, no matching link block",servername);
6481 char* error_message = finalparam+2;
6482 WriteOpers("ERROR from %s: %s",udp_host,error_message);
6483 // remove this server from any lists
6484 for (int j = 0; j < 255; j++) {
6485 if (servers[j] != NULL) {
6486 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
6496 serverrec* source_server = NULL;
6498 for (int j = 0; j < 255; j++) {
6499 if (servers[j] != NULL) {
6500 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
6501 if (servers[j]->key == theirkey) {
6502 // found a valid key for this server, can process restricted stuff here
6503 process_restricted_commands(token,params,servers[j],serv,udp_host,udp_port);
6510 log(DEBUG,"Unrecognised token or unauthenticated host in datagram from %s:%d: %c",udp_host,udp_port,token);
6514 int reap_counter = 0;
6518 struct sockaddr_in client, server;
6519 char addrs[MAXBUF][255];
6520 int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
6522 int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
6523 int selectResult = 0, selectResult2 = 0;
6524 char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
6525 char resolvedHost[MAXBUF];
6529 log_file = fopen("ircd.log","a+");
6532 printf("ERROR: Could not write to logfile ircd.log, bailing!\n\n");
6536 log(DEBUG,"InspIRCd: startup: begin");
6540 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
6542 log(DEBUG,"InspIRCd: startup: not starting with UID 0!");
6544 SetupCommandTable();
6545 log(DEBUG,"InspIRCd: startup: default command table set up");
6548 if (strcmp(DieValue,""))
6550 printf("WARNING: %s\n\n",DieValue);
6553 log(DEBUG,"InspIRCd: startup: read config");
6555 int count2 = 0, count3 = 0;
6557 for (count = 0; count < ConfValueEnum("bind",&config_f); count++)
6559 ConfValue("bind","port",count,configToken,&config_f);
6560 ConfValue("bind","address",count,Addr,&config_f);
6561 ConfValue("bind","type",count,Type,&config_f);
6562 if (!strcmp(Type,"servers"))
6564 char Default[MAXBUF];
6565 strcpy(Default,"no");
6566 ConfValue("bind","default",count,Default,&config_f);
6567 if (strchr(Default,'y'))
6569 defaultRoute = count3;
6570 log(DEBUG,"InspIRCd: startup: binding '%s:%s' is default server route",Addr,configToken);
6572 me[count3] = new serverrec(ServerName,100L,false);
6573 me[count3]->CreateListener(Addr,atoi(configToken));
6578 ports[count2] = atoi(configToken);
6579 strcpy(addrs[count2],Addr);
6582 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
6585 UDPportCount = count3;
6587 log(DEBUG,"InspIRCd: startup: read %d total client ports and %d total server ports",portCount,UDPportCount);
6589 log(DEBUG,"InspIRCd: startup: InspIRCd is now running!");
6593 /* BugFix By Craig! :p */
6595 for (count2 = 0; count2 < ConfValueEnum("module",&config_f); count2++)
6597 char modfile[MAXBUF];
6598 ConfValue("module","name",count2,configToken,&config_f);
6599 sprintf(modfile,"%s/%s",MOD_PATH,configToken,&config_f);
6600 printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
6601 log(DEBUG,"InspIRCd: startup: Loading module: %s",modfile);
6602 /* If The File Doesnt exist, Trying to load it
6603 * Will Segfault the IRCd.. So, check to see if
6604 * it Exists, Before Proceeding. */
6605 if (FileExists(modfile))
6607 factory[count] = new ircd_module(modfile);
6608 if (factory[count]->LastError())
6610 log(DEBUG,"Unable to load %s: %s",modfile,factory[count]->LastError());
6611 sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
6614 if (factory[count]->factory)
6616 modules[count] = factory[count]->factory->CreateModule();
6617 /* save the module and the module's classfactory, if
6618 * this isnt done, random crashes can occur :/ */
6619 module_names.push_back(modfile);
6623 log(DEBUG,"Unable to load %s",modfile);
6624 sprintf("Unable to load %s\nExiting...\n",modfile);
6627 /* Increase the Count */
6632 log(DEBUG,"InspIRCd: startup: Module Not Found %s",modfile);
6633 printf("Module Not Found: \033[1;37m%s\033[0;37m, Skipping\n",modfile);
6636 MODCOUNT = count - 1;
6637 log(DEBUG,"Total loaded modules: %d",MODCOUNT+1);
6639 printf("\nInspIRCd is now running!\n");
6641 startup_time = time(NULL);
6645 log(VERBOSE,"Not forking as -nofork was specified");
6649 if (DaemonSeed() == ERROR)
6651 log(DEBUG,"InspIRCd: startup: can't daemonise");
6652 printf("ERROR: could not go into daemon mode. Shutting down.\n");
6658 /* setup select call */
6659 FD_ZERO(&selectFds);
6660 log(DEBUG,"InspIRCd: startup: zero selects");
6661 log(VERBOSE,"InspIRCd: startup: portCount = %d", portCount);
6663 for (count = 0; count < portCount; count++)
6665 if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
6667 log(DEBUG,"InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
6670 if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
6672 log(DEBUG,"InspIRCd: startup: failed to bind port %d",ports[count]);
6674 else /* well we at least bound to one socket so we'll continue */
6680 log(DEBUG,"InspIRCd: startup: total bound ports %d",boundPortCount);
6682 /* if we didn't bind to anything then abort */
6683 if (boundPortCount == 0)
6685 log(DEBUG,"InspIRCd: startup: no ports bound, bailing!");
6689 length = sizeof (client);
6690 int flip_flop = 0, udp_port = 0;
6691 char udp_msg[MAXBUF], udp_host[MAXBUF];
6693 /* main loop, this never returns */
6698 struct timeval tval;
6701 user_hash::iterator count2 = clientlist.begin();
6703 // *FIX* Instead of closing sockets in kill_link when they receive the ERROR :blah line, we should queue
6704 // them in a list, then reap the list every second or so.
6705 if (reap_counter>5000)
6707 if (fd_reap.size() > 0)
6709 for( int n = 0; n < fd_reap.size(); n++)
6711 Blocking(fd_reap[n]);
6713 NonBlocking(fd_reap[n]);
6721 for (int x = 0; x != UDPportCount; x++)
6724 if (me[x]->RecvPacket(udp_msg, udp_host, udp_port, theirkey))
6726 if (strlen(udp_msg)<1) {
6727 log(DEBUG,"Invalid datagram from %s:%d:%d [route%d]",udp_host,udp_port,me[x]->port,x);
6731 FOREACH_MOD OnPacketReceive(udp_msg);
6732 // Packets must go back via the route they arrived on :)
6733 handle_link_packet(theirkey, udp_msg, udp_host, udp_port, me[x]);
6734 // link packets can manipulate the usertable so beware of
6735 // any loops here watching the user or channels hash
6736 log(DEBUG,"Sync: exit 3");
6743 while (count2 != clientlist.end())
6746 tval.tv_usec = tval.tv_sec = 0;
6748 int total_in_this_set = 0;
6750 user_hash::iterator xcount = count2;
6751 user_hash::iterator endingiter = count2;
6753 if (!count2->second) break;
6756 if (count2->second->fd != 0)
6758 // assemble up to 64 sockets into an fd_set
6759 // to implement a pooling mechanism.
6761 // This should be up to 64x faster than the
6762 // old implementation.
6763 while (total_in_this_set < 64)
6765 if (count2 != clientlist.end())
6767 // we don't check the state of remote users.
6768 if (count2->second->fd > 0)
6770 FD_SET (count2->second->fd, &sfd);
6772 // registration timeout -- didnt send USER/NICK/HOST in the time specified in
6773 // their connection class.
6774 if ((time(NULL) > count2->second->timeout) && (count2->second->registered != 7))
6776 log(DEBUG,"InspIRCd: registration timeout: %s",count2->second->nick);
6777 kill_link(count2->second,"Registration timeout");
6780 if (((time(NULL)) > count2->second->nping) && (isnick(count2->second->nick)) && (count2->second->registered == 7))
6782 if ((!count2->second->lastping) && (count2->second->registered == 7))
6784 log(DEBUG,"InspIRCd: ping timeout: %s",count2->second->nick);
6785 kill_link(count2->second,"Ping timeout");
6788 Write(count2->second->fd,"PING :%s",ServerName);
6789 log(DEBUG,"InspIRCd: pinging: %s",count2->second->nick);
6790 count2->second->lastping = 0;
6791 count2->second->nping = time(NULL)+120;
6795 total_in_this_set++;
6800 endingiter = count2;
6801 count2 = xcount; // roll back to where we were
6807 selectResult2 = select(65535, &sfd, NULL, NULL, &tval);
6809 // now loop through all of the items in this pool if any are waiting
6810 //if (selectResult2 > 0)
6811 for (user_hash::iterator count2a = xcount; count2a != endingiter; count2a++)
6814 if ((count2a->second->fd != -1) && (FD_ISSET (count2a->second->fd, &sfd)))
6816 log(DEBUG,"Reading fd %d",count2a->second->fd);
6817 memset(data, 0, 10240);
6818 result = read(count2a->second->fd, data, 10240);
6822 log(DEBUG,"Read %d characters from socket",result);
6823 userrec* current = count2a->second;
6824 int currfd = current->fd;
6825 char* l = strtok(data,"\n");
6830 if ((floodlines > current->flood) && (current->flood != 0))
6832 log(DEFAULT,"Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
6833 WriteOpers("*** Excess flood from: %s!%s@%s",current->nick,current->ident,current->host);
6834 kill_link(current,"Excess flood");
6837 char sanitized[NetBufferSize];
6838 memset(sanitized, 0, NetBufferSize);
6840 for (int pt = 0; pt < strlen(l); pt++)
6844 sanitized[ptt++] = l[pt];
6847 sanitized[ptt] = '\0';
6848 if (strlen(sanitized))
6852 // we're gonna re-scan to check if the nick is gone, after every
6853 // command - if it has, we're gonna bail
6854 bool find_again = false;
6855 process_buffer(sanitized,current);
6857 // look for the user's record in case it's changed
6858 for (user_hash::iterator c2 = clientlist.begin(); c2 != clientlist.end(); c2++)
6860 if (c2->second->fd == currfd)
6862 // found again, update pointer
6863 current == c2->second;
6872 l = strtok(NULL,"\n");
6877 if ((result == -1) && (errno != EAGAIN) && (errno != EINTR))
6879 log(DEBUG,"killing: %s",count2a->second->nick);
6880 kill_link(count2a->second,strerror(errno));
6884 // result EAGAIN means nothing read
6885 if (result == EAGAIN)
6893 log(DEBUG,"InspIRCd: Exited: %s",count2a->second->nick);
6894 kill_link(count2a->second,"Client exited");
6895 // must bail here? kill_link removes the hash, corrupting the iterator
6896 log(DEBUG,"Bailing from client exit");
6900 else if (result > 0)
6905 for (int q = 0; q < total_in_this_set; q++)
6907 // there is no iterator += operator :(
6908 //if (count2 != clientlist.end())
6915 // set up select call
6916 for (count = 0; count < boundPortCount; count++)
6918 FD_SET (openSockfd[count], &selectFds);
6921 /* added timeout! select was waiting forever... wank... :/ */
6933 selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
6935 /* select is reporting a waiting socket. Poll them all to find out which */
6936 if (selectResult > 0)
6938 char target[MAXBUF], resolved[MAXBUF];
6939 for (count = 0; count < boundPortCount; count++)
6941 if (FD_ISSET (openSockfd[count], &selectFds))
6943 incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
6945 address_cache::iterator iter = IP.find(client.sin_addr);
6946 bool iscached = false;
6947 if (iter == IP.end())
6949 /* ip isn't in cache, add it */
6950 strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
6951 if(CleanAndResolve(resolved, target) != TRUE)
6953 strncpy(resolved,target,MAXBUF);
6955 /* hostname now in 'target' */
6956 IP[client.sin_addr] = new string(resolved);
6957 /* hostname in cache */
6961 /* found ip (cached) */
6962 strncpy(resolved, iter->second->c_str(), MAXBUF);
6966 if (incomingSockfd < 0)
6968 WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
6969 log(DEBUG,"InspIRCd: accept failed: %d",ports[count]);
6973 AddClient(incomingSockfd, resolved, ports[count], iscached);
6974 log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
6981 if(0) {}; // "Label must be followed by a statement"... so i gave it one.
6984 close (incomingSockfd);