1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2003 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! ;) */
20 #include "inspircd_io.h"
21 #include "inspircd_util.h"
22 #include "inspircd_config.h"
25 #include <sys/errno.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
32 #include <ext/hash_map>
41 #include "connection.h"
53 #define nspace __gnu_cxx
58 int LogLevel = DEFAULT;
59 char ServerName[MAXBUF];
61 char ServerDesc[MAXBUF];
62 char AdminName[MAXBUF];
63 char AdminEmail[MAXBUF];
64 char AdminNick[MAXBUF];
66 char restartpass[MAXBUF];
70 char PrefixQuit[MAXBUF];
71 char DieValue[MAXBUF];
73 int WHOWAS_STALE = 48; // default WHOWAS Entries last 2 days before they go 'stale'
74 int WHOWAS_MAX = 100; // default 100 people maximum in the WHOWAS list
76 time_t startup_time = time(NULL);
78 extern vector<Module*> modules;
79 extern vector<ircd_module*> factory;
88 template<> struct nspace::hash<in_addr>
90 size_t operator()(const struct in_addr &a) const
93 memcpy(&q,&a,sizeof(size_t));
98 template<> struct nspace::hash<string>
100 size_t operator()(const string &s) const
103 static struct hash<const char *> strhash;
115 bool operator()(const string& s1, const string& s2) const
117 char a[MAXBUF],b[MAXBUF];
118 strcpy(a,s1.c_str());
119 strcpy(b,s2.c_str());
120 return (strcasecmp(a,b) == 0);
125 struct InAddr_HashComp
128 bool operator()(const in_addr &s1, const in_addr &s2) const
133 memcpy(&q,&s1,sizeof(size_t));
134 memcpy(&p,&s2,sizeof(size_t));
142 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
143 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
144 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
145 typedef std::deque<command_t> command_table;
148 serverrec* servers[255];
150 user_hash clientlist;
153 command_table cmdlist;
160 struct linger linger = { 0 };
161 char bannerBuffer[MAXBUF];
162 int boundPortCount = 0;
163 int portCount = 0, UDPportCount = 0, ports[MAXSOCKS];
164 int defaultRoute = 0;
168 long MyKey = C.GenKey();
172 int has_channel(userrec *u, chanrec *c);
173 int usercount(chanrec *c);
174 int usercount_i(chanrec *c);
175 void update_stats_l(int fd,int data_out);
176 char* Passwd(userrec *user);
177 bool IsDenied(userrec *user);
178 void AddWhoWas(userrec* u);
181 void safedelete(userrec *p)
185 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
186 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
191 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
195 void safedelete(chanrec *p)
200 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
204 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
209 void tidystring(char* str)
211 // strips out double spaces before a : parameter
214 bool go_again = true;
221 while ((str[0] == ' ') && (strlen(str)>0))
228 bool noparse = false;
231 while (a < strlen(str))
233 if ((a<strlen(str)-1) && (noparse==false))
235 if ((str[a] == ' ') && (str[a+1] == ' '))
237 log(DEBUG,"Tidied extra space out of string: %s",str);
245 if ((str[a] == ' ') && (str[a+1] == ':'))
251 temp[t++] = str[a++];
254 strncpy(str,temp,MAXBUF);
258 /* chop a string down to 512 characters and preserve linefeed (irc max
265 FOREACH_MOD OnServerRaw(temp,false);
266 const char* str2 = temp.c_str();
267 sprintf(str,"%s",str2);
270 if (strlen(str) >= 512)
279 std::string getservername()
284 std::string getserverdesc()
289 std::string getnetworkname()
294 std::string getadminname()
299 std::string getadminemail()
304 std::string getadminnick()
309 void log(int level,char *text, ...)
311 char textbuffer[MAXBUF];
315 struct tm * timeinfo;
316 if (level < LogLevel)
320 timeinfo = localtime (&rawtime);
322 f = fopen("ircd.log","a+");
326 va_start (argsPtr, text);
327 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
329 strcpy(b,asctime(timeinfo));
330 b[strlen(b)-1] = ':';
331 fprintf(f,"%s %s\n",b,textbuffer);
335 // nofork enabled? display it on terminal too
336 printf("%s %s\n",b,textbuffer);
341 printf("Can't write log file, bailing!!!");
346 void readfile(file_cache &F, const char* fname)
349 char linebuf[MAXBUF];
351 log(DEBUG,"readfile: loading %s",fname);
353 file = fopen(fname,"r");
358 fgets(linebuf,sizeof(linebuf),file);
359 linebuf[strlen(linebuf)-1]='\0';
360 if (!strcmp(linebuf,""))
366 F.push_back(linebuf);
373 log(DEBUG,"readfile: failed to load file: %s",fname);
375 log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
378 void ReadConfig(void)
380 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF];
383 ConfValue("server","name",0,ServerName);
384 ConfValue("server","description",0,ServerDesc);
385 ConfValue("server","network",0,Network);
386 ConfValue("admin","name",0,AdminName);
387 ConfValue("admin","email",0,AdminEmail);
388 ConfValue("admin","nick",0,AdminNick);
389 ConfValue("files","motd",0,motd);
390 ConfValue("files","rules",0,rules);
391 ConfValue("power","diepass",0,diepass);
392 ConfValue("power","pause",0,pauseval);
393 ConfValue("power","restartpass",0,restartpass);
394 ConfValue("options","prefixquit",0,PrefixQuit);
395 ConfValue("die","value",0,DieValue);
396 ConfValue("options","loglevel",0,dbg);
397 if (!strcmp(dbg,"debug"))
399 if (!strcmp(dbg,"verbose"))
401 if (!strcmp(dbg,"default"))
403 if (!strcmp(dbg,"sparse"))
405 if (!strcmp(dbg,"none"))
407 readfile(RULES,rules);
408 log(DEBUG,"Reading connect classes");
410 for (int i = 0; i < ConfValueEnum("connect"); i++)
413 ConfValue("connect","allow",i,Value);
414 if (strcmp(Value,""))
416 strcpy(c.host,Value);
419 ConfValue("connect","password",i,Value);
420 strcpy(c.pass,Value);
421 Classes.push_back(c);
422 log(DEBUG,"Read connect class type ALLOW, host=%s password=%s",c.host,c.pass);
426 ConfValue("connect","deny",i,Value);
427 strcpy(c.host,Value);
429 Classes.push_back(c);
430 log(DEBUG,"Read connect class type DENY, host=%s",c.host);
439 log(DEBUG,"Blocking: %d",s);
440 flags = fcntl(s, F_GETFL, 0);
441 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
444 void NonBlocking(int s)
447 log(DEBUG,"NonBlocking: %d",s);
448 flags = fcntl(s, F_GETFL, 0);
449 fcntl(s, F_SETFL, flags | O_NONBLOCK);
453 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
455 struct hostent *hostPtr = NULL;
458 memset (resolvedHost, '\0',MAXBUF);
459 if(unresolvedHost == NULL)
461 if ((inet_aton(unresolvedHost,&addr)) == 0)
463 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
465 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
467 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
471 /* write formatted text to a socket, in same format as printf */
473 void Write(int sock,char *text, ...)
477 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
480 char textbuffer[MAXBUF];
484 va_start (argsPtr, text);
485 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
487 sprintf(tb,"%s\r\n",textbuffer);
489 write(sock,tb,strlen(tb));
490 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
493 /* write a server formatted numeric response to a single socket */
495 void WriteServ(int sock, char* text, ...)
499 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
502 char textbuffer[MAXBUF],tb[MAXBUF];
504 va_start (argsPtr, text);
506 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
508 sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
510 write(sock,tb,strlen(tb));
511 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
514 /* write text from an originating user to originating user */
516 void WriteFrom(int sock, userrec *user,char* text, ...)
518 if ((!text) || (!user))
520 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
523 char textbuffer[MAXBUF],tb[MAXBUF];
525 va_start (argsPtr, text);
527 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
529 sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
531 write(sock,tb,strlen(tb));
532 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
535 /* write text to an destination user from a source user (e.g. user privmsg) */
537 void WriteTo(userrec *source, userrec *dest,char *data, ...)
539 if ((!source) || (!dest) || (!data))
541 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
544 char textbuffer[MAXBUF],tb[MAXBUF];
546 va_start (argsPtr, data);
547 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
550 WriteFrom(dest->fd,source,"%s",textbuffer);
553 /* write formatted text from a source user to all users on a channel
554 * including the sender (NOT for privmsg, notice etc!) */
556 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
558 if ((!Ptr) || (!user) || (!text))
560 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
563 char textbuffer[MAXBUF];
565 va_start (argsPtr, text);
566 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
568 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
570 if (has_channel(i->second,Ptr))
572 WriteTo(user,i->second,"%s",textbuffer);
577 void WriteChannelWithServ(char* ServerName, chanrec* Ptr, userrec* user, char* text, ...)
579 if ((!Ptr) || (!user) || (!text))
581 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
584 char textbuffer[MAXBUF];
586 va_start (argsPtr, text);
587 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
589 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
593 if (has_channel(i->second,Ptr))
595 WriteServ(i->second->fd,"%s",textbuffer);
602 /* write formatted text from a source user to all users on a channel except
603 * for the sender (for privmsg etc) */
605 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
607 if ((!Ptr) || (!user) || (!text))
609 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
612 char textbuffer[MAXBUF];
614 va_start (argsPtr, text);
615 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
618 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
622 if (has_channel(i->second,Ptr) && (user != i->second))
624 WriteTo(user,i->second,"%s",textbuffer);
630 int c_count(userrec* u)
633 for (int i =0; i != MAXCHANS; i++)
634 if (u->chans[i].channel != NULL)
640 /* return 0 or 1 depending if users u and u2 share one or more common channels
641 * (used by QUIT, NICK etc which arent channel specific notices) */
643 int common_channels(userrec *u, userrec *u2)
650 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
653 for (i = 0; i != MAXCHANS; i++)
655 for (z = 0; z != MAXCHANS; z++)
657 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
659 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
661 if ((c_count(u)) && (c_count(u2)))
672 /* write a formatted string to all users who share at least one common
673 * channel, including the source user e.g. for use in NICK */
675 void WriteCommon(userrec *u, char* text, ...)
679 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
683 if (u->registered != 7) {
684 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
688 char textbuffer[MAXBUF];
690 va_start (argsPtr, text);
691 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
694 WriteFrom(u->fd,u,"%s",textbuffer);
696 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
700 if (common_channels(u,i->second) && (i->second != u))
702 WriteFrom(i->second->fd,u,"%s",textbuffer);
708 /* write a formatted string to all users who share at least one common
709 * channel, NOT including the source user e.g. for use in QUIT */
711 void WriteCommonExcept(userrec *u, char* text, ...)
715 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
719 if (u->registered != 7) {
720 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
724 char textbuffer[MAXBUF];
726 va_start (argsPtr, text);
727 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
730 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
734 if ((common_channels(u,i->second)) && (u != i->second))
736 WriteFrom(i->second->fd,u,"%s",textbuffer);
742 void WriteOpers(char* text, ...)
746 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
750 char textbuffer[MAXBUF];
752 va_start (argsPtr, text);
753 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
756 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
760 if (strchr(i->second->modes,'o'))
762 if (strchr(i->second->modes,'s'))
764 // send server notices to all with +s
765 // (TODO: needs SNOMASKs)
766 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
773 void WriteWallOps(userrec *source, char* text, ...)
775 if ((!text) || (!source))
777 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
782 char textbuffer[MAXBUF];
784 va_start (argsPtr, text);
785 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
788 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
792 if (strchr(i->second->modes,'w'))
794 WriteTo(source,i->second,"WALLOPS %s",textbuffer);
800 /* convert a string to lowercase. Note following special circumstances
801 * taken from RFC 1459. Many "official" server branches still hold to this
802 * rule so i will too;
804 * Because of IRC's scandanavian origin, the characters {}| are
805 * considered to be the lower case equivalents of the characters []\,
806 * respectively. This is a critical issue when determining the
807 * equivalence of two nicknames.
810 void strlower(char *n)
816 for (int i = 0; i != strlen(n); i++)
818 n[i] = tolower(n[i]);
828 /* verify that a user's ident and nickname is valid */
830 int isident(const char* n)
843 for (i = 0; i != strlen(n); i++)
845 if ((n[i] < 33) || (n[i] > 125))
849 /* can't occur ANYWHERE in an Ident! */
850 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
859 int isnick(const char* n)
871 if (strlen(n) > NICKMAX-1)
875 for (i = 0; i != strlen(n); i++)
877 if ((n[i] < 33) || (n[i] > 125))
881 /* can't occur ANYWHERE in a nickname! */
882 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
886 /* can't occur as the first char of a nickname... */
887 if ((strchr("0123456789",n[i])) && (!i))
895 /* Find a user record by nickname and return a pointer to it */
897 userrec* Find(string nick)
899 user_hash::iterator iter = clientlist.find(nick);
901 if (iter == clientlist.end())
902 /* Couldn't find it */
908 void update_stats_l(int fd,int data_out) /* add one line-out to stats L for this fd */
910 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
914 if (i->second->fd == fd)
916 i->second->bytes_out+=data_out;
917 i->second->cmds_out++;
924 /* find a channel record by channel name and return a pointer to it */
926 chanrec* FindChan(const char* chan)
930 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
934 chan_hash::iterator iter = chanlist.find(chan);
936 if (iter == chanlist.end())
937 /* Couldn't find it */
944 void purge_empty_chans(void)
946 int go_again = 1, purge = 0;
951 for (chan_hash::iterator i = chanlist.begin(); i != chanlist.end(); i++)
954 if (!usercount(i->second))
956 /* kill the record */
957 if (i != chanlist.end())
959 log(DEBUG,"del_channel: destroyed: %s",i->second->name);
969 log(DEBUG,"skipped purge for %s",i->second->name);
974 log(DEBUG,"completed channel purge, killed %d",purge);
977 /* returns the status character for a given user on a channel, e.g. @ for op,
978 * % for halfop etc. If the user has several modes set, the highest mode
979 * the user has must be returned. */
981 char* cmode(userrec *user, chanrec *chan)
983 if ((!user) || (!chan))
985 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
990 for (i = 0; i != MAXCHANS; i++)
992 if ((user->chans[i].channel == chan) && (chan != NULL))
994 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
998 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
1002 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
1011 char scratch[MAXMODES];
1012 char sparam[MAXMODES];
1014 char* chanmodes(chanrec *chan)
1018 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1025 if (chan->noexternal)
1027 strcat(scratch,"n");
1029 if (chan->topiclock)
1031 strcat(scratch,"t");
1033 if (strcmp(chan->key,""))
1035 strcat(scratch,"k");
1039 strcat(scratch,"l");
1041 if (chan->inviteonly)
1043 strcat(scratch,"i");
1045 if (chan->moderated)
1047 strcat(scratch,"m");
1051 strcat(scratch,"s");
1053 if (chan->c_private)
1055 strcat(scratch,"p");
1057 if (strcmp(chan->key,""))
1059 strcat(sparam,chan->key);
1064 sprintf(foo," %d",chan->limit);
1067 if (strlen(chan->custom_modes))
1069 // TODO: Tack on mode parameters here -
1070 // IN ORDER OF CUSTOM_MODES!
1071 strncat(scratch,chan->custom_modes,MAXMODES);
1073 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
1074 strncat(scratch,sparam,MAXMODES);
1078 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
1079 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
1080 * highest mode the user has must be returned. */
1082 int cstatus(userrec *user, chanrec *chan)
1084 if ((!chan) || (!user))
1086 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
1091 for (i = 0; i != MAXCHANS; i++)
1093 if ((user->chans[i].channel == chan) && (chan != NULL))
1095 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
1099 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
1103 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
1105 return STATUS_VOICE;
1107 return STATUS_NORMAL;
1113 /* compile a userlist of a channel into a string, each nick seperated by
1114 * spaces and op, voice etc status shown as @ and + */
1116 void userlist(userrec *user,chanrec *c)
1118 if ((!c) || (!user))
1120 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1124 sprintf(list,"353 %s = %s :", user->nick, c->name);
1125 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1127 if (has_channel(i->second,c))
1129 if (isnick(i->second->nick))
1131 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1133 /* user is +i, and source not on the channel, does not show
1134 * nick in NAMES list */
1137 strcat(list,cmode(i->second,c));
1138 strcat(list,i->second->nick);
1140 if (strlen(list)>(480-NICKMAX))
1142 /* list overflowed into
1143 * multiple numerics */
1144 WriteServ(user->fd,list);
1145 sprintf(list,"353 %s = %s :", user->nick, c->name);
1150 /* if whats left in the list isnt empty, send it */
1151 if (list[strlen(list)-1] != ':')
1153 WriteServ(user->fd,list);
1157 /* return a count of the users on a specific channel accounting for
1158 * invisible users who won't increase the count. e.g. for /LIST */
1160 int usercount_i(chanrec *c)
1167 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1172 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1176 if (has_channel(i->second,c))
1178 if (isnick(i->second->nick))
1180 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1182 /* user is +i, and source not on the channel, does not show
1183 * nick in NAMES list */
1191 log(DEBUG,"usercount_i: %s %d",c->name,count);
1196 int usercount(chanrec *c)
1203 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
1208 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1212 if (has_channel(i->second,c))
1214 if ((isnick(i->second->nick)) && (i->second->registered == 7))
1221 log(DEBUG,"usercount: %s %d",c->name,count);
1226 /* add a channel to a user, creating the record for it if needed and linking
1227 * it to the user record */
1229 chanrec* add_channel(userrec *user, char* cname, char* key)
1232 if ((!user) || (!cname))
1234 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
1242 // we MUST declare this wherever we use FOREACH_RESULT
1245 if ((!cname) || (!user))
1249 if (strlen(cname) > CHANMAX-1)
1251 cname[CHANMAX-1] = '\0';
1254 log(DEBUG,"add_channel: %s %s",user->nick,cname);
1256 if ((has_channel(user,FindChan(cname))) && (FindChan(cname)))
1258 return NULL; // already on the channel!
1262 if (!FindChan(cname))
1264 FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
1269 /* create a new one */
1270 log(DEBUG,"add_channel: creating: %s",cname);
1272 chanlist[cname] = new chanrec();
1274 strcpy(chanlist[cname]->name, cname);
1275 chanlist[cname]->topiclock = 1;
1276 chanlist[cname]->noexternal = 1;
1277 chanlist[cname]->created = time(NULL);
1278 strcpy(chanlist[cname]->topic, "");
1279 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
1280 chanlist[cname]->topicset = 0;
1281 Ptr = chanlist[cname];
1282 log(DEBUG,"add_channel: created: %s",cname);
1283 /* set created to 2 to indicate user
1284 * is the first in the channel
1285 * and should be given ops */
1291 /* channel exists, just fish out a pointer to its struct */
1292 Ptr = FindChan(cname);
1295 FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
1300 log(DEBUG,"add_channel: joining to: %s",Ptr->name);
1301 if (strcmp(Ptr->key,""))
1303 log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
1306 log(DEBUG,"add_channel: no key given in JOIN");
1307 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
1312 log(DEBUG,"key at %p is %s",key,key);
1313 if (strcasecmp(key,Ptr->key))
1315 log(DEBUG,"add_channel: bad key given in JOIN");
1316 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
1321 log(DEBUG,"add_channel: no key");
1323 if (Ptr->inviteonly)
1325 log(DEBUG,"add_channel: channel is +i");
1326 if (user->IsInvited(Ptr->name))
1328 /* user was invited to channel */
1329 /* there may be an optional channel NOTICE here */
1333 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
1337 log(DEBUG,"add_channel: channel is not +i");
1341 if (usercount(Ptr) == Ptr->limit)
1343 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
1348 log(DEBUG,"add_channel: about to walk banlist");
1350 /* check user against the channel banlist */
1351 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1353 if (match(user->GetFullHost(),i->data))
1355 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
1360 log(DEBUG,"add_channel: bans checked");
1362 user->RemoveInvite(Ptr->name);
1364 log(DEBUG,"add_channel: invites removed");
1370 log(DEBUG,"Passed channel checks");
1372 for (i =0; i != MAXCHANS; i++)
1374 if (user->chans[i].channel == NULL)
1376 log(DEBUG,"Adding into their channel list");
1380 /* first user in is given ops */
1381 user->chans[i].uc_modes = UCMODE_OP;
1385 user->chans[i].uc_modes = 0;
1387 user->chans[i].channel = Ptr;
1388 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
1390 log(DEBUG,"Sent JOIN to client");
1394 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
1395 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
1398 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
1399 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name,chanmodes(Ptr));
1400 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1401 FOREACH_MOD OnUserJoin(user,Ptr);
1405 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
1406 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
1410 /* remove a channel from a users record, and remove the record from memory
1411 * if the channel has become empty */
1413 chanrec* del_channel(userrec *user, char* cname, char* reason)
1415 if ((!user) || (!cname))
1417 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
1425 if ((!cname) || (!user))
1430 Ptr = FindChan(cname);
1437 FOREACH_MOD OnUserPart(user,Ptr);
1438 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
1440 for (i =0; i != MAXCHANS; i++)
1442 /* zap it from the channel list of the user */
1443 if (user->chans[i].channel == Ptr)
1447 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
1451 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
1453 user->chans[i].uc_modes = 0;
1454 user->chans[i].channel = NULL;
1455 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1460 /* if there are no users left on the channel */
1461 if (!usercount(Ptr))
1463 chan_hash::iterator iter = chanlist.find(Ptr->name);
1465 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1467 /* kill the record */
1468 if (iter != chanlist.end())
1470 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1471 delete iter->second;
1472 chanlist.erase(iter);
1478 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
1480 if ((!src) || (!user) || (!Ptr) || (!reason))
1482 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
1489 if ((!Ptr) || (!user) || (!src))
1494 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
1496 if (!has_channel(user,Ptr))
1498 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
1501 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
1503 if (cstatus(src,Ptr) == STATUS_HOP)
1505 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
1509 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
1515 for (i =0; i != MAXCHANS; i++)
1517 /* zap it from the channel list of the user */
1518 if (user->chans[i].channel == Ptr)
1520 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
1521 user->chans[i].uc_modes = 0;
1522 user->chans[i].channel = NULL;
1523 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1528 /* if there are no users left on the channel */
1529 if (!usercount(Ptr))
1531 chan_hash::iterator iter = chanlist.find(Ptr->name);
1533 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1535 /* kill the record */
1536 if (iter != chanlist.end())
1538 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1539 delete iter->second;
1540 chanlist.erase(iter);
1546 /* returns 1 if user u has channel c in their record, 0 if not */
1548 int has_channel(userrec *u, chanrec *c)
1554 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
1557 for (i =0; i != MAXCHANS; i++)
1559 if (u->chans[i].channel == c)
1567 int give_ops(userrec *user,char *dest,chanrec *chan,int status)
1572 if ((!user) || (!dest) || (!chan))
1574 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
1577 if (status != STATUS_OP)
1579 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1586 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1592 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1597 for (i = 0; i != MAXCHANS; i++)
1599 if ((d->chans[i].channel == chan) && (chan != NULL))
1601 if (d->chans[i].uc_modes & UCMODE_OP)
1603 /* mode already set on user, dont allow multiple */
1606 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
1607 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
1615 int give_hops(userrec *user,char *dest,chanrec *chan,int status)
1620 if ((!user) || (!dest) || (!chan))
1622 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
1625 if (status != STATUS_OP)
1627 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1635 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1640 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1645 for (i = 0; i != MAXCHANS; i++)
1647 if ((d->chans[i].channel == chan) && (chan != NULL))
1649 if (d->chans[i].uc_modes & UCMODE_HOP)
1651 /* mode already set on user, dont allow multiple */
1654 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
1655 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
1663 int give_voice(userrec *user,char *dest,chanrec *chan,int status)
1668 if ((!user) || (!dest) || (!chan))
1670 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
1673 if (status < STATUS_HOP)
1675 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1683 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1688 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1693 for (i = 0; i != MAXCHANS; i++)
1695 if ((d->chans[i].channel == chan) && (chan != NULL))
1697 if (d->chans[i].uc_modes & UCMODE_VOICE)
1699 /* mode already set on user, dont allow multiple */
1702 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
1703 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
1711 int take_ops(userrec *user,char *dest,chanrec *chan,int status)
1716 if ((!user) || (!dest) || (!chan))
1718 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
1721 if (status != STATUS_OP)
1723 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1731 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1736 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1741 for (i = 0; i != MAXCHANS; i++)
1743 if ((d->chans[i].channel == chan) && (chan != NULL))
1745 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
1747 /* mode already set on user, dont allow multiple */
1750 d->chans[i].uc_modes ^= UCMODE_OP;
1751 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
1759 int take_hops(userrec *user,char *dest,chanrec *chan,int status)
1764 if ((!user) || (!dest) || (!chan))
1766 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
1769 if (status != STATUS_OP)
1771 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1779 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1784 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1789 for (i = 0; i != MAXCHANS; i++)
1791 if ((d->chans[i].channel == chan) && (chan != NULL))
1793 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
1795 /* mode already set on user, dont allow multiple */
1798 d->chans[i].uc_modes ^= UCMODE_HOP;
1799 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
1807 int take_voice(userrec *user,char *dest,chanrec *chan,int status)
1812 if ((!user) || (!dest) || (!chan))
1814 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
1817 if (status < STATUS_HOP)
1819 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1827 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1832 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1837 for (i = 0; i != MAXCHANS; i++)
1839 if ((d->chans[i].channel == chan) && (chan != NULL))
1841 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
1843 /* mode already set on user, dont allow multiple */
1846 d->chans[i].uc_modes ^= UCMODE_VOICE;
1847 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
1855 void TidyBan(char *ban)
1858 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
1862 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
1866 char* pos_of_pling = strchr(temp,'!');
1867 char* pos_of_at = strchr(temp,'@');
1869 pos_of_pling[0] = '\0';
1870 pos_of_at[0] = '\0';
1874 strncpy(NICK,temp,NICKMAX);
1875 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
1876 strncpy(HOST,pos_of_at,160);
1878 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
1881 int add_ban(userrec *user,char *dest,chanrec *chan,int status)
1883 if ((!user) || (!dest) || (!chan)) {
1884 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
1889 if ((!user) || (!dest) || (!chan))
1891 if (strchr(dest,'!')==0)
1893 if (strchr(dest,'@')==0)
1895 for (int i = 0; i < strlen(dest); i++)
1898 for (int i = 0; i < strlen(dest); i++)
1902 for (int i = 0; i < strlen(dest); i++)
1908 for (int i = 0; i < strlen(dest); i++)
1913 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
1916 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1918 if (!strcasecmp(i->data,dest))
1920 // dont allow a user to set the same ban twice
1925 b.set_time = time(NULL);
1926 strncpy(b.data,dest,MAXBUF);
1927 strncpy(b.set_by,user->nick,NICKMAX);
1928 chan->bans.push_back(b);
1932 int take_ban(userrec *user,char *dest,chanrec *chan,int status)
1934 if ((!user) || (!dest) || (!chan)) {
1935 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
1939 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
1940 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1942 if (!strcasecmp(i->data,dest))
1944 chan->bans.erase(i);
1951 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode)
1953 if ((!parameters) || (!user)) {
1954 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
1959 char modelist[MAXBUF];
1960 char outlist[MAXBUF];
1961 char outstr[MAXBUF];
1962 char outpars[32][MAXBUF];
1968 bool k_set = false, l_set = false;
1975 log(DEBUG,"process_modes: start");
1977 strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
1978 /* parameters[2] onwards are parameters for
1979 * modes that require them :) */
1980 strcpy(outlist,"+");
1983 log(DEBUG,"process_modes: modelist: %s",modelist);
1985 for (ptr = 0; ptr < strlen(modelist); ptr++)
1990 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
1991 char modechar = modelist[ptr];
1992 switch (modelist[ptr])
1997 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
1999 outlist[strlen(outlist)-1] = '-';
2003 strcat(outlist,"-");
2013 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
2015 outlist[strlen(outlist)-1] = '+';
2019 strcat(outlist,"+");
2026 if ((param >= pcnt)) break;
2029 r = give_ops(user,parameters[param++],chan,status);
2033 r = take_ops(user,parameters[param++],chan,status);
2037 strcat(outlist,"o");
2038 strcpy(outpars[pc++],parameters[param-1]);
2043 if ((param >= pcnt)) break;
2046 r = give_hops(user,parameters[param++],chan,status);
2050 r = take_hops(user,parameters[param++],chan,status);
2054 strcat(outlist,"h");
2055 strcpy(outpars[pc++],parameters[param-1]);
2061 if ((param >= pcnt)) break;
2064 r = give_voice(user,parameters[param++],chan,status);
2068 r = take_voice(user,parameters[param++],chan,status);
2072 strcat(outlist,"v");
2073 strcpy(outpars[pc++],parameters[param-1]);
2078 if ((param >= pcnt)) break;
2081 r = add_ban(user,parameters[param++],chan,status);
2085 r = take_ban(user,parameters[param++],chan,status);
2089 strcat(outlist,"b");
2090 strcpy(outpars[pc++],parameters[param-1]);
2095 if ((param >= pcnt))
2103 if (!strcmp(chan->key,""))
2105 strcat(outlist,"k");
2106 strcpy(outpars[pc++],parameters[param++]);
2107 strcpy(chan->key,parameters[param-1]);
2113 /* only allow -k if correct key given */
2114 if (strcmp(chan->key,""))
2116 strcat(outlist,"k");
2117 strcpy(chan->key,"");
2127 strcat(outlist,"l");
2132 if ((param >= pcnt)) break;
2138 bool invalid = false;
2139 for (int i = 0; i < strlen(parameters[param]); i++)
2141 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
2146 if (atoi(parameters[param]) < 1)
2154 chan->limit = atoi(parameters[param]);
2157 strcat(outlist,"l");
2158 strcpy(outpars[pc++],parameters[param++]);
2165 if (chan->inviteonly != mdir)
2167 strcat(outlist,"i");
2169 chan->inviteonly = mdir;
2173 if (chan->topiclock != mdir)
2175 strcat(outlist,"t");
2177 chan->topiclock = mdir;
2181 if (chan->noexternal != mdir)
2183 strcat(outlist,"n");
2185 chan->noexternal = mdir;
2189 if (chan->moderated != mdir)
2191 strcat(outlist,"m");
2193 chan->moderated = mdir;
2197 if (chan->secret != mdir)
2199 strcat(outlist,"s");
2200 if (chan->c_private)
2202 chan->c_private = 0;
2205 strcat(outlist,"-p+");
2209 strcat(outlist,"+p-");
2213 chan->secret = mdir;
2217 if (chan->c_private != mdir)
2219 strcat(outlist,"p");
2225 strcat(outlist,"-s+");
2229 strcat(outlist,"+s-");
2233 chan->c_private = mdir;
2237 log(DEBUG,"Preprocessing custom mode %c",modechar);
2240 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
2242 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
2245 if (ModeDefined(modechar,MT_CHANNEL))
2247 log(DEBUG,"A module has claimed this mode");
2248 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
2250 p.push_back(parameters[param]);
2252 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
2254 p.push_back(parameters[param]);
2256 bool handled = false;
2257 for (int i = 0; i <= MODCOUNT; i++)
2261 if (modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p))
2263 log(DEBUG,"OnExtendedMode returned nonzero for a module");
2264 char app[] = {modechar, 0};
2267 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
2269 strcat(outlist, app);
2271 else if (!strchr(outlist,modechar))
2273 strcat(outlist, app);
2276 chan->SetCustomMode(modechar,mdir);
2277 // include parameters in output if mode has them
2278 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) || (ModeDefinedOff(modechar,MT_CHANNEL)>0))
2280 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
2281 strcpy(outpars[pc++],parameters[param++]);
2283 // break, because only one module can handle the mode.
2295 /* this ensures only the *valid* modes are sent out onto the network */
2296 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
2298 outlist[strlen(outlist)-1] = '\0';
2300 if (strcmp(outlist,""))
2302 strcpy(outstr,outlist);
2303 for (ptr = 0; ptr < pc; ptr++)
2306 strcat(outstr,outpars[ptr]);
2310 WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
2314 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
2319 void handle_mode(char **parameters, int pcnt, userrec *user)
2325 char outpars[MAXBUF];
2327 dest = Find(parameters[0]);
2329 if ((dest) && (pcnt == 1))
2331 WriteServ(user->fd,"221 %s :+%s",user->nick,user->modes);
2334 if ((dest) && (pcnt > 1))
2339 if (strchr(user->modes,'o'))
2350 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
2354 strcpy(outpars,"+");
2357 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
2360 for (i = 0; i < strlen(parameters[1]); i++)
2362 if (parameters[1][i] == '+')
2366 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2368 outpars[strlen(outpars)-1] = '+';
2372 strcat(outpars,"+");
2378 if (parameters[1][i] == '-')
2382 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2384 outpars[strlen(outpars)-1] = '-';
2388 strcat(outpars,"-");
2396 if (strchr(user->modes,'o'))
2402 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's'))
2411 if (!strchr(dest->modes,parameters[1][i]))
2413 dest->modes[strlen(dest->modes)+1]='\0';
2414 dest->modes[strlen(dest->modes)] = parameters[1][i];
2415 outpars[strlen(outpars)+1]='\0';
2416 outpars[strlen(outpars)] = parameters[1][i];
2425 outpars[strlen(outpars)+1]='\0';
2426 outpars[strlen(outpars)] = parameters[1][i];
2429 for (q = 0; q < strlen(user->modes); q++)
2431 if (user->modes[q] != parameters[1][i])
2433 moo[0] = user->modes[q];
2438 strcpy(user->modes,temp);
2443 if (strlen(outpars))
2449 while (i < strlen (outpars))
2451 b[z++] = outpars[i++];
2453 if (i<strlen(outpars)-1)
2455 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2457 // someones playing silly buggers and trying
2458 // to put a +- or -+ into the line...
2462 if (i == strlen(outpars)-1)
2464 if ((outpars[i] == '-') || (outpars[i] == '+'))
2472 if ((b[z] == '-') || (b[z] == '+'))
2475 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2478 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2483 Ptr = FindChan(parameters[0]);
2488 /* just /modes #channel */
2489 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
2490 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
2496 if ((!strcmp(parameters[1],"+b")) || (!strcmp(parameters[1],"b")))
2499 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
2501 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
2503 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
2507 if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
2509 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, Ptr->name);
2513 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false);
2517 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2524 void server_mode(char **parameters, int pcnt, userrec *user)
2530 char outpars[MAXBUF];
2532 dest = Find(parameters[0]);
2534 if ((dest) && (pcnt > 1))
2536 strcpy(outpars,"+");
2539 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
2542 for (i = 0; i < strlen(parameters[1]); i++)
2544 if (parameters[1][i] == '+')
2548 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2550 outpars[strlen(outpars)-1] = '+';
2554 strcat(outpars,"+");
2560 if (parameters[1][i] == '-')
2564 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2566 outpars[strlen(outpars)-1] = '-';
2570 strcat(outpars,"-");
2578 if (strchr(user->modes,'o'))
2584 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's'))
2593 if (!strchr(dest->modes,parameters[1][i]))
2595 dest->modes[strlen(dest->modes)+1]='\0';
2596 dest->modes[strlen(dest->modes)] = parameters[1][i];
2597 outpars[strlen(outpars)+1]='\0';
2598 outpars[strlen(outpars)] = parameters[1][i];
2607 outpars[strlen(outpars)+1]='\0';
2608 outpars[strlen(outpars)] = parameters[1][i];
2611 for (q = 0; q < strlen(user->modes); q++)
2613 if (user->modes[q] != parameters[1][i])
2615 moo[0] = user->modes[q];
2620 strcpy(user->modes,temp);
2625 if (strlen(outpars))
2631 while (i < strlen (outpars))
2633 b[z++] = outpars[i++];
2635 if (i<strlen(outpars)-1)
2637 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2639 // someones playing silly buggers and trying
2640 // to put a +- or -+ into the line...
2644 if (i == strlen(outpars)-1)
2646 if ((outpars[i] == '-') || (outpars[i] == '+'))
2654 if ((b[z] == '-') || (b[z] == '+'))
2657 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2660 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2665 Ptr = FindChan(parameters[0]);
2668 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true);
2672 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2677 /* This function pokes and hacks at a parameter list like the following:
2679 * PART #winbot, #darkgalaxy :m00!
2681 * to turn it into a series of individual calls like this:
2683 * PART #winbot :m00!
2684 * PART #darkgalaxy :m00!
2686 * The seperate calls are sent to a callback function provided by the caller
2687 * (the caller will usually call itself recursively). The callback function
2688 * must be a command handler. Calling this function on a line with no list causes
2689 * no action to be taken. You must provide a starting and ending parameter number
2690 * where the range of the list can be found, useful if you have a terminating
2691 * parameter as above which is actually not part of the list, or parameters
2692 * before the actual list as well. This code is used by many functions which
2693 * can function as "one to list" (see the RFC) */
2695 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
2700 char blog[32][MAXBUF];
2701 char blog2[32][MAXBUF];
2702 int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
2703 char keystr[MAXBUF];
2706 for (i = 0; i <32; i++)
2709 for (i = 0; i <32; i++)
2710 strcpy(blog2[i],"");
2713 for (i = 0; i <10; i++)
2717 parameters[i] = moo;
2722 if (pcnt > 1) /* we have a key to copy */
2724 strcpy(keystr,parameters[1]);
2728 if (!parameters[start])
2732 if (!strchr(parameters[start],','))
2737 for (i = start; i <= end; i++)
2741 strcat(plist,parameters[i]);
2749 for (i = 0; i < t; i++)
2751 if (plist[i] == ',')
2754 strcpy(blog[j++],param);
2758 strcpy(blog[j++],param);
2761 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
2766 if ((joins) && (keystr))
2768 if (strchr(keystr,','))
2772 t2 = strlen(keystr);
2773 for (i = 0; i < t2; i++)
2775 if (keystr[i] == ',')
2778 strcpy(blog2[j++],param);
2782 strcpy(blog2[j++],param);
2787 for (j = 0; j < total; j++)
2793 for (q = end; q < pcnt-1; q++)
2795 if (parameters[q+1])
2797 pars[q-end+1] = parameters[q+1];
2800 if ((joins) && (parameters[1]))
2811 /* repeatedly call the function with the hacked parameter list */
2812 if ((joins) && (pcnt > 1))
2816 // pars[1] already set up and containing key from blog2[j]
2821 pars[1] = parameters[1];
2827 fn(pars,pcnt-(end-start),u);
2834 void handle_join(char **parameters, int pcnt, userrec *user)
2839 if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
2841 if (parameters[0][0] == '#')
2843 Ptr = add_channel(user,parameters[0],parameters[1]);
2848 void handle_part(char **parameters, int pcnt, userrec *user)
2854 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
2856 del_channel(user,parameters[0],parameters[1]);
2860 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
2862 del_channel(user,parameters[0],NULL);
2866 void handle_kick(char **parameters, int pcnt, userrec *user)
2868 chanrec* Ptr = FindChan(parameters[0]);
2869 userrec* u = Find(parameters[1]);
2873 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2877 if (!has_channel(u,Ptr))
2879 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
2885 char reason[MAXBUF];
2886 strncpy(reason,parameters[2],MAXBUF);
2887 if (strlen(reason)>MAXKICK)
2889 reason[MAXKICK-1] = '\0';
2892 kick_channel(user,u,Ptr,reason);
2896 kick_channel(user,u,Ptr,user->nick);
2901 void handle_die(char **parameters, int pcnt, userrec *user)
2903 log(DEBUG,"die: %s",user->nick);
2904 if (!strcmp(parameters[0],diepass))
2906 WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
2912 WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
2916 void handle_restart(char **parameters, int pcnt, userrec *user)
2918 log(DEBUG,"restart: %s",user->nick);
2919 if (!strcmp(parameters[0],restartpass))
2921 WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
2924 /* Will finish this later when i can be arsed :) */
2928 WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
2933 void kill_link(userrec *user,char* reason)
2935 user_hash::iterator iter = clientlist.find(user->nick);
2937 if (strlen(reason)>MAXQUIT)
2939 reason[MAXQUIT-1] = '\0';
2942 log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
2943 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
2944 fdatasync(user->fd);
2945 log(DEBUG,"closing fd %d",user->fd);
2947 /* bugfix, cant close() a nonblocking socket (sux!) */
2948 if (user->registered == 7) {
2949 FOREACH_MOD OnUserQuit(user);
2950 WriteCommonExcept(user,"QUIT :%s",reason);
2953 /* push the socket on a stack of sockets due to be closed at the next opportunity */
2954 fd_reap.push_back(user->fd);
2956 bool do_purge = false;
2958 if (user->registered == 7) {
2959 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
2963 if (iter != clientlist.end())
2965 log(DEBUG,"deleting user hash value %d",iter->second);
2966 if ((iter->second) && (user->registered == 7)) {
2967 delete iter->second;
2969 clientlist.erase(iter);
2972 if (user->registered == 7) {
2973 purge_empty_chans();
2978 void handle_kill(char **parameters, int pcnt, userrec *user)
2980 userrec *u = Find(parameters[0]);
2981 char killreason[MAXBUF];
2983 log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
2986 WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
2987 // :Brain!brain@NetAdmin.chatspike.net KILL [Brain] :homer!NetAdmin.chatspike.net!Brain (test kill)
2988 WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
2989 sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
2990 kill_link(u,killreason);
2994 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2998 void handle_summon(char **parameters, int pcnt, userrec *user)
3000 WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
3003 void handle_users(char **parameters, int pcnt, userrec *user)
3005 WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
3009 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
3011 char* Passwd(userrec *user)
3013 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3015 if (match(user->host,i->host) && (i->type == CC_ALLOW))
3023 bool IsDenied(userrec *user)
3025 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3027 if (match(user->host,i->host) && (i->type == CC_DENY))
3036 void handle_pass(char **parameters, int pcnt, userrec *user)
3038 if (!strcasecmp(parameters[0],Passwd(user)))
3040 user->haspassed = true;
3044 void handle_invite(char **parameters, int pcnt, userrec *user)
3046 userrec* u = Find(parameters[0]);
3047 chanrec* c = FindChan(parameters[1]);
3053 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[1]);
3059 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
3068 if (cstatus(user,c) < STATUS_HOP)
3070 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, c->name);
3074 u->InviteTo(c->name);
3075 WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
3076 WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
3080 void handle_topic(char **parameters, int pcnt, userrec *user)
3086 if (strlen(parameters[0]) <= CHANMAX)
3088 Ptr = FindChan(parameters[0]);
3093 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
3094 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
3098 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
3103 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3110 if (strlen(parameters[0]) <= CHANMAX)
3112 Ptr = FindChan(parameters[0]);
3115 if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
3117 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator", user->nick, Ptr->name);
3122 strncpy(topic,parameters[1],MAXBUF);
3123 if (strlen(topic)>MAXTOPIC)
3125 topic[MAXTOPIC-1] = '\0';
3128 strcpy(Ptr->topic,topic);
3129 strcpy(Ptr->setby,user->nick);
3130 Ptr->topicset = time(NULL);
3131 WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
3135 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3141 /* sends out an error notice to all connected clients (not to be used
3144 void send_error(char *s)
3146 log(DEBUG,"send_error: %s",s);
3147 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3149 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
3153 void Error(int status)
3155 signal (SIGALRM, SIG_IGN);
3156 signal (SIGPIPE, SIG_IGN);
3157 signal (SIGTERM, SIG_IGN);
3158 signal (SIGABRT, SIG_IGN);
3159 signal (SIGSEGV, SIG_IGN);
3160 signal (SIGURG, SIG_IGN);
3161 signal (SIGKILL, SIG_IGN);
3162 log(DEBUG,"*** fell down a pothole in the road to perfection ***");
3163 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
3167 int main (int argc, char *argv[])
3170 log(DEBUG,"*** InspIRCd starting up!");
3171 if (!FileExists(CONFIG_FILE))
3173 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
3174 log(DEBUG,"main: no config");
3175 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
3179 if (!strcmp(argv[1],"-nofork")) {
3183 if (InspIRCd() == ERROR)
3185 log(DEBUG,"main: daemon function bailed");
3186 printf("ERROR: could not initialise. Shutting down.\n");
3193 template<typename T> inline string ConvToStr(const T &in)
3196 if (!(tmp << in)) return string();
3200 /* re-allocates a nick in the user_hash after they change nicknames,
3201 * returns a pointer to the new user as it may have moved */
3203 userrec* ReHashNick(char* Old, char* New)
3205 user_hash::iterator newnick;
3206 user_hash::iterator oldnick = clientlist.find(Old);
3208 log(DEBUG,"ReHashNick: %s %s",Old,New);
3210 if (!strcasecmp(Old,New))
3212 log(DEBUG,"old nick is new nick, skipping");
3213 return oldnick->second;
3216 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
3218 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
3220 clientlist[New] = new userrec();
3221 clientlist[New] = oldnick->second;
3222 /*delete oldnick->second; */
3223 clientlist.erase(oldnick);
3225 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
3227 return clientlist[New];
3230 /* adds or updates an entry in the whowas list */
3231 void AddWhoWas(userrec* u)
3233 user_hash::iterator iter = whowas.find(u->nick);
3234 userrec *a = new userrec();
3235 strcpy(a->nick,u->nick);
3236 strcpy(a->ident,u->ident);
3237 strcpy(a->dhost,u->dhost);
3238 strcpy(a->host,u->host);
3239 strcpy(a->fullname,u->fullname);
3240 strcpy(a->server,u->server);
3241 a->signon = u->signon;
3243 /* MAX_WHOWAS: max number of /WHOWAS items
3244 * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
3245 * can be replaced by a newer one
3248 if (iter == whowas.end())
3250 if (whowas.size() == WHOWAS_MAX)
3252 for (user_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
3254 // 3600 seconds in an hour ;)
3255 if ((i->second->signon)<(time(NULL)-(WHOWAS_STALE*3600)))
3259 log(DEBUG,"added WHOWAS entry, purged an old record");
3266 log(DEBUG,"added fresh WHOWAS entry");
3267 whowas[a->nick] = a;
3272 log(DEBUG,"updated WHOWAS entry");
3273 delete iter->second;
3279 /* add a client connection to the sockets list */
3280 void AddClient(int socket, char* host, int port, bool iscached)
3284 char resolved[MAXBUF];
3287 user_hash::iterator iter;
3289 tempnick = ConvToStr(socket) + "-unknown";
3290 sprintf(tn2,"%d-unknown",socket);
3292 iter = clientlist.find(tempnick);
3294 if (iter != clientlist.end()) return;
3297 * It is OK to access the value here this way since we know
3298 * it exists, we just created it above.
3300 * At NO other time should you access a value in a map or a
3301 * hash_map this way.
3303 clientlist[tempnick] = new userrec();
3305 NonBlocking(socket);
3306 log(DEBUG,"AddClient: %d %s %d",socket,host,port);
3309 clientlist[tempnick]->fd = socket;
3310 strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
3311 strncpy(clientlist[tempnick]->host, host,160);
3312 strncpy(clientlist[tempnick]->dhost, host,160);
3313 strncpy(clientlist[tempnick]->server, ServerName,256);
3314 clientlist[tempnick]->registered = 0;
3315 clientlist[tempnick]->signon = time(NULL);
3316 clientlist[tempnick]->nping = time(NULL)+240;
3317 clientlist[tempnick]->lastping = 1;
3318 clientlist[tempnick]->port = port;
3322 WriteServ(socket,"NOTICE Auth :Found your hostname (cached)...");
3326 WriteServ(socket,"NOTICE Auth :Looking up your hostname...");
3329 if (clientlist.size() == MAXCLIENTS)
3330 kill_link(clientlist[tempnick],"No more connections allowed in this class");
3333 void handle_names(char **parameters, int pcnt, userrec *user)
3337 if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
3339 c = FindChan(parameters[0]);
3342 /*WriteServ(user->fd,"353 %s = %s :%s", user->nick, c->name,*/
3344 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
3348 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3353 void handle_privmsg(char **parameters, int pcnt, userrec *user)
3358 if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
3360 if (parameters[0][0] == '#')
3362 chan = FindChan(parameters[0]);
3365 if ((chan->noexternal) && (!has_channel(user,chan)))
3367 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
3370 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
3372 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
3375 ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
3379 /* no such nick/channel */
3380 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3385 dest = Find(parameters[0]);
3388 if (strcmp(dest->awaymsg,""))
3390 /* auto respond with aweh msg */
3391 WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
3393 WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
3397 /* no such nick/channel */
3398 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3402 void handle_notice(char **parameters, int pcnt, userrec *user)
3407 if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
3409 if (parameters[0][0] == '#')
3411 chan = FindChan(parameters[0]);
3414 if ((chan->noexternal) && (!has_channel(user,chan)))
3416 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
3419 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
3421 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
3424 WriteChannel(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
3428 /* no such nick/channel */
3429 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3434 dest = Find(parameters[0]);
3437 WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
3441 /* no such nick/channel */
3442 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3448 char* chlist(userrec *user)
3453 log(DEBUG,"chlist: %s",user->nick);
3459 for (i = 0; i != MAXCHANS; i++)
3461 if (user->chans[i].channel != NULL)
3463 if (user->chans[i].channel->name)
3465 strcpy(cmp,user->chans[i].channel->name);
3467 if (!strstr(lst,cmp))
3469 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
3471 strcat(lst,cmode(user,user->chans[i].channel));
3472 strcat(lst,user->chans[i].channel->name);
3482 void handle_info(char **parameters, int pcnt, userrec *user)
3484 WriteServ(user->fd,"371 %s :The Inspire IRCd Project Has been brought to you by the following people..",user->nick);
3485 WriteServ(user->fd,"371 %s :Craig Edwards, Craig McLure, and Others..",user->nick);
3486 WriteServ(user->fd,"371 %s :Will finish this later when i can be arsed :p",user->nick);
3487 WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
3490 void handle_time(char **parameters, int pcnt, userrec *user)
3493 struct tm * timeinfo;
3496 timeinfo = localtime ( &rawtime );
3497 WriteServ(user->fd,"391 %s %s :%s",user->nick,ServerName, asctime (timeinfo) );
3501 void handle_whois(char **parameters, int pcnt, userrec *user)
3506 if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
3508 dest = Find(parameters[0]);
3511 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
3512 if ((user == dest) || (strchr(user->modes,'o')))
3514 WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
3516 if (strcmp(chlist(dest),""))
3518 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
3520 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
3521 if (strcmp(dest->awaymsg,""))
3523 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
3525 if (strchr(dest->modes,'o'))
3527 WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
3529 //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
3530 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
3532 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
3536 /* no such nick/channel */
3537 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3541 void handle_quit(char **parameters, int pcnt, userrec *user)
3543 user_hash::iterator iter = clientlist.find(user->nick);
3546 if (user->registered == 7)
3548 /* theres more to do here, but for now just close the socket */
3551 if (parameters[0][0] == ':')
3555 reason = parameters[0];
3557 if (strlen(reason)>MAXQUIT)
3559 reason[MAXQUIT-1] = '\0';
3562 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
3563 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
3564 WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
3568 Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
3569 WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
3570 WriteCommonExcept(user,"QUIT :Client exited");
3572 FOREACH_MOD OnUserQuit(user);
3576 /* push the socket on a stack of sockets due to be closed at the next opportunity */
3577 fd_reap.push_back(user->fd);
3579 if (iter != clientlist.end())
3581 log(DEBUG,"deleting user hash value %d",iter->second);
3582 if ((iter->second) && (user->registered == 7)) {
3583 delete iter->second;
3585 clientlist.erase(iter);
3588 if (user->registered == 7) {
3589 purge_empty_chans();
3593 void handle_who(char **parameters, int pcnt, userrec *user)
3595 chanrec* Ptr = NULL;
3597 /* theres more to do here, but for now just close the socket */
3600 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
3602 if (user->chans[0].channel)
3604 Ptr = user->chans[0].channel;
3605 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3607 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
3609 WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, ServerName, i->second->nick, i->second->fullname);
3615 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3619 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, user->nick);
3623 if (parameters[0][0] = '#')
3625 Ptr = FindChan(parameters[0]);
3628 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3630 if ((has_channel(i->second,Ptr)) && (isnick(i->second->nick)))
3632 WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, ServerName, i->second->nick, i->second->fullname);
3635 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3639 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3645 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
3647 Ptr = user->chans[0].channel;
3648 printf(user->chans[0].channel->name);
3649 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3651 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
3653 if (strchr(i->second->modes,'o'))
3655 WriteServ(user->fd,"352 %s %s %s %s %s %s Hr@ :0 %s",user->nick, Ptr->name, i->second->ident, i->second->dhost, ServerName, i->second->nick, i->second->fullname);
3659 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3665 void handle_wallops(char **parameters, int pcnt, userrec *user)
3667 WriteWallOps(user,"%s",parameters[0]);
3670 void handle_list(char **parameters, int pcnt, userrec *user)
3674 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
3675 for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
3677 if ((!i->second->c_private) && (!i->second->secret))
3679 WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
3682 WriteServ(user->fd,"323 %s :End of channel list.",user->nick);
3686 void handle_rehash(char **parameters, int pcnt, userrec *user)
3688 WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CONFIG_FILE);
3690 FOREACH_MOD OnRehash();
3691 WriteOpers("%s is rehashing config file %s",user->nick,CONFIG_FILE);
3697 return clientlist.size();
3700 int usercount_invisible(void)
3704 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3706 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
3711 int usercount_opers(void)
3715 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3717 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
3722 int usercount_unknown(void)
3726 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3728 if ((i->second->fd) && (i->second->registered != 7))
3736 return chanlist.size();
3739 int servercount(void)
3744 void handle_lusers(char **parameters, int pcnt, userrec *user)
3746 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),servercount());
3747 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
3748 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
3749 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
3750 WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,usercnt());
3753 void handle_admin(char **parameters, int pcnt, userrec *user)
3755 WriteServ(user->fd,"256 %s :Administrative info for %s",user->nick,ServerName);
3756 WriteServ(user->fd,"257 %s :Name - %s",user->nick,AdminName);
3757 WriteServ(user->fd,"258 %s :Nickname - %s",user->nick,AdminNick);
3758 WriteServ(user->fd,"258 %s :E-Mail - %s",user->nick,AdminEmail);
3761 void ShowMOTD(userrec *user)
3765 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
3768 WriteServ(user->fd,"375 %s :- %s message of the day",user->nick,ServerName);
3769 for (int i = 0; i != MOTD.size(); i++)
3771 WriteServ(user->fd,"372 %s :- %s",user->nick,MOTD[i].c_str());
3773 WriteServ(user->fd,"376 %s :End of %s message of the day.",user->nick,ServerName);
3776 void ShowRULES(userrec *user)
3780 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
3783 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
3784 for (int i = 0; i != RULES.size(); i++)
3786 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
3788 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
3791 /* shows the message of the day, and any other on-logon stuff */
3792 void ConnectUser(userrec *user)
3794 user->registered = 7;
3795 user->idle_lastmsg = time(NULL);
3796 log(DEBUG,"ConnectUser: %s",user->nick);
3798 if (strcmp(Passwd(user),"") && (!user->haspassed))
3800 Write(user->fd,"ERROR :Closing link: Invalid password");
3801 fdatasync(user->fd);
3802 kill_link(user,"Invalid password");
3807 Write(user->fd,"ERROR :Closing link: Unauthorized connection");
3808 fdatasync(user->fd);
3809 kill_link(user,"Unauthorised connection");
3812 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
3813 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
3814 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
3815 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
3816 WriteServ(user->fd,"004 %s :%s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
3817 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);
3818 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);
3820 FOREACH_MOD OnUserConnect(user);
3821 WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
3824 void handle_version(char **parameters, int pcnt, userrec *user)
3826 WriteServ(user->fd,"351 %s :%s %s %s :%s",user->nick,VERSION,"$Id$",ServerName,SYSTEM);
3829 void handle_ping(char **parameters, int pcnt, userrec *user)
3831 WriteServ(user->fd,"PONG %s :%s",ServerName,parameters[0]);
3834 void handle_pong(char **parameters, int pcnt, userrec *user)
3836 // set the user as alive so they survive to next ping
3840 void handle_motd(char **parameters, int pcnt, userrec *user)
3845 void handle_rules(char **parameters, int pcnt, userrec *user)
3850 void handle_user(char **parameters, int pcnt, userrec *user)
3852 if (user->registered < 3)
3854 if (isident(parameters[0]) == 0) {
3855 // This kinda Sucks, According to the RFC thou, its either this,
3856 // or "You have already registered" :p -- Craig
3857 WriteServ(user->fd,"461 %s USER :Not enough parameters",user->nick);
3860 WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
3861 strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
3862 strncat(user->ident,parameters[0],IDENTMAX);
3863 strncpy(user->fullname,parameters[3],128);
3864 user->registered = (user->registered | 1);
3869 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
3872 /* parameters 2 and 3 are local and remote hosts, ignored when sent by client connection */
3873 if (user->registered == 3)
3875 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
3880 void handle_userhost(char **parameters, int pcnt, userrec *user)
3882 char Return[MAXBUF],junk[MAXBUF];
3883 sprintf(Return,"302 %s :",user->nick);
3884 for (int i = 0; i < pcnt; i++)
3886 userrec *u = Find(parameters[i]);
3889 if (strchr(u->modes,'o'))
3891 sprintf(junk,"%s*=+%s@%s ",u->nick,u->ident,u->host);
3892 strcat(Return,junk);
3896 sprintf(junk,"%s=+%s@%s ",u->nick,u->ident,u->host);
3897 strcat(Return,junk);
3901 WriteServ(user->fd,Return);
3905 void handle_ison(char **parameters, int pcnt, userrec *user)
3907 char Return[MAXBUF];
3908 sprintf(Return,"303 %s :",user->nick);
3909 for (int i = 0; i < pcnt; i++)
3911 userrec *u = Find(parameters[i]);
3914 strcat(Return,u->nick);
3918 WriteServ(user->fd,Return);
3922 void handle_away(char **parameters, int pcnt, userrec *user)
3926 strcpy(user->awaymsg,parameters[0]);
3927 WriteServ(user->fd,"306 %s :You have been marked as being away",user->nick);
3931 strcpy(user->awaymsg,"");
3932 WriteServ(user->fd,"305 %s :You are no longer marked as being away",user->nick);
3936 void handle_whowas(char **parameters, int pcnt, userrec* user)
3938 user_hash::iterator i = whowas.find(parameters[0]);
3940 if (i == whowas.end())
3942 WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
3943 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3947 time_t rawtime = i->second->signon;
3951 timeinfo = localtime(&rawtime);
3952 strcpy(b,asctime(timeinfo));
3953 b[strlen(b)-1] = '\0';
3955 WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
3956 WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
3957 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3962 void handle_trace(char **parameters, int pcnt, userrec *user)
3964 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
3968 if (isnick(i->second->nick))
3970 if (strchr(i->second->modes,'o'))
3972 WriteServ(user->fd,"205 %s :Oper 0 %s",user->nick,i->second->nick);
3976 WriteServ(user->fd,"204 %s :User 0 %s",user->nick,i->second->nick);
3981 WriteServ(user->fd,"203 %s :???? 0 [%s]",user->nick,i->second->host);
3987 void handle_stats(char **parameters, int pcnt, userrec *user)
3993 if (strlen(parameters[0])>1)
3995 /* make the stats query 1 character long */
3996 parameters[0][1] = '\0';
3999 /* stats m (list number of times each command has been used, plus bytecount) */
4000 if (!strcasecmp(parameters[0],"m"))
4002 for (int i = 0; i < cmdlist.size(); i++)
4004 if (cmdlist[i].handler_function)
4006 if (cmdlist[i].use_count)
4008 /* RPL_STATSCOMMANDS */
4009 WriteServ(user->fd,"212 %s %s %d %d",user->nick,cmdlist[i].command,cmdlist[i].use_count,cmdlist[i].total_bytes);
4016 /* stats z (debug and memory info) */
4017 if (!strcasecmp(parameters[0],"z"))
4019 WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
4020 WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
4021 WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
4022 WriteServ(user->fd,"249 %s :MOTD(VECTOR) %d, RULES(VECTOR) %d",user->nick,MOTD.size(),RULES.size());
4023 WriteServ(user->fd,"249 %s :address_cache(HASH_MAP) %d (%d buckets)",user->nick,IP.size(),IP.bucket_count());
4024 WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
4025 WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
4026 WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
4030 if (!strcasecmp(parameters[0],"o"))
4032 for (int i = 0; i < ConfValueEnum("oper"); i++)
4034 char LoginName[MAXBUF];
4035 char HostName[MAXBUF];
4036 char OperType[MAXBUF];
4037 ConfValue("oper","name",i,LoginName);
4038 ConfValue("oper","host",i,HostName);
4039 ConfValue("oper","type",i,OperType);
4040 WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
4044 /* stats l (show user I/O stats) */
4045 if (!strcasecmp(parameters[0],"l"))
4047 WriteServ(user->fd,"211 %s :server:port nick bytes_in cmds_in bytes_out cmds_out",user->nick);
4048 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
4050 if (isnick(i->second->nick))
4052 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);
4056 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);
4062 /* stats u (show server uptime) */
4063 if (!strcasecmp(parameters[0],"u"))
4065 time_t current_time = 0;
4066 current_time = time(NULL);
4067 time_t server_uptime = current_time - startup_time;
4069 stime = gmtime(&server_uptime);
4070 /* i dont know who the hell would have an ircd running for over a year nonstop, but
4071 * Craig suggested this, and it seemed a good idea so in it went */
4072 if (stime->tm_year > 70)
4074 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);
4078 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);
4082 WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
4083 WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
4087 void handle_connect(char **parameters, int pcnt, userrec *user)
4089 char Link_ServerName[1024];
4090 char Link_IPAddr[1024];
4091 char Link_Port[1024];
4092 char Link_Pass[1024];
4096 for (int i = 0; i < ConfValueEnum("link"); i++)
4098 ConfValue("link","name",i,Link_ServerName);
4099 ConfValue("link","ipaddr",i,Link_IPAddr);
4100 ConfValue("link","port",i,Link_Port);
4101 ConfValue("link","sendpass",i,Link_Pass);
4102 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
4103 LinkPort = atoi(Link_Port);
4104 if (match(Link_ServerName,parameters[0])) {
4110 WriteServ(user->fd,"NOTICE %s :*** Failed to connect to %s: No servers matching this pattern are configured for linking.",user->nick,parameters[0]);
4114 // TODO: Perform a check here to stop a server being linked twice!
4116 WriteServ(user->fd,"NOTICE %s :*** Connecting to %s (%s) port %s...",user->nick,Link_ServerName,Link_IPAddr,Link_Port);
4118 if (me[defaultRoute])
4121 // at this point parameters[0] is an ip in a string.
4122 // TODO: Look this up from the <link> blocks instead!
4123 for (int j = 0; j < 255; j++) {
4124 if (servers[j] == NULL) {
4125 servers[j] = new serverrec;
4126 strcpy(servers[j]->internal_addr,Link_IPAddr);
4127 strcpy(servers[j]->name,Link_ServerName);
4128 log(DEBUG,"Allocated new serverrec");
4129 if (!me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass))
4131 WriteServ(user->fd,"NOTICE %s :*** Failed to send auth packet to %s!",user->nick,Link_IPAddr);
4136 WriteServ(user->fd,"NOTICE %s :*** Failed to create server record for address %s!",user->nick,Link_IPAddr);
4140 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);
4144 void handle_squit(char **parameters, int pcnt, userrec *user)
4146 // send out an squit across the mesh and then clear the server list (for local squit)
4149 void handle_oper(char **parameters, int pcnt, userrec *user)
4151 char LoginName[MAXBUF];
4152 char Password[MAXBUF];
4153 char OperType[MAXBUF];
4154 char TypeName[MAXBUF];
4155 char Hostname[MAXBUF];
4158 for (i = 0; i < ConfValueEnum("oper"); i++)
4160 ConfValue("oper","name",i,LoginName);
4161 ConfValue("oper","password",i,Password);
4162 if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
4164 /* correct oper credentials */
4165 ConfValue("oper","type",i,OperType);
4166 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
4167 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
4168 WriteServ(user->fd,"MODE %s :+o",user->nick);
4169 for (j =0; j < ConfValueEnum("type"); j++)
4171 ConfValue("type","name",j,TypeName);
4172 if (!strcmp(TypeName,OperType))
4174 /* found this oper's opertype */
4175 ConfValue("type","host",j,Hostname);
4176 strncpy(user->dhost,Hostname,256);
4179 if (!strchr(user->modes,'o'))
4181 strcat(user->modes,"o");
4187 WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
4188 WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
4191 void handle_nick(char **parameters, int pcnt, userrec *user)
4195 log(DEBUG,"not enough params for handle_nick");
4200 log(DEBUG,"invalid parameter passed to handle_nick");
4203 if (!strlen(parameters[0]))
4205 log(DEBUG,"zero length new nick passed to handle_nick");
4210 log(DEBUG,"invalid user passed to handle_nick");
4215 log(DEBUG,"invalid old nick passed to handle_nick");
4218 if (!strcasecmp(user->nick,parameters[0]))
4220 log(DEBUG,"old nick is new nick, skipping");
4225 if (strlen(parameters[0]) > 1)
4227 if (parameters[0][0] == ':')
4232 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
4234 WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
4238 if (isnick(parameters[0]) == 0)
4240 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
4244 if (user->registered == 7)
4246 WriteCommon(user,"NICK %s",parameters[0]);
4249 /* change the nick of the user in the users_hash */
4250 user = ReHashNick(user->nick, parameters[0]);
4251 /* actually change the nick within the record */
4253 if (!user->nick) return;
4255 strncpy(user->nick, parameters[0],NICKMAX);
4257 log(DEBUG,"new nick set: %s",user->nick);
4259 if (user->registered < 3)
4260 user->registered = (user->registered | 2);
4261 if (user->registered == 3)
4263 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
4266 log(DEBUG,"exit nickchange: %s",user->nick);
4269 int process_parameters(char **command_p,char *parameters)
4274 q = strlen(parameters);
4277 /* no parameters, command_p invalid! */
4280 if (parameters[0] == ':')
4282 command_p[0] = parameters+1;
4287 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
4289 /* only one parameter */
4290 command_p[0] = parameters;
4291 if (parameters[0] == ':')
4293 if (strchr(parameters,' ') != NULL)
4301 command_p[j++] = parameters;
4302 for (i = 0; i <= q; i++)
4304 if (parameters[i] == ' ')
4306 command_p[j++] = parameters+i+1;
4307 parameters[i] = '\0';
4308 if (command_p[j-1][0] == ':')
4310 *command_p[j-1]++; /* remove dodgy ":" */
4312 /* parameter like this marks end of the sequence */
4316 return j; /* returns total number of items in the list */
4319 void process_command(userrec *user, char* cmd)
4323 char *command_p[127];
4324 char p[MAXBUF], temp[MAXBUF];
4325 int i, j, items, cmd_found;
4327 for (int i = 0; i < 127; i++)
4328 command_p[i] = NULL;
4338 if (!strcmp(cmd,""))
4345 FOREACH_MOD OnServerRaw(tmp,true);
4346 const char* cmd2 = tmp.c_str();
4347 snprintf(cmd,512,"%s",cmd2);
4349 if (!strchr(cmd,' '))
4351 /* no parameters, lets skip the formalities and not chop up
4354 command_p[0] = NULL;
4356 for (int i = 0; i <= strlen(cmd); i++)
4358 cmd[i] = toupper(cmd[i]);
4365 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
4366 for (i = 0; i < strlen(temp); i++)
4368 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
4374 /* split the full string into a command plus parameters */
4378 if (strchr(cmd,' '))
4380 for (i = 0; i <= strlen(cmd); i++)
4382 /* capitalise the command ONLY, leave params intact */
4383 cmd[i] = toupper(cmd[i]);
4384 /* are we nearly there yet?! :P */
4388 parameters = cmd+i+1;
4396 for (i = 0; i <= strlen(cmd); i++)
4398 cmd[i] = toupper(cmd[i]);
4406 if (strlen(command)>MAXCOMMAND)
4408 command[MAXCOMMAND-1] = '\0';
4409 WriteOpers("Possible command-flood from %s, sending excessively long commands.",user->nick);
4412 for (i = 0; i != cmdlist.size(); i++)
4414 if (strcmp(cmdlist[i].command,""))
4416 if (!strcmp(command, cmdlist[i].command))
4420 if (strcmp(parameters,""))
4422 items = process_parameters(command_p,parameters);
4427 command_p[0] = NULL;
4433 command_p[0] = NULL;
4438 user->idle_lastmsg = time(NULL);
4439 /* activity resets the ping pending timer */
4440 user->nping = time(NULL) + 120;
4441 if ((items) < cmdlist[i].min_params)
4443 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
4444 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
4447 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
4449 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
4450 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
4454 /* if the command isnt USER, PASS, or NICK, and nick is empty,
4456 if ((strcmp(command,"USER")) && (strcmp(command,"NICK")) && (strcmp(command,"PASS")))
4458 if ((!isnick(user->nick)) || (user->registered != 7))
4460 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
4461 WriteServ(user->fd,"451 %s :You have not registered",command);
4465 if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
4467 log(DEBUG,"process_command: handler: %s %s %d",user->nick,command,items);
4468 if (cmdlist[i].handler_function)
4470 /* ikky /stats counters */
4475 user->bytes_in += strlen(temp);
4478 cmdlist[i].use_count++;
4479 cmdlist[i].total_bytes+=strlen(temp);
4482 /* WARNING: nothing may come after the
4483 * command handler call, as the handler
4484 * may free the user structure! */
4486 cmdlist[i].handler_function(command_p,items,user);
4492 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
4493 WriteServ(user->fd,"451 %s :You have not registered",command);
4501 if ((!cmd_found) && (user))
4503 log(DEBUG,"process_command: not in table: %s %s",user->nick,command);
4504 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
4509 void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
4512 /* create the command and push it onto the table */
4513 strcpy(comm.command,cmd);
4514 comm.handler_function = f;
4515 comm.flags_needed = flags;
4516 comm.min_params = minparams;
4518 comm.total_bytes = 0;
4519 cmdlist.push_back(comm);
4520 log(DEBUG,"Added command %s (%d parameters)",cmd,minparams);
4523 void SetupCommandTable(void)
4525 createcommand("USER",handle_user,0,4);
4526 createcommand("NICK",handle_nick,0,1);
4527 createcommand("QUIT",handle_quit,0,0);
4528 createcommand("VERSION",handle_version,0,0);
4529 createcommand("PING",handle_ping,0,1);
4530 createcommand("PONG",handle_pong,0,1);
4531 createcommand("ADMIN",handle_admin,0,0);
4532 createcommand("PRIVMSG",handle_privmsg,0,2);
4533 createcommand("INFO",handle_info,0,0);
4534 createcommand("TIME",handle_time,0,0);
4535 createcommand("WHOIS",handle_whois,0,1);
4536 createcommand("WALLOPS",handle_wallops,'o',1);
4537 createcommand("NOTICE",handle_notice,0,2);
4538 createcommand("JOIN",handle_join,0,1);
4539 createcommand("NAMES",handle_names,0,1);
4540 createcommand("PART",handle_part,0,1);
4541 createcommand("KICK",handle_kick,0,2);
4542 createcommand("MODE",handle_mode,0,1);
4543 createcommand("TOPIC",handle_topic,0,1);
4544 createcommand("WHO",handle_who,0,1);
4545 createcommand("MOTD",handle_motd,0,0);
4546 createcommand("RULES",handle_join,0,0);
4547 createcommand("OPER",handle_oper,0,2);
4548 createcommand("LIST",handle_list,0,0);
4549 createcommand("DIE",handle_die,'o',1);
4550 createcommand("RESTART",handle_restart,'o',1);
4551 createcommand("KILL",handle_kill,'o',2);
4552 createcommand("REHASH",handle_rehash,'o',0);
4553 createcommand("LUSERS",handle_lusers,0,0);
4554 createcommand("STATS",handle_stats,0,1);
4555 createcommand("USERHOST",handle_userhost,0,1);
4556 createcommand("AWAY",handle_away,0,0);
4557 createcommand("ISON",handle_ison,0,0);
4558 createcommand("SUMMON",handle_summon,0,0);
4559 createcommand("USERS",handle_users,0,0);
4560 createcommand("INVITE",handle_invite,0,2);
4561 createcommand("PASS",handle_pass,0,1);
4562 createcommand("TRACE",handle_trace,'o',0);
4563 createcommand("WHOWAS",handle_whowas,0,1);
4564 createcommand("CONNECT",handle_connect,'o',1);
4565 createcommand("SQUIT",handle_squit,'o',1);
4568 void process_buffer(userrec *user)
4572 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
4579 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
4582 if (!strcmp(user->inbuf,""))
4586 strncpy(cmd,user->inbuf,MAXBUF);
4587 if (!strcmp(cmd,""))
4591 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
4593 cmd[strlen(cmd)-1] = '\0';
4595 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
4597 cmd[strlen(cmd)-1] = '\0';
4599 strcpy(user->inbuf,"");
4600 if (!strcmp(cmd,""))
4604 log(DEBUG,"InspIRCd: processing: %s %s",user->nick,cmd);
4608 process_command(user,cmd);
4612 void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
4614 WriteOpers("Secure-UDP-Channel: Token='%c', Params='%s'",token,params);
4618 void handle_link_packet(long theirkey, char* udp_msg, char* udp_host, int udp_port, serverrec *serv)
4620 char response[10240];
4621 char token = udp_msg[0];
4622 char* params = udp_msg + 2;
4623 char finalparam[1024];
4624 strcpy(finalparam," :xxxx");
4625 if (strstr(params," :")) {
4626 strncpy(finalparam,strstr(params," :"),1024);
4629 // S test.chatspike.net password :ChatSpike InspIRCd test server
4630 char* servername = strtok(params," ");
4631 char* password = strtok(NULL," ");
4632 char* serverdesc = finalparam+2;
4633 WriteOpers("CONNECT from %s (%s)",servername,udp_host,password,serverdesc);
4636 char Link_ServerName[1024];
4637 char Link_IPAddr[1024];
4638 char Link_Port[1024];
4639 char Link_Pass[1024];
4640 char Link_SendPass[1024];
4643 // search for a corresponding <link> block in the config files
4644 for (int i = 0; i < ConfValueEnum("link"); i++)
4646 ConfValue("link","name",i,Link_ServerName);
4647 ConfValue("link","ipaddr",i,Link_IPAddr);
4648 ConfValue("link","port",i,Link_Port);
4649 ConfValue("link","recvpass",i,Link_Pass);
4650 ConfValue("link","sendpass",i,Link_SendPass);
4651 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
4652 LinkPort = atoi(Link_Port);
4653 if (!strcasecmp(Link_ServerName,servername)) {
4654 if (!strcasecmp(Link_IPAddr,udp_host)) {
4655 if (LinkPort == udp_port) {
4656 // we have a matching link line -
4657 // send a 'diminutive' server message back...
4658 snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
4659 serv->SendPacket(response,udp_host,udp_port,0);
4660 WriteOpers("CONNECT from %s accepted, authenticating",servername);
4661 for (int j = 0; j < 255; j++) {
4662 if (servers[j] == NULL) {
4663 servers[j] = new serverrec;
4664 strcpy(servers[j]->internal_addr,udp_host);
4665 strcpy(servers[j]->name,servername);
4666 // create a server record for this server
4667 snprintf(response,10240,"O %d",MyKey);
4668 serv->SendPacket(response,udp_host,udp_port,0);
4672 WriteOpers("Internal error connecting to %s, failed to create server record!",servername);
4676 log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
4680 log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
4684 log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
4687 serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
4688 WriteOpers("CONNECT from %s denied, no matching link block",servername);
4693 // if this is received, this means the server-ip that sent it said "OK" to credentials.
4694 // only when a server says this do we exchange keys. The server MUST have an entry in the servers
4695 // array, which is only added by an 'S' packet or BeginLink().
4696 for (int i = 0; i < 255; i++) {
4697 if (servers[i] != NULL) {
4698 if (!strcasecmp(servers[i]->internal_addr,udp_host)) {
4699 servers[i]->key = atoi(params);
4700 log(DEBUG,"Key for this server is now %d",servers[i]->key);
4701 serv->SendPacket("Z blah blah",udp_host,udp_port,MyKey);
4706 WriteOpers("\2WARNING!\2 Server ip %s attempted a key exchange, but is not in the authentication state! Possible intrusion attempt!",udp_host);
4710 // S test.chatspike.net password :ChatSpike InspIRCd test server
4711 char* servername = strtok(params," ");
4712 char* password = strtok(NULL," ");
4713 char* serverdesc = finalparam+2;
4715 // TODO: we should do a check here to ensure that this server is one we recently initiated a
4716 // link with, and didnt hear an 's' or 'E' back from yet (these are the only two valid responses
4717 // to an 'S' command. If we didn't recently send an 'S' to this server, theyre trying to spoof
4718 // a connect, so put out an oper alert!
4723 // for now, just accept all, we'll fix that later.
4724 WriteOpers("%s accepted our link credentials ",servername);
4726 char Link_ServerName[1024];
4727 char Link_IPAddr[1024];
4728 char Link_Port[1024];
4729 char Link_Pass[1024];
4730 char Link_SendPass[1024];
4733 // search for a corresponding <link> block in the config files
4734 for (int i = 0; i < ConfValueEnum("link"); i++)
4736 ConfValue("link","name",i,Link_ServerName);
4737 ConfValue("link","ipaddr",i,Link_IPAddr);
4738 ConfValue("link","port",i,Link_Port);
4739 ConfValue("link","recvpass",i,Link_Pass);
4740 ConfValue("link","sendpass",i,Link_SendPass);
4741 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
4742 LinkPort = atoi(Link_Port);
4743 if (!strcasecmp(Link_ServerName,servername)) {
4744 if (!strcasecmp(Link_IPAddr,udp_host)) {
4745 if (LinkPort == udp_port) {
4746 // matching link at this end too, we're all done!
4747 // at this point we must begin key exchange and insert this
4748 // server into our 'active' table.
4749 for (int j = 0; j < 255; j++) {
4750 if (servers[j] != NULL) {
4751 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
4752 WriteOpers("Server %s authenticated, exchanging server keys...",servername);
4753 snprintf(response,10240,"O %d",MyKey);
4754 serv->SendPacket(response,udp_host,udp_port,0);
4759 WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right noe! Possible intrusion attempt!",udp_host);
4764 log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
4768 log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
4772 log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
4775 serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
4776 WriteOpers("CONNECT from %s denied, no matching link block",servername);
4781 char* error_message = finalparam+2;
4782 WriteOpers("ERROR from %s: %s",udp_host,error_message);
4783 // remove this server from any lists
4784 for (int j = 0; j < 255; j++) {
4785 if (servers[j] != NULL) {
4786 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
4796 serverrec* source_server = NULL;
4798 for (int j = 0; j < 255; j++) {
4799 if (servers[j] != NULL) {
4800 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
4801 if (servers[j]->key == theirkey) {
4802 // found a valid key for this server, can process restricted stuff here
4803 process_restricted_commands(token,params,servers[j],serv,udp_host,udp_port);
4811 log(DEBUG,"Unrecognised token or unauthenticated host in datagram from %s:%d: %c",udp_host,udp_port,token);
4815 int reap_counter = 0;
4819 struct sockaddr_in client, server;
4820 char addrs[MAXBUF][255];
4821 int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
4823 int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
4824 int selectResult = 0;
4825 char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
4826 char resolvedHost[MAXBUF];
4830 log(DEBUG,"InspIRCd: startup: begin");
4834 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
4836 log(DEBUG,"InspIRCd: startup: not starting with UID 0!");
4838 SetupCommandTable();
4839 log(DEBUG,"InspIRCd: startup: default command table set up");
4842 if (strcmp(DieValue,""))
4844 printf("WARNING: %s\n\n",DieValue);
4847 log(DEBUG,"InspIRCd: startup: read config");
4849 int count2 = 0, count3 = 0;
4850 for (count = 0; count < ConfValueEnum("bind"); count++)
4852 ConfValue("bind","port",count,configToken);
4853 ConfValue("bind","address",count,Addr);
4854 ConfValue("bind","type",count,Type);
4855 if (!strcmp(Type,"servers"))
4857 char Default[MAXBUF];
4858 strcpy(Default,"no");
4859 ConfValue("bind","default",count,Default);
4860 if (strchr(Default,'y'))
4862 defaultRoute = count3;
4863 log(DEBUG,"InspIRCd: startup: binding '%s:%s' is default server route",Addr,configToken);
4865 me[count3] = new serverrec(ServerName,100L,false);
4866 me[count3]->CreateListener(Addr,atoi(configToken));
4871 ports[count2] = atoi(configToken);
4872 strcpy(addrs[count2],Addr);
4875 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
4878 UDPportCount = count3;
4880 log(DEBUG,"InspIRCd: startup: read %d total client ports and %d total server ports",portCount,UDPportCount);
4882 log(DEBUG,"InspIRCd: startup: InspIRCd is now running!");
4886 /* BugFix By Craig! :p */
4888 for (count = 0; count2 < ConfValueEnum("module"); count2++)
4890 char modfile[MAXBUF];
4891 ConfValue("module","name",count,configToken);
4892 sprintf(modfile,"%s/%s",MOD_PATH,configToken);
4893 printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
4894 log(DEBUG,"InspIRCd: startup: Loading module: %s",modfile);
4895 /* If The File Doesnt exist, Trying to load it
4896 * Will Segfault the IRCd.. So, check to see if
4897 * it Exists, Before Proceeding. */
4898 if (FileExists(modfile))
4900 factory[count] = new ircd_module(modfile);
4901 if (factory[count]->LastError())
4903 log(DEBUG,"Unable to load %s: %s",modfile,factory[count]->LastError());
4904 sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
4907 if (factory[count]->factory)
4909 modules[count] = factory[count]->factory->CreateModule();
4910 /* save the module and the module's classfactory, if
4911 * this isnt done, random crashes can occur :/ */
4915 log(DEBUG,"Unable to load %s",modfile);
4916 sprintf("Unable to load %s\nExiting...\n",modfile);
4919 /* Increase the Count */
4924 log(DEBUG,"InspIRCd: startup: Module Not Found %s",modfile);
4925 printf("Module Not Found: \033[1;37m%s\033[0;37m, Skipping\n",modfile);
4928 MODCOUNT = count - 1;
4929 log(DEBUG,"Total loaded modules: %d",MODCOUNT+1);
4931 printf("\nInspIRCd is now running!\n");
4933 startup_time = time(NULL);
4937 log(VERBOSE,"Not forking as -nofork was specified");
4941 if (DaemonSeed() == ERROR)
4943 log(DEBUG,"InspIRCd: startup: can't daemonise");
4944 printf("ERROR: could not go into daemon mode. Shutting down.\n");
4950 /* setup select call */
4951 FD_ZERO(&selectFds);
4952 log(DEBUG,"InspIRCd: startup: zero selects");
4953 log(VERBOSE,"InspIRCd: startup: portCount = %d", portCount);
4955 for (count = 0; count < portCount; count++)
4957 if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
4959 log(DEBUG,"InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
4962 if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
4964 log(DEBUG,"InspIRCd: startup: failed to bind port %d",ports[count]);
4966 else /* well we at least bound to one socket so we'll continue */
4972 log(DEBUG,"InspIRCd: startup: total bound ports %d",boundPortCount);
4974 /* if we didn't bind to anything then abort */
4975 if (boundPortCount == 0)
4977 log(DEBUG,"InspIRCd: startup: no ports bound, bailing!");
4981 length = sizeof (client);
4982 int flip_flop = 0, udp_port = 0;
4983 char udp_msg[MAXBUF], udp_host[MAXBUF];
4985 /* main loop for multiplexing/resetting */
4988 /* set up select call */
4989 for (count = 0; count < boundPortCount; count++)
4991 FD_SET (openSockfd[count], &selectFds);
4994 /* added timeout! select was waiting forever... wank... :/ */
5005 vector<int>::iterator niterator;
5008 // *FIX* Instead of closing sockets in kill_link when they receive the ERROR :blah line, we should queue
5009 // them in a list, then reap the list every second or so.
5010 if (reap_counter>5000) {
5011 if (fd_reap.size() > 0) {
5012 for( int n = 0; n < fd_reap.size(); n++)
5014 Blocking(fd_reap[n]);
5016 NonBlocking(fd_reap[n]);
5025 selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
5027 for (int x = 0; x != UDPportCount; x++)
5030 if (me[x]->RecvPacket(udp_msg, udp_host, udp_port, theirkey))
5032 if (strlen(udp_msg)<1) {
5033 log(DEBUG,"Invalid datagram from %s:%d:%d [route%d]",udp_host,udp_port,me[x]->port,x);
5036 FOREACH_MOD OnPacketReceive(udp_msg);
5037 // Packets must go back via the route they arrived on :)
5038 handle_link_packet(theirkey, udp_msg, udp_host, udp_port, me[x]);
5043 for (user_hash::iterator count2 = clientlist.begin(); count2 != clientlist.end(); count2++)
5047 if (!count2->second) break;
5050 if (count2->second->fd)
5052 if (((time(NULL)) > count2->second->nping) && (isnick(count2->second->nick)) && (count2->second->registered == 7))
5054 if (!count2->second->lastping)
5056 log(DEBUG,"InspIRCd: ping timeout: %s",count2->second->nick);
5057 kill_link(count2->second,"Ping timeout");
5060 Write(count2->second->fd,"PING :%s",ServerName);
5061 log(DEBUG,"InspIRCd: pinging: %s",count2->second->nick);
5062 count2->second->lastping = 0;
5063 count2->second->nping = time(NULL)+120;
5066 result = read(count2->second->fd, data, 1);
5067 // result EAGAIN means nothing read
5068 if (result == EAGAIN)
5076 log(DEBUG,"InspIRCd: Exited: %s",count2->second->nick);
5077 kill_link(count2->second,"Client exited");
5078 // must bail here? kill_link removes the hash, corrupting the iterator
5079 log(DEBUG,"Bailing from client exit");
5083 else if (result > 0)
5087 strncat(count2->second->inbuf, data, result);
5089 if (strlen(count2->second->inbuf) > 509) {
5090 count2->second->inbuf[509] = '\r';
5091 count2->second->inbuf[510] = '\n';
5092 count2->second->inbuf[511] = '\0';
5095 if (strchr(count2->second->inbuf, '\n') || strchr(count2->second->inbuf, '\r') || (strlen(count2->second->inbuf) > 509))
5097 /* at least one complete line is waiting to be processed */
5098 if (!count2->second->fd)
5102 process_buffer(count2->second);
5111 /* select is reporting a waiting socket. Poll them all to find out which */
5112 if (selectResult > 0)
5114 char target[MAXBUF], resolved[MAXBUF];
5115 for (count = 0; count < boundPortCount; count++)
5117 if (FD_ISSET (openSockfd[count], &selectFds))
5119 incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
5121 address_cache::iterator iter = IP.find(client.sin_addr);
5122 bool iscached = false;
5123 if (iter == IP.end())
5125 /* ip isn't in cache, add it */
5126 strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
5127 if(CleanAndResolve(resolved, target) != TRUE)
5129 strncpy(resolved,target,MAXBUF);
5131 /* hostname now in 'target' */
5132 IP[client.sin_addr] = new string(resolved);
5133 /* hostname in cache */
5137 /* found ip (cached) */
5138 strncpy(resolved, iter->second->c_str(), MAXBUF);
5142 if (incomingSockfd < 0)
5144 WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
5145 log(DEBUG,"InspIRCd: accept failed: %d",ports[count]);
5149 AddClient(incomingSockfd, resolved, ports[count], iscached);
5150 log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
5159 close (incomingSockfd);