+bool ModuleManager::CanUnload(Module* mod)
+{
+ std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
+
+ if ((modfind == Modules.end()) || (modfind->second != mod) || (mod->dying))
+ {
+ 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;
+ }
+
+ mod->dying = true;
+ 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(); )
+ {
+ Channel* chan = c->second;
+ ++c;
+ mod->OnCleanup(TYPE_CHANNEL, chan);
+ chan->doUnhookExtensions(items);
+ const UserMembList* users = chan->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(); )
+ {
+ User* user = u->second;
+ // The module may quit the user (e.g. SSL mod unloading) and that will remove it from the container
+ ++u;
+ mod->OnCleanup(TYPE_USER, user);
+ user->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);
+ }
+
+ dynamic_reference_base::reset_all();
+
+ /* 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();
+}
+
+void ModuleManager::UnloadAll()
+{
+ /* We do this more than once, so that any service providers get a
+ * chance to be unhooked by the modules using them, but then get
+ * a chance to be removed themsleves.
+ *
+ * Note: this deliberately does NOT delete the DLLManager objects
+ */
+ for (int tries = 0; tries < 4; tries++)
+ {
+ std::map<std::string, Module*>::iterator i = Modules.begin();
+ while (i != Modules.end())
+ {
+ std::map<std::string, Module*>::iterator me = i++;
+ if (CanUnload(me->second))
+ {
+ DoSafeUnload(me->second);
+ }
+ }
+ ServerInstance->GlobalCulls.Apply();
+ }
+}
+