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 char ServerName[MAXBUF];
50 char ServerDesc[MAXBUF];
51 char AdminName[MAXBUF];
52 char AdminEmail[MAXBUF];
53 char AdminNick[MAXBUF];
55 char restartpass[MAXBUF];
59 char PrefixQuit[MAXBUF];
60 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 template<> struct hash<in_addr>
70 size_t operator()(const struct in_addr &a) const
73 memcpy(&q,&a,sizeof(size_t));
78 template<> struct hash<string>
80 size_t operator()(const string &s) const
83 static struct hash<const char *> strhash;
95 bool operator()(const string& s1, const string& s2) const
97 char a[MAXBUF],b[MAXBUF];
100 return (strcasecmp(a,b) == 0);
105 struct InAddr_HashComp
108 bool operator()(const in_addr &s1, const in_addr &s2) const
113 memcpy(&q,&s1,sizeof(size_t));
114 memcpy(&p,&s2,sizeof(size_t));
122 typedef hash_map<string, userrec*, hash<string>, StrHashComp> user_hash;
123 typedef hash_map<string, chanrec*, hash<string>, StrHashComp> chan_hash;
124 typedef hash_map<in_addr,string*, hash<in_addr>, InAddr_HashComp> address_cache;
125 typedef deque<command_t> command_table;
126 typedef DLLFactory<ModuleFactory> ircd_module;
128 user_hash clientlist;
132 command_table cmdlist;
136 vector<Module*> modules(255);
137 vector<ircd_module*> factory(255);
140 struct linger linger = { 0 };
141 char bannerBuffer[MAXBUF];
142 int boundPortCount = 0;
146 int has_channel(userrec *u, chanrec *c);
147 int usercount(chanrec *c);
148 int usercount_i(chanrec *c);
149 void update_stats_l(int fd,int data_out);
150 char* Passwd(userrec *user);
151 bool IsDenied(userrec *user);
152 void AddWhoWas(userrec* u);
155 void safedelete(userrec *p)
159 debug("deleting %s %s %s %s",p->nick,p->ident,p->dhost,p->fullname);
160 debug("safedelete(userrec*): pointer is safe to delete");
165 debug("safedelete(userrec*): unsafe pointer operation squished");
169 void safedelete(chanrec *p)
174 debug("safedelete(chanrec*): pointer is safe to delete");
178 debug("safedelete(chanrec*): unsafe pointer operation squished");
183 /* chop a string down to 512 characters and preserve linefeed (irc max
188 if (strlen(str) > 512)
197 string getservername()
202 string getnetworkname()
207 string getadminname()
212 string getadminemail()
217 string getadminnick()
222 void debug(char *text, ...)
224 char textbuffer[MAXBUF];
228 struct tm * timeinfo;
231 timeinfo = localtime (&rawtime);
235 f = fopen("ircd.log","a+");
239 va_start (argsPtr, text);
240 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
242 strcpy(b,asctime(timeinfo));
243 b[strlen(b)-1] = ':';
244 fprintf(f,"%s %s\n",b,textbuffer);
249 printf("Can't write log file, bailing!!!");
255 void readfile(file_cache &F, const char* fname)
258 char linebuf[MAXBUF];
260 debug("readfile: loading %s",fname);
262 file = fopen(fname,"r");
267 fgets(linebuf,sizeof(linebuf),file);
268 linebuf[strlen(linebuf)-1]='\0';
269 if (!strcmp(linebuf,""))
275 F.push_back(linebuf);
282 debug("readfile: failed to load file: %s",fname);
284 debug("readfile: loaded %s, %d lines",fname,F.size());
287 void ReadConfig(void)
289 char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF];
292 ConfValue("server","name",0,ServerName);
293 ConfValue("server","description",0,ServerDesc);
294 ConfValue("server","network",0,Network);
295 ConfValue("admin","name",0,AdminName);
296 ConfValue("admin","email",0,AdminEmail);
297 ConfValue("admin","nick",0,AdminNick);
298 ConfValue("files","motd",0,motd);
299 ConfValue("files","rules",0,rules);
300 ConfValue("power","diepass",0,diepass);
301 ConfValue("power","pause",0,pauseval);
302 ConfValue("power","restartpass",0,restartpass);
303 ConfValue("options","prefixquit",0,PrefixQuit);
304 ConfValue("die","value",0,DieValue);
305 ConfValue("options","debug",0,dbg);
307 if (!strcmp(dbg,"on"))
311 DieDelay = atoi(pauseval);
313 readfile(RULES,rules);
314 debug("Reading connect classes");
316 for (int i = 0; i < ConfValueEnum("connect"); i++)
319 ConfValue("connect","allow",i,Value);
320 if (strcmp(Value,""))
322 strcpy(c.host,Value);
325 ConfValue("connect","password",i,Value);
326 strcpy(c.pass,Value);
327 Classes.push_back(c);
328 debug("Read connect class type ALLOW, host=%s password=%s",c.host,c.pass);
332 ConfValue("connect","deny",i,Value);
333 strcpy(c.host,Value);
335 Classes.push_back(c);
336 debug("Read connect class type DENY, host=%s",c.host);
345 debug("Blocking: %d",s);
346 flags = fcntl(s, F_GETFL, 0);
347 fcntl(s, F_SETFL, flags ^ O_NONBLOCK);
350 void NonBlocking(int s)
353 debug("NonBlocking: %d",s);
354 flags = fcntl(s, F_GETFL, 0);
355 fcntl(s, F_SETFL, flags | O_NONBLOCK);
359 int CleanAndResolve (char *resolvedHost, const char *unresolvedHost)
361 struct hostent *hostPtr = NULL;
364 memset (resolvedHost, '\0',MAXBUF);
365 if(unresolvedHost == NULL)
367 if ((inet_aton(unresolvedHost,&addr)) == 0)
369 hostPtr = gethostbyaddr ((char *)&addr.s_addr,sizeof(addr.s_addr),AF_INET);
371 snprintf(resolvedHost,MAXBUF,"%s",hostPtr->h_name);
373 snprintf(resolvedHost,MAXBUF,"%s",unresolvedHost);
377 /* write formatted text to a socket, in same format as printf */
379 void Write(int sock,char *text, ...)
381 char textbuffer[MAXBUF];
385 va_start (argsPtr, text);
386 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
388 sprintf(tb,"%s\r\n",textbuffer);
390 write(sock,tb,strlen(tb));
391 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
394 /* write a server formatted numeric response to a single socket */
396 void WriteServ(int sock, char* text, ...)
398 char textbuffer[MAXBUF],tb[MAXBUF];
400 va_start (argsPtr, text);
402 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
404 sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
406 write(sock,tb,strlen(tb));
407 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
410 /* write text from an originating user to originating user */
412 void WriteFrom(int sock, userrec *user,char* text, ...)
414 char textbuffer[MAXBUF],tb[MAXBUF];
416 va_start (argsPtr, text);
418 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
420 sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
422 write(sock,tb,strlen(tb));
423 update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
426 /* write text to an destination user from a source user (e.g. user privmsg) */
428 void WriteTo(userrec *source, userrec *dest,char *data, ...)
430 char textbuffer[MAXBUF],tb[MAXBUF];
432 va_start (argsPtr, data);
433 if ((!dest) || (!source))
437 vsnprintf(textbuffer, MAXBUF, data, argsPtr);
440 WriteFrom(dest->fd,source,"%s",textbuffer);
443 /* write formatted text from a source user to all users on a channel
444 * including the sender (NOT for privmsg, notice etc!) */
446 void WriteChannel(chanrec* Ptr, userrec* user, char* text, ...)
448 char textbuffer[MAXBUF];
450 va_start (argsPtr, text);
451 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
453 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
455 if (has_channel(i->second,Ptr))
457 WriteTo(user,i->second,"%s",textbuffer);
462 /* write formatted text from a source user to all users on a channel except
463 * for the sender (for privmsg etc) */
465 void ChanExceptSender(chanrec* Ptr, userrec* user, char* text, ...)
467 char textbuffer[MAXBUF];
469 va_start (argsPtr, text);
470 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
473 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
475 if (has_channel(i->second,Ptr) && (user != i->second))
477 WriteTo(user,i->second,"%s",textbuffer);
482 int c_count(userrec* u)
485 for (int i =0; i != MAXCHANS; i++)
486 if (u->chans[i].channel)
492 /* return 0 or 1 depending if users u and u2 share one or more common channels
493 * (used by QUIT, NICK etc which arent channel specific notices) */
495 int common_channels(userrec *u, userrec *u2)
504 for (i = 0; i != MAXCHANS; i++)
506 for (z = 0; z != MAXCHANS; z++)
508 if ((u->chans[i].channel == u2->chans[z].channel) && (u->chans[i].channel) && (u2->chans[z].channel) && (u->registered == 7) && (u2->registered == 7))
510 if ((c_count(u)) && (c_count(u2)))
520 /* write a formatted string to all users who share at least one common
521 * channel, including the source user e.g. for use in NICK */
523 void WriteCommon(userrec *u, char* text, ...)
525 char textbuffer[MAXBUF];
527 va_start (argsPtr, text);
528 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
531 WriteFrom(u->fd,u,"%s",textbuffer);
533 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
535 if (common_channels(u,i->second) && (i->second != u))
537 WriteFrom(i->second->fd,u,"%s",textbuffer);
542 /* write a formatted string to all users who share at least one common
543 * channel, NOT including the source user e.g. for use in QUIT */
545 void WriteCommonExcept(userrec *u, char* text, ...)
547 char textbuffer[MAXBUF];
549 va_start (argsPtr, text);
550 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
553 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
555 if ((common_channels(u,i->second)) && (u != i->second))
557 WriteFrom(i->second->fd,u,"%s",textbuffer);
562 void WriteOpers(char* text, ...)
564 char textbuffer[MAXBUF];
566 va_start (argsPtr, text);
567 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
570 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
572 if (strchr(i->second->modes,'o'))
574 if (strchr(i->second->modes,'s'))
576 // send server notices to all with +s
577 // (TODO: needs SNOMASKs)
578 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,textbuffer);
584 void WriteWallOps(userrec *source, char* text, ...)
587 char textbuffer[MAXBUF];
589 va_start (argsPtr, text);
590 vsnprintf(textbuffer, MAXBUF, text, argsPtr);
593 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
595 if (strchr(i->second->modes,'w'))
597 WriteTo(source,i->second,"WALLOPS %s",textbuffer);
602 /* convert a string to lowercase. Note following special circumstances
603 * taken from RFC 1459. Many "official" server branches still hold to this
604 * rule so i will too;
606 * Because of IRC's scandanavian origin, the characters {}| are
607 * considered to be the lower case equivalents of the characters []\,
608 * respectively. This is a critical issue when determining the
609 * equivalence of two nicknames.
612 void strlower(char *n)
618 for (int i = 0; i != strlen(n); i++)
620 n[i] = tolower(n[i]);
630 /* verify that a user's nickname is valid */
632 int isnick(const char* n)
644 if (strlen(n) > NICKMAX-1)
648 for (i = 0; i != strlen(n); i++)
650 if ((n[i] < 33) || (n[i] > 125))
654 /* can't occur ANYWHERE in a nickname! */
655 if (strchr("<>,./?:;@'~#=+()*&%$£ \"!",n[i]))
659 /* can't occur as the first char of a nickname... */
660 if ((strchr("0123456789",n[i])) && (!i))
668 /* Find a user record by nickname and return a pointer to it */
670 userrec* Find(string nick)
672 user_hash::iterator iter = clientlist.find(nick);
674 if (iter == clientlist.end())
675 /* Couldn't find it */
681 void update_stats_l(int fd,int data_out) /* add one line-out to stats L for this fd */
683 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
685 if (i->second->fd == fd)
687 i->second->bytes_out+=data_out;
688 i->second->cmds_out++;
694 /* find a channel record by channel name and return a pointer to it */
696 chanrec* FindChan(const char* chan)
698 chan_hash::iterator iter = chanlist.find(chan);
700 if (iter == chanlist.end())
701 /* Couldn't find it */
708 void purge_empty_chans(void)
710 int go_again = 1, purge = 0;
715 for (chan_hash::iterator i = chanlist.begin(); i != chanlist.end(); i++)
718 if (!usercount(i->second))
720 /* kill the record */
721 if (i != chanlist.end())
723 debug("del_channel: destroyed: %s",i->second->name);
734 debug("completed channel purge, killed %d",purge);
737 /* returns the status character for a given user on a channel, e.g. @ for op,
738 * % for halfop etc. If the user has several modes set, the highest mode
739 * the user has must be returned. */
741 char* cmode(userrec *user, chanrec *chan)
744 for (i = 0; i != MAXCHANS; i++)
746 if ((user->chans[i].channel == chan) && (chan != NULL))
748 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
752 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
756 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
765 char scratch[MAXMODES];
767 char* chanmodes(chanrec *chan)
770 if (chan->noexternal)
778 if (strcmp(chan->key,""))
786 if (chan->inviteonly)
802 if (strcmp(chan->key,""))
805 strcat(scratch,chan->key);
810 sprintf(foo," %d",chan->limit);
813 debug("chanmodes: %s %s",chan->name,scratch);
817 /* returns the status value for a given user on a channel, e.g. STATUS_OP for
818 * op, STATUS_VOICE for voice etc. If the user has several modes set, the
819 * highest mode the user has must be returned. */
821 int cstatus(userrec *user, chanrec *chan)
824 for (i = 0; i != MAXCHANS; i++)
826 if ((user->chans[i].channel == chan) && (chan != NULL))
828 if ((user->chans[i].uc_modes & UCMODE_OP) > 0)
832 if ((user->chans[i].uc_modes & UCMODE_HOP) > 0)
836 if ((user->chans[i].uc_modes & UCMODE_VOICE) > 0)
840 return STATUS_NORMAL;
846 /* compile a userlist of a channel into a string, each nick seperated by
847 * spaces and op, voice etc status shown as @ and + */
849 void userlist(userrec *user,chanrec *c)
851 sprintf(list,"353 %s = %s :", user->nick, c->name);
852 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
854 if (has_channel(i->second,c))
856 if (isnick(i->second->nick))
858 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
860 /* user is +i, and source not on the channel, does not show
861 * nick in NAMES list */
864 strcat(list,cmode(i->second,c));
865 strcat(list,i->second->nick);
867 if (strlen(list)>(480-NICKMAX))
869 /* list overflowed into
870 * multiple numerics */
871 WriteServ(user->fd,list);
872 sprintf(list,"353 %s = %s :", user->nick, c->name);
877 /* if whats left in the list isnt empty, send it */
878 if (list[strlen(list)-1] != ':')
880 WriteServ(user->fd,list);
884 /* return a count of the users on a specific channel accounting for
885 * invisible users who won't increase the count. e.g. for /LIST */
887 int usercount_i(chanrec *c)
893 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
895 if (has_channel(i->second,c))
897 if (isnick(i->second->nick))
899 if ((!has_channel(i->second,c)) && (strchr(i->second->modes,'i')))
901 /* user is +i, and source not on the channel, does not show
902 * nick in NAMES list */
909 debug("usercount_i: %s %d",c->name,count);
914 int usercount(chanrec *c)
920 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
922 if (has_channel(i->second,c))
924 if (isnick(i->second->nick))
930 debug("usercount: %s %d",c->name,count);
935 /* add a channel to a user, creating the record for it if needed and linking
936 * it to the user record */
938 chanrec* add_channel(userrec *user, char* cname, char* key)
944 if ((!cname) || (!user))
948 if (strlen(cname) > CHANMAX-1)
950 cname[CHANMAX-1] = '\0';
953 debug("add_channel: %s %s",user->nick,cname);
955 if ((has_channel(user,FindChan(cname))) && (FindChan(cname)))
957 return NULL; // already on the channel!
960 if (!FindChan(cname))
962 /* create a new one */
963 debug("add_channel: creating: %s",cname);
965 chanlist[cname] = new chanrec();
967 strcpy(chanlist[cname]->name, cname);
968 chanlist[cname]->topiclock = 1;
969 chanlist[cname]->noexternal = 1;
970 chanlist[cname]->created = time(NULL);
971 strcpy(chanlist[cname]->topic, "");
972 strncpy(chanlist[cname]->setby, user->nick,NICKMAX);
973 chanlist[cname]->topicset = 0;
974 Ptr = chanlist[cname];
975 debug("add_channel: created: %s",cname);
976 /* set created to 2 to indicate user
977 * is the first in the channel
978 * and should be given ops */
984 /* channel exists, just fish out a pointer to its struct */
985 Ptr = FindChan(cname);
988 debug("add_channel: joining to: %s",Ptr->name);
989 if (strcmp(Ptr->key,""))
991 debug("add_channel: %s has key %s",Ptr->name,Ptr->key);
994 debug("add_channel: no key given in JOIN");
995 WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
1000 debug("key at %p is %s",key,key);
1001 if (strcasecmp(key,Ptr->key))
1003 debug("add_channel: bad key given in JOIN");
1004 WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
1010 if (Ptr->inviteonly)
1012 if (user->IsInvited(Ptr->name))
1014 /* user was invited to channel */
1015 /* there may be an optional channel NOTICE here */
1019 WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
1026 if (usercount(Ptr) == Ptr->limit)
1028 WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
1033 /* check user against the channel banlist */
1034 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
1036 if (match(user->GetFullHost(),i->data))
1038 WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
1043 user->RemoveInvite(Ptr->name);
1050 for (i =0; i != MAXCHANS; i++)
1052 if (user->chans[i].channel == NULL)
1056 /* first user in is given ops */
1057 user->chans[i].uc_modes = UCMODE_OP;
1061 user->chans[i].uc_modes = 0;
1063 user->chans[i].channel = Ptr;
1064 WriteChannel(Ptr,user,"JOIN :%s",Ptr->name);
1067 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
1068 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
1071 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
1072 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name,chanmodes(Ptr));
1073 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
1074 FOREACH_MOD OnUserJoin(user,Ptr);
1078 debug("add_channel: user channel max exceeded: %s %s",user->nick,cname);
1079 WriteServ(user->fd,"405 %s %s :You are on too many channels",user->nick, cname);
1083 /* remove a channel from a users record, and remove the record from memory
1084 * if the channel has become empty */
1086 chanrec* del_channel(userrec *user, char* cname, char* reason)
1092 if ((!cname) || (!user))
1097 Ptr = FindChan(cname);
1104 FOREACH_MOD OnUserPart(user,Ptr);
1105 debug("del_channel: removing: %s %s",user->nick,Ptr->name);
1107 for (i =0; i != MAXCHANS; i++)
1109 /* zap it from the channel list of the user */
1110 if (user->chans[i].channel == Ptr)
1114 WriteChannel(Ptr,user,"PART %s :%s",Ptr->name, reason);
1118 WriteChannel(Ptr,user,"PART :%s",Ptr->name);
1120 user->chans[i].uc_modes = 0;
1121 user->chans[i].channel = NULL;
1122 debug("del_channel: unlinked: %s %s",user->nick,Ptr->name);
1127 /* if there are no users left on the channel */
1128 if (!usercount(Ptr))
1130 chan_hash::iterator iter = chanlist.find(Ptr->name);
1132 debug("del_channel: destroying channel: %s",Ptr->name);
1134 /* kill the record */
1135 if (iter != chanlist.end())
1137 debug("del_channel: destroyed: %s",Ptr->name);
1138 delete iter->second;
1139 chanlist.erase(iter);
1145 void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
1150 if ((!Ptr) || (!user) || (!src))
1155 debug("kick_channel: removing: %s %s %s",user->nick,Ptr->name,src->nick);
1157 if (!has_channel(user,Ptr))
1159 WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
1162 if ((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr)))
1164 if (cstatus(src,Ptr) == STATUS_HOP)
1166 WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
1170 WriteServ(src->fd,"482 %s %s :You must be at least a half-operator",src->nick, Ptr->name);
1176 for (i =0; i != MAXCHANS; i++)
1178 /* zap it from the channel list of the user */
1179 if (user->chans[i].channel == Ptr)
1181 WriteChannel(Ptr,src,"KICK %s %s :%s",Ptr->name, user->nick, reason);
1182 user->chans[i].uc_modes = 0;
1183 user->chans[i].channel = NULL;
1184 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 debug("del_channel: destroying channel: %s",Ptr->name);
1196 /* kill the record */
1197 if (iter != chanlist.end())
1199 debug("del_channel: destroyed: %s",Ptr->name);
1200 delete iter->second;
1201 chanlist.erase(iter);
1207 /* returns 1 if user u has channel c in their record, 0 if not */
1209 int has_channel(userrec *u, chanrec *c)
1217 for (i =0; i != MAXCHANS; i++)
1219 if (u->chans[i].channel == c)
1227 int give_ops(userrec *user,char *dest,chanrec *chan,int status)
1232 if ((!user) || (!dest) || (!chan))
1236 if (status != STATUS_OP)
1238 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1245 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1251 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1256 for (i = 0; i != MAXCHANS; i++)
1258 if ((d->chans[i].channel == chan) && (chan != NULL))
1260 if (d->chans[i].uc_modes & UCMODE_OP)
1262 /* mode already set on user, dont allow multiple */
1265 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_OP;
1266 debug("gave ops: %s %s",d->chans[i].channel->name,d->nick);
1274 int give_hops(userrec *user,char *dest,chanrec *chan,int status)
1279 if ((!user) || (!dest) || (!chan))
1283 if (status != STATUS_OP)
1285 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1293 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1298 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1303 for (i = 0; i != MAXCHANS; i++)
1305 if ((d->chans[i].channel == chan) && (chan != NULL))
1307 if (d->chans[i].uc_modes & UCMODE_HOP)
1309 /* mode already set on user, dont allow multiple */
1312 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_HOP;
1313 debug("gave h-ops: %s %s",d->chans[i].channel->name,d->nick);
1321 int give_voice(userrec *user,char *dest,chanrec *chan,int status)
1326 if ((!user) || (!dest) || (!chan))
1330 if (status < STATUS_HOP)
1332 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1340 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1345 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1350 for (i = 0; i != MAXCHANS; i++)
1352 if ((d->chans[i].channel == chan) && (chan != NULL))
1354 if (d->chans[i].uc_modes & UCMODE_VOICE)
1356 /* mode already set on user, dont allow multiple */
1359 d->chans[i].uc_modes = d->chans[i].uc_modes | UCMODE_VOICE;
1360 debug("gave voice: %s %s",d->chans[i].channel->name,d->nick);
1368 int take_ops(userrec *user,char *dest,chanrec *chan,int status)
1373 if ((!user) || (!dest) || (!chan))
1377 if (status != STATUS_OP)
1379 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1387 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1392 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1397 for (i = 0; i != MAXCHANS; i++)
1399 if ((d->chans[i].channel == chan) && (chan != NULL))
1401 if ((d->chans[i].uc_modes & UCMODE_OP) == 0)
1403 /* mode already set on user, dont allow multiple */
1406 d->chans[i].uc_modes ^= UCMODE_OP;
1407 debug("took ops: %s %s",d->chans[i].channel->name,d->nick);
1415 int take_hops(userrec *user,char *dest,chanrec *chan,int status)
1420 if ((!user) || (!dest) || (!chan))
1424 if (status != STATUS_OP)
1426 WriteServ(user->fd,"482 %s %s :You're not a channel operator",user->nick, chan->name);
1434 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1439 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1444 for (i = 0; i != MAXCHANS; i++)
1446 if ((d->chans[i].channel == chan) && (chan != NULL))
1448 if ((d->chans[i].uc_modes & UCMODE_HOP) == 0)
1450 /* mode already set on user, dont allow multiple */
1453 d->chans[i].uc_modes ^= UCMODE_HOP;
1454 debug("took h-ops: %s %s",d->chans[i].channel->name,d->nick);
1462 int take_voice(userrec *user,char *dest,chanrec *chan,int status)
1467 if ((!user) || (!dest) || (!chan))
1471 if (status < STATUS_HOP)
1473 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, chan->name);
1481 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1486 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, dest);
1491 for (i = 0; i != MAXCHANS; i++)
1493 if ((d->chans[i].channel == chan) && (chan != NULL))
1495 if ((d->chans[i].uc_modes & UCMODE_VOICE) == 0)
1497 /* mode already set on user, dont allow multiple */
1500 d->chans[i].uc_modes ^= UCMODE_VOICE;
1501 debug("took voice: %s %s",d->chans[i].channel->name,d->nick);
1509 void TidyBan(char *ban)
1511 char temp[MAXBUF],NICK[MAXBUF],IDENT[MAXBUF],HOST[MAXBUF];
1515 char* pos_of_pling = strchr(temp,'!');
1516 char* pos_of_at = strchr(temp,'@');
1518 pos_of_pling[0] = '\0';
1519 pos_of_at[0] = '\0';
1523 strncpy(NICK,temp,NICKMAX);
1524 strncpy(IDENT,pos_of_pling,IDENTMAX+1);
1525 strncpy(HOST,pos_of_at,160);
1527 sprintf(ban,"%s!%s@%s",NICK,IDENT,HOST);
1530 int add_ban(userrec *user,char *dest,chanrec *chan,int status)
1533 if ((!user) || (!dest) || (!chan))
1535 if (strchr(dest,'!')==0)
1537 if (strchr(dest,'@')==0)
1539 for (int i = 0; i < strlen(dest); i++)
1542 for (int i = 0; i < strlen(dest); i++)
1546 for (int i = 0; i < strlen(dest); i++)
1552 for (int i = 0; i < strlen(dest); i++)
1557 debug("add_ban: %s %s",chan->name,user->nick);
1560 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1562 if (!strcasecmp(i->data,dest))
1564 // dont allow a user to set the same ban twice
1569 b.set_time = time(NULL);
1570 strncpy(b.data,dest,MAXBUF);
1571 strncpy(b.set_by,user->nick,NICKMAX);
1572 chan->bans.push_back(b);
1576 int take_ban(userrec *user,char *dest,chanrec *chan,int status)
1578 if ((!user) || (!dest) || (!chan))
1583 debug("del_ban: %s %s",chan->name,user->nick);
1584 for (BanList::iterator i = chan->bans.begin(); i != chan->bans.end(); i++)
1586 if (!strcasecmp(i->data,dest))
1588 chan->bans.erase(i);
1595 void process_modes(char **parameters,userrec* user,chanrec *chan,int status, int pcnt)
1597 char modelist[MAXBUF];
1598 char outlist[MAXBUF];
1599 char outstr[MAXBUF];
1600 char outpars[32][MAXBUF];
1606 bool k_set = false, l_set = false;
1613 debug("process_modes: start");
1615 strcpy(modelist,parameters[1]); /* mode list, e.g. +oo-o */
1616 /* parameters[2] onwards are parameters for
1617 * modes that require them :) */
1618 strcpy(outlist,"+");
1621 debug("process_modes: modelist: %s",modelist);
1623 for (ptr = 0; ptr < strlen(modelist); ptr++)
1628 debug("process_modes: modechar: %c",modelist[ptr]);
1629 switch (modelist[ptr])
1634 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
1636 outlist[strlen(outlist)-1] = '-';
1640 strcat(outlist,"-");
1650 if ((outlist[strlen(outlist)-1] == '+') || (outlist[strlen(outlist)-1] == '-'))
1652 outlist[strlen(outlist)-1] = '+';
1656 strcat(outlist,"+");
1663 if ((param >= pcnt)) break;
1666 r = give_ops(user,parameters[param++],chan,status);
1670 r = take_ops(user,parameters[param++],chan,status);
1674 strcat(outlist,"o");
1675 strcpy(outpars[pc++],parameters[param-1]);
1680 if ((param >= pcnt)) break;
1683 r = give_hops(user,parameters[param++],chan,status);
1687 r = take_hops(user,parameters[param++],chan,status);
1691 strcat(outlist,"h");
1692 strcpy(outpars[pc++],parameters[param-1]);
1698 if ((param >= pcnt)) break;
1701 r = give_voice(user,parameters[param++],chan,status);
1705 r = take_voice(user,parameters[param++],chan,status);
1709 strcat(outlist,"v");
1710 strcpy(outpars[pc++],parameters[param-1]);
1715 if ((param >= pcnt)) break;
1718 r = add_ban(user,parameters[param++],chan,status);
1722 r = take_ban(user,parameters[param++],chan,status);
1726 strcat(outlist,"b");
1727 strcpy(outpars[pc++],parameters[param-1]);
1732 if ((param >= pcnt))
1740 if (!strcmp(chan->key,""))
1742 strcat(outlist,"k");
1743 strcpy(outpars[pc++],parameters[param++]);
1744 strcpy(chan->key,parameters[param-1]);
1750 /* only allow -k if correct key given */
1751 if (strcmp(chan->key,""))
1753 strcat(outlist,"k");
1754 strcpy(chan->key,"");
1764 strcat(outlist,"l");
1769 if ((param >= pcnt)) break;
1775 bool invalid = false;
1776 for (int i = 0; i < strlen(parameters[param]); i++)
1778 if ((parameters[param][i] < '0') || (parameters[param][i] > '9'))
1783 if (atoi(parameters[param]) < 1)
1791 chan->limit = atoi(parameters[param]);
1794 strcat(outlist,"l");
1795 strcpy(outpars[pc++],parameters[param++]);
1802 if (chan->inviteonly != mdir)
1804 strcat(outlist,"i");
1806 chan->inviteonly = mdir;
1810 if (chan->topiclock != mdir)
1812 strcat(outlist,"t");
1814 chan->topiclock = mdir;
1818 if (chan->noexternal != mdir)
1820 strcat(outlist,"n");
1822 chan->noexternal = mdir;
1826 if (chan->moderated != mdir)
1828 strcat(outlist,"m");
1830 chan->moderated = mdir;
1834 if (chan->secret != mdir)
1836 strcat(outlist,"s");
1837 if (chan->c_private)
1839 chan->c_private = 0;
1842 strcat(outlist,"-p+");
1846 strcat(outlist,"+p-");
1850 chan->secret = mdir;
1854 if (chan->c_private != mdir)
1856 strcat(outlist,"p");
1862 strcat(outlist,"-s+");
1866 strcat(outlist,"+s-");
1870 chan->c_private = mdir;
1877 /* this ensures only the *valid* modes are sent out onto the network */
1878 while ((outlist[strlen(outlist)-1] == '-') || (outlist[strlen(outlist)-1] == '+'))
1880 outlist[strlen(outlist)-1] = '\0';
1882 if (strcmp(outlist,""))
1884 strcpy(outstr,outlist);
1885 for (ptr = 0; ptr < pc; ptr++)
1888 strcat(outstr,outpars[ptr]);
1890 WriteChannel(chan,user,"MODE %s %s",chan->name,outstr);
1894 void handle_mode(char **parameters, int pcnt, userrec *user)
1900 char outpars[MAXBUF];
1902 dest = Find(parameters[0]);
1904 if ((dest) && (pcnt == 1))
1906 WriteServ(user->fd,"221 %s :+%s",user->nick,user->modes);
1909 if ((dest) && (pcnt > 1))
1914 if (strchr(user->modes,'o'))
1925 WriteServ(user->fd,"482 %s :Can't change mode for other users",user->nick);
1929 strcpy(outpars,"+");
1932 if ((parameters[1][0] != '+') && (parameters[1][0] != '-'))
1935 for (i = 0; i < strlen(parameters[1]); i++)
1937 if (parameters[1][i] == '+')
1941 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1943 outpars[strlen(outpars)-1] = '+';
1947 strcat(outpars,"+");
1953 if (parameters[1][i] == '-')
1957 if ((outpars[strlen(outpars)-1] == '+') || (outpars[strlen(outpars)-1] == '-'))
1959 outpars[strlen(outpars)-1] = '-';
1963 strcat(outpars,"-");
1971 if (strchr(user->modes,'o'))
1977 if ((parameters[1][i] == 'i') || (parameters[1][i] == 'w') || (parameters[1][i] == 's'))
1986 if (!strchr(dest->modes,parameters[1][i]))
1988 dest->modes[strlen(dest->modes)+1]='\0';
1989 dest->modes[strlen(dest->modes)] = parameters[1][i];
1990 outpars[strlen(outpars)+1]='\0';
1991 outpars[strlen(outpars)] = parameters[1][i];
2000 outpars[strlen(outpars)+1]='\0';
2001 outpars[strlen(outpars)] = parameters[1][i];
2004 for (q = 0; q < strlen(user->modes); q++)
2006 if (user->modes[q] != parameters[1][i])
2008 moo[0] = user->modes[q];
2013 strcpy(user->modes,temp);
2018 if (strlen(outpars))
2024 while (i < strlen (outpars))
2026 b[z++] = outpars[i++];
2028 if (i<strlen(outpars)-1)
2030 if (((outpars[i] == '-') || (outpars[i] == '+')) && ((outpars[i+1] == '-') || (outpars[i+1] == '+')))
2032 // someones playing silly buggers and trying
2033 // to put a +- or -+ into the line...
2037 if (i == strlen(outpars)-1)
2039 if ((outpars[i] == '-') || (outpars[i] == '+'))
2047 if ((b[z] == '-') || (b[z] == '+'))
2050 if ((!strcmp(b,"+")) || (!strcmp(b,"-")))
2053 WriteTo(user, dest, "MODE %s :%s", dest->nick, b);
2058 Ptr = FindChan(parameters[0]);
2063 /* just /modes #channel */
2064 WriteServ(user->fd,"324 %s %s +%s",user->nick, Ptr->name, chanmodes(Ptr));
2065 WriteServ(user->fd,"329 %s %s %d", user->nick, Ptr->name, Ptr->created);
2071 if ((!strcmp(parameters[1],"+b")) || (!strcmp(parameters[1],"b")))
2074 for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
2076 WriteServ(user->fd,"367 %s %s %s %s %d",user->nick, Ptr->name, i->data, i->set_by, i->set_time);
2078 WriteServ(user->fd,"368 %s %s :End of channel ban list",user->nick, Ptr->name);
2082 if ((cstatus(user,Ptr) < STATUS_HOP) && (Ptr))
2084 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, Ptr->name);
2088 process_modes(parameters,user,Ptr,cstatus(user,Ptr),pcnt);
2092 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2096 /* This function pokes and hacks at a parameter list like the following:
2098 * PART #winbot, #darkgalaxy :m00!
2100 * to turn it into a series of individual calls like this:
2102 * PART #winbot :m00!
2103 * PART #darkgalaxy :m00!
2105 * The seperate calls are sent to a callback function provided by the caller
2106 * (the caller will usually call itself recursively). The callback function
2107 * must be a command handler. Calling this function on a line with no list causes
2108 * no action to be taken. You must provide a starting and ending parameter number
2109 * where the range of the list can be found, useful if you have a terminating
2110 * parameter as above which is actually not part of the list, or parameters
2111 * before the actual list as well. This code is used by many functions which
2112 * can function as "one to list" (see the RFC) */
2114 int loop_call(handlerfunc fn, char **parameters, int pcnt, userrec *u, int start, int end, int joins)
2119 char blog[32][MAXBUF];
2120 char blog2[32][MAXBUF];
2121 int i = 0, j = 0, q = 0, total = 0, t = 0, t2 = 0, total2 = 0;
2122 char keystr[MAXBUF];
2125 for (i = 0; i <32; i++)
2128 for (i = 0; i <32; i++)
2129 strcpy(blog2[i],"");
2132 for (i = 0; i <10; i++)
2136 parameters[i] = moo;
2141 if (pcnt > 1) /* we have a key to copy */
2143 strcpy(keystr,parameters[1]);
2147 if (!parameters[start])
2151 if (!strchr(parameters[start],','))
2156 for (i = start; i <= end; i++)
2160 strcat(plist,parameters[i]);
2168 for (i = 0; i < t; i++)
2170 if (plist[i] == ',')
2173 strcpy(blog[j++],param);
2177 strcpy(blog[j++],param);
2180 if ((joins) && (keystr) && (total>0)) // more than one channel and is joining
2185 if ((joins) && (keystr))
2187 if (strchr(keystr,','))
2191 t2 = strlen(keystr);
2192 for (i = 0; i < t2; i++)
2194 if (keystr[i] == ',')
2197 strcpy(blog2[j++],param);
2201 strcpy(blog2[j++],param);
2206 for (j = 0; j < total; j++)
2212 for (q = end; q < pcnt-1; q++)
2214 if (parameters[q+1])
2216 pars[q-end+1] = parameters[q+1];
2219 if ((joins) && (parameters[1]))
2230 /* repeatedly call the function with the hacked parameter list */
2231 if ((joins) && (pcnt > 1))
2235 // pars[1] already set up and containing key from blog2[j]
2240 pars[1] = parameters[1];
2246 fn(pars,pcnt-(end-start),u);
2253 void handle_join(char **parameters, int pcnt, userrec *user)
2258 if (loop_call(handle_join,parameters,pcnt,user,0,0,1))
2260 if (parameters[0][0] == '#')
2262 Ptr = add_channel(user,parameters[0],parameters[1]);
2267 void handle_part(char **parameters, int pcnt, userrec *user)
2273 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-2,0))
2275 del_channel(user,parameters[0],parameters[1]);
2279 if (loop_call(handle_part,parameters,pcnt,user,0,pcnt-1,0))
2281 del_channel(user,parameters[0],NULL);
2285 void handle_kick(char **parameters, int pcnt, userrec *user)
2287 chanrec* Ptr = FindChan(parameters[0]);
2288 userrec* u = Find(parameters[1]);
2292 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2296 if (!has_channel(u,Ptr))
2298 WriteServ(user->fd,"442 %s %s :You're not on that channel!",user->nick, parameters[0]);
2304 kick_channel(user,u,Ptr,parameters[2]);
2308 kick_channel(user,u,Ptr,user->nick);
2313 void handle_die(char **parameters, int pcnt, userrec *user)
2315 debug("die: %s",user->nick);
2316 if (!strcmp(parameters[0],diepass))
2318 WriteOpers("*** DIE command from %s!%s@%s, terminating...",user->nick,user->ident,user->host);
2324 WriteOpers("*** Failed DIE Command from %s!%s@%s.",user->nick,user->ident,user->host);
2328 void handle_restart(char **parameters, int pcnt, userrec *user)
2330 debug("restart: %s",user->nick);
2331 if (!strcmp(parameters[0],restartpass))
2333 WriteOpers("*** RESTART command from %s!%s@%s, Pretending to restart till this is finished :D",user->nick,user->ident,user->host);
2336 /* Will finish this later when i can be arsed :) */
2340 WriteOpers("*** Failed RESTART Command from %s!%s@%s.",user->nick,user->ident,user->host);
2345 void kill_link(userrec *user,char* reason)
2347 user_hash::iterator iter = clientlist.find(user->nick);
2349 debug("kill_link: %s '%s'",user->nick,reason);
2350 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
2351 fdatasync(user->fd);
2352 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,reason);
2353 FOREACH_MOD OnUserQuit(user);
2354 debug("closing fd %d",user->fd);
2355 /* bugfix, cant close() a nonblocking socket (sux!) */
2356 WriteCommonExcept(user,"QUIT :%s",reason);
2359 NonBlocking(user->fd);
2362 if (iter != clientlist.end())
2364 debug("deleting user hash value %p",iter->second);
2365 delete iter->second;
2366 clientlist.erase(iter);
2369 purge_empty_chans();
2373 void handle_kill(char **parameters, int pcnt, userrec *user)
2375 userrec *u = Find(parameters[0]);
2376 char killreason[MAXBUF];
2378 debug("kill: %s %s",parameters[0],parameters[1]);
2381 WriteOpers("*** Local Kill by %s: %s!%s@%s (%s)",user->nick,u->nick,u->ident,u->host,parameters[1]);
2382 sprintf(killreason,"Killed (%s (%s))",user->nick,parameters[1]);
2383 kill_link(u,killreason);
2387 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2391 void handle_summon(char **parameters, int pcnt, userrec *user)
2393 WriteServ(user->fd,"445 %s :SUMMON has been disabled (depreciated command)",user->nick);
2396 void handle_users(char **parameters, int pcnt, userrec *user)
2398 WriteServ(user->fd,"445 %s :USERS has been disabled (depreciated command)",user->nick);
2402 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
2404 char* Passwd(userrec *user)
2406 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
2408 if (match(user->host,i->host) && (i->type == CC_ALLOW))
2416 bool IsDenied(userrec *user)
2418 for (ClassVector::iterator i = Classes.begin(); i != Classes.end(); i++)
2420 if (match(user->host,i->host) && (i->type == CC_DENY))
2429 void handle_pass(char **parameters, int pcnt, userrec *user)
2431 if (!strcasecmp(parameters[0],Passwd(user)))
2433 user->haspassed = true;
2437 void handle_invite(char **parameters, int pcnt, userrec *user)
2439 userrec* u = Find(parameters[0]);
2440 chanrec* c = FindChan(parameters[1]);
2446 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[1]);
2452 WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, parameters[0]);
2461 if (cstatus(user,c) < STATUS_HOP)
2463 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator",user->nick, c->name);
2467 u->InviteTo(c->name);
2468 WriteFrom(u->fd,user,"INVITE %s :%s",u->nick,c->name);
2469 WriteServ(user->fd,"341 %s %s %s",user->nick,u->nick,c->name);
2473 void handle_topic(char **parameters, int pcnt, userrec *user)
2479 if (strlen(parameters[0]) <= CHANMAX)
2481 Ptr = FindChan(parameters[0]);
2486 WriteServ(user->fd,"332 %s %s :%s", user->nick, Ptr->name, Ptr->topic);
2487 WriteServ(user->fd,"333 %s %s %s %d", user->nick, Ptr->name, Ptr->setby, Ptr->topicset);
2491 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
2496 WriteServ(user->fd,"331 %s %s :No topic is set.", user->nick, Ptr->name);
2502 if (loop_call(handle_topic,parameters,pcnt,user,0,pcnt-2,0))
2504 if (strlen(parameters[0]) <= CHANMAX)
2506 Ptr = FindChan(parameters[0]);
2509 if ((Ptr->topiclock) && (cstatus(user,Ptr)<STATUS_HOP))
2511 WriteServ(user->fd,"482 %s %s :You must be at least a half-operator", user->nick, Ptr->name);
2514 strcpy(Ptr->topic,parameters[1]);
2515 strcpy(Ptr->setby,user->nick);
2516 Ptr->topicset = time(NULL);
2517 WriteChannel(Ptr,user,"TOPIC %s :%s",Ptr->name, Ptr->topic);
2521 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2527 /* sends out an error notice to all connected clients (not to be used
2530 void send_error(char *s)
2532 debug("send_error: %s",s);
2533 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
2535 WriteServ(i->second->fd,"NOTICE %s :%s",i->second->nick,s);
2539 void Error(int status)
2541 signal (SIGALRM, SIG_IGN);
2542 signal (SIGPIPE, SIG_IGN);
2543 signal (SIGTERM, SIG_IGN);
2544 signal (SIGABRT, SIG_IGN);
2545 signal (SIGSEGV, SIG_IGN);
2546 signal (SIGURG, SIG_IGN);
2547 signal (SIGKILL, SIG_IGN);
2548 debug("*** fell down a pothole in the road to perfection ***");
2549 send_error("Error! Segmentation fault! save meeeeeeeeeeeeee *splat!*");
2553 int main (int argc, char *argv[])
2556 debug("*** InspIRCd starting up!");
2559 debug("main: no config");
2560 printf("ERROR: Your config file is missing, this IRCd will self destruct in 10 seconds!\n");
2563 if (InspIRCd() == ERROR)
2565 debug("main: daemon function bailed");
2566 printf("ERROR: could not initialise. Shutting down.\n");
2573 template<typename T> inline string ConvToStr(const T &in)
2576 if (!(tmp << in)) return string();
2580 /* re-allocates a nick in the user_hash after they change nicknames,
2581 * returns a pointer to the new user as it may have moved */
2583 userrec* ReHashNick(char* Old, char* New)
2585 user_hash::iterator newnick;
2586 user_hash::iterator oldnick = clientlist.find(Old);
2588 debug("ReHashNick: %s %s",Old,New);
2590 if (!strcasecmp(Old,New))
2592 debug("old nick is new nick, skipping");
2593 return oldnick->second;
2596 if (oldnick == clientlist.end()) return NULL; /* doesnt exist */
2598 debug("ReHashNick: Found hashed nick %s",Old);
2600 clientlist[New] = new userrec();
2601 clientlist[New] = oldnick->second;
2602 /*delete oldnick->second; */
2603 clientlist.erase(oldnick);
2605 debug("ReHashNick: Nick rehashed as %s",New);
2607 return clientlist[New];
2610 /* adds or updates an entry in the whowas list */
2611 void AddWhoWas(userrec* u)
2613 user_hash::iterator iter = whowas.find(u->nick);
2614 userrec *a = new userrec();
2615 strcpy(a->nick,u->nick);
2616 strcpy(a->ident,u->ident);
2617 strcpy(a->dhost,u->dhost);
2618 strcpy(a->host,u->host);
2619 strcpy(a->fullname,u->fullname);
2620 strcpy(a->server,u->server);
2621 a->signon = u->signon;
2623 /* MAX_WHOWAS: max number of /WHOWAS items
2624 * WHOWAS_STALE: number of hours before a WHOWAS item is marked as stale and
2625 * can be replaced by a newer one
2628 if (iter == whowas.end())
2630 if (whowas.size() == WHOWAS_MAX)
2632 for (user_hash::iterator i = whowas.begin(); i != whowas.end(); i++)
2634 // 3600 seconds in an hour ;)
2635 if ((i->second->signon)<(time(NULL)-(WHOWAS_STALE*3600)))
2639 debug("added WHOWAS entry, purged an old record");
2646 debug("added fresh WHOWAS entry");
2647 whowas[a->nick] = a;
2652 debug("updated WHOWAS entry");
2653 delete iter->second;
2659 /* add a client connection to the sockets list */
2660 void AddClient(int socket, char* host, int port, bool iscached)
2664 char resolved[MAXBUF];
2667 user_hash::iterator iter;
2669 tempnick = ConvToStr(socket) + "-unknown";
2670 sprintf(tn2,"%d-unknown",socket);
2672 iter = clientlist.find(tempnick);
2674 if (iter != clientlist.end()) return;
2677 * It is OK to access the value here this way since we know
2678 * it exists, we just created it above.
2680 * At NO other time should you access a value in a map or a
2681 * hash_map this way.
2683 clientlist[tempnick] = new userrec();
2685 NonBlocking(socket);
2686 debug("AddClient: %d %s %d",socket,host,port);
2689 clientlist[tempnick]->fd = socket;
2690 strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
2691 strncpy(clientlist[tempnick]->host, host,160);
2692 strncpy(clientlist[tempnick]->dhost, host,160);
2693 strncpy(clientlist[tempnick]->server, ServerName,256);
2694 clientlist[tempnick]->registered = 0;
2695 clientlist[tempnick]->signon = time(NULL);
2696 clientlist[tempnick]->nping = time(NULL)+240;
2697 clientlist[tempnick]->lastping = 1;
2698 clientlist[tempnick]->port = port;
2702 WriteServ(socket,"NOTICE Auth :Found your hostname (cached)...");
2706 WriteServ(socket,"NOTICE Auth :Looking up your hostname...");
2709 if (clientlist.size() == MAXCLIENTS)
2710 kill_link(clientlist[tempnick],"No more connections allowed in this class");
2713 void handle_names(char **parameters, int pcnt, userrec *user)
2717 if (loop_call(handle_names,parameters,pcnt,user,0,pcnt-1,0))
2719 c = FindChan(parameters[0]);
2722 /*WriteServ(user->fd,"353 %s = %s :%s", user->nick, c->name,*/
2724 WriteServ(user->fd,"366 %s %s :End of /NAMES list.", user->nick, c->name);
2728 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2733 void handle_privmsg(char **parameters, int pcnt, userrec *user)
2738 if (loop_call(handle_privmsg,parameters,pcnt,user,0,pcnt-2,0))
2740 if (parameters[0][0] == '#')
2742 chan = FindChan(parameters[0]);
2745 if ((chan->noexternal) && (!has_channel(user,chan)))
2747 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
2750 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
2752 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
2755 ChanExceptSender(chan, user, "PRIVMSG %s :%s", chan->name, parameters[1]);
2759 /* no such nick/channel */
2760 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2765 dest = Find(parameters[0]);
2768 if (strcmp(dest->awaymsg,""))
2770 /* auto respond with aweh msg */
2771 WriteServ(user->fd,"301 %s %s :%s",user->nick,dest->nick,dest->awaymsg);
2773 WriteTo(user, dest, "PRIVMSG %s :%s", dest->nick, parameters[1]);
2777 /* no such nick/channel */
2778 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2782 void handle_notice(char **parameters, int pcnt, userrec *user)
2787 if (loop_call(handle_notice,parameters,pcnt,user,0,pcnt-2,0))
2789 if (parameters[0][0] == '#')
2791 chan = FindChan(parameters[0]);
2794 if ((chan->noexternal) && (!has_channel(user,chan)))
2796 WriteServ(user->fd,"404 %s %s :Cannot send to channel (no external messages)", user->nick, chan->name);
2799 if ((chan->moderated) && (cstatus(user,chan)<STATUS_VOICE))
2801 WriteServ(user->fd,"404 %s %s :Cannot send to channel (+m)", user->nick, chan->name);
2804 WriteChannel(chan, user, "NOTICE %s :%s", chan->name, parameters[1]);
2808 /* no such nick/channel */
2809 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2814 dest = Find(parameters[0]);
2817 WriteTo(user, dest, "NOTICE %s :%s", dest->nick, parameters[1]);
2821 /* no such nick/channel */
2822 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2828 char* chlist(userrec *user)
2833 debug("chlist: %s",user->nick);
2839 for (i = 0; i != MAXCHANS; i++)
2841 if (user->chans[i].channel != NULL)
2843 if (user->chans[i].channel->name)
2845 strcpy(cmp,user->chans[i].channel->name);
2847 if (!strstr(lst,cmp))
2849 if ((!user->chans[i].channel->c_private) && (!user->chans[i].channel->secret))
2851 strcat(lst,cmode(user,user->chans[i].channel));
2852 strcat(lst,user->chans[i].channel->name);
2862 void handle_info(char **parameters, int pcnt, userrec *user)
2864 WriteServ(user->fd,"371 %s :The Inspire IRCd Project Has been brought to you by the following people..",user->nick);
2865 WriteServ(user->fd,"371 %s :Craig Edwards, Craig McLure, and Others..",user->nick);
2866 WriteServ(user->fd,"371 %s :Will finish this later when i can be arsed :p",user->nick);
2867 WriteServ(user->fd,"374 %s :End of /INFO list",user->nick);
2870 void handle_time(char **parameters, int pcnt, userrec *user)
2873 struct tm * timeinfo;
2876 timeinfo = localtime ( &rawtime );
2877 WriteServ(user->fd,"391 %s %s :%s",user->nick,ServerName, asctime (timeinfo) );
2881 void handle_whois(char **parameters, int pcnt, userrec *user)
2886 if (loop_call(handle_whois,parameters,pcnt,user,0,pcnt-1,0))
2888 dest = Find(parameters[0]);
2891 WriteServ(user->fd,"311 %s %s %s %s * :%s",user->nick, dest->nick, dest->ident, dest->dhost, dest->fullname);
2892 if ((user == dest) || (strchr(user->modes,'o')))
2894 WriteServ(user->fd,"378 %s %s :is connecting from *@%s",user->nick, dest->nick, dest->host);
2896 if (strcmp(chlist(dest),""))
2898 WriteServ(user->fd,"319 %s %s :%s",user->nick, dest->nick, chlist(dest));
2900 WriteServ(user->fd,"312 %s %s %s :%s",user->nick, dest->nick, dest->server, ServerDesc);
2901 if (strcmp(dest->awaymsg,""))
2903 WriteServ(user->fd,"301 %s %s :%s",user->nick, dest->nick, dest->awaymsg);
2905 if (strchr(dest->modes,'o'))
2907 WriteServ(user->fd,"313 %s %s :is an IRC operator",user->nick, dest->nick);
2909 //WriteServ(user->fd,"310 %s %s :is available for help.",user->nick, dest->nick);
2910 WriteServ(user->fd,"317 %s %s %d %d :seconds idle, signon time",user->nick, dest->nick, abs((dest->idle_lastmsg)-time(NULL)), dest->signon);
2912 WriteServ(user->fd,"318 %s %s :End of /WHOIS list.",user->nick, dest->nick);
2916 /* no such nick/channel */
2917 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
2921 void handle_quit(char **parameters, int pcnt, userrec *user)
2923 user_hash::iterator iter = clientlist.find(user->nick);
2925 /* theres more to do here, but for now just close the socket */
2928 if (parameters[0][0] == ':')
2932 Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,parameters[0]);
2933 WriteOpers("*** Client exiting: %s!%s@%s [%s]",user->nick,user->ident,user->host,parameters[0]);
2934 WriteCommonExcept(user,"QUIT :%s%s",PrefixQuit,parameters[0]);
2938 Write(user->fd,"ERROR :Closing link (%s@%s) [QUIT]",user->ident,user->host);
2939 WriteOpers("*** Client exiting: %s!%s@%s [Client exited]",user->nick,user->ident,user->host);
2940 WriteCommonExcept(user,"QUIT :Client exited");
2943 FOREACH_MOD OnUserQuit(user);
2945 /* confucious say, he who close nonblocking socket, get nothing! */
2948 NonBlocking(user->fd);
2951 if (iter != clientlist.end())
2953 debug("deleting user hash value");
2954 delete iter->second;
2955 clientlist.erase(iter);
2958 purge_empty_chans();
2961 void handle_who(char **parameters, int pcnt, userrec *user)
2965 /* theres more to do here, but for now just close the socket */
2968 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")))
2970 Ptr = user->chans[0].channel;
2971 printf(user->chans[0].channel->name);
2972 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
2974 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
2976 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);
2979 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
2982 if (parameters[0][0] = '#')
2984 Ptr = FindChan(parameters[0]);
2987 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
2989 if ((has_channel(i->second,Ptr)) && (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);
2998 WriteServ(user->fd,"401 %s %s :No suck nick/channel",user->nick, parameters[0]);
3004 if ((!strcmp(parameters[0],"0")) || (!strcmp(parameters[0],"*")) && (!strcmp(parameters[1],"o")))
3006 Ptr = user->chans[0].channel;
3007 printf(user->chans[0].channel->name);
3008 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3010 if ((common_channels(user,i->second)) && (isnick(i->second->nick)))
3012 if (strchr(i->second->modes,'o'))
3014 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);
3018 WriteServ(user->fd,"315 %s %s :End of /WHO list.",user->nick, Ptr->name);
3024 void handle_wallops(char **parameters, int pcnt, userrec *user)
3026 WriteWallOps(user,"%s",parameters[0]);
3029 void handle_list(char **parameters, int pcnt, userrec *user)
3033 WriteServ(user->fd,"321 %s Channel :Users Name",user->nick);
3034 for (chan_hash::const_iterator i = chanlist.begin(); i != chanlist.end(); i++)
3036 if ((!i->second->c_private) && (!i->second->secret))
3038 WriteServ(user->fd,"322 %s %s %d :[+%s] %s",user->nick,i->second->name,usercount_i(i->second),chanmodes(i->second),i->second->topic);
3041 WriteServ(user->fd,"323 %s :End of channel list.",user->nick);
3045 void handle_rehash(char **parameters, int pcnt, userrec *user)
3047 WriteServ(user->fd,"382 %s %s :Rehashing",user->nick,CONFIG_FILE);
3049 WriteOpers("%s is rehashing config file %s",user->nick,CONFIG_FILE);
3055 return clientlist.size();
3058 int usercount_invisible(void)
3062 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3064 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'i'))) c++;
3069 int usercount_opers(void)
3073 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3075 if ((i->second->fd) && (isnick(i->second->nick)) && (strchr(i->second->modes,'o'))) c++;
3080 int usercount_unknown(void)
3084 for (user_hash::const_iterator i = clientlist.begin(); i != clientlist.end(); i++)
3086 if ((i->second->fd) && (i->second->registered != 7))
3094 return chanlist.size();
3097 int servercount(void)
3102 void handle_lusers(char **parameters, int pcnt, userrec *user)
3104 WriteServ(user->fd,"251 %s :There are %d users and %d invisible on %d servers",user->nick,usercnt()-usercount_invisible(),usercount_invisible(),servercount());
3105 WriteServ(user->fd,"252 %s %d :operator(s) online",user->nick,usercount_opers());
3106 WriteServ(user->fd,"253 %s %d :unknown connections",user->nick,usercount_unknown());
3107 WriteServ(user->fd,"254 %s %d :channels formed",user->nick,chancount());
3108 WriteServ(user->fd,"254 %s :I have %d clients and 0 servers",user->nick,usercnt());
3111 void handle_admin(char **parameters, int pcnt, userrec *user)
3113 WriteServ(user->fd,"256 %s :Administrative info for %s",user->nick,ServerName);
3114 WriteServ(user->fd,"257 %s :Name - %s",user->nick,AdminName);
3115 WriteServ(user->fd,"258 %s :Nickname - %s",user->nick,AdminNick);
3116 WriteServ(user->fd,"258 %s :E-Mail - %s",user->nick,AdminEmail);
3119 void ShowMOTD(userrec *user)
3123 WriteServ(user->fd,"422 %s :Message of the day file is missing.",user->nick);
3126 WriteServ(user->fd,"375 %s :- %s message of the day",user->nick,ServerName);
3127 for (int i = 0; i != MOTD.size(); i++)
3129 WriteServ(user->fd,"372 %s :- %s",user->nick,MOTD[i].c_str());
3131 WriteServ(user->fd,"376 %s :End of %s message of the day.",user->nick,ServerName);
3134 void ShowRULES(userrec *user)
3138 WriteServ(user->fd,"NOTICE %s :Rules file is missing.",user->nick);
3141 WriteServ(user->fd,"NOTICE %s :%s rules",user->nick,ServerName);
3142 for (int i = 0; i != RULES.size(); i++)
3144 WriteServ(user->fd,"NOTICE %s :%s",user->nick,RULES[i].c_str());
3146 WriteServ(user->fd,"NOTICE %s :End of %s rules.",user->nick,ServerName);
3149 /* shows the message of the day, and any other on-logon stuff */
3150 void ConnectUser(userrec *user)
3152 user->registered = 7;
3153 user->idle_lastmsg = time(NULL);
3154 debug("ConnectUser: %s",user->nick);
3156 if (strcmp(Passwd(user),"") && (!user->haspassed))
3158 Write(user->fd,"ERROR :Closing link: Invalid password");
3159 fdatasync(user->fd);
3160 kill_link(user,"Invalid password");
3165 Write(user->fd,"ERROR :Closing link: Unauthorized connection");
3166 fdatasync(user->fd);
3167 kill_link(user,"Unauthorised connection");
3170 WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
3171 WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
3172 WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
3173 WriteServ(user->fd,"003 %s :This server was created %s %s",user->nick,__TIME__,__DATE__);
3174 WriteServ(user->fd,"004 %s :%s %s iowghraAsORVSxNCWqBzvdHtGI lvhopsmntikrRcaqOALQbSeKVfHGCuzN",user->nick,ServerName,VERSION);
3175 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);
3176 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);
3178 FOREACH_MOD OnUserConnect(user);
3179 WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
3182 void handle_version(char **parameters, int pcnt, userrec *user)
3184 WriteServ(user->fd,"351 %s :%s %s :%s",user->nick,VERSION,ServerName,SYSTEM);
3187 void handle_ping(char **parameters, int pcnt, userrec *user)
3189 WriteServ(user->fd,"PONG %s :%s",ServerName,parameters[0]);
3192 void handle_pong(char **parameters, int pcnt, userrec *user)
3194 // set the user as alive so they survive to next ping
3198 void handle_motd(char **parameters, int pcnt, userrec *user)
3203 void handle_rules(char **parameters, int pcnt, userrec *user)
3208 void handle_user(char **parameters, int pcnt, userrec *user)
3210 if (user->registered < 3)
3212 WriteServ(user->fd,"NOTICE Auth :No ident response, ident prefixed with ~");
3213 strcpy(user->ident,"~"); /* we arent checking ident... but these days why bother anyway? */
3214 strncat(user->ident,parameters[0],IDENTMAX);
3215 strncpy(user->fullname,parameters[3],128);
3216 user->registered = (user->registered | 1);
3220 WriteServ(user->fd,"462 %s :You may not reregister",user->nick);
3223 /* parameters 2 and 3 are local and remote hosts, ignored when sent by client connection */
3224 if (user->registered == 3)
3226 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
3231 void handle_userhost(char **parameters, int pcnt, userrec *user)
3233 char Return[MAXBUF],junk[MAXBUF];
3234 sprintf(Return,"302 %s :",user->nick);
3235 for (int i = 0; i < pcnt; i++)
3237 userrec *u = Find(parameters[i]);
3240 if (strchr(u->modes,'o'))
3242 sprintf(junk,"%s*=+%s@%s ",u->nick,u->ident,u->host);
3243 strcat(Return,junk);
3247 sprintf(junk,"%s=+%s@%s ",u->nick,u->ident,u->host);
3248 strcat(Return,junk);
3252 WriteServ(user->fd,Return);
3256 void handle_ison(char **parameters, int pcnt, userrec *user)
3258 char Return[MAXBUF];
3259 sprintf(Return,"303 %s :",user->nick);
3260 for (int i = 0; i < pcnt; i++)
3262 userrec *u = Find(parameters[i]);
3265 strcat(Return,u->nick);
3269 WriteServ(user->fd,Return);
3273 void handle_away(char **parameters, int pcnt, userrec *user)
3277 strcpy(user->awaymsg,parameters[0]);
3278 WriteServ(user->fd,"306 %s :You have been marked as being away",user->nick);
3282 strcpy(user->awaymsg,"");
3283 WriteServ(user->fd,"305 %s :You are no longer marked as being away",user->nick);
3287 void handle_whowas(char **parameters, int pcnt, userrec* user)
3289 user_hash::iterator i = whowas.find(parameters[0]);
3291 if (i == whowas.end())
3293 WriteServ(user->fd,"406 %s %s :There was no such nickname",user->nick,parameters[0]);
3294 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3298 time_t rawtime = i->second->signon;
3302 timeinfo = localtime(&rawtime);
3303 strcpy(b,asctime(timeinfo));
3304 b[strlen(b)-1] = '\0';
3306 WriteServ(user->fd,"314 %s %s %s %s * :%s",user->nick,i->second->nick,i->second->ident,i->second->dhost,i->second->fullname);
3307 WriteServ(user->fd,"312 %s %s %s :%s",user->nick,i->second->nick,i->second->server,b);
3308 WriteServ(user->fd,"369 %s %s :End of WHOWAS",user->nick,parameters[0]);
3313 void handle_trace(char **parameters, int pcnt, userrec *user)
3315 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
3319 if (isnick(i->second->nick))
3321 if (strchr(i->second->modes,'o'))
3323 WriteServ(user->fd,"205 %s :Oper 0 %s",user->nick,i->second->nick);
3327 WriteServ(user->fd,"204 %s :User 0 %s",user->nick,i->second->nick);
3332 WriteServ(user->fd,"203 %s :???? 0 [%s]",user->nick,i->second->host);
3338 void handle_stats(char **parameters, int pcnt, userrec *user)
3344 if (strlen(parameters[0])>1)
3346 /* make the stats query 1 character long */
3347 parameters[0][1] = '\0';
3350 /* stats m (list number of times each command has been used, plus bytecount) */
3351 if (!strcasecmp(parameters[0],"m"))
3353 for (int i = 0; i < cmdlist.size(); i++)
3355 if (cmdlist[i].handler_function)
3357 if (cmdlist[i].use_count)
3359 /* RPL_STATSCOMMANDS */
3360 WriteServ(user->fd,"212 %s %s %d %d",user->nick,cmdlist[i].command,cmdlist[i].use_count,cmdlist[i].total_bytes);
3367 /* stats z (debug and memory info) */
3368 if (!strcasecmp(parameters[0],"z"))
3370 WriteServ(user->fd,"249 %s :Users(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,clientlist.size(),clientlist.size()*sizeof(userrec),clientlist.bucket_count());
3371 WriteServ(user->fd,"249 %s :Channels(HASH_MAP) %d (%d bytes, %d buckets)",user->nick,chanlist.size(),chanlist.size()*sizeof(chanrec),chanlist.bucket_count());
3372 WriteServ(user->fd,"249 %s :Commands(VECTOR) %d (%d bytes)",user->nick,cmdlist.size(),cmdlist.size()*sizeof(command_t));
3373 WriteServ(user->fd,"249 %s :MOTD(VECTOR) %d, RULES(VECTOR) %d",user->nick,MOTD.size(),RULES.size());
3374 WriteServ(user->fd,"249 %s :address_cache(HASH_MAP) %d (%d buckets)",user->nick,IP.size(),IP.bucket_count());
3375 WriteServ(user->fd,"249 %s :Modules(VECTOR) %d (%d)",user->nick,modules.size(),modules.size()*sizeof(Module));
3376 WriteServ(user->fd,"249 %s :ClassFactories(VECTOR) %d (%d)",user->nick,factory.size(),factory.size()*sizeof(ircd_module));
3377 WriteServ(user->fd,"249 %s :Ports(STATIC_ARRAY) %d",user->nick,boundPortCount);
3381 if (!strcasecmp(parameters[0],"o"))
3383 for (int i = 0; i < ConfValueEnum("oper"); i++)
3385 char LoginName[MAXBUF];
3386 char HostName[MAXBUF];
3387 char OperType[MAXBUF];
3388 ConfValue("oper","name",i,LoginName);
3389 ConfValue("oper","host",i,HostName);
3390 ConfValue("oper","type",i,OperType);
3391 WriteServ(user->fd,"243 %s O %s * %s %s 0",user->nick,HostName,LoginName,OperType);
3395 /* stats l (show user I/O stats) */
3396 if (!strcasecmp(parameters[0],"l"))
3398 WriteServ(user->fd,"211 %s :server:port nick bytes_in cmds_in bytes_out cmds_out",user->nick);
3399 for (user_hash::iterator i = clientlist.begin(); i != clientlist.end(); i++)
3401 if (isnick(i->second->nick))
3403 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);
3407 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);
3413 /* stats u (show server uptime) */
3414 if (!strcasecmp(parameters[0],"u"))
3416 time_t current_time = 0;
3417 current_time = time(NULL);
3418 time_t server_uptime = current_time - startup_time;
3420 stime = gmtime(&server_uptime);
3421 /* i dont know who the hell would have an ircd running for over a year nonstop, but
3422 * Craig suggested this, and it seemed a good idea so in it went */
3423 if (stime->tm_year > 70)
3425 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);
3429 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);
3433 WriteServ(user->fd,"219 %s %s :End of /STATS report",user->nick,parameters[0]);
3434 WriteOpers("*** Notice: Stats '%s' requested by %s (%s@%s)",parameters[0],user->nick,user->ident,user->host);
3438 void handle_connect(char **parameters, int pcnt, userrec *user)
3442 void handle_squit(char **parameters, int pcnt, userrec *user)
3446 void handle_oper(char **parameters, int pcnt, userrec *user)
3448 char LoginName[MAXBUF];
3449 char Password[MAXBUF];
3450 char OperType[MAXBUF];
3451 char TypeName[MAXBUF];
3452 char Hostname[MAXBUF];
3455 for (i = 0; i < ConfValueEnum("oper"); i++)
3457 ConfValue("oper","name",i,LoginName);
3458 ConfValue("oper","password",i,Password);
3459 if ((!strcmp(LoginName,parameters[0])) && (!strcmp(Password,parameters[1])))
3461 /* correct oper credentials */
3462 ConfValue("oper","type",i,OperType);
3463 WriteOpers("*** %s (%s@%s) is now an IRC operator of type %s",user->nick,user->ident,user->host,OperType);
3464 WriteServ(user->fd,"381 %s :You are now an IRC operator of type %s",user->nick,OperType);
3465 WriteServ(user->fd,"MODE %s :+o",user->nick);
3466 for (j =0; j < ConfValueEnum("type"); j++)
3468 ConfValue("type","name",j,TypeName);
3469 if (!strcmp(TypeName,OperType))
3471 /* found this oper's opertype */
3472 ConfValue("type","host",j,Hostname);
3473 strncpy(user->dhost,Hostname,256);
3476 if (!strchr(user->modes,'o'))
3478 strcat(user->modes,"o");
3484 WriteServ(user->fd,"491 %s :Invalid oper credentials",user->nick);
3485 WriteOpers("*** WARNING! Failed oper attempt by %s!%s@%s!",user->nick,user->ident,user->host);
3488 void handle_nick(char **parameters, int pcnt, userrec *user)
3492 debug("not enough params for handle_nick");
3497 debug("invalid parameter passed to handle_nick");
3500 if (!strlen(parameters[0]))
3502 debug("zero length new nick passed to handle_nick");
3507 debug("invalid user passed to handle_nick");
3512 debug("invalid old nick passed to handle_nick");
3515 if (!strcasecmp(user->nick,parameters[0]))
3517 debug("old nick is new nick, skipping");
3522 if (strlen(parameters[0]) > 1)
3524 if (parameters[0][0] == ':')
3529 if ((Find(parameters[0])) && (Find(parameters[0]) != user))
3531 WriteServ(user->fd,"433 %s %s :Nickname is already in use.",user->nick,parameters[0]);
3535 if (isnick(parameters[0]) == 0)
3537 WriteServ(user->fd,"432 %s %s :Erroneous Nickname",user->nick,parameters[0]);
3541 if (user->registered == 7)
3543 WriteCommon(user,"NICK %s",parameters[0]);
3546 /* change the nick of the user in the users_hash */
3547 user = ReHashNick(user->nick, parameters[0]);
3548 /* actually change the nick within the record */
3550 if (!user->nick) return;
3552 strncpy(user->nick, parameters[0],NICKMAX);
3554 debug("new nick set: %s",user->nick);
3556 if (user->registered < 3)
3557 user->registered = (user->registered | 2);
3558 if (user->registered == 3)
3560 /* user is registered now, bit 0 = USER command, bit 1 = sent a NICK command */
3563 debug("exit nickchange: %s",user->nick);
3566 int process_parameters(char **command_p,char *parameters)
3571 q = strlen(parameters);
3574 /* no parameters, command_p invalid! */
3577 if (parameters[0] == ':')
3579 command_p[0] = parameters+1;
3584 if ((strchr(parameters,' ')==NULL) || (parameters[0] == ':'))
3586 /* only one parameter */
3587 command_p[0] = parameters;
3588 if (parameters[0] == ':')
3590 if (strchr(parameters,' ') != NULL)
3598 command_p[j++] = parameters;
3599 for (i = 0; i <= q; i++)
3601 if (parameters[i] == ' ')
3603 command_p[j++] = parameters+i+1;
3604 parameters[i] = '\0';
3605 if (command_p[j-1][0] == ':')
3607 *command_p[j-1]++; /* remove dodgy ":" */
3609 /* parameter like this marks end of the sequence */
3613 return j; /* returns total number of items in the list */
3616 void process_command(userrec *user, char* cmd)
3620 char *command_p[127];
3621 char p[MAXBUF], temp[MAXBUF];
3622 int i, j, items, cmd_found;
3624 for (int i = 0; i < 127; i++)
3625 command_p[i] = NULL;
3635 if (!strcmp(cmd,""))
3640 if (!strchr(cmd,' '))
3642 /* no parameters, lets skip the formalities and not chop up
3645 command_p[0] = NULL;
3647 for (int i = 0; i <= strlen(cmd); i++)
3649 cmd[i] = toupper(cmd[i]);
3656 /* strip out extraneous linefeeds through mirc's crappy pasting (thanks Craig) */
3657 for (i = 0; i < strlen(temp); i++)
3659 if ((temp[i] != 10) && (temp[i] != 13) && (temp[i] != 0) && (temp[i] != 7))
3665 /* split the full string into a command plus parameters */
3669 if (strchr(cmd,' '))
3671 for (i = 0; i <= strlen(cmd); i++)
3673 /* capitalise the command ONLY, leave params intact */
3674 cmd[i] = toupper(cmd[i]);
3675 /* are we nearly there yet?! :P */
3679 parameters = cmd+i+1;
3687 for (i = 0; i <= strlen(cmd); i++)
3689 cmd[i] = toupper(cmd[i]);
3697 for (i = 0; i != cmdlist.size(); i++)
3699 if (strcmp(cmdlist[i].command,""))
3701 if (!strcmp(command, cmdlist[i].command))
3705 if (strcmp(parameters,""))
3707 items = process_parameters(command_p,parameters);
3712 command_p[0] = NULL;
3718 command_p[0] = NULL;
3723 user->idle_lastmsg = time(NULL);
3724 /* activity resets the ping pending timer */
3725 user->nping = time(NULL) + 120;
3726 if ((items) < cmdlist[i].min_params)
3728 debug("process_command: not enough parameters: %s %s",user->nick,command);
3729 WriteServ(user->fd,"461 %s %s :Not enough parameters",user->nick,command);
3732 if ((!strchr(user->modes,cmdlist[i].flags_needed)) && (cmdlist[i].flags_needed))
3734 debug("process_command: permission denied: %s %s",user->nick,command);
3735 WriteServ(user->fd,"481 %s :Permission Denied- You do not have the required operator privilages",user->nick);
3739 /* if the command isnt USER, PASS, or NICK, and nick is empty,
3741 if ((strcmp(command,"USER")) && (strcmp(command,"NICK")) && (strcmp(command,"PASS")))
3743 if ((!isnick(user->nick)) || (user->registered != 7))
3745 debug("process_command: not registered: %s %s",user->nick,command);
3746 WriteServ(user->fd,"451 %s :You have not registered",command);
3750 if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
3752 debug("process_command: handler: %s %s %d",user->nick,command,items);
3753 if (cmdlist[i].handler_function)
3755 /* ikky /stats counters */
3760 user->bytes_in += strlen(temp);
3763 cmdlist[i].use_count++;
3764 cmdlist[i].total_bytes+=strlen(temp);
3767 /* WARNING: nothing may come after the
3768 * command handler call, as the handler
3769 * may free the user structure! */
3771 cmdlist[i].handler_function(command_p,items,user);
3777 debug("process_command: not registered: %s %s",user->nick,command);
3778 WriteServ(user->fd,"451 %s :You have not registered",command);
3786 if ((!cmd_found) && (user))
3788 debug("process_command: not in table: %s %s",user->nick,command);
3789 WriteServ(user->fd,"421 %s %s :Unknown command",user->nick,command);
3794 void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
3797 /* create the command and push it onto the table */
3798 strcpy(comm.command,cmd);
3799 comm.handler_function = f;
3800 comm.flags_needed = flags;
3801 comm.min_params = minparams;
3803 comm.total_bytes = 0;
3804 cmdlist.push_back(comm);
3807 void SetupCommandTable(void)
3809 createcommand("USER",handle_user,0,4);
3810 createcommand("NICK",handle_nick,0,1);
3811 createcommand("QUIT",handle_quit,0,1);
3812 createcommand("VERSION",handle_version,0,0);
3813 createcommand("PING",handle_ping,0,1);
3814 createcommand("PONG",handle_pong,0,1);
3815 createcommand("ADMIN",handle_admin,0,0);
3816 createcommand("PRIVMSG",handle_privmsg,0,2);
3817 createcommand("INFO",handle_info,0,0);
3818 createcommand("TIME",handle_time,0,0);
3819 createcommand("WHOIS",handle_whois,0,1);
3820 createcommand("WALLOPS",handle_wallops,'o',1);
3821 createcommand("NOTICE",handle_notice,0,2);
3822 createcommand("JOIN",handle_join,0,1);
3823 createcommand("NAMES",handle_names,0,1);
3824 createcommand("PART",handle_part,0,1);
3825 createcommand("KICK",handle_kick,0,2);
3826 createcommand("MODE",handle_mode,0,1);
3827 createcommand("TOPIC",handle_topic,0,1);
3828 createcommand("WHO",handle_who,0,1);
3829 createcommand("MOTD",handle_motd,0,0);
3830 createcommand("RULES",handle_join,0,0);
3831 createcommand("OPER",handle_oper,0,2);
3832 createcommand("LIST",handle_list,0,0);
3833 createcommand("DIE",handle_die,'o',1);
3834 createcommand("RESTART",handle_restart,'o',1);
3835 createcommand("KILL",handle_kill,'o',2);
3836 createcommand("REHASH",handle_rehash,'o',0);
3837 createcommand("LUSERS",handle_lusers,0,0);
3838 createcommand("STATS",handle_stats,0,1);
3839 createcommand("USERHOST",handle_userhost,0,1);
3840 createcommand("AWAY",handle_away,0,0);
3841 createcommand("ISON",handle_ison,0,0);
3842 createcommand("SUMMON",handle_summon,0,0);
3843 createcommand("USERS",handle_users,0,0);
3844 createcommand("INVITE",handle_invite,0,2);
3845 createcommand("PASS",handle_pass,0,1);
3846 createcommand("TRACE",handle_trace,'o',0);
3847 createcommand("WHOWAS",handle_whowas,0,1);
3848 createcommand("CONNECT",handle_connect,'o',1);
3849 createcommand("SQUIT",handle_squit,'o',1);
3852 void process_buffer(userrec *user)
3860 if (!strcmp(user->inbuf,""))
3864 strncpy(cmd,user->inbuf,MAXBUF);
3865 if (!strcmp(cmd,""))
3869 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
3871 cmd[strlen(cmd)-1] = '\0';
3873 if ((cmd[strlen(cmd)-1] == 13) || (cmd[strlen(cmd)-1] == 10))
3875 cmd[strlen(cmd)-1] = '\0';
3877 strcpy(user->inbuf,"");
3878 if (!strcmp(cmd,""))
3882 debug("InspIRCd: processing: %s %s",user->nick,cmd);
3883 process_command(user,cmd);
3888 struct sockaddr_in client, server;
3889 int portCount = 0, ports[MAXSOCKS];
3890 char addrs[MAXBUF][255];
3891 int openSockfd[MAXSOCKS], incomingSockfd, result = TRUE;
3893 int count = 0, scanDetectTrigger = TRUE, showBanner = FALSE;
3894 int selectResult = 0;
3895 char *temp, configToken[MAXBUF], stuff[MAXBUF], Addr[MAXBUF];
3896 char resolvedHost[MAXBUF];
3901 debug("InspIRCd: startup: begin");
3903 if ((geteuid()) && (getuid()) == 0)
3905 printf("WARNING!!! You are running an irc server as ROOT!!! DO NOT DO THIS!!!\n\n");
3907 debug("InspIRCd: startup: not starting with UID 0!");
3909 SetupCommandTable();
3910 debug("InspIRCd: startup: default command table set up");
3913 if (strcmp(DieValue,""))
3915 printf("WARNING: %s\n\n",DieValue);
3918 debug("InspIRCd: startup: read config");
3920 for (count = 0; count < ConfValueEnum("bind"); count++)
3922 ConfValue("bind","port",count,configToken);
3923 ConfValue("bind","address",count,Addr);
3924 ports[count] = atoi(configToken);
3925 strcpy(addrs[count],Addr);
3926 debug("InspIRCd: startup: read binding %s:%d from config",addrs[count],ports[count]);
3928 portCount = ConfValueEnum("bind");
3929 debug("InspIRCd: startup: read %d total ports",portCount);
3931 debug("InspIRCd: startup: InspIRCd is now running!");
3934 for (count = 0; count < ConfValueEnum("module"); count++)
3936 char modfile[MAXBUF];
3937 ConfValue("module","name",count,configToken);
3938 sprintf(modfile,"%s/%s",MOD_PATH,configToken);
3939 printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
3940 debug("InspIRCd: startup: Loading module: %s",modfile);
3942 factory[count] = new ircd_module(modfile);
3943 if (factory[count]->LastError())
3945 debug("Unable to load %s: %s",modfile,factory[count]->LastError());
3946 sprintf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
3949 if (factory[count]->factory)
3951 modules[count] = factory[count]->factory->CreateModule();
3952 /* save the module and the module's classfactory, if
3953 * this isnt done, random crashes can occur :/ */
3957 debug("Unable to load %s",modfile);
3958 sprintf("Unable to load %s\nExiting...\n",modfile);
3962 MODCOUNT = count - 1;
3963 debug("Total loaded modules: %d",MODCOUNT+1);
3965 printf("\nInspIRCd is now running!\n");
3967 startup_time = time(NULL);
3969 if (DaemonSeed() == ERROR)
3971 debug("InspIRCd: startup: can't daemonise");
3972 printf("ERROR: could not go into daemon mode. Shutting down.\n");
3977 /* setup select call */
3978 FD_ZERO(&selectFds);
3979 debug("InspIRCd: startup: zero selects");
3981 for (count = 0; count < portCount; count++)
3983 if ((openSockfd[boundPortCount] = OpenTCPSocket()) == ERROR)
3985 debug("InspIRCd: startup: bad fd %d",openSockfd[boundPortCount]);
3988 if (BindSocket(openSockfd[boundPortCount],client,server,ports[count],addrs[count]) == ERROR)
3990 debug("InspIRCd: startup: failed to bind port %d",ports[count]);
3992 else /* well we at least bound to one socket so we'll continue */
3998 debug("InspIRCd: startup: total bound ports %d",boundPortCount);
4000 /* if we didn't bind to anything then abort */
4001 if (boundPortCount == 0)
4003 debug("InspIRCd: startup: no ports bound, bailing!");
4007 length = sizeof (client);
4010 /* main loop for multiplexing/resetting */
4013 /* set up select call */
4014 for (count = 0; count < boundPortCount; count++)
4016 FD_SET (openSockfd[count], &selectFds);
4019 /* added timeout! select was waiting forever... wank... :/ */
4030 selectResult = select(MAXSOCKS, &selectFds, NULL, NULL, &tv);
4032 for (user_hash::iterator count2 = clientlist.begin(); count2 != clientlist.end(); count2++)
4036 if (!count2->second) break;
4039 if (count2->second->fd)
4041 if (((time(NULL)) > count2->second->nping) && (isnick(count2->second->nick)) && (count2->second->registered == 7))
4043 if (!count2->second->lastping)
4045 debug("InspIRCd: ping timeout: %s",count2->second->nick);
4046 kill_link(count2->second,"Ping timeout");
4049 Write(count2->second->fd,"PING :%s",ServerName);
4050 debug("InspIRCd: pinging: %s",count2->second->nick);
4051 count2->second->lastping = 0;
4052 count2->second->nping = time(NULL)+120;
4055 result = read(count2->second->fd, data, 1);
4056 // result EAGAIN means nothing read
4057 if (result == EAGAIN)
4063 debug("InspIRCd: Exited: %s",count2->second->nick);
4064 kill_link(count2->second,"Client exited");
4066 else if (result > 0)
4068 strncat(count2->second->inbuf, data, result);
4069 if (strchr(count2->second->inbuf, '\n') || strchr(count2->second->inbuf, '\r'))
4071 /* at least one complete line is waiting to be processed */
4072 if (!count2->second->fd)
4076 process_buffer(count2->second);
4084 /* select is reporting a waiting socket. Poll them all to find out which */
4085 if (selectResult > 0)
4087 char target[MAXBUF], resolved[MAXBUF];
4088 for (count = 0; count < boundPortCount; count++)
4090 if (FD_ISSET (openSockfd[count], &selectFds))
4092 incomingSockfd = accept (openSockfd[count], (struct sockaddr *) &client, &length);
4094 address_cache::iterator iter = IP.find(client.sin_addr);
4095 bool iscached = false;
4096 if (iter == IP.end())
4098 /* ip isn't in cache, add it */
4099 strncpy (target, (char *) inet_ntoa (client.sin_addr), MAXBUF);
4100 if(CleanAndResolve(resolved, target) != TRUE)
4102 strncpy(resolved,target,MAXBUF);
4104 /* hostname now in 'target' */
4105 IP[client.sin_addr] = new string(resolved);
4106 /* hostname in cache */
4110 /* found ip (cached) */
4111 strncpy(resolved, iter->second->c_str(), MAXBUF);
4115 if (incomingSockfd < 0)
4117 WriteOpers("*** WARNING: Accept failed on port %d (%s)", ports[count],target);
4118 debug("InspIRCd: accept failed: %d",ports[count]);
4122 AddClient(incomingSockfd, resolved, ports[count], iscached);
4123 debug("InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
4132 close (incomingSockfd);