+bool ModuleManager::EraseHandle(unsigned int j)
+{
+ ModuleHandleList::iterator iter;
+
+ if((j < 0) || (j >= handles.size()))
+ {
+ return false;
+ }
+
+ iter = handles.begin() + j;
+
+ if(*iter)
+ {
+ delete *iter;
+ handles.erase(iter);
+ handles.push_back(NULL);
+ }
+
+ return true;
+}
+
+bool ModuleManager::EraseModule(unsigned int j)
+{
+ bool success = false;
+
+ {
+ ModuleList::iterator iter;
+
+ if((j < 0) || (j >= modules.size()))
+ {
+ return false;
+ }
+
+ iter = modules.begin() + j;
+
+ if(*iter)
+ {
+ delete *iter;
+ modules.erase(iter);
+ modules.push_back(NULL);
+ success = true;
+ }
+ }
+
+ {
+ std::vector<std::string>::iterator iter;
+
+ if((j < 0) || (j >= Instance->Config->module_names.size()))
+ {
+ return false;
+ }
+
+ iter = Instance->Config->module_names.begin() + j;
+
+ Instance->Config->module_names.erase(iter);
+ success = true;
+ }
+
+ return success;
+}
+
+void ModuleManager::MoveTo(std::string modulename,int slot)
+{
+ unsigned int v2 = 256;
+
+ for (unsigned int v = 0; v < Instance->Config->module_names.size(); v++)
+ {
+ if (Instance->Config->module_names[v] == modulename)
+ {
+ // found an instance, swap it with the item at the end
+ v2 = v;
+ break;
+ }
+ }
+ if ((v2 != (unsigned int)slot) && (v2 < 256))
+ {
+ // Swap the module names over
+ Instance->Config->module_names[v2] = Instance->Config->module_names[slot];
+ Instance->Config->module_names[slot] = modulename;
+ // now swap the module factories
+ ircd_module* temp = handles[v2];
+ handles[v2] = handles[slot];
+ handles[slot] = temp;
+ // now swap the module objects
+ Module* temp_module = modules[v2];
+ modules[v2] = modules[slot];
+ modules[slot] = temp_module;
+ // now swap the implement lists (we dont
+ // need to swap the global or recount it)
+ for (int n = 0; n < 255; n++)
+ {
+ char x = Instance->Config->implement_lists[v2][n];
+ Instance->Config->implement_lists[v2][n] = Instance->Config->implement_lists[slot][n];
+ Instance->Config->implement_lists[slot][n] = x;
+ }
+ }
+}
+
+void ModuleManager::MoveAfter(std::string modulename, std::string after)
+{
+ for (unsigned int v = 0; v < Instance->Config->module_names.size(); v++)
+ {
+ if (Instance->Config->module_names[v] == after)
+ {
+ MoveTo(modulename, v);
+ return;
+ }
+ }
+}
+
+void ModuleManager::MoveBefore(std::string modulename, std::string before)
+{
+ for (unsigned int v = 0; v < Instance->Config->module_names.size(); v++)
+ {
+ if (Instance->Config->module_names[v] == before)
+ {
+ if (v > 0)
+ {
+ MoveTo(modulename, v-1);
+ }
+ else
+ {
+ MoveTo(modulename, v);
+ }
+ return;
+ }
+ }
+}
+
+void ModuleManager::MoveToFirst(std::string modulename)
+{
+ MoveTo(modulename,0);
+}
+
+void ModuleManager::MoveToLast(std::string modulename)
+{
+ MoveTo(modulename,this->GetCount());
+}
+
+bool ModuleManager::Unload(const char* filename)
+{
+ std::string filename_str = filename;
+ for (unsigned int j = 0; j != Instance->Config->module_names.size(); j++)
+ {
+ if (Instance->Config->module_names[j] == filename_str)
+ {
+ if (modules[j]->GetVersion().Flags & VF_STATIC)
+ {
+ Instance->Log(DEFAULT,"Failed to unload STATIC module %s",filename);
+ snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)");
+ return false;
+ }
+ std::pair<int,std::string> intercount = GetInterfaceInstanceCount(modules[j]);
+ if (intercount.first > 0)
+ {
+ Instance->Log(DEFAULT,"Failed to unload module %s, being used by %d other(s) via interface '%s'",filename, intercount.first, intercount.second.c_str());
+ snprintf(MODERR,MAXBUF,"Module not unloadable (Still in use by %d other module%s which %s using its interface '%s') -- unload dependent modules first!",
+ intercount.first,
+ intercount.first > 1 ? "s" : "",
+ intercount.first > 1 ? "are" : "is",
+ intercount.second.c_str());
+ return false;
+ }
+ /* Give the module a chance to tidy out all its metadata */
+ for (chan_hash::iterator c = Instance->chanlist->begin(); c != Instance->chanlist->end(); c++)
+ {
+ modules[j]->OnCleanup(TYPE_CHANNEL,c->second);
+ }
+ for (user_hash::iterator u = Instance->clientlist->begin(); u != Instance->clientlist->end(); u++)
+ {
+ modules[j]->OnCleanup(TYPE_USER,u->second);
+ }
+
+ /* Tidy up any dangling resolvers */
+ Instance->Res->CleanResolvers(modules[j]);
+
+ FOREACH_MOD_I(Instance,I_OnUnloadModule,OnUnloadModule(modules[j],Instance->Config->module_names[j]));
+
+ for(int t = 0; t < 255; t++)
+ {
+ Instance->Config->global_implementation[t] -= Instance->Config->implement_lists[j][t];
+ }
+
+ /* We have to renumber implement_lists after unload because the module numbers change!
+ */
+ for(int j2 = j; j2 < 254; j2++)
+ {
+ for(int t = 0; t < 255; t++)
+ {
+ Instance->Config->implement_lists[j2][t] = Instance->Config->implement_lists[j2+1][t];
+ }
+ }
+
+ // found the module
+ Instance->Parser->RemoveCommands(filename);
+ this->EraseModule(j);
+ this->EraseHandle(j);
+ Instance->Log(DEFAULT,"Module %s unloaded",filename);
+ this->ModCount--;
+ Instance->BuildISupport();
+ return true;
+ }
+ }
+ Instance->Log(DEFAULT,"Module %s is not loaded, cannot unload it!",filename);
+ snprintf(MODERR,MAXBUF,"Module not loaded");
+ return false;
+}
+
+/* We must load the modules AFTER initializing the socket engine, now */
+void ModuleManager::LoadAll()