bool ModuleManager::Load(const std::string& name, bool defer)
{
    modmap::iterator it = modlist->find(name);
    if (it == modlist->end())
        return false;
    Module* mod = NULL;

    ServiceList newservices;
    if (!defer)
        this->NewServices = &newservices;

    try
    {
        mod = (*it->second->init)();
        mod->ModuleSourceFile = name;
        mod->ModuleDLLManager = NULL;
        mod->dying = false;
        Modules[name] = mod;
        this->NewServices = NULL;
        if (defer)
        {
            ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s", name.c_str());
            return true;
        }
        else
        {
            ConfigStatus confstatus;

            AttachAll(mod);
            AddServices(newservices);
            mod->init();
            mod->ReadConfig(confstatus);
        }
    }
    catch (CoreException& modexcept)
    {
        this->NewServices = NULL;

        if (mod)
            DoSafeUnload(mod);
        ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Unable to load " + name + ": " + modexcept.GetReason());
        return false;
    }

    FOREACH_MOD(OnLoadModule, (mod));
    PrioritizeHooks();
    ServerInstance->ISupport.Build();
    return true;
}
Beispiel #2
0
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();
	}
}
Beispiel #3
0
bool ModuleManager::Load(const std::string& modname, bool defer)
{
    /* Don't allow people to specify paths for modules, it doesn't work as expected */
    if (modname.find('/') != std::string::npos)
    {
        LastModuleError = "You can't load modules with a path: " + modname;
        return false;
    }

    const std::string filename = ExpandModName(modname);
    const std::string moduleFile = ServerInstance->Config->Paths.PrependModule(filename);

    if (!FileSystem::FileExists(moduleFile))
    {
        LastModuleError = "Module file could not be found: " + filename;
        ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
        return false;
    }

    if (Modules.find(filename) != Modules.end())
    {
        LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
        ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
        return false;
    }

    Module* newmod = NULL;
    DLLManager* newhandle = new DLLManager(moduleFile.c_str());
    ServiceList newservices;
    if (!defer)
        this->NewServices = &newservices;

    try
    {
        newmod = newhandle->CallInit();
        this->NewServices = NULL;

        if (newmod)
        {
            newmod->ModuleSourceFile = filename;
            newmod->ModuleDLLManager = newhandle;
            newmod->dying = false;
            Modules[filename] = newmod;
            std::string version = newhandle->GetVersion();
            if (defer)
            {
                ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)",
                                          filename.c_str(), version.c_str());
            }
            else
            {
                ConfigStatus confstatus;

                AttachAll(newmod);
                AddServices(newservices);
                newmod->init();
                newmod->ReadConfig(confstatus);

                Version v = newmod->GetVersion();
                ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "New module introduced: %s (Module version %s)%s",
                                          filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
            }
        }
        else
        {
            LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
            ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
            delete newhandle;
            return false;
        }
    }
    catch (CoreException& modexcept)
    {
        this->NewServices = NULL;

        // failure in module constructor
        if (newmod)
        {
            DoSafeUnload(newmod);
            ServerInstance->GlobalCulls.AddItem(newhandle);
        }
        else
            delete newhandle;
        LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
        ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
        return false;
    }

    if (defer)
        return true;

    FOREACH_MOD(OnLoadModule, (newmod));
    PrioritizeHooks();
    ServerInstance->ISupport.Build();
    return true;
}
bool ModuleManager::Load(const std::string& filename, bool defer)
{
	/* Don't allow people to specify paths for modules, it doesn't work as expected */
	if (filename.find('/') != std::string::npos)
		return false;

	char modfile[MAXBUF];
	snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename.c_str());

	if (!ServerConfig::FileExists(modfile))
	{
		LastModuleError = "Module file could not be found: " + filename;
		ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
		return false;
	}

	if (Modules.find(filename) != Modules.end())
	{
		LastModuleError = "Module " + filename + " is already loaded, cannot load a module twice!";
		ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
		return false;
	}

	Module* newmod = NULL;
	DLLManager* newhandle = new DLLManager(modfile);

	try
	{
		newmod = newhandle->CallInit();

		if (newmod)
		{
			newmod->ModuleSourceFile = filename;
			newmod->ModuleDLLManager = newhandle;
			newmod->dying = false;
			Modules[filename] = newmod;
			std::string version = newhandle->GetVersion();
			if (defer)
			{
				ServerInstance->Logs->Log("MODULE", LOG_DEFAULT,"New module introduced: %s (Module version %s)",
					filename.c_str(), version.c_str());
			}
			else
			{
				newmod->init();

				Version v = newmod->GetVersion();
				ServerInstance->Logs->Log("MODULE", LOG_DEFAULT,"New module introduced: %s (Module version %s)%s",
					filename.c_str(), version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
			}
		}
		else
		{
			LastModuleError = "Unable to load " + filename + ": " + newhandle->LastError();
			ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
			delete newhandle;
			return false;
		}
	}
	catch (CoreException& modexcept)
	{
		// failure in module constructor
		if (newmod)
		{
			DoSafeUnload(newmod);
			ServerInstance->GlobalCulls.AddItem(newhandle);
		}
		else
			delete newhandle;
		LastModuleError = "Unable to load " + filename + ": " + modexcept.GetReason();
		ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
		return false;
	}

	this->ModCount++;
	if (defer)
		return true;

	FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
	/* 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 while loading " + filename);
	}

	ServerInstance->ISupport.Build();
	return true;
}