1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * Inspire is copyright (C) 2002-2003 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 /* Now with added unF! ;) */
20 #include "inspircd_io.h"
21 #include "inspircd_util.h"
22 #include "inspircd_config.h"
25 #include <sys/errno.h>
26 #include <sys/ioctl.h>
27 #include <sys/utsname.h>
32 #include <ext/hash_map>
41 #include "connection.h"
53 #define nspace __gnu_cxx
58 int LogLevel = DEFAULT;
59 char ServerName[MAXBUF];
61 char ServerDesc[MAXBUF];
62 char AdminName[MAXBUF];
63 char AdminEmail[MAXBUF];
64 char AdminNick[MAXBUF];
66 char restartpass[MAXBUF];
70 char PrefixQuit[MAXBUF];
71 char DieValue[MAXBUF];
73 int WHOWAS_STALE = 48; // default WHOWAS Entries last 2 days before they go 'stale'
74 int WHOWAS_MAX = 100; // default 100 people maximum in the WHOWAS list
76 time_t startup_time = time(NULL);
78 extern vector<Module*> modules;
79 extern vector<ircd_module*> factory;
85 template<> struct nspace::hash<in_addr>
87 size_t operator()(const struct in_addr &a) const
90 memcpy(&q,&a,sizeof(size_t));
95 template<> struct nspace::hash<string>
97 size_t operator()(const string &s) const
100 static struct hash<const char *> strhash;
112 bool operator()(const string& s1, const string& s2) const
114 char a[MAXBUF],b[MAXBUF];
115 strcpy(a,s1.c_str());
116 strcpy(b,s2.c_str());
117 return (strcasecmp(a,b) == 0);
122 struct InAddr_HashComp
125 bool operator()(const in_addr &s1, const in_addr &s2) const
130 memcpy(&q,&s1,sizeof(size_t));
131 memcpy(&p,&s2,sizeof(size_t));
139 typedef nspace::hash_map<std::string, userrec*, nspace::hash<string>, StrHashComp> user_hash;
140 typedef nspace::hash_map<std::string, chanrec*, nspace::hash<string>, StrHashComp> chan_hash;
141 typedef nspace::hash_map<in_addr,string*, nspace::hash<in_addr>, InAddr_HashComp> address_cache;
142 typedef std::deque<command_t> command_table;
145 server_list* servers;
147 user_hash clientlist;
150 command_table cmdlist;
157 struct linger linger = { 0 };
158 char bannerBuffer[MAXBUF];
159 int boundPortCount = 0;
160 int portCount = 0, UDPportCount = 0, ports[MAXSOCKS];
161 int defaultRoute = 0;
165 int has_channel(userrec *u, chanrec *c);
166 int usercount(chanrec *c);
167 int usercount_i(chanrec *c);
168 void update_stats_l(int fd,int data_out);
169 char* Passwd(userrec *user);
170 bool IsDenied(userrec *user);
171 void AddWhoWas(userrec* u);
174 void safedelete(userrec *p)
178 log(DEBUG,"deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
179 log(DEBUG,"safedelete(userrec*): pointer is safe to delete");
184 log(DEBUG,"safedelete(userrec*): unsafe pointer operation squished");
188 void safedelete(chanrec *p)
193 log(DEBUG,"safedelete(chanrec*): pointer is safe to delete");
197 log(DEBUG,"safedelete(chanrec*): unsafe pointer operation squished");
202 /* chop a string down to 512 characters and preserve linefeed (irc max
209 FOREACH_MOD OnServerRaw(temp,false);
210 const char* str2 = temp.c_str();
211 sprintf(str,"%s",str2);
214 if (strlen(str) > 512)
223 std::string getservername()
228 std::string getserverdesc()
233 std::string getnetworkname()
238 std::string getadminname()
243 std::string getadminemail()
248 std::string getadminnick()
253 void log(int level,char *text, ...)
255 char textbuffer[MAXBUF];
259 struct tm * timeinfo;
260 if (level < LogLevel)
264 timeinfo = localtime (&rawtime);
266 f = fopen("ircd.log","a+");
270 va_start (argsPtr, text);
271 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
273 strcpy(b,asctime(timeinfo));
274 b[strlen(b)-1] = ':';
275 fprintf(f,"%s %s\n",b,textbuffer);
280 printf("Can't write log file, bailing!!!");
285 void readfile(file_cache &F, const char* fname)
288 char linebuf[MAXBUF];
290 log(DEBUG,"readfile: loading %s",fname);
292 file = fopen(fname,"r");
297 fgets(linebuf,sizeof(linebuf),file);
298 linebuf[strlen(linebuf)-1]='\0';
299 if (!strcmp(linebuf,""))
305 F.push_back(linebuf);
312 log(DEBUG,"readfile: failed to load file: %s",fname);
314 log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
317 void ReadConfig(void)
319 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF];
322 ConfValue("server","name",0,ServerName);
323 ConfValue("server","description",0,ServerDesc);
324 ConfValue("server","network",0,Network);
325 ConfValue("admin","name",0,AdminName);
326 ConfValue("admin","email",0,AdminEmail);
327 ConfValue("admin","nick",0,AdminNick);
328 ConfValue("files","motd",0,motd);
329 ConfValue("files","rules",0,rules);
330 ConfValue("power","diepass",0,diepass);
331 ConfValue("power","pause",0,pauseval);
332 ConfValue("power","restartpass",0,restartpass);
333 ConfValue("options","prefixquit",0,PrefixQuit);
334 ConfValue("die","value",0,DieValue);
335 ConfValue("options","loglevel",0,dbg);
336 if (!strcmp(dbg,"debug"))
338 if (!strcmp(dbg,"verbose"))
340 if (!strcmp(dbg,"default"))
342 if (!strcmp(dbg,"sparse"))
344 if (!strcmp(dbg,"none"))
346 readfile(RULES,rules);
347 log(DEBUG,"Reading connect classes");
349 for (int i = 0; i < ConfValueEnum("connect"); i++)
352 ConfValue("connect","allow",i,Value);
353 if (strcmp(Value,""))
355 strcpy(c.host,Value);
358 ConfValue("connect","password",i,Value);
359 strcpy(c.pass,Value);
360 Classes.push_back(c);
361 log(DEBUG,"Read connect class type ALLOW, host=%s password=%s",c.host,c.pass);
365 ConfValue("connect","deny",i,Value);
366 strcpy(c.host,Value);
368 Classes.push_back(c);
369 log(DEBUG,"Read connect class type DENY, host=%s",c.host);
378 log(DEBUG,"Blocking: %d",s);
379 flags = fcntl(s, F_GETFL, 0);
380 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
383 void NonBlocking(int s)
386 log(DEBUG,"NonBlocking: %d",s);
387 flags = fcntl(s, F_GETFL, 0);
388 fcntl(s, F_SETFL, flags | O_NONBLOCK);
392 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
394 struct hostent *hostPtr = NULL;
397 memset (resolvedHost, '\0',MAXBUF);
398 if(unresolvedHost == NULL)
400 if ((inet_aton(unresolvedHost,&addr)) == 0)
402 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
404 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
406 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
410 /* write formatted text to a socket, in same format as printf */
412 void Write(int sock,char *text, ...)
414 char textbuffer[MAXBUF];
418 va_start (argsPtr, text);
419 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
421 sprintf(tb,"%s\r\n",textbuffer);
423 write(sock,tb,strlen(tb));
424 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
427 /* write a server formatted numeric response to a single socket */
429 void WriteServ(int sock, char* text, ...)
431 char textbuffer[MAXBUF],tb[MAXBUF];
433 va_start (argsPtr, text);
435 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
437 sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
439 write(sock,tb,strlen(tb));
440 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
443 /* write text from an originating user to originating user */
445 void WriteFrom(int sock, userrec *user,char* text, ...)
447 char textbuffer[MAXBUF],tb[MAXBUF];
449 va_start (argsPtr, text);
451 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
453 sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
455 write(sock,tb,strlen(tb));
456 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
459 /* write text to an destination user from a source user (e.g. user privmsg) */
461 void WriteTo(userrec *source, userrec *dest,char *data, ...)
463 char textbuffer[MAXBUF],tb[MAXBUF];
465 va_start (argsPtr, data);
466 if ((!dest) || (!source))
470 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
473 WriteFrom(dest->fd,source,"%s",textbuffer);
476 /* write formatted text from a source user to all users on a channel
477 * including the sender (NOT for privmsg, notice etc!) */
479 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
481 char textbuffer[MAXBUF];
483 va_start (argsPtr, text);
484 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
486 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
488 if (has_channel(i->second,Ptr))
490 WriteTo(user,i->second,"%s",textbuffer);
495 /* write formatted text from a source user to all users on a channel except
496 * for the sender (for privmsg etc) */
498 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
500 char textbuffer[MAXBUF];
502 va_start (argsPtr, text);
503 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
506 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
508 if (has_channel(i->second,Ptr) && (user != i->second))
510 WriteTo(user,i->second,"%s",textbuffer);
515 int c_count(userrec* u)
518 for (int i =0; i != MAXCHANS; i++)
519 if (u->chans[i].channel)
525 /* return 0 or 1 depending if users u and u2 share one or more common channels
526 * (used by QUIT, NICK etc which arent channel specific notices) */
528 int common_channels(userrec *u, userrec *u2)
537 for (i = 0; i != MAXCHANS; i++)
539 for (z = 0; z != MAXCHANS; z++)
541 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
543 if ((c_count(u)) && (c_count(u2)))
553 /* write a formatted string to all users who share at least one common
554 * channel, including the source user e.g. for use in NICK */
556 void WriteCommon(userrec *u, char* text, ...)
558 char textbuffer[MAXBUF];
560 va_start (argsPtr, text);
561 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
564 WriteFrom(u->fd,u,"%s",textbuffer);
566 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
568 if (common_channels(u,i->second) && (i->second != u))
570 WriteFrom(i->second->fd,u,"%s",textbuffer);
575 /* write a formatted string to all users who share at least one common
576 * channel, NOT including the source user e.g. for use in QUIT */
578 void WriteCommonExcept(userrec *u, char* text, ...)
580 char textbuffer[MAXBUF];
582 va_start (argsPtr, text);
583 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
586 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
588 if ((common_channels(u,i->second)) && (u != i->second))
590 WriteFrom(i->second->fd,u,"%s",textbuffer);
595 void WriteOpers(char* text, ...)
597 char textbuffer[MAXBUF];
599 va_start (argsPtr, text);
600 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
603 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
605 if (strchr(i->second->modes,'o'))
607 if (strchr(i->second->modes,'s'))
609 // send server notices to all with +s
610 // (TODO: needs SNOMASKs)
611 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
617 void WriteWallOps(userrec *source, char* text, ...)
620 char textbuffer[MAXBUF];
622 va_start (argsPtr, text);
623 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
626 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
628 if (strchr(i->second->modes,'w'))
630 WriteTo(source,i->second,"WALLOPS %s",textbuffer);
635 /* convert a string to lowercase. Note following special circumstances
636 * taken from RFC 1459. Many "official" server branches still hold to this
637 * rule so i will too;
639 * Because of IRC's scandanavian origin, the characters {}| are
640 * considered to be the lower case equivalents of the characters []\,
641 * respectively. This is a critical issue when determining the
642 * equivalence of two nicknames.
645 void strlower(char *n)
651 for (int i = 0; i != strlen(n); i++)
653 n[i] = tolower(n[i]);
663 /* verify that a user's ident and nickname is valid */
665 int isident(const char* n)
678 for (i = 0; i != strlen(n); i++)
680 if ((n[i] < 33) || (n[i] > 125))
684 /* can't occur ANYWHERE in an Ident! */
685 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
694 int isnick(const char* n)
706 if (strlen(n) > NICKMAX-1)
710 for (i = 0; i != strlen(n); i++)
712 if ((n[i] < 33) || (n[i] > 125))
716 /* can't occur ANYWHERE in a nickname! */
717 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
721 /* can't occur as the first char of a nickname... */
722 if ((strchr("0123456789",n[i])) && (!i))
730 /* Find a user record by nickname and return a pointer to it */
732 userrec* Find(string nick)
734 user_hash::iterator iter = clientlist.find(nick);
736 if (iter == clientlist.end())
737 /* Couldn't find it */
743 void update_stats_l(int fd,int data_out) /* add one line-out to stats L for this fd */
745 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
747 if (i->second->fd == fd)
749 i->second->bytes_out+=data_out;
750 i->second->cmds_out++;
756 /* find a channel record by channel name and return a pointer to it */
758 chanrec* FindChan(const char* chan)
760 chan_hash::iterator iter = chanlist.find(chan);
762 if (iter == chanlist.end())
763 /* Couldn't find it */
770 void purge_empty_chans(void)
772 int go_again = 1, purge = 0;
777 for (chan_hash::iterator i = chanlist.begin(); i != chanlist.end(); i++)
780 if (!usercount(i->second))
782 /* kill the record */
783 if (i != chanlist.end())
785 log(DEBUG,"del_channel: destroyed: %s",i->second->name);
796 log(DEBUG,"completed channel purge, killed %d",purge);
799 /* returns the status character for a given user on a channel, e.g. @ for op,
800 * % for halfop etc. If the user has several modes set, the highest mode
801 * the user has must be returned. */
803 char* cmode(userrec *user, chanrec *chan)
806 for (i = 0; i != MAXCHANS; i++)
808 if ((user->chans[i].channel == chan) && (chan != NULL))
810 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
814 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
818 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
827 char scratch[MAXMODES];
829 char* chanmodes(chanrec *chan)
832 if (chan->noexternal)
840 if (strcmp(chan->key,""))
848 if (chan->inviteonly)
864 if (strcmp(chan->key,""))
867 strcat(scratch,chan->key);
872 sprintf(foo," %d",chan->limit);
875 log(DEBUG,"chanmodes: %s %s",chan->name,scratch);
879 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
880 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
881 * highest mode the user has must be returned. */
883 int cstatus(userrec *user, chanrec *chan)
886 for (i = 0; i != MAXCHANS; i++)
888 if ((user->chans[i].channel == chan) && (chan != NULL))
890 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
894 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
898 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
902 return STATUS_NORMAL;
908 /* compile a userlist of a channel into a string, each nick seperated by
909 * spaces and op, voice etc status shown as @ and + */
911 void userlist(userrec *user,chanrec *c)
913 sprintf(list,"353 %s = %s :", user->nick, c->name);
914 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
916 if (has_channel(i->second,c))
918 if (isnick(i->second->nick))
920 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
922 /* user is +i, and source not on the channel, does not show
923 * nick in NAMES list */
926 strcat(list,cmode(i->second,c));
927 strcat(list,i->second->nick);
929 if (strlen(list)>(480-NICKMAX))
931 /* list overflowed into
932 * multiple numerics */
933 WriteServ(user->fd,list);
934 sprintf(list,"353 %s = %s :", user->nick, c->name);
939 /* if whats left in the list isnt empty, send it */
940 if (list[strlen(list)-1] != ':')
942 WriteServ(user->fd,list);
946 /* return a count of the users on a specific channel accounting for
947 * invisible users who won't increase the count. e.g. for /LIST */
949 int usercount_i(chanrec *c)
955 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
957 if (has_channel(i->second,c))
959 if (isnick(i->second->nick))
961 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
963 /* user is +i, and source not on the channel, does not show
964 * nick in NAMES list */
971 log(DEBUG,"usercount_i: %s %d",c->name,count);
976 int usercount(chanrec *c)
982 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
984 if (has_channel(i->second,c))
986 if (isnick(i->second->nick))
992 log(DEBUG,"usercount: %s %d",c->name,count);
997 /* add a channel to a user, creating the record for it if needed and linking
998 * it to the user record */
1000 chanrec* add_channel(userrec *user, char* cname, char* key)
1006 if ((!cname) || (!user))
1010 if (strlen(cname) > CHANMAX-1)
1012 cname[CHANMAX-1] = '\0';
1015 log(DEBUG,"add_channel: %s %s",user->nick,cname);
1017 if ((has_channel(user,FindChan(cname))) && (FindChan(cname)))
1019 return NULL; // already on the channel!
1022 if (!FindChan(cname))
1024 /* create a new one */
1025 log(DEBUG,"add_channel: creating: %s",cname);
1027 chanlist[cname] = new chanrec();
1029 strcpy(chanlist[cname]->name, cname);
1030 chanlist[cname]->topiclock = 1;
1031 chanlist[cname]->noexternal = 1;
1032 chanlist[cname]->created = time(NULL);
1033 strcpy(chanlist[cname]->topic, "");
1034 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
1035 chanlist[cname]->topicset = 0;
1036 Ptr = chanlist[cname];
1037 log(DEBUG,"add_channel: created: %s",cname);
1038 /* set created to 2 to indicate user
1039 * is the first in the channel
1040 * and should be given ops */
1046 /* channel exists, just fish out a pointer to its struct */
1047 Ptr = FindChan(cname);
1050 log(DEBUG,"add_channel: joining to: %s",Ptr->name);
1051 if (strcmp(Ptr->key,""))
1053 log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
1056 log(DEBUG,"add_channel: no key given in JOIN");
1057 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
1062 log(DEBUG,"key at %p is %s",key,key);
1063 if (strcasecmp(key,Ptr->key))
1065 log(DEBUG,"add_channel: bad key given in JOIN");
1066 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
1072 if (Ptr->inviteonly)
1074 if (user->IsInvited(Ptr->name))
1076 /* user was invited to channel */
1077 /* there may be an optional channel NOTICE here */
1081 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
1088 if (usercount(Ptr) == Ptr->limit)
1090 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
1095 /* check user against the channel banlist */
1096 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1098 if (match(user->GetFullHost(),i->data))
1100 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
1105 user->RemoveInvite(Ptr->name);
1112 for (i =0; i != MAXCHANS; i++)
1114 if (user->chans[i].channel == NULL)
1118 /* first user in is given ops */
1119 user->chans[i].uc_modes = UCMODE_OP;
1123 user->chans[i].uc_modes = 0;
1125 user->chans[i].channel = Ptr;
1126 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
1129 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
1130 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
1133 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
1134 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name,chanmodes(Ptr));
1135 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1136 FOREACH_MOD OnUserJoin(user,Ptr);
1140 log(DEBUG,"add_channel: user channel max exceeded: %s %s",user->nick,cname);
1141 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
1145 /* remove a channel from a users record, and remove the record from memory
1146 * if the channel has become empty */
1148 chanrec* del_channel(userrec *user, char* cname, char* reason)
1154 if ((!cname) || (!user))
1159 Ptr = FindChan(cname);
1166 FOREACH_MOD OnUserPart(user,Ptr);
1167 log(DEBUG,"del_channel: removing: %s %s",user->nick,Ptr->name);
1169 for (i =0; i != MAXCHANS; i++)
1171 /* zap it from the channel list of the user */
1172 if (user->chans[i].channel == Ptr)
1176 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
1180 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
1182 user->chans[i].uc_modes = 0;
1183 user->chans[i].channel = NULL;
1184 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1189 /* if there are no users left on the channel */
1190 if (!usercount(Ptr))
1192 chan_hash::iterator iter = chanlist.find(Ptr->name);
1194 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1196 /* kill the record */
1197 if (iter != chanlist.end())
1199 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1200 delete iter->second;
1201 chanlist.erase(iter);
1207 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
1212 if ((!Ptr) || (!user) || (!src))
1217 log(DEBUG,"kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
1219 if (!has_channel(user,Ptr))
1221 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
1224 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
1226 if (cstatus(src,Ptr) == STATUS_HOP)
1228 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
1232 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
1238 for (i =0; i != MAXCHANS; i++)
1240 /* zap it from the channel list of the user */
1241 if (user->chans[i].channel == Ptr)
1243 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
1244 user->chans[i].uc_modes = 0;
1245 user->chans[i].channel = NULL;
1246 log(DEBUG,"del_channel: unlinked: %s %s",user->nick,Ptr->name);
1251 /* if there are no users left on the channel */
1252 if (!usercount(Ptr))
1254 chan_hash::iterator iter = chanlist.find(Ptr->name);
1256 log(DEBUG,"del_channel: destroying channel: %s",Ptr->name);
1258 /* kill the record */
1259 if (iter != chanlist.end())
1261 log(DEBUG,"del_channel: destroyed: %s",Ptr->name);
1262 delete iter->second;
1263 chanlist.erase(iter);
1269 /* returns 1 if user u has channel c in their record, 0 if not */
1271 int has_channel(userrec *u, chanrec *c)
1279 for (i =0; i != MAXCHANS; i++)
1281 if (u->chans[i].channel == c)
1289 int give_ops(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);
1307 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_OP)
1324 /* mode already set on user, dont allow multiple */
1327 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
1328 log(DEBUG,"gave ops: %s %s",d->chans[i].channel->name,d->nick);
1336 int give_hops(userrec *user,char *dest,chanrec *chan,int status)
1341 if ((!user) || (!dest) || (!chan))
1345 if (status != STATUS_OP)
1347 WriteServ(user->fd,"482 %s %s :You're not a channel 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_HOP)
1371 /* mode already set on user, dont allow multiple */
1374 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
1375 log(DEBUG,"gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
1383 int give_voice(userrec *user,char *dest,chanrec *chan,int status)
1388 if ((!user) || (!dest) || (!chan))
1392 if (status < STATUS_HOP)
1394 WriteServ(user->fd,"482 %s %s :You must be at least a half-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_VOICE)
1418 /* mode already set on user, dont allow multiple */
1421 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
1422 log(DEBUG,"gave voice: %s %s",d->chans[i].channel->name,d->nick);
1430 int take_ops(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_OP) == 0)
1465 /* mode already set on user, dont allow multiple */
1468 d->chans[i].uc_modes ^= UCMODE_OP;
1469 log(DEBUG,"took ops: %s %s",d->chans[i].channel->name,d->nick);
1477 int take_hops(userrec *user,char *dest,chanrec *chan,int status)
1482 if ((!user) || (!dest) || (!chan))
1486 if (status != STATUS_OP)
1488 WriteServ(user->fd,"482 %s %s :You're not a channel 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_HOP) == 0)
1512 /* mode already set on user, dont allow multiple */
1515 d->chans[i].uc_modes ^= UCMODE_HOP;
1516 log(DEBUG,"took h-ops: %s %s",d->chans[i].channel->name,d->nick);
1524 int take_voice(userrec *user,char *dest,chanrec *chan,int status)
1529 if ((!user) || (!dest) || (!chan))
1533 if (status < STATUS_HOP)
1535 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1543 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1548 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1553 for (i = 0; i != MAXCHANS; i++)
1555 if ((d->chans[i].channel == chan) && (chan != NULL))
1557 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
1559 /* mode already set on user, dont allow multiple */
1562 d->chans[i].uc_modes ^= UCMODE_VOICE;
1563 log(DEBUG,"took voice: %s %s",d->chans[i].channel->name,d->nick);
1571 void TidyBan(char *ban)
1573 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
1577 char* pos_of_pling = strchr(temp,'!');
1578 char* pos_of_at = strchr(temp,'@');
1580 pos_of_pling[0] = '\0';
1581 pos_of_at[0] = '\0';
1585 strncpy(NICK,temp,NICKMAX);
1586 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
1587 strncpy(HOST,pos_of_at,160);
1589 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
1592 int add_ban(userrec *user,char *dest,chanrec *chan,int status)
1595 if ((!user) || (!dest) || (!chan))
1597 if (strchr(dest,'!')==0)
1599 if (strchr(dest,'@')==0)
1601 for (int i = 0; i < strlen(dest); i++)
1604 for (int i = 0; i < strlen(dest); i++)
1608 for (int i = 0; i < strlen(dest); i++)
1614 for (int i = 0; i < strlen(dest); i++)
1619 log(DEBUG,"add_ban: %s %s",chan->name,user->nick);
1622 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1624 if (!strcasecmp(i->data,dest))
1626 // dont allow a user to set the same ban twice
1631 b.set_time = time(NULL);
1632 strncpy(b.data,dest,MAXBUF);
1633 strncpy(b.set_by,user->nick,NICKMAX);
1634 chan->bans.push_back(b);
1638 int take_ban(userrec *user,char *dest,chanrec *chan,int status)
1640 if ((!user) || (!dest) || (!chan))
1645 log(DEBUG,"del_ban: %s %s",chan->name,user->nick);
1646 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1648 if (!strcasecmp(i->data,dest))
1650 chan->bans.erase(i);
1657 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt)
1659 char modelist[MAXBUF];
1660 char outlist[MAXBUF];
1661 char outstr[MAXBUF];
1662 char outpars[32][MAXBUF];
1668 bool k_set = false, l_set = false;
1675 log(DEBUG,"process_modes: start");
1677 strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
1678 /* parameters[2] onwards are parameters for
1679 * modes that require them :) */
1680 strcpy(outlist,"+");
1683 log(DEBUG,"process_modes: modelist: %s",modelist);
1685 for (ptr = 0; ptr < strlen(modelist); ptr++)
1690 log(DEBUG,"process_modes: modechar: %c",modelist[ptr]);
1691 char modechar = modelist[ptr];
1692 switch (modelist[ptr])
1697 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
1699 outlist[strlen(outlist)-1] = '-';
1703 strcat(outlist,"-");
1713 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
1715 outlist[strlen(outlist)-1] = '+';
1719 strcat(outlist,"+");
1726 if ((param >= pcnt)) break;
1729 r = give_ops(user,parameters[param++],chan,status);
1733 r = take_ops(user,parameters[param++],chan,status);
1737 strcat(outlist,"o");
1738 strcpy(outpars[pc++],parameters[param-1]);
1743 if ((param >= pcnt)) break;
1746 r = give_hops(user,parameters[param++],chan,status);
1750 r = take_hops(user,parameters[param++],chan,status);
1754 strcat(outlist,"h");
1755 strcpy(outpars[pc++],parameters[param-1]);
1761 if ((param >= pcnt)) break;
1764 r = give_voice(user,parameters[param++],chan,status);
1768 r = take_voice(user,parameters[param++],chan,status);
1772 strcat(outlist,"v");
1773 strcpy(outpars[pc++],parameters[param-1]);
1778 if ((param >= pcnt)) break;
1781 r = add_ban(user,parameters[param++],chan,status);
1785 r = take_ban(user,parameters[param++],chan,status);
1789 strcat(outlist,"b");
1790 strcpy(outpars[pc++],parameters[param-1]);
1795 if ((param >= pcnt))
1803 if (!strcmp(chan->key,""))
1805 strcat(outlist,"k");
1806 strcpy(outpars[pc++],parameters[param++]);
1807 strcpy(chan->key,parameters[param-1]);
1813 /* only allow -k if correct key given */
1814 if (strcmp(chan->key,""))
1816 strcat(outlist,"k");
1817 strcpy(chan->key,"");
1827 strcat(outlist,"l");
1832 if ((param >= pcnt)) break;
1838 bool invalid = false;
1839 for (int i = 0; i < strlen(parameters[param]); i++)
1841 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
1846 if (atoi(parameters[param]) < 1)
1854 chan->limit = atoi(parameters[param]);
1857 strcat(outlist,"l");
1858 strcpy(outpars[pc++],parameters[param++]);
1865 if (chan->inviteonly != mdir)
1867 strcat(outlist,"i");
1869 chan->inviteonly = mdir;
1873 if (chan->topiclock != mdir)
1875 strcat(outlist,"t");
1877 chan->topiclock = mdir;
1881 if (chan->noexternal != mdir)
1883 strcat(outlist,"n");
1885 chan->noexternal = mdir;
1889 if (chan->moderated != mdir)
1891 strcat(outlist,"m");
1893 chan->moderated = mdir;
1897 if (chan->secret != mdir)
1899 strcat(outlist,"s");
1900 if (chan->c_private)
1902 chan->c_private = 0;
1905 strcat(outlist,"-p+");
1909 strcat(outlist,"+p-");
1913 chan->secret = mdir;
1917 if (chan->c_private != mdir)
1919 strcat(outlist,"p");
1925 strcat(outlist,"-s+");
1929 strcat(outlist,"+s-");
1933 chan->c_private = mdir;
1939 if (ModeDefined(modelist[ptr],MT_CHANNEL))
1941 if ((ModeDefinedOn(modelist[ptr],MT_CHANNEL)>0) && (mdir))
1943 p.push_back(parameters[param]);
1945 if ((ModeDefinedOff(modelist[ptr],MT_CHANNEL)>0) && (!mdir))
1947 p.push_back(parameters[param]);
1949 for (int i = 0; i <= MODCOUNT; i++)
1951 if (modules[i]->OnExtendedMode(user,chan,modechar,MT_CHANNEL,mdir,p))
1953 char app[] = {modechar, 0};
1954 strcat(outlist, app);
1955 chan->SetCustomMode(modelist[ptr],mdir);
1956 // include parameters in output if mode has them
1957 if ((ModeDefinedOn(modelist[ptr],MT_CHANNEL)>0) || (ModeDefinedOff(modelist[ptr],MT_CHANNEL)>0))
1959 chan->SetCustomModeParam(modelist[ptr],parameters[param],mdir);
1960 strcpy(outpars[pc++],parameters[param++]);
1971 /* this ensures only the *valid* modes are sent out onto the network */
1972 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
1974 outlist[strlen(outlist)-1] = '\0';
1976 if (strcmp(outlist,""))
1978 strcpy(outstr,outlist);
1979 for (ptr = 0; ptr < pc; ptr++)
1982 strcat(outstr,outpars[ptr]);
1984 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1988 void handle_mode(char **parameters, int pcnt, userrec *user)
1994 char outpars[MAXBUF];
1996 dest = Find(parameters[0]);
1998 if ((dest) && (pcnt == 1))
2000 WriteServ(user->fd,"221 %s :+%s",user->nick,user->modes);
2003 if ((dest) && (pcnt > 1))
2008 if (strchr(user->modes,'o'))
2019 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
2023 strcpy(outpars,"+");
2026 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
2029 for (i = 0; i < strlen(parameters[1]); i++)
2031 if (parameters[1][i] == '+')
2035 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2037 outpars[strlen(outpars)-1] = '+';
2041 strcat(outpars,"+");
2047 if (parameters[1][i] == '-')
2051 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
2053 outpars[strlen(outpars)-1] = '-';
2057 strcat(outpars,"-");
2065 if (strchr(user->modes,'o'))
2071 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's'))
2080 if (!strchr(dest->modes,parameters[1][i]))
2082 dest->modes[strlen(dest->modes)+1]='\0';
2083 dest->modes[strlen(dest->modes)] = parameters[1][i];
2084 outpars[strlen(outpars)+1]='\0';
2085 outpars[strlen(outpars)] = parameters[1][i];
2094 outpars[strlen(outpars)+1]='\0';
2095 outpars[strlen(outpars)] = parameters[1][i];
2098 for (q = 0; q < strlen(user->modes); q++)
2100 if (user->modes[q] != parameters[1][i])
2102 moo[0] = user->modes[q];
2107 strcpy(user->modes,temp);
2112 if (strlen(outpars))
2118 while (i < strlen (outpars))
2120 b[z++] = outpars[i++];
2122 if (i<strlen(outpars)-1)
2124 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2126 // someones playing silly buggers and trying
2127 // to put a +- or -+ into the line...
2131 if (i == strlen(outpars)-1)
2133 if ((outpars[i] == '-') || (outpars[i] == '+'))
2141 if ((b[z] == '-') || (b[z] == '+'))
2144 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2147 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2152 Ptr = FindChan(parameters[0]);
2157 /* just /modes #channel */
2158 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
2159 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
2165 if ((!strcmp(parameters[1],"+b")) || (!strcmp(parameters[1],"b")))
2168 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
2170 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
2172 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
2176 if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
2178 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, Ptr->name);
2182 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt);
2186 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2190 /* This function pokes and hacks at a parameter list like the following:
2192 * PART #winbot, #darkgalaxy :m00!
2194 * to turn it into a series of individual calls like this:
2196 * PART #winbot :m00!
2197 * PART #darkgalaxy :m00!
2199 * The seperate calls are sent to a callback function provided by the caller
2200 * (the caller will usually call itself recursively). The callback function
2201 * must be a command handler. Calling this function on a line with no list causes
2202 * no action to be taken. You must provide a starting and ending parameter number
2203 * where the range of the list can be found, useful if you have a terminating
2204 * parameter as above which is actually not part of the list, or parameters
2205 * before the actual list as well. This code is used by many functions which
2206 * can function as "one to list" (see the RFC) */
2208 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
2213 char blog[32][MAXBUF];
2214 char blog2[32][MAXBUF];
2215 int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
2216 char keystr[MAXBUF];
2219 for (i = 0; i <32; i++)
2222 for (i = 0; i <32; i++)
2223 strcpy(blog2[i],"");
2226 for (i = 0; i <10; i++)
2230 parameters[i] = moo;
2235 if (pcnt > 1) /* we have a key to copy */
2237 strcpy(keystr,parameters[1]);
2241 if (!parameters[start])
2245 if (!strchr(parameters[start],','))
2250 for (i = start; i <= end; i++)
2254 strcat(plist,parameters[i]);
2262 for (i = 0; i < t; i++)
2264 if (plist[i] == ',')
2267 strcpy(blog[j++],param);
2271 strcpy(blog[j++],param);
2274 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
2279 if ((joins) && (keystr))
2281 if (strchr(keystr,','))
2285 t2 = strlen(keystr);
2286 for (i = 0; i < t2; i++)
2288 if (keystr[i] == ',')
2291 strcpy(blog2[j++],param);
2295 strcpy(blog2[j++],param);
2300 for (j = 0; j < total; j++)
2306 for (q = end; q < pcnt-1; q++)
2308 if (parameters[q+1])
2310 pars[q-end+1] = parameters[q+1];
2313 if ((joins) && (parameters[1]))
2324 /* repeatedly call the function with the hacked parameter list */
2325 if ((joins) && (pcnt > 1))
2329 // pars[1] already set up and containing key from blog2[j]
2334 pars[1] = parameters[1];
2340 fn(pars,pcnt-(end-start),u);
2347 void handle_join(char **parameters, int pcnt, userrec *user)
2352 if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
2354 if (parameters[0][0] == '#')
2356 Ptr = add_channel(user,parameters[0],parameters[1]);
2361 void handle_part(char **parameters, int pcnt, userrec *user)
2367 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
2369 del_channel(user,parameters[0],parameters[1]);
2373 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
2375 del_channel(user,parameters[0],NULL);
2379 void handle_kick(char **parameters, int pcnt, userrec *user)
2381 chanrec* Ptr = FindChan(parameters[0]);
2382 userrec* u = Find(parameters[1]);
2386 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2390 if (!has_channel(u,Ptr))
2392 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
2398 kick_channel(user,u,Ptr,parameters[2]);
2402 kick_channel(user,u,Ptr,user->nick);
2407 void handle_die(char **parameters, int pcnt, userrec *user)
2409 log(DEBUG,"die: %s",user->nick);
2410 if (!strcmp(parameters[0],diepass))
2412 WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
2418 WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
2422 void handle_restart(char **parameters, int pcnt, userrec *user)
2424 log(DEBUG,"restart: %s",user->nick);
2425 if (!strcmp(parameters[0],restartpass))
2427 WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
2430 /* Will finish this later when i can be arsed :) */
2434 WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
2439 void kill_link(userrec *user,char* reason)
2441 user_hash::iterator iter = clientlist.find(user->nick);
2443 log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
2444 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
2445 fdatasync(user->fd);
2446 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
2447 FOREACH_MOD OnUserQuit(user);
2448 log(DEBUG,"closing fd %d",user->fd);
2449 /* bugfix, cant close() a nonblocking socket (sux!) */
2450 WriteCommonExcept(user,"QUIT :%s",reason);
2453 NonBlocking(user->fd);
2456 if (iter != clientlist.end())
2458 log(DEBUG,"deleting user hash value %p",iter->second);
2459 delete iter->second;
2460 clientlist.erase(iter);
2463 purge_empty_chans();
2467 void handle_kill(char **parameters, int pcnt, userrec *user)
2469 userrec *u = Find(parameters[0]);
2470 char killreason[MAXBUF];
2472 log(DEBUG,"kill: %s %s",parameters[0],parameters[1]);
2475 WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
2476 sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
2477 kill_link(u,killreason);
2481 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2485 void handle_summon(char **parameters, int pcnt, userrec *user)
2487 WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
2490 void handle_users(char **parameters, int pcnt, userrec *user)
2492 WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
2496 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
2498 char* Passwd(userrec *user)
2500 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
2502 if (match(user->host,i->host) && (i->type == CC_ALLOW))
2510 bool IsDenied(userrec *user)
2512 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
2514 if (match(user->host,i->host) && (i->type == CC_DENY))
2523 void handle_pass(char **parameters, int pcnt, userrec *user)
2525 if (!strcasecmp(parameters[0],Passwd(user)))
2527 user->haspassed = true;
2531 void handle_invite(char **parameters, int pcnt, userrec *user)
2533 userrec* u = Find(parameters[0]);
2534 chanrec* c = FindChan(parameters[1]);
2540 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[1]);
2546 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
2555 if (cstatus(user,c) < STATUS_HOP)
2557 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, c->name);
2561 u->InviteTo(c->name);
2562 WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
2563 WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
2567 void handle_topic(char **parameters, int pcnt, userrec *user)
2573 if (strlen(parameters[0]) <= CHANMAX)
2575 Ptr = FindChan(parameters[0]);
2580 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
2581 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
2585 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
2590 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
2596 if (loop_call(handle_topic,parameters,pcnt,user,0,pcnt-2,0))
2598 if (strlen(parameters[0]) <= CHANMAX)
2600 Ptr = FindChan(parameters[0]);
2603 if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
2605 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator", user->nick, Ptr->name);
2608 strcpy(Ptr->topic,parameters[1]);
2609 strcpy(Ptr->setby,user->nick);
2610 Ptr->topicset = time(NULL);
2611 WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
2615 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2621 /* sends out an error notice to all connected clients (not to be used
2624 void send_error(char *s)
2626 log(DEBUG,"send_error: %s",s);
2627 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
2629 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
2633 void Error(int status)
2635 signal (SIGALRM, SIG_IGN);
2636 signal (SIGPIPE, SIG_IGN);
2637 signal (SIGTERM, SIG_IGN);
2638 signal (SIGABRT, SIG_IGN);
2639 signal (SIGSEGV, SIG_IGN);
2640 signal (SIGURG, SIG_IGN);
2641 signal (SIGKILL, SIG_IGN);
2642 log(DEBUG,"*** fell down a pothole in the road to perfection ***");
2643 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
2647 int main (int argc, char *argv[])
2650 log(DEBUG,"*** InspIRCd starting up!");
2651 if (!FileExists(CONFIG_FILE))
2653 printf("ERROR: Cannot open config file: %s\nExiting...\n",CONFIG_FILE);
2654 log(DEBUG,"main: no config");
2655 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
2658 if (InspIRCd() == ERROR)
2660 log(DEBUG,"main: daemon function bailed");
2661 printf("ERROR: could not initialise. Shutting down.\n");
2668 template<typename T> inline string ConvToStr(const T &in)
2671 if (!(tmp << in)) return string();
2675 /* re-allocates a nick in the user_hash after they change nicknames,
2676 * returns a pointer to the new user as it may have moved */
2678 userrec* ReHashNick(char* Old, char* New)
2680 user_hash::iterator newnick;
2681 user_hash::iterator oldnick = clientlist.find(Old);
2683 log(DEBUG,"ReHashNick: %s %s",Old,New);
2685 if (!strcasecmp(Old,New))
2687 log(DEBUG,"old nick is new nick, skipping");
2688 return oldnick->second;
2691 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
2693 log(DEBUG,"ReHashNick: Found hashed nick %s",Old);
2695 clientlist[New] = new userrec();
2696 clientlist[New] = oldnick->second;
2697 /*delete oldnick->second; */
2698 clientlist.erase(oldnick);
2700 log(DEBUG,"ReHashNick: Nick rehashed as %s",New);
2702 return clientlist[New];
2705 /* adds or updates an entry in the whowas list */
2706 void AddWhoWas(userrec* u)
2708 user_hash::iterator iter = whowas.find(u->nick);
2709 userrec *a = new userrec();
2710 strcpy(a->nick,u->nick);
2711 strcpy(a->ident,u->ident);
2712 strcpy(a->dhost,u->dhost);
2713 strcpy(a->host,u->host);
2714 strcpy(a->fullname,u->fullname);
2715 strcpy(a->server,u->server);
2716 a->signon = u->signon;
2718 /* MAX_WHOWAS: max number of /WHOWAS items
2719 * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
2720 * can be replaced by a newer one
2723 if (iter == whowas.end())
2725 if (whowas.size() == WHOWAS_MAX)
2727 for (user_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
2729 // 3600 seconds in an hour ;)
2730 if ((i->second->signon)<(time(NULL)-(WHOWAS_STALE*3600)))
2734 log(DEBUG,"added WHOWAS entry, purged an old record");
2741 log(DEBUG,"added fresh WHOWAS entry");
2742 whowas[a->nick] = a;
2747 log(DEBUG,"updated WHOWAS entry");
2748 delete iter->second;
2754 /* add a client connection to the sockets list */
2755 void AddClient(int socket, char* host, int port, bool iscached)
2759 char resolved[MAXBUF];
2762 user_hash::iterator iter;
2764 tempnick = ConvToStr(socket) + "-unknown";
2765 sprintf(tn2,"%d-unknown",socket);
2767 iter = clientlist.find(tempnick);
2769 if (iter != clientlist.end()) return;
2772 * It is OK to access the value here this way since we know
2773 * it exists, we just created it above.
2775 * At NO other time should you access a value in a map or a
2776 * hash_map this way.
2778 clientlist[tempnick] = new userrec();
2780 NonBlocking(socket);
2781 log(DEBUG,"AddClient: %d %s %d",socket,host,port);
2784 clientlist[tempnick]->fd = socket;
2785 strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
2786 strncpy(clientlist[tempnick]->host, host,160);
2787 strncpy(clientlist[tempnick]->dhost, host,160);
2788 strncpy(clientlist[tempnick]->server, ServerName,256);
2789 clientlist[tempnick]->registered = 0;
2790 clientlist[tempnick]->signon = time(NULL);
2791 clientlist[tempnick]->nping = time(NULL)+240;
2792 clientlist[tempnick]->lastping = 1;
2793 clientlist[tempnick]->port = port;
2797 WriteServ(socket,"NOTICE Auth :Found your hostname (cached)...");
2801 WriteServ(socket,"NOTICE Auth :Looking up your hostname...");
2804 if (clientlist.size() == MAXCLIENTS)
2805 kill_link(clientlist[tempnick],"No more connections allowed in this class");
2808 void handle_names(char **parameters, int pcnt, userrec *user)
2812 if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
2814 c = FindChan(parameters[0]);
2817 /*WriteServ(user->fd,"353 %s = %s :%s", user->nick, c->name,*/
2819 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
2823 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2828 void handle_privmsg(char **parameters, int pcnt, userrec *user)
2833 if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
2835 if (parameters[0][0] == '#')
2837 chan = FindChan(parameters[0]);
2840 if ((chan->noexternal) && (!has_channel(user,chan)))
2842 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
2845 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
2847 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
2850 ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
2854 /* no such nick/channel */
2855 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2860 dest = Find(parameters[0]);
2863 if (strcmp(dest->awaymsg,""))
2865 /* auto respond with aweh msg */
2866 WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
2868 WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
2872 /* no such nick/channel */
2873 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2877 void handle_notice(char **parameters, int pcnt, userrec *user)
2882 if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
2884 if (parameters[0][0] == '#')
2886 chan = FindChan(parameters[0]);
2889 if ((chan->noexternal) && (!has_channel(user,chan)))
2891 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
2894 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
2896 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
2899 WriteChannel(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
2903 /* no such nick/channel */
2904 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2909 dest = Find(parameters[0]);
2912 WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
2916 /* no such nick/channel */
2917 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2923 char* chlist(userrec *user)
2928 log(DEBUG,"chlist: %s",user->nick);
2934 for (i = 0; i != MAXCHANS; i++)
2936 if (user->chans[i].channel != NULL)
2938 if (user->chans[i].channel->name)
2940 strcpy(cmp,user->chans[i].channel->name);
2942 if (!strstr(lst,cmp))
2944 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
2946 strcat(lst,cmode(user,user->chans[i].channel));
2947 strcat(lst,user->chans[i].channel->name);
2957 void handle_info(char **parameters, int pcnt, userrec *user)
2959 WriteServ(user->fd,"371 %s :The Inspire IRCd Project Has been brought to you by the following people..",user->nick);
2960 WriteServ(user->fd,"371 %s :Craig Edwards, Craig McLure, and Others..",user->nick);
2961 WriteServ(user->fd,"371 %s :Will finish this later when i can be arsed :p",user->nick);
2962 WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
2965 void handle_time(char **parameters, int pcnt, userrec *user)
2968 struct tm * timeinfo;
2971 timeinfo = localtime ( &rawtime );
2972 WriteServ(user->fd,"391 %s %s :%s",user->nick,ServerName, asctime (timeinfo) );
2976 void handle_whois(char **parameters, int pcnt, userrec *user)
2981 if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
2983 dest = Find(parameters[0]);
2986 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
2987 if ((user == dest) || (strchr(user->modes,'o')))
2989 WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
2991 if (strcmp(chlist(dest),""))
2993 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
2995 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
2996 if (strcmp(dest->awaymsg,""))
2998 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
3000 if (strchr(dest->modes,'o'))
3002 WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
3004 //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
3005 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
3007 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
3011 /* no such nick/channel */
3012 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3016 void handle_quit(char **parameters, int pcnt, userrec *user)
3018 user_hash::iterator iter = clientlist.find(user->nick);
3020 /* theres more to do here, but for now just close the socket */
3023 if (parameters[0][0] == ':')
3027 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
3028 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
3029 WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
3033 Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
3034 WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
3035 WriteCommonExcept(user,"QUIT :Client exited");
3038 FOREACH_MOD OnUserQuit(user);
3040 /* confucious say, he who close nonblocking socket, get nothing! */
3043 NonBlocking(user->fd);
3046 if (iter != clientlist.end())
3048 log(DEBUG,"deleting user hash value");
3049 delete iter->second;
3050 clientlist.erase(iter);
3053 purge_empty_chans();
3056 void handle_who(char **parameters, int pcnt, userrec *user)
3058 chanrec* Ptr = NULL;
3060 /* theres more to do here, but for now just close the socket */
3063 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
3065 if (user->chans[0].channel)
3067 Ptr = user->chans[0].channel;
3068 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3070 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
3072 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);
3078 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3082 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, user->nick);
3086 if (parameters[0][0] = '#')
3088 Ptr = FindChan(parameters[0]);
3091 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3093 if ((has_channel(i->second,Ptr)) && (isnick(i->second->nick)))
3095 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);
3098 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3102 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3108 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
3110 Ptr = user->chans[0].channel;
3111 printf(user->chans[0].channel->name);
3112 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3114 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
3116 if (strchr(i->second->modes,'o'))
3118 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);
3122 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3128 void handle_wallops(char **parameters, int pcnt, userrec *user)
3130 WriteWallOps(user,"%s",parameters[0]);
3133 void handle_list(char **parameters, int pcnt, userrec *user)
3137 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
3138 for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
3140 if ((!i->second->c_private) && (!i->second->secret))
3142 WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
3145 WriteServ(user->fd,"323 %s :End of channel list.",user->nick);
3149 void handle_rehash(char **parameters, int pcnt, userrec *user)
3151 WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CONFIG_FILE);
3153 FOREACH_MOD OnRehash();
3154 WriteOpers("%s is rehashing config file %s",user->nick,CONFIG_FILE);
3160 return clientlist.size();
3163 int usercount_invisible(void)
3167 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3169 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
3174 int usercount_opers(void)
3178 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3180 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
3185 int usercount_unknown(void)
3189 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3191 if ((i->second->fd) && (i->second->registered != 7))
3199 return chanlist.size();
3202 int servercount(void)
3207 void handle_lusers(char **parameters, int pcnt, userrec *user)
3209 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),servercount());
3210 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
3211 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
3212 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
3213 WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,usercnt());
3216 void handle_admin(char **parameters, int pcnt, userrec *user)
3218 WriteServ(user->fd,"256 %s :Administrative info for %s",user->nick,ServerName);
3219 WriteServ(user->fd,"257 %s :Name - %s",user->nick,AdminName);
3220 WriteServ(user->fd,"258 %s :Nickname - %s",user->nick,AdminNick);
3221 WriteServ(user->fd,"258 %s :E-Mail - %s",user->nick,AdminEmail);
3224 void ShowMOTD(userrec *user)
3228 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
3231 WriteServ(user->fd,"375 %s :- %s message of the day",user->nick,ServerName);
3232 for (int i = 0; i != MOTD.size(); i++)
3234 WriteServ(user->fd,"372 %s :- %s",user->nick,MOTD[i].c_str());
3236 WriteServ(user->fd,"376 %s :End of %s message of the day.",user->nick,ServerName);
3239 void ShowRULES(userrec *user)
3243 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
3246 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
3247 for (int i = 0; i != RULES.size(); i++)
3249 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
3251 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
3254 /* shows the message of the day, and any other on-logon stuff */
3255 void ConnectUser(userrec *user)
3257 user->registered = 7;
3258 user->idle_lastmsg = time(NULL);
3259 log(DEBUG,"ConnectUser: %s",user->nick);
3261 if (strcmp(Passwd(user),"") && (!user->haspassed))
3263 Write(user->fd,"ERROR :Closing link: Invalid password");
3264 fdatasync(user->fd);
3265 kill_link(user,"Invalid password");
3270 Write(user->fd,"ERROR :Closing link: Unauthorized connection");
3271 fdatasync(user->fd);
3272 kill_link(user,"Unauthorised connection");
3275 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
3276 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
3277 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
3278 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
3279 WriteServ(user->fd,"004 %s :%s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
3280 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);
3281 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);
3283 FOREACH_MOD OnUserConnect(user);
3284 WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
3287 void handle_version(char **parameters, int pcnt, userrec *user)
3289 WriteServ(user->fd,"351 %s :%s %s :%s",user->nick,VERSION,ServerName,SYSTEM);
3292 void handle_ping(char **parameters, int pcnt, userrec *user)
3294 WriteServ(user->fd,"PONG %s :%s",ServerName,parameters[0]);
3297 void handle_pong(char **parameters, int pcnt, userrec *user)
3299 // set the user as alive so they survive to next ping
3303 void handle_motd(char **parameters, int pcnt, userrec *user)
3308 void handle_rules(char **parameters, int pcnt, userrec *user)
3313 void handle_user(char **parameters, int pcnt, userrec *user)
3315 if (user->registered < 3)
3317 if (isident(parameters[0]) == 0) {
3318 // This kinda Sucks, According to the RFC thou, its either this,
3319 // or "You have already registered" :p -- Craig
3320 WriteServ(user->fd,"461 %s USER :Not enough parameters",user->nick);
3323 WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
3324 strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
3325 strncat(user->ident,parameters[0],IDENTMAX);
3326 strncpy(user->fullname,parameters[3],128);
3327 user->registered = (user->registered | 1);
3332 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
3335 /* parameters 2 and 3 are local and remote hosts, ignored when sent by client connection */
3336 if (user->registered == 3)
3338 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
3343 void handle_userhost(char **parameters, int pcnt, userrec *user)
3345 char Return[MAXBUF],junk[MAXBUF];
3346 sprintf(Return,"302 %s :",user->nick);
3347 for (int i = 0; i < pcnt; i++)
3349 userrec *u = Find(parameters[i]);
3352 if (strchr(u->modes,'o'))
3354 sprintf(junk,"%s*=+%s@%s ",u->nick,u->ident,u->host);
3355 strcat(Return,junk);
3359 sprintf(junk,"%s=+%s@%s ",u->nick,u->ident,u->host);
3360 strcat(Return,junk);
3364 WriteServ(user->fd,Return);
3368 void handle_ison(char **parameters, int pcnt, userrec *user)
3370 char Return[MAXBUF];
3371 sprintf(Return,"303 %s :",user->nick);
3372 for (int i = 0; i < pcnt; i++)
3374 userrec *u = Find(parameters[i]);
3377 strcat(Return,u->nick);
3381 WriteServ(user->fd,Return);
3385 void handle_away(char **parameters, int pcnt, userrec *user)
3389 strcpy(user->awaymsg,parameters[0]);
3390 WriteServ(user->fd,"306 %s :You have been marked as being away",user->nick);
3394 strcpy(user->awaymsg,"");
3395 WriteServ(user->fd,"305 %s :You are no longer marked as being away",user->nick);
3399 void handle_whowas(char **parameters, int pcnt, userrec* user)
3401 user_hash::iterator i = whowas.find(parameters[0]);
3403 if (i == whowas.end())
3405 WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
3406 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3410 time_t rawtime = i->second->signon;
3414 timeinfo = localtime(&rawtime);
3415 strcpy(b,asctime(timeinfo));
3416 b[strlen(b)-1] = '\0';
3418 WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
3419 WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
3420 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3425 void handle_trace(char **parameters, int pcnt, userrec *user)
3427 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
3431 if (isnick(i->second->nick))
3433 if (strchr(i->second->modes,'o'))
3435 WriteServ(user->fd,"205 %s :Oper 0 %s",user->nick,i->second->nick);
3439 WriteServ(user->fd,"204 %s :User 0 %s",user->nick,i->second->nick);
3444 WriteServ(user->fd,"203 %s :???? 0 [%s]",user->nick,i->second->host);
3450 void handle_stats(char **parameters, int pcnt, userrec *user)
3456 if (strlen(parameters[0])>1)
3458 /* make the stats query 1 character long */
3459 parameters[0][1] = '\0';
3462 /* stats m (list number of times each command has been used, plus bytecount) */
3463 if (!strcasecmp(parameters[0],"m"))
3465 for (int i = 0; i < cmdlist.size(); i++)
3467 if (cmdlist[i].handler_function)
3469 if (cmdlist[i].use_count)
3471 /* RPL_STATSCOMMANDS */
3472 WriteServ(user->fd,"212 %s %s %d %d",user->nick,cmdlist[i].command,cmdlist[i].use_count,cmdlist[i].total_bytes);
3479 /* stats z (debug and memory info) */
3480 if (!strcasecmp(parameters[0],"z"))
3482 WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
3483 WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
3484 WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
3485 WriteServ(user->fd,"249 %s :MOTD(VECTOR) %d, RULES(VECTOR) %d",user->nick,MOTD.size(),RULES.size());
3486 WriteServ(user->fd,"249 %s :address_cache(HASH_MAP) %d (%d buckets)",user->nick,IP.size(),IP.bucket_count());
3487 WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
3488 WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
3489 WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
3493 if (!strcasecmp(parameters[0],"o"))
3495 for (int i = 0; i < ConfValueEnum("oper"); i++)
3497 char LoginName[MAXBUF];
3498 char HostName[MAXBUF];
3499 char OperType[MAXBUF];
3500 ConfValue("oper","name",i,LoginName);
3501 ConfValue("oper","host",i,HostName);
3502 ConfValue("oper","type",i,OperType);
3503 WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
3507 /* stats l (show user I/O stats) */
3508 if (!strcasecmp(parameters[0],"l"))
3510 WriteServ(user->fd,"211 %s :server:port nick bytes_in cmds_in bytes_out cmds_out",user->nick);
3511 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
3513 if (isnick(i->second->nick))
3515 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);
3519 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);
3525 /* stats u (show server uptime) */
3526 if (!strcasecmp(parameters[0],"u"))
3528 time_t current_time = 0;
3529 current_time = time(NULL);
3530 time_t server_uptime = current_time - startup_time;
3532 stime = gmtime(&server_uptime);
3533 /* i dont know who the hell would have an ircd running for over a year nonstop, but
3534 * Craig suggested this, and it seemed a good idea so in it went */
3535 if (stime->tm_year > 70)
3537 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);
3541 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);
3545 WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
3546 WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
3550 void handle_connect(char **parameters, int pcnt, userrec *user)
3552 WriteServ(user->fd,"NOTICE %s :*** Connecting to %s port %s...",user->nick,parameters[0],parameters[1]);
3553 if (!me[defaultRoute]->BeginLink(parameters[0],atoi(parameters[1]),"password"))
3555 WriteServ(user->fd,"NOTICE %s :*** Failed to send auth packet to %s!",user->nick,parameters[0]);
3559 void handle_squit(char **parameters, int pcnt, userrec *user)
3563 void handle_oper(char **parameters, int pcnt, userrec *user)
3565 char LoginName[MAXBUF];
3566 char Password[MAXBUF];
3567 char OperType[MAXBUF];
3568 char TypeName[MAXBUF];
3569 char Hostname[MAXBUF];
3572 for (i = 0; i < ConfValueEnum("oper"); i++)
3574 ConfValue("oper","name",i,LoginName);
3575 ConfValue("oper","password",i,Password);
3576 if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
3578 /* correct oper credentials */
3579 ConfValue("oper","type",i,OperType);
3580 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
3581 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
3582 WriteServ(user->fd,"MODE %s :+o",user->nick);
3583 for (j =0; j < ConfValueEnum("type"); j++)
3585 ConfValue("type","name",j,TypeName);
3586 if (!strcmp(TypeName,OperType))
3588 /* found this oper's opertype */
3589 ConfValue("type","host",j,Hostname);
3590 strncpy(user->dhost,Hostname,256);
3593 if (!strchr(user->modes,'o'))
3595 strcat(user->modes,"o");
3601 WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
3602 WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
3605 void handle_nick(char **parameters, int pcnt, userrec *user)
3609 log(DEBUG,"not enough params for handle_nick");
3614 log(DEBUG,"invalid parameter passed to handle_nick");
3617 if (!strlen(parameters[0]))
3619 log(DEBUG,"zero length new nick passed to handle_nick");
3624 log(DEBUG,"invalid user passed to handle_nick");
3629 log(DEBUG,"invalid old nick passed to handle_nick");
3632 if (!strcasecmp(user->nick,parameters[0]))
3634 log(DEBUG,"old nick is new nick, skipping");
3639 if (strlen(parameters[0]) > 1)
3641 if (parameters[0][0] == ':')
3646 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
3648 WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
3652 if (isnick(parameters[0]) == 0)
3654 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
3658 if (user->registered == 7)
3660 WriteCommon(user,"NICK %s",parameters[0]);
3663 /* change the nick of the user in the users_hash */
3664 user = ReHashNick(user->nick, parameters[0]);
3665 /* actually change the nick within the record */
3667 if (!user->nick) return;
3669 strncpy(user->nick, parameters[0],NICKMAX);
3671 log(DEBUG,"new nick set: %s",user->nick);
3673 if (user->registered < 3)
3674 user->registered = (user->registered | 2);
3675 if (user->registered == 3)
3677 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
3680 log(DEBUG,"exit nickchange: %s",user->nick);
3683 int process_parameters(char **command_p,char *parameters)
3688 q = strlen(parameters);
3691 /* no parameters, command_p invalid! */
3694 if (parameters[0] == ':')
3696 command_p[0] = parameters+1;
3701 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
3703 /* only one parameter */
3704 command_p[0] = parameters;
3705 if (parameters[0] == ':')
3707 if (strchr(parameters,' ') != NULL)
3715 command_p[j++] = parameters;
3716 for (i = 0; i <= q; i++)
3718 if (parameters[i] == ' ')
3720 command_p[j++] = parameters+i+1;
3721 parameters[i] = '\0';
3722 if (command_p[j-1][0] == ':')
3724 *command_p[j-1]++; /* remove dodgy ":" */
3726 /* parameter like this marks end of the sequence */
3730 return j; /* returns total number of items in the list */
3733 void process_command(userrec *user, char* cmd)
3737 char *command_p[127];
3738 char p[MAXBUF], temp[MAXBUF];
3739 int i, j, items, cmd_found;
3741 for (int i = 0; i < 127; i++)
3742 command_p[i] = NULL;
3752 if (!strcmp(cmd,""))
3759 FOREACH_MOD OnServerRaw(tmp,true);
3760 const char* cmd2 = tmp.c_str();
3761 snprintf(cmd,512,"%s",cmd2);
3763 if (!strchr(cmd,' '))
3765 /* no parameters, lets skip the formalities and not chop up
3768 command_p[0] = NULL;
3770 for (int i = 0; i <= strlen(cmd); i++)
3772 cmd[i] = toupper(cmd[i]);
3779 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
3780 for (i = 0; i < strlen(temp); i++)
3782 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
3788 /* split the full string into a command plus parameters */
3792 if (strchr(cmd,' '))
3794 for (i = 0; i <= strlen(cmd); i++)
3796 /* capitalise the command ONLY, leave params intact */
3797 cmd[i] = toupper(cmd[i]);
3798 /* are we nearly there yet?! :P */
3802 parameters = cmd+i+1;
3810 for (i = 0; i <= strlen(cmd); i++)
3812 cmd[i] = toupper(cmd[i]);
3820 for (i = 0; i != cmdlist.size(); i++)
3822 if (strcmp(cmdlist[i].command,""))
3824 if (!strcmp(command, cmdlist[i].command))
3828 if (strcmp(parameters,""))
3830 items = process_parameters(command_p,parameters);
3835 command_p[0] = NULL;
3841 command_p[0] = NULL;
3846 user->idle_lastmsg = time(NULL);
3847 /* activity resets the ping pending timer */
3848 user->nping = time(NULL) + 120;
3849 if ((items) < cmdlist[i].min_params)
3851 log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
3852 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
3855 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
3857 log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
3858 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
3862 /* if the command isnt USER, PASS, or NICK, and nick is empty,
3864 if ((strcmp(command,"USER")) && (strcmp(command,"NICK")) && (strcmp(command,"PASS")))
3866 if ((!isnick(user->nick)) || (user->registered != 7))
3868 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
3869 WriteServ(user->fd,"451 %s :You have not registered",command);
3873 if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
3875 log(DEBUG,"process_command: handler: %s %s %d",user->nick,command,items);
3876 if (cmdlist[i].handler_function)
3878 /* ikky /stats counters */
3883 user->bytes_in += strlen(temp);
3886 cmdlist[i].use_count++;
3887 cmdlist[i].total_bytes+=strlen(temp);
3890 /* WARNING: nothing may come after the
3891 * command handler call, as the handler
3892 * may free the user structure! */
3894 cmdlist[i].handler_function(command_p,items,user);
3900 log(DEBUG,"process_command: not registered: %s %s",user->nick,command);
3901 WriteServ(user->fd,"451 %s :You have not registered",command);
3909 if ((!cmd_found) && (user))
3911 log(DEBUG,"process_command: not in table: %s %s",user->nick,command);
3912 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
3917 void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
3920 /* create the command and push it onto the table */
3921 strcpy(comm.command,cmd);
3922 comm.handler_function = f;
3923 comm.flags_needed = flags;
3924 comm.min_params = minparams;
3926 comm.total_bytes = 0;
3927 cmdlist.push_back(comm);
3930 void SetupCommandTable(void)
3932 createcommand("USER",handle_user,0,4);
3933 createcommand("NICK",handle_nick,0,1);
3934 createcommand("QUIT",handle_quit,0,0);
3935 createcommand("VERSION",handle_version,0,0);
3936 createcommand("PING",handle_ping,0,1);
3937 createcommand("PONG",handle_pong,0,1);
3938 createcommand("ADMIN",handle_admin,0,0);
3939 createcommand("PRIVMSG",handle_privmsg,0,2);
3940 createcommand("INFO",handle_info,0,0);
3941 createcommand("TIME",handle_time,0,0);
3942 createcommand("WHOIS",handle_whois,0,1);
3943 createcommand("WALLOPS",handle_wallops,'o',1);
3944 createcommand("NOTICE",handle_notice,0,2);
3945 createcommand("JOIN",handle_join,0,1);
3946 createcommand("NAMES",handle_names,0,1);
3947 createcommand("PART",handle_part,0,1);
3948 createcommand("KICK",handle_kick,0,2);
3949 createcommand("MODE",handle_mode,0,1);
3950 createcommand("TOPIC",handle_topic,0,1);
3951 createcommand("WHO",handle_who,0,1);
3952 createcommand("MOTD",handle_motd,0,0);
3953 createcommand("RULES",handle_join,0,0);
3954 createcommand("OPER",handle_oper,0,2);
3955 createcommand("LIST",handle_list,0,0);
3956 createcommand("DIE",handle_die,'o',1);
3957 createcommand("RESTART",handle_restart,'o',1);
3958 createcommand("KILL",handle_kill,'o',2);
3959 createcommand("REHASH",handle_rehash,'o',0);
3960 createcommand("LUSERS",handle_lusers,0,0);
3961 createcommand("STATS",handle_stats,0,1);
3962 createcommand("USERHOST",handle_userhost,0,1);
3963 createcommand("AWAY",handle_away,0,0);
3964 createcommand("ISON",handle_ison,0,0);
3965 createcommand("SUMMON",handle_summon,0,0);
3966 createcommand("USERS",handle_users,0,0);
3967 createcommand("INVITE",handle_invite,0,2);
3968 createcommand("PASS",handle_pass,0,1);
3969 createcommand("TRACE",handle_trace,'o',0);
3970 createcommand("WHOWAS",handle_whowas,0,1);
3971 createcommand("CONNECT",handle_connect,'o',2);
3972 createcommand("SQUIT",handle_squit,'o',1);
3975 void process_buffer(userrec *user)
3983 if (!strcmp(user->inbuf,""))
3987 strncpy(cmd,user->inbuf,MAXBUF);
3988 if (!strcmp(cmd,""))
3992 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
3994 cmd[strlen(cmd)-1] = '\0';
3996 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
3998 cmd[strlen(cmd)-1] = '\0';
4000 strcpy(user->inbuf,"");
4001 if (!strcmp(cmd,""))
4005 log(DEBUG,"InspIRCd: processing: %s %s",user->nick,cmd);
4006 process_command(user,cmd);
4009 void handle_link_packet(char* udp_msg, char* udp_host, int udp_port, serverrec *serv)
4015 struct sockaddr_in client, server;
4016 char addrs[MAXBUF][255];
4017 int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
4019 int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
4020 int selectResult = 0;
4021 char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF], Type[MAXBUF];
4022 char resolvedHost[MAXBUF];
4026 log(DEBUG,"InspIRCd: startup: begin");
4030 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
4032 log(DEBUG,"InspIRCd: startup: not starting with UID 0!");
4034 SetupCommandTable();
4035 log(DEBUG,"InspIRCd: startup: default command table set up");
4038 if (strcmp(DieValue,""))
4040 printf("WARNING: %s\n\n",DieValue);
4043 log(DEBUG,"InspIRCd: startup: read config");
4045 int count2 = 0, count3 = 0;
4046 for (count = 0; count < ConfValueEnum("bind"); count++)
4048 ConfValue("bind","port",count,configToken);
4049 ConfValue("bind","address",count,Addr);
4050 ConfValue("bind","type",count,Type);
4051 if (!strcmp(Type,"servers"))
4053 char Default[MAXBUF];
4054 strcpy(Default,"no");
4055 ConfValue("bind","default",count,Default);
4056 if (strchr(Default,'y'))
4058 defaultRoute = count3;
4059 log(DEBUG,"InspIRCd: startup: binding '%s:%s' is default server route",Addr,configToken);
4061 me[count3] = new serverrec(ServerName,100L,false);
4062 me[count3]->CreateListener(Addr,atoi(configToken));
4067 ports[count2] = atoi(configToken);
4068 strcpy(addrs[count2],Addr);
4071 log(DEBUG,"InspIRCd: startup: read binding %s:%s [%s] from config",Addr,configToken, Type);
4074 UDPportCount = count3;
4076 log(DEBUG,"InspIRCd: startup: read %d total client ports and %d total server ports",portCount,UDPportCount);
4078 log(DEBUG,"InspIRCd: startup: InspIRCd is now running!");
4082 /* BugFix By Craig! :p */
4084 for (count = 0; count2 < ConfValueEnum("module"); count2++)
4086 char modfile[MAXBUF];
4087 ConfValue("module","name",count,configToken);
4088 sprintf(modfile,"%s/%s",MOD_PATH,configToken);
4089 printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
4090 log(DEBUG,"InspIRCd: startup: Loading module: %s",modfile);
4091 /* If The File Doesnt exist, Trying to load it
4092 * Will Segfault the IRCd.. So, check to see if
4093 * it Exists, Before Proceeding. */
4094 if (FileExists(modfile))
4096 factory[count] = new ircd_module(modfile);
4097 if (factory[count]->LastError())
4099 log(DEBUG,"Unable to load %s: %s",modfile,factory[count]->LastError());
4100 sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
4103 if (factory[count]->factory)
4105 modules[count] = factory[count]->factory->CreateModule();
4106 /* save the module and the module's classfactory, if
4107 * this isnt done, random crashes can occur :/ */
4111 log(DEBUG,"Unable to load %s",modfile);
4112 sprintf("Unable to load %s\nExiting...\n",modfile);
4115 /* Increase the Count */
4120 log(DEBUG,"InspIRCd: startup: Module Not Found %s",modfile);
4121 printf("Module Not Found: \033[1;37m%s\033[0;37m, Skipping\n",modfile);
4124 MODCOUNT = count - 1;
4125 log(DEBUG,"Total loaded modules: %d",MODCOUNT+1);
4127 servers = new server_list;
4130 printf("\nInspIRCd is now running!\n");
4132 startup_time = time(NULL);
4134 if (DaemonSeed() == ERROR)
4136 log(DEBUG,"InspIRCd: startup: can't daemonise");
4137 printf("ERROR: could not go into daemon mode. Shutting down.\n");
4142 /* setup select call */
4143 FD_ZERO(&selectFds);
4144 log(DEBUG,"InspIRCd: startup: zero selects");
4145 log(VERBOSE,"InspIRCd: startup: portCount = %d", portCount);
4147 for (count = 0; count < portCount; count++)
4149 if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
4151 log(DEBUG,"InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
4154 if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
4156 log(DEBUG,"InspIRCd: startup: failed to bind port %d",ports[count]);
4158 else /* well we at least bound to one socket so we'll continue */
4164 log(DEBUG,"InspIRCd: startup: total bound ports %d",boundPortCount);
4166 /* if we didn't bind to anything then abort */
4167 if (boundPortCount == 0)
4169 log(DEBUG,"InspIRCd: startup: no ports bound, bailing!");
4173 length = sizeof (client);
4174 int flip_flop = 0, udp_port = 0;
4175 char udp_msg[MAXBUF], udp_host[MAXBUF];
4177 /* main loop for multiplexing/resetting */
4180 /* set up select call */
4181 for (count = 0; count < boundPortCount; count++)
4183 FD_SET (openSockfd[count], &selectFds);
4186 /* added timeout! select was waiting forever... wank... :/ */
4197 selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
4199 for (int x = 0; x != UDPportCount; x++)
4201 if (me[x]->RecvPacket(udp_msg, udp_host, udp_port))
4203 FOREACH_MOD OnPacketReceive(udp_msg);
4204 WriteOpers("UDP Link Packet: '%s' from %s:%d:%d [route%d]",udp_msg,udp_host,udp_port,me[x]->port,x);
4205 // Packets must go back via the route they arrived on :)
4206 handle_link_packet(udp_msg, udp_host, udp_port, me[x]);
4210 for (user_hash::iterator count2 = clientlist.begin(); count2 != clientlist.end(); count2++)
4214 if (!count2->second) break;
4217 if (count2->second->fd)
4219 if (((time(NULL)) > count2->second->nping) && (isnick(count2->second->nick)) && (count2->second->registered == 7))
4221 if (!count2->second->lastping)
4223 log(DEBUG,"InspIRCd: ping timeout: %s",count2->second->nick);
4224 kill_link(count2->second,"Ping timeout");
4227 Write(count2->second->fd,"PING :%s",ServerName);
4228 log(DEBUG,"InspIRCd: pinging: %s",count2->second->nick);
4229 count2->second->lastping = 0;
4230 count2->second->nping = time(NULL)+120;
4233 result = read(count2->second->fd, data, 1);
4234 // result EAGAIN means nothing read
4235 if (result == EAGAIN)
4241 log(DEBUG,"InspIRCd: Exited: %s",count2->second->nick);
4242 kill_link(count2->second,"Client exited");
4244 else if (result > 0)
4246 strncat(count2->second->inbuf, data, result);
4247 if (strchr(count2->second->inbuf, '\n') || strchr(count2->second->inbuf, '\r'))
4249 /* at least one complete line is waiting to be processed */
4250 if (!count2->second->fd)
4254 process_buffer(count2->second);
4262 /* select is reporting a waiting socket. Poll them all to find out which */
4263 if (selectResult > 0)
4265 char target[MAXBUF], resolved[MAXBUF];
4266 for (count = 0; count < boundPortCount; count++)
4268 if (FD_ISSET (openSockfd[count], &selectFds))
4270 incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
4272 address_cache::iterator iter = IP.find(client.sin_addr);
4273 bool iscached = false;
4274 if (iter == IP.end())
4276 /* ip isn't in cache, add it */
4277 strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
4278 if(CleanAndResolve(resolved, target) != TRUE)
4280 strncpy(resolved,target,MAXBUF);
4282 /* hostname now in 'target' */
4283 IP[client.sin_addr] = new string(resolved);
4284 /* hostname in cache */
4288 /* found ip (cached) */
4289 strncpy(resolved, iter->second->c_str(), MAXBUF);
4293 if (incomingSockfd < 0)
4295 WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
4296 log(DEBUG,"InspIRCd: accept failed: %d",ports[count]);
4300 AddClient(incomingSockfd, resolved, ports[count], iscached);
4301 log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
4310 close (incomingSockfd);