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 vector<string> module_names;
80 extern vector<ircd_module*> factory;
89 template<> struct nspace::hash<in_addr>
91 size_t operator()(const struct in_addr &a) const
94 memcpy(&q,&a,sizeof(size_t));
99 template<> struct nspace::hash<string>
101 size_t operator()(const string &s) const
104 static struct hash<const char *> strhash;
116 bool operator()(const string& s1, const string& s2) const
118 char a[MAXBUF],b[MAXBUF];
119 strcpy(a,s1.c_str());
120 strcpy(b,s2.c_str());
121 return (strcasecmp(a,b) == 0);
126 struct InAddr_HashComp
129 bool operator()(const in_addr &s1, const in_addr &s2) const
134 memcpy(&q,&s1,sizeof(size_t));
135 memcpy(&p,&s2,sizeof(size_t));
143 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
144 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
145 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
146 typedef std::deque<command_t> command_table;
149 serverrec* servers[255];
153 user_hash clientlist;
156 command_table cmdlist;
163 struct linger linger = { 0 };
164 char bannerBuffer[MAXBUF];
165 int boundPortCount = 0;
166 int portCount = 0, UDPportCount = 0, ports[MAXSOCKS];
167 int defaultRoute = 0;
171 long MyKey = C.GenKey();
175 int has_channel(userrec *u, chanrec *c);
176 int usercount(chanrec *c);
177 int usercount_i(chanrec *c);
178 void update_stats_l(int fd,int data_out);
179 char* Passwd(userrec *user);
180 bool IsDenied(userrec *user);
181 void AddWhoWas(userrec* u);
184 void safedelete(userrec *p)
188 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
189 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
194 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
198 void safedelete(chanrec *p)
203 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
207 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
212 void tidystring(char* str)
214 // strips out double spaces before a : parameter
217 bool go_again = true;
224 while ((str[0] == ' ') && (strlen(str)>0))
231 bool noparse = false;
234 while (a < strlen(str))
236 if ((a<strlen(str)-1) && (noparse==false))
238 if ((str[a] == ' ') && (str[a+1] == ' '))
240 log(DEBUG,"Tidied extra space out of string: %s",str);
248 if ((str[a] == ' ') && (str[a+1] == ':'))
254 temp[t++] = str[a++];
257 strncpy(str,temp,MAXBUF);
261 /* chop a string down to 512 characters and preserve linefeed (irc max
268 FOREACH_MOD OnServerRaw(temp,false);
269 const char* str2 = temp.c_str();
270 sprintf(str,"%s",str2);
273 if (strlen(str) >= 512)
282 std::string getservername()
287 std::string getserverdesc()
292 std::string getnetworkname()
297 std::string getadminname()
302 std::string getadminemail()
307 std::string getadminnick()
312 void log(int level,char *text, ...)
314 char textbuffer[MAXBUF];
317 struct tm * timeinfo;
318 if (level < LogLevel)
322 timeinfo = localtime (&rawtime);
327 va_start (argsPtr, text);
328 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
330 strcpy(b,asctime(timeinfo));
331 b[strlen(b)-1] = ':';
332 fprintf(log_file,"%s %s\n",b,textbuffer);
335 // nofork enabled? display it on terminal too
336 printf("%s %s\n",b,textbuffer);
341 void readfile(file_cache &F, const char* fname)
344 char linebuf[MAXBUF];
346 log(DEBUG,"readfile: loading %s",fname);
348 file = fopen(fname,"r");
353 fgets(linebuf,sizeof(linebuf),file);
354 linebuf[strlen(linebuf)-1]='\0';
355 if (!strcmp(linebuf,""))
361 F.push_back(linebuf);
368 log(DEBUG,"readfile: failed to load file: %s",fname);
370 log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
373 void ReadConfig(void)
375 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF];
378 ConfValue("server","name",0,ServerName);
379 ConfValue("server","description",0,ServerDesc);
380 ConfValue("server","network",0,Network);
381 ConfValue("admin","name",0,AdminName);
382 ConfValue("admin","email",0,AdminEmail);
383 ConfValue("admin","nick",0,AdminNick);
384 ConfValue("files","motd",0,motd);
385 ConfValue("files","rules",0,rules);
386 ConfValue("power","diepass",0,diepass);
387 ConfValue("power","pause",0,pauseval);
388 ConfValue("power","restartpass",0,restartpass);
389 ConfValue("options","prefixquit",0,PrefixQuit);
390 ConfValue("die","value",0,DieValue);
391 ConfValue("options","loglevel",0,dbg);
392 if (!strcmp(dbg,"debug"))
394 if (!strcmp(dbg,"verbose"))
396 if (!strcmp(dbg,"default"))
398 if (!strcmp(dbg,"sparse"))
400 if (!strcmp(dbg,"none"))
402 readfile(RULES,rules);
403 log(DEBUG,"Reading connect classes");
405 for (int i = 0; i < ConfValueEnum("connect"); i++)
408 ConfValue("connect","allow",i,Value);
409 if (strcmp(Value,""))
411 strcpy(c.host,Value);
414 ConfValue("connect","password",i,Value);
415 strcpy(c.pass,Value);
416 Classes.push_back(c);
417 log(DEBUG,"Read connect class type ALLOW, host=%s password=%s",c.host,c.pass);
421 ConfValue("connect","deny",i,Value);
422 strcpy(c.host,Value);
424 Classes.push_back(c);
425 log(DEBUG,"Read connect class type DENY, host=%s",c.host);
434 log(DEBUG,"Blocking: %d",s);
435 flags = fcntl(s, F_GETFL, 0);
436 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
439 void NonBlocking(int s)
442 log(DEBUG,"NonBlocking: %d",s);
443 flags = fcntl(s, F_GETFL, 0);
444 fcntl(s, F_SETFL, flags | O_NONBLOCK);
448 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
450 struct hostent *hostPtr = NULL;
453 memset (resolvedHost, '\0',MAXBUF);
454 if(unresolvedHost == NULL)
456 if ((inet_aton(unresolvedHost,&addr)) == 0)
458 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
460 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
462 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
466 /* write formatted text to a socket, in same format as printf */
468 void Write(int sock,char *text, ...)
472 log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
475 char textbuffer[MAXBUF];
479 va_start (argsPtr, text);
480 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
482 sprintf(tb,"%s\r\n",textbuffer);
484 write(sock,tb,strlen(tb));
485 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
488 /* write a server formatted numeric response to a single socket */
490 void WriteServ(int sock, char* text, ...)
494 log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
497 char textbuffer[MAXBUF],tb[MAXBUF];
499 va_start (argsPtr, text);
501 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
503 sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
505 write(sock,tb,strlen(tb));
506 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
509 /* write text from an originating user to originating user */
511 void WriteFrom(int sock, userrec *user,char* text, ...)
513 if ((!text) || (!user))
515 log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
518 char textbuffer[MAXBUF],tb[MAXBUF];
520 va_start (argsPtr, text);
522 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
524 sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
526 write(sock,tb,strlen(tb));
527 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
530 /* write text to an destination user from a source user (e.g. user privmsg) */
532 void WriteTo(userrec *source, userrec *dest,char *data, ...)
534 if ((!source) || (!dest) || (!data))
536 log(DEFAULT,"*** BUG *** WriteTo was given an invalid parameter");
539 char textbuffer[MAXBUF],tb[MAXBUF];
541 va_start (argsPtr, data);
542 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
545 WriteFrom(dest->fd,source,"%s",textbuffer);
548 /* write formatted text from a source user to all users on a channel
549 * including the sender (NOT for privmsg, notice etc!) */
551 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
553 if ((!Ptr) || (!user) || (!text))
555 log(DEFAULT,"*** BUG *** WriteChannel was given an invalid parameter");
558 char textbuffer[MAXBUF];
560 va_start (argsPtr, text);
561 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
563 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
565 if (has_channel(i->second,Ptr))
567 WriteTo(user,i->second,"%s",textbuffer);
572 void WriteChannelWithServ(char* ServerName, chanrec* Ptr, userrec* user, char* text, ...)
574 if ((!Ptr) || (!user) || (!text))
576 log(DEFAULT,"*** BUG *** WriteChannelWithServ was given an invalid parameter");
579 char textbuffer[MAXBUF];
581 va_start (argsPtr, text);
582 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
584 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
588 if (has_channel(i->second,Ptr))
590 WriteServ(i->second->fd,"%s",textbuffer);
597 /* write formatted text from a source user to all users on a channel except
598 * for the sender (for privmsg etc) */
600 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
602 if ((!Ptr) || (!user) || (!text))
604 log(DEFAULT,"*** BUG *** ChanExceptSender was given an invalid parameter");
607 char textbuffer[MAXBUF];
609 va_start (argsPtr, text);
610 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
613 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
617 if (has_channel(i->second,Ptr) && (user != i->second))
619 WriteTo(user,i->second,"%s",textbuffer);
625 int c_count(userrec* u)
628 for (int i =0; i != MAXCHANS; i++)
629 if (u->chans[i].channel != NULL)
635 /* return 0 or 1 depending if users u and u2 share one or more common channels
636 * (used by QUIT, NICK etc which arent channel specific notices) */
638 int common_channels(userrec *u, userrec *u2)
645 log(DEFAULT,"*** BUG *** common_channels was given an invalid parameter");
648 for (i = 0; i != MAXCHANS; i++)
650 for (z = 0; z != MAXCHANS; z++)
652 if ((u->chans[i].channel != NULL) && (u2->chans[z].channel != NULL))
654 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
656 if ((c_count(u)) && (c_count(u2)))
667 /* write a formatted string to all users who share at least one common
668 * channel, including the source user e.g. for use in NICK */
670 void WriteCommon(userrec *u, char* text, ...)
674 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
678 if (u->registered != 7) {
679 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
683 char textbuffer[MAXBUF];
685 va_start (argsPtr, text);
686 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
689 WriteFrom(u->fd,u,"%s",textbuffer);
691 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
695 if (common_channels(u,i->second) && (i->second != u))
697 WriteFrom(i->second->fd,u,"%s",textbuffer);
703 /* write a formatted string to all users who share at least one common
704 * channel, NOT including the source user e.g. for use in QUIT */
706 void WriteCommonExcept(userrec *u, char* text, ...)
710 log(DEFAULT,"*** BUG *** WriteCommon was given an invalid parameter");
714 if (u->registered != 7) {
715 log(DEFAULT,"*** BUG *** WriteCommon on an unregistered user");
719 char textbuffer[MAXBUF];
721 va_start (argsPtr, text);
722 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
725 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
729 if ((common_channels(u,i->second)) && (u != i->second))
731 WriteFrom(i->second->fd,u,"%s",textbuffer);
737 void WriteOpers(char* text, ...)
741 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
745 char textbuffer[MAXBUF];
747 va_start (argsPtr, text);
748 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
751 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
755 if (strchr(i->second->modes,'o'))
757 if (strchr(i->second->modes,'s'))
759 // send server notices to all with +s
760 // (TODO: needs SNOMASKs)
761 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
768 void WriteWallOps(userrec *source, char* text, ...)
770 if ((!text) || (!source))
772 log(DEFAULT,"*** BUG *** WriteOpers was given an invalid parameter");
777 char textbuffer[MAXBUF];
779 va_start (argsPtr, text);
780 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
783 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
787 if (strchr(i->second->modes,'w'))
789 WriteTo(source,i->second,"WALLOPS %s",textbuffer);
795 /* convert a string to lowercase. Note following special circumstances
796 * taken from RFC 1459. Many "official" server branches still hold to this
797 * rule so i will too;
799 * Because of IRC's scandanavian origin, the characters {}| are
800 * considered to be the lower case equivalents of the characters []\,
801 * respectively. This is a critical issue when determining the
802 * equivalence of two nicknames.
805 void strlower(char *n)
811 for (int i = 0; i != strlen(n); i++)
813 n[i] = tolower(n[i]);
823 /* verify that a user's ident and nickname is valid */
825 int isident(const char* n)
838 for (i = 0; i != strlen(n); i++)
840 if ((n[i] < 33) || (n[i] > 125))
844 /* can't occur ANYWHERE in an Ident! */
845 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
854 int isnick(const char* n)
866 if (strlen(n) > NICKMAX-1)
870 for (i = 0; i != strlen(n); i++)
872 if ((n[i] < 33) || (n[i] > 125))
876 /* can't occur ANYWHERE in a nickname! */
877 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
881 /* can't occur as the first char of a nickname... */
882 if ((strchr("0123456789",n[i])) && (!i))
890 /* Find a user record by nickname and return a pointer to it */
892 userrec* Find(string nick)
894 user_hash::iterator iter = clientlist.find(nick);
896 if (iter == clientlist.end())
897 /* Couldn't find it */
903 void update_stats_l(int fd,int data_out) /* add one line-out to stats L for this fd */
905 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
909 if (i->second->fd == fd)
911 i->second->bytes_out+=data_out;
912 i->second->cmds_out++;
919 /* find a channel record by channel name and return a pointer to it */
921 chanrec* FindChan(const char* chan)
925 log(DEFAULT,"*** BUG *** Findchan was given an invalid parameter");
929 chan_hash::iterator iter = chanlist.find(chan);
931 if (iter == chanlist.end())
932 /* Couldn't find it */
939 void purge_empty_chans(void)
941 int go_again = 1, purge = 0;
946 for (chan_hash::iterator i = chanlist.begin(); i != chanlist.end(); i++)
949 if (!usercount(i->second))
951 /* kill the record */
952 if (i != chanlist.end())
954 log(DEBUG,"del_channel: destroyed: %s",i->second->name);
964 log(DEBUG,"skipped purge for %s",i->second->name);
969 log(DEBUG,"completed channel purge, killed %d",purge);
972 /* returns the status character for a given user on a channel, e.g. @ for op,
973 * % for halfop etc. If the user has several modes set, the highest mode
974 * the user has must be returned. */
976 char* cmode(userrec *user, chanrec *chan)
978 if ((!user) || (!chan))
980 log(DEFAULT,"*** BUG *** cmode was given an invalid parameter");
985 for (i = 0; i != MAXCHANS; i++)
987 if ((user->chans[i].channel == chan) && (chan != NULL))
989 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
993 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
997 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
1006 char scratch[MAXMODES];
1007 char sparam[MAXMODES];
1009 char* chanmodes(chanrec *chan)
1013 log(DEFAULT,"*** BUG *** chanmodes was given an invalid parameter");
1020 if (chan->noexternal)
1022 strcat(scratch,"n");
1024 if (chan->topiclock)
1026 strcat(scratch,"t");
1028 if (strcmp(chan->key,""))
1030 strcat(scratch,"k");
1034 strcat(scratch,"l");
1036 if (chan->inviteonly)
1038 strcat(scratch,"i");
1040 if (chan->moderated)
1042 strcat(scratch,"m");
1046 strcat(scratch,"s");
1048 if (chan->c_private)
1050 strcat(scratch,"p");
1052 if (strcmp(chan->key,""))
1054 strcat(sparam,chan->key);
1059 sprintf(foo," %d",chan->limit);
1062 if (strlen(chan->custom_modes))
1064 // TODO: Tack on mode parameters here -
1065 // IN ORDER OF CUSTOM_MODES!
1066 strncat(scratch,chan->custom_modes,MAXMODES);
1068 log(DEBUG,"chanmodes: %s %s%s",chan->name,scratch,sparam);
1069 strncat(scratch,sparam,MAXMODES);
1073 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
1074 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
1075 * highest mode the user has must be returned. */
1077 int cstatus(userrec *user, chanrec *chan)
1079 if ((!chan) || (!user))
1081 log(DEFAULT,"*** BUG *** cstatus was given an invalid parameter");
1086 for (i = 0; i != MAXCHANS; i++)
1088 if ((user->chans[i].channel == chan) && (chan != NULL))
1090 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
1094 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
1098 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
1100 return STATUS_VOICE;
1102 return STATUS_NORMAL;
1108 /* compile a userlist of a channel into a string, each nick seperated by
1109 * spaces and op, voice etc status shown as @ and + */
1111 void userlist(userrec *user,chanrec *c)
1113 if ((!c) || (!user))
1115 log(DEFAULT,"*** BUG *** userlist was given an invalid parameter");
1119 sprintf(list,"353 %s = %s :", user->nick, c->name);
1120 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1122 if (has_channel(i->second,c))
1124 if (isnick(i->second->nick))
1126 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1128 /* user is +i, and source not on the channel, does not show
1129 * nick in NAMES list */
1132 strcat(list,cmode(i->second,c));
1133 strcat(list,i->second->nick);
1135 if (strlen(list)>(480-NICKMAX))
1137 /* list overflowed into
1138 * multiple numerics */
1139 WriteServ(user->fd,list);
1140 sprintf(list,"353 %s = %s :", user->nick, c->name);
1145 /* if whats left in the list isnt empty, send it */
1146 if (list[strlen(list)-1] != ':')
1148 WriteServ(user->fd,list);
1152 /* return a count of the users on a specific channel accounting for
1153 * invisible users who won't increase the count. e.g. for /LIST */
1155 int usercount_i(chanrec *c)
1162 log(DEFAULT,"*** BUG *** usercount_i was given an invalid parameter");
1167 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1171 if (has_channel(i->second,c))
1173 if (isnick(i->second->nick))
1175 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
1177 /* user is +i, and source not on the channel, does not show
1178 * nick in NAMES list */
1186 log(DEBUG,"usercount_i: %s %d",c->name,count);
1191 int usercount(chanrec *c)
1198 log(DEFAULT,"*** BUG *** usercount was given an invalid parameter");
1203 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
1207 if (has_channel(i->second,c))
1209 if ((isnick(i->second->nick)) && (i->second->registered == 7))
1216 log(DEBUG,"usercount: %s %d",c->name,count);
1221 /* add a channel to a user, creating the record for it if needed and linking
1222 * it to the user record */
1224 chanrec* add_channel(userrec *user, char* cname, char* key)
1227 if ((!user) || (!cname))
1229 log(DEFAULT,"*** BUG *** add_channel was given an invalid parameter");
1237 // we MUST declare this wherever we use FOREACH_RESULT
1240 if ((!cname) || (!user))
1244 if (strlen(cname) > CHANMAX-1)
1246 cname[CHANMAX-1] = '\0';
1249 log(DEBUG,"add_channel: %s %s",user->nick,cname);
1251 if ((has_channel(user,FindChan(cname))) && (FindChan(cname)))
1253 return NULL; // already on the channel!
1257 if (!FindChan(cname))
1259 FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
1264 /* create a new one */
1265 log(DEBUG,"add_channel: creating: %s",cname);
1267 chanlist[cname] = new chanrec();
1269 strcpy(chanlist[cname]->name, cname);
1270 chanlist[cname]->topiclock = 1;
1271 chanlist[cname]->noexternal = 1;
1272 chanlist[cname]->created = time(NULL);
1273 strcpy(chanlist[cname]->topic, "");
1274 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
1275 chanlist[cname]->topicset = 0;
1276 Ptr = chanlist[cname];
1277 log(DEBUG,"add_channel: created: %s",cname);
1278 /* set created to 2 to indicate user
1279 * is the first in the channel
1280 * and should be given ops */
1286 /* channel exists, just fish out a pointer to its struct */
1287 Ptr = FindChan(cname);
1290 FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
1295 log(DEBUG,"add_channel: joining to: %s",Ptr->name);
1296 if (strcmp(Ptr->key,""))
1298 log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
1301 log(DEBUG,"add_channel: no key given in JOIN");
1302 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
1307 log(DEBUG,"key at %p is %s",key,key);
1308 if (strcasecmp(key,Ptr->key))
1310 log(DEBUG,"add_channel: bad key given in JOIN");
1311 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
1316 log(DEBUG,"add_channel: no key");
1318 if (Ptr->inviteonly)
1320 log(DEBUG,"add_channel: channel is +i");
1321 if (user->IsInvited(Ptr->name))
1323 /* user was invited to channel */
1324 /* there may be an optional channel NOTICE here */
1328 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
1332 log(DEBUG,"add_channel: channel is not +i");
1336 if (usercount(Ptr) == Ptr->limit)
1338 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
1343 log(DEBUG,"add_channel: about to walk banlist");
1345 /* check user against the channel banlist */
1346 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1348 if (match(user->GetFullHost(),i->data))
1350 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
1355 log(DEBUG,"add_channel: bans checked");
1357 user->RemoveInvite(Ptr->name);
1359 log(DEBUG,"add_channel: invites removed");
1365 log(DEBUG,"Passed channel checks");
1367 for (i =0; i != MAXCHANS; i++)
1369 if (user->chans[i].channel == NULL)
1371 log(DEBUG,"Adding into their channel list");
1375 /* first user in is given ops */
1376 user->chans[i].uc_modes = UCMODE_OP;
1380 user->chans[i].uc_modes = 0;
1382 user->chans[i].channel = Ptr;
1383 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
1385 log(DEBUG,"Sent JOIN to client");
1389 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
1390 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
1393 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
1394 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name,chanmodes(Ptr));
1395 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1396 FOREACH_MOD OnUserJoin(user,Ptr);
1400 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
1401 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
1405 /* remove a channel from a users record, and remove the record from memory
1406 * if the channel has become empty */
1408 chanrec* del_channel(userrec *user, char* cname, char* reason)
1410 if ((!user) || (!cname))
1412 log(DEFAULT,"*** BUG *** del_channel was given an invalid parameter");
1420 if ((!cname) || (!user))
1425 Ptr = FindChan(cname);
1432 FOREACH_MOD OnUserPart(user,Ptr);
1433 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
1435 for (i =0; i != MAXCHANS; i++)
1437 /* zap it from the channel list of the user */
1438 if (user->chans[i].channel == Ptr)
1442 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
1446 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
1448 user->chans[i].uc_modes = 0;
1449 user->chans[i].channel = NULL;
1450 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1455 /* if there are no users left on the channel */
1456 if (!usercount(Ptr))
1458 chan_hash::iterator iter = chanlist.find(Ptr->name);
1460 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1462 /* kill the record */
1463 if (iter != chanlist.end())
1465 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1466 delete iter->second;
1467 chanlist.erase(iter);
1473 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
1475 if ((!src) || (!user) || (!Ptr) || (!reason))
1477 log(DEFAULT,"*** BUG *** kick_channel was given an invalid parameter");
1484 if ((!Ptr) || (!user) || (!src))
1489 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
1491 if (!has_channel(user,Ptr))
1493 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
1496 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
1498 if (cstatus(src,Ptr) == STATUS_HOP)
1500 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
1504 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
1510 for (i =0; i != MAXCHANS; i++)
1512 /* zap it from the channel list of the user */
1513 if (user->chans[i].channel == Ptr)
1515 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
1516 user->chans[i].uc_modes = 0;
1517 user->chans[i].channel = NULL;
1518 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1523 /* if there are no users left on the channel */
1524 if (!usercount(Ptr))
1526 chan_hash::iterator iter = chanlist.find(Ptr->name);
1528 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1530 /* kill the record */
1531 if (iter != chanlist.end())
1533 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1534 delete iter->second;
1535 chanlist.erase(iter);
1541 /* returns 1 if user u has channel c in their record, 0 if not */
1543 int has_channel(userrec *u, chanrec *c)
1549 log(DEFAULT,"*** BUG *** has_channel was given an invalid parameter");
1552 for (i =0; i != MAXCHANS; i++)
1554 if (u->chans[i].channel == c)
1562 int give_ops(userrec *user,char *dest,chanrec *chan,int status)
1567 if ((!user) || (!dest) || (!chan))
1569 log(DEFAULT,"*** BUG *** give_ops was given an invalid parameter");
1572 if (status != STATUS_OP)
1574 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1581 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1587 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1592 for (i = 0; i != MAXCHANS; i++)
1594 if ((d->chans[i].channel == chan) && (chan != NULL))
1596 if (d->chans[i].uc_modes & UCMODE_OP)
1598 /* mode already set on user, dont allow multiple */
1601 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
1602 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
1610 int give_hops(userrec *user,char *dest,chanrec *chan,int status)
1615 if ((!user) || (!dest) || (!chan))
1617 log(DEFAULT,"*** BUG *** give_hops was given an invalid parameter");
1620 if (status != STATUS_OP)
1622 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1630 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1635 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1640 for (i = 0; i != MAXCHANS; i++)
1642 if ((d->chans[i].channel == chan) && (chan != NULL))
1644 if (d->chans[i].uc_modes & UCMODE_HOP)
1646 /* mode already set on user, dont allow multiple */
1649 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
1650 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
1658 int give_voice(userrec *user,char *dest,chanrec *chan,int status)
1663 if ((!user) || (!dest) || (!chan))
1665 log(DEFAULT,"*** BUG *** give_voice was given an invalid parameter");
1668 if (status < STATUS_HOP)
1670 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1678 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1683 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1688 for (i = 0; i != MAXCHANS; i++)
1690 if ((d->chans[i].channel == chan) && (chan != NULL))
1692 if (d->chans[i].uc_modes & UCMODE_VOICE)
1694 /* mode already set on user, dont allow multiple */
1697 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
1698 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
1706 int take_ops(userrec *user,char *dest,chanrec *chan,int status)
1711 if ((!user) || (!dest) || (!chan))
1713 log(DEFAULT,"*** BUG *** take_ops was given an invalid parameter");
1716 if (status != STATUS_OP)
1718 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1726 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1731 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1736 for (i = 0; i != MAXCHANS; i++)
1738 if ((d->chans[i].channel == chan) && (chan != NULL))
1740 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
1742 /* mode already set on user, dont allow multiple */
1745 d->chans[i].uc_modes ^= UCMODE_OP;
1746 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
1754 int take_hops(userrec *user,char *dest,chanrec *chan,int status)
1759 if ((!user) || (!dest) || (!chan))
1761 log(DEFAULT,"*** BUG *** take_hops was given an invalid parameter");
1764 if (status != STATUS_OP)
1766 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1774 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1779 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1784 for (i = 0; i != MAXCHANS; i++)
1786 if ((d->chans[i].channel == chan) && (chan != NULL))
1788 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
1790 /* mode already set on user, dont allow multiple */
1793 d->chans[i].uc_modes ^= UCMODE_HOP;
1794 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
1802 int take_voice(userrec *user,char *dest,chanrec *chan,int status)
1807 if ((!user) || (!dest) || (!chan))
1809 log(DEFAULT,"*** BUG *** take_voice was given an invalid parameter");
1812 if (status < STATUS_HOP)
1814 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1822 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1827 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1832 for (i = 0; i != MAXCHANS; i++)
1834 if ((d->chans[i].channel == chan) && (chan != NULL))
1836 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
1838 /* mode already set on user, dont allow multiple */
1841 d->chans[i].uc_modes ^= UCMODE_VOICE;
1842 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
1850 void TidyBan(char *ban)
1853 log(DEFAULT,"*** BUG *** TidyBan was given an invalid parameter");
1857 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
1861 char* pos_of_pling = strchr(temp,'!');
1862 char* pos_of_at = strchr(temp,'@');
1864 pos_of_pling[0] = '\0';
1865 pos_of_at[0] = '\0';
1869 strncpy(NICK,temp,NICKMAX);
1870 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
1871 strncpy(HOST,pos_of_at,160);
1873 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
1876 int add_ban(userrec *user,char *dest,chanrec *chan,int status)
1878 if ((!user) || (!dest) || (!chan)) {
1879 log(DEFAULT,"*** BUG *** add_ban was given an invalid parameter");
1884 if ((!user) || (!dest) || (!chan))
1886 if (strchr(dest,'!')==0)
1888 if (strchr(dest,'@')==0)
1890 for (int i = 0; i < strlen(dest); i++)
1893 for (int i = 0; i < strlen(dest); i++)
1897 for (int i = 0; i < strlen(dest); i++)
1903 for (int i = 0; i < strlen(dest); i++)
1908 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
1911 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1913 if (!strcasecmp(i->data,dest))
1915 // dont allow a user to set the same ban twice
1920 b.set_time = time(NULL);
1921 strncpy(b.data,dest,MAXBUF);
1922 strncpy(b.set_by,user->nick,NICKMAX);
1923 chan->bans.push_back(b);
1927 int take_ban(userrec *user,char *dest,chanrec *chan,int status)
1929 if ((!user) || (!dest) || (!chan)) {
1930 log(DEFAULT,"*** BUG *** take_ban was given an invalid parameter");
1934 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
1935 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1937 if (!strcasecmp(i->data,dest))
1939 chan->bans.erase(i);
1946 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt, bool servermode)
1948 if ((!parameters) || (!user)) {
1949 log(DEFAULT,"*** BUG *** process_modes was given an invalid parameter");
1954 char modelist[MAXBUF];
1955 char outlist[MAXBUF];
1956 char outstr[MAXBUF];
1957 char outpars[32][MAXBUF];
1963 bool k_set = false, l_set = false;
1970 log(DEBUG,"process_modes: start");
1972 strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
1973 /* parameters[2] onwards are parameters for
1974 * modes that require them :) */
1975 strcpy(outlist,"+");
1978 log(DEBUG,"process_modes: modelist: %s",modelist);
1980 for (ptr = 0; ptr < strlen(modelist); ptr++)
1985 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
1986 char modechar = modelist[ptr];
1987 switch (modelist[ptr])
1992 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
1994 outlist[strlen(outlist)-1] = '-';
1998 strcat(outlist,"-");
2008 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
2010 outlist[strlen(outlist)-1] = '+';
2014 strcat(outlist,"+");
2021 if ((param >= pcnt)) break;
2024 r = give_ops(user,parameters[param++],chan,status);
2028 r = take_ops(user,parameters[param++],chan,status);
2032 strcat(outlist,"o");
2033 strcpy(outpars[pc++],parameters[param-1]);
2038 if ((param >= pcnt)) break;
2041 r = give_hops(user,parameters[param++],chan,status);
2045 r = take_hops(user,parameters[param++],chan,status);
2049 strcat(outlist,"h");
2050 strcpy(outpars[pc++],parameters[param-1]);
2056 if ((param >= pcnt)) break;
2059 r = give_voice(user,parameters[param++],chan,status);
2063 r = take_voice(user,parameters[param++],chan,status);
2067 strcat(outlist,"v");
2068 strcpy(outpars[pc++],parameters[param-1]);
2073 if ((param >= pcnt)) break;
2076 r = add_ban(user,parameters[param++],chan,status);
2080 r = take_ban(user,parameters[param++],chan,status);
2084 strcat(outlist,"b");
2085 strcpy(outpars[pc++],parameters[param-1]);
2090 if ((param >= pcnt))
2098 if (!strcmp(chan->key,""))
2100 strcat(outlist,"k");
2101 strcpy(outpars[pc++],parameters[param++]);
2102 strcpy(chan->key,parameters[param-1]);
2108 /* only allow -k if correct key given */
2109 if (strcmp(chan->key,""))
2111 strcat(outlist,"k");
2112 strcpy(chan->key,"");
2122 strcat(outlist,"l");
2127 if ((param >= pcnt)) break;
2133 bool invalid = false;
2134 for (int i = 0; i < strlen(parameters[param]); i++)
2136 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
2141 if (atoi(parameters[param]) < 1)
2149 chan->limit = atoi(parameters[param]);
2152 strcat(outlist,"l");
2153 strcpy(outpars[pc++],parameters[param++]);
2160 if (chan->inviteonly != mdir)
2162 strcat(outlist,"i");
2164 chan->inviteonly = mdir;
2168 if (chan->topiclock != mdir)
2170 strcat(outlist,"t");
2172 chan->topiclock = mdir;
2176 if (chan->noexternal != mdir)
2178 strcat(outlist,"n");
2180 chan->noexternal = mdir;
2184 if (chan->moderated != mdir)
2186 strcat(outlist,"m");
2188 chan->moderated = mdir;
2192 if (chan->secret != mdir)
2194 strcat(outlist,"s");
2195 if (chan->c_private)
2197 chan->c_private = 0;
2200 strcat(outlist,"-p+");
2204 strcat(outlist,"+p-");
2208 chan->secret = mdir;
2212 if (chan->c_private != mdir)
2214 strcat(outlist,"p");
2220 strcat(outlist,"-s+");
2224 strcat(outlist,"+s-");
2228 chan->c_private = mdir;
2232 log(DEBUG,"Preprocessing custom mode %c",modechar);
2235 if (((!strchr(chan->custom_modes,modechar)) && (!mdir)) || ((strchr(chan->custom_modes,modechar)) && (mdir)))
2237 log(DEBUG,"Mode %c isnt set on %s but trying to remove!",modechar,chan->name);
2240 if (ModeDefined(modechar,MT_CHANNEL))
2242 log(DEBUG,"A module has claimed this mode");
2243 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) && (mdir))
2245 p.push_back(parameters[param]);
2247 if ((ModeDefinedOff(modechar,MT_CHANNEL)>0) && (!mdir))
2249 p.push_back(parameters[param]);
2251 bool handled = false;
2252 for (int i = 0; i <= MODCOUNT; i++)
2256 if (modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p))
2258 log(DEBUG,"OnExtendedMode returned nonzero for a module");
2259 char app[] = {modechar, 0};
2262 if ((modelist[ptr-1] == '+') || (modelist[ptr-1] == '-'))
2264 strcat(outlist, app);
2266 else if (!strchr(outlist,modechar))
2268 strcat(outlist, app);
2271 chan->SetCustomMode(modechar,mdir);
2272 // include parameters in output if mode has them
2273 if ((ModeDefinedOn(modechar,MT_CHANNEL)>0) || (ModeDefinedOff(modechar,MT_CHANNEL)>0))
2275 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
2276 strcpy(outpars[pc++],parameters[param++]);
2278 // break, because only one module can handle the mode.
2290 /* this ensures only the *valid* modes are sent out onto the network */
2291 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
2293 outlist[strlen(outlist)-1] = '\0';
2295 if (strcmp(outlist,""))
2297 strcpy(outstr,outlist);
2298 for (ptr = 0; ptr < pc; ptr++)
2301 strcat(outstr,outpars[ptr]);
2305 WriteChannelWithServ(ServerName,chan,user,"MODE %s %s",chan->name,outstr);
2309 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
2314 void handle_mode(char **parameters, int pcnt, userrec *user)
2320 char outpars[MAXBUF];
2322 dest = Find(parameters[0]);
2324 if ((dest) && (pcnt == 1))
2326 WriteServ(user->fd,"221 %s :+%s",user->nick,user->modes);
2329 if ((dest) && (pcnt > 1))
2334 if (strchr(user->modes,'o'))
2345 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
2349 strcpy(outpars,"+");
2352 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
2355 for (i = 0; i < strlen(parameters[1]); i++)
2357 if (parameters[1][i] == '+')
2361 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2363 outpars[strlen(outpars)-1] = '+';
2367 strcat(outpars,"+");
2373 if (parameters[1][i] == '-')
2377 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2379 outpars[strlen(outpars)-1] = '-';
2383 strcat(outpars,"-");
2391 if (strchr(user->modes,'o'))
2397 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's'))
2406 if (!strchr(dest->modes,parameters[1][i]))
2408 dest->modes[strlen(dest->modes)+1]='\0';
2409 dest->modes[strlen(dest->modes)] = parameters[1][i];
2410 outpars[strlen(outpars)+1]='\0';
2411 outpars[strlen(outpars)] = parameters[1][i];
2420 outpars[strlen(outpars)+1]='\0';
2421 outpars[strlen(outpars)] = parameters[1][i];
2424 for (q = 0; q < strlen(user->modes); q++)
2426 if (user->modes[q] != parameters[1][i])
2428 moo[0] = user->modes[q];
2433 strcpy(user->modes,temp);
2438 if (strlen(outpars))
2444 while (i < strlen (outpars))
2446 b[z++] = outpars[i++];
2448 if (i<strlen(outpars)-1)
2450 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2452 // someones playing silly buggers and trying
2453 // to put a +- or -+ into the line...
2457 if (i == strlen(outpars)-1)
2459 if ((outpars[i] == '-') || (outpars[i] == '+'))
2467 if ((b[z] == '-') || (b[z] == '+'))
2470 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2473 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2478 Ptr = FindChan(parameters[0]);
2483 /* just /modes #channel */
2484 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
2485 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
2491 if ((!strcmp(parameters[1],"+b")) || (!strcmp(parameters[1],"b")))
2494 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
2496 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
2498 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
2502 if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
2504 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, Ptr->name);
2508 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt,false);
2512 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2519 void server_mode(char **parameters, int pcnt, userrec *user)
2525 char outpars[MAXBUF];
2527 dest = Find(parameters[0]);
2529 if ((dest) && (pcnt > 1))
2531 strcpy(outpars,"+");
2534 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
2537 for (i = 0; i < strlen(parameters[1]); i++)
2539 if (parameters[1][i] == '+')
2543 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2545 outpars[strlen(outpars)-1] = '+';
2549 strcat(outpars,"+");
2555 if (parameters[1][i] == '-')
2559 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2561 outpars[strlen(outpars)-1] = '-';
2565 strcat(outpars,"-");
2573 if (strchr(user->modes,'o'))
2579 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's'))
2588 if (!strchr(dest->modes,parameters[1][i]))
2590 dest->modes[strlen(dest->modes)+1]='\0';
2591 dest->modes[strlen(dest->modes)] = parameters[1][i];
2592 outpars[strlen(outpars)+1]='\0';
2593 outpars[strlen(outpars)] = parameters[1][i];
2602 outpars[strlen(outpars)+1]='\0';
2603 outpars[strlen(outpars)] = parameters[1][i];
2606 for (q = 0; q < strlen(user->modes); q++)
2608 if (user->modes[q] != parameters[1][i])
2610 moo[0] = user->modes[q];
2615 strcpy(user->modes,temp);
2620 if (strlen(outpars))
2626 while (i < strlen (outpars))
2628 b[z++] = outpars[i++];
2630 if (i<strlen(outpars)-1)
2632 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2634 // someones playing silly buggers and trying
2635 // to put a +- or -+ into the line...
2639 if (i == strlen(outpars)-1)
2641 if ((outpars[i] == '-') || (outpars[i] == '+'))
2649 if ((b[z] == '-') || (b[z] == '+'))
2652 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2655 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2660 Ptr = FindChan(parameters[0]);
2663 process_modes(parameters,user,Ptr,STATUS_OP,pcnt,true);
2667 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2672 /* This function pokes and hacks at a parameter list like the following:
2674 * PART #winbot, #darkgalaxy :m00!
2676 * to turn it into a series of individual calls like this:
2678 * PART #winbot :m00!
2679 * PART #darkgalaxy :m00!
2681 * The seperate calls are sent to a callback function provided by the caller
2682 * (the caller will usually call itself recursively). The callback function
2683 * must be a command handler. Calling this function on a line with no list causes
2684 * no action to be taken. You must provide a starting and ending parameter number
2685 * where the range of the list can be found, useful if you have a terminating
2686 * parameter as above which is actually not part of the list, or parameters
2687 * before the actual list as well. This code is used by many functions which
2688 * can function as "one to list" (see the RFC) */
2690 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
2695 char blog[32][MAXBUF];
2696 char blog2[32][MAXBUF];
2697 int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
2698 char keystr[MAXBUF];
2701 for (i = 0; i <32; i++)
2704 for (i = 0; i <32; i++)
2705 strcpy(blog2[i],"");
2708 for (i = 0; i <10; i++)
2712 parameters[i] = moo;
2717 if (pcnt > 1) /* we have a key to copy */
2719 strcpy(keystr,parameters[1]);
2723 if (!parameters[start])
2727 if (!strchr(parameters[start],','))
2732 for (i = start; i <= end; i++)
2736 strcat(plist,parameters[i]);
2744 for (i = 0; i < t; i++)
2746 if (plist[i] == ',')
2749 strcpy(blog[j++],param);
2753 strcpy(blog[j++],param);
2756 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
2761 if ((joins) && (keystr))
2763 if (strchr(keystr,','))
2767 t2 = strlen(keystr);
2768 for (i = 0; i < t2; i++)
2770 if (keystr[i] == ',')
2773 strcpy(blog2[j++],param);
2777 strcpy(blog2[j++],param);
2782 for (j = 0; j < total; j++)
2788 for (q = end; q < pcnt-1; q++)
2790 if (parameters[q+1])
2792 pars[q-end+1] = parameters[q+1];
2795 if ((joins) && (parameters[1]))
2806 /* repeatedly call the function with the hacked parameter list */
2807 if ((joins) && (pcnt > 1))
2811 // pars[1] already set up and containing key from blog2[j]
2816 pars[1] = parameters[1];
2822 fn(pars,pcnt-(end-start),u);
2830 void handle_join(char **parameters, int pcnt, userrec *user)
2835 if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
2837 if (parameters[0][0] == '#')
2839 Ptr = add_channel(user,parameters[0],parameters[1]);
2844 void handle_part(char **parameters, int pcnt, userrec *user)
2850 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
2852 del_channel(user,parameters[0],parameters[1]);
2856 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
2858 del_channel(user,parameters[0],NULL);
2862 void handle_kick(char **parameters, int pcnt, userrec *user)
2864 chanrec* Ptr = FindChan(parameters[0]);
2865 userrec* u = Find(parameters[1]);
2869 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2873 if (!has_channel(u,Ptr))
2875 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
2881 char reason[MAXBUF];
2882 strncpy(reason,parameters[2],MAXBUF);
2883 if (strlen(reason)>MAXKICK)
2885 reason[MAXKICK-1] = '\0';
2888 kick_channel(user,u,Ptr,reason);
2892 kick_channel(user,u,Ptr,user->nick);
2897 void handle_die(char **parameters, int pcnt, userrec *user)
2899 log(DEBUG,"die: %s",user->nick);
2900 if (!strcmp(parameters[0],diepass))
2902 WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
2908 WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
2912 void handle_restart(char **parameters, int pcnt, userrec *user)
2914 log(DEBUG,"restart: %s",user->nick);
2915 if (!strcmp(parameters[0],restartpass))
2917 WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
2920 /* Will finish this later when i can be arsed :) */
2924 WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
2929 void kill_link(userrec *user,char* reason)
2931 user_hash::iterator iter = clientlist.find(user->nick);
2933 if (strlen(reason)>MAXQUIT)
2935 reason[MAXQUIT-1] = '\0';
2938 log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
2939 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
2940 log(DEBUG,"closing fd %d",user->fd);
2942 /* bugfix, cant close() a nonblocking socket (sux!) */
2943 if (user->registered == 7) {
2944 FOREACH_MOD OnUserQuit(user);
2945 WriteCommonExcept(user,"QUIT :%s",reason);
2948 /* push the socket on a stack of sockets due to be closed at the next opportunity */
2949 fd_reap.push_back(user->fd);
2951 bool do_purge = false;
2953 if (user->registered == 7) {
2954 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
2958 if (iter != clientlist.end())
2960 log(DEBUG,"deleting user hash value %d",iter->second);
2961 if ((iter->second) && (user->registered == 7)) {
2962 delete iter->second;
2964 clientlist.erase(iter);
2967 if (user->registered == 7) {
2968 purge_empty_chans();
2973 void handle_kill(char **parameters, int pcnt, userrec *user)
2975 userrec *u = Find(parameters[0]);
2976 char killreason[MAXBUF];
2978 log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
2981 WriteTo(user, u, "KILL %s :%s!%s!%s (%s)", u->nick, ServerName,user->dhost,user->nick,parameters[1]);
2982 // :Brain!brain@NetAdmin.chatspike.net KILL [Brain] :homer!NetAdmin.chatspike.net!Brain (test kill)
2983 WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
2984 sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
2985 kill_link(u,killreason);
2989 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2993 void handle_summon(char **parameters, int pcnt, userrec *user)
2995 WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
2998 void handle_users(char **parameters, int pcnt, userrec *user)
3000 WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
3004 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
3006 char* Passwd(userrec *user)
3008 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3010 if (match(user->host,i->host) && (i->type == CC_ALLOW))
3018 bool IsDenied(userrec *user)
3020 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
3022 if (match(user->host,i->host) && (i->type == CC_DENY))
3031 void handle_pass(char **parameters, int pcnt, userrec *user)
3033 if (!strcasecmp(parameters[0],Passwd(user)))
3035 user->haspassed = true;
3039 void handle_invite(char **parameters, int pcnt, userrec *user)
3041 userrec* u = Find(parameters[0]);
3042 chanrec* c = FindChan(parameters[1]);
3048 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[1]);
3054 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
3063 if (cstatus(user,c) < STATUS_HOP)
3065 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, c->name);
3069 u->InviteTo(c->name);
3070 WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
3071 WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
3075 void handle_topic(char **parameters, int pcnt, userrec *user)
3081 if (strlen(parameters[0]) <= CHANMAX)
3083 Ptr = FindChan(parameters[0]);
3088 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
3089 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
3093 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
3098 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3105 if (strlen(parameters[0]) <= CHANMAX)
3107 Ptr = FindChan(parameters[0]);
3110 if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
3112 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator", user->nick, Ptr->name);
3117 strncpy(topic,parameters[1],MAXBUF);
3118 if (strlen(topic)>MAXTOPIC)
3120 topic[MAXTOPIC-1] = '\0';
3123 strcpy(Ptr->topic,topic);
3124 strcpy(Ptr->setby,user->nick);
3125 Ptr->topicset = time(NULL);
3126 WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
3130 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3136 /* sends out an error notice to all connected clients (not to be used
3139 void send_error(char *s)
3141 log(DEBUG,"send_error: %s",s);
3142 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3144 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
3148 void Error(int status)
3150 signal (SIGALRM, SIG_IGN);
3151 signal (SIGPIPE, SIG_IGN);
3152 signal (SIGTERM, SIG_IGN);
3153 signal (SIGABRT, SIG_IGN);
3154 signal (SIGSEGV, SIG_IGN);
3155 signal (SIGURG, SIG_IGN);
3156 signal (SIGKILL, SIG_IGN);
3157 log(DEBUG,"*** fell down a pothole in the road to perfection ***");
3158 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
3162 int main (int argc, char *argv[])
3165 log(DEBUG,"*** InspIRCd starting up!");
3166 if (!FileExists(CONFIG_FILE))
3168 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
3169 log(DEBUG,"main: no config");
3170 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
3174 if (!strcmp(argv[1],"-nofork")) {
3178 if (InspIRCd() == ERROR)
3180 log(DEBUG,"main: daemon function bailed");
3181 printf("ERROR: could not initialise. Shutting down.\n");
3188 template<typename T> inline string ConvToStr(const T &in)
3191 if (!(tmp << in)) return string();
3195 /* re-allocates a nick in the user_hash after they change nicknames,
3196 * returns a pointer to the new user as it may have moved */
3198 userrec* ReHashNick(char* Old, char* New)
3200 user_hash::iterator newnick;
3201 user_hash::iterator oldnick = clientlist.find(Old);
3203 log(DEBUG,"ReHashNick: %s %s",Old,New);
3205 if (!strcasecmp(Old,New))
3207 log(DEBUG,"old nick is new nick, skipping");
3208 return oldnick->second;
3211 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
3213 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
3215 clientlist[New] = new userrec();
3216 clientlist[New] = oldnick->second;
3217 /*delete oldnick->second; */
3218 clientlist.erase(oldnick);
3220 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
3222 return clientlist[New];
3225 /* adds or updates an entry in the whowas list */
3226 void AddWhoWas(userrec* u)
3228 user_hash::iterator iter = whowas.find(u->nick);
3229 userrec *a = new userrec();
3230 strcpy(a->nick,u->nick);
3231 strcpy(a->ident,u->ident);
3232 strcpy(a->dhost,u->dhost);
3233 strcpy(a->host,u->host);
3234 strcpy(a->fullname,u->fullname);
3235 strcpy(a->server,u->server);
3236 a->signon = u->signon;
3238 /* MAX_WHOWAS: max number of /WHOWAS items
3239 * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
3240 * can be replaced by a newer one
3243 if (iter == whowas.end())
3245 if (whowas.size() == WHOWAS_MAX)
3247 for (user_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
3249 // 3600 seconds in an hour ;)
3250 if ((i->second->signon)<(time(NULL)-(WHOWAS_STALE*3600)))
3254 log(DEBUG,"added WHOWAS entry, purged an old record");
3261 log(DEBUG,"added fresh WHOWAS entry");
3262 whowas[a->nick] = a;
3267 log(DEBUG,"updated WHOWAS entry");
3268 delete iter->second;
3274 /* add a client connection to the sockets list */
3275 void AddClient(int socket, char* host, int port, bool iscached)
3279 char resolved[MAXBUF];
3282 user_hash::iterator iter;
3284 tempnick = ConvToStr(socket) + "-unknown";
3285 sprintf(tn2,"%d-unknown",socket);
3287 iter = clientlist.find(tempnick);
3289 if (iter != clientlist.end()) return;
3292 * It is OK to access the value here this way since we know
3293 * it exists, we just created it above.
3295 * At NO other time should you access a value in a map or a
3296 * hash_map this way.
3298 clientlist[tempnick] = new userrec();
3300 NonBlocking(socket);
3301 log(DEBUG,"AddClient: %d %s %d",socket,host,port);
3304 clientlist[tempnick]->fd = socket;
3305 strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
3306 strncpy(clientlist[tempnick]->host, host,160);
3307 strncpy(clientlist[tempnick]->dhost, host,160);
3308 strncpy(clientlist[tempnick]->server, ServerName,256);
3309 clientlist[tempnick]->registered = 0;
3310 clientlist[tempnick]->signon = time(NULL);
3311 clientlist[tempnick]->nping = time(NULL)+240;
3312 clientlist[tempnick]->lastping = 1;
3313 clientlist[tempnick]->port = port;
3317 WriteServ(socket,"NOTICE Auth :Found your hostname (cached)...");
3321 WriteServ(socket,"NOTICE Auth :Looking up your hostname...");
3324 if (clientlist.size() == MAXCLIENTS)
3325 kill_link(clientlist[tempnick],"No more connections allowed in this class");
3328 void handle_names(char **parameters, int pcnt, userrec *user)
3332 if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
3334 c = FindChan(parameters[0]);
3337 /*WriteServ(user->fd,"353 %s = %s :%s", user->nick, c->name,*/
3339 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
3343 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3348 void handle_privmsg(char **parameters, int pcnt, userrec *user)
3353 if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
3355 if (parameters[0][0] == '#')
3357 chan = FindChan(parameters[0]);
3360 if ((chan->noexternal) && (!has_channel(user,chan)))
3362 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
3365 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
3367 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
3370 ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
3374 /* no such nick/channel */
3375 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3380 dest = Find(parameters[0]);
3383 if (strcmp(dest->awaymsg,""))
3385 /* auto respond with aweh msg */
3386 WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
3388 WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
3392 /* no such nick/channel */
3393 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3397 void handle_notice(char **parameters, int pcnt, userrec *user)
3402 if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
3404 if (parameters[0][0] == '#')
3406 chan = FindChan(parameters[0]);
3409 if ((chan->noexternal) && (!has_channel(user,chan)))
3411 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
3414 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
3416 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
3419 WriteChannel(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
3423 /* no such nick/channel */
3424 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3429 dest = Find(parameters[0]);
3432 WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
3436 /* no such nick/channel */
3437 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3443 char* chlist(userrec *user)
3448 log(DEBUG,"chlist: %s",user->nick);
3454 for (i = 0; i != MAXCHANS; i++)
3456 if (user->chans[i].channel != NULL)
3458 if (user->chans[i].channel->name)
3460 strcpy(cmp,user->chans[i].channel->name);
3462 if (!strstr(lst,cmp))
3464 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
3466 strcat(lst,cmode(user,user->chans[i].channel));
3467 strcat(lst,user->chans[i].channel->name);
3477 void handle_info(char **parameters, int pcnt, userrec *user)
3479 WriteServ(user->fd,"371 %s :The Inspire IRCd Project Has been brought to you by the following people..",user->nick);
3480 WriteServ(user->fd,"371 %s :Craig Edwards, Craig McLure, and Others..",user->nick);
3481 WriteServ(user->fd,"371 %s :Will finish this later when i can be arsed :p",user->nick);
3482 WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
3485 void handle_time(char **parameters, int pcnt, userrec *user)
3488 struct tm * timeinfo;
3491 timeinfo = localtime ( &rawtime );
3492 WriteServ(user->fd,"391 %s %s :%s",user->nick,ServerName, asctime (timeinfo) );
3496 void handle_whois(char **parameters, int pcnt, userrec *user)
3501 if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
3503 dest = Find(parameters[0]);
3506 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
3507 if ((user == dest) || (strchr(user->modes,'o')))
3509 WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
3511 if (strcmp(chlist(dest),""))
3513 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
3515 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
3516 if (strcmp(dest->awaymsg,""))
3518 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
3520 if (strchr(dest->modes,'o'))
3522 WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
3524 //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
3525 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
3527 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
3531 /* no such nick/channel */
3532 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3536 void handle_quit(char **parameters, int pcnt, userrec *user)
3538 user_hash::iterator iter = clientlist.find(user->nick);
3541 if (user->registered == 7)
3543 /* theres more to do here, but for now just close the socket */
3546 if (parameters[0][0] == ':')
3550 reason = parameters[0];
3552 if (strlen(reason)>MAXQUIT)
3554 reason[MAXQUIT-1] = '\0';
3557 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
3558 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
3559 WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
3563 Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
3564 WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
3565 WriteCommonExcept(user,"QUIT :Client exited");
3567 FOREACH_MOD OnUserQuit(user);
3571 /* push the socket on a stack of sockets due to be closed at the next opportunity */
3572 fd_reap.push_back(user->fd);
3574 if (iter != clientlist.end())
3576 log(DEBUG,"deleting user hash value %d",iter->second);
3577 if ((iter->second) && (user->registered == 7)) {
3578 delete iter->second;
3580 clientlist.erase(iter);
3583 if (user->registered == 7) {
3584 purge_empty_chans();
3588 void handle_who(char **parameters, int pcnt, userrec *user)
3590 chanrec* Ptr = NULL;
3592 /* theres more to do here, but for now just close the socket */
3595 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
3597 if (user->chans[0].channel)
3599 Ptr = user->chans[0].channel;
3600 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3602 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
3604 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);
3610 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3614 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, user->nick);
3618 if (parameters[0][0] = '#')
3620 Ptr = FindChan(parameters[0]);
3623 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3625 if ((has_channel(i->second,Ptr)) && (isnick(i->second->nick)))
3627 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);
3630 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3634 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3640 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
3642 Ptr = user->chans[0].channel;
3643 printf(user->chans[0].channel->name);
3644 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3646 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
3648 if (strchr(i->second->modes,'o'))
3650 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);
3654 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3660 void handle_wallops(char **parameters, int pcnt, userrec *user)
3662 WriteWallOps(user,"%s",parameters[0]);
3665 void handle_list(char **parameters, int pcnt, userrec *user)
3669 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
3670 for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
3672 if ((!i->second->c_private) && (!i->second->secret))
3674 WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
3677 WriteServ(user->fd,"323 %s :End of channel list.",user->nick);
3681 void handle_rehash(char **parameters, int pcnt, userrec *user)
3683 WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CONFIG_FILE);
3685 FOREACH_MOD OnRehash();
3686 WriteOpers("%s is rehashing config file %s",user->nick,CONFIG_FILE);
3692 return clientlist.size();
3695 int usercount_invisible(void)
3699 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3701 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
3706 int usercount_opers(void)
3710 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3712 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
3717 int usercount_unknown(void)
3721 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3723 if ((i->second->fd) && (i->second->registered != 7))
3731 return chanlist.size();
3734 int servercount(void)
3739 void handle_lusers(char **parameters, int pcnt, userrec *user)
3741 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),servercount());
3742 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
3743 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
3744 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
3745 WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,usercnt());
3748 void handle_admin(char **parameters, int pcnt, userrec *user)
3750 WriteServ(user->fd,"256 %s :Administrative info for %s",user->nick,ServerName);
3751 WriteServ(user->fd,"257 %s :Name - %s",user->nick,AdminName);
3752 WriteServ(user->fd,"258 %s :Nickname - %s",user->nick,AdminNick);
3753 WriteServ(user->fd,"258 %s :E-Mail - %s",user->nick,AdminEmail);
3756 void ShowMOTD(userrec *user)
3760 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
3763 WriteServ(user->fd,"375 %s :- %s message of the day",user->nick,ServerName);
3764 for (int i = 0; i != MOTD.size(); i++)
3766 WriteServ(user->fd,"372 %s :- %s",user->nick,MOTD[i].c_str());
3768 WriteServ(user->fd,"376 %s :End of %s message of the day.",user->nick,ServerName);
3771 void ShowRULES(userrec *user)
3775 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
3778 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
3779 for (int i = 0; i != RULES.size(); i++)
3781 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
3783 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
3786 /* shows the message of the day, and any other on-logon stuff */
3787 void ConnectUser(userrec *user)
3789 user->registered = 7;
3790 user->idle_lastmsg = time(NULL);
3791 log(DEBUG,"ConnectUser: %s",user->nick);
3793 if (strcmp(Passwd(user),"") && (!user->haspassed))
3795 Write(user->fd,"ERROR :Closing link: Invalid password");
3796 kill_link(user,"Invalid password");
3801 Write(user->fd,"ERROR :Closing link: Unauthorized connection");
3802 kill_link(user,"Unauthorised connection");
3805 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
3806 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
3807 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
3808 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
3809 WriteServ(user->fd,"004 %s :%s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
3810 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);
3811 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);
3813 FOREACH_MOD OnUserConnect(user);
3814 WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
3817 void handle_version(char **parameters, int pcnt, userrec *user)
3819 WriteServ(user->fd,"351 %s :%s %s %s :%s",user->nick,VERSION,"$Id$",ServerName,SYSTEM);
3822 void handle_ping(char **parameters, int pcnt, userrec *user)
3824 WriteServ(user->fd,"PONG %s :%s",ServerName,parameters[0]);
3827 void handle_pong(char **parameters, int pcnt, userrec *user)
3829 // set the user as alive so they survive to next ping
3833 void handle_motd(char **parameters, int pcnt, userrec *user)
3838 void handle_rules(char **parameters, int pcnt, userrec *user)
3843 void handle_user(char **parameters, int pcnt, userrec *user)
3845 if (user->registered < 3)
3847 if (isident(parameters[0]) == 0) {
3848 // This kinda Sucks, According to the RFC thou, its either this,
3849 // or "You have already registered" :p -- Craig
3850 WriteServ(user->fd,"461 %s USER :Not enough parameters",user->nick);
3853 WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
3854 strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
3855 strncat(user->ident,parameters[0],IDENTMAX);
3856 strncpy(user->fullname,parameters[3],128);
3857 user->registered = (user->registered | 1);
3862 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
3865 /* parameters 2 and 3 are local and remote hosts, ignored when sent by client connection */
3866 if (user->registered == 3)
3868 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
3873 void handle_userhost(char **parameters, int pcnt, userrec *user)
3875 char Return[MAXBUF],junk[MAXBUF];
3876 sprintf(Return,"302 %s :",user->nick);
3877 for (int i = 0; i < pcnt; i++)
3879 userrec *u = Find(parameters[i]);
3882 if (strchr(u->modes,'o'))
3884 sprintf(junk,"%s*=+%s@%s ",u->nick,u->ident,u->host);
3885 strcat(Return,junk);
3889 sprintf(junk,"%s=+%s@%s ",u->nick,u->ident,u->host);
3890 strcat(Return,junk);
3894 WriteServ(user->fd,Return);
3898 void handle_ison(char **parameters, int pcnt, userrec *user)
3900 char Return[MAXBUF];
3901 sprintf(Return,"303 %s :",user->nick);
3902 for (int i = 0; i < pcnt; i++)
3904 userrec *u = Find(parameters[i]);
3907 strcat(Return,u->nick);
3911 WriteServ(user->fd,Return);
3915 void handle_away(char **parameters, int pcnt, userrec *user)
3919 strcpy(user->awaymsg,parameters[0]);
3920 WriteServ(user->fd,"306 %s :You have been marked as being away",user->nick);
3924 strcpy(user->awaymsg,"");
3925 WriteServ(user->fd,"305 %s :You are no longer marked as being away",user->nick);
3929 void handle_whowas(char **parameters, int pcnt, userrec* user)
3931 user_hash::iterator i = whowas.find(parameters[0]);
3933 if (i == whowas.end())
3935 WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
3936 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3940 time_t rawtime = i->second->signon;
3944 timeinfo = localtime(&rawtime);
3945 strcpy(b,asctime(timeinfo));
3946 b[strlen(b)-1] = '\0';
3948 WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
3949 WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
3950 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3955 void handle_trace(char **parameters, int pcnt, userrec *user)
3957 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
3961 if (isnick(i->second->nick))
3963 if (strchr(i->second->modes,'o'))
3965 WriteServ(user->fd,"205 %s :Oper 0 %s",user->nick,i->second->nick);
3969 WriteServ(user->fd,"204 %s :User 0 %s",user->nick,i->second->nick);
3974 WriteServ(user->fd,"203 %s :???? 0 [%s]",user->nick,i->second->host);
3980 void handle_modules(char **parameters, int pcnt, userrec *user)
3982 for (int i = 0; i < module_names.size(); i++)
3984 Version V = modules[i]->GetVersion();
3985 WriteServ(user->fd,"900 0x%08lx %d.%d.%d.%d :%s",modules[i],V.Major,V.Minor,V.Revision,V.Build,module_names[i].c_str());
3989 void handle_stats(char **parameters, int pcnt, userrec *user)
3995 if (strlen(parameters[0])>1)
3997 /* make the stats query 1 character long */
3998 parameters[0][1] = '\0';
4001 /* stats m (list number of times each command has been used, plus bytecount) */
4002 if (!strcasecmp(parameters[0],"m"))
4004 for (int i = 0; i < cmdlist.size(); i++)
4006 if (cmdlist[i].handler_function)
4008 if (cmdlist[i].use_count)
4010 /* RPL_STATSCOMMANDS */
4011 WriteServ(user->fd,"212 %s %s %d %d",user->nick,cmdlist[i].command,cmdlist[i].use_count,cmdlist[i].total_bytes);
4018 /* stats z (debug and memory info) */
4019 if (!strcasecmp(parameters[0],"z"))
4021 WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
4022 WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
4023 WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
4024 WriteServ(user->fd,"249 %s :MOTD(VECTOR) %d, RULES(VECTOR) %d",user->nick,MOTD.size(),RULES.size());
4025 WriteServ(user->fd,"249 %s :address_cache(HASH_MAP) %d (%d buckets)",user->nick,IP.size(),IP.bucket_count());
4026 WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
4027 WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
4028 WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
4032 if (!strcasecmp(parameters[0],"o"))
4034 for (int i = 0; i < ConfValueEnum("oper"); i++)
4036 char LoginName[MAXBUF];
4037 char HostName[MAXBUF];
4038 char OperType[MAXBUF];
4039 ConfValue("oper","name",i,LoginName);
4040 ConfValue("oper","host",i,HostName);
4041 ConfValue("oper","type",i,OperType);
4042 WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
4046 /* stats l (show user I/O stats) */
4047 if (!strcasecmp(parameters[0],"l"))
4049 WriteServ(user->fd,"211 %s :server:port nick bytes_in cmds_in bytes_out cmds_out",user->nick);
4050 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
4052 if (isnick(i->second->nick))
4054 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);
4058 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);
4064 /* stats u (show server uptime) */
4065 if (!strcasecmp(parameters[0],"u"))
4067 time_t current_time = 0;
4068 current_time = time(NULL);
4069 time_t server_uptime = current_time - startup_time;
4071 stime = gmtime(&server_uptime);
4072 /* i dont know who the hell would have an ircd running for over a year nonstop, but
4073 * Craig suggested this, and it seemed a good idea so in it went */
4074 if (stime->tm_year > 70)
4076 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);
4080 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);
4084 WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
4085 WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
4089 void handle_connect(char **parameters, int pcnt, userrec *user)
4091 char Link_ServerName[1024];
4092 char Link_IPAddr[1024];
4093 char Link_Port[1024];
4094 char Link_Pass[1024];
4098 for (int i = 0; i < ConfValueEnum("link"); i++)
4100 ConfValue("link","name",i,Link_ServerName);
4101 ConfValue("link","ipaddr",i,Link_IPAddr);
4102 ConfValue("link","port",i,Link_Port);
4103 ConfValue("link","sendpass",i,Link_Pass);
4104 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
4105 LinkPort = atoi(Link_Port);
4106 if (match(Link_ServerName,parameters[0])) {
4112 WriteServ(user->fd,"NOTICE %s :*** Failed to connect to %s: No servers matching this pattern are configured for linking.",user->nick,parameters[0]);
4116 // TODO: Perform a check here to stop a server being linked twice!
4118 WriteServ(user->fd,"NOTICE %s :*** Connecting to %s (%s) port %s...",user->nick,Link_ServerName,Link_IPAddr,Link_Port);
4120 if (me[defaultRoute])
4123 // at this point parameters[0] is an ip in a string.
4124 // TODO: Look this up from the <link> blocks instead!
4125 for (int j = 0; j < 255; j++) {
4126 if (servers[j] == NULL) {
4127 servers[j] = new serverrec;
4128 strcpy(servers[j]->internal_addr,Link_IPAddr);
4129 strcpy(servers[j]->name,Link_ServerName);
4130 log(DEBUG,"Allocated new serverrec");
4131 if (!me[defaultRoute]->BeginLink(Link_IPAddr,LinkPort,Link_Pass))
4133 WriteServ(user->fd,"NOTICE %s :*** Failed to send auth packet to %s!",user->nick,Link_IPAddr);
4138 WriteServ(user->fd,"NOTICE %s :*** Failed to create server record for address %s!",user->nick,Link_IPAddr);
4142 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);
4146 void handle_squit(char **parameters, int pcnt, userrec *user)
4148 // send out an squit across the mesh and then clear the server list (for local squit)
4151 void handle_oper(char **parameters, int pcnt, userrec *user)
4153 char LoginName[MAXBUF];
4154 char Password[MAXBUF];
4155 char OperType[MAXBUF];
4156 char TypeName[MAXBUF];
4157 char Hostname[MAXBUF];
4160 for (i = 0; i < ConfValueEnum("oper"); i++)
4162 ConfValue("oper","name",i,LoginName);
4163 ConfValue("oper","password",i,Password);
4164 if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
4166 /* correct oper credentials */
4167 ConfValue("oper","type",i,OperType);
4168 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
4169 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
4170 WriteServ(user->fd,"MODE %s :+o",user->nick);
4171 for (j =0; j < ConfValueEnum("type"); j++)
4173 ConfValue("type","name",j,TypeName);
4174 if (!strcmp(TypeName,OperType))
4176 /* found this oper's opertype */
4177 ConfValue("type","host",j,Hostname);
4178 strncpy(user->dhost,Hostname,256);
4181 if (!strchr(user->modes,'o'))
4183 strcat(user->modes,"o");
4189 WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
4190 WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
4193 void handle_nick(char **parameters, int pcnt, userrec *user)
4197 log(DEBUG,"not enough params for handle_nick");
4202 log(DEBUG,"invalid parameter passed to handle_nick");
4205 if (!strlen(parameters[0]))
4207 log(DEBUG,"zero length new nick passed to handle_nick");
4212 log(DEBUG,"invalid user passed to handle_nick");
4217 log(DEBUG,"invalid old nick passed to handle_nick");
4220 if (!strcasecmp(user->nick,parameters[0]))
4222 log(DEBUG,"old nick is new nick, skipping");
4227 if (strlen(parameters[0]) > 1)
4229 if (parameters[0][0] == ':')
4234 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
4236 WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
4240 if (isnick(parameters[0]) == 0)
4242 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
4246 if (user->registered == 7)
4248 WriteCommon(user,"NICK %s",parameters[0]);
4251 /* change the nick of the user in the users_hash */
4252 user = ReHashNick(user->nick, parameters[0]);
4253 /* actually change the nick within the record */
4255 if (!user->nick) return;
4257 strncpy(user->nick, parameters[0],NICKMAX);
4259 log(DEBUG,"new nick set: %s",user->nick);
4261 if (user->registered < 3)
4262 user->registered = (user->registered | 2);
4263 if (user->registered == 3)
4265 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
4268 log(DEBUG,"exit nickchange: %s",user->nick);
4271 int process_parameters(char **command_p,char *parameters)
4276 q = strlen(parameters);
4279 /* no parameters, command_p invalid! */
4282 if (parameters[0] == ':')
4284 command_p[0] = parameters+1;
4289 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
4291 /* only one parameter */
4292 command_p[0] = parameters;
4293 if (parameters[0] == ':')
4295 if (strchr(parameters,' ') != NULL)
4303 command_p[j++] = parameters;
4304 for (i = 0; i <= q; i++)
4306 if (parameters[i] == ' ')
4308 command_p[j++] = parameters+i+1;
4309 parameters[i] = '\0';
4310 if (command_p[j-1][0] == ':')
4312 *command_p[j-1]++; /* remove dodgy ":" */
4314 /* parameter like this marks end of the sequence */
4318 return j; /* returns total number of items in the list */
4321 void process_command(userrec *user, char* cmd)
4325 char *command_p[127];
4326 char p[MAXBUF], temp[MAXBUF];
4327 int i, j, items, cmd_found;
4329 for (int i = 0; i < 127; i++)
4330 command_p[i] = NULL;
4340 if (!strcmp(cmd,""))
4347 FOREACH_MOD OnServerRaw(tmp,true);
4348 const char* cmd2 = tmp.c_str();
4349 snprintf(cmd,512,"%s",cmd2);
4351 if (!strchr(cmd,' '))
4353 /* no parameters, lets skip the formalities and not chop up
4356 command_p[0] = NULL;
4358 for (int i = 0; i <= strlen(cmd); i++)
4360 cmd[i] = toupper(cmd[i]);
4367 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
4368 for (i = 0; i < strlen(temp); i++)
4370 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
4376 /* split the full string into a command plus parameters */
4380 if (strchr(cmd,' '))
4382 for (i = 0; i <= strlen(cmd); i++)
4384 /* capitalise the command ONLY, leave params intact */
4385 cmd[i] = toupper(cmd[i]);
4386 /* are we nearly there yet?! :P */
4390 parameters = cmd+i+1;
4398 for (i = 0; i <= strlen(cmd); i++)
4400 cmd[i] = toupper(cmd[i]);
4408 if (strlen(command)>MAXCOMMAND)
4410 command[MAXCOMMAND-1] = '\0';
4411 WriteOpers("Possible command-flood from %s, sending excessively long commands.",user->nick);
4414 for (i = 0; i != cmdlist.size(); i++)
4416 if (strcmp(cmdlist[i].command,""))
4418 if (!strcmp(command, cmdlist[i].command))
4422 if (strcmp(parameters,""))
4424 items = process_parameters(command_p,parameters);
4429 command_p[0] = NULL;
4435 command_p[0] = NULL;
4440 user->idle_lastmsg = time(NULL);
4441 /* activity resets the ping pending timer */
4442 user->nping = time(NULL) + 120;
4443 if ((items) < cmdlist[i].min_params)
4445 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
4446 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
4449 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
4451 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
4452 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
4456 /* if the command isnt USER, PASS, or NICK, and nick is empty,
4458 if ((strcmp(command,"USER")) && (strcmp(command,"NICK")) && (strcmp(command,"PASS")))
4460 if ((!isnick(user->nick)) || (user->registered != 7))
4462 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
4463 WriteServ(user->fd,"451 %s :You have not registered",command);
4467 if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
4469 log(DEBUG,"process_command: handler: %s %s %d",user->nick,command,items);
4470 if (cmdlist[i].handler_function)
4472 /* ikky /stats counters */
4477 user->bytes_in += strlen(temp);
4480 cmdlist[i].use_count++;
4481 cmdlist[i].total_bytes+=strlen(temp);
4484 /* WARNING: nothing may come after the
4485 * command handler call, as the handler
4486 * may free the user structure! */
4488 cmdlist[i].handler_function(command_p,items,user);
4494 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
4495 WriteServ(user->fd,"451 %s :You have not registered",command);
4503 if ((!cmd_found) && (user))
4505 log(DEBUG,"process_command: not in table: %s %s",user->nick,command);
4506 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
4511 void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
4514 /* create the command and push it onto the table */
4515 strcpy(comm.command,cmd);
4516 comm.handler_function = f;
4517 comm.flags_needed = flags;
4518 comm.min_params = minparams;
4520 comm.total_bytes = 0;
4521 cmdlist.push_back(comm);
4522 log(DEBUG,"Added command %s (%d parameters)",cmd,minparams);
4525 void SetupCommandTable(void)
4527 createcommand("USER",handle_user,0,4);
4528 createcommand("NICK",handle_nick,0,1);
4529 createcommand("QUIT",handle_quit,0,0);
4530 createcommand("VERSION",handle_version,0,0);
4531 createcommand("PING",handle_ping,0,1);
4532 createcommand("PONG",handle_pong,0,1);
4533 createcommand("ADMIN",handle_admin,0,0);
4534 createcommand("PRIVMSG",handle_privmsg,0,2);
4535 createcommand("INFO",handle_info,0,0);
4536 createcommand("TIME",handle_time,0,0);
4537 createcommand("WHOIS",handle_whois,0,1);
4538 createcommand("WALLOPS",handle_wallops,'o',1);
4539 createcommand("NOTICE",handle_notice,0,2);
4540 createcommand("JOIN",handle_join,0,1);
4541 createcommand("NAMES",handle_names,0,1);
4542 createcommand("PART",handle_part,0,1);
4543 createcommand("KICK",handle_kick,0,2);
4544 createcommand("MODE",handle_mode,0,1);
4545 createcommand("TOPIC",handle_topic,0,1);
4546 createcommand("WHO",handle_who,0,1);
4547 createcommand("MOTD",handle_motd,0,0);
4548 createcommand("RULES",handle_join,0,0);
4549 createcommand("OPER",handle_oper,0,2);
4550 createcommand("LIST",handle_list,0,0);
4551 createcommand("DIE",handle_die,'o',1);
4552 createcommand("RESTART",handle_restart,'o',1);
4553 createcommand("KILL",handle_kill,'o',2);
4554 createcommand("REHASH",handle_rehash,'o',0);
4555 createcommand("LUSERS",handle_lusers,0,0);
4556 createcommand("STATS",handle_stats,0,1);
4557 createcommand("USERHOST",handle_userhost,0,1);
4558 createcommand("AWAY",handle_away,0,0);
4559 createcommand("ISON",handle_ison,0,0);
4560 createcommand("SUMMON",handle_summon,0,0);
4561 createcommand("USERS",handle_users,0,0);
4562 createcommand("INVITE",handle_invite,0,2);
4563 createcommand("PASS",handle_pass,0,1);
4564 createcommand("TRACE",handle_trace,'o',0);
4565 createcommand("WHOWAS",handle_whowas,0,1);
4566 createcommand("CONNECT",handle_connect,'o',1);
4567 createcommand("SQUIT",handle_squit,'o',1);
4568 createcommand("MODULES",handle_modules,'o',0);
4571 void process_buffer(userrec *user)
4575 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
4582 log(DEFAULT,"*** BUG *** process_buffer was given an invalid parameter");
4585 if (!strcmp(user->inbuf,""))
4589 strncpy(cmd,user->inbuf,MAXBUF);
4590 if (!strcmp(cmd,""))
4594 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
4596 cmd[strlen(cmd)-1] = '\0';
4598 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
4600 cmd[strlen(cmd)-1] = '\0';
4602 strcpy(user->inbuf,"");
4603 if (!strcmp(cmd,""))
4607 log(DEBUG,"InspIRCd: processing: %s %s",user->nick,cmd);
4611 process_command(user,cmd);
4615 void process_restricted_commands(char token,char* params,serverrec* source,serverrec* reply, char* udp_host,int udp_port)
4617 WriteOpers("Secure-UDP-Channel: Token='%c', Params='%s'",token,params);
4621 void handle_link_packet(long theirkey, char* udp_msg, char* udp_host, int udp_port, serverrec *serv)
4623 char response[10240];
4624 char token = udp_msg[0];
4625 char* params = udp_msg + 2;
4626 char finalparam[1024];
4627 strcpy(finalparam," :xxxx");
4628 if (strstr(params," :")) {
4629 strncpy(finalparam,strstr(params," :"),1024);
4632 // S test.chatspike.net password :ChatSpike InspIRCd test server
4633 char* servername = strtok(params," ");
4634 char* password = strtok(NULL," ");
4635 char* serverdesc = finalparam+2;
4636 WriteOpers("CONNECT from %s (%s)",servername,udp_host,password,serverdesc);
4639 char Link_ServerName[1024];
4640 char Link_IPAddr[1024];
4641 char Link_Port[1024];
4642 char Link_Pass[1024];
4643 char Link_SendPass[1024];
4646 // search for a corresponding <link> block in the config files
4647 for (int i = 0; i < ConfValueEnum("link"); i++)
4649 ConfValue("link","name",i,Link_ServerName);
4650 ConfValue("link","ipaddr",i,Link_IPAddr);
4651 ConfValue("link","port",i,Link_Port);
4652 ConfValue("link","recvpass",i,Link_Pass);
4653 ConfValue("link","sendpass",i,Link_SendPass);
4654 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
4655 LinkPort = atoi(Link_Port);
4656 if (!strcasecmp(Link_ServerName,servername)) {
4657 if (!strcasecmp(Link_IPAddr,udp_host)) {
4658 if (LinkPort == udp_port) {
4659 // we have a matching link line -
4660 // send a 'diminutive' server message back...
4661 snprintf(response,10240,"s %s %s :%s",ServerName,Link_SendPass,ServerDesc);
4662 serv->SendPacket(response,udp_host,udp_port,0);
4663 WriteOpers("CONNECT from %s accepted, authenticating",servername);
4664 for (int j = 0; j < 255; j++) {
4665 if (servers[j] == NULL) {
4666 servers[j] = new serverrec;
4667 strcpy(servers[j]->internal_addr,udp_host);
4668 strcpy(servers[j]->name,servername);
4669 // create a server record for this server
4670 snprintf(response,10240,"O %d",MyKey);
4671 serv->SendPacket(response,udp_host,udp_port,0);
4675 WriteOpers("Internal error connecting to %s, failed to create server record!",servername);
4679 log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
4683 log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
4687 log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
4690 serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
4691 WriteOpers("CONNECT from %s denied, no matching link block",servername);
4696 // if this is received, this means the server-ip that sent it said "OK" to credentials.
4697 // only when a server says this do we exchange keys. The server MUST have an entry in the servers
4698 // array, which is only added by an 'S' packet or BeginLink().
4699 for (int i = 0; i < 255; i++) {
4700 if (servers[i] != NULL) {
4701 if (!strcasecmp(servers[i]->internal_addr,udp_host)) {
4702 servers[i]->key = atoi(params);
4703 log(DEBUG,"Key for this server is now %d",servers[i]->key);
4704 serv->SendPacket("Z blah blah",udp_host,udp_port,MyKey);
4709 WriteOpers("\2WARNING!\2 Server ip %s attempted a key exchange, but is not in the authentication state! Possible intrusion attempt!",udp_host);
4713 // S test.chatspike.net password :ChatSpike InspIRCd test server
4714 char* servername = strtok(params," ");
4715 char* password = strtok(NULL," ");
4716 char* serverdesc = finalparam+2;
4718 // TODO: we should do a check here to ensure that this server is one we recently initiated a
4719 // link with, and didnt hear an 's' or 'E' back from yet (these are the only two valid responses
4720 // to an 'S' command. If we didn't recently send an 'S' to this server, theyre trying to spoof
4721 // a connect, so put out an oper alert!
4726 // for now, just accept all, we'll fix that later.
4727 WriteOpers("%s accepted our link credentials ",servername);
4729 char Link_ServerName[1024];
4730 char Link_IPAddr[1024];
4731 char Link_Port[1024];
4732 char Link_Pass[1024];
4733 char Link_SendPass[1024];
4736 // search for a corresponding <link> block in the config files
4737 for (int i = 0; i < ConfValueEnum("link"); i++)
4739 ConfValue("link","name",i,Link_ServerName);
4740 ConfValue("link","ipaddr",i,Link_IPAddr);
4741 ConfValue("link","port",i,Link_Port);
4742 ConfValue("link","recvpass",i,Link_Pass);
4743 ConfValue("link","sendpass",i,Link_SendPass);
4744 log(DEBUG,"(%d) Comparing against name='%s', ipaddr='%s', port='%s', recvpass='%s'",i,Link_ServerName,Link_IPAddr,Link_Port,Link_Pass);
4745 LinkPort = atoi(Link_Port);
4746 if (!strcasecmp(Link_ServerName,servername)) {
4747 if (!strcasecmp(Link_IPAddr,udp_host)) {
4748 if (LinkPort == udp_port) {
4749 // matching link at this end too, we're all done!
4750 // at this point we must begin key exchange and insert this
4751 // server into our 'active' table.
4752 for (int j = 0; j < 255; j++) {
4753 if (servers[j] != NULL) {
4754 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
4755 WriteOpers("Server %s authenticated, exchanging server keys...",servername);
4756 snprintf(response,10240,"O %d",MyKey);
4757 serv->SendPacket(response,udp_host,udp_port,0);
4762 WriteOpers("\2WARNING!\2 %s sent us an authentication packet but we are not authenticating with this server right noe! Possible intrusion attempt!",udp_host);
4767 log(DEBUG,"Port numbers '%d' and '%d' don't match",LinkPort,udp_port);
4771 log(DEBUG,"IP Addresses '%s' and '%s' don't match",Link_IPAddr,udp_host);
4775 log(DEBUG,"Server names '%s' and '%s' don't match",Link_ServerName,servername);
4778 serv->SendPacket("E :Access is denied (no matching link block)",udp_host,udp_port,0);
4779 WriteOpers("CONNECT from %s denied, no matching link block",servername);
4784 char* error_message = finalparam+2;
4785 WriteOpers("ERROR from %s: %s",udp_host,error_message);
4786 // remove this server from any lists
4787 for (int j = 0; j < 255; j++) {
4788 if (servers[j] != NULL) {
4789 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
4799 serverrec* source_server = NULL;
4801 for (int j = 0; j < 255; j++) {
4802 if (servers[j] != NULL) {
4803 if (!strcasecmp(servers[j]->internal_addr,udp_host)) {
4804 if (servers[j]->key == theirkey) {
4805 // found a valid key for this server, can process restricted stuff here
4806 process_restricted_commands(token,params,servers[j],serv,udp_host,udp_port);
4814 log(DEBUG,"Unrecognised token or unauthenticated host in datagram from %s:%d: %c",udp_host,udp_port,token);
4818 int reap_counter = 0;
4822 struct sockaddr_in client, server;
4823 char addrs[MAXBUF][255];
4824 int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
4826 int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
4827 int selectResult = 0;
4828 char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
4829 char resolvedHost[MAXBUF];
4833 log_file = fopen("ircd.log","a+");
4836 printf("ERROR: Could not write to logfile ircd.log, bailing!\n\n");
4840 log(DEBUG,"InspIRCd: startup: begin");
4844 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
4846 log(DEBUG,"InspIRCd: startup: not starting with UID 0!");
4848 SetupCommandTable();
4849 log(DEBUG,"InspIRCd: startup: default command table set up");
4852 if (strcmp(DieValue,""))
4854 printf("WARNING: %s\n\n",DieValue);
4857 log(DEBUG,"InspIRCd: startup: read config");
4859 int count2 = 0, count3 = 0;
4860 for (count = 0; count < ConfValueEnum("bind"); count++)
4862 ConfValue("bind","port",count,configToken);
4863 ConfValue("bind","address",count,Addr);
4864 ConfValue("bind","type",count,Type);
4865 if (!strcmp(Type,"servers"))
4867 char Default[MAXBUF];
4868 strcpy(Default,"no");
4869 ConfValue("bind","default",count,Default);
4870 if (strchr(Default,'y'))
4872 defaultRoute = count3;
4873 log(DEBUG,"InspIRCd: startup: binding '%s:%s' is default server route",Addr,configToken);
4875 me[count3] = new serverrec(ServerName,100L,false);
4876 me[count3]->CreateListener(Addr,atoi(configToken));
4881 ports[count2] = atoi(configToken);
4882 strcpy(addrs[count2],Addr);
4885 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
4888 UDPportCount = count3;
4890 log(DEBUG,"InspIRCd: startup: read %d total client ports and %d total server ports",portCount,UDPportCount);
4892 log(DEBUG,"InspIRCd: startup: InspIRCd is now running!");
4896 /* BugFix By Craig! :p */
4898 for (count = 0; count2 < ConfValueEnum("module"); count2++)
4900 char modfile[MAXBUF];
4901 ConfValue("module","name",count,configToken);
4902 sprintf(modfile,"%s/%s",MOD_PATH,configToken);
4903 printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
4904 log(DEBUG,"InspIRCd: startup: Loading module: %s",modfile);
4905 /* If The File Doesnt exist, Trying to load it
4906 * Will Segfault the IRCd.. So, check to see if
4907 * it Exists, Before Proceeding. */
4908 if (FileExists(modfile))
4910 factory[count] = new ircd_module(modfile);
4911 if (factory[count]->LastError())
4913 log(DEBUG,"Unable to load %s: %s",modfile,factory[count]->LastError());
4914 sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
4917 if (factory[count]->factory)
4919 modules[count] = factory[count]->factory->CreateModule();
4920 /* save the module and the module's classfactory, if
4921 * this isnt done, random crashes can occur :/ */
4922 module_names.push_back(modfile);
4926 log(DEBUG,"Unable to load %s",modfile);
4927 sprintf("Unable to load %s\nExiting...\n",modfile);
4930 /* Increase the Count */
4935 log(DEBUG,"InspIRCd: startup: Module Not Found %s",modfile);
4936 printf("Module Not Found: \033[1;37m%s\033[0;37m, Skipping\n",modfile);
4939 MODCOUNT = count - 1;
4940 log(DEBUG,"Total loaded modules: %d",MODCOUNT+1);
4942 printf("\nInspIRCd is now running!\n");
4944 startup_time = time(NULL);
4948 log(VERBOSE,"Not forking as -nofork was specified");
4952 if (DaemonSeed() == ERROR)
4954 log(DEBUG,"InspIRCd: startup: can't daemonise");
4955 printf("ERROR: could not go into daemon mode. Shutting down.\n");
4961 /* setup select call */
4962 FD_ZERO(&selectFds);
4963 log(DEBUG,"InspIRCd: startup: zero selects");
4964 log(VERBOSE,"InspIRCd: startup: portCount = %d", portCount);
4966 for (count = 0; count < portCount; count++)
4968 if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
4970 log(DEBUG,"InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
4973 if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
4975 log(DEBUG,"InspIRCd: startup: failed to bind port %d",ports[count]);
4977 else /* well we at least bound to one socket so we'll continue */
4983 log(DEBUG,"InspIRCd: startup: total bound ports %d",boundPortCount);
4985 /* if we didn't bind to anything then abort */
4986 if (boundPortCount == 0)
4988 log(DEBUG,"InspIRCd: startup: no ports bound, bailing!");
4992 length = sizeof (client);
4993 int flip_flop = 0, udp_port = 0;
4994 char udp_msg[MAXBUF], udp_host[MAXBUF];
4996 /* main loop for multiplexing/resetting */
4999 /* set up select call */
5000 for (count = 0; count < boundPortCount; count++)
5002 FD_SET (openSockfd[count], &selectFds);
5005 /* added timeout! select was waiting forever... wank... :/ */
5016 vector<int>::iterator niterator;
5019 // *FIX* Instead of closing sockets in kill_link when they receive the ERROR :blah line, we should queue
5020 // them in a list, then reap the list every second or so.
5021 if (reap_counter>5000) {
5022 if (fd_reap.size() > 0) {
5023 for( int n = 0; n < fd_reap.size(); n++)
5025 Blocking(fd_reap[n]);
5027 NonBlocking(fd_reap[n]);
5036 selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
5038 for (int x = 0; x != UDPportCount; x++)
5041 if (me[x]->RecvPacket(udp_msg, udp_host, udp_port, theirkey))
5043 if (strlen(udp_msg)<1) {
5044 log(DEBUG,"Invalid datagram from %s:%d:%d [route%d]",udp_host,udp_port,me[x]->port,x);
5047 FOREACH_MOD OnPacketReceive(udp_msg);
5048 // Packets must go back via the route they arrived on :)
5049 handle_link_packet(theirkey, udp_msg, udp_host, udp_port, me[x]);
5054 for (user_hash::iterator count2 = clientlist.begin(); count2 != clientlist.end(); count2++)
5058 if (!count2->second) break;
5061 if (count2->second->fd)
5063 if (((time(NULL)) > count2->second->nping) && (isnick(count2->second->nick)) && (count2->second->registered == 7))
5065 if (!count2->second->lastping)
5067 log(DEBUG,"InspIRCd: ping timeout: %s",count2->second->nick);
5068 kill_link(count2->second,"Ping timeout");
5071 Write(count2->second->fd,"PING :%s",ServerName);
5072 log(DEBUG,"InspIRCd: pinging: %s",count2->second->nick);
5073 count2->second->lastping = 0;
5074 count2->second->nping = time(NULL)+120;
5077 result = read(count2->second->fd, data, 1);
5078 // result EAGAIN means nothing read
5079 if (result == EAGAIN)
5087 log(DEBUG,"InspIRCd: Exited: %s",count2->second->nick);
5088 kill_link(count2->second,"Client exited");
5089 // must bail here? kill_link removes the hash, corrupting the iterator
5090 log(DEBUG,"Bailing from client exit");
5094 else if (result > 0)
5098 strncat(count2->second->inbuf, data, result);
5100 if (strlen(count2->second->inbuf) > 509) {
5101 count2->second->inbuf[509] = '\r';
5102 count2->second->inbuf[510] = '\n';
5103 count2->second->inbuf[511] = '\0';
5106 if (strchr(count2->second->inbuf, '\n') || strchr(count2->second->inbuf, '\r') || (strlen(count2->second->inbuf) > 509))
5108 /* at least one complete line is waiting to be processed */
5109 if (!count2->second->fd)
5113 process_buffer(count2->second);
5122 /* select is reporting a waiting socket. Poll them all to find out which */
5123 if (selectResult > 0)
5125 char target[MAXBUF], resolved[MAXBUF];
5126 for (count = 0; count < boundPortCount; count++)
5128 if (FD_ISSET (openSockfd[count], &selectFds))
5130 incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
5132 address_cache::iterator iter = IP.find(client.sin_addr);
5133 bool iscached = false;
5134 if (iter == IP.end())
5136 /* ip isn't in cache, add it */
5137 strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
5138 if(CleanAndResolve(resolved, target) != TRUE)
5140 strncpy(resolved,target,MAXBUF);
5142 /* hostname now in 'target' */
5143 IP[client.sin_addr] = new string(resolved);
5144 /* hostname in cache */
5148 /* found ip (cached) */
5149 strncpy(resolved, iter->second->c_str(), MAXBUF);
5153 if (incomingSockfd < 0)
5155 WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
5156 log(DEBUG,"InspIRCd: accept failed: %d",ports[count]);
5160 AddClient(incomingSockfd, resolved, ports[count], iscached);
5161 log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
5170 close (incomingSockfd);