]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules.cpp
m_dnsbl updates
[user/henk/code/inspircd.git] / src / modules.cpp
index 46582fc799929100c5cc9fbcb0c90bb172b7d02d..6586166c8c21396094b1f89975675f8a283c7824 100644 (file)
 
 // Version is a simple class for holding a modules version number
 template<>
-VersionBase<API_VERSION>::VersionBase(const std::string &modv, int flags, const std::string& rev)
-: description(modv), version(rev), 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)
+{
+}
+
+template<>
+bool VersionBase<API_VERSION>::CanLink(const std::string& other_data)
+{
+       return link_data == other_data;
+}
+
 Request::Request(Module* src, Module* dst, const char* idstr)
 : id(idstr), source(src), dest(dst)
 {
@@ -87,8 +99,9 @@ ModResult     Module::OnKill(User*, User*, const std::string&) { return MOD_RES_PASS
 void           Module::OnLoadModule(Module*) { }
 void           Module::OnUnloadModule(Module*) { }
 void           Module::OnBackgroundTimer(time_t) { }
-ModResult      Module::OnPreCommand(std::string&, std::vector<std::string>&, User *, bool, const std::string&) { return MOD_RES_PASSTHRU; }
-void           Module::OnPostCommand(const std::string&, const std::vector<std::string>&, User *, CmdResult, const std::string&) { }
+ModResult      Module::OnPreCommand(std::string&, std::vector<std::string>&, LocalUser*, bool, const std::string&) { return MOD_RES_PASSTHRU; }
+void           Module::OnPostCommand(const std::string&, const std::vector<std::string>&, LocalUser*, CmdResult, const std::string&) { }
+void           Module::OnUserInit(LocalUser*) { }
 ModResult      Module::OnCheckReady(LocalUser*) { return MOD_RES_PASSTHRU; }
 ModResult      Module::OnUserRegister(LocalUser*) { return MOD_RES_PASSTHRU; }
 ModResult      Module::OnUserPreKick(User*, Membership*, const std::string&) { return MOD_RES_PASSTHRU; }
@@ -143,13 +156,14 @@ ModResult Module::OnUserList(User*, Channel*) { return MOD_RES_PASSTHRU; }
 ModResult      Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
 void           Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
 void           Module::OnGarbageCollect() { }
+ModResult      Module::OnSetConnectClass(LocalUser* user, ConnectClass* myclass) { return MOD_RES_PASSTHRU; }
 void           Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
 void           Module::OnRunTestSuite() { }
 void           Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
 ModResult      Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
 void           Module::OnHookIO(StreamSocket*, ListenSocket*) { }
 ModResult   Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { return MOD_RES_PASSTHRU; }
-void           Module::OnSendWhoLine(User*, User*, Channel*, std::string&) { }
+void           Module::OnSendWhoLine(User*, const std::vector<std::string>&, User*, Channel*, std::string&) { }
 ModResult      Module::OnChannelRestrictionApply(User*, Channel*, const char*) { return MOD_RES_PASSTHRU; }
 
 ModuleManager::ModuleManager() : ModCount(0)
@@ -162,6 +176,9 @@ ModuleManager::~ModuleManager()
 
 bool ModuleManager::Attach(Implementation i, Module* mod)
 {
+       if (Modules.find(mod->ModuleSourceFile) == Modules.end())
+               ServerInstance->Logs->Log("MODULE", DEFAULT, "Module is attaching to hook %d in constructor; this does not handle exceptions correctly!", i);
+
        if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
                return false;
 
@@ -317,6 +334,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;
@@ -332,24 +422,19 @@ 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", DEFAULT, "Module is registering item %s in constructor; this does not handle exceptions correctly!", item.name.c_str());
+
        switch (item.service)
        {
                case SERVICE_COMMAND:
                        if (!ServerInstance->Parser->AddCommand(static_cast<Command*>(&item)))
                                throw ModuleException("Command "+std::string(item.name)+" already exists.");
                        return;
-               case SERVICE_CMODE:
-               case SERVICE_UMODE:
+               case SERVICE_MODE:
                        if (!ServerInstance->Modes->AddMode(static_cast<ModeHandler*>(&item)))
                                throw ModuleException("Mode "+std::string(item.name)+" already exists.");
                        return;
@@ -440,6 +525,14 @@ void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
        this->Modes->Process(parameters, user);
 }
 
+
+void InspIRCd::SendGlobalMode(const std::vector<std::string>& parameters, User *user)
+{
+       Modes->Process(parameters, user);
+       if (!Modes->GetLastParse().empty())
+               this->PI->SendMode(parameters[0], Modes->GetLastParseParams(), Modes->GetLastParseTranslate());
+}
+
 bool InspIRCd::AddResolver(Resolver* r, bool cached)
 {
        if (!cached)
@@ -474,6 +567,8 @@ const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
 ConfigReader::ConfigReader()
 {
        this->error = 0;
+       ServerInstance->Logs->Log("MODULE", DEBUG, "ConfigReader is deprecated in 2.0; "
+               "use ServerInstance->Config->ConfValue(\"key\") or ->ConfTags(\"key\") instead");
 }
 
 
@@ -591,13 +686,27 @@ void FileReader::CalcSize()
 
 void FileReader::LoadFile(const std::string &filename)
 {
-       file_cache c;
-       c.clear();
-       if (ServerInstance->Config->ReadFile(c,filename.c_str()))
+       std::map<std::string, file_cache>::iterator file = ServerInstance->Config->Files.find(filename);
+       if (file != ServerInstance->Config->Files.end())
        {
-               this->fc = c;
-               this->CalcSize();
+               this->fc = file->second;
+       }
+       else
+       {
+               fc.clear();
+               FILE* f = fopen(filename.c_str(), "r");
+               if (!f)
+                       return;
+               char linebuf[MAXBUF*10];
+               while (fgets(linebuf, sizeof(linebuf), f))
+               {
+                       int len = strlen(linebuf);
+                       if (len)
+                               fc.push_back(std::string(linebuf, len - 1));
+               }
+               fclose(f);
        }
+       CalcSize();
 }