*/
-#include <fstream>
+#include <iostream>
#include "inspircd.h"
#include "xline.h"
#include "socket.h"
void Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&, MessageType) { DetachEvent(I_OnUserMessage); }
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); }
void Module::OnSendWhoLine(User*, const std::vector<std::string>&, User*, std::string&) { DetachEvent(I_OnSendWhoLine); }
void Module::OnSetUserIP(LocalUser*) { DetachEvent(I_OnSetUserIP); }
+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()
{
}
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);
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++)
+ for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); )
{
- mod->OnCleanup(TYPE_CHANNEL,c->second);
- c->second->doUnhookExtensions(items);
- const UserMembList* users = c->second->GetUsers();
+ 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(); u++)
+ for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); )
{
- mod->OnCleanup(TYPE_USER,u->second);
- u->second->doUnhookExtensions(items);
+ 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++)
{
}
}
+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);
+ }
+ }
+
+ this->NewServices = NULL;
+
+ 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)
else
return modfind->second;
}
-
-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.
- std::string realName = ServerInstance->Config->Paths.PrependConfig(filename);
- ConfigFileCache::iterator it = ServerInstance->Config->Files.find(realName);
- if (it != ServerInstance->Config->Files.end())
- {
- this->lines = it->second;
- }
- else
- {
- lines.clear();
-
- std::ifstream stream(realName.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;
-}