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>
37 #include "connection.h"
48 int LogLevel = DEFAULT;
49 char ServerName[MAXBUF];
51 char ServerDesc[MAXBUF];
52 char AdminName[MAXBUF];
53 char AdminEmail[MAXBUF];
54 char AdminNick[MAXBUF];
56 char restartpass[MAXBUF];
60 char PrefixQuit[MAXBUF];
61 char DieValue[MAXBUF];
63 int WHOWAS_STALE = 48; // default WHOWAS Entries last 2 days before they go 'stale'
64 int WHOWAS_MAX = 100; // default 100 people maximum in the WHOWAS list
66 time_t startup_time = time(NULL);
68 extern vector<Module*> modules;
69 extern vector<ircd_module*> factory;
73 template<> struct hash<in_addr>
75 size_t operator()(const struct in_addr &a) const
78 memcpy(&q,&a,sizeof(size_t));
83 template<> struct hash<string>
85 size_t operator()(const string &s) const
88 static struct hash<const char *> strhash;
100 bool operator()(const string& s1, const string& s2) const
102 char a[MAXBUF],b[MAXBUF];
103 strcpy(a,s1.c_str());
104 strcpy(b,s2.c_str());
105 return (strcasecmp(a,b) == 0);
110 struct InAddr_HashComp
113 bool operator()(const in_addr &s1, const in_addr &s2) const
118 memcpy(&q,&s1,sizeof(size_t));
119 memcpy(&p,&s2,sizeof(size_t));
127 typedef hash_map<string, userrec*, hash<string>, StrHashComp> user_hash;
128 typedef hash_map<string, chanrec*, hash<string>, StrHashComp> chan_hash;
129 typedef hash_map<in_addr,string*, hash<in_addr>, InAddr_HashComp> address_cache;
130 typedef deque<command_t> command_table;
133 server_list* servers;
135 user_hash clientlist;
138 command_table cmdlist;
145 struct linger linger = { 0 };
146 char bannerBuffer[MAXBUF];
147 int boundPortCount = 0;
148 int portCount = 0, UDPportCount = 0, ports[MAXSOCKS];
149 int defaultRoute = 0;
153 int has_channel(userrec *u, chanrec *c);
154 int usercount(chanrec *c);
155 int usercount_i(chanrec *c);
156 void update_stats_l(int fd,int data_out);
157 char* Passwd(userrec *user);
158 bool IsDenied(userrec *user);
159 void AddWhoWas(userrec* u);
162 void safedelete(userrec *p)
166 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
167 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
172 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
176 void safedelete(chanrec *p)
181 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
185 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
190 /* chop a string down to 512 characters and preserve linefeed (irc max
195 if (strlen(str) > 512)
204 string getservername()
209 string getserverdesc()
214 string getnetworkname()
219 string getadminname()
224 string getadminemail()
229 string getadminnick()
234 void log(int level,char *text, ...)
236 char textbuffer[MAXBUF];
240 struct tm * timeinfo;
242 if (level < LogLevel)
246 timeinfo = localtime (&rawtime);
248 f = fopen("ircd.log","a+");
252 va_start (argsPtr, text);
253 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
255 strcpy(b,asctime(timeinfo));
256 b[strlen(b)-1] = ':';
257 fprintf(f,"%s %s\n",b,textbuffer);
262 printf("Can't write log file, bailing!!!");
267 void readfile(file_cache &F, const char* fname)
270 char linebuf[MAXBUF];
272 log(DEBUG,"readfile: loading %s",fname);
274 file = fopen(fname,"r");
279 fgets(linebuf,sizeof(linebuf),file);
280 linebuf[strlen(linebuf)-1]='\0';
281 if (!strcmp(linebuf,""))
287 F.push_back(linebuf);
294 log(DEBUG,"readfile: failed to load file: %s",fname);
296 log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
299 void ReadConfig(void)
301 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF];
304 ConfValue("server","name",0,ServerName);
305 ConfValue("server","description",0,ServerDesc);
306 ConfValue("server","network",0,Network);
307 ConfValue("admin","name",0,AdminName);
308 ConfValue("admin","email",0,AdminEmail);
309 ConfValue("admin","nick",0,AdminNick);
310 ConfValue("files","motd",0,motd);
311 ConfValue("files","rules",0,rules);
312 ConfValue("power","diepass",0,diepass);
313 ConfValue("power","pause",0,pauseval);
314 ConfValue("power","restartpass",0,restartpass);
315 ConfValue("options","prefixquit",0,PrefixQuit);
316 ConfValue("die","value",0,DieValue);
317 ConfValue("options","loglevel",0,dbg);
318 if (!strcmp(dbg,"debug"))
320 if (!strcmp(dbg,"verbose"))
322 if (!strcmp(dbg,"default"))
324 if (!strcmp(dbg,"sparse"))
326 if (!strcmp(dbg,"none"))
328 readfile(RULES,rules);
329 log(DEBUG,"Reading connect classes");
331 for (int i = 0; i < ConfValueEnum("connect"); i++)
334 ConfValue("connect","allow",i,Value);
335 if (strcmp(Value,""))
337 strcpy(c.host,Value);
340 ConfValue("connect","password",i,Value);
341 strcpy(c.pass,Value);
342 Classes.push_back(c);
343 log(DEBUG,"Read connect class type ALLOW, host=%s password=%s",c.host,c.pass);
347 ConfValue("connect","deny",i,Value);
348 strcpy(c.host,Value);
350 Classes.push_back(c);
351 log(DEBUG,"Read connect class type DENY, host=%s",c.host);
360 log(DEBUG,"Blocking: %d",s);
361 flags = fcntl(s, F_GETFL, 0);
362 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
365 void NonBlocking(int s)
368 log(DEBUG,"NonBlocking: %d",s);
369 flags = fcntl(s, F_GETFL, 0);
370 fcntl(s, F_SETFL, flags | O_NONBLOCK);
374 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
376 struct hostent *hostPtr = NULL;
379 memset (resolvedHost, '\0',MAXBUF);
380 if(unresolvedHost == NULL)
382 if ((inet_aton(unresolvedHost,&addr)) == 0)
384 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
386 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
388 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
392 /* write formatted text to a socket, in same format as printf */
394 void Write(int sock,char *text, ...)
396 char textbuffer[MAXBUF];
400 va_start (argsPtr, text);
401 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
403 sprintf(tb,"%s\r\n",textbuffer);
405 write(sock,tb,strlen(tb));
406 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
409 /* write a server formatted numeric response to a single socket */
411 void WriteServ(int sock, char* text, ...)
413 char textbuffer[MAXBUF],tb[MAXBUF];
415 va_start (argsPtr, text);
417 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
419 sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
421 write(sock,tb,strlen(tb));
422 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
425 /* write text from an originating user to originating user */
427 void WriteFrom(int sock, userrec *user,char* text, ...)
429 char textbuffer[MAXBUF],tb[MAXBUF];
431 va_start (argsPtr, text);
433 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
435 sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
437 write(sock,tb,strlen(tb));
438 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
441 /* write text to an destination user from a source user (e.g. user privmsg) */
443 void WriteTo(userrec *source, userrec *dest,char *data, ...)
445 char textbuffer[MAXBUF],tb[MAXBUF];
447 va_start (argsPtr, data);
448 if ((!dest) || (!source))
452 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
455 WriteFrom(dest->fd,source,"%s",textbuffer);
458 /* write formatted text from a source user to all users on a channel
459 * including the sender (NOT for privmsg, notice etc!) */
461 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
463 char textbuffer[MAXBUF];
465 va_start (argsPtr, text);
466 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
468 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
470 if (has_channel(i->second,Ptr))
472 WriteTo(user,i->second,"%s",textbuffer);
477 /* write formatted text from a source user to all users on a channel except
478 * for the sender (for privmsg etc) */
480 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
482 char textbuffer[MAXBUF];
484 va_start (argsPtr, text);
485 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
488 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
490 if (has_channel(i->second,Ptr) && (user != i->second))
492 WriteTo(user,i->second,"%s",textbuffer);
497 int c_count(userrec* u)
500 for (int i =0; i != MAXCHANS; i++)
501 if (u->chans[i].channel)
507 /* return 0 or 1 depending if users u and u2 share one or more common channels
508 * (used by QUIT, NICK etc which arent channel specific notices) */
510 int common_channels(userrec *u, userrec *u2)
519 for (i = 0; i != MAXCHANS; i++)
521 for (z = 0; z != MAXCHANS; z++)
523 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
525 if ((c_count(u)) && (c_count(u2)))
535 /* write a formatted string to all users who share at least one common
536 * channel, including the source user e.g. for use in NICK */
538 void WriteCommon(userrec *u, char* text, ...)
540 char textbuffer[MAXBUF];
542 va_start (argsPtr, text);
543 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
546 WriteFrom(u->fd,u,"%s",textbuffer);
548 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
550 if (common_channels(u,i->second) && (i->second != u))
552 WriteFrom(i->second->fd,u,"%s",textbuffer);
557 /* write a formatted string to all users who share at least one common
558 * channel, NOT including the source user e.g. for use in QUIT */
560 void WriteCommonExcept(userrec *u, char* text, ...)
562 char textbuffer[MAXBUF];
564 va_start (argsPtr, text);
565 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
568 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
570 if ((common_channels(u,i->second)) && (u != i->second))
572 WriteFrom(i->second->fd,u,"%s",textbuffer);
577 void WriteOpers(char* text, ...)
579 char textbuffer[MAXBUF];
581 va_start (argsPtr, text);
582 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
585 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
587 if (strchr(i->second->modes,'o'))
589 if (strchr(i->second->modes,'s'))
591 // send server notices to all with +s
592 // (TODO: needs SNOMASKs)
593 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
599 void WriteWallOps(userrec *source, char* text, ...)
602 char textbuffer[MAXBUF];
604 va_start (argsPtr, text);
605 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
608 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
610 if (strchr(i->second->modes,'w'))
612 WriteTo(source,i->second,"WALLOPS %s",textbuffer);
617 /* convert a string to lowercase. Note following special circumstances
618 * taken from RFC 1459. Many "official" server branches still hold to this
619 * rule so i will too;
621 * Because of IRC's scandanavian origin, the characters {}| are
622 * considered to be the lower case equivalents of the characters []\,
623 * respectively. This is a critical issue when determining the
624 * equivalence of two nicknames.
627 void strlower(char *n)
633 for (int i = 0; i != strlen(n); i++)
635 n[i] = tolower(n[i]);
645 /* verify that a user's nickname is valid */
647 int isnick(const char* n)
659 if (strlen(n) > NICKMAX-1)
663 for (i = 0; i != strlen(n); i++)
665 if ((n[i] < 33) || (n[i] > 125))
669 /* can't occur ANYWHERE in a nickname! */
670 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
674 /* can't occur as the first char of a nickname... */
675 if ((strchr("0123456789",n[i])) && (!i))
683 /* Find a user record by nickname and return a pointer to it */
685 userrec* Find(string nick)
687 user_hash::iterator iter = clientlist.find(nick);
689 if (iter == clientlist.end())
690 /* Couldn't find it */
696 void update_stats_l(int fd,int data_out) /* add one line-out to stats L for this fd */
698 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
700 if (i->second->fd == fd)
702 i->second->bytes_out+=data_out;
703 i->second->cmds_out++;
709 /* find a channel record by channel name and return a pointer to it */
711 chanrec* FindChan(const char* chan)
713 chan_hash::iterator iter = chanlist.find(chan);
715 if (iter == chanlist.end())
716 /* Couldn't find it */
723 void purge_empty_chans(void)
725 int go_again = 1, purge = 0;
730 for (chan_hash::iterator i = chanlist.begin(); i != chanlist.end(); i++)
733 if (!usercount(i->second))
735 /* kill the record */
736 if (i != chanlist.end())
738 log(DEBUG,"del_channel: destroyed: %s",i->second->name);
749 log(DEBUG,"completed channel purge, killed %d",purge);
752 /* returns the status character for a given user on a channel, e.g. @ for op,
753 * % for halfop etc. If the user has several modes set, the highest mode
754 * the user has must be returned. */
756 char* cmode(userrec *user, chanrec *chan)
759 for (i = 0; i != MAXCHANS; i++)
761 if ((user->chans[i].channel == chan) && (chan != NULL))
763 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
767 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
771 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
780 char scratch[MAXMODES];
782 char* chanmodes(chanrec *chan)
785 if (chan->noexternal)
793 if (strcmp(chan->key,""))
801 if (chan->inviteonly)
817 if (strcmp(chan->key,""))
820 strcat(scratch,chan->key);
825 sprintf(foo," %d",chan->limit);
828 log(DEBUG,"chanmodes: %s %s",chan->name,scratch);
832 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
833 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
834 * highest mode the user has must be returned. */
836 int cstatus(userrec *user, chanrec *chan)
839 for (i = 0; i != MAXCHANS; i++)
841 if ((user->chans[i].channel == chan) && (chan != NULL))
843 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
847 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
851 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
855 return STATUS_NORMAL;
861 /* compile a userlist of a channel into a string, each nick seperated by
862 * spaces and op, voice etc status shown as @ and + */
864 void userlist(userrec *user,chanrec *c)
866 sprintf(list,"353 %s = %s :", user->nick, c->name);
867 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
869 if (has_channel(i->second,c))
871 if (isnick(i->second->nick))
873 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
875 /* user is +i, and source not on the channel, does not show
876 * nick in NAMES list */
879 strcat(list,cmode(i->second,c));
880 strcat(list,i->second->nick);
882 if (strlen(list)>(480-NICKMAX))
884 /* list overflowed into
885 * multiple numerics */
886 WriteServ(user->fd,list);
887 sprintf(list,"353 %s = %s :", user->nick, c->name);
892 /* if whats left in the list isnt empty, send it */
893 if (list[strlen(list)-1] != ':')
895 WriteServ(user->fd,list);
899 /* return a count of the users on a specific channel accounting for
900 * invisible users who won't increase the count. e.g. for /LIST */
902 int usercount_i(chanrec *c)
908 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
910 if (has_channel(i->second,c))
912 if (isnick(i->second->nick))
914 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
916 /* user is +i, and source not on the channel, does not show
917 * nick in NAMES list */
924 log(DEBUG,"usercount_i: %s %d",c->name,count);
929 int usercount(chanrec *c)
935 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
937 if (has_channel(i->second,c))
939 if (isnick(i->second->nick))
945 log(DEBUG,"usercount: %s %d",c->name,count);
950 /* add a channel to a user, creating the record for it if needed and linking
951 * it to the user record */
953 chanrec* add_channel(userrec *user, char* cname, char* key)
959 if ((!cname) || (!user))
963 if (strlen(cname) > CHANMAX-1)
965 cname[CHANMAX-1] = '\0';
968 log(DEBUG,"add_channel: %s %s",user->nick,cname);
970 if ((has_channel(user,FindChan(cname))) && (FindChan(cname)))
972 return NULL; // already on the channel!
975 if (!FindChan(cname))
977 /* create a new one */
978 log(DEBUG,"add_channel: creating: %s",cname);
980 chanlist[cname] = new chanrec();
982 strcpy(chanlist[cname]->name, cname);
983 chanlist[cname]->topiclock = 1;
984 chanlist[cname]->noexternal = 1;
985 chanlist[cname]->created = time(NULL);
986 strcpy(chanlist[cname]->topic, "");
987 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
988 chanlist[cname]->topicset = 0;
989 Ptr = chanlist[cname];
990 log(DEBUG,"add_channel: created: %s",cname);
991 /* set created to 2 to indicate user
992 * is the first in the channel
993 * and should be given ops */
999 /* channel exists, just fish out a pointer to its struct */
1000 Ptr = FindChan(cname);
1003 log(DEBUG,"add_channel: joining to: %s",Ptr->name);
1004 if (strcmp(Ptr->key,""))
1006 log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
1009 log(DEBUG,"add_channel: no key given in JOIN");
1010 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
1015 log(DEBUG,"key at %p is %s",key,key);
1016 if (strcasecmp(key,Ptr->key))
1018 log(DEBUG,"add_channel: bad key given in JOIN");
1019 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
1025 if (Ptr->inviteonly)
1027 if (user->IsInvited(Ptr->name))
1029 /* user was invited to channel */
1030 /* there may be an optional channel NOTICE here */
1034 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
1041 if (usercount(Ptr) == Ptr->limit)
1043 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
1048 /* check user against the channel banlist */
1049 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1051 if (match(user->GetFullHost(),i->data))
1053 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
1058 user->RemoveInvite(Ptr->name);
1065 for (i =0; i != MAXCHANS; i++)
1067 if (user->chans[i].channel == NULL)
1071 /* first user in is given ops */
1072 user->chans[i].uc_modes = UCMODE_OP;
1076 user->chans[i].uc_modes = 0;
1078 user->chans[i].channel = Ptr;
1079 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
1082 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
1083 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
1086 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
1087 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name,chanmodes(Ptr));
1088 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1089 FOREACH_MOD OnUserJoin(user,Ptr);
1093 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
1094 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
1098 /* remove a channel from a users record, and remove the record from memory
1099 * if the channel has become empty */
1101 chanrec* del_channel(userrec *user, char* cname, char* reason)
1107 if ((!cname) || (!user))
1112 Ptr = FindChan(cname);
1119 FOREACH_MOD OnUserPart(user,Ptr);
1120 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
1122 for (i =0; i != MAXCHANS; i++)
1124 /* zap it from the channel list of the user */
1125 if (user->chans[i].channel == Ptr)
1129 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
1133 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
1135 user->chans[i].uc_modes = 0;
1136 user->chans[i].channel = NULL;
1137 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1142 /* if there are no users left on the channel */
1143 if (!usercount(Ptr))
1145 chan_hash::iterator iter = chanlist.find(Ptr->name);
1147 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1149 /* kill the record */
1150 if (iter != chanlist.end())
1152 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1153 delete iter->second;
1154 chanlist.erase(iter);
1160 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
1165 if ((!Ptr) || (!user) || (!src))
1170 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
1172 if (!has_channel(user,Ptr))
1174 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
1177 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
1179 if (cstatus(src,Ptr) == STATUS_HOP)
1181 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
1185 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
1191 for (i =0; i != MAXCHANS; i++)
1193 /* zap it from the channel list of the user */
1194 if (user->chans[i].channel == Ptr)
1196 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
1197 user->chans[i].uc_modes = 0;
1198 user->chans[i].channel = NULL;
1199 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1204 /* if there are no users left on the channel */
1205 if (!usercount(Ptr))
1207 chan_hash::iterator iter = chanlist.find(Ptr->name);
1209 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1211 /* kill the record */
1212 if (iter != chanlist.end())
1214 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1215 delete iter->second;
1216 chanlist.erase(iter);
1222 /* returns 1 if user u has channel c in their record, 0 if not */
1224 int has_channel(userrec *u, chanrec *c)
1232 for (i =0; i != MAXCHANS; i++)
1234 if (u->chans[i].channel == c)
1242 int give_ops(userrec *user,char *dest,chanrec *chan,int status)
1247 if ((!user) || (!dest) || (!chan))
1251 if (status != STATUS_OP)
1253 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1260 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1266 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1271 for (i = 0; i != MAXCHANS; i++)
1273 if ((d->chans[i].channel == chan) && (chan != NULL))
1275 if (d->chans[i].uc_modes & UCMODE_OP)
1277 /* mode already set on user, dont allow multiple */
1280 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
1281 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
1289 int give_hops(userrec *user,char *dest,chanrec *chan,int status)
1294 if ((!user) || (!dest) || (!chan))
1298 if (status != STATUS_OP)
1300 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1308 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1313 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1318 for (i = 0; i != MAXCHANS; i++)
1320 if ((d->chans[i].channel == chan) && (chan != NULL))
1322 if (d->chans[i].uc_modes & UCMODE_HOP)
1324 /* mode already set on user, dont allow multiple */
1327 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
1328 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
1336 int give_voice(userrec *user,char *dest,chanrec *chan,int status)
1341 if ((!user) || (!dest) || (!chan))
1345 if (status < STATUS_HOP)
1347 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1355 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1360 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1365 for (i = 0; i != MAXCHANS; i++)
1367 if ((d->chans[i].channel == chan) && (chan != NULL))
1369 if (d->chans[i].uc_modes & UCMODE_VOICE)
1371 /* mode already set on user, dont allow multiple */
1374 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
1375 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
1383 int take_ops(userrec *user,char *dest,chanrec *chan,int status)
1388 if ((!user) || (!dest) || (!chan))
1392 if (status != STATUS_OP)
1394 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1402 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1407 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1412 for (i = 0; i != MAXCHANS; i++)
1414 if ((d->chans[i].channel == chan) && (chan != NULL))
1416 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
1418 /* mode already set on user, dont allow multiple */
1421 d->chans[i].uc_modes ^= UCMODE_OP;
1422 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
1430 int take_hops(userrec *user,char *dest,chanrec *chan,int status)
1435 if ((!user) || (!dest) || (!chan))
1439 if (status != STATUS_OP)
1441 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1449 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1454 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1459 for (i = 0; i != MAXCHANS; i++)
1461 if ((d->chans[i].channel == chan) && (chan != NULL))
1463 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
1465 /* mode already set on user, dont allow multiple */
1468 d->chans[i].uc_modes ^= UCMODE_HOP;
1469 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
1477 int take_voice(userrec *user,char *dest,chanrec *chan,int status)
1482 if ((!user) || (!dest) || (!chan))
1486 if (status < STATUS_HOP)
1488 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1496 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1501 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1506 for (i = 0; i != MAXCHANS; i++)
1508 if ((d->chans[i].channel == chan) && (chan != NULL))
1510 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
1512 /* mode already set on user, dont allow multiple */
1515 d->chans[i].uc_modes ^= UCMODE_VOICE;
1516 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
1524 void TidyBan(char *ban)
1526 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
1530 char* pos_of_pling = strchr(temp,'!');
1531 char* pos_of_at = strchr(temp,'@');
1533 pos_of_pling[0] = '\0';
1534 pos_of_at[0] = '\0';
1538 strncpy(NICK,temp,NICKMAX);
1539 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
1540 strncpy(HOST,pos_of_at,160);
1542 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
1545 int add_ban(userrec *user,char *dest,chanrec *chan,int status)
1548 if ((!user) || (!dest) || (!chan))
1550 if (strchr(dest,'!')==0)
1552 if (strchr(dest,'@')==0)
1554 for (int i = 0; i < strlen(dest); i++)
1557 for (int i = 0; i < strlen(dest); i++)
1561 for (int i = 0; i < strlen(dest); i++)
1567 for (int i = 0; i < strlen(dest); i++)
1572 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
1575 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1577 if (!strcasecmp(i->data,dest))
1579 // dont allow a user to set the same ban twice
1584 b.set_time = time(NULL);
1585 strncpy(b.data,dest,MAXBUF);
1586 strncpy(b.set_by,user->nick,NICKMAX);
1587 chan->bans.push_back(b);
1591 int take_ban(userrec *user,char *dest,chanrec *chan,int status)
1593 if ((!user) || (!dest) || (!chan))
1598 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
1599 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1601 if (!strcasecmp(i->data,dest))
1603 chan->bans.erase(i);
1610 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt)
1612 char modelist[MAXBUF];
1613 char outlist[MAXBUF];
1614 char outstr[MAXBUF];
1615 char outpars[32][MAXBUF];
1621 bool k_set = false, l_set = false;
1628 log(DEBUG,"process_modes: start");
1630 strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
1631 /* parameters[2] onwards are parameters for
1632 * modes that require them :) */
1633 strcpy(outlist,"+");
1636 log(DEBUG,"process_modes: modelist: %s",modelist);
1638 for (ptr = 0; ptr < strlen(modelist); ptr++)
1643 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
1644 switch (modelist[ptr])
1649 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
1651 outlist[strlen(outlist)-1] = '-';
1655 strcat(outlist,"-");
1665 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
1667 outlist[strlen(outlist)-1] = '+';
1671 strcat(outlist,"+");
1678 if ((param >= pcnt)) break;
1681 r = give_ops(user,parameters[param++],chan,status);
1685 r = take_ops(user,parameters[param++],chan,status);
1689 strcat(outlist,"o");
1690 strcpy(outpars[pc++],parameters[param-1]);
1695 if ((param >= pcnt)) break;
1698 r = give_hops(user,parameters[param++],chan,status);
1702 r = take_hops(user,parameters[param++],chan,status);
1706 strcat(outlist,"h");
1707 strcpy(outpars[pc++],parameters[param-1]);
1713 if ((param >= pcnt)) break;
1716 r = give_voice(user,parameters[param++],chan,status);
1720 r = take_voice(user,parameters[param++],chan,status);
1724 strcat(outlist,"v");
1725 strcpy(outpars[pc++],parameters[param-1]);
1730 if ((param >= pcnt)) break;
1733 r = add_ban(user,parameters[param++],chan,status);
1737 r = take_ban(user,parameters[param++],chan,status);
1741 strcat(outlist,"b");
1742 strcpy(outpars[pc++],parameters[param-1]);
1747 if ((param >= pcnt))
1755 if (!strcmp(chan->key,""))
1757 strcat(outlist,"k");
1758 strcpy(outpars[pc++],parameters[param++]);
1759 strcpy(chan->key,parameters[param-1]);
1765 /* only allow -k if correct key given */
1766 if (strcmp(chan->key,""))
1768 strcat(outlist,"k");
1769 strcpy(chan->key,"");
1779 strcat(outlist,"l");
1784 if ((param >= pcnt)) break;
1790 bool invalid = false;
1791 for (int i = 0; i < strlen(parameters[param]); i++)
1793 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
1798 if (atoi(parameters[param]) < 1)
1806 chan->limit = atoi(parameters[param]);
1809 strcat(outlist,"l");
1810 strcpy(outpars[pc++],parameters[param++]);
1817 if (chan->inviteonly != mdir)
1819 strcat(outlist,"i");
1821 chan->inviteonly = mdir;
1825 if (chan->topiclock != mdir)
1827 strcat(outlist,"t");
1829 chan->topiclock = mdir;
1833 if (chan->noexternal != mdir)
1835 strcat(outlist,"n");
1837 chan->noexternal = mdir;
1841 if (chan->moderated != mdir)
1843 strcat(outlist,"m");
1845 chan->moderated = mdir;
1849 if (chan->secret != mdir)
1851 strcat(outlist,"s");
1852 if (chan->c_private)
1854 chan->c_private = 0;
1857 strcat(outlist,"-p+");
1861 strcat(outlist,"+p-");
1865 chan->secret = mdir;
1869 if (chan->c_private != mdir)
1871 strcat(outlist,"p");
1877 strcat(outlist,"-s+");
1881 strcat(outlist,"+s-");
1885 chan->c_private = mdir;
1892 /* this ensures only the *valid* modes are sent out onto the network */
1893 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
1895 outlist[strlen(outlist)-1] = '\0';
1897 if (strcmp(outlist,""))
1899 strcpy(outstr,outlist);
1900 for (ptr = 0; ptr < pc; ptr++)
1903 strcat(outstr,outpars[ptr]);
1905 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1909 void handle_mode(char **parameters, int pcnt, userrec *user)
1915 char outpars[MAXBUF];
1917 dest = Find(parameters[0]);
1919 if ((dest) && (pcnt == 1))
1921 WriteServ(user->fd,"221 %s :+%s",user->nick,user->modes);
1924 if ((dest) && (pcnt > 1))
1929 if (strchr(user->modes,'o'))
1940 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1944 strcpy(outpars,"+");
1947 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1950 for (i = 0; i < strlen(parameters[1]); i++)
1952 if (parameters[1][i] == '+')
1956 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1958 outpars[strlen(outpars)-1] = '+';
1962 strcat(outpars,"+");
1968 if (parameters[1][i] == '-')
1972 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1974 outpars[strlen(outpars)-1] = '-';
1978 strcat(outpars,"-");
1986 if (strchr(user->modes,'o'))
1992 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's'))
2001 if (!strchr(dest->modes,parameters[1][i]))
2003 dest->modes[strlen(dest->modes)+1]='\0';
2004 dest->modes[strlen(dest->modes)] = parameters[1][i];
2005 outpars[strlen(outpars)+1]='\0';
2006 outpars[strlen(outpars)] = parameters[1][i];
2015 outpars[strlen(outpars)+1]='\0';
2016 outpars[strlen(outpars)] = parameters[1][i];
2019 for (q = 0; q < strlen(user->modes); q++)
2021 if (user->modes[q] != parameters[1][i])
2023 moo[0] = user->modes[q];
2028 strcpy(user->modes,temp);
2033 if (strlen(outpars))
2039 while (i < strlen (outpars))
2041 b[z++] = outpars[i++];
2043 if (i<strlen(outpars)-1)
2045 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2047 // someones playing silly buggers and trying
2048 // to put a +- or -+ into the line...
2052 if (i == strlen(outpars)-1)
2054 if ((outpars[i] == '-') || (outpars[i] == '+'))
2062 if ((b[z] == '-') || (b[z] == '+'))
2065 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2068 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2073 Ptr = FindChan(parameters[0]);
2078 /* just /modes #channel */
2079 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
2080 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
2086 if ((!strcmp(parameters[1],"+b")) || (!strcmp(parameters[1],"b")))
2089 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
2091 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
2093 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
2097 if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
2099 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, Ptr->name);
2103 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt);
2107 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2111 /* This function pokes and hacks at a parameter list like the following:
2113 * PART #winbot, #darkgalaxy :m00!
2115 * to turn it into a series of individual calls like this:
2117 * PART #winbot :m00!
2118 * PART #darkgalaxy :m00!
2120 * The seperate calls are sent to a callback function provided by the caller
2121 * (the caller will usually call itself recursively). The callback function
2122 * must be a command handler. Calling this function on a line with no list causes
2123 * no action to be taken. You must provide a starting and ending parameter number
2124 * where the range of the list can be found, useful if you have a terminating
2125 * parameter as above which is actually not part of the list, or parameters
2126 * before the actual list as well. This code is used by many functions which
2127 * can function as "one to list" (see the RFC) */
2129 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
2134 char blog[32][MAXBUF];
2135 char blog2[32][MAXBUF];
2136 int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
2137 char keystr[MAXBUF];
2140 for (i = 0; i <32; i++)
2143 for (i = 0; i <32; i++)
2144 strcpy(blog2[i],"");
2147 for (i = 0; i <10; i++)
2151 parameters[i] = moo;
2156 if (pcnt > 1) /* we have a key to copy */
2158 strcpy(keystr,parameters[1]);
2162 if (!parameters[start])
2166 if (!strchr(parameters[start],','))
2171 for (i = start; i <= end; i++)
2175 strcat(plist,parameters[i]);
2183 for (i = 0; i < t; i++)
2185 if (plist[i] == ',')
2188 strcpy(blog[j++],param);
2192 strcpy(blog[j++],param);
2195 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
2200 if ((joins) && (keystr))
2202 if (strchr(keystr,','))
2206 t2 = strlen(keystr);
2207 for (i = 0; i < t2; i++)
2209 if (keystr[i] == ',')
2212 strcpy(blog2[j++],param);
2216 strcpy(blog2[j++],param);
2221 for (j = 0; j < total; j++)
2227 for (q = end; q < pcnt-1; q++)
2229 if (parameters[q+1])
2231 pars[q-end+1] = parameters[q+1];
2234 if ((joins) && (parameters[1]))
2245 /* repeatedly call the function with the hacked parameter list */
2246 if ((joins) && (pcnt > 1))
2250 // pars[1] already set up and containing key from blog2[j]
2255 pars[1] = parameters[1];
2261 fn(pars,pcnt-(end-start),u);
2268 void handle_join(char **parameters, int pcnt, userrec *user)
2273 if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
2275 if (parameters[0][0] == '#')
2277 Ptr = add_channel(user,parameters[0],parameters[1]);
2282 void handle_part(char **parameters, int pcnt, userrec *user)
2288 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
2290 del_channel(user,parameters[0],parameters[1]);
2294 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
2296 del_channel(user,parameters[0],NULL);
2300 void handle_kick(char **parameters, int pcnt, userrec *user)
2302 chanrec* Ptr = FindChan(parameters[0]);
2303 userrec* u = Find(parameters[1]);
2307 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2311 if (!has_channel(u,Ptr))
2313 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
2319 kick_channel(user,u,Ptr,parameters[2]);
2323 kick_channel(user,u,Ptr,user->nick);
2328 void handle_die(char **parameters, int pcnt, userrec *user)
2330 log(DEBUG,"die: %s",user->nick);
2331 if (!strcmp(parameters[0],diepass))
2333 WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
2339 WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
2343 void handle_restart(char **parameters, int pcnt, userrec *user)
2345 log(DEBUG,"restart: %s",user->nick);
2346 if (!strcmp(parameters[0],restartpass))
2348 WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
2351 /* Will finish this later when i can be arsed :) */
2355 WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
2360 void kill_link(userrec *user,char* reason)
2362 user_hash::iterator iter = clientlist.find(user->nick);
2364 log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
2365 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
2366 fdatasync(user->fd);
2367 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
2368 FOREACH_MOD OnUserQuit(user);
2369 log(DEBUG,"closing fd %d",user->fd);
2370 /* bugfix, cant close() a nonblocking socket (sux!) */
2371 WriteCommonExcept(user,"QUIT :%s",reason);
2374 NonBlocking(user->fd);
2377 if (iter != clientlist.end())
2379 log(DEBUG,"deleting user hash value %p",iter->second);
2380 delete iter->second;
2381 clientlist.erase(iter);
2384 purge_empty_chans();
2388 void handle_kill(char **parameters, int pcnt, userrec *user)
2390 userrec *u = Find(parameters[0]);
2391 char killreason[MAXBUF];
2393 log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
2396 WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
2397 sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
2398 kill_link(u,killreason);
2402 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2406 void handle_summon(char **parameters, int pcnt, userrec *user)
2408 WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
2411 void handle_users(char **parameters, int pcnt, userrec *user)
2413 WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
2417 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
2419 char* Passwd(userrec *user)
2421 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
2423 if (match(user->host,i->host) && (i->type == CC_ALLOW))
2431 bool IsDenied(userrec *user)
2433 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
2435 if (match(user->host,i->host) && (i->type == CC_DENY))
2444 void handle_pass(char **parameters, int pcnt, userrec *user)
2446 if (!strcasecmp(parameters[0],Passwd(user)))
2448 user->haspassed = true;
2452 void handle_invite(char **parameters, int pcnt, userrec *user)
2454 userrec* u = Find(parameters[0]);
2455 chanrec* c = FindChan(parameters[1]);
2461 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[1]);
2467 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
2476 if (cstatus(user,c) < STATUS_HOP)
2478 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, c->name);
2482 u->InviteTo(c->name);
2483 WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
2484 WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
2488 void handle_topic(char **parameters, int pcnt, userrec *user)
2494 if (strlen(parameters[0]) <= CHANMAX)
2496 Ptr = FindChan(parameters[0]);
2501 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
2502 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
2506 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
2511 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
2517 if (loop_call(handle_topic,parameters,pcnt,user,0,pcnt-2,0))
2519 if (strlen(parameters[0]) <= CHANMAX)
2521 Ptr = FindChan(parameters[0]);
2524 if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
2526 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator", user->nick, Ptr->name);
2529 strcpy(Ptr->topic,parameters[1]);
2530 strcpy(Ptr->setby,user->nick);
2531 Ptr->topicset = time(NULL);
2532 WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
2536 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2542 /* sends out an error notice to all connected clients (not to be used
2545 void send_error(char *s)
2547 log(DEBUG,"send_error: %s",s);
2548 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
2550 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
2554 void Error(int status)
2556 signal (SIGALRM, SIG_IGN);
2557 signal (SIGPIPE, SIG_IGN);
2558 signal (SIGTERM, SIG_IGN);
2559 signal (SIGABRT, SIG_IGN);
2560 signal (SIGSEGV, SIG_IGN);
2561 signal (SIGURG, SIG_IGN);
2562 signal (SIGKILL, SIG_IGN);
2563 log(DEBUG,"*** fell down a pothole in the road to perfection ***");
2564 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
2568 int main (int argc, char *argv[])
2571 log(DEBUG,"*** InspIRCd starting up!");
2574 log(DEBUG,"main: no config");
2575 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
2578 if (InspIRCd() == ERROR)
2580 log(DEBUG,"main: daemon function bailed");
2581 printf("ERROR: could not initialise. Shutting down.\n");
2588 template<typename T> inline string ConvToStr(const T &in)
2591 if (!(tmp << in)) return string();
2595 /* re-allocates a nick in the user_hash after they change nicknames,
2596 * returns a pointer to the new user as it may have moved */
2598 userrec* ReHashNick(char* Old, char* New)
2600 user_hash::iterator newnick;
2601 user_hash::iterator oldnick = clientlist.find(Old);
2603 log(DEBUG,"ReHashNick: %s %s",Old,New);
2605 if (!strcasecmp(Old,New))
2607 log(DEBUG,"old nick is new nick, skipping");
2608 return oldnick->second;
2611 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
2613 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
2615 clientlist[New] = new userrec();
2616 clientlist[New] = oldnick->second;
2617 /*delete oldnick->second; */
2618 clientlist.erase(oldnick);
2620 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
2622 return clientlist[New];
2625 /* adds or updates an entry in the whowas list */
2626 void AddWhoWas(userrec* u)
2628 user_hash::iterator iter = whowas.find(u->nick);
2629 userrec *a = new userrec();
2630 strcpy(a->nick,u->nick);
2631 strcpy(a->ident,u->ident);
2632 strcpy(a->dhost,u->dhost);
2633 strcpy(a->host,u->host);
2634 strcpy(a->fullname,u->fullname);
2635 strcpy(a->server,u->server);
2636 a->signon = u->signon;
2638 /* MAX_WHOWAS: max number of /WHOWAS items
2639 * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
2640 * can be replaced by a newer one
2643 if (iter == whowas.end())
2645 if (whowas.size() == WHOWAS_MAX)
2647 for (user_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
2649 // 3600 seconds in an hour ;)
2650 if ((i->second->signon)<(time(NULL)-(WHOWAS_STALE*3600)))
2654 log(DEBUG,"added WHOWAS entry, purged an old record");
2661 log(DEBUG,"added fresh WHOWAS entry");
2662 whowas[a->nick] = a;
2667 log(DEBUG,"updated WHOWAS entry");
2668 delete iter->second;
2674 /* add a client connection to the sockets list */
2675 void AddClient(int socket, char* host, int port, bool iscached)
2679 char resolved[MAXBUF];
2682 user_hash::iterator iter;
2684 tempnick = ConvToStr(socket) + "-unknown";
2685 sprintf(tn2,"%d-unknown",socket);
2687 iter = clientlist.find(tempnick);
2689 if (iter != clientlist.end()) return;
2692 * It is OK to access the value here this way since we know
2693 * it exists, we just created it above.
2695 * At NO other time should you access a value in a map or a
2696 * hash_map this way.
2698 clientlist[tempnick] = new userrec();
2700 NonBlocking(socket);
2701 log(DEBUG,"AddClient: %d %s %d",socket,host,port);
2704 clientlist[tempnick]->fd = socket;
2705 strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
2706 strncpy(clientlist[tempnick]->host, host,160);
2707 strncpy(clientlist[tempnick]->dhost, host,160);
2708 strncpy(clientlist[tempnick]->server, ServerName,256);
2709 clientlist[tempnick]->registered = 0;
2710 clientlist[tempnick]->signon = time(NULL);
2711 clientlist[tempnick]->nping = time(NULL)+240;
2712 clientlist[tempnick]->lastping = 1;
2713 clientlist[tempnick]->port = port;
2717 WriteServ(socket,"NOTICE Auth :Found your hostname (cached)...");
2721 WriteServ(socket,"NOTICE Auth :Looking up your hostname...");
2724 if (clientlist.size() == MAXCLIENTS)
2725 kill_link(clientlist[tempnick],"No more connections allowed in this class");
2728 void handle_names(char **parameters, int pcnt, userrec *user)
2732 if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
2734 c = FindChan(parameters[0]);
2737 /*WriteServ(user->fd,"353 %s = %s :%s", user->nick, c->name,*/
2739 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
2743 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2748 void handle_privmsg(char **parameters, int pcnt, userrec *user)
2753 if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
2755 if (parameters[0][0] == '#')
2757 chan = FindChan(parameters[0]);
2760 if ((chan->noexternal) && (!has_channel(user,chan)))
2762 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
2765 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
2767 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
2770 ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
2774 /* no such nick/channel */
2775 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2780 dest = Find(parameters[0]);
2783 if (strcmp(dest->awaymsg,""))
2785 /* auto respond with aweh msg */
2786 WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
2788 WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
2792 /* no such nick/channel */
2793 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2797 void handle_notice(char **parameters, int pcnt, userrec *user)
2802 if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
2804 if (parameters[0][0] == '#')
2806 chan = FindChan(parameters[0]);
2809 if ((chan->noexternal) && (!has_channel(user,chan)))
2811 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
2814 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
2816 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
2819 WriteChannel(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
2823 /* no such nick/channel */
2824 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2829 dest = Find(parameters[0]);
2832 WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
2836 /* no such nick/channel */
2837 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2843 char* chlist(userrec *user)
2848 log(DEBUG,"chlist: %s",user->nick);
2854 for (i = 0; i != MAXCHANS; i++)
2856 if (user->chans[i].channel != NULL)
2858 if (user->chans[i].channel->name)
2860 strcpy(cmp,user->chans[i].channel->name);
2862 if (!strstr(lst,cmp))
2864 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
2866 strcat(lst,cmode(user,user->chans[i].channel));
2867 strcat(lst,user->chans[i].channel->name);
2877 void handle_info(char **parameters, int pcnt, userrec *user)
2879 WriteServ(user->fd,"371 %s :The Inspire IRCd Project Has been brought to you by the following people..",user->nick);
2880 WriteServ(user->fd,"371 %s :Craig Edwards, Craig McLure, and Others..",user->nick);
2881 WriteServ(user->fd,"371 %s :Will finish this later when i can be arsed :p",user->nick);
2882 WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
2885 void handle_time(char **parameters, int pcnt, userrec *user)
2888 struct tm * timeinfo;
2891 timeinfo = localtime ( &rawtime );
2892 WriteServ(user->fd,"391 %s %s :%s",user->nick,ServerName, asctime (timeinfo) );
2896 void handle_whois(char **parameters, int pcnt, userrec *user)
2901 if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
2903 dest = Find(parameters[0]);
2906 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
2907 if ((user == dest) || (strchr(user->modes,'o')))
2909 WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
2911 if (strcmp(chlist(dest),""))
2913 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
2915 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
2916 if (strcmp(dest->awaymsg,""))
2918 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
2920 if (strchr(dest->modes,'o'))
2922 WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
2924 //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
2925 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
2927 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
2931 /* no such nick/channel */
2932 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2936 void handle_quit(char **parameters, int pcnt, userrec *user)
2938 user_hash::iterator iter = clientlist.find(user->nick);
2940 /* theres more to do here, but for now just close the socket */
2943 if (parameters[0][0] == ':')
2947 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
2948 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
2949 WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
2953 Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
2954 WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
2955 WriteCommonExcept(user,"QUIT :Client exited");
2958 FOREACH_MOD OnUserQuit(user);
2960 /* confucious say, he who close nonblocking socket, get nothing! */
2963 NonBlocking(user->fd);
2966 if (iter != clientlist.end())
2968 log(DEBUG,"deleting user hash value");
2969 delete iter->second;
2970 clientlist.erase(iter);
2973 purge_empty_chans();
2976 void handle_who(char **parameters, int pcnt, userrec *user)
2980 /* theres more to do here, but for now just close the socket */
2983 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
2985 Ptr = user->chans[0].channel;
2986 printf(user->chans[0].channel->name);
2987 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
2989 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
2991 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);
2994 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
2997 if (parameters[0][0] = '#')
2999 Ptr = FindChan(parameters[0]);
3002 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3004 if ((has_channel(i->second,Ptr)) && (isnick(i->second->nick)))
3006 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);
3009 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3013 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3019 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
3021 Ptr = user->chans[0].channel;
3022 printf(user->chans[0].channel->name);
3023 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3025 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
3027 if (strchr(i->second->modes,'o'))
3029 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);
3033 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3039 void handle_wallops(char **parameters, int pcnt, userrec *user)
3041 WriteWallOps(user,"%s",parameters[0]);
3044 void handle_list(char **parameters, int pcnt, userrec *user)
3048 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
3049 for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
3051 if ((!i->second->c_private) && (!i->second->secret))
3053 WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
3056 WriteServ(user->fd,"323 %s :End of channel list.",user->nick);
3060 void handle_rehash(char **parameters, int pcnt, userrec *user)
3062 WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CONFIG_FILE);
3064 FOREACH_MOD OnRehash();
3065 WriteOpers("%s is rehashing config file %s",user->nick,CONFIG_FILE);
3071 return clientlist.size();
3074 int usercount_invisible(void)
3078 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3080 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
3085 int usercount_opers(void)
3089 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3091 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
3096 int usercount_unknown(void)
3100 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3102 if ((i->second->fd) && (i->second->registered != 7))
3110 return chanlist.size();
3113 int servercount(void)
3118 void handle_lusers(char **parameters, int pcnt, userrec *user)
3120 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),servercount());
3121 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
3122 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
3123 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
3124 WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,usercnt());
3127 void handle_admin(char **parameters, int pcnt, userrec *user)
3129 WriteServ(user->fd,"256 %s :Administrative info for %s",user->nick,ServerName);
3130 WriteServ(user->fd,"257 %s :Name - %s",user->nick,AdminName);
3131 WriteServ(user->fd,"258 %s :Nickname - %s",user->nick,AdminNick);
3132 WriteServ(user->fd,"258 %s :E-Mail - %s",user->nick,AdminEmail);
3135 void ShowMOTD(userrec *user)
3139 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
3142 WriteServ(user->fd,"375 %s :- %s message of the day",user->nick,ServerName);
3143 for (int i = 0; i != MOTD.size(); i++)
3145 WriteServ(user->fd,"372 %s :- %s",user->nick,MOTD[i].c_str());
3147 WriteServ(user->fd,"376 %s :End of %s message of the day.",user->nick,ServerName);
3150 void ShowRULES(userrec *user)
3154 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
3157 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
3158 for (int i = 0; i != RULES.size(); i++)
3160 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
3162 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
3165 /* shows the message of the day, and any other on-logon stuff */
3166 void ConnectUser(userrec *user)
3168 user->registered = 7;
3169 user->idle_lastmsg = time(NULL);
3170 log(DEBUG,"ConnectUser: %s",user->nick);
3172 if (strcmp(Passwd(user),"") && (!user->haspassed))
3174 Write(user->fd,"ERROR :Closing link: Invalid password");
3175 fdatasync(user->fd);
3176 kill_link(user,"Invalid password");
3181 Write(user->fd,"ERROR :Closing link: Unauthorized connection");
3182 fdatasync(user->fd);
3183 kill_link(user,"Unauthorised connection");
3186 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
3187 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
3188 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
3189 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
3190 WriteServ(user->fd,"004 %s :%s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
3191 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);
3192 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);
3194 FOREACH_MOD OnUserConnect(user);
3195 WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
3198 void handle_version(char **parameters, int pcnt, userrec *user)
3200 WriteServ(user->fd,"351 %s :%s %s :%s",user->nick,VERSION,ServerName,SYSTEM);
3203 void handle_ping(char **parameters, int pcnt, userrec *user)
3205 WriteServ(user->fd,"PONG %s :%s",ServerName,parameters[0]);
3208 void handle_pong(char **parameters, int pcnt, userrec *user)
3210 // set the user as alive so they survive to next ping
3214 void handle_motd(char **parameters, int pcnt, userrec *user)
3219 void handle_rules(char **parameters, int pcnt, userrec *user)
3224 void handle_user(char **parameters, int pcnt, userrec *user)
3226 if (user->registered < 3)
3228 WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
3229 strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
3230 strncat(user->ident,parameters[0],IDENTMAX);
3231 strncpy(user->fullname,parameters[3],128);
3232 user->registered = (user->registered | 1);
3236 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
3239 /* parameters 2 and 3 are local and remote hosts, ignored when sent by client connection */
3240 if (user->registered == 3)
3242 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
3247 void handle_userhost(char **parameters, int pcnt, userrec *user)
3249 char Return[MAXBUF],junk[MAXBUF];
3250 sprintf(Return,"302 %s :",user->nick);
3251 for (int i = 0; i < pcnt; i++)
3253 userrec *u = Find(parameters[i]);
3256 if (strchr(u->modes,'o'))
3258 sprintf(junk,"%s*=+%s@%s ",u->nick,u->ident,u->host);
3259 strcat(Return,junk);
3263 sprintf(junk,"%s=+%s@%s ",u->nick,u->ident,u->host);
3264 strcat(Return,junk);
3268 WriteServ(user->fd,Return);
3272 void handle_ison(char **parameters, int pcnt, userrec *user)
3274 char Return[MAXBUF];
3275 sprintf(Return,"303 %s :",user->nick);
3276 for (int i = 0; i < pcnt; i++)
3278 userrec *u = Find(parameters[i]);
3281 strcat(Return,u->nick);
3285 WriteServ(user->fd,Return);
3289 void handle_away(char **parameters, int pcnt, userrec *user)
3293 strcpy(user->awaymsg,parameters[0]);
3294 WriteServ(user->fd,"306 %s :You have been marked as being away",user->nick);
3298 strcpy(user->awaymsg,"");
3299 WriteServ(user->fd,"305 %s :You are no longer marked as being away",user->nick);
3303 void handle_whowas(char **parameters, int pcnt, userrec* user)
3305 user_hash::iterator i = whowas.find(parameters[0]);
3307 if (i == whowas.end())
3309 WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
3310 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3314 time_t rawtime = i->second->signon;
3318 timeinfo = localtime(&rawtime);
3319 strcpy(b,asctime(timeinfo));
3320 b[strlen(b)-1] = '\0';
3322 WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
3323 WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
3324 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3329 void handle_trace(char **parameters, int pcnt, userrec *user)
3331 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
3335 if (isnick(i->second->nick))
3337 if (strchr(i->second->modes,'o'))
3339 WriteServ(user->fd,"205 %s :Oper 0 %s",user->nick,i->second->nick);
3343 WriteServ(user->fd,"204 %s :User 0 %s",user->nick,i->second->nick);
3348 WriteServ(user->fd,"203 %s :???? 0 [%s]",user->nick,i->second->host);
3354 void handle_stats(char **parameters, int pcnt, userrec *user)
3360 if (strlen(parameters[0])>1)
3362 /* make the stats query 1 character long */
3363 parameters[0][1] = '\0';
3366 /* stats m (list number of times each command has been used, plus bytecount) */
3367 if (!strcasecmp(parameters[0],"m"))
3369 for (int i = 0; i < cmdlist.size(); i++)
3371 if (cmdlist[i].handler_function)
3373 if (cmdlist[i].use_count)
3375 /* RPL_STATSCOMMANDS */
3376 WriteServ(user->fd,"212 %s %s %d %d",user->nick,cmdlist[i].command,cmdlist[i].use_count,cmdlist[i].total_bytes);
3383 /* stats z (debug and memory info) */
3384 if (!strcasecmp(parameters[0],"z"))
3386 WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
3387 WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
3388 WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
3389 WriteServ(user->fd,"249 %s :MOTD(VECTOR) %d, RULES(VECTOR) %d",user->nick,MOTD.size(),RULES.size());
3390 WriteServ(user->fd,"249 %s :address_cache(HASH_MAP) %d (%d buckets)",user->nick,IP.size(),IP.bucket_count());
3391 WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
3392 WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
3393 WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
3397 if (!strcasecmp(parameters[0],"o"))
3399 for (int i = 0; i < ConfValueEnum("oper"); i++)
3401 char LoginName[MAXBUF];
3402 char HostName[MAXBUF];
3403 char OperType[MAXBUF];
3404 ConfValue("oper","name",i,LoginName);
3405 ConfValue("oper","host",i,HostName);
3406 ConfValue("oper","type",i,OperType);
3407 WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
3411 /* stats l (show user I/O stats) */
3412 if (!strcasecmp(parameters[0],"l"))
3414 WriteServ(user->fd,"211 %s :server:port nick bytes_in cmds_in bytes_out cmds_out",user->nick);
3415 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
3417 if (isnick(i->second->nick))
3419 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);
3423 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);
3429 /* stats u (show server uptime) */
3430 if (!strcasecmp(parameters[0],"u"))
3432 time_t current_time = 0;
3433 current_time = time(NULL);
3434 time_t server_uptime = current_time - startup_time;
3436 stime = gmtime(&server_uptime);
3437 /* i dont know who the hell would have an ircd running for over a year nonstop, but
3438 * Craig suggested this, and it seemed a good idea so in it went */
3439 if (stime->tm_year > 70)
3441 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);
3445 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);
3449 WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
3450 WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
3454 void handle_connect(char **parameters, int pcnt, userrec *user)
3456 WriteServ(user->fd,"NOTICE %s :*** Connecting to %s port %s...",user->nick,parameters[0],parameters[1]);
3457 if (!me[defaultRoute]->BeginLink(parameters[0],atoi(parameters[1]),"password"))
3459 WriteServ(user->fd,"NOTICE %s :*** Failed to send auth packet to %s!",user->nick,parameters[0]);
3463 void handle_squit(char **parameters, int pcnt, userrec *user)
3467 void handle_oper(char **parameters, int pcnt, userrec *user)
3469 char LoginName[MAXBUF];
3470 char Password[MAXBUF];
3471 char OperType[MAXBUF];
3472 char TypeName[MAXBUF];
3473 char Hostname[MAXBUF];
3476 for (i = 0; i < ConfValueEnum("oper"); i++)
3478 ConfValue("oper","name",i,LoginName);
3479 ConfValue("oper","password",i,Password);
3480 if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
3482 /* correct oper credentials */
3483 ConfValue("oper","type",i,OperType);
3484 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
3485 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
3486 WriteServ(user->fd,"MODE %s :+o",user->nick);
3487 for (j =0; j < ConfValueEnum("type"); j++)
3489 ConfValue("type","name",j,TypeName);
3490 if (!strcmp(TypeName,OperType))
3492 /* found this oper's opertype */
3493 ConfValue("type","host",j,Hostname);
3494 strncpy(user->dhost,Hostname,256);
3497 if (!strchr(user->modes,'o'))
3499 strcat(user->modes,"o");
3505 WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
3506 WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
3509 void handle_nick(char **parameters, int pcnt, userrec *user)
3513 log(DEBUG,"not enough params for handle_nick");
3518 log(DEBUG,"invalid parameter passed to handle_nick");
3521 if (!strlen(parameters[0]))
3523 log(DEBUG,"zero length new nick passed to handle_nick");
3528 log(DEBUG,"invalid user passed to handle_nick");
3533 log(DEBUG,"invalid old nick passed to handle_nick");
3536 if (!strcasecmp(user->nick,parameters[0]))
3538 log(DEBUG,"old nick is new nick, skipping");
3543 if (strlen(parameters[0]) > 1)
3545 if (parameters[0][0] == ':')
3550 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
3552 WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
3556 if (isnick(parameters[0]) == 0)
3558 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
3562 if (user->registered == 7)
3564 WriteCommon(user,"NICK %s",parameters[0]);
3567 /* change the nick of the user in the users_hash */
3568 user = ReHashNick(user->nick, parameters[0]);
3569 /* actually change the nick within the record */
3571 if (!user->nick) return;
3573 strncpy(user->nick, parameters[0],NICKMAX);
3575 log(DEBUG,"new nick set: %s",user->nick);
3577 if (user->registered < 3)
3578 user->registered = (user->registered | 2);
3579 if (user->registered == 3)
3581 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
3584 log(DEBUG,"exit nickchange: %s",user->nick);
3587 int process_parameters(char **command_p,char *parameters)
3592 q = strlen(parameters);
3595 /* no parameters, command_p invalid! */
3598 if (parameters[0] == ':')
3600 command_p[0] = parameters+1;
3605 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
3607 /* only one parameter */
3608 command_p[0] = parameters;
3609 if (parameters[0] == ':')
3611 if (strchr(parameters,' ') != NULL)
3619 command_p[j++] = parameters;
3620 for (i = 0; i <= q; i++)
3622 if (parameters[i] == ' ')
3624 command_p[j++] = parameters+i+1;
3625 parameters[i] = '\0';
3626 if (command_p[j-1][0] == ':')
3628 *command_p[j-1]++; /* remove dodgy ":" */
3630 /* parameter like this marks end of the sequence */
3634 return j; /* returns total number of items in the list */
3637 void process_command(userrec *user, char* cmd)
3641 char *command_p[127];
3642 char p[MAXBUF], temp[MAXBUF];
3643 int i, j, items, cmd_found;
3645 for (int i = 0; i < 127; i++)
3646 command_p[i] = NULL;
3656 if (!strcmp(cmd,""))
3661 if (!strchr(cmd,' '))
3663 /* no parameters, lets skip the formalities and not chop up
3666 command_p[0] = NULL;
3668 for (int i = 0; i <= strlen(cmd); i++)
3670 cmd[i] = toupper(cmd[i]);
3677 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
3678 for (i = 0; i < strlen(temp); i++)
3680 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
3686 /* split the full string into a command plus parameters */
3690 if (strchr(cmd,' '))
3692 for (i = 0; i <= strlen(cmd); i++)
3694 /* capitalise the command ONLY, leave params intact */
3695 cmd[i] = toupper(cmd[i]);
3696 /* are we nearly there yet?! :P */
3700 parameters = cmd+i+1;
3708 for (i = 0; i <= strlen(cmd); i++)
3710 cmd[i] = toupper(cmd[i]);
3718 for (i = 0; i != cmdlist.size(); i++)
3720 if (strcmp(cmdlist[i].command,""))
3722 if (!strcmp(command, cmdlist[i].command))
3726 if (strcmp(parameters,""))
3728 items = process_parameters(command_p,parameters);
3733 command_p[0] = NULL;
3739 command_p[0] = NULL;
3744 user->idle_lastmsg = time(NULL);
3745 /* activity resets the ping pending timer */
3746 user->nping = time(NULL) + 120;
3747 if ((items) < cmdlist[i].min_params)
3749 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
3750 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
3753 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
3755 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
3756 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
3760 /* if the command isnt USER, PASS, or NICK, and nick is empty,
3762 if ((strcmp(command,"USER")) && (strcmp(command,"NICK")) && (strcmp(command,"PASS")))
3764 if ((!isnick(user->nick)) || (user->registered != 7))
3766 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
3767 WriteServ(user->fd,"451 %s :You have not registered",command);
3771 if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
3773 log(DEBUG,"process_command: handler: %s %s %d",user->nick,command,items);
3774 if (cmdlist[i].handler_function)
3776 /* ikky /stats counters */
3781 user->bytes_in += strlen(temp);
3784 cmdlist[i].use_count++;
3785 cmdlist[i].total_bytes+=strlen(temp);
3788 /* WARNING: nothing may come after the
3789 * command handler call, as the handler
3790 * may free the user structure! */
3792 cmdlist[i].handler_function(command_p,items,user);
3798 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
3799 WriteServ(user->fd,"451 %s :You have not registered",command);
3807 if ((!cmd_found) && (user))
3809 log(DEBUG,"process_command: not in table: %s %s",user->nick,command);
3810 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
3815 void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
3818 /* create the command and push it onto the table */
3819 strcpy(comm.command,cmd);
3820 comm.handler_function = f;
3821 comm.flags_needed = flags;
3822 comm.min_params = minparams;
3824 comm.total_bytes = 0;
3825 cmdlist.push_back(comm);
3828 void SetupCommandTable(void)
3830 createcommand("USER",handle_user,0,4);
3831 createcommand("NICK",handle_nick,0,1);
3832 createcommand("QUIT",handle_quit,0,1);
3833 createcommand("VERSION",handle_version,0,0);
3834 createcommand("PING",handle_ping,0,1);
3835 createcommand("PONG",handle_pong,0,1);
3836 createcommand("ADMIN",handle_admin,0,0);
3837 createcommand("PRIVMSG",handle_privmsg,0,2);
3838 createcommand("INFO",handle_info,0,0);
3839 createcommand("TIME",handle_time,0,0);
3840 createcommand("WHOIS",handle_whois,0,1);
3841 createcommand("WALLOPS",handle_wallops,'o',1);
3842 createcommand("NOTICE",handle_notice,0,2);
3843 createcommand("JOIN",handle_join,0,1);
3844 createcommand("NAMES",handle_names,0,1);
3845 createcommand("PART",handle_part,0,1);
3846 createcommand("KICK",handle_kick,0,2);
3847 createcommand("MODE",handle_mode,0,1);
3848 createcommand("TOPIC",handle_topic,0,1);
3849 createcommand("WHO",handle_who,0,1);
3850 createcommand("MOTD",handle_motd,0,0);
3851 createcommand("RULES",handle_join,0,0);
3852 createcommand("OPER",handle_oper,0,2);
3853 createcommand("LIST",handle_list,0,0);
3854 createcommand("DIE",handle_die,'o',1);
3855 createcommand("RESTART",handle_restart,'o',1);
3856 createcommand("KILL",handle_kill,'o',2);
3857 createcommand("REHASH",handle_rehash,'o',0);
3858 createcommand("LUSERS",handle_lusers,0,0);
3859 createcommand("STATS",handle_stats,0,1);
3860 createcommand("USERHOST",handle_userhost,0,1);
3861 createcommand("AWAY",handle_away,0,0);
3862 createcommand("ISON",handle_ison,0,0);
3863 createcommand("SUMMON",handle_summon,0,0);
3864 createcommand("USERS",handle_users,0,0);
3865 createcommand("INVITE",handle_invite,0,2);
3866 createcommand("PASS",handle_pass,0,1);
3867 createcommand("TRACE",handle_trace,'o',0);
3868 createcommand("WHOWAS",handle_whowas,0,1);
3869 createcommand("CONNECT",handle_connect,'o',2);
3870 createcommand("SQUIT",handle_squit,'o',1);
3873 void process_buffer(userrec *user)
3881 if (!strcmp(user->inbuf,""))
3885 strncpy(cmd,user->inbuf,MAXBUF);
3886 if (!strcmp(cmd,""))
3890 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
3892 cmd[strlen(cmd)-1] = '\0';
3894 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
3896 cmd[strlen(cmd)-1] = '\0';
3898 strcpy(user->inbuf,"");
3899 if (!strcmp(cmd,""))
3903 log(DEBUG,"InspIRCd: processing: %s %s",user->nick,cmd);
3904 process_command(user,cmd);
3907 void handle_link_packet(char* udp_msg, char* udp_host, int udp_port, serverrec *serv)
3913 struct sockaddr_in client, server;
3914 char addrs[MAXBUF][255];
3915 int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
3917 int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
3918 int selectResult = 0;
3919 char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
3920 char resolvedHost[MAXBUF];
3924 log(DEBUG,"InspIRCd: startup: begin");
3928 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
3930 log(DEBUG,"InspIRCd: startup: not starting with UID 0!");
3932 SetupCommandTable();
3933 log(DEBUG,"InspIRCd: startup: default command table set up");
3936 if (strcmp(DieValue,""))
3938 printf("WARNING: %s\n\n",DieValue);
3941 log(DEBUG,"InspIRCd: startup: read config");
3943 int count2 = 0, count3 = 0;
3944 for (count = 0; count < ConfValueEnum("bind"); count++)
3946 ConfValue("bind","port",count,configToken);
3947 ConfValue("bind","address",count,Addr);
3948 ConfValue("bind","type",count,Type);
3949 if (!strcmp(Type,"clients"))
3951 ports[count2] = atoi(configToken);
3952 strcpy(addrs[count2],Addr);
3957 char Default[MAXBUF];
3958 strcpy(Default,"no");
3959 ConfValue("bind","default",count,Default);
3960 if (strchr(Default,'y'))
3962 defaultRoute = count3;
3963 log(DEBUG,"InspIRCd: startup: binding '%s:%s' is default server route",Addr,configToken);
3965 me[count3] = new serverrec(ServerName,100L,false);
3966 me[count3]->CreateListener(Addr,atoi(configToken));
3969 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
3972 UDPportCount = count3;
3974 log(DEBUG,"InspIRCd: startup: read %d total client ports and %d total server ports",portCount,UDPportCount);
3976 log(DEBUG,"InspIRCd: startup: InspIRCd is now running!");
3979 for (count = 0; count < ConfValueEnum("module"); count++)
3981 char modfile[MAXBUF];
3982 ConfValue("module","name",count,configToken);
3983 sprintf(modfile,"%s/%s",MOD_PATH,configToken);
3984 printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
3985 log(DEBUG,"InspIRCd: startup: Loading module: %s",modfile);
3987 factory[count] = new ircd_module(modfile);
3988 if (factory[count]->LastError())
3990 log(DEBUG,"Unable to load %s: %s",modfile,factory[count]->LastError());
3991 sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
3994 if (factory[count]->factory)
3996 modules[count] = factory[count]->factory->CreateModule();
3997 /* save the module and the module's classfactory, if
3998 * this isnt done, random crashes can occur :/ */
4002 log(DEBUG,"Unable to load %s",modfile);
4003 sprintf("Unable to load %s\nExiting...\n",modfile);
4007 MODCOUNT = count - 1;
4008 log(DEBUG,"Total loaded modules: %d",MODCOUNT+1);
4010 servers = new server_list;
4013 printf("\nInspIRCd is now running!\n");
4015 startup_time = time(NULL);
4017 if (DaemonSeed() == ERROR)
4019 log(DEBUG,"InspIRCd: startup: can't daemonise");
4020 printf("ERROR: could not go into daemon mode. Shutting down.\n");
4025 /* setup select call */
4026 FD_ZERO(&selectFds);
4027 log(DEBUG,"InspIRCd: startup: zero selects");
4029 for (count = 0; count < portCount; count++)
4031 if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
4033 log(DEBUG,"InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
4036 if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
4038 log(DEBUG,"InspIRCd: startup: failed to bind port %d",ports[count]);
4040 else /* well we at least bound to one socket so we'll continue */
4046 log(DEBUG,"InspIRCd: startup: total bound ports %d",boundPortCount);
4048 /* if we didn't bind to anything then abort */
4049 if (boundPortCount == 0)
4051 log(DEBUG,"InspIRCd: startup: no ports bound, bailing!");
4055 length = sizeof (client);
4056 int flip_flop = 0, udp_port = 0;
4057 char udp_msg[MAXBUF], udp_host[MAXBUF];
4059 /* main loop for multiplexing/resetting */
4062 /* set up select call */
4063 for (count = 0; count < boundPortCount; count++)
4065 FD_SET (openSockfd[count], &selectFds);
4068 /* added timeout! select was waiting forever... wank... :/ */
4079 selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
4081 for (int x = 0; x != UDPportCount; x++)
4083 if (me[x]->RecvPacket(udp_msg, udp_host, udp_port))
4085 FOREACH_MOD OnPacketReceive(udp_msg);
4086 WriteOpers("UDP Link Packet: '%s' from %s:%d:%d [route%d]",udp_msg,udp_host,udp_port,me[x]->port,x);
4087 // Packets must go back via the route they arrived on :)
4088 handle_link_packet(udp_msg, udp_host, udp_port, me[x]);
4092 for (user_hash::iterator count2 = clientlist.begin(); count2 != clientlist.end(); count2++)
4096 if (!count2->second) break;
4099 if (count2->second->fd)
4101 if (((time(NULL)) > count2->second->nping) && (isnick(count2->second->nick)) && (count2->second->registered == 7))
4103 if (!count2->second->lastping)
4105 log(DEBUG,"InspIRCd: ping timeout: %s",count2->second->nick);
4106 kill_link(count2->second,"Ping timeout");
4109 Write(count2->second->fd,"PING :%s",ServerName);
4110 log(DEBUG,"InspIRCd: pinging: %s",count2->second->nick);
4111 count2->second->lastping = 0;
4112 count2->second->nping = time(NULL)+120;
4115 result = read(count2->second->fd, data, 1);
4116 // result EAGAIN means nothing read
4117 if (result == EAGAIN)
4123 log(DEBUG,"InspIRCd: Exited: %s",count2->second->nick);
4124 kill_link(count2->second,"Client exited");
4126 else if (result > 0)
4128 strncat(count2->second->inbuf, data, result);
4129 if (strchr(count2->second->inbuf, '\n') || strchr(count2->second->inbuf, '\r'))
4131 /* at least one complete line is waiting to be processed */
4132 if (!count2->second->fd)
4136 process_buffer(count2->second);
4144 /* select is reporting a waiting socket. Poll them all to find out which */
4145 if (selectResult > 0)
4147 char target[MAXBUF], resolved[MAXBUF];
4148 for (count = 0; count < boundPortCount; count++)
4150 if (FD_ISSET (openSockfd[count], &selectFds))
4152 incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
4154 address_cache::iterator iter = IP.find(client.sin_addr);
4155 bool iscached = false;
4156 if (iter == IP.end())
4158 /* ip isn't in cache, add it */
4159 strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
4160 if(CleanAndResolve(resolved, target) != TRUE)
4162 strncpy(resolved,target,MAXBUF);
4164 /* hostname now in 'target' */
4165 IP[client.sin_addr] = new string(resolved);
4166 /* hostname in cache */
4170 /* found ip (cached) */
4171 strncpy(resolved, iter->second->c_str(), MAXBUF);
4175 if (incomingSockfd < 0)
4177 WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
4178 log(DEBUG,"InspIRCd: accept failed: %d",ports[count]);
4182 AddClient(incomingSockfd, resolved, ports[count], iscached);
4183 log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
4192 close (incomingSockfd);