]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules.cpp
Add Module::init() for correct exception handling during hook registration
[user/henk/code/inspircd.git] / src / modules.cpp
index f460a6a28401b9df0baaa7c8e06cdbc0559c19ce..22628ff0f1c5edc144527e3abcb203961d1e1e78 100644 (file)
 
 // Version is a simple class for holding a modules version number
 template<>
-VersionBase<API_VERSION>::VersionBase(const std::string &modv, int flags)
-: description(modv), Flags(flags)
+VersionBase<API_VERSION>::VersionBase(const std::string &desc, int flags)
+: description(desc), Flags(flags)
+{
+}
+
+template<>
+VersionBase<API_VERSION>::VersionBase(const std::string &desc, int flags, const std::string& linkdata)
+: description(desc), Flags(flags), link_data(linkdata)
 {
 }
 
@@ -168,6 +174,9 @@ ModuleManager::~ModuleManager()
 
 bool ModuleManager::Attach(Implementation i, Module* mod)
 {
+       if (Modules.find(mod->ModuleSourceFile) == Modules.end())
+               ServerInstance->Logs->Log("MODULE", ERROR, "Module %s is attaching to hook %d in constructor; this does not handle exceptions correctly!", mod->ModuleSourceFile.c_str(), i);
+
        if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
                return false;
 
@@ -323,6 +332,79 @@ bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Modul
        return true;
 }
 
+bool ModuleManager::CanUnload(Module* mod)
+{
+       std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
+
+       if (modfind == Modules.end() || modfind->second != mod)
+       {
+               LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
+               ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
+               return false;
+       }
+       if (mod->GetVersion().Flags & VF_STATIC)
+       {
+               LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
+               ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
+               return false;
+       }
+       return true;
+}
+
+void ModuleManager::DoSafeUnload(Module* mod)
+{
+       std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
+
+       std::vector<reference<ExtensionItem> > items;
+       ServerInstance->Extensions.BeginUnregister(modfind->second, items);
+       /* Give the module a chance to tidy out all its metadata */
+       for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
+       {
+               mod->OnCleanup(TYPE_CHANNEL,c->second);
+               c->second->doUnhookExtensions(items);
+               const UserMembList* users = c->second->GetUsers();
+               for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
+                       mi->second->doUnhookExtensions(items);
+       }
+       for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
+       {
+               mod->OnCleanup(TYPE_USER,u->second);
+               u->second->doUnhookExtensions(items);
+       }
+       for(char m='A'; m <= 'z'; m++)
+       {
+               ModeHandler* mh;
+               mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
+               if (mh && mh->creator == mod)
+                       ServerInstance->Modes->DelMode(mh);
+               mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
+               if (mh && mh->creator == mod)
+                       ServerInstance->Modes->DelMode(mh);
+       }
+       for(std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.begin(); i != DataProviders.end(); )
+       {
+               std::multimap<std::string, ServiceProvider*>::iterator curr = i++;
+               if (curr->second->creator == mod)
+                       DataProviders.erase(curr);
+       }
+       for(unsigned int i = 0; i < ServerInstance->Modules->ActiveDynrefs.size(); i++)
+               ServerInstance->Modules->ActiveDynrefs[i]->ClearCache();
+
+       /* Tidy up any dangling resolvers */
+       ServerInstance->Res->CleanResolvers(mod);
+
+       FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
+
+       DetachAll(mod);
+
+       Modules.erase(modfind);
+       ServerInstance->GlobalCulls.AddItem(mod);
+
+       ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",mod->ModuleSourceFile.c_str());
+       this->ModCount--;
+       ServerInstance->BuildISupport();
+}
+
 std::string& ModuleManager::LastError()
 {
        return LastModuleError;
@@ -338,16 +420,12 @@ bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, Us
        return this->Parser->IsValidCommand(commandname, pcnt, user);
 }
 
-void InspIRCd::AddCommand(Command *f)
-{
-       if (!this->Parser->AddCommand(f))
-       {
-               throw ModuleException("Command "+std::string(f->name)+" already exists.");
-       }
-}
-
 void ModuleManager::AddService(ServiceProvider& item)
 {
+       Module* owner = item.creator;
+       if (Modules.find(owner->ModuleSourceFile) == Modules.end())
+               ServerInstance->Logs->Log("MODULE", ERROR, "Module %s is registering item %s in constructor; this does not handle exceptions correctly!", owner->ModuleSourceFile.c_str(), item.name.c_str());
+
        switch (item.service)
        {
                case SERVICE_COMMAND: