1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2009 InspIRCd Development Team
6 * See: http://wiki.inspircd.org/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
17 #include "socketengine.h"
18 #include "command_parse.h"
20 #include "exitcodes.h"
27 // Version is a simple class for holding a modules version number
29 VersionBase<API_VERSION>::VersionBase(const std::string &modv, int flags, const std::string& rev)
30 : description(modv), version(rev), Flags(flags)
34 Request::Request(Module* src, Module* dst, const char* idstr)
35 : id(idstr), source(src), dest(dst)
42 dest->OnRequest(*this);
45 Event::Event(Module* src, const std::string &eventid) : source(src), id(eventid) { }
49 FOREACH_MOD(I_OnEvent,OnEvent(*this));
52 // These declarations define the behavours of the base class Module (which does nothing at all)
55 CullResult Module::cull()
57 return classbase::cull();
63 ModResult Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
64 void Module::OnUserConnect(LocalUser*) { }
65 void Module::OnUserQuit(User*, const std::string&, const std::string&) { }
66 void Module::OnUserDisconnect(LocalUser*) { }
67 void Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
68 void Module::OnPostJoin(Membership*) { }
69 void Module::OnUserPart(Membership*, std::string&, CUList&) { }
70 void Module::OnPreRehash(User*, const std::string&) { }
71 void Module::OnModuleRehash(User*, const std::string&) { }
72 void Module::OnRehash(User*) { }
73 ModResult Module::OnUserPreJoin(User*, Channel*, const char*, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
74 void Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
75 void Module::OnOper(User*, const std::string&) { }
76 void Module::OnPostOper(User*, const std::string&, const std::string &) { }
77 void Module::OnInfo(User*) { }
78 void Module::OnWhois(User*, User*) { }
79 ModResult Module::OnUserPreInvite(User*, User*, Channel*, time_t) { return MOD_RES_PASSTHRU; }
80 ModResult Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
81 ModResult Module::OnUserPreNotice(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
82 ModResult Module::OnUserPreNick(User*, const std::string&) { return MOD_RES_PASSTHRU; }
83 void Module::OnUserPostNick(User*, const std::string&) { }
84 ModResult Module::OnPreMode(User*, User*, Channel*, const std::vector<std::string>&) { return MOD_RES_PASSTHRU; }
85 void Module::On005Numeric(std::string&) { }
86 ModResult Module::OnKill(User*, User*, const std::string&) { return MOD_RES_PASSTHRU; }
87 void Module::OnLoadModule(Module*) { }
88 void Module::OnUnloadModule(Module*) { }
89 void Module::OnBackgroundTimer(time_t) { }
90 ModResult Module::OnPreCommand(std::string&, std::vector<std::string>&, User *, bool, const std::string&) { return MOD_RES_PASSTHRU; }
91 void Module::OnPostCommand(const std::string&, const std::vector<std::string>&, User *, CmdResult, const std::string&) { }
92 ModResult Module::OnCheckReady(LocalUser*) { return MOD_RES_PASSTHRU; }
93 ModResult Module::OnUserRegister(LocalUser*) { return MOD_RES_PASSTHRU; }
94 ModResult Module::OnUserPreKick(User*, Membership*, const std::string&) { return MOD_RES_PASSTHRU; }
95 void Module::OnUserKick(User*, Membership*, const std::string&, CUList&) { }
96 ModResult Module::OnRawMode(User*, Channel*, const char, const std::string &, bool, int) { return MOD_RES_PASSTHRU; }
97 ModResult Module::OnCheckInvite(User*, Channel*) { return MOD_RES_PASSTHRU; }
98 ModResult Module::OnCheckKey(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
99 ModResult Module::OnCheckLimit(User*, Channel*) { return MOD_RES_PASSTHRU; }
100 ModResult Module::OnCheckChannelBan(User*, Channel*) { return MOD_RES_PASSTHRU; }
101 ModResult Module::OnCheckBan(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
102 ModResult Module::OnExtBanCheck(User*, Channel*, char) { return MOD_RES_PASSTHRU; }
103 ModResult Module::OnStats(char, User*, string_list&) { return MOD_RES_PASSTHRU; }
104 ModResult Module::OnChangeLocalUserHost(LocalUser*, const std::string&) { return MOD_RES_PASSTHRU; }
105 ModResult Module::OnChangeLocalUserGECOS(LocalUser*, const std::string&) { return MOD_RES_PASSTHRU; }
106 ModResult Module::OnPreTopicChange(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
107 void Module::OnEvent(Event&) { }
108 void Module::OnRequest(Request&) { }
109 ModResult Module::OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype) { return MOD_RES_PASSTHRU; }
110 void Module::OnGlobalOper(User*) { }
111 void Module::OnPostConnect(User*) { }
112 ModResult Module::OnAddBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
113 ModResult Module::OnDelBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
114 void Module::OnStreamSocketAccept(StreamSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { }
115 int Module::OnStreamSocketWrite(StreamSocket*, std::string&) { return -1; }
116 void Module::OnStreamSocketClose(StreamSocket*) { }
117 void Module::OnStreamSocketConnect(StreamSocket*) { }
118 int Module::OnStreamSocketRead(StreamSocket*, std::string&) { return -1; }
119 void Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&) { }
120 void Module::OnUserNotice(User*, void*, int, const std::string&, char, const CUList&) { }
121 void Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
122 void Module::OnUserInvite(User*, User*, Channel*, time_t) { }
123 void Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
124 void Module::OnGetServerDescription(const std::string&, std::string&) { }
125 void Module::OnSyncUser(User*, Module*, void*) { }
126 void Module::OnSyncChannel(Channel*, Module*, void*) { }
127 void Module::OnSyncNetwork(Module*, void*) { }
128 void Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
129 void Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
130 void Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
131 void Module::OnWallops(User*, const std::string&) { }
132 void Module::OnChangeHost(User*, const std::string&) { }
133 void Module::OnChangeName(User*, const std::string&) { }
134 void Module::OnChangeIdent(User*, const std::string&) { }
135 void Module::OnAddLine(User*, XLine*) { }
136 void Module::OnDelLine(User*, XLine*) { }
137 void Module::OnExpireLine(XLine*) { }
138 void Module::OnCleanup(int, void*) { }
139 ModResult Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
140 void Module::OnChannelDelete(Channel*) { }
141 ModResult Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
142 ModResult Module::OnUserList(User*, Channel*) { return MOD_RES_PASSTHRU; }
143 ModResult Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
144 void Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
145 void Module::OnGarbageCollect() { }
146 void Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
147 void Module::OnRunTestSuite() { }
148 void Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
149 ModResult Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
150 void Module::OnHookIO(StreamSocket*, ListenSocket*) { }
151 ModResult Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { return MOD_RES_PASSTHRU; }
152 void Module::OnSendWhoLine(User*, User*, Channel*, std::string&) { }
153 ModResult Module::OnChannelRestrictionApply(User*, Channel*, const char*) { return MOD_RES_PASSTHRU; }
155 ModuleManager::ModuleManager() : ModCount(0)
159 ModuleManager::~ModuleManager()
163 bool ModuleManager::Attach(Implementation i, Module* mod)
165 if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
168 EventHandlers[i].push_back(mod);
172 bool ModuleManager::Detach(Implementation i, Module* mod)
174 EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
176 if (x == EventHandlers[i].end())
179 EventHandlers[i].erase(x);
183 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
185 for (size_t n = 0; n < sz; ++n)
189 void ModuleManager::DetachAll(Module* mod)
191 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
192 Detach((Implementation)n, mod);
195 bool ModuleManager::SetPriority(Module* mod, Priority s)
197 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
198 SetPriority(mod, (Implementation)n, s);
203 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module** modules, size_t sz)
205 /** To change the priority of a module, we first find its position in the vector,
206 * then we find the position of the other modules in the vector that this module
207 * wants to be before/after. We pick off either the first or last of these depending
208 * on which they want, and we make sure our module is *at least* before or after
209 * the first or last of this subset, depending again on the type of priority.
216 /* Locate our module. This is O(n) but it only occurs on module load so we're
217 * not too bothered about it
219 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
221 if (EventHandlers[i][x] == mod)
229 /* Eh? this module doesnt exist, probably trying to set priority on an event
230 * theyre not attached to.
238 case PRIORITY_DONTCARE:
241 /* Module wants to be first, sod everything else */
243 if (prioritizationState != PRIO_STATE_FIRST)
248 /* Module wants to be last. */
250 if (prioritizationState != PRIO_STATE_FIRST)
252 else if (EventHandlers[i].empty())
255 swap_pos = EventHandlers[i].size() - 1;
257 /* Place this module after a set of other modules */
260 /* Find the latest possible position */
263 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
265 for (size_t n = 0; n < sz; ++n)
267 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x >= swap_pos) && (source <= swap_pos))
276 /* Place this module before a set of other modules */
277 case PRIORITY_BEFORE:
279 swap_pos = EventHandlers[i].size() - 1;
281 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
283 for (size_t n = 0; n < sz; ++n)
285 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x <= swap_pos) && (source >= swap_pos))
296 /* Do we need to swap? */
297 if (swap && (swap_pos != source))
299 // We are going to change positions; we'll need to run again to verify all requirements
300 if (prioritizationState == PRIO_STATE_LAST)
301 prioritizationState = PRIO_STATE_AGAIN;
302 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
305 if (source > swap_pos)
308 for (unsigned int j = source; j != swap_pos; j += incrmnt)
310 if (( j + incrmnt > EventHandlers[i].size() - 1) || (j + incrmnt < 0))
313 std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
320 std::string& ModuleManager::LastError()
322 return LastModuleError;
325 bool ModuleManager::Load(const char* filename)
327 /* Don't allow people to specify paths for modules, it doesn't work as expected */
328 if (strchr(filename, '/'))
330 /* Do we have a glob pattern in the filename?
331 * The user wants to load multiple modules which
334 if (strchr(filename,'*') || (strchr(filename,'?')))
337 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
340 /* Try and locate and load all modules matching the pattern */
341 dirent* entry = NULL;
342 while (0 != (entry = readdir(library)))
344 if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
346 if (!this->Load(entry->d_name))
352 /* Loadmodule will now return false if any one of the modules failed
353 * to load (but wont abort when it encounters a bad one) and when 1 or
354 * more modules were actually loaded.
356 return (n_match > 0 ? false : true);
359 char modfile[MAXBUF];
360 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
361 std::string filename_str = filename;
363 if (!ServerConfig::FileExists(modfile))
365 LastModuleError = "Module file could not be found: " + filename_str;
366 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
370 if (Modules.find(filename_str) != Modules.end())
372 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
373 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
377 Module* newmod = NULL;
378 DLLManager* newhandle = new DLLManager(modfile);
382 newmod = newhandle->callInit();
386 newmod->ModuleSourceFile = filename_str;
387 newmod->ModuleDLLManager = newhandle;
388 Version v = newmod->GetVersion();
390 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
391 filename, v.version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
393 Modules[filename_str] = newmod;
397 LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
398 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
403 catch (CoreException& modexcept)
405 // failure in module constructor
408 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
409 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
414 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
416 /* We give every module a chance to re-prioritize when we introduce a new one,
417 * not just the one thats loading, as the new module could affect the preference
420 for(int tries = 0; tries < 20; tries++)
422 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
423 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
424 n->second->Prioritize();
426 if (prioritizationState == PRIO_STATE_LAST)
429 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
432 ServerInstance->BuildISupport();
436 bool ModuleManager::CanUnload(Module* mod)
438 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
440 if (modfind == Modules.end() || modfind->second != mod)
442 LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
443 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
446 if (mod->GetVersion().Flags & VF_STATIC)
448 LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
449 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
452 std::pair<int,std::string> intercount = GetInterfaceInstanceCount(mod);
453 if (intercount.first > 0)
455 LastModuleError = "Failed to unload module " + mod->ModuleSourceFile + ", being used by " + ConvToStr(intercount.first) + " other(s) via interface '" + intercount.second + "'";
456 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
462 void ModuleManager::DoSafeUnload(Module* mod)
464 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
466 std::vector<reference<ExtensionItem> > items;
467 ServerInstance->Extensions.BeginUnregister(modfind->second, items);
468 /* Give the module a chance to tidy out all its metadata */
469 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
471 mod->OnCleanup(TYPE_CHANNEL,c->second);
472 c->second->doUnhookExtensions(items);
473 const UserMembList* users = c->second->GetUsers();
474 for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
475 mi->second->doUnhookExtensions(items);
477 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
479 mod->OnCleanup(TYPE_USER,u->second);
480 u->second->doUnhookExtensions(items);
482 for(char m='A'; m <= 'z'; m++)
485 mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
486 if (mh && mh->creator == mod)
487 ServerInstance->Modes->DelMode(mh);
488 mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
489 if (mh && mh->creator == mod)
490 ServerInstance->Modes->DelMode(mh);
493 /* Tidy up any dangling resolvers */
494 ServerInstance->Res->CleanResolvers(mod);
496 FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
500 Modules.erase(modfind);
501 ServerInstance->GlobalCulls.AddItem(mod);
503 ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",mod->ModuleSourceFile.c_str());
505 ServerInstance->BuildISupport();
509 struct UnloadAction : public HandlerBase0<void>
512 UnloadAction(Module* m) : mod(m) {}
515 DLLManager* dll = mod->ModuleDLLManager;
516 ServerInstance->Modules->DoSafeUnload(mod);
517 ServerInstance->GlobalCulls.Apply();
519 ServerInstance->GlobalCulls.AddItem(this);
523 struct ReloadAction : public HandlerBase0<void>
526 HandlerBase1<void, bool>* const callback;
527 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
528 : mod(m), callback(c) {}
531 DLLManager* dll = mod->ModuleDLLManager;
532 std::string name = mod->ModuleSourceFile;
533 ServerInstance->Modules->DoSafeUnload(mod);
534 ServerInstance->GlobalCulls.Apply();
536 bool rv = ServerInstance->Modules->Load(name.c_str());
538 ServerInstance->GlobalCulls.AddItem(this);
543 bool ModuleManager::Unload(Module* mod)
547 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
551 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
554 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
556 callback->Call(false);
559 /* We must load the modules AFTER initializing the socket engine, now */
560 void ModuleManager::LoadAll()
564 printf("\nLoading core commands");
567 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
570 dirent* entry = NULL;
571 while (0 != (entry = readdir(library)))
573 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
578 if (!Load(entry->d_name))
580 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
581 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
582 ServerInstance->Exit(EXIT_STATUS_MODULE);
590 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
591 for(ConfigIter i = tags.first; i != tags.second; ++i)
593 ConfigTag* tag = i->second;
594 std::string name = tag->getString("name");
595 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
597 if (!this->Load(name.c_str()))
599 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
600 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
601 ServerInstance->Exit(EXIT_STATUS_MODULE);
606 void ModuleManager::UnloadAll()
608 /* We do this more than once, so that any service providers get a
609 * chance to be unhooked by the modules using them, but then get
610 * a chance to be removed themsleves.
612 * Note: this deliberately does NOT delete the DLLManager objects
614 for (int tries = 0; tries < 4; tries++)
616 std::map<std::string, Module*>::iterator i = Modules.begin();
617 while (i != Modules.end())
619 std::map<std::string, Module*>::iterator me = i++;
620 if (CanUnload(me->second))
622 DoSafeUnload(me->second);
625 ServerInstance->GlobalCulls.Apply();
629 bool ModuleManager::PublishFeature(const std::string &FeatureName, Module* Mod)
631 if (Features.find(FeatureName) == Features.end())
633 Features[FeatureName] = Mod;
639 bool ModuleManager::UnpublishFeature(const std::string &FeatureName)
641 featurelist::iterator iter = Features.find(FeatureName);
643 if (iter == Features.end())
646 Features.erase(iter);
650 Module* ModuleManager::FindFeature(const std::string &FeatureName)
652 featurelist::iterator iter = Features.find(FeatureName);
654 if (iter == Features.end())
660 bool ModuleManager::PublishInterface(const std::string &InterfaceName, Module* Mod)
662 interfacelist::iterator iter = Interfaces.find(InterfaceName);
664 if (iter == Interfaces.end())
668 Interfaces[InterfaceName] = std::make_pair(0, ml);
672 iter->second.second.push_back(Mod);
677 bool ModuleManager::UnpublishInterface(const std::string &InterfaceName, Module* Mod)
679 interfacelist::iterator iter = Interfaces.find(InterfaceName);
681 if (iter == Interfaces.end())
684 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
688 iter->second.second.erase(x);
689 if (iter->second.second.empty())
690 Interfaces.erase(InterfaceName);
697 modulelist* ModuleManager::FindInterface(const std::string &InterfaceName)
699 interfacelist::iterator iter = Interfaces.find(InterfaceName);
700 if (iter == Interfaces.end())
703 return &(iter->second.second);
706 bool ModuleManager::ModuleHasInterface(Module* mod, const std::string& InterfaceName)
708 interfacelist::iterator iter = Interfaces.find(InterfaceName);
709 if (iter == Interfaces.end())
713 modulelist& ml = iter->second.second;
714 modulelist::iterator mi = std::find(ml.begin(), ml.end(), mod);
715 return (mi != ml.end());
719 void ModuleManager::UseInterface(const std::string &InterfaceName)
721 interfacelist::iterator iter = Interfaces.find(InterfaceName);
722 if (iter != Interfaces.end())
723 iter->second.first++;
727 void ModuleManager::DoneWithInterface(const std::string &InterfaceName)
729 interfacelist::iterator iter = Interfaces.find(InterfaceName);
730 if (iter != Interfaces.end())
731 iter->second.first--;
734 std::pair<int,std::string> ModuleManager::GetInterfaceInstanceCount(Module* m)
736 for (interfacelist::iterator iter = Interfaces.begin(); iter != Interfaces.end(); iter++)
738 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
742 return std::make_pair(iter->second.first, iter->first);
746 return std::make_pair(0, "");
749 const std::string& ModuleManager::GetModuleName(Module* m)
751 static std::string nothing;
753 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
762 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user)
764 return this->Parser->CallHandler(commandname, parameters, user);
767 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, User* user)
769 return this->Parser->IsValidCommand(commandname, pcnt, user);
772 void InspIRCd::AddCommand(Command *f)
774 if (!this->Parser->AddCommand(f))
776 throw ModuleException("Command "+std::string(f->command)+" already exists.");
780 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
782 this->Modes->Process(parameters, user);
785 bool InspIRCd::AddResolver(Resolver* r, bool cached)
788 return this->Res->AddResolverClass(r);
791 r->TriggerCachedResult();
797 Module* ModuleManager::Find(const std::string &name)
799 std::map<std::string, Module*>::iterator modfind = Modules.find(name);
801 if (modfind == Modules.end())
804 return modfind->second;
807 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
809 std::vector<std::string> retval;
810 for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
811 if (!filter || (x->second->GetVersion().Flags & filter))
812 retval.push_back(x->first);
816 ConfigReader::ConfigReader()
822 ConfigReader::~ConfigReader()
826 static ConfigTag* SlowGetTag(const std::string &tag, int index)
828 ConfigTagList tags = ServerInstance->Config->ConfTags(tag);
829 while (tags.first != tags.second)
832 return tags.first->second;
839 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
841 std::string result = default_value;
842 if (!SlowGetTag(tag, index)->readString(name, result, allow_linefeeds))
844 this->error = CONF_VALUE_NOT_FOUND;
849 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
851 return ReadValue(tag, name, "", index, allow_linefeeds);
854 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
856 bool def = (default_value == "yes");
857 return SlowGetTag(tag, index)->getBool(name, def);
860 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
862 return ReadFlag(tag, name, "", index);
866 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
868 int v = atoi(default_value.c_str());
869 int result = SlowGetTag(tag, index)->getInt(name, v);
871 if ((need_positive) && (result < 0))
873 this->error = CONF_INT_NEGATIVE;
880 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
882 return ReadInteger(tag, name, "", index, need_positive);
885 long ConfigReader::GetError()
887 long olderr = this->error;
892 int ConfigReader::Enumerate(const std::string &tag)
894 ServerInstance->Logs->Log("MODULE", DEBUG, "Module is using ConfigReader::Enumerate on %s; this is slow!",
897 while (SlowGetTag(tag, i)) i++;
901 FileReader::FileReader(const std::string &filename)
906 FileReader::FileReader()
910 std::string FileReader::Contents()
913 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
921 unsigned long FileReader::ContentSize()
923 return this->contentsize;
926 void FileReader::CalcSize()
929 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
930 n += (a->length() + 2);
931 this->contentsize = n;
934 void FileReader::LoadFile(const std::string &filename)
938 if (ServerInstance->Config->ReadFile(c,filename.c_str()))
946 FileReader::~FileReader()
950 bool FileReader::Exists()
952 return (!(fc.size() == 0));
955 std::string FileReader::GetLine(int x)
957 if ((x<0) || ((unsigned)x>fc.size()))
962 int FileReader::FileSize()