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();
61 ModResult Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
62 void Module::OnUserConnect(User*) { }
63 void Module::OnUserQuit(User*, const std::string&, const std::string&) { }
64 void Module::OnUserDisconnect(User*) { }
65 void Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
66 void Module::OnPostJoin(Membership*) { }
67 void Module::OnUserPart(Membership*, std::string&, CUList&) { }
68 void Module::OnPreRehash(User*, const std::string&) { }
69 void Module::OnModuleRehash(User*, const std::string&) { }
70 void Module::OnRehash(User*) { }
71 ModResult Module::OnUserPreJoin(User*, Channel*, const char*, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
72 void Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
73 void Module::OnOper(User*, const std::string&) { }
74 void Module::OnPostOper(User*, const std::string&, const std::string &) { }
75 void Module::OnInfo(User*) { }
76 void Module::OnWhois(User*, User*) { }
77 ModResult Module::OnUserPreInvite(User*, User*, Channel*, time_t) { return MOD_RES_PASSTHRU; }
78 ModResult Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
79 ModResult Module::OnUserPreNotice(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
80 ModResult Module::OnUserPreNick(User*, const std::string&) { return MOD_RES_PASSTHRU; }
81 void Module::OnUserPostNick(User*, const std::string&) { }
82 ModResult Module::OnPreMode(User*, User*, Channel*, const std::vector<std::string>&) { return MOD_RES_PASSTHRU; }
83 void Module::On005Numeric(std::string&) { }
84 ModResult Module::OnKill(User*, User*, const std::string&) { return MOD_RES_PASSTHRU; }
85 void Module::OnLoadModule(Module*) { }
86 void Module::OnUnloadModule(Module*) { }
87 void Module::OnBackgroundTimer(time_t) { }
88 ModResult Module::OnPreCommand(std::string&, std::vector<std::string>&, User *, bool, const std::string&) { return MOD_RES_PASSTHRU; }
89 void Module::OnPostCommand(const std::string&, const std::vector<std::string>&, User *, CmdResult, const std::string&) { }
90 ModResult Module::OnCheckReady(User*) { return MOD_RES_PASSTHRU; }
91 ModResult Module::OnUserRegister(User*) { return MOD_RES_PASSTHRU; }
92 ModResult Module::OnUserPreKick(User*, Membership*, const std::string&) { return MOD_RES_PASSTHRU; }
93 void Module::OnUserKick(User*, Membership*, const std::string&, CUList&) { }
94 ModResult Module::OnRawMode(User*, Channel*, const char, const std::string &, bool, int) { return MOD_RES_PASSTHRU; }
95 ModResult Module::OnCheckInvite(User*, Channel*) { return MOD_RES_PASSTHRU; }
96 ModResult Module::OnCheckKey(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
97 ModResult Module::OnCheckLimit(User*, Channel*) { return MOD_RES_PASSTHRU; }
98 ModResult Module::OnCheckChannelBan(User*, Channel*) { return MOD_RES_PASSTHRU; }
99 ModResult Module::OnCheckBan(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
100 ModResult Module::OnExtBanCheck(User*, Channel*, char) { return MOD_RES_PASSTHRU; }
101 ModResult Module::OnStats(char, User*, string_list&) { return MOD_RES_PASSTHRU; }
102 ModResult Module::OnChangeLocalUserHost(User*, const std::string&) { return MOD_RES_PASSTHRU; }
103 ModResult Module::OnChangeLocalUserGECOS(User*, const std::string&) { return MOD_RES_PASSTHRU; }
104 ModResult Module::OnPreTopicChange(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
105 void Module::OnEvent(Event&) { }
106 void Module::OnRequest(Request&) { }
107 ModResult Module::OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype) { return MOD_RES_PASSTHRU; }
108 void Module::OnGlobalOper(User*) { }
109 void Module::OnPostConnect(User*) { }
110 ModResult Module::OnAddBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
111 ModResult Module::OnDelBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
112 void Module::OnStreamSocketAccept(StreamSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { }
113 int Module::OnStreamSocketWrite(StreamSocket*, std::string&) { return -1; }
114 void Module::OnStreamSocketClose(StreamSocket*) { }
115 void Module::OnStreamSocketConnect(StreamSocket*) { }
116 int Module::OnStreamSocketRead(StreamSocket*, std::string&) { return -1; }
117 void Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&) { }
118 void Module::OnUserNotice(User*, void*, int, const std::string&, char, const CUList&) { }
119 void Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
120 void Module::OnUserInvite(User*, User*, Channel*, time_t) { }
121 void Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
122 void Module::OnGetServerDescription(const std::string&, std::string&) { }
123 void Module::OnSyncUser(User*, Module*, void*) { }
124 void Module::OnSyncChannel(Channel*, Module*, void*) { }
125 void Module::OnSyncNetwork(Module*, void*) { }
126 void Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
127 void Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
128 void Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
129 void Module::OnWallops(User*, const std::string&) { }
130 void Module::OnChangeHost(User*, const std::string&) { }
131 void Module::OnChangeName(User*, const std::string&) { }
132 void Module::OnChangeIdent(User*, const std::string&) { }
133 void Module::OnAddLine(User*, XLine*) { }
134 void Module::OnDelLine(User*, XLine*) { }
135 void Module::OnExpireLine(XLine*) { }
136 void Module::OnCleanup(int, void*) { }
137 ModResult Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
138 void Module::OnChannelDelete(Channel*) { }
139 ModResult Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
140 ModResult Module::OnUserList(User*, Channel*) { return MOD_RES_PASSTHRU; }
141 ModResult Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
142 void Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
143 void Module::OnGarbageCollect() { }
144 void Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
145 void Module::OnRunTestSuite() { }
146 void Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
147 ModResult Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
148 void Module::OnHookIO(StreamSocket*, ListenSocketBase*) { }
149 void Module::OnSendWhoLine(User*, User*, Channel*, std::string&) { }
150 ModResult Module::OnChannelRestrictionApply(User*, Channel*, const char*) { return MOD_RES_PASSTHRU; }
152 ModuleManager::ModuleManager() : ModCount(0)
156 ModuleManager::~ModuleManager()
160 bool ModuleManager::Attach(Implementation i, Module* mod)
162 if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
165 EventHandlers[i].push_back(mod);
169 bool ModuleManager::Detach(Implementation i, Module* mod)
171 EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
173 if (x == EventHandlers[i].end())
176 EventHandlers[i].erase(x);
180 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
182 for (size_t n = 0; n < sz; ++n)
186 void ModuleManager::DetachAll(Module* mod)
188 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
189 Detach((Implementation)n, mod);
192 bool ModuleManager::SetPriority(Module* mod, Priority s)
194 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
195 SetPriority(mod, (Implementation)n, s);
200 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module** modules, size_t sz)
202 /** To change the priority of a module, we first find its position in the vector,
203 * then we find the position of the other modules in the vector that this module
204 * wants to be before/after. We pick off either the first or last of these depending
205 * on which they want, and we make sure our module is *at least* before or after
206 * the first or last of this subset, depending again on the type of priority.
213 /* Locate our module. This is O(n) but it only occurs on module load so we're
214 * not too bothered about it
216 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
218 if (EventHandlers[i][x] == mod)
226 /* Eh? this module doesnt exist, probably trying to set priority on an event
227 * theyre not attached to.
235 case PRIORITY_DONTCARE:
238 /* Module wants to be first, sod everything else */
240 if (prioritizationState != PRIO_STATE_FIRST)
245 /* Module wants to be last. */
247 if (prioritizationState != PRIO_STATE_FIRST)
249 else if (EventHandlers[i].empty())
252 swap_pos = EventHandlers[i].size() - 1;
254 /* Place this module after a set of other modules */
257 /* Find the latest possible position */
260 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
262 for (size_t n = 0; n < sz; ++n)
264 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x >= swap_pos) && (source <= swap_pos))
273 /* Place this module before a set of other modules */
274 case PRIORITY_BEFORE:
276 swap_pos = EventHandlers[i].size() - 1;
278 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
280 for (size_t n = 0; n < sz; ++n)
282 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x <= swap_pos) && (source >= swap_pos))
293 /* Do we need to swap? */
294 if (swap && (swap_pos != source))
296 // We are going to change positions; we'll need to run again to verify all requirements
297 if (prioritizationState == PRIO_STATE_LAST)
298 prioritizationState = PRIO_STATE_AGAIN;
299 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
302 if (source > swap_pos)
305 for (unsigned int j = source; j != swap_pos; j += incrmnt)
307 if (( j + incrmnt > EventHandlers[i].size() - 1) || (j + incrmnt < 0))
310 std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
317 std::string& ModuleManager::LastError()
319 return LastModuleError;
322 bool ModuleManager::Load(const char* filename)
324 /* Don't allow people to specify paths for modules, it doesn't work as expected */
325 if (strchr(filename, '/'))
327 /* Do we have a glob pattern in the filename?
328 * The user wants to load multiple modules which
331 if (strchr(filename,'*') || (strchr(filename,'?')))
334 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
337 /* Try and locate and load all modules matching the pattern */
338 dirent* entry = NULL;
339 while (0 != (entry = readdir(library)))
341 if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
343 if (!this->Load(entry->d_name))
349 /* Loadmodule will now return false if any one of the modules failed
350 * to load (but wont abort when it encounters a bad one) and when 1 or
351 * more modules were actually loaded.
353 return (n_match > 0 ? false : true);
356 char modfile[MAXBUF];
357 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
358 std::string filename_str = filename;
360 if (!ServerConfig::FileExists(modfile))
362 LastModuleError = "Module file could not be found: " + filename_str;
363 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
367 if (Modules.find(filename_str) != Modules.end())
369 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
370 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
374 Module* newmod = NULL;
375 DLLManager* newhandle = new DLLManager(modfile);
379 newmod = newhandle->callInit();
383 newmod->ModuleSourceFile = filename_str;
384 newmod->ModuleDLLManager = newhandle;
385 Version v = newmod->GetVersion();
387 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (Module version %s)%s",
388 filename, v.version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
390 Modules[filename_str] = newmod;
394 LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
395 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
400 catch (CoreException& modexcept)
402 // failure in module constructor
405 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
406 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
411 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod));
413 /* We give every module a chance to re-prioritize when we introduce a new one,
414 * not just the one thats loading, as the new module could affect the preference
417 for(int tries = 0; tries < 20; tries++)
419 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
420 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
421 n->second->Prioritize();
423 if (prioritizationState == PRIO_STATE_LAST)
426 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
429 ServerInstance->BuildISupport();
433 bool ModuleManager::CanUnload(Module* mod)
435 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
437 if (modfind == Modules.end() || modfind->second != mod)
439 LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
440 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
443 if (mod->GetVersion().Flags & VF_STATIC)
445 LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
446 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
449 std::pair<int,std::string> intercount = GetInterfaceInstanceCount(mod);
450 if (intercount.first > 0)
452 LastModuleError = "Failed to unload module " + mod->ModuleSourceFile + ", being used by " + ConvToStr(intercount.first) + " other(s) via interface '" + intercount.second + "'";
453 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
459 void ModuleManager::DoSafeUnload(Module* mod)
461 std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
463 std::vector<reference<ExtensionItem> > items;
464 ServerInstance->Extensions.BeginUnregister(modfind->second, items);
465 /* Give the module a chance to tidy out all its metadata */
466 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
468 mod->OnCleanup(TYPE_CHANNEL,c->second);
469 c->second->doUnhookExtensions(items);
470 const UserMembList* users = c->second->GetUsers();
471 for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
472 mi->second->doUnhookExtensions(items);
474 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
476 mod->OnCleanup(TYPE_USER,u->second);
477 u->second->doUnhookExtensions(items);
479 for(char m='A'; m < 'z'; m++)
482 mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
483 if (mh && mh->creator == mod)
484 ServerInstance->Modes->DelMode(mh);
485 mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
486 if (mh && mh->creator == mod)
487 ServerInstance->Modes->DelMode(mh);
490 /* Tidy up any dangling resolvers */
491 ServerInstance->Res->CleanResolvers(mod);
493 FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
497 Modules.erase(modfind);
498 ServerInstance->GlobalCulls.AddItem(mod);
500 ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",mod->ModuleSourceFile.c_str());
502 ServerInstance->BuildISupport();
506 struct UnloadAction : public HandlerBase0<void>
509 UnloadAction(Module* m) : mod(m) {}
512 DLLManager* dll = mod->ModuleDLLManager;
513 ServerInstance->Modules->DoSafeUnload(mod);
514 ServerInstance->GlobalCulls.Apply();
516 ServerInstance->GlobalCulls.AddItem(this);
520 struct ReloadAction : public HandlerBase0<void>
523 HandlerBase1<void, bool>* const callback;
524 ReloadAction(Module* m, HandlerBase1<void, bool>* c)
525 : mod(m), callback(c) {}
528 DLLManager* dll = mod->ModuleDLLManager;
529 std::string name = mod->ModuleSourceFile;
530 ServerInstance->Modules->DoSafeUnload(mod);
531 ServerInstance->GlobalCulls.Apply();
533 bool rv = ServerInstance->Modules->Load(name.c_str());
535 ServerInstance->GlobalCulls.AddItem(this);
540 bool ModuleManager::Unload(Module* mod)
544 ServerInstance->AtomicActions.AddAction(new UnloadAction(mod));
548 void ModuleManager::Reload(Module* mod, HandlerBase1<void, bool>* callback)
551 ServerInstance->AtomicActions.AddAction(new ReloadAction(mod, callback));
553 callback->Call(false);
556 /* We must load the modules AFTER initializing the socket engine, now */
557 void ModuleManager::LoadAll()
561 printf("\nLoading core commands");
564 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
567 dirent* entry = NULL;
568 while (0 != (entry = readdir(library)))
570 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
575 if (!Load(entry->d_name))
577 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
578 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
579 ServerInstance->Exit(EXIT_STATUS_MODULE);
587 for(int count = 0;; count++)
589 ConfigTag* tag = ServerInstance->Config->ConfValue("module", count);
592 std::string name = tag->getString("name");
593 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",name.c_str());
595 if (!this->Load(name.c_str()))
597 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
598 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
599 ServerInstance->Exit(EXIT_STATUS_MODULE);
604 void ModuleManager::UnloadAll()
606 /* We do this more than once, so that any service providers get a
607 * chance to be unhooked by the modules using them, but then get
608 * a chance to be removed themsleves.
610 * Note: this deliberately does NOT delete the DLLManager objects
612 for (int tries = 0; tries < 4; tries++)
614 std::map<std::string, Module*>::iterator i = Modules.begin();
615 while (i != Modules.end())
617 std::map<std::string, Module*>::iterator me = i++;
618 if (CanUnload(me->second))
620 DoSafeUnload(me->second);
623 ServerInstance->GlobalCulls.Apply();
627 bool ModuleManager::PublishFeature(const std::string &FeatureName, Module* Mod)
629 if (Features.find(FeatureName) == Features.end())
631 Features[FeatureName] = Mod;
637 bool ModuleManager::UnpublishFeature(const std::string &FeatureName)
639 featurelist::iterator iter = Features.find(FeatureName);
641 if (iter == Features.end())
644 Features.erase(iter);
648 Module* ModuleManager::FindFeature(const std::string &FeatureName)
650 featurelist::iterator iter = Features.find(FeatureName);
652 if (iter == Features.end())
658 bool ModuleManager::PublishInterface(const std::string &InterfaceName, Module* Mod)
660 interfacelist::iterator iter = Interfaces.find(InterfaceName);
662 if (iter == Interfaces.end())
666 Interfaces[InterfaceName] = std::make_pair(0, ml);
670 iter->second.second.push_back(Mod);
675 bool ModuleManager::UnpublishInterface(const std::string &InterfaceName, Module* Mod)
677 interfacelist::iterator iter = Interfaces.find(InterfaceName);
679 if (iter == Interfaces.end())
682 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
686 iter->second.second.erase(x);
687 if (iter->second.second.empty())
688 Interfaces.erase(InterfaceName);
695 modulelist* ModuleManager::FindInterface(const std::string &InterfaceName)
697 interfacelist::iterator iter = Interfaces.find(InterfaceName);
698 if (iter == Interfaces.end())
701 return &(iter->second.second);
704 bool ModuleManager::ModuleHasInterface(Module* mod, const std::string& InterfaceName)
706 interfacelist::iterator iter = Interfaces.find(InterfaceName);
707 if (iter == Interfaces.end())
711 modulelist& ml = iter->second.second;
712 modulelist::iterator mi = std::find(ml.begin(), ml.end(), mod);
713 return (mi != ml.end());
717 void ModuleManager::UseInterface(const std::string &InterfaceName)
719 interfacelist::iterator iter = Interfaces.find(InterfaceName);
720 if (iter != Interfaces.end())
721 iter->second.first++;
725 void ModuleManager::DoneWithInterface(const std::string &InterfaceName)
727 interfacelist::iterator iter = Interfaces.find(InterfaceName);
728 if (iter != Interfaces.end())
729 iter->second.first--;
732 std::pair<int,std::string> ModuleManager::GetInterfaceInstanceCount(Module* m)
734 for (interfacelist::iterator iter = Interfaces.begin(); iter != Interfaces.end(); iter++)
736 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
740 return std::make_pair(iter->second.first, iter->first);
744 return std::make_pair(0, "");
747 const std::string& ModuleManager::GetModuleName(Module* m)
749 static std::string nothing;
751 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
760 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user)
762 return this->Parser->CallHandler(commandname, parameters, user);
765 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, User* user)
767 return this->Parser->IsValidCommand(commandname, pcnt, user);
770 void InspIRCd::AddCommand(Command *f)
772 if (!this->Parser->AddCommand(f))
774 throw ModuleException("Command "+std::string(f->command)+" already exists.");
778 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
780 this->Modes->Process(parameters, user);
783 void InspIRCd::DumpText(User* user, const std::string &text)
791 PI->PushToClient(user, text);
795 void InspIRCd::DumpText(User* user, const char *text, ...)
800 va_start(argsPtr, text);
801 vsnprintf(line, MAXBUF, text, argsPtr);
804 DumpText(user, std::string(line));
807 void InspIRCd::DumpText(User* user, const std::string &LinePrefix, std::stringstream &TextStream)
810 int start_pos = LinePrefix.length();
812 memcpy(line, LinePrefix.data(), pos);
814 while (TextStream >> Word)
816 int len = Word.length();
817 if (pos + len + 12 > MAXBUF)
820 DumpText(user, std::string(line));
824 memcpy(line + pos + 1, Word.data(), len);
828 DumpText(user, std::string(line));
831 bool InspIRCd::AddResolver(Resolver* r, bool cached)
834 return this->Res->AddResolverClass(r);
837 r->TriggerCachedResult();
843 Module* ModuleManager::Find(const std::string &name)
845 std::map<std::string, Module*>::iterator modfind = Modules.find(name);
847 if (modfind == Modules.end())
850 return modfind->second;
853 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
855 std::vector<std::string> retval;
856 for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
857 if (!filter || (x->second->GetVersion().Flags & filter))
858 retval.push_back(x->first);
862 ConfigReader::ConfigReader()
868 ConfigReader::~ConfigReader()
873 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
875 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
876 std::string result = default_value;
877 if (!ServerInstance->Config->ConfValue(tag, index)->readString(name, result, allow_linefeeds))
879 this->error = CONF_VALUE_NOT_FOUND;
884 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
886 return ReadValue(tag, name, "", index, allow_linefeeds);
889 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
891 bool def = (default_value == "yes");
892 return ServerInstance->Config->ConfValue(tag, index)->getBool(name, def);
895 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
897 return ReadFlag(tag, name, "", index);
901 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
903 int v = atoi(default_value.c_str());
904 int result = ServerInstance->Config->ConfValue(tag, index)->getInt(name, v);
906 if ((need_positive) && (result < 0))
908 this->error = CONF_INT_NEGATIVE;
915 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
917 return ReadInteger(tag, name, "", index, need_positive);
920 long ConfigReader::GetError()
922 long olderr = this->error;
927 int ConfigReader::Enumerate(const std::string &tag)
929 ServerInstance->Logs->Log("MODULE", DEBUG, "Module is using ConfigReader::Enumerate on %s; this is slow!",
932 while (ServerInstance->Config->ConfValue(tag, i)) i++;
936 FileReader::FileReader(const std::string &filename)
941 FileReader::FileReader()
945 std::string FileReader::Contents()
948 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
956 unsigned long FileReader::ContentSize()
958 return this->contentsize;
961 void FileReader::CalcSize()
964 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
965 n += (a->length() + 2);
966 this->contentsize = n;
969 void FileReader::LoadFile(const std::string &filename)
973 if (ServerInstance->Config->ReadFile(c,filename.c_str()))
981 FileReader::~FileReader()
985 bool FileReader::Exists()
987 return (!(fc.size() == 0));
990 std::string FileReader::GetLine(int x)
992 if ((x<0) || ((unsigned)x>fc.size()))
997 int FileReader::FileSize()