]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
fixes to /STATS P
[user/henk/code/inspircd.git] / src / inspircd.cpp
index 5b9f54fd162146dea3ab41718391be87a99970fc..4250bdc75e9147a8bb1e40a0cca079fa1d4f2550 100644 (file)
@@ -54,6 +54,7 @@ using namespace std;
 #include "message.h"
 #include "mode.h"
 #include "commands.h"
+#include "xline.h"
 
 #ifdef GCC3
 #define nspace __gnu_cxx
@@ -81,8 +82,13 @@ int WHOWAS_MAX = 100;  // default 100 people maximum in the WHOWAS list
 int DieDelay  =  5;
 time_t startup_time = time(NULL);
 int NetBufferSize = 10240; // NetBufferSize used as the buffer size for all read() ops
+extern int MaxWhoResults;
 time_t nb_start = 0;
 
+bool AllowHalfop = true;
+bool AllowProtect = true;
+bool AllowFounder = true;
+
 extern vector<Module*> modules;
 std::vector<std::string> module_names;
 extern vector<ircd_module*> factory;
@@ -266,184 +272,203 @@ void log(int level,char *text, ...)
 
 void readfile(file_cache &F, const char* fname)
 {
-  FILE* file;
-  char linebuf[MAXBUF];
-
-  log(DEBUG,"readfile: loading %s",fname);
-  F.clear();
-  file =  fopen(fname,"r");
-  if (file)
-  {
-       while (!feof(file))
-       {
-               fgets(linebuf,sizeof(linebuf),file);
-               linebuf[strlen(linebuf)-1]='\0';
-               if (!strcmp(linebuf,""))
-               {
-                       strcpy(linebuf,"  ");
-               }
-               if (!feof(file))
-               {
-                       F.push_back(linebuf);
-               }
-       }
-       fclose(file);
-  }
-  else
-  {
-         log(DEBUG,"readfile: failed to load file: %s",fname);
-  }
-  log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
+       FILE* file;
+       char linebuf[MAXBUF];
+       
+       log(DEBUG,"readfile: loading %s",fname);
+       F.clear();
+       file =  fopen(fname,"r");
+       if (file)
+       {
+               while (!feof(file))
+               {
+                       fgets(linebuf,sizeof(linebuf),file);
+                       linebuf[strlen(linebuf)-1]='\0';
+                       if (!strcmp(linebuf,""))
+                       {
+                               strcpy(linebuf,"  ");
+                       }
+                       if (!feof(file))
+                       {
+                               F.push_back(linebuf);
+                       }
+               }
+               fclose(file);
+       }
+       else
+       {
+               log(DEBUG,"readfile: failed to load file: %s",fname);
+       }
+       log(DEBUG,"readfile: loaded %s, %d lines",fname,F.size());
 }
 
 void ReadConfig(void)
 {
-  char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF];
-  ConnectClass c;
-
-  LoadConf(CONFIG_FILE,&config_f);
-  
-  ConfValue("server","name",0,ServerName,&config_f);
-  ConfValue("server","description",0,ServerDesc,&config_f);
-  ConfValue("server","network",0,Network,&config_f);
-  ConfValue("admin","name",0,AdminName,&config_f);
-  ConfValue("admin","email",0,AdminEmail,&config_f);
-  ConfValue("admin","nick",0,AdminNick,&config_f);
-  ConfValue("files","motd",0,motd,&config_f);
-  ConfValue("files","rules",0,rules,&config_f);
-  ConfValue("power","diepass",0,diepass,&config_f);
-  ConfValue("power","pause",0,pauseval,&config_f);
-  ConfValue("power","restartpass",0,restartpass,&config_f);
-  ConfValue("options","prefixquit",0,PrefixQuit,&config_f);
-  ConfValue("die","value",0,DieValue,&config_f);
-  ConfValue("options","loglevel",0,dbg,&config_f);
-  ConfValue("options","netbuffersize",0,NB,&config_f);
-  NetBufferSize = atoi(NB);
-  if ((!NetBufferSize) || (NetBufferSize > 65535) || (NetBufferSize < 1024))
-  {
-       log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
-       NetBufferSize = 10240;
-  }
-  if (!strcmp(dbg,"debug"))
-       LogLevel = DEBUG;
-  if (!strcmp(dbg,"verbose"))
-       LogLevel = VERBOSE;
-  if (!strcmp(dbg,"default"))
-       LogLevel = DEFAULT;
-  if (!strcmp(dbg,"sparse"))
-       LogLevel = SPARSE;
-  if (!strcmp(dbg,"none"))
-       LogLevel = NONE;
-  readfile(MOTD,motd);
-  log(DEBUG,"Reading message of the day");
-  readfile(RULES,rules);
-  log(DEBUG,"Reading connect classes");
-  Classes.clear();
-  for (int i = 0; i < ConfValueEnum("connect",&config_f); i++)
-  {
-       strcpy(Value,"");
-       ConfValue("connect","allow",i,Value,&config_f);
-       ConfValue("connect","timeout",i,timeout,&config_f);
-       ConfValue("connect","flood",i,flood,&config_f);
-       if (strcmp(Value,""))
-       {
-               strcpy(c.host,Value);
-               c.type = CC_ALLOW;
+       char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF],MW[MAXBUF];
+       char AH[MAXBUF],AP[MAXBUF],AF[MAXBUF];
+       ConnectClass c;
+       
+       LoadConf(CONFIG_FILE,&config_f);
+         
+       ConfValue("server","name",0,ServerName,&config_f);
+       ConfValue("server","description",0,ServerDesc,&config_f);
+       ConfValue("server","network",0,Network,&config_f);
+       ConfValue("admin","name",0,AdminName,&config_f);
+       ConfValue("admin","email",0,AdminEmail,&config_f);
+       ConfValue("admin","nick",0,AdminNick,&config_f);
+       ConfValue("files","motd",0,motd,&config_f);
+       ConfValue("files","rules",0,rules,&config_f);
+       ConfValue("power","diepass",0,diepass,&config_f);
+       ConfValue("power","pause",0,pauseval,&config_f);
+       ConfValue("power","restartpass",0,restartpass,&config_f);
+       ConfValue("options","prefixquit",0,PrefixQuit,&config_f);
+       ConfValue("die","value",0,DieValue,&config_f);
+       ConfValue("options","loglevel",0,dbg,&config_f);
+       ConfValue("options","netbuffersize",0,NB,&config_f);
+       ConfValue("options","maxwho",0,MW,&config_f);
+       ConfValue("options","allowhalfop",0,AH,&config_f);
+       ConfValue("options","allowprotect",0,AP,&config_f);
+       ConfValue("options","allowfounder",0,AF,&config_f);
+       NetBufferSize = atoi(NB);
+       MaxWhoResults = atoi(MW);
+       AllowHalfop = ((!strcasecmp(AH,"true")) || (!strcasecmp(AH,"1")) || (!strcasecmp(AH,"yes")));
+       AllowProtect = ((!strcasecmp(AP,"true")) || (!strcasecmp(AP,"1")) || (!strcasecmp(AP,"yes")));
+       AllowFounder = ((!strcasecmp(AF,"true")) || (!strcasecmp(AF,"1")) || (!strcasecmp(AF,"yes")));
+       if ((!NetBufferSize) || (NetBufferSize > 65535) || (NetBufferSize < 1024))
+       {
+               log(DEFAULT,"No NetBufferSize specified or size out of range, setting to default of 10240.");
+               NetBufferSize = 10240;
+       }
+       if ((!MaxWhoResults) || (MaxWhoResults > 65535) || (MaxWhoResults < 1))
+       {
+               log(DEFAULT,"No MaxWhoResults specified or size out of range, setting to default of 128.");
+               MaxWhoResults = 128;
+       }
+       if (!strcmp(dbg,"debug"))
+               LogLevel = DEBUG;
+       if (!strcmp(dbg,"verbose"))
+               LogLevel = VERBOSE;
+       if (!strcmp(dbg,"default"))
+               LogLevel = DEFAULT;
+       if (!strcmp(dbg,"sparse"))
+               LogLevel = SPARSE;
+       if (!strcmp(dbg,"none"))
+               LogLevel = NONE;
+       readfile(MOTD,motd);
+       log(DEFAULT,"Reading message of the day...");
+       readfile(RULES,rules);
+       log(DEFAULT,"Reading connect classes...");
+       Classes.clear();
+       for (int i = 0; i < ConfValueEnum("connect",&config_f); i++)
+       {
                strcpy(Value,"");
-               ConfValue("connect","password",i,Value,&config_f);
-               strcpy(c.pass,Value);
-               c.registration_timeout = 90; // default is 2 minutes
-               c.flood = atoi(flood);
-               if (atoi(timeout)>0)
+               ConfValue("connect","allow",i,Value,&config_f);
+               ConfValue("connect","timeout",i,timeout,&config_f);
+               ConfValue("connect","flood",i,flood,&config_f);
+               if (strcmp(Value,""))
+               {
+                       strcpy(c.host,Value);
+                       c.type = CC_ALLOW;
+                       strcpy(Value,"");
+                       ConfValue("connect","password",i,Value,&config_f);
+                       strcpy(c.pass,Value);
+                       c.registration_timeout = 90; // default is 2 minutes
+                       c.flood = atoi(flood);
+                       if (atoi(timeout)>0)
+                       {
+                               c.registration_timeout = atoi(timeout);
+                       }
+                       Classes.push_back(c);
+                       log(DEBUG,"Read connect class type ALLOW, host=%s password=%s timeout=%d flood=%d",c.host,c.pass,c.registration_timeout,c.flood);
+               }
+               else
                {
-                       c.registration_timeout = atoi(timeout);
+                       ConfValue("connect","deny",i,Value,&config_f);
+                       strcpy(c.host,Value);
+                       c.type = CC_DENY;
+                       Classes.push_back(c);
+                       log(DEBUG,"Read connect class type DENY, host=%s",c.host);
                }
-               Classes.push_back(c);
-               log(DEBUG,"Read connect class type ALLOW, host=%s password=%s timeout=%d flood=%d",c.host,c.pass,c.registration_timeout,c.flood);
-       }
-       else
-       {
-               ConfValue("connect","deny",i,Value,&config_f);
-                strcpy(c.host,Value);
-                c.type = CC_DENY;
-               Classes.push_back(c);
-               log(DEBUG,"Read connect class type DENY, host=%s",c.host);
-       }
        
-  }
+       }
+       log(DEFAULT,"Reading K lines,Q lines and Z lines from config...");
+       read_xline_defaults();
+       log(DEFAULT,"Applying K lines, Q lines and Z lines...");
+       apply_lines();
+       log(DEFAULT,"Done reading configuration file, InspIRCd is now running.");
 }
 
 /* write formatted text to a socket, in same format as printf */
 
 void Write(int sock,char *text, ...)
 {
-  if (!text)
-  {
-       log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
-       return;
-  }
-  char textbuffer[MAXBUF];
-  va_list argsPtr;
-  char tb[MAXBUF];
-
-  va_start (argsPtr, text);
-  vsnprintf(textbuffer, MAXBUF, text, argsPtr);
-  va_end(argsPtr);
-  sprintf(tb,"%s\r\n",textbuffer);
-  chop(tb);
-  if (sock != -1)
-  {
-       write(sock,tb,strlen(tb));
-       update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
-  }
+       if (!text)
+       {
+               log(DEFAULT,"*** BUG *** Write was given an invalid parameter");
+               return;
+       }
+       char textbuffer[MAXBUF];
+       va_list argsPtr;
+       char tb[MAXBUF];
+       
+       va_start (argsPtr, text);
+       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       va_end(argsPtr);
+       sprintf(tb,"%s\r\n",textbuffer);
+       chop(tb);
+       if (sock != -1)
+       {
+               write(sock,tb,strlen(tb));
+               update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
+       }
 }
 
 /* write a server formatted numeric response to a single socket */
 
 void WriteServ(int sock, char* text, ...)
 {
-  if (!text)
-  {
-       log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
-       return;
-  }
-  char textbuffer[MAXBUF],tb[MAXBUF];
-  va_list argsPtr;
-  va_start (argsPtr, text);
-
-  vsnprintf(textbuffer, MAXBUF, text, argsPtr);
-  va_end(argsPtr);
-  sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
-  chop(tb);
-  if (sock != -1)
-  {
-       write(sock,tb,strlen(tb));
-       update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
-  }
+       if (!text)
+       {
+               log(DEFAULT,"*** BUG *** WriteServ was given an invalid parameter");
+               return;
+       }
+       char textbuffer[MAXBUF],tb[MAXBUF];
+       va_list argsPtr;
+       va_start (argsPtr, text);
+       
+       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       va_end(argsPtr);
+       sprintf(tb,":%s %s\r\n",ServerName,textbuffer);
+       chop(tb);
+       if (sock != -1)
+       {
+               write(sock,tb,strlen(tb));
+               update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
+       }
 }
 
 /* write text from an originating user to originating user */
 
 void WriteFrom(int sock, userrec *user,char* text, ...)
 {
-  if ((!text) || (!user))
-  {
-       log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
-       return;
-  }
-  char textbuffer[MAXBUF],tb[MAXBUF];
-  va_list argsPtr;
-  va_start (argsPtr, text);
-
-  vsnprintf(textbuffer, MAXBUF, text, argsPtr);
-  va_end(argsPtr);
-  sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
-  chop(tb);
-  if (sock != -1)
-  {
-       write(sock,tb,strlen(tb));
-       update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
-  }
+       if ((!text) || (!user))
+       {
+               log(DEFAULT,"*** BUG *** WriteFrom was given an invalid parameter");
+               return;
+       }
+       char textbuffer[MAXBUF],tb[MAXBUF];
+       va_list argsPtr;
+       va_start (argsPtr, text);
+       
+       vsnprintf(textbuffer, MAXBUF, text, argsPtr);
+       va_end(argsPtr);
+       sprintf(tb,":%s!%s@%s %s\r\n",user->nick,user->ident,user->dhost,textbuffer);
+       chop(tb);
+       if (sock != -1)
+       {
+               write(sock,tb,strlen(tb));
+               update_stats_l(sock,strlen(tb)); /* add one line-out to stats L for this fd */
+       }
 }
 
 /* write text to an destination user from a source user (e.g. user privmsg) */
@@ -1108,6 +1133,7 @@ char* chanmodes(chanrec *chan)
        }
        if (strcmp(chan->key,""))
        {
+               strncat(sparam," ",MAXBUF);
                strncat(sparam,chan->key,MAXBUF);
        }
        if (chan->limit)
@@ -1284,8 +1310,9 @@ chanrec* add_channel(userrec *user, const char* cn, const char* key, bool overri
 
        if (!FindChan(cname))
        {
+               int MOD_RESULT = 0;
                FOREACH_RESULT(OnUserPreJoin(user,NULL,cname));
-               if (MOD_RESULT) {
+               if (MOD_RESULT == 1) {
                        return NULL;
                }
 
@@ -1321,77 +1348,83 @@ chanrec* add_channel(userrec *user, const char* cn, const char* key, bool overri
                        // and bans (used by servers)
                        if (!override)
                        {
+                               int MOD_RESULT = 0;
                                FOREACH_RESULT(OnUserPreJoin(user,Ptr,cname));
-                               if (MOD_RESULT) {
+                               if (MOD_RESULT == 1) {
                                        return NULL;
                                }
                                
-                               if (strcmp(Ptr->key,""))
+                               if (MOD_RESULT == 0) 
                                {
-                                       log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
-                                       if (!key)
-                                       {
-                                               log(DEBUG,"add_channel: no key given in JOIN");
-                                               WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
-                                               return NULL;
-                                       }
-                                       else
+                                       
+                                       if (strcmp(Ptr->key,""))
                                        {
-                                               log(DEBUG,"key at %p is %s",key,key);
-                                               if (strcasecmp(key,Ptr->key))
+                                               log(DEBUG,"add_channel: %s has key %s",Ptr->name,Ptr->key);
+                                               if (!key)
                                                {
-                                                       log(DEBUG,"add_channel: bad key given in JOIN");
-                                                       WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
+                                                       log(DEBUG,"add_channel: no key given in JOIN");
+                                                       WriteServ(user->fd,"475 %s %s :Cannot join channel (Requires key)",user->nick, Ptr->name);
                                                        return NULL;
                                                }
+                                               else
+                                               {
+                                                       log(DEBUG,"key at %p is %s",key,key);
+                                                       if (strcasecmp(key,Ptr->key))
+                                                       {
+                                                               log(DEBUG,"add_channel: bad key given in JOIN");
+                                                               WriteServ(user->fd,"475 %s %s :Cannot join channel (Incorrect key)",user->nick, Ptr->name);
+                                                               return NULL;
+                                                       }
+                                               }
                                        }
-                               }
-                               log(DEBUG,"add_channel: no key");
-       
-                               if (Ptr->inviteonly)
-                               {
-                                       log(DEBUG,"add_channel: channel is +i");
-                                       if (user->IsInvited(Ptr->name))
-                                       {
-                                               /* user was invited to channel */
-                                               /* there may be an optional channel NOTICE here */
-                                       }
-                                       else
+                                       log(DEBUG,"add_channel: no key");
+               
+                                       if (Ptr->inviteonly)
                                        {
-                                               WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
-                                               return NULL;
+                                               log(DEBUG,"add_channel: channel is +i");
+                                               if (user->IsInvited(Ptr->name))
+                                               {
+                                                       /* user was invited to channel */
+                                                       /* there may be an optional channel NOTICE here */
+                                               }
+                                               else
+                                               {
+                                                       WriteServ(user->fd,"473 %s %s :Cannot join channel (Invite only)",user->nick, Ptr->name);
+                                                       return NULL;
+                                               }
                                        }
-                               }
-                               log(DEBUG,"add_channel: channel is not +i");
-       
-                               if (Ptr->limit)
-                               {
-                                       if (usercount(Ptr) == Ptr->limit)
+                                       log(DEBUG,"add_channel: channel is not +i");
+               
+                                       if (Ptr->limit)
                                        {
-                                               WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
-                                               return NULL;
+                                               if (usercount(Ptr) == Ptr->limit)
+                                               {
+                                                       WriteServ(user->fd,"471 %s %s :Cannot join channel (Channel is full)",user->nick, Ptr->name);
+                                                       return NULL;
+                                               }
                                        }
-                               }
-                               
-                               log(DEBUG,"add_channel: about to walk banlist");
-       
-                               /* check user against the channel banlist */
-                               if (Ptr)
-                               {
-                                       if (Ptr->bans.size())
+                                       
+                                       log(DEBUG,"add_channel: about to walk banlist");
+               
+                                       /* check user against the channel banlist */
+                                       if (Ptr)
                                        {
-                                               for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
+                                               if (Ptr->bans.size())
                                                {
-                                                       if (match(user->GetFullHost(),i->data))
+                                                       for (BanList::iterator i = Ptr->bans.begin(); i != Ptr->bans.end(); i++)
                                                        {
-                                                               WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
-                                                               return NULL;
+                                                               if (match(user->GetFullHost(),i->data))
+                                                               {
+                                                                       WriteServ(user->fd,"474 %s %s :Cannot join channel (You're banned)",user->nick, Ptr->name);
+                                                                       return NULL;
+                                                               }
                                                        }
                                                }
                                        }
-                               }
+                                       
+                                       log(DEBUG,"add_channel: bans checked");
                                
-                               log(DEBUG,"add_channel: bans checked");
+                               }
                                
 
                                if ((Ptr) && (user))
@@ -1437,7 +1470,14 @@ chanrec* add_channel(userrec *user, const char* cn, const char* key, bool overri
                        {
                                // use the stamdard J token with no privilages.
                                char buffer[MAXBUF];
-                               snprintf(buffer,MAXBUF,"J %s %s",user->nick,Ptr->name);
+                               if (created == 2)
+                               {
+                                       snprintf(buffer,MAXBUF,"J %s @%s",user->nick,Ptr->name);
+                               }
+                               else
+                               {
+                                       snprintf(buffer,MAXBUF,"J %s %s",user->nick,Ptr->name);
+                               }
                                NetSendToAll(buffer);
                        }
 
@@ -1567,18 +1607,28 @@ void kick_channel(userrec *src,userrec *user, chanrec *Ptr, char* reason)
                WriteServ(src->fd,"441 %s %s %s :They are not on that channel",src->nick, user->nick, Ptr->name);
                return;
        }
-       if (((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr))) && (!is_uline(src->server)))
+
+       int MOD_RESULT = 0;
+       FOREACH_RESULT(OnAccessCheck(src,user,Ptr,AC_KICK));
+       
+       if (MOD_RESULT == ACR_DENY)
+               return;
+
+       if (MOD_RESULT == ACR_DEFAULT)
        {
-               if (cstatus(src,Ptr) == STATUS_HOP)
-               {
-                       WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
-               }
-               else
+               if (((cstatus(src,Ptr) < STATUS_HOP) || (cstatus(src,Ptr) < cstatus(user,Ptr))) && (!is_uline(src->server)))
                {
-                       WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
+                       if (cstatus(src,Ptr) == STATUS_HOP)
+                       {
+                               WriteServ(src->fd,"482 %s %s :You must be a channel operator",src->nick, Ptr->name);
+                       }
+                       else
+                       {
+                               WriteServ(src->fd,"482 %s %s :You must be at least a half-operator to change modes on this channel",src->nick, Ptr->name);
+                       }
+                       
+                       return;
                }
-               
-               return;
        }
        
        for (int i =0; i != MAXCHANS; i++)
@@ -1834,6 +1884,56 @@ void kill_link(userrec *user,const char* r)
        }
 }
 
+void kill_link_silent(userrec *user,const char* r)
+{
+       user_hash::iterator iter = clientlist.find(user->nick);
+       
+       char reason[MAXBUF];
+       
+       strncpy(reason,r,MAXBUF);
+
+       if (strlen(reason)>MAXQUIT)
+       {
+               reason[MAXQUIT-1] = '\0';
+       }
+
+       log(DEBUG,"kill_link: %s '%s'",user->nick,reason);
+       Write(user->fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,reason);
+       log(DEBUG,"closing fd %d",user->fd);
+
+       /* bugfix, cant close() a nonblocking socket (sux!) */
+       if (user->registered == 7) {
+               FOREACH_MOD OnUserQuit(user);
+               WriteCommonExcept(user,"QUIT :%s",reason);
+
+               // Q token must go to ALL servers!!!
+               char buffer[MAXBUF];
+               snprintf(buffer,MAXBUF,"Q %s :%s",user->nick,reason);
+               NetSendToAll(buffer);
+       }
+
+       /* push the socket on a stack of sockets due to be closed at the next opportunity
+        * 'Client exited' is an exception to this as it means the client side has already
+        * closed the socket, we don't need to do it.
+        */
+       fd_reap.push_back(user->fd);
+       
+       bool do_purge = false;
+       
+       if (iter != clientlist.end())
+       {
+               log(DEBUG,"deleting user hash value %d",iter->second);
+               if ((iter->second) && (user->registered == 7)) {
+                       delete iter->second;
+               }
+               clientlist.erase(iter);
+       }
+
+       if (user->registered == 7) {
+               purge_empty_chans();
+       }
+}
+
 
 
 // looks up a users password for their connection class (<ALLOW>/<DENY> tags)
@@ -2014,7 +2114,7 @@ void AddWhoWas(userrec* u)
 
 
 /* add a client connection to the sockets list */
-void AddClient(int socket, char* host, int port, bool iscached)
+void AddClient(int socket, char* host, int port, bool iscached, char* ip)
 {
        int i;
        int blocking = 1;
@@ -2040,7 +2140,7 @@ void AddClient(int socket, char* host, int port, bool iscached)
        clientlist[tempnick] = new userrec();
 
        NonBlocking(socket);
-       log(DEBUG,"AddClient: %d %s %d",socket,host,port);
+       log(DEBUG,"AddClient: %d %s %d %s",socket,host,port,ip);
 
        clientlist[tempnick]->fd = socket;
        strncpy(clientlist[tempnick]->nick, tn2,NICKMAX);
@@ -2053,6 +2153,7 @@ void AddClient(int socket, char* host, int port, bool iscached)
        clientlist[tempnick]->nping = time(NULL)+240;
        clientlist[tempnick]->lastping = 1;
        clientlist[tempnick]->port = port;
+       strncpy(clientlist[tempnick]->ip,ip,32);
 
        if (iscached)
        {
@@ -2095,6 +2196,14 @@ void AddClient(int socket, char* host, int port, bool iscached)
 
        if (clientlist.size() == MAXCLIENTS)
                kill_link(clientlist[tempnick],"No more connections allowed in this class");
+               
+       char* r = matches_zline(ip);
+       if (r)
+       {
+               char reason[MAXBUF];
+               snprintf(reason,MAXBUF,"Z-Lined: %s",r);
+               kill_link(clientlist[tempnick],reason);
+       }
 }
 
 
@@ -2218,6 +2327,26 @@ void ConnectUser(userrec *user)
                return;
        }
 
+       char match_against[MAXBUF];
+       snprintf(match_against,MAXBUF,"%s@%s",user->ident,user->host);
+       char* r = matches_gline(match_against);
+       if (r)
+       {
+               char reason[MAXBUF];
+               snprintf(reason,MAXBUF,"G-Lined: %s",r);
+               kill_link_silent(user,reason);
+               return;
+       }
+
+       r = matches_kline(user->host);
+       if (r)
+       {
+               char reason[MAXBUF];
+               snprintf(reason,MAXBUF,"K-Lined: %s",r);
+               kill_link_silent(user,reason);
+               return;
+       }
+
        WriteServ(user->fd,"NOTICE Auth :Welcome to \002%s\002!",Network);
        WriteServ(user->fd,"001 %s :Welcome to the %s IRC Network %s!%s@%s",user->nick,Network,user->nick,user->ident,user->host);
        WriteServ(user->fd,"002 %s :Your host is %s, running version %s",user->nick,ServerName,VERSION);
@@ -2230,7 +2359,7 @@ void ConnectUser(userrec *user)
        WriteOpers("*** Client connecting on port %d: %s!%s@%s",user->port,user->nick,user->ident,user->host);
        
        char buffer[MAXBUF];
-       snprintf(buffer,MAXBUF,"N %d %s %s %s %s +%s %s :%s",user->age,user->nick,user->host,user->dhost,user->ident,user->modes,ServerName,user->fullname);
+       snprintf(buffer,MAXBUF,"N %d %s %s %s %s +%s %s %s :%s",user->age,user->nick,user->host,user->dhost,user->ident,user->modes,user->ip,ServerName,user->fullname);
        NetSendToAll(buffer);
 }
 
@@ -2365,6 +2494,11 @@ void force_nickchange(userrec* user,const char* newnick)
                kill_link(user,"Nickname collision");
                return;
        }
+       if (matches_qline(newnick))
+       {
+               kill_link(user,"Nickname collision");
+               return;
+       }
        
        if (user)
        {
@@ -2475,7 +2609,8 @@ void process_command(userrec *user, char* cmd)
        // another phidjit bug...
        if (total_params > 126)
        {
-               kill_link(user,"Protocol violation (1)");
+               //kill_link(user,"Protocol violation (1)");
+               WriteServ(user->fd,"421 %s * :Unknown command",user->nick);
                return;
        }
        
@@ -2485,7 +2620,7 @@ void process_command(userrec *user, char* cmd)
        for (int i = 0; i <= MODCOUNT; i++)
        {
                std::string oldtmp = tmp;
-               modules[i]->OnServerRaw(tmp,true);
+               modules[i]->OnServerRaw(tmp,true,user);
                if (oldtmp != tmp)
                {
                        log(DEBUG,"A Module changed the input string!");
@@ -2558,7 +2693,8 @@ void process_command(userrec *user, char* cmd)
        
        if (strlen(command)>MAXCOMMAND)
        {
-               kill_link(user,"Protocol violation (2)");
+               //kill_link(user,"Protocol violation (2)");
+               WriteServ(user->fd,"421 %s * :Unknown command",user->nick);
                return;
        }
        
@@ -2568,9 +2704,10 @@ void process_command(userrec *user, char* cmd)
                {
                        if (((command[x] < '0') || (command[x]> '9')) && (command[x] != '-'))
                        {
-                               if (!strchr("@!\"$%^&*(){}[]_-=+;:'#~,.<>/?\\|`",command[x]))
+                               if (strchr("@!\"$%^&*(){}[]_=+;:'#~,<>/?\\|`",command[x]))
                                {
-                                       kill_link(user,"Protocol violation (3)");
+                                       //kill_link(user,"Protocol violation (3)");
+                                       WriteServ(user->fd,"421 %s * :Unknown command",user->nick);
                                        return;
                                }
                        }
@@ -2622,6 +2759,13 @@ void process_command(userrec *user, char* cmd)
                                                cmd_found = 1;
                                                return;
                                        }
+                                       if ((cmdlist[i].flags_needed) && (!user->HasPermission(command)))
+                                       {
+                                               log(DEBUG,"process_command: permission denied: %s %s",user->nick,command);
+                                               WriteServ(user->fd,"481 %s :Permission Denied- Oper type %s does not have access to command %s",user->nick,user->oper,command);
+                                               cmd_found = 1;
+                                               return;
+                                       }
                                        /* if the command isnt USER, PASS, or NICK, and nick is empty,
                                         * deny command! */
                                        if ((strncmp(command,"USER",4)) && (strncmp(command,"NICK",4)) && (strncmp(command,"PASS",4)))
@@ -2737,6 +2881,11 @@ void SetupCommandTable(void)
        createcommand("MODULES",handle_modules,'o',0);
        createcommand("LINKS",handle_links,0,0);
        createcommand("MAP",handle_map,0,0);
+       createcommand("KLINE",handle_kline,'o',1);
+       createcommand("GLINE",handle_gline,'o',1);
+       createcommand("ZLINE",handle_zline,'o',1);
+       createcommand("QLINE",handle_qline,'o',1);
+       createcommand("SERVER",handle_server,0,0);
 }
 
 void process_buffer(const char* cmdbuf,userrec *user)
@@ -2802,8 +2951,17 @@ void DoSync(serverrec* serv, char* tcp_host)
        // send users and channels
        for (user_hash::iterator u = clientlist.begin(); u != clientlist.end(); u++)
        {
-               snprintf(data,MAXBUF,"N %d %s %s %s %s +%s %s :%s",u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,u->second->server,u->second->fullname);
+               snprintf(data,MAXBUF,"N %d %s %s %s %s +%s %s %s :%s",u->second->age,u->second->nick,u->second->host,u->second->dhost,u->second->ident,u->second->modes,u->second->ip,u->second->server,u->second->fullname);
                serv->SendPacket(data,tcp_host);
+               for (int i = 0; i <= MODCOUNT; i++)
+               {
+                       string_list l = modules[i]->OnUserSync(u->second);
+                       for (int j = 0; j < l.size(); j++)
+                       {
+                               strncpy(data,l[j].c_str(),MAXBUF);
+                               serv->SendPacket(data,tcp_host);
+                       }
+               }
                if (strcmp(chlist(u->second),""))
                {
                        snprintf(data,MAXBUF,"J %s %s",u->second->nick,chlist(u->second));
@@ -2815,6 +2973,15 @@ void DoSync(serverrec* serv, char* tcp_host)
        {
                snprintf(data,MAXBUF,"M %s +%s",c->second->name,chanmodes(c->second));
                serv->SendPacket(data,tcp_host);
+               for (int i = 0; i <= MODCOUNT; i++)
+               {
+                       string_list l = modules[i]->OnChannelSync(c->second);
+                       for (int j = 0; j < l.size(); j++)
+                       {
+                               strncpy(data,l[j].c_str(),MAXBUF);
+                               serv->SendPacket(data,tcp_host);
+                       }
+               }
                if (strcmp(c->second->topic,""))
                {
                        snprintf(data,MAXBUF,"T %d %s %s :%s",c->second->topicset,c->second->setby,c->second->name,c->second->topic);
@@ -2828,6 +2995,8 @@ void DoSync(serverrec* serv, char* tcp_host)
                        serv->SendPacket(data,tcp_host);
                }
        }
+       // sync global zlines, glines, etc
+       sync_xlines(serv,tcp_host);
        snprintf(data,MAXBUF,"F %d",time(NULL));
        serv->SendPacket(data,tcp_host);
        log(DEBUG,"Sent sync");
@@ -3126,6 +3295,8 @@ int InspIRCd(void)
 #ifdef _POSIX_PRIORITY_SCHEDULING
                sched_yield();
 #endif
+               // update the status of klines, etc
+               expire_lines();
 
                fd_set sfd;
                timeval tval;
@@ -3447,7 +3618,7 @@ int InspIRCd(void)
                                }
                                else
                                {
-                                       AddClient(incomingSockfd, resolved, ports[count], iscached);
+                                       AddClient(incomingSockfd, resolved, ports[count], iscached, inet_ntoa (client.sin_addr));
                                        log(DEBUG,"InspIRCd: adding client on port %d fd=%d",ports[count],incomingSockfd);
                                }
                                goto label;