]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules.cpp
Move stuff around a bit:
[user/henk/code/inspircd.git] / src / modules.cpp
index 5fc8ffc471bddb6ea0ad99b7573b07d6cbd4f08f..92f619743d11f0b640c57539f8eb0765b49b9a8a 100644 (file)
@@ -24,7 +24,7 @@
  */
 
 
-#include <fstream>
+#include <iostream>
 #include "inspircd.h"
 #include "xline.h"
 #include "socket.h"
@@ -81,6 +81,7 @@ void Module::DetachEvent(Implementation i)
        ServerInstance->Modules->Detach(i, this);
 }
 
+void           Module::ReadConfig(ConfigStatus& status) { }
 ModResult      Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { DetachEvent(I_OnSendSnotice); return MOD_RES_PASSTHRU; }
 void           Module::OnUserConnect(LocalUser*) { DetachEvent(I_OnUserConnect); }
 void           Module::OnUserQuit(User*, const std::string&, const std::string&) { DetachEvent(I_OnUserQuit); }
@@ -90,7 +91,6 @@ void          Module::OnPostJoin(Membership*) { DetachEvent(I_OnPostJoin); }
 void           Module::OnUserPart(Membership*, std::string&, CUList&) { DetachEvent(I_OnUserPart); }
 void           Module::OnPreRehash(User*, const std::string&) { DetachEvent(I_OnPreRehash); }
 void           Module::OnModuleRehash(User*, const std::string&) { DetachEvent(I_OnModuleRehash); }
-void           Module::OnRehash(User*) { DetachEvent(I_OnRehash); }
 ModResult      Module::OnUserPreJoin(LocalUser*, Channel*, const std::string&, std::string&, const std::string&) { DetachEvent(I_OnUserPreJoin); return MOD_RES_PASSTHRU; }
 void           Module::OnMode(User*, User*, Channel*, const std::vector<std::string>&, const std::vector<TranslateType>&) { DetachEvent(I_OnMode); }
 void           Module::OnOper(User*, const std::string&) { DetachEvent(I_OnOper); }
@@ -133,12 +133,10 @@ void              Module::OnUserMessage(User*, void*, int, const std::string&, char, const C
 void           Module::OnUserInvite(User*, User*, Channel*, time_t) { DetachEvent(I_OnUserInvite); }
 void           Module::OnPostTopicChange(User*, Channel*, const std::string&) { DetachEvent(I_OnPostTopicChange); }
 void           Module::OnGetServerDescription(const std::string&, std::string&) { DetachEvent(I_OnGetServerDescription); }
-void           Module::OnSyncUser(User*, Module*, void*) { DetachEvent(I_OnSyncUser); }
-void           Module::OnSyncChannel(Channel*, Module*, void*) { DetachEvent(I_OnSyncChannel); }
-void           Module::OnSyncNetwork(Module*, void*) { DetachEvent(I_OnSyncNetwork); }
-void           Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
+void           Module::OnSyncUser(User*, ProtocolInterface::Server&) { DetachEvent(I_OnSyncUser); }
+void           Module::OnSyncChannel(Channel*, ProtocolInterface::Server&) { DetachEvent(I_OnSyncChannel); }
+void           Module::OnSyncNetwork(ProtocolInterface::Server&) { DetachEvent(I_OnSyncNetwork); }
 void           Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { DetachEvent(I_OnDecodeMetaData); }
-void           Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
 void           Module::OnChangeHost(User*, const std::string&) { DetachEvent(I_OnChangeHost); }
 void           Module::OnChangeName(User*, const std::string&) { DetachEvent(I_OnChangeName); }
 void           Module::OnChangeIdent(User*, const std::string&) { DetachEvent(I_OnChangeIdent); }
@@ -162,7 +160,25 @@ ModResult   Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockadd
 void           Module::OnSendWhoLine(User*, const std::vector<std::string>&, User*, std::string&) { DetachEvent(I_OnSendWhoLine); }
 void           Module::OnSetUserIP(LocalUser*) { DetachEvent(I_OnSetUserIP); }
 
-ModuleManager::ModuleManager() : ModCount(0)
+ServiceProvider::ServiceProvider(Module* Creator, const std::string& Name, ServiceType Type)
+       : creator(Creator), name(Name), service(Type)
+{
+       if ((ServerInstance) && (ServerInstance->Modules->NewServices))
+               ServerInstance->Modules->NewServices->push_back(this);
+}
+
+void ServiceProvider::DisableAutoRegister()
+{
+       if ((ServerInstance) && (ServerInstance->Modules->NewServices))
+       {
+               ModuleManager::ServiceList& list = *ServerInstance->Modules->NewServices;
+               ModuleManager::ServiceList::iterator it = std::find(list.begin(), list.end(), this);
+               if (it != list.end())
+                       list.erase(it);
+       }
+}
+
+ModuleManager::ModuleManager()
 {
 }
 
@@ -317,6 +333,29 @@ swap_now:
        return true;
 }
 
+bool ModuleManager::PrioritizeHooks()
+{
+       /* We give every module a chance to re-prioritize when we introduce a new one,
+        * not just the one thats loading, as the new module could affect the preference
+        * of others
+        */
+       for (int tries = 0; tries < 20; tries++)
+       {
+               prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
+               for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
+                       n->second->Prioritize();
+
+               if (prioritizationState == PRIO_STATE_LAST)
+                       break;
+               if (tries == 19)
+               {
+                       ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Hook priority dependency loop detected");
+                       return false;
+               }
+       }
+       return true;
+}
+
 bool ModuleManager::CanUnload(Module* mod)
 {
        std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
@@ -388,7 +427,6 @@ void ModuleManager::DoSafeUnload(Module* mod)
        ServerInstance->GlobalCulls.AddItem(mod);
 
        ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Module %s unloaded",mod->ModuleSourceFile.c_str());
-       this->ModCount--;
        ServerInstance->ISupport.Build();
 }
 
@@ -415,11 +453,121 @@ void ModuleManager::UnloadAll()
        }
 }
 
+namespace
+{
+       struct UnloadAction : public HandlerBase0<void>
+       {
+               Module* const mod;
+               UnloadAction(Module* m) : mod(m) {}
+               void Call()
+               {
+                       DLLManager* dll = mod->ModuleDLLManager;
+                       ServerInstance->Modules->DoSafeUnload(mod);
+                       ServerInstance->GlobalCulls.Apply();
+                       // In pure static mode this is always NULL
+                       delete dll;
+                       ServerInstance->GlobalCulls.AddItem(this);
+               }
+       };
+
+       struct ReloadAction : public HandlerBase0<void>
+       {
+               Module* const mod;
+               HandlerBase1<void, bool>* const callback;
+               ReloadAction(Module* m, HandlerBase1<void, bool>* c)
+                       : mod(m), callback(c) {}
+               void Call()
+               {
+                       DLLManager* dll = mod->ModuleDLLManager;
+                       std::string name = mod->ModuleSourceFile;
+                       ServerInstance->Modules->DoSafeUnload(mod);
+                       ServerInstance->GlobalCulls.Apply();
+                       delete dll;
+                       bool rv = ServerInstance->Modules->Load(name);
+                       if (callback)
+                               callback->Call(rv);
+                       ServerInstance->GlobalCulls.AddItem(this);
+               }
+       };
+}
+
+bool ModuleManager::Unload(Module* mod)
+{
+       if (!CanUnload(mod))
+               return false;
+       ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
+       return true;
+}
+
+void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
+{
+       if (CanUnload(mod))
+               ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
+       else
+               callback->Call(false);
+}
+
+void ModuleManager::LoadAll()
+{
+       std::map<std::string, ServiceList> servicemap;
+       LoadCoreModules(servicemap);
+
+       ConfigTagList tags = ServerInstance->Config->ConfTags("module");
+       for (ConfigIter i = tags.first; i != tags.second; ++i)
+       {
+               ConfigTag* tag = i->second;
+               std::string name = tag->getString("name");
+               this->NewServices = &servicemap[name];
+               std::cout << "[" << con_green << "*" << con_reset << "] Loading module:\t" << con_green << name << con_reset << std::endl;
+
+               if (!this->Load(name, true))
+               {
+                       ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, this->LastError());
+                       std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << this->LastError() << std::endl << std::endl;
+                       ServerInstance->Exit(EXIT_STATUS_MODULE);
+               }
+       }
+
+       ConfigStatus confstatus;
+
+       for (ModuleMap::const_iterator i = Modules.begin(); i != Modules.end(); ++i)
+       {
+               Module* mod = i->second;
+               try
+               {
+                       ServerInstance->Logs->Log("MODULE", LOG_DEBUG, "Initializing %s", i->first.c_str());
+                       AttachAll(mod);
+                       AddServices(servicemap[i->first]);
+                       mod->init();
+                       mod->ReadConfig(confstatus);
+               }
+               catch (CoreException& modexcept)
+               {
+                       LastModuleError = "Unable to initialize " + mod->ModuleSourceFile + ": " + modexcept.GetReason();
+                       ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
+                       std::cout << std::endl << "[" << con_red << "*" << con_reset << "] " << LastModuleError << std::endl << std::endl;
+                       ServerInstance->Exit(EXIT_STATUS_MODULE);
+               }
+       }
+
+       if (!PrioritizeHooks())
+               ServerInstance->Exit(EXIT_STATUS_MODULE);
+}
+
 std::string& ModuleManager::LastError()
 {
        return LastModuleError;
 }
 
+void ModuleManager::AddServices(const ServiceList& list)
+{
+       for (ServiceList::const_iterator i = list.begin(); i != list.end(); ++i)
+       {
+               ServiceProvider& s = **i;
+               AddService(s);
+       }
+}
+
 void ModuleManager::AddService(ServiceProvider& item)
 {
        switch (item.service)
@@ -559,55 +707,3 @@ Module* ModuleManager::Find(const std::string &name)
        else
                return modfind->second;
 }
-
-const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
-{
-       std::vector<std::string> retval;
-       for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
-               if (!filter || (x->second->GetVersion().Flags & filter))
-                       retval.push_back(x->first);
-       return retval;
-}
-
-FileReader::FileReader(const std::string& filename)
-{
-       Load(filename);
-}
-
-void FileReader::Load(const std::string& filename)
-{
-       // If the file is stored in the file cache then we used that version instead.
-       ConfigFileCache::iterator it = ServerInstance->Config->Files.find(filename);
-       if (it != ServerInstance->Config->Files.end())
-       {
-               this->lines = it->second;
-       }
-       else
-       {
-               lines.clear();
-
-               std::ifstream stream(filename.c_str());
-               if (!stream.is_open())
-                       throw CoreException(filename + " does not exist or is not readable!");
-
-               std::string line;
-               while (std::getline(stream, line))
-               {
-                       lines.push_back(line);
-                       totalSize += line.size() + 2;
-               }
-
-               stream.close();
-       }
-}
-
-std::string FileReader::GetString()
-{
-       std::string buffer;
-       for (file_cache::iterator it = this->lines.begin(); it != this->lines.end(); ++it)
-       {
-               buffer.append(*it);
-               buffer.append("\r\n");
-       }
-       return buffer;
-}