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 * ---------------------------------------------------
19 #include "socketengine.h"
20 #include "command_parse.h"
22 #include "exitcodes.h"
29 // version is a simple class for holding a modules version number
30 Version::Version(const std::string &modv, int flags, int api_ver, const std::string& rev)
31 : description(modv), version(rev), Flags(flags), API(api_ver)
35 Request::Request(char* anydata, Module* src, Module* dst)
36 : data(anydata), source(src), dest(dst)
38 /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
39 * by passing them uninitialized pointers (could happen)
44 Request::Request(Module* src, Module* dst, const char* idstr)
45 : id(idstr), source(src), dest(dst)
49 char* Request::GetData()
54 const char* Request::GetId()
59 Module* Request::GetSource()
64 Module* Request::GetDest()
69 const char* Request::Send()
73 return dest->OnRequest(this);
81 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { }
83 char* Event::GetData()
88 Module* Event::GetSource()
95 FOREACH_MOD(I_OnEvent,OnEvent(this));
99 std::string Event::GetEventID()
105 // These declarations define the behavours of the base class Module (which does nothing at all)
110 ServerInstance->GlobalCulls.AddItem(ModuleDLLFactory);
113 Module::~Module() { }
115 ModResult Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
116 void Module::OnUserConnect(User*) { }
117 void Module::OnUserQuit(User*, const std::string&, const std::string&) { }
118 void Module::OnUserDisconnect(User*) { }
119 void Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
120 void Module::OnPostJoin(Membership*) { }
121 void Module::OnUserPart(Membership*, std::string&, CUList&) { }
122 void Module::OnPreRehash(User*, const std::string&) { }
123 void Module::OnModuleRehash(User*, const std::string&) { }
124 void Module::OnRehash(User*) { }
125 ModResult Module::OnUserPreJoin(User*, Channel*, const char*, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
126 void Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
127 void Module::OnOper(User*, const std::string&) { }
128 void Module::OnPostOper(User*, const std::string&, const std::string &) { }
129 void Module::OnInfo(User*) { }
130 void Module::OnWhois(User*, User*) { }
131 ModResult Module::OnUserPreInvite(User*, User*, Channel*, time_t) { return MOD_RES_PASSTHRU; }
132 ModResult Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
133 ModResult Module::OnUserPreNotice(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
134 ModResult Module::OnUserPreNick(User*, const std::string&) { return MOD_RES_PASSTHRU; }
135 void Module::OnUserPostNick(User*, const std::string&) { }
136 ModResult Module::OnPreMode(User*, User*, Channel*, const std::vector<std::string>&) { return MOD_RES_PASSTHRU; }
137 void Module::On005Numeric(std::string&) { }
138 ModResult Module::OnKill(User*, User*, const std::string&) { return MOD_RES_PASSTHRU; }
139 void Module::OnLoadModule(Module*, const std::string&) { }
140 void Module::OnUnloadModule(Module*, const std::string&) { }
141 void Module::OnBackgroundTimer(time_t) { }
142 ModResult Module::OnPreCommand(std::string&, std::vector<std::string>&, User *, bool, const std::string&) { return MOD_RES_PASSTHRU; }
143 void Module::OnPostCommand(const std::string&, const std::vector<std::string>&, User *, CmdResult, const std::string&) { }
144 ModResult Module::OnCheckReady(User*) { return MOD_RES_PASSTHRU; }
145 ModResult Module::OnUserRegister(User*) { return MOD_RES_PASSTHRU; }
146 ModResult Module::OnUserPreKick(User*, Membership*, const std::string&) { return MOD_RES_PASSTHRU; }
147 void Module::OnUserKick(User*, Membership*, const std::string&, CUList&) { }
148 ModResult Module::OnRawMode(User*, Channel*, const char, const std::string &, bool, int) { return MOD_RES_PASSTHRU; }
149 ModResult Module::OnCheckInvite(User*, Channel*) { return MOD_RES_PASSTHRU; }
150 ModResult Module::OnCheckKey(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
151 ModResult Module::OnCheckLimit(User*, Channel*) { return MOD_RES_PASSTHRU; }
152 ModResult Module::OnCheckChannelBan(User*, Channel*) { return MOD_RES_PASSTHRU; }
153 ModResult Module::OnCheckBan(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
154 ModResult Module::OnExtBanCheck(User*, Channel*, char) { return MOD_RES_PASSTHRU; }
155 ModResult Module::OnStats(char, User*, string_list&) { return MOD_RES_PASSTHRU; }
156 ModResult Module::OnChangeLocalUserHost(User*, const std::string&) { return MOD_RES_PASSTHRU; }
157 ModResult Module::OnChangeLocalUserGECOS(User*, const std::string&) { return MOD_RES_PASSTHRU; }
158 ModResult Module::OnPreTopicChange(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
159 void Module::OnEvent(Event*) { return; }
160 const char* Module::OnRequest(Request*) { return NULL; }
161 ModResult Module::OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype) { return MOD_RES_PASSTHRU; }
162 void Module::OnGlobalOper(User*) { }
163 void Module::OnPostConnect(User*) { }
164 ModResult Module::OnAddBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
165 ModResult Module::OnDelBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
166 void Module::OnStreamSocketAccept(StreamSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { }
167 int Module::OnStreamSocketWrite(StreamSocket*, std::string&) { return -1; }
168 void Module::OnStreamSocketClose(StreamSocket*) { }
169 void Module::OnStreamSocketConnect(StreamSocket*) { }
170 int Module::OnStreamSocketRead(StreamSocket*, std::string&) { return -1; }
171 void Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&) { }
172 void Module::OnUserNotice(User*, void*, int, const std::string&, char, const CUList&) { }
173 void Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
174 void Module::OnUserInvite(User*, User*, Channel*, time_t) { }
175 void Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
176 void Module::OnGetServerDescription(const std::string&, std::string&) { }
177 void Module::OnSyncUser(User*, Module*, void*) { }
178 void Module::OnSyncChannel(Channel*, Module*, void*) { }
179 void Module::OnSyncNetwork(Module*, void*) { }
180 void Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
181 void Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
182 void Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
183 void Module::OnWallops(User*, const std::string&) { }
184 void Module::OnChangeHost(User*, const std::string&) { }
185 void Module::OnChangeName(User*, const std::string&) { }
186 void Module::OnChangeIdent(User*, const std::string&) { }
187 void Module::OnAddLine(User*, XLine*) { }
188 void Module::OnDelLine(User*, XLine*) { }
189 void Module::OnExpireLine(XLine*) { }
190 void Module::OnCleanup(int, void*) { }
191 ModResult Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
192 void Module::OnChannelDelete(Channel*) { }
193 ModResult Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
194 ModResult Module::OnUserList(User*, Channel*) { return MOD_RES_PASSTHRU; }
195 ModResult Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
196 void Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
197 void Module::OnGarbageCollect() { }
198 void Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
199 void Module::OnRunTestSuite() { }
200 void Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
201 ModResult Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
202 void Module::OnHookIO(StreamSocket*, ListenSocketBase*) { }
203 void Module::OnSendWhoLine(User*, User*, Channel*, std::string&) { }
205 ModuleManager::ModuleManager() : ModCount(0)
209 ModuleManager::~ModuleManager()
213 bool ModuleManager::Attach(Implementation i, Module* mod)
215 if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
218 EventHandlers[i].push_back(mod);
222 bool ModuleManager::Detach(Implementation i, Module* mod)
224 EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
226 if (x == EventHandlers[i].end())
229 EventHandlers[i].erase(x);
233 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
235 for (size_t n = 0; n < sz; ++n)
239 void ModuleManager::DetachAll(Module* mod)
241 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
242 Detach((Implementation)n, mod);
245 bool ModuleManager::SetPriority(Module* mod, Priority s)
247 for (size_t n = I_BEGIN + 1; n != I_END; ++n)
248 SetPriority(mod, (Implementation)n, s);
253 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module** modules, size_t sz)
255 /** To change the priority of a module, we first find its position in the vector,
256 * then we find the position of the other modules in the vector that this module
257 * wants to be before/after. We pick off either the first or last of these depending
258 * on which they want, and we make sure our module is *at least* before or after
259 * the first or last of this subset, depending again on the type of priority.
266 /* Locate our module. This is O(n) but it only occurs on module load so we're
267 * not too bothered about it
269 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
271 if (EventHandlers[i][x] == mod)
279 /* Eh? this module doesnt exist, probably trying to set priority on an event
280 * theyre not attached to.
288 case PRIORITY_DONTCARE:
291 /* Module wants to be first, sod everything else */
293 if (prioritizationState != PRIO_STATE_FIRST)
298 /* Module wants to be last. */
300 if (prioritizationState != PRIO_STATE_FIRST)
302 else if (EventHandlers[i].empty())
305 swap_pos = EventHandlers[i].size() - 1;
307 /* Place this module after a set of other modules */
310 /* Find the latest possible position */
313 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
315 for (size_t n = 0; n < sz; ++n)
317 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x >= swap_pos) && (source <= swap_pos))
326 /* Place this module before a set of other modules */
327 case PRIORITY_BEFORE:
329 swap_pos = EventHandlers[i].size() - 1;
331 for (size_t x = 0; x != EventHandlers[i].size(); ++x)
333 for (size_t n = 0; n < sz; ++n)
335 if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x <= swap_pos) && (source >= swap_pos))
346 /* Do we need to swap? */
347 if (swap && (swap_pos != source))
349 // We are going to change positions; we'll need to run again to verify all requirements
350 if (prioritizationState == PRIO_STATE_LAST)
351 prioritizationState = PRIO_STATE_AGAIN;
352 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
355 if (source > swap_pos)
358 for (unsigned int j = source; j != swap_pos; j += incrmnt)
360 if (( j + incrmnt > EventHandlers[i].size() - 1) || (j + incrmnt < 0))
363 std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
370 std::string& ModuleManager::LastError()
372 return LastModuleError;
375 bool ModuleManager::Load(const char* filename)
377 /* Do we have a glob pattern in the filename?
378 * The user wants to load multiple modules which
381 if (strchr(filename,'*') || (strchr(filename,'?')))
384 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
387 /* Try and locate and load all modules matching the pattern */
388 dirent* entry = NULL;
389 while (0 != (entry = readdir(library)))
391 if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
393 if (!this->Load(entry->d_name))
399 /* Loadmodule will now return false if any one of the modules failed
400 * to load (but wont abort when it encounters a bad one) and when 1 or
401 * more modules were actually loaded.
403 return (n_match > 0 ? false : true);
406 char modfile[MAXBUF];
407 snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
408 std::string filename_str = filename;
410 if (!ServerConfig::FileExists(modfile))
412 LastModuleError = "Module file could not be found: " + filename_str;
413 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
417 if (Modules.find(filename_str) != Modules.end())
419 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
420 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
424 Module* newmod = NULL;
425 DLLFactory* newhandle = NULL;
429 /* This will throw a CoreException if there's a problem loading
430 * the module file or getting a pointer to the init_module symbol.
432 newhandle = new DLLFactory(modfile, "init_module");
433 if (newhandle->init_func)
434 newmod = newhandle->init_func();
438 newmod->ModuleSourceFile = filename_str;
439 newmod->ModuleDLLFactory = newhandle;
440 Version v = newmod->GetVersion();
442 if (v.API != API_VERSION)
447 LastModuleError = "Unable to load " + filename_str + ": Incorrect module API version: " + ConvToStr(v.API) + " (our version: " + ConvToStr(API_VERSION) + ")";
448 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
453 ServerInstance->Logs->Log("MODULE", DEFAULT,"New module introduced: %s (API version %d, Module version %s)%s", filename, v.API, v.version.c_str(), (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
456 Modules[filename_str] = newmod;
461 LastModuleError = "Unable to load " + filename_str + ": Probably missing init_module() entrypoint, but dlsym() didn't notice a problem";
462 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
466 /** XXX: Is there anything we can do about this mess? -- Brain */
467 catch (LoadModuleException& modexcept)
474 LastModuleError = "Unable to load " + filename_str + ": Error when loading: " + modexcept.GetReason();
475 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
478 catch (FindSymbolException& modexcept)
485 LastModuleError = "Unable to load " + filename_str + ": Error finding symbol: " + modexcept.GetReason();
486 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
489 catch (CoreException& modexcept)
496 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
497 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
502 FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod, filename_str));
504 /* We give every module a chance to re-prioritize when we introduce a new one,
505 * not just the one thats loading, as the new module could affect the preference
508 for(int tries = 0; tries < 20; tries++)
510 prioritizationState = tries > 0 ? PRIO_STATE_LAST : PRIO_STATE_FIRST;
511 for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
512 n->second->Prioritize();
514 if (prioritizationState == PRIO_STATE_LAST)
517 ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
520 ServerInstance->BuildISupport();
524 bool ModuleManager::Unload(const char* filename)
526 std::string filename_str(filename);
527 std::map<std::string, Module*>::iterator modfind = Modules.find(filename);
529 if (modfind != Modules.end())
531 if (modfind->second->GetVersion().Flags & VF_STATIC)
533 LastModuleError = "Module " + filename_str + " not unloadable (marked static)";
534 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
537 std::pair<int,std::string> intercount = GetInterfaceInstanceCount(modfind->second);
538 if (intercount.first > 0)
540 LastModuleError = "Failed to unload module " + filename_str + ", being used by " + ConvToStr(intercount.first) + " other(s) via interface '" + intercount.second + "'";
541 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
545 std::vector<ExtensionItem*> items = Extensible::BeginUnregister(modfind->second);
546 /* Give the module a chance to tidy out all its metadata */
547 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
549 modfind->second->OnCleanup(TYPE_CHANNEL,c->second);
550 c->second->doUnhookExtensions(items);
551 const UserMembList* users = c->second->GetUsers();
552 for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
553 mi->second->doUnhookExtensions(items);
555 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
557 modfind->second->OnCleanup(TYPE_USER,u->second);
558 u->second->doUnhookExtensions(items);
561 /* Tidy up any dangling resolvers */
562 ServerInstance->Res->CleanResolvers(modfind->second);
564 FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(modfind->second, modfind->first));
566 this->DetachAll(modfind->second);
568 ServerInstance->Parser->RemoveCommands(modfind->second);
569 ServerInstance->Modes->RemoveModes(modfind->second);
571 ServerInstance->GlobalCulls.AddItem(modfind->second);
572 Modules.erase(modfind);
574 ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",filename);
576 ServerInstance->BuildISupport();
580 LastModuleError = "Module " + filename_str + " is not loaded, cannot unload it!";
581 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
585 /* We must load the modules AFTER initializing the socket engine, now */
586 void ModuleManager::LoadAll()
588 char configToken[MAXBUF];
591 printf("\nLoading core commands");
594 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
597 dirent* entry = NULL;
598 while (0 != (entry = readdir(library)))
600 if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
605 if (!Load(entry->d_name))
607 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
608 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
609 ServerInstance->Exit(EXIT_STATUS_MODULE);
617 for(int count = 0; count < ServerInstance->Config->ConfValueEnum("module"); count++)
619 ServerInstance->Config->ConfValue("module", "name", count, configToken, MAXBUF);
620 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
622 if (!this->Load(configToken))
624 ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
625 printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
626 ServerInstance->Exit(EXIT_STATUS_MODULE);
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 /* This is ugly, yes, but hash_map's arent designed to be
765 * addressed in this manner, and this is a bit of a kludge.
766 * Luckily its a specialist function and rarely used by
767 * many modules (in fact, it was specially created to make
768 * m_safelist possible, initially).
771 Channel* InspIRCd::GetChannelIndex(long index)
774 for (chan_hash::iterator n = this->chanlist->begin(); n != this->chanlist->end(); n++, target++)
782 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user)
784 return this->Parser->CallHandler(commandname, parameters, user);
787 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, User* user)
789 return this->Parser->IsValidCommand(commandname, pcnt, user);
792 void InspIRCd::AddCommand(Command *f)
794 if (!this->Parser->CreateCommand(f))
796 ModuleException err("Command "+std::string(f->command)+" already exists.");
801 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
803 this->Modes->Process(parameters, user);
806 void InspIRCd::DumpText(User* user, const std::string &text)
814 PI->PushToClient(user, text);
818 void InspIRCd::DumpText(User* user, const char *text, ...)
823 va_start(argsPtr, text);
824 vsnprintf(line, MAXBUF, text, argsPtr);
827 DumpText(user, std::string(line));
830 void InspIRCd::DumpText(User* user, const std::string &LinePrefix, std::stringstream &TextStream)
833 int start_pos = LinePrefix.length();
835 memcpy(line, LinePrefix.data(), pos);
837 while (TextStream >> Word)
839 int len = Word.length();
840 if (pos + len + 12 > MAXBUF)
843 DumpText(user, std::string(line));
847 memcpy(line + pos + 1, Word.data(), len);
851 DumpText(user, std::string(line));
854 bool InspIRCd::AddResolver(Resolver* r, bool cached)
857 return this->Res->AddResolverClass(r);
860 r->TriggerCachedResult();
866 Module* ModuleManager::Find(const std::string &name)
868 std::map<std::string, Module*>::iterator modfind = Modules.find(name);
870 if (modfind == Modules.end())
873 return modfind->second;
876 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
878 std::vector<std::string> retval;
879 for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
880 if (!filter || (x->second->GetVersion().Flags & filter))
881 retval.push_back(x->first);
885 ConfigReader::ConfigReader()
891 ConfigReader::~ConfigReader()
896 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
898 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
901 if (!ServerInstance->Config->ConfValue(tag, name, default_value, index, result, allow_linefeeds))
903 this->error = CONF_VALUE_NOT_FOUND;
908 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
910 return ReadValue(tag, name, "", index, allow_linefeeds);
913 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
915 return ServerInstance->Config->ConfValueBool(tag, name, default_value, index);
918 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
920 return ReadFlag(tag, name, "", index);
924 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
928 if(!ServerInstance->Config->ConfValueInteger(tag, name, default_value, index, result))
930 this->error = CONF_VALUE_NOT_FOUND;
934 if ((need_positive) && (result < 0))
936 this->error = CONF_INT_NEGATIVE;
943 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
945 return ReadInteger(tag, name, "", index, need_positive);
948 long ConfigReader::GetError()
950 long olderr = this->error;
955 int ConfigReader::Enumerate(const std::string &tag)
957 return ServerInstance->Config->ConfValueEnum(tag);
960 int ConfigReader::EnumerateValues(const std::string &tag, int index)
962 return ServerInstance->Config->ConfVarEnum(tag, index);
965 FileReader::FileReader(const std::string &filename)
970 FileReader::FileReader()
974 std::string FileReader::Contents()
977 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
985 unsigned long FileReader::ContentSize()
987 return this->contentsize;
990 void FileReader::CalcSize()
993 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
994 n += (a->length() + 2);
995 this->contentsize = n;
998 void FileReader::LoadFile(const std::string &filename)
1002 if (ServerInstance->Config->ReadFile(c,filename.c_str()))
1010 FileReader::~FileReader()
1014 bool FileReader::Exists()
1016 return (!(fc.size() == 0));
1019 std::string FileReader::GetLine(int x)
1021 if ((x<0) || ((unsigned)x>fc.size()))
1026 int FileReader::FileSize()