1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
18 #include "socketengine.h"
19 #include "command_parse.h"
21 #include "exitcodes.h"
27 // version is a simple class for holding a modules version number
28 Version::Version(int major, int minor, int revision, int build, int flags, int api_ver)
29 : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags), API(api_ver)
33 Request::Request(char* anydata, Module* src, Module* dst)
34 : data(anydata), source(src), dest(dst)
36 /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
37 * by passing them uninitialized pointers (could happen)
42 Request::Request(Module* src, Module* dst, const char* idstr)
43 : id(idstr), source(src), dest(dst)
47 char* Request::GetData()
52 const char* Request::GetId()
57 Module* Request::GetSource()
62 Module* Request::GetDest()
71 return dest->OnRequest(this);
79 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { }
81 char* Event::GetData()
83 return (char*)this->data;
86 Module* Event::GetSource()
91 char* Event::Send(InspIRCd* ServerInstance)
93 FOREACH_MOD(I_OnEvent,OnEvent(this));
97 std::string Event::GetEventID()
103 // These declarations define the behavours of the base class Module (which does nothing at all)
105 Module::Module(InspIRCd* Me) : ServerInstance(Me) { }
106 Module::~Module() { }
107 void Module::OnUserConnect(userrec* user) { }
108 void Module::OnUserQuit(userrec* user, const std::string& message, const std::string &oper_message) { }
109 void Module::OnUserDisconnect(userrec* user) { }
110 void Module::OnUserJoin(userrec* user, chanrec* channel, bool &silent) { }
111 void Module::OnPostJoin(userrec* user, chanrec* channel) { }
112 void Module::OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent) { }
113 void Module::OnRehash(userrec* user, const std::string ¶meter) { }
114 void Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
115 int Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs) { return 0; }
116 void Module::OnMode(userrec* user, void* dest, int target_type, const std::string &text) { }
117 Version Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR,-1); }
118 void Module::OnOper(userrec* user, const std::string &opertype) { }
119 void Module::OnPostOper(userrec* user, const std::string &opertype) { }
120 void Module::OnInfo(userrec* user) { }
121 void Module::OnWhois(userrec* source, userrec* dest) { }
122 int Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; }
123 int Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status, CUList &exempt_list) { return 0; }
124 int Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status, CUList &exempt_list) { return 0; }
125 int Module::OnUserPreNick(userrec* user, const std::string &newnick) { return 0; }
126 void Module::OnUserPostNick(userrec* user, const std::string &oldnick) { }
127 int Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; }
128 void Module::On005Numeric(std::string &output) { }
129 int Module::OnKill(userrec* source, userrec* dest, const std::string &reason) { return 0; }
130 void Module::OnLoadModule(Module* mod,const std::string &name) { }
131 void Module::OnUnloadModule(Module* mod,const std::string &name) { }
132 void Module::OnBackgroundTimer(time_t curtime) { }
133 int Module::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line) { return 0; }
134 void Module::OnPostCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, CmdResult result, const std::string &original_line) { }
135 bool Module::OnCheckReady(userrec* user) { return true; }
136 int Module::OnUserRegister(userrec* user) { return 0; }
137 int Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { return 0; }
138 void Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent) { }
139 int Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; }
140 int Module::OnCheckKey(userrec* user, chanrec* chan, const std::string &keygiven) { return 0; }
141 int Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; }
142 int Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; }
143 int Module::OnStats(char symbol, userrec* user, string_list &results) { return 0; }
144 int Module::OnChangeLocalUserHost(userrec* user, const std::string &newhost) { return 0; }
145 int Module::OnChangeLocalUserGECOS(userrec* user, const std::string &newhost) { return 0; }
146 int Module::OnLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { return 0; }
147 void Module::OnEvent(Event* event) { return; }
148 char* Module::OnRequest(Request* request) { return NULL; }
149 int Module::OnOperCompare(const std::string &password, const std::string &input, int tagnumber) { return 0; }
150 void Module::OnGlobalOper(userrec* user) { }
151 void Module::OnPostConnect(userrec* user) { }
152 int Module::OnAddBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; }
153 int Module::OnDelBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; }
154 void Module::OnRawSocketAccept(int fd, const std::string &ip, int localport) { }
155 int Module::OnRawSocketWrite(int fd, const char* buffer, int count) { return 0; }
156 void Module::OnRawSocketClose(int fd) { }
157 void Module::OnRawSocketConnect(int fd) { }
158 int Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; }
159 void Module::OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list) { }
160 void Module::OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list) { }
161 void Module::OnRemoteKill(userrec* source, userrec* dest, const std::string &reason, const std::string &operreason) { }
162 void Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { }
163 void Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { }
164 void Module::OnGetServerDescription(const std::string &servername,std::string &description) { }
165 void Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { }
166 void Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { }
167 void Module::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline) { }
168 void Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, const std::string &extname, bool displayable) { }
169 void Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname, bool displayable) { }
170 void Module::OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable) { }
171 void Module::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { }
172 void Module::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata) { }
173 void Module::OnWallops(userrec* user, const std::string &text) { }
174 void Module::OnChangeHost(userrec* user, const std::string &newhost) { }
175 void Module::OnChangeName(userrec* user, const std::string &gecos) { }
176 void Module::OnAddGLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { }
177 void Module::OnAddZLine(long duration, userrec* source, const std::string &reason, const std::string &ipmask) { }
178 void Module::OnAddKLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { }
179 void Module::OnAddQLine(long duration, userrec* source, const std::string &reason, const std::string &nickmask) { }
180 void Module::OnAddELine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { }
181 void Module::OnDelGLine(userrec* source, const std::string &hostmask) { }
182 void Module::OnDelZLine(userrec* source, const std::string &ipmask) { }
183 void Module::OnDelKLine(userrec* source, const std::string &hostmask) { }
184 void Module::OnDelQLine(userrec* source, const std::string &nickmask) { }
185 void Module::OnDelELine(userrec* source, const std::string &hostmask) { }
186 void Module::OnCleanup(int target_type, void* item) { }
187 void Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; }
188 void Module::OnChannelDelete(chanrec* chan) { }
189 Priority Module::Prioritize() { return PRIORITY_DONTCARE; }
190 void Module::OnSetAway(userrec* user) { }
191 void Module::OnCancelAway(userrec* user) { }
192 int Module::OnUserList(userrec* user, chanrec* Ptr, CUList* &userlist) { return 0; }
193 int Module::OnWhoisLine(userrec* user, userrec* dest, int &numeric, std::string &text) { return 0; }
194 void Module::OnBuildExemptList(MessageType message_type, chanrec* chan, userrec* sender, char status, CUList &exempt_list) { }
195 void Module::OnGarbageCollect() { }
196 void Module::OnBufferFlushed(userrec* user) { }
199 ModuleManager::ModuleManager(InspIRCd* Ins)
200 : ModCount(0), Instance(Ins)
204 ModuleManager::~ModuleManager()
208 const char* ModuleManager::LastError()
213 bool ModuleManager::Load(const char* filename)
215 /* Do we have a glob pattern in the filename?
216 * The user wants to load multiple modules which
219 if (strchr(filename,'*') || (strchr(filename,'?')))
222 DIR* library = opendir(Instance->Config->ModPath);
225 /* Try and locate and load all modules matching the pattern */
226 dirent* entry = NULL;
227 while ((entry = readdir(library)))
229 if (Instance->MatchText(entry->d_name, filename))
231 if (!this->Load(entry->d_name))
237 /* Loadmodule will now return false if any one of the modules failed
238 * to load (but wont abort when it encounters a bad one) and when 1 or
239 * more modules were actually loaded.
241 return (n_match > 0);
244 char modfile[MAXBUF];
245 snprintf(modfile,MAXBUF,"%s/%s",Instance->Config->ModPath,filename);
246 std::string filename_str = filename;
248 if (!ServerConfig::DirValid(modfile))
250 snprintf(MODERR, MAXBUF,"Module %s is not within the modules directory.", modfile);
251 Instance->Log(DEFAULT, MODERR);
255 if (!ServerConfig::FileExists(modfile))
257 snprintf(MODERR,MAXBUF,"Module file could not be found: %s", modfile);
258 Instance->Log(DEFAULT, MODERR);
262 if(find(Instance->Config->module_names.begin(), Instance->Config->module_names.end(), filename_str) != Instance->Config->module_names.end())
264 Instance->Log(DEFAULT,"Module %s is already loaded, cannot load a module twice!",modfile);
265 snprintf(MODERR, MAXBUF, "Module already loaded");
270 ircd_module* newhandle;
277 /* This will throw a CoreException if there's a problem loading
278 * the module file or getting a pointer to the init_module symbol.
280 newhandle = new ircd_module(Instance, modfile, "init_module");
282 handles[this->ModCount+1] = newhandle;
284 newmod = handles[this->ModCount+1]->CallInit();
288 Version v = newmod->GetVersion();
290 if (v.API != API_VERSION)
293 Instance->Log(DEFAULT,"Unable to load %s: Incorrect module API version: %d (our version: %d)",modfile,v.API,API_VERSION);
294 snprintf(MODERR,MAXBUF,"Loader/Linker error: Incorrect module API version: %d (our version: %d)",v.API,API_VERSION);
299 Instance->Log(DEFAULT,"New module introduced: %s (API version %d, Module version %d.%d.%d.%d)%s", filename, v.API, v.Major, v.Minor, v.Revision, v.Build, (!(v.Flags & VF_VENDOR) ? " [3rd Party]" : " [Vendor]"));
302 modules[this->ModCount+1] = newmod;
304 /* save the module and the module's classfactory, if
305 * this isnt done, random crashes can occur :/ */
306 Instance->Config->module_names.push_back(filename);
308 char* x = &Instance->Config->implement_lists[this->ModCount+1][0];
309 for(int t = 0; t < 255; t++)
312 modules[this->ModCount+1]->Implements(x);
314 for(int t = 0; t < 255; t++)
315 Instance->Config->global_implementation[t] += Instance->Config->implement_lists[this->ModCount+1][t];
319 Instance->Log(DEFAULT, "Unable to load %s",modfile);
320 snprintf(MODERR,MAXBUF, "Probably missing init_module() entrypoint, but dlsym() didn't notice a problem");
324 catch (LoadModuleException& modexcept)
326 Instance->Log(DEFAULT,"Unable to load %s: %s", modfile, modexcept.GetReason());
327 snprintf(MODERR,MAXBUF,"Loader/Linker error: %s", modexcept.GetReason());
330 catch (FindSymbolException& modexcept)
332 Instance->Log(DEFAULT,"Unable to load %s: %s", modfile, modexcept.GetReason());
333 snprintf(MODERR,MAXBUF,"Loader/Linker error: %s", modexcept.GetReason());
336 catch (CoreException& modexcept)
338 Instance->Log(DEFAULT,"Unable to load %s: %s",modfile,modexcept.GetReason());
339 snprintf(MODERR,MAXBUF,"Factory function of %s threw an exception: %s", modexcept.GetSource(), modexcept.GetReason());
344 FOREACH_MOD_I(Instance,I_OnLoadModule,OnLoadModule(modules[this->ModCount],filename_str));
345 // now work out which modules, if any, want to move to the back of the queue,
346 // and if they do, move them there.
347 std::vector<std::string> put_to_back;
348 std::vector<std::string> put_to_front;
349 std::map<std::string,std::string> put_before;
350 std::map<std::string,std::string> put_after;
351 for (unsigned int j = 0; j < Instance->Config->module_names.size(); j++)
353 if (modules[j]->Prioritize() == PRIORITY_LAST)
354 put_to_back.push_back(Instance->Config->module_names[j]);
355 else if (modules[j]->Prioritize() == PRIORITY_FIRST)
356 put_to_front.push_back(Instance->Config->module_names[j]);
357 else if ((modules[j]->Prioritize() & 0xFF) == PRIORITY_BEFORE)
358 put_before[Instance->Config->module_names[j]] = Instance->Config->module_names[modules[j]->Prioritize() >> 8];
359 else if ((modules[j]->Prioritize() & 0xFF) == PRIORITY_AFTER)
360 put_after[Instance->Config->module_names[j]] = Instance->Config->module_names[modules[j]->Prioritize() >> 8];
362 for (unsigned int j = 0; j < put_to_back.size(); j++)
363 MoveToLast(put_to_back[j]);
364 for (unsigned int j = 0; j < put_to_front.size(); j++)
365 MoveToFirst(put_to_front[j]);
366 for (std::map<std::string,std::string>::iterator j = put_before.begin(); j != put_before.end(); j++)
367 MoveBefore(j->first,j->second);
368 for (std::map<std::string,std::string>::iterator j = put_after.begin(); j != put_after.end(); j++)
369 MoveAfter(j->first,j->second);
370 Instance->BuildISupport();
374 bool ModuleManager::EraseHandle(unsigned int j)
376 ModuleHandleList::iterator iter;
378 if((j < 0) || (j >= handles.size()))
383 iter = handles.begin() + j;
389 handles.push_back(NULL);
395 bool ModuleManager::EraseModule(unsigned int j)
397 bool success = false;
400 ModuleList::iterator iter;
402 if((j < 0) || (j >= modules.size()))
407 iter = modules.begin() + j;
413 modules.push_back(NULL);
419 std::vector<std::string>::iterator iter;
421 if((j < 0) || (j >= Instance->Config->module_names.size()))
426 iter = Instance->Config->module_names.begin() + j;
428 Instance->Config->module_names.erase(iter);
435 void ModuleManager::MoveTo(std::string modulename,int slot)
437 unsigned int v2 = 256;
439 for (unsigned int v = 0; v < Instance->Config->module_names.size(); v++)
441 if (Instance->Config->module_names[v] == modulename)
443 // found an instance, swap it with the item at the end
448 if ((v2 != (unsigned int)slot) && (v2 < 256))
450 // Swap the module names over
451 Instance->Config->module_names[v2] = Instance->Config->module_names[slot];
452 Instance->Config->module_names[slot] = modulename;
453 // now swap the module factories
454 ircd_module* temp = handles[v2];
455 handles[v2] = handles[slot];
456 handles[slot] = temp;
457 // now swap the module objects
458 Module* temp_module = modules[v2];
459 modules[v2] = modules[slot];
460 modules[slot] = temp_module;
461 // now swap the implement lists (we dont
462 // need to swap the global or recount it)
463 for (int n = 0; n < 255; n++)
465 char x = Instance->Config->implement_lists[v2][n];
466 Instance->Config->implement_lists[v2][n] = Instance->Config->implement_lists[slot][n];
467 Instance->Config->implement_lists[slot][n] = x;
472 void ModuleManager::MoveAfter(std::string modulename, std::string after)
474 for (unsigned int v = 0; v < Instance->Config->module_names.size(); v++)
476 if (Instance->Config->module_names[v] == after)
478 MoveTo(modulename, v);
484 void ModuleManager::MoveBefore(std::string modulename, std::string before)
486 for (unsigned int v = 0; v < Instance->Config->module_names.size(); v++)
488 if (Instance->Config->module_names[v] == before)
492 MoveTo(modulename, v-1);
496 MoveTo(modulename, v);
503 void ModuleManager::MoveToFirst(std::string modulename)
505 MoveTo(modulename,0);
508 void ModuleManager::MoveToLast(std::string modulename)
510 MoveTo(modulename,this->GetCount());
513 bool ModuleManager::Unload(const char* filename)
515 std::string filename_str = filename;
516 for (unsigned int j = 0; j != Instance->Config->module_names.size(); j++)
518 if (Instance->Config->module_names[j] == filename_str)
520 if (modules[j]->GetVersion().Flags & VF_STATIC)
522 Instance->Log(DEFAULT,"Failed to unload STATIC module %s",filename);
523 snprintf(MODERR,MAXBUF,"Module not unloadable (marked static)");
526 std::pair<int,std::string> intercount = GetInterfaceInstanceCount(modules[j]);
527 if (intercount.first > 0)
529 Instance->Log(DEFAULT,"Failed to unload module %s, being used by %d other(s) via interface '%s'",filename, intercount.first, intercount.second.c_str());
530 snprintf(MODERR,MAXBUF,"Module not unloadable (Still in use by %d other module%s which %s using its interface '%s') -- unload dependent modules first!",
532 intercount.first > 1 ? "s" : "",
533 intercount.first > 1 ? "are" : "is",
534 intercount.second.c_str());
537 /* Give the module a chance to tidy out all its metadata */
538 for (chan_hash::iterator c = Instance->chanlist->begin(); c != Instance->chanlist->end(); c++)
540 modules[j]->OnCleanup(TYPE_CHANNEL,c->second);
542 for (user_hash::iterator u = Instance->clientlist->begin(); u != Instance->clientlist->end(); u++)
544 modules[j]->OnCleanup(TYPE_USER,u->second);
547 /* Tidy up any dangling resolvers */
548 Instance->Res->CleanResolvers(modules[j]);
550 FOREACH_MOD_I(Instance,I_OnUnloadModule,OnUnloadModule(modules[j],Instance->Config->module_names[j]));
552 for(int t = 0; t < 255; t++)
554 Instance->Config->global_implementation[t] -= Instance->Config->implement_lists[j][t];
557 /* We have to renumber implement_lists after unload because the module numbers change!
559 for(int j2 = j; j2 < 254; j2++)
561 for(int t = 0; t < 255; t++)
563 Instance->Config->implement_lists[j2][t] = Instance->Config->implement_lists[j2+1][t];
568 Instance->Parser->RemoveCommands(filename);
569 this->EraseModule(j);
570 this->EraseHandle(j);
571 Instance->Log(DEFAULT,"Module %s unloaded",filename);
573 Instance->BuildISupport();
577 Instance->Log(DEFAULT,"Module %s is not loaded, cannot unload it!",filename);
578 snprintf(MODERR,MAXBUF,"Module not loaded");
582 /* We must load the modules AFTER initializing the socket engine, now */
583 void ModuleManager::LoadAll()
585 char configToken[MAXBUF];
586 Instance->Config->module_names.clear();
589 for(int count = 0; count < Instance->Config->ConfValueEnum(Instance->Config->config_data, "module"); count++)
591 Instance->Config->ConfValue(Instance->Config->config_data, "module", "name", count, configToken, MAXBUF);
592 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
594 if (!this->Load(configToken))
596 Instance->Log(DEFAULT,"There was an error loading the module '%s': %s", configToken, this->LastError());
597 printf_c("\n[\033[1;31m*\033[0m] There was an error loading the module '%s': %s\n\n", configToken, this->LastError());
598 Instance->Exit(EXIT_STATUS_MODULE);
601 printf_c("\nA total of \033[1;32m%d\033[0m module%s been loaded.\n", this->GetCount(), this->GetCount() == 1 ? " has" : "s have");
602 Instance->Log(DEFAULT,"Total loaded modules: %d", this->GetCount());
605 long ModuleManager::PriorityAfter(const std::string &modulename)
607 for (unsigned int j = 0; j < Instance->Config->module_names.size(); j++)
609 if (Instance->Config->module_names[j] == modulename)
611 return ((j << 8) | PRIORITY_AFTER);
614 return PRIORITY_DONTCARE;
617 long ModuleManager::PriorityBefore(const std::string &modulename)
619 for (unsigned int j = 0; j < Instance->Config->module_names.size(); j++)
621 if (Instance->Config->module_names[j] == modulename)
623 return ((j << 8) | PRIORITY_BEFORE);
626 return PRIORITY_DONTCARE;
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);
673 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 void ModuleManager::UseInterface(const std::string &InterfaceName)
710 interfacelist::iterator iter = Interfaces.find(InterfaceName);
711 if (iter != Interfaces.end())
712 iter->second.first++;
716 void ModuleManager::DoneWithInterface(const std::string &InterfaceName)
718 interfacelist::iterator iter = Interfaces.find(InterfaceName);
719 if (iter != Interfaces.end())
720 iter->second.first--;
723 std::pair<int,std::string> ModuleManager::GetInterfaceInstanceCount(Module* m)
725 for (interfacelist::iterator iter = Interfaces.begin(); iter != Interfaces.end(); iter++)
727 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
731 return std::make_pair(iter->second.first, iter->first);
735 return std::make_pair(0, "");
738 const std::string& ModuleManager::GetModuleName(Module* m)
740 static std::string nothing; /* Prevent compiler warning */
742 if (!this->GetCount())
745 for (int i = 0; i <= this->GetCount(); i++)
747 if (this->modules[i] == m)
749 return Instance->Config->module_names[i];
752 return nothing; /* As above */
755 /* This is ugly, yes, but hash_map's arent designed to be
756 * addressed in this manner, and this is a bit of a kludge.
757 * Luckily its a specialist function and rarely used by
758 * many modules (in fact, it was specially created to make
759 * m_safelist possible, initially).
762 chanrec* InspIRCd::GetChannelIndex(long index)
765 for (chan_hash::iterator n = this->chanlist->begin(); n != this->chanlist->end(); n++, target++)
773 bool InspIRCd::MatchText(const std::string &sliteral, const std::string &spattern)
775 return match(sliteral.c_str(),spattern.c_str());
778 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
780 return this->Parser->CallHandler(commandname,parameters,pcnt,user);
783 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
785 return this->Parser->IsValidCommand(commandname, pcnt, user);
788 void InspIRCd::AddCommand(command_t *f)
790 if (!this->Parser->CreateCommand(f))
792 ModuleException err("Command "+std::string(f->command)+" already exists.");
797 void InspIRCd::SendMode(const char** parameters, int pcnt, userrec *user)
799 this->Modes->Process(parameters,pcnt,user,true);
802 void InspIRCd::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
804 std::string CompleteLine = LinePrefix;
806 while (TextStream >> Word)
808 if (CompleteLine.length() + Word.length() + 3 > 500)
810 User->WriteServ(CompleteLine);
811 CompleteLine = LinePrefix;
813 CompleteLine = CompleteLine + Word + " ";
815 User->WriteServ(CompleteLine);
818 userrec* FindDescriptorHandler::Call(int socket)
820 return reinterpret_cast<userrec*>(Server->SE->GetRef(socket));
823 bool InspIRCd::AddMode(ModeHandler* mh, const unsigned char mode)
825 return this->Modes->AddMode(mh,mode);
828 bool InspIRCd::AddModeWatcher(ModeWatcher* mw)
830 return this->Modes->AddModeWatcher(mw);
833 bool InspIRCd::DelModeWatcher(ModeWatcher* mw)
835 return this->Modes->DelModeWatcher(mw);
838 bool InspIRCd::AddResolver(Resolver* r, bool cached)
841 return this->Res->AddResolverClass(r);
844 r->TriggerCachedResult();
850 void InspIRCd::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
852 XLines->add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
853 XLines->apply_lines(APPLY_GLINES);
856 void InspIRCd::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
858 XLines->add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
859 XLines->apply_lines(APPLY_QLINES);
862 void InspIRCd::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
864 XLines->add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
865 XLines->apply_lines(APPLY_ZLINES);
868 void InspIRCd::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
870 XLines->add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
871 XLines->apply_lines(APPLY_KLINES);
874 void InspIRCd::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
876 XLines->add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
879 bool InspIRCd::DelGLine(const std::string &hostmask)
881 return XLines->del_gline(hostmask.c_str());
884 bool InspIRCd::DelQLine(const std::string &nickname)
886 return XLines->del_qline(nickname.c_str());
889 bool InspIRCd::DelZLine(const std::string &ipaddr)
891 return XLines->del_zline(ipaddr.c_str());
894 bool InspIRCd::DelKLine(const std::string &hostmask)
896 return XLines->del_kline(hostmask.c_str());
899 bool InspIRCd::DelELine(const std::string &hostmask)
901 return XLines->del_eline(hostmask.c_str());
905 * XXX why on *earth* is this in modules.cpp...? I think
906 * perhaps we need a server.cpp for InspIRCd:: stuff where possible. -- w00t
908 bool InspIRCd::IsValidMask(const std::string &mask)
910 char* dest = (char*)mask.c_str();
914 for (char* i = dest; *i; i++)
916 /* out of range character, bad mask */
917 if (*i < 32 || *i > 126)
933 /* valid masks only have 1 ! and @ */
934 if (exclamation != 1 || atsign != 1)
940 Module* ModuleManager::Find(const std::string &name)
942 for (int i = 0; i <= this->GetCount(); i++)
944 if (Instance->Config->module_names[i] == name)
946 return this->modules[i];
952 ConfigReader::ConfigReader(InspIRCd* Instance) : ServerInstance(Instance)
954 /* Is there any reason to load the entire config file again here?
955 * it's needed if they specify another config file, but using the
956 * default one we can just use the global config data - pre-parsed!
958 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
960 this->data = &ServerInstance->Config->config_data;
961 this->privatehash = false;
965 ConfigReader::~ConfigReader()
968 delete this->errorlog;
969 if(this->privatehash)
974 ConfigReader::ConfigReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
976 ServerInstance->Config->ClearStack();
978 this->data = new ConfigDataHash;
979 this->privatehash = true;
980 this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
981 this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
982 if (!this->readerror)
983 this->error = CONF_FILE_NOT_FOUND;
987 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
989 /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
992 if (!ServerInstance->Config->ConfValue(*this->data, tag, name, default_value, index, result, allow_linefeeds))
994 this->error = CONF_VALUE_NOT_FOUND;
999 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
1001 return ReadValue(tag, name, "", index, allow_linefeeds);
1004 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
1006 return ServerInstance->Config->ConfValueBool(*this->data, tag, name, default_value, index);
1009 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
1011 return ReadFlag(tag, name, "", index);
1015 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool needs_unsigned)
1019 if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, default_value, index, result))
1021 this->error = CONF_VALUE_NOT_FOUND;
1025 if ((needs_unsigned) && (result < 0))
1027 this->error = CONF_NOT_UNSIGNED;
1034 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
1036 return ReadInteger(tag, name, "", index, needs_unsigned);
1039 long ConfigReader::GetError()
1041 long olderr = this->error;
1046 void ConfigReader::DumpErrors(bool bail, userrec* user)
1048 ServerInstance->Config->ReportConfigError(this->errorlog->str(), bail, user);
1052 int ConfigReader::Enumerate(const std::string &tag)
1054 return ServerInstance->Config->ConfValueEnum(*this->data, tag);
1057 int ConfigReader::EnumerateValues(const std::string &tag, int index)
1059 return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
1062 bool ConfigReader::Verify()
1064 return this->readerror;
1068 FileReader::FileReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
1073 FileReader::FileReader(InspIRCd* Instance) : ServerInstance(Instance)
1077 std::string FileReader::Contents()
1080 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
1088 unsigned long FileReader::ContentSize()
1090 return this->contentsize;
1093 void FileReader::CalcSize()
1095 unsigned long n = 0;
1096 for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
1097 n += (a->length() + 2);
1098 this->contentsize = n;
1101 void FileReader::LoadFile(const std::string &filename)
1105 if (ServerInstance->Config->ReadFile(c,filename.c_str()))
1113 FileReader::~FileReader()
1117 bool FileReader::Exists()
1119 return (!(fc.size() == 0));
1122 std::string FileReader::GetLine(int x)
1124 if ((x<0) || ((unsigned)x>fc.size()))
1129 int FileReader::FileSize()