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)
54 Module::Module() : refcount(1) { }
55 CullResult Module::cull()
57 return classbase::cull();
62 ServerInstance->Logs->Log("MODULE", DEFAULT, "References remain to destructed module " + ModuleSourceFile);
65 ModResult Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
66 void Module::OnUserConnect(LocalUser*) { }
67 void Module::OnUserQuit(User*, const std::string&, const std::string&) { }
68 void Module::OnUserDisconnect(LocalUser*) { }
69 void Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
70 void Module::OnPostJoin(Membership*) { }
71 void Module::OnUserPart(Membership*, std::string&, CUList&) { }
72 void Module::OnPreRehash(User*, const std::string&) { }
73 void Module::OnModuleRehash(User*, const std::string&) { }
74 void Module::OnRehash(User*) { }
75 ModResult Module::OnUserPreJoin(User*, Channel*, const char*, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
76 void Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
77 void Module::OnOper(User*, const std::string&) { }
78 void Module::OnPostOper(User*, const std::string&, const std::string &) { }
79 void Module::OnInfo(User*) { }
80 void Module::OnWhois(User*, User*) { }
81 ModResult Module::OnUserPreInvite(User*, User*, Channel*, time_t) { return MOD_RES_PASSTHRU; }
82 ModResult Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
83 ModResult Module::OnUserPreNotice(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
84 ModResult Module::OnUserPreNick(User*, const std::string&) { return MOD_RES_PASSTHRU; }
85 void Module::OnUserPostNick(User*, const std::string&) { }
86 ModResult Module::OnPreMode(User*, User*, Channel*, const std::vector<std::string>&) { return MOD_RES_PASSTHRU; }
87 void Module::On005Numeric(std::string&) { }
88 ModResult Module::OnKill(User*, User*, const std::string&) { return MOD_RES_PASSTHRU; }
89 void Module::OnLoadModule(Module*) { }
90 void Module::OnUnloadModule(Module*) { }
91 void Module::OnBackgroundTimer(time_t) { }
92 ModResult Module::OnPreCommand(std::string&, std::vector<std::string>&, User *, bool, const std::string&) { return MOD_RES_PASSTHRU; }
93 void Module::OnPostCommand(const std::string&, const std::vector<std::string>&, User *, CmdResult, const std::string&) { }
94 ModResult Module::OnCheckReady(LocalUser*) { return MOD_RES_PASSTHRU; }
95 ModResult Module::OnUserRegister(LocalUser*) { return MOD_RES_PASSTHRU; }
96 ModResult Module::OnUserPreKick(User*, Membership*, const std::string&) { return MOD_RES_PASSTHRU; }
97 void Module::OnUserKick(User*, Membership*, const std::string&, CUList&) { }
98 ModResult Module::OnRawMode(User*, Channel*, const char, const std::string &, bool, int) { return MOD_RES_PASSTHRU; }
99 ModResult Module::OnCheckInvite(User*, Channel*) { return MOD_RES_PASSTHRU; }
100 ModResult Module::OnCheckKey(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
101 ModResult Module::OnCheckLimit(User*, Channel*) { return MOD_RES_PASSTHRU; }
102 ModResult Module::OnCheckChannelBan(User*, Channel*) { return MOD_RES_PASSTHRU; }
103 ModResult Module::OnCheckBan(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
104 ModResult Module::OnExtBanCheck(User*, Channel*, char) { return MOD_RES_PASSTHRU; }
105 ModResult Module::OnStats(char, User*, string_list&) { return MOD_RES_PASSTHRU; }
106 ModResult Module::OnChangeLocalUserHost(LocalUser*, const std::string&) { return MOD_RES_PASSTHRU; }
107 ModResult Module::OnChangeLocalUserGECOS(LocalUser*, const std::string&) { return MOD_RES_PASSTHRU; }
108 ModResult Module::OnPreTopicChange(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
109 void Module::OnEvent(Event&) { }
110 void Module::OnRequest(Request&) { }
111 ModResult Module::OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype) { return MOD_RES_PASSTHRU; }
112 void Module::OnGlobalOper(User*) { }
113 void Module::OnPostConnect(User*) { }
114 ModResult Module::OnAddBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
115 ModResult Module::OnDelBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
116 void Module::OnStreamSocketAccept(StreamSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { }
117 int Module::OnStreamSocketWrite(StreamSocket*, std::string&) { return -1; }
118 void Module::OnStreamSocketClose(StreamSocket*) { }
119 void Module::OnStreamSocketConnect(StreamSocket*) { }
120 int Module::OnStreamSocketRead(StreamSocket*, std::string&) { return -1; }
121 void Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&) { }
122 void Module::OnUserNotice(User*, void*, int, const std::string&, char, const CUList&) { }
123 void Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
124 void Module::OnUserInvite(User*, User*, Channel*, time_t) { }
125 void Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
126 void Module::OnGetServerDescription(const std::string&, std::string&) { }
127 void Module::OnSyncUser(User*, Module*, void*) { }
128 void Module::OnSyncChannel(Channel*, Module*, void*) { }
129 void Module::OnSyncNetwork(Module*, void*) { }
130 void Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
131 void Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
132 void Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
133 void Module::OnWallops(User*, const std::string&) { }
134 void Module::OnChangeHost(User*, const std::string&) { }
135 void Module::OnChangeName(User*, const std::string&) { }
136 void Module::OnChangeIdent(User*, const std::string&) { }
137 void Module::OnAddLine(User*, XLine*) { }
138 void Module::OnDelLine(User*, XLine*) { }
139 void Module::OnExpireLine(XLine*) { }
140 void Module::OnCleanup(int, void*) { }
141 ModResult Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
142 void Module::OnChannelDelete(Channel*) { }
143 ModResult Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
144 ModResult Module::OnUserList(User*, Channel*) { return MOD_RES_PASSTHRU; }
145 ModResult Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
146 void Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
147 void Module::OnGarbageCollect() { }
148 void Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
149 void Module::OnRunTestSuite() { }
150 void Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
151 ModResult Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
152 void Module::OnHookIO(StreamSocket*, ListenSocket*) { }
153 ModResult Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { return MOD_RES_PASSTHRU; }
154 void Module::OnSendWhoLine(User*, User*, Channel*, std::string&) { }
155 ModResult Module::OnChannelRestrictionApply(User*, Channel*, const char*) { return MOD_RES_PASSTHRU; }
157 ModuleManager::ModuleManager() : ModCount(0)
161 ModuleManager::~ModuleManager()
165 bool ModuleManager::Attach(Implementation i, Module* mod)
167 if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
170 EventHandlers[i].push_back(mod);
174 bool ModuleManager::Detach(Implementation i, Module* mod)
176 EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
178 if (x == EventHandlers[i].end())
181 EventHandlers[i].erase(x);
185 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
187 for (size_t n = 0; n < sz; ++n)
191 void ModuleManager::DetachAll(Module* mod)
193 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
194 Detach((Implementation)n, mod);
197 bool ModuleManager::SetPriority(Module* mod, Priority s)
199 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
200 SetPriority(mod, (Implementation)n, s);
205 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module** modules, size_t sz)
207 /** To change the priority of a module, we first find its position in the vector,
208 * then we find the position of the other modules in the vector that this module
209 * wants to be before/after. We pick off either the first or last of these depending
210 * on which they want, and we make sure our module is *at least* before or after
211 * the first or last of this subset, depending again on the type of priority.
218 /* Locate our module. This is O(n) but it only occurs on module load so we're
219 * not too bothered about it
221 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
223 if (EventHandlers[i][x] == mod)
231 /* Eh? this module doesnt exist, probably trying to set priority on an event
232 * theyre not attached to.
240 case PRIORITY_DONTCARE:
243 /* Module wants to be first, sod everything else */
245 if (prioritizationState != PRIO_STATE_FIRST)
250 /* Module wants to be last. */
252 if (prioritizationState != PRIO_STATE_FIRST)
254 else if (EventHandlers[i].empty())
257 swap_pos = EventHandlers[i].size() - 1;
259 /* Place this module after a set of other modules */
262 /* Find the latest possible position */
265 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
267 for (size_t n = 0; n < sz; ++n)
269 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x >= swap_pos) && (source <= swap_pos))
278 /* Place this module before a set of other modules */
279 case PRIORITY_BEFORE:
281 swap_pos = EventHandlers[i].size() - 1;
283 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
285 for (size_t n = 0; n < sz; ++n)
287 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x <= swap_pos) && (source >= swap_pos))
298 /* Do we need to swap? */
299 if (swap && (swap_pos != source))
301 // We are going to change positions; we'll need to run again to verify all requirements
302 if (prioritizationState == PRIO_STATE_LAST)
303 prioritizationState = PRIO_STATE_AGAIN;
304 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
307 if (source > swap_pos)
310 for (unsigned int j = source; j != swap_pos; j += incrmnt)
312 if (( j + incrmnt > EventHandlers[i].size() - 1) || (j + incrmnt < 0))
315 std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
322 std::string& ModuleManager::LastError()
324 return LastModuleError;
327 bool ModuleManager::Load(const char* filename)
329 /* Don't allow people to specify paths for modules, it doesn't work as expected */
330 if (strchr(filename, '/'))
332 /* Do we have a glob pattern in the filename?
333 * The user wants to load multiple modules which
336 if (strchr(filename,'*') || (strchr(filename,'?')))
339 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
342 /* Try and locate and load all modules matching the pattern */
343 dirent* entry = NULL;
344 while (0 != (entry = readdir(library)))
346 if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
348 if (!this->Load(entry->d_name))
354 /* Loadmodule will now return false if any one of the modules failed
355 * to load (but wont abort when it encounters a bad one) and when 1 or
356 * more modules were actually loaded.
358 return (n_match > 0 ? false : true);
361 char modfile[MAXBUF];
362 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
363 std::string filename_str = filename;
365 if (!ServerConfig::FileExists(modfile))
367 LastModuleError = "Module file could not be found: " + filename_str;
368 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
372 if (Modules.find(filename_str) != Modules.end())
374 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
375 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
379 Module* newmod = NULL;
380 DLLManager* newhandle = new DLLManager(modfile);
384 newmod = newhandle->callInit();
388 newmod->ModuleSourceFile = filename_str;
389 newmod->ModuleDLLManager = newhandle;
390 Version v = newmod->GetVersion();
392 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
393 filename, v.version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
395 Modules[filename_str] = newmod;
399 LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
400 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
405 catch (CoreException& modexcept)
407 // failure in module constructor
410 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
411 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
416 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
418 /* We give every module a chance to re-prioritize when we introduce a new one,
419 * not just the one thats loading, as the new module could affect the preference
422 for(int tries = 0; tries < 20; tries++)
424 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
425 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
426 n->second->Prioritize();
428 if (prioritizationState == PRIO_STATE_LAST)
431 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
434 ServerInstance->BuildISupport();
438 bool ModuleManager::CanUnload(Module* mod)
440 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
442 if (modfind == Modules.end() || modfind->second != mod)
444 LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
445 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
448 if (mod->GetVersion().Flags & VF_STATIC)
450 LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
451 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
454 std::pair<int,std::string> intercount = GetInterfaceInstanceCount(mod);
455 if (intercount.first > 0)
457 LastModuleError = "Failed to unload module " + mod->ModuleSourceFile + ", being used by " + ConvToStr(intercount.first) + " other(s) via interface '" + intercount.second + "'";
458 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
464 void ModuleManager::DoSafeUnload(Module* mod)
466 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
468 std::vector<reference<ExtensionItem> > items;
469 ServerInstance->Extensions.BeginUnregister(modfind->second, items);
470 /* Give the module a chance to tidy out all its metadata */
471 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
473 mod->OnCleanup(TYPE_CHANNEL,c->second);
474 c->second->doUnhookExtensions(items);
475 const UserMembList* users = c->second->GetUsers();
476 for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
477 mi->second->doUnhookExtensions(items);
479 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
481 mod->OnCleanup(TYPE_USER,u->second);
482 u->second->doUnhookExtensions(items);
484 for(char m='A'; m <= 'z'; m++)
487 mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
488 if (mh && mh->creator == mod)
489 ServerInstance->Modes->DelMode(mh);
490 mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
491 if (mh && mh->creator == mod)
492 ServerInstance->Modes->DelMode(mh);
495 /* Tidy up any dangling resolvers */
496 ServerInstance->Res->CleanResolvers(mod);
498 FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
502 Modules.erase(modfind);
503 ServerInstance->GlobalCulls.AddItem(mod);
505 ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",mod->ModuleSourceFile.c_str());
507 ServerInstance->BuildISupport();
511 struct UnloadAction : public HandlerBase0<void>
514 UnloadAction(Module* m) : mod(m) {}
517 DLLManager* dll = mod->ModuleDLLManager;
518 ServerInstance->Modules->DoSafeUnload(mod);
519 ServerInstance->GlobalCulls.Apply();
521 ServerInstance->GlobalCulls.AddItem(this);
525 struct ReloadAction : public HandlerBase0<void>
528 HandlerBase1<void, bool>* const callback;
529 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
530 : mod(m), callback(c) {}
533 DLLManager* dll = mod->ModuleDLLManager;
534 std::string name = mod->ModuleSourceFile;
535 ServerInstance->Modules->DoSafeUnload(mod);
536 ServerInstance->GlobalCulls.Apply();
538 bool rv = ServerInstance->Modules->Load(name.c_str());
540 ServerInstance->GlobalCulls.AddItem(this);
545 bool ModuleManager::Unload(Module* mod)
549 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
553 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
556 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
558 callback->Call(false);
561 /* We must load the modules AFTER initializing the socket engine, now */
562 void ModuleManager::LoadAll()
566 printf("\nLoading core commands");
569 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
572 dirent* entry = NULL;
573 while (0 != (entry = readdir(library)))
575 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
580 if (!Load(entry->d_name))
582 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
583 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
584 ServerInstance->Exit(EXIT_STATUS_MODULE);
592 ConfigTagList tags = ServerInstance->Config->ConfTags("module");
593 for(ConfigIter i = tags.first; i != tags.second; ++i)
595 ConfigTag* tag = i->second;
596 std::string name = tag->getString("name");
597 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
599 if (!this->Load(name.c_str()))
601 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
602 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
603 ServerInstance->Exit(EXIT_STATUS_MODULE);
608 void ModuleManager::UnloadAll()
610 /* We do this more than once, so that any service providers get a
611 * chance to be unhooked by the modules using them, but then get
612 * a chance to be removed themsleves.
614 * Note: this deliberately does NOT delete the DLLManager objects
616 for (int tries = 0; tries < 4; tries++)
618 std::map<std::string, Module*>::iterator i = Modules.begin();
619 while (i != Modules.end())
621 std::map<std::string, Module*>::iterator me = i++;
622 if (CanUnload(me->second))
624 DoSafeUnload(me->second);
627 ServerInstance->GlobalCulls.Apply();
631 bool ModuleManager::PublishFeature(const std::string &FeatureName, Module* Mod)
633 if (Features.find(FeatureName) == Features.end())
635 Features[FeatureName] = Mod;
641 bool ModuleManager::UnpublishFeature(const std::string &FeatureName)
643 featurelist::iterator iter = Features.find(FeatureName);
645 if (iter == Features.end())
648 Features.erase(iter);
652 Module* ModuleManager::FindFeature(const std::string &FeatureName)
654 featurelist::iterator iter = Features.find(FeatureName);
656 if (iter == Features.end())
662 bool ModuleManager::PublishInterface(const std::string &InterfaceName, Module* Mod)
664 interfacelist::iterator iter = Interfaces.find(InterfaceName);
666 if (iter == Interfaces.end())
670 Interfaces[InterfaceName] = std::make_pair(0, ml);
674 iter->second.second.push_back(Mod);
679 bool ModuleManager::UnpublishInterface(const std::string &InterfaceName, Module* Mod)
681 interfacelist::iterator iter = Interfaces.find(InterfaceName);
683 if (iter == Interfaces.end())
686 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
690 iter->second.second.erase(x);
691 if (iter->second.second.empty())
692 Interfaces.erase(InterfaceName);
699 modulelist* ModuleManager::FindInterface(const std::string &InterfaceName)
701 interfacelist::iterator iter = Interfaces.find(InterfaceName);
702 if (iter == Interfaces.end())
705 return &(iter->second.second);
708 bool ModuleManager::ModuleHasInterface(Module* mod, const std::string& InterfaceName)
710 interfacelist::iterator iter = Interfaces.find(InterfaceName);
711 if (iter == Interfaces.end())
715 modulelist& ml = iter->second.second;
716 modulelist::iterator mi = std::find(ml.begin(), ml.end(), mod);
717 return (mi != ml.end());
721 void ModuleManager::UseInterface(const std::string &InterfaceName)
723 interfacelist::iterator iter = Interfaces.find(InterfaceName);
724 if (iter != Interfaces.end())
725 iter->second.first++;
729 void ModuleManager::DoneWithInterface(const std::string &InterfaceName)
731 interfacelist::iterator iter = Interfaces.find(InterfaceName);
732 if (iter != Interfaces.end())
733 iter->second.first--;
736 std::pair<int,std::string> ModuleManager::GetInterfaceInstanceCount(Module* m)
738 for (interfacelist::iterator iter = Interfaces.begin(); iter != Interfaces.end(); iter++)
740 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
744 return std::make_pair(iter->second.first, iter->first);
748 return std::make_pair(0, "");
751 const std::string& ModuleManager::GetModuleName(Module* m)
753 static std::string nothing;
755 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
764 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user)
766 return this->Parser->CallHandler(commandname, parameters, user);
769 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, User* user)
771 return this->Parser->IsValidCommand(commandname, pcnt, user);
774 void InspIRCd::AddCommand(Command *f)
776 if (!this->Parser->AddCommand(f))
778 throw ModuleException("Command "+std::string(f->command)+" already exists.");
782 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
784 this->Modes->Process(parameters, user);
787 bool InspIRCd::AddResolver(Resolver* r, bool cached)
790 return this->Res->AddResolverClass(r);
793 r->TriggerCachedResult();
799 Module* ModuleManager::Find(const std::string &name)
801 std::map<std::string, Module*>::iterator modfind = Modules.find(name);
803 if (modfind == Modules.end())
806 return modfind->second;
809 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
811 std::vector<std::string> retval;
812 for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
813 if (!filter || (x->second->GetVersion().Flags & filter))
814 retval.push_back(x->first);
818 ConfigReader::ConfigReader()
824 ConfigReader::~ConfigReader()
828 static ConfigTag* SlowGetTag(const std::string &tag, int index)
830 ConfigTagList tags = ServerInstance->Config->ConfTags(tag);
831 while (tags.first != tags.second)
834 return tags.first->second;
841 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
843 std::string result = default_value;
844 if (!SlowGetTag(tag, index)->readString(name, result, allow_linefeeds))
846 this->error = CONF_VALUE_NOT_FOUND;
851 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
853 return ReadValue(tag, name, "", index, allow_linefeeds);
856 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
858 bool def = (default_value == "yes");
859 return SlowGetTag(tag, index)->getBool(name, def);
862 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
864 return ReadFlag(tag, name, "", index);
868 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
870 int v = atoi(default_value.c_str());
871 int result = SlowGetTag(tag, index)->getInt(name, v);
873 if ((need_positive) && (result < 0))
875 this->error = CONF_INT_NEGATIVE;
882 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
884 return ReadInteger(tag, name, "", index, need_positive);
887 long ConfigReader::GetError()
889 long olderr = this->error;
894 int ConfigReader::Enumerate(const std::string &tag)
896 ServerInstance->Logs->Log("MODULE", DEBUG, "Module is using ConfigReader::Enumerate on %s; this is slow!",
899 while (SlowGetTag(tag, i)) i++;
903 FileReader::FileReader(const std::string &filename)
908 FileReader::FileReader()
912 std::string FileReader::Contents()
915 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
923 unsigned long FileReader::ContentSize()
925 return this->contentsize;
928 void FileReader::CalcSize()
931 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
932 n += (a->length() + 2);
933 this->contentsize = n;
936 void FileReader::LoadFile(const std::string &filename)
940 if (ServerInstance->Config->ReadFile(c,filename.c_str()))
948 FileReader::~FileReader()
952 bool FileReader::Exists()
954 return (!(fc.size() == 0));
957 std::string FileReader::GetLine(int x)
959 if ((x<0) || ((unsigned)x>fc.size()))
964 int FileReader::FileSize()