]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/inspircd.cpp
Changed parameters of OnPacketTransmit and OnPacketReceive to be much more useful
[user/henk/code/inspircd.git] / src / inspircd.cpp
index b74c798c4291e727c12f622d2ea93426517c48b3..d69aa6a1e1e332818a396e9e3a7acb7a45a29f9e 100644 (file)
@@ -89,6 +89,8 @@ extern int MaxWhoResults;
 time_t nb_start = 0;
 int dns_timeout = 5;
 
+char DisabledCommands[MAXBUF];
+
 bool AllowHalfop = true;
 bool AllowProtect = true;
 bool AllowFounder = true;
@@ -191,6 +193,7 @@ char MyExecutable[1024];
 int boundPortCount = 0;
 int portCount = 0, UDPportCount = 0, ports[MAXSOCKS];
 int defaultRoute = 0;
+char ModPath[MAXBUF];
 
 connection C;
 
@@ -317,7 +320,7 @@ void readfile(file_cache &F, const char* fname)
 void ReadConfig(bool bail, userrec* user)
 {
        char dbg[MAXBUF],pauseval[MAXBUF],Value[MAXBUF],timeout[MAXBUF],NB[MAXBUF],flood[MAXBUF],MW[MAXBUF];
-       char AH[MAXBUF],AP[MAXBUF],AF[MAXBUF],DNT[MAXBUF];
+       char AH[MAXBUF],AP[MAXBUF],AF[MAXBUF],DNT[MAXBUF],pfreq[MAXBUF];
        ConnectClass c;
        std::stringstream errstr;
        
@@ -375,6 +378,9 @@ void ReadConfig(bool bail, userrec* user)
        ConfValue("options","allowfounder",0,AF,&config_f);
        ConfValue("dns","server",0,DNSServer,&config_f);
        ConfValue("dns","timeout",0,DNT,&config_f);
+       ConfValue("options","moduledir",0,ModPath,&config_f);
+        ConfValue("disabled","commands",0,DisabledCommands,&config_f);
+
        NetBufferSize = atoi(NB);
        MaxWhoResults = atoi(MW);
        dns_timeout = atoi(DNT);
@@ -382,6 +388,8 @@ void ReadConfig(bool bail, userrec* user)
                dns_timeout = 5;
        if (!strcmp(DNSServer,""))
                strlcpy(DNSServer,"127.0.0.1",MAXBUF);
+       if (!strcmp(ModPath,""))
+               strlcpy(ModPath,MOD_PATH,MAXBUF);
        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")));
@@ -416,6 +424,7 @@ void ReadConfig(bool bail, userrec* user)
                ConfValue("connect","allow",i,Value,&config_f);
                ConfValue("connect","timeout",i,timeout,&config_f);
                ConfValue("connect","flood",i,flood,&config_f);
+               ConfValue("connect","pingfreq",i,pfreq,&config_f);
                if (strcmp(Value,""))
                {
                        strlcpy(c.host,Value,MAXBUF);
@@ -424,11 +433,16 @@ void ReadConfig(bool bail, userrec* user)
                        ConfValue("connect","password",i,Value,&config_f);
                        strlcpy(c.pass,Value,MAXBUF);
                        c.registration_timeout = 90; // default is 2 minutes
+                       c.pingtime = 120;
                        c.flood = atoi(flood);
                        if (atoi(timeout)>0)
                        {
                                c.registration_timeout = atoi(timeout);
                        }
+                       if (atoi(pfreq)>0)
+                       {
+                               c.pingtime = atoi(pfreq);
+                       }
                        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);
                }
@@ -447,6 +461,82 @@ void ReadConfig(bool bail, userrec* user)
        log(DEFAULT,"Applying K lines, Q lines and Z lines...");
        apply_lines();
        log(DEFAULT,"Done reading configuration file, InspIRCd is now running.");
+       if (!bail)
+       {
+               log(DEFAULT,"Adding and removing modules due to rehash...");
+
+               std::vector<std::string> old_module_names, new_module_names, added_modules, removed_modules;
+
+               // store the old module names
+               for (std::vector<std::string>::iterator t = module_names.begin(); t != module_names.end(); t++)
+               {
+                       old_module_names.push_back(*t);
+               }
+
+               // get the new module names
+               for (int count2 = 0; count2 < ConfValueEnum("module",&config_f); count2++)
+               {
+                       ConfValue("module","name",count2,Value,&config_f);
+                       new_module_names.push_back(Value);
+               }
+
+               // now create a list of new modules that are due to be loaded
+               // and a seperate list of modules which are due to be unloaded
+               for (std::vector<std::string>::iterator _new = new_module_names.begin(); _new != new_module_names.end(); _new++)
+               {
+                       bool added = true;
+                       for (std::vector<std::string>::iterator old = old_module_names.begin(); old != old_module_names.end(); old++)
+                       {
+                               if (*old == *_new)
+                                       added = false;
+                       }
+                       if (added)
+                               added_modules.push_back(*_new);
+               }
+               for (std::vector<std::string>::iterator oldm = old_module_names.begin(); oldm != old_module_names.end(); oldm++)
+               {
+                       bool removed = true;
+                       for (std::vector<std::string>::iterator newm = new_module_names.begin(); newm != new_module_names.end(); newm++)
+                       {
+                               if (*newm == *oldm)
+                                       removed = false;
+                       }
+                       if (removed)
+                               removed_modules.push_back(*oldm);
+               }
+               // now we have added_modules, a vector of modules to be loaded, and removed_modules, a vector of modules
+               // to be removed.
+               int rem = 0, add = 0;
+               if (!removed_modules.empty())
+               for (std::vector<std::string>::iterator removing = removed_modules.begin(); removing != removed_modules.end(); removing++)
+               {
+                       if (UnloadModule(removing->c_str()))
+                       {
+                               WriteOpers("*** REHASH UNLOADED MODULE: %s",removing->c_str());
+                               WriteServ(user->fd,"973 %s %s :Module %s successfully unloaded.",user->nick, removing->c_str(), removing->c_str());
+                               rem++;
+                       }
+                       else
+                       {
+                               WriteServ(user->fd,"972 %s %s :Failed to unload module %s: %s",user->nick, removing->c_str(), removing->c_str(), ModuleError());
+                       }
+               }
+               if (!added_modules.empty())
+               for (std::vector<std::string>::iterator adding = added_modules.begin(); adding != added_modules.end(); adding++)
+               {
+                       if (LoadModule(adding->c_str()))
+                       {
+                               WriteOpers("*** REHASH LOADED MODULE: %s",adding->c_str());
+                               WriteServ(user->fd,"975 %s %s :Module %s successfully loaded.",user->nick, adding->c_str(), adding->c_str());
+                               add++;
+                       }
+                       else
+                       {
+                               WriteServ(user->fd,"974 %s %s :Failed to load module %s: %s",user->nick, adding->c_str(), adding->c_str(), ModuleError());
+                       }
+               }
+               log(DEFAULT,"Successfully unloaded %d of %d modules and loaded %d of %d modules.",rem,removed_modules.size(),add,added_modules.size());
+       }
 }
 
 /* write formatted text to a socket, in same format as printf */
@@ -825,6 +915,10 @@ void NetSendToCommon(userrec* u, char* s)
        
        log(DEBUG,"NetSendToCommon: '%s' '%s'",u->nick,s);
 
+        std::string msg = buffer;
+        FOREACH_MOD OnPacketTransmit(msg,s);
+        strlcpy(buffer,msg.c_str(),MAXBUF);
+
        for (int j = 0; j < 32; j++)
        {
                if (me[j] != NULL)
@@ -848,6 +942,10 @@ void NetSendToAll(char* s)
        
        log(DEBUG,"NetSendToAll: '%s'",s);
 
+        std::string msg = buffer;
+        FOREACH_MOD OnPacketTransmit(msg,s);
+        strlcpy(buffer,msg.c_str(),MAXBUF);
+
        for (int j = 0; j < 32; j++)
        {
                if (me[j] != NULL)
@@ -867,6 +965,10 @@ void NetSendToAllAlive(char* s)
        
        log(DEBUG,"NetSendToAllAlive: '%s'",s);
 
+        std::string msg = buffer;
+        FOREACH_MOD OnPacketTransmit(msg,s);
+        strlcpy(buffer,msg.c_str(),MAXBUF);
+
        for (int j = 0; j < 32; j++)
        {
                if (me[j] != NULL)
@@ -894,6 +996,10 @@ void NetSendToOne(char* target,char* s)
        
        log(DEBUG,"NetSendToOne: '%s' '%s'",target,s);
 
+        std::string msg = buffer;
+        FOREACH_MOD OnPacketTransmit(msg,s);
+        strlcpy(buffer,msg.c_str(),MAXBUF);
+
        for (int j = 0; j < 32; j++)
        {
                if (me[j] != NULL)
@@ -916,6 +1022,10 @@ void NetSendToAllExcept(const char* target,char* s)
        
        log(DEBUG,"NetSendToAllExcept: '%s' '%s'",target,s);
        
+        std::string msg = buffer;
+        FOREACH_MOD OnPacketTransmit(msg,s);
+        strlcpy(buffer,msg.c_str(),MAXBUF);
+
        for (int j = 0; j < 32; j++)
        {
                if (me[j] != NULL)
@@ -2207,7 +2317,6 @@ void AddClient(int socket, char* host, int port, bool iscached, char* ip)
        strncpy(clientlist[tempnick]->ident, "unknown",9);
        clientlist[tempnick]->registered = 0;
        clientlist[tempnick]->signon = TIME+dns_timeout;
-       clientlist[tempnick]->nping = TIME+240+dns_timeout;
        clientlist[tempnick]->lastping = 1;
        clientlist[tempnick]->port = port;
        strncpy(clientlist[tempnick]->ip,ip,32);
@@ -2222,10 +2331,12 @@ void AddClient(int socket, char* host, int port, bool iscached, char* ip)
                {
                        class_regtimeout = (unsigned long)i->registration_timeout;
                        class_flood = i->flood;
+                       clientlist[tempnick]->pingmax = i->pingtime;
                        break;
                }
        }
 
+       clientlist[tempnick]->nping = TIME+clientlist[tempnick]->pingmax+dns_timeout;
        clientlist[tempnick]->timeout = TIME+class_regtimeout;
        clientlist[tempnick]->flood = class_flood;
 
@@ -2842,7 +2953,7 @@ void process_command(userrec *user, char* cmd)
                                        log(DEBUG,"Processing command");
                                        
                                        /* activity resets the ping pending timer */
-                                       user->nping = TIME + 120;
+                                       user->nping = TIME + user->pingmax;
                                        if ((items) < cmdlist[i].min_params)
                                        {
                                                log(DEBUG,"process_command: not enough parameters: %s %s",user->nick,command);
@@ -2873,6 +2984,24 @@ void process_command(userrec *user, char* cmd)
                                                        WriteServ(user->fd,"451 %s :You have not registered",command);
                                                        return;
                                                }
+                                       }
+                                       if ((user->registered == 7) && (!strchr(user->modes,'o')))
+                                       {
+                                               char* mycmd;
+                                               char* savept2;
+                                               mycmd = strtok_r(DisabledCommands," ",&savept2);
+                                               while (mycmd)
+                                               {
+                                                       if (!strcasecmp(mycmd,command))
+                                                       {
+                                                               // command is disabled!
+                                                               WriteServ(user->fd,"421 %s %s :This command has been disabled.",user->nick,command);
+                                                               return;
+                                                       }
+                                                       mycmd = strtok_r(NULL," ",&savept2);
+                                               }
+        
+
                                        }
                                        if ((user->registered == 7) || (!strcmp(command,"USER")) || (!strcmp(command,"NICK")) || (!strcmp(command,"PASS")))
                                        {
@@ -2920,11 +3049,12 @@ void process_command(userrec *user, char* cmd)
 }
 
 
-void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
+void createcommand(char* cmd, handlerfunc f, char flags, int minparams,char* source)
 {
        command_t comm;
        /* create the command and push it onto the table */     
        strlcpy(comm.command,cmd,MAXBUF);
+       strlcpy(comm.source,source,MAXBUF);
        comm.handler_function = f;
        comm.flags_needed = flags;
        comm.min_params = minparams;
@@ -2934,58 +3064,80 @@ void createcommand(char* cmd, handlerfunc f, char flags, int minparams)
        log(DEBUG,"Added command %s (%d parameters)",cmd,minparams);
 }
 
+bool removecommands(const char* source)
+{
+       bool go_again = true;
+       while (go_again)
+       {
+               go_again = false;
+               for (std::deque<command_t>::iterator i = cmdlist.begin(); i != cmdlist.end(); i++)
+               {
+                       if (!strcmp(i->source,source))
+                       {
+                               log(DEBUG,"removecommands(%s) Removing dependent command: %s",i->source,i->command);
+                               cmdlist.erase(i);
+                               go_again = true;
+                               break;
+                       }
+               }
+       }
+       return true;
+}
+
 void SetupCommandTable(void)
 {
-       createcommand("USER",handle_user,0,4);
-       createcommand("NICK",handle_nick,0,1);
-       createcommand("QUIT",handle_quit,0,0);
-       createcommand("VERSION",handle_version,0,0);
-       createcommand("PING",handle_ping,0,1);
-       createcommand("PONG",handle_pong,0,1);
-       createcommand("ADMIN",handle_admin,0,0);
-       createcommand("PRIVMSG",handle_privmsg,0,2);
-       createcommand("INFO",handle_info,0,0);
-       createcommand("TIME",handle_time,0,0);
-       createcommand("WHOIS",handle_whois,0,1);
-       createcommand("WALLOPS",handle_wallops,'o',1);
-       createcommand("NOTICE",handle_notice,0,2);
-       createcommand("JOIN",handle_join,0,1);
-       createcommand("NAMES",handle_names,0,1);
-       createcommand("PART",handle_part,0,1);
-       createcommand("KICK",handle_kick,0,2);
-       createcommand("MODE",handle_mode,0,1);
-       createcommand("TOPIC",handle_topic,0,1);
-       createcommand("WHO",handle_who,0,1);
-       createcommand("MOTD",handle_motd,0,0);
-       createcommand("RULES",handle_rules,0,0);
-       createcommand("OPER",handle_oper,0,2);
-       createcommand("LIST",handle_list,0,0);
-       createcommand("DIE",handle_die,'o',1);
-       createcommand("RESTART",handle_restart,'o',1);
-       createcommand("KILL",handle_kill,'o',2);
-       createcommand("REHASH",handle_rehash,'o',0);
-       createcommand("LUSERS",handle_lusers,0,0);
-       createcommand("STATS",handle_stats,0,1);
-       createcommand("USERHOST",handle_userhost,0,1);
-       createcommand("AWAY",handle_away,0,0);
-       createcommand("ISON",handle_ison,0,0);
-       createcommand("SUMMON",handle_summon,0,0);
-       createcommand("USERS",handle_users,0,0);
-       createcommand("INVITE",handle_invite,0,2);
-       createcommand("PASS",handle_pass,0,1);
-       createcommand("TRACE",handle_trace,'o',0);
-       createcommand("WHOWAS",handle_whowas,0,1);
-       createcommand("CONNECT",handle_connect,'o',1);
-       createcommand("SQUIT",handle_squit,'o',0);
-       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("ELINE",handle_eline,'o',1);
-       createcommand("SERVER",handle_server,0,0);
+       createcommand("USER",handle_user,0,4,"<core>");
+       createcommand("NICK",handle_nick,0,1,"<core>");
+       createcommand("QUIT",handle_quit,0,0,"<core>");
+       createcommand("VERSION",handle_version,0,0,"<core>");
+       createcommand("PING",handle_ping,0,1,"<core>");
+       createcommand("PONG",handle_pong,0,1,"<core>");
+       createcommand("ADMIN",handle_admin,0,0,"<core>");
+       createcommand("PRIVMSG",handle_privmsg,0,2,"<core>");
+       createcommand("INFO",handle_info,0,0,"<core>");
+       createcommand("TIME",handle_time,0,0,"<core>");
+       createcommand("WHOIS",handle_whois,0,1,"<core>");
+       createcommand("WALLOPS",handle_wallops,'o',1,"<core>");
+       createcommand("NOTICE",handle_notice,0,2,"<core>");
+       createcommand("JOIN",handle_join,0,1,"<core>");
+       createcommand("NAMES",handle_names,0,0,"<core>");
+       createcommand("PART",handle_part,0,1,"<core>");
+       createcommand("KICK",handle_kick,0,2,"<core>");
+       createcommand("MODE",handle_mode,0,1,"<core>");
+       createcommand("TOPIC",handle_topic,0,1,"<core>");
+       createcommand("WHO",handle_who,0,1,"<core>");
+       createcommand("MOTD",handle_motd,0,0,"<core>");
+       createcommand("RULES",handle_rules,0,0,"<core>");
+       createcommand("OPER",handle_oper,0,2,"<core>");
+       createcommand("LIST",handle_list,0,0,"<core>");
+       createcommand("DIE",handle_die,'o',1,"<core>");
+       createcommand("RESTART",handle_restart,'o',1,"<core>");
+       createcommand("KILL",handle_kill,'o',2,"<core>");
+       createcommand("REHASH",handle_rehash,'o',0,"<core>");
+       createcommand("LUSERS",handle_lusers,0,0,"<core>");
+       createcommand("STATS",handle_stats,0,1,"<core>");
+       createcommand("USERHOST",handle_userhost,0,1,"<core>");
+       createcommand("AWAY",handle_away,0,0,"<core>");
+       createcommand("ISON",handle_ison,0,0,"<core>");
+       createcommand("SUMMON",handle_summon,0,0,"<core>");
+       createcommand("USERS",handle_users,0,0,"<core>");
+       createcommand("INVITE",handle_invite,0,2,"<core>");
+       createcommand("PASS",handle_pass,0,1,"<core>");
+       createcommand("TRACE",handle_trace,'o',0,"<core>");
+       createcommand("WHOWAS",handle_whowas,0,1,"<core>");
+       createcommand("CONNECT",handle_connect,'o',1,"<core>");
+       createcommand("SQUIT",handle_squit,'o',0,"<core>");
+       createcommand("MODULES",handle_modules,0,0,"<core>");
+       createcommand("LINKS",handle_links,0,0,"<core>");
+       createcommand("MAP",handle_map,0,0,"<core>");
+       createcommand("KLINE",handle_kline,'o',1,"<core>");
+       createcommand("GLINE",handle_gline,'o',1,"<core>");
+       createcommand("ZLINE",handle_zline,'o',1,"<core>");
+       createcommand("QLINE",handle_qline,'o',1,"<core>");
+       createcommand("ELINE",handle_eline,'o',1,"<core>");
+       createcommand("LOADMODULE",handle_loadmodule,'o',1,"<core>");
+       createcommand("UNLOADMODULE",handle_unloadmodule,'o',1,"<core>");
+       createcommand("SERVER",handle_server,0,0,"<core>");
 }
 
 void process_buffer(const char* cmdbuf,userrec *user)
@@ -3233,6 +3385,169 @@ void RemoveServer(const char* name)
 
 
 int reap_counter = 0;
+char MODERR[MAXBUF];
+
+char* ModuleError()
+{
+       return MODERR;
+}
+
+bool UnloadModule(const char* filename)
+{
+       for (int j = 0; j != module_names.size(); j++)
+       {
+               if (module_names[j] == std::string(filename))
+               {
+                       if (modules[j]->GetVersion().Flags & VF_STATIC)
+                       {
+                               log(DEFAULT,"Failed to unload STATIC module %s",filename);
+                               snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)");
+                               return false;
+                       }
+                       // found the module
+                       log(DEBUG,"Deleting module...");
+                       delete modules[j];
+                       modules[j] = NULL;
+                       log(DEBUG,"Deleting module factory pointer...");
+                       delete factory[j]->factory;
+                       log(DEBUG,"Deleting module factory...");
+                       delete factory[j];
+                       log(DEBUG,"Erasing module entry...");
+                       factory[j] = NULL;
+                       // here we should locate ALL resources claimed by this module... and release them
+                       // for example commands
+                       log(DEBUG,"Erasing module vector...");
+                       for (std::vector<ircd_module*>::iterator t = factory.begin(); t != factory.end(); t++)
+                       {
+                               if (*t == NULL)
+                               {
+                                       factory.erase(t);
+                                       break;
+                               }
+                       }
+                       log(DEBUG,"Erasing module name vector...");
+                       for (std::vector<std::string>::iterator v = module_names.begin(); v != module_names.end(); v++)
+                        {
+                                if (*v == std::string(filename))
+                                {
+                                        module_names.erase(v);
+                                        break;
+                                }
+                        }
+                       log(DEBUG,"Erasing module pointer...");
+                       for (std::vector<Module*>::iterator m = modules.begin(); m!= modules.end(); m++)
+                       {
+                               if (*m == NULL)
+                               {
+                                       modules.erase(m);
+                                       break;
+                               }
+                       }
+                        log(DEBUG,"Removing dependent commands...");
+                        removecommands(filename);
+                       log(DEFAULT,"Module %s unloaded",filename);
+                       MODCOUNT--;
+                       return true;
+               }
+       }
+       log(DEFAULT,"Module %s is not loaded, cannot unload it!",filename);
+       snprintf(MODERR,MAXBUF,"Module not loaded");
+       return false;
+}
+
+bool DirValid(char* dirandfile)
+{
+       char work[MAXBUF];
+       strlcpy(work,dirandfile,MAXBUF);
+       int p = strlen(work);
+       // we just want the dir
+       while (strlen(work))
+       {
+               if (work[p] == '/')
+               {
+                       work[p] = '\0';
+                       break;
+               }
+               work[p--] = '\0';
+       }
+       log(DEBUG,"Dir valid: %s",work);
+       char buffer[MAXBUF], otherdir[MAXBUF];
+       // Get the current working directory
+       if( getcwd( buffer, MAXBUF ) == NULL )
+               return false;
+       chdir(work);
+       if( getcwd( otherdir, MAXBUF ) == NULL )
+               return false;
+       chdir(buffer);
+       log(DEBUG,"Dir is really: %s",otherdir);
+       if (strlen(otherdir) >= strlen(work))
+       {
+               otherdir[strlen(work)] = '\0';
+               log(DEBUG,"Compare: '%s' -> '%s'",otherdir,work);
+               if (!strcmp(otherdir,work))
+               {
+                       log(DEBUG,"Match ok");
+                       return true;
+               }
+               log(DEBUG,"No match");
+               return false;
+       }
+       else return false;
+}
+
+bool LoadModule(const char* filename)
+{
+       char modfile[MAXBUF];
+       snprintf(modfile,MAXBUF,"%s/%s",ModPath,filename);
+       if (!DirValid(modfile))
+       {
+               log(DEFAULT,"Module %s is not within the modules directory.",modfile);
+               snprintf(MODERR,MAXBUF,"Module %s is not within the modules directory.",modfile);
+               return false;
+       }
+       log(DEBUG,"Loading module: %s",modfile);
+        if (FileExists(modfile))
+        {
+               for (int j = 0; j < module_names.size(); j++)
+               {
+                       if (module_names[j] == std::string(filename))
+                       {
+                               log(DEFAULT,"Module %s is already loaded, cannot load a module twice!",modfile);
+                               snprintf(MODERR,MAXBUF,"Module already loaded");
+                               return false;
+                       }
+               }
+                factory[MODCOUNT+1] = new ircd_module(modfile);
+                if (factory[MODCOUNT+1]->LastError())
+                {
+                        log(DEFAULT,"Unable to load %s: %s",modfile,factory[MODCOUNT+1]->LastError());
+                       snprintf(MODERR,MAXBUF,"Loader/Linker error: %s",factory[MODCOUNT+1]->LastError());
+                       MODCOUNT--;
+                       return false;
+                }
+                if (factory[MODCOUNT+1]->factory)
+                {
+                        modules[MODCOUNT+1] = factory[MODCOUNT+1]->factory->CreateModule();
+                        /* save the module and the module's classfactory, if
+                         * this isnt done, random crashes can occur :/ */
+                        module_names.push_back(filename);
+                }
+               else
+                {
+                        log(DEFAULT,"Unable to load %s",modfile);
+                       snprintf(MODERR,MAXBUF,"Factory function failed!");
+                       return false;
+                }
+        }
+        else
+        {
+                log(DEFAULT,"InspIRCd: startup: Module Not Found %s",modfile);
+               snprintf(MODERR,MAXBUF,"Module file could not be found");
+               return false;
+        }
+       MODCOUNT++;
+       return true;
+}
 
 int InspIRCd(void)
 {
@@ -3319,49 +3634,18 @@ int InspIRCd(void)
        printf("\n");
        
        /* BugFix By Craig! :p */
-       count = 0;
+       MODCOUNT = -1;
        for (count2 = 0; count2 < ConfValueEnum("module",&config_f); count2++)
        {
-               char modfile[MAXBUF];
                ConfValue("module","name",count2,configToken,&config_f);
-               snprintf(modfile,MAXBUF,"%s/%s",MOD_PATH,configToken,&config_f);
-               printf("Loading module... \033[1;37m%s\033[0;37m\n",modfile);
-               log(DEBUG,"InspIRCd: startup: Loading module: %s",modfile);
-               /* If The File Doesnt exist, Trying to load it
-                * Will Segfault the IRCd.. So, check to see if
-                * it Exists, Before Proceeding. */
-               if (FileExists(modfile))
-               {
-                       factory[count] = new ircd_module(modfile);
-                       if (factory[count]->LastError())
-                       {
-                               log(DEFAULT,"Unable to load %s: %s",modfile,factory[count]->LastError());
-                               printf("Unable to load %s: %s\nExiting...\n",modfile,factory[count]->LastError());
-                               Exit(ERROR);
-                       }
-                       if (factory[count]->factory)
-                       {
-                               modules[count] = factory[count]->factory->CreateModule();
-                               /* save the module and the module's classfactory, if
-                                * this isnt done, random crashes can occur :/ */
-                               module_names.push_back(modfile);        
-                       }
-                       else
-                       {
-                               log(DEFAULT,"Unable to load %s",modfile);
-                               printf("Unable to load %s\nExiting...\n",modfile);
-                               Exit(ERROR);
-                       }
-                       /* Increase the Count */
-                       count++;
-               }
-               else
+               printf("Loading module... \033[1;37m%s\033[0;37m\n",configToken);
+               if (!LoadModule(configToken))
                {
-                       log(DEBUG,"InspIRCd: startup: Module Not Found %s",modfile);
-                       printf("Module Not Found: \033[1;37m%s\033[0;37m, Skipping\n",modfile);
+                       log(DEBUG,"Exiting due to a module loader error.");
+                       printf("There was an error loading a module: %s\n",ModuleError());
+                       Exit(0);
                }
        }
-       MODCOUNT = count - 1;
        log(DEBUG,"Total loaded modules: %d",MODCOUNT+1);
        
        printf("\nInspIRCd is now running!\n");
@@ -3499,14 +3783,17 @@ int InspIRCd(void)
                                        char remotehost[MAXBUF],resolved[MAXBUF];
                                        length = sizeof (client);
                                        incomingSockfd = accept (me[x]->fd, (sockaddr *) &client, &length);
-                                       strlcpy(remotehost,(char *)inet_ntoa(client.sin_addr),MAXBUF);
-                                       if(CleanAndResolve(resolved, remotehost) != TRUE)
+                                       if (incomingSockfd != -1)
                                        {
-                                               strlcpy(resolved,remotehost,MAXBUF);
+                                               strlcpy(remotehost,(char *)inet_ntoa(client.sin_addr),MAXBUF);
+                                               if(CleanAndResolve(resolved, remotehost) != TRUE)
+                                               {
+                                                       strlcpy(resolved,remotehost,MAXBUF);
+                                               }
+                                               // add to this connections ircd_connector vector
+                                               // *FIX* - we need the LOCAL port not the remote port in &client!
+                                               me[x]->AddIncoming(incomingSockfd,resolved,me[x]->port);
                                        }
-                                       // add to this connections ircd_connector vector
-                                       // *FIX* - we need the LOCAL port not the remote port in &client!
-                                       me[x]->AddIncoming(incomingSockfd,resolved,me[x]->port);
                                }
                        }
                }
@@ -3539,8 +3826,9 @@ int InspIRCd(void)
                                                else
                                                        NetSendToAllExcept(tcp_host,udp_msg);
                                        }
-                                       FOREACH_MOD OnPacketReceive(udp_msg);
-                                       handle_link_packet(udp_msg, tcp_host, me[x]);
+                                       std::string msg = udp_msg;
+                                       FOREACH_MOD OnPacketReceive(msg,tcp_host);
+                                       strlcpy(udp_msg,msg.c_str(),MAXBUF);
                                }
                                goto label;
                        }
@@ -3604,7 +3892,7 @@ int InspIRCd(void)
                                                        Write(count2->second->fd,"PING :%s",ServerName);
                                                        log(DEBUG,"InspIRCd: pinging: %s",count2->second->nick);
                                                        count2->second->lastping = 0;
-                                                       count2->second->nping = TIME+120;
+                                                       count2->second->nping = TIME+count2->second->pingmax;   // was hard coded to 120
                                                }
                                        }
                                        count2++;