]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
8c0fccba6d91fd7239d6cc33121a07da6e94e588
[user/henk/code/inspircd.git] / src / modules.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 /* $Core */
15
16 #include "inspircd.h"
17 #include "xline.h"
18 #include "socket.h"
19 #include "socketengine.h"
20 #include "command_parse.h"
21 #include "dns.h"
22 #include "exitcodes.h"
23
24 #ifndef WIN32
25         #include <dirent.h>
26 #endif
27
28
29 // version is a simple class for holding a modules version number
30 template<>
31 VersionBase<API_VERSION>::VersionBase(const std::string &modv, int flags, int, const std::string& rev)
32 : description(modv), version(rev), Flags(flags)
33 {
34 }
35
36 Request::Request(Module* src, Module* dst, const char* idstr)
37 : id(idstr), source(src), dest(dst)
38 {
39 }
40
41 void Request::Send()
42 {
43         if (dest)
44                 dest->OnRequest(*this);
45 }
46
47 Event::Event(Module* src, const std::string &eventid) : source(src), id(eventid) { }
48
49 void Event::Send()
50 {
51         FOREACH_MOD(I_OnEvent,OnEvent(*this));
52 }
53
54 // These declarations define the behavours of the base class Module (which does nothing at all)
55
56 Module::Module() { }
57 bool Module::cull()
58 {
59         ServerInstance->GlobalCulls.AddItem(ModuleDLLManager);
60         return true;
61 }
62 Module::~Module() { }
63
64 ModResult       Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
65 void            Module::OnUserConnect(User*) { }
66 void            Module::OnUserQuit(User*, const std::string&, const std::string&) { }
67 void            Module::OnUserDisconnect(User*) { }
68 void            Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
69 void            Module::OnPostJoin(Membership*) { }
70 void            Module::OnUserPart(Membership*, std::string&, CUList&) { }
71 void            Module::OnPreRehash(User*, const std::string&) { }
72 void            Module::OnModuleRehash(User*, const std::string&) { }
73 void            Module::OnRehash(User*) { }
74 ModResult       Module::OnUserPreJoin(User*, Channel*, const char*, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
75 void            Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
76 void            Module::OnOper(User*, const std::string&) { }
77 void            Module::OnPostOper(User*, const std::string&, const std::string &) { }
78 void            Module::OnInfo(User*) { }
79 void            Module::OnWhois(User*, User*) { }
80 ModResult       Module::OnUserPreInvite(User*, User*, Channel*, time_t) { return MOD_RES_PASSTHRU; }
81 ModResult       Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
82 ModResult       Module::OnUserPreNotice(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
83 ModResult       Module::OnUserPreNick(User*, const std::string&) { return MOD_RES_PASSTHRU; }
84 void            Module::OnUserPostNick(User*, const std::string&) { }
85 ModResult       Module::OnPreMode(User*, User*, Channel*, const std::vector<std::string>&) { return MOD_RES_PASSTHRU; }
86 void            Module::On005Numeric(std::string&) { }
87 ModResult       Module::OnKill(User*, User*, const std::string&) { return MOD_RES_PASSTHRU; }
88 void            Module::OnLoadModule(Module*, const std::string&) { }
89 void            Module::OnUnloadModule(Module*, const std::string&) { }
90 void            Module::OnBackgroundTimer(time_t) { }
91 ModResult       Module::OnPreCommand(std::string&, std::vector<std::string>&, User *, bool, const std::string&) { return MOD_RES_PASSTHRU; }
92 void            Module::OnPostCommand(const std::string&, const std::vector<std::string>&, User *, CmdResult, const std::string&) { }
93 ModResult       Module::OnCheckReady(User*) { return MOD_RES_PASSTHRU; }
94 ModResult       Module::OnUserRegister(User*) { return MOD_RES_PASSTHRU; }
95 ModResult       Module::OnUserPreKick(User*, Membership*, const std::string&) { return MOD_RES_PASSTHRU; }
96 void            Module::OnUserKick(User*, Membership*, const std::string&, CUList&) { }
97 ModResult       Module::OnRawMode(User*, Channel*, const char, const std::string &, bool, int) { return MOD_RES_PASSTHRU; }
98 ModResult       Module::OnCheckInvite(User*, Channel*) { return MOD_RES_PASSTHRU; }
99 ModResult       Module::OnCheckKey(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
100 ModResult       Module::OnCheckLimit(User*, Channel*) { return MOD_RES_PASSTHRU; }
101 ModResult       Module::OnCheckChannelBan(User*, Channel*) { return MOD_RES_PASSTHRU; }
102 ModResult       Module::OnCheckBan(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
103 ModResult       Module::OnExtBanCheck(User*, Channel*, char) { return MOD_RES_PASSTHRU; }
104 ModResult       Module::OnStats(char, User*, string_list&) { return MOD_RES_PASSTHRU; }
105 ModResult       Module::OnChangeLocalUserHost(User*, const std::string&) { return MOD_RES_PASSTHRU; }
106 ModResult       Module::OnChangeLocalUserGECOS(User*, const std::string&) { return MOD_RES_PASSTHRU; }
107 ModResult       Module::OnPreTopicChange(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
108 void            Module::OnEvent(Event&) { }
109 void            Module::OnRequest(Request&) { }
110 ModResult       Module::OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype) { return MOD_RES_PASSTHRU; }
111 void            Module::OnGlobalOper(User*) { }
112 void            Module::OnPostConnect(User*) { }
113 ModResult       Module::OnAddBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
114 ModResult       Module::OnDelBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
115 void            Module::OnStreamSocketAccept(StreamSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { }
116 int             Module::OnStreamSocketWrite(StreamSocket*, std::string&) { return -1; }
117 void            Module::OnStreamSocketClose(StreamSocket*) { }
118 void            Module::OnStreamSocketConnect(StreamSocket*) { }
119 int             Module::OnStreamSocketRead(StreamSocket*, std::string&) { return -1; }
120 void            Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&) { }
121 void            Module::OnUserNotice(User*, void*, int, const std::string&, char, const CUList&) { }
122 void            Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
123 void            Module::OnUserInvite(User*, User*, Channel*, time_t) { }
124 void            Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
125 void            Module::OnGetServerDescription(const std::string&, std::string&) { }
126 void            Module::OnSyncUser(User*, Module*, void*) { }
127 void            Module::OnSyncChannel(Channel*, Module*, void*) { }
128 void            Module::OnSyncNetwork(Module*, void*) { }
129 void            Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
130 void            Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
131 void            Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
132 void            Module::OnWallops(User*, const std::string&) { }
133 void            Module::OnChangeHost(User*, const std::string&) { }
134 void            Module::OnChangeName(User*, const std::string&) { }
135 void            Module::OnChangeIdent(User*, const std::string&) { }
136 void            Module::OnAddLine(User*, XLine*) { }
137 void            Module::OnDelLine(User*, XLine*) { }
138 void            Module::OnExpireLine(XLine*) { }
139 void            Module::OnCleanup(int, void*) { }
140 ModResult       Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
141 void            Module::OnChannelDelete(Channel*) { }
142 ModResult       Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
143 ModResult       Module::OnUserList(User*, Channel*) { return MOD_RES_PASSTHRU; }
144 ModResult       Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
145 void            Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
146 void            Module::OnGarbageCollect() { }
147 void            Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
148 void            Module::OnRunTestSuite() { }
149 void            Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
150 ModResult       Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
151 void            Module::OnHookIO(StreamSocket*, ListenSocketBase*) { }
152 void            Module::OnSendWhoLine(User*, User*, Channel*, std::string&) { }
153 ModResult       Module::OnChannelRestrictionApply(User*, Channel*, const char*) { return MOD_RES_PASSTHRU; }
154
155 ModuleManager::ModuleManager() : ModCount(0)
156 {
157 }
158
159 ModuleManager::~ModuleManager()
160 {
161 }
162
163 bool ModuleManager::Attach(Implementation i, Module* mod)
164 {
165         if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
166                 return false;
167
168         EventHandlers[i].push_back(mod);
169         return true;
170 }
171
172 bool ModuleManager::Detach(Implementation i, Module* mod)
173 {
174         EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
175
176         if (x == EventHandlers[i].end())
177                 return false;
178
179         EventHandlers[i].erase(x);
180         return true;
181 }
182
183 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
184 {
185         for (size_t n = 0; n < sz; ++n)
186                 Attach(i[n], mod);
187 }
188
189 void ModuleManager::DetachAll(Module* mod)
190 {
191         for (size_t n = I_BEGIN + 1; n != I_END; ++n)
192                 Detach((Implementation)n, mod);
193 }
194
195 bool ModuleManager::SetPriority(Module* mod, Priority s)
196 {
197         for (size_t n = I_BEGIN + 1; n != I_END; ++n)
198                 SetPriority(mod, (Implementation)n, s);
199
200         return true;
201 }
202
203 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module** modules, size_t sz)
204 {
205         /** To change the priority of a module, we first find its position in the vector,
206          * then we find the position of the other modules in the vector that this module
207          * wants to be before/after. We pick off either the first or last of these depending
208          * on which they want, and we make sure our module is *at least* before or after
209          * the first or last of this subset, depending again on the type of priority.
210          */
211         size_t swap_pos = 0;
212         size_t source = 0;
213         bool swap = true;
214         bool found = false;
215
216         /* Locate our module. This is O(n) but it only occurs on module load so we're
217          * not too bothered about it
218          */
219         for (size_t x = 0; x != EventHandlers[i].size(); ++x)
220         {
221                 if (EventHandlers[i][x] == mod)
222                 {
223                         source = x;
224                         found = true;
225                         break;
226                 }
227         }
228
229         /* Eh? this module doesnt exist, probably trying to set priority on an event
230          * theyre not attached to.
231          */
232         if (!found)
233                 return false;
234
235         switch (s)
236         {
237                 /* Dummy value */
238                 case PRIORITY_DONTCARE:
239                         swap = false;
240                 break;
241                 /* Module wants to be first, sod everything else */
242                 case PRIORITY_FIRST:
243                         if (prioritizationState != PRIO_STATE_FIRST)
244                                 swap = false;
245                         else
246                                 swap_pos = 0;
247                 break;
248                 /* Module wants to be last. */
249                 case PRIORITY_LAST:
250                         if (prioritizationState != PRIO_STATE_FIRST)
251                                 swap = false;
252                         else if (EventHandlers[i].empty())
253                                 swap_pos = 0;
254                         else
255                                 swap_pos = EventHandlers[i].size() - 1;
256                 break;
257                 /* Place this module after a set of other modules */
258                 case PRIORITY_AFTER:
259                 {
260                         /* Find the latest possible position */
261                         swap_pos = 0;
262                         swap = false;
263                         for (size_t x = 0; x != EventHandlers[i].size(); ++x)
264                         {
265                                 for (size_t n = 0; n < sz; ++n)
266                                 {
267                                         if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x >= swap_pos) && (source <= swap_pos))
268                                         {
269                                                 swap_pos = x;
270                                                 swap = true;
271                                         }
272                                 }
273                         }
274                 }
275                 break;
276                 /* Place this module before a set of other modules */
277                 case PRIORITY_BEFORE:
278                 {
279                         swap_pos = EventHandlers[i].size() - 1;
280                         swap = false;
281                         for (size_t x = 0; x != EventHandlers[i].size(); ++x)
282                         {
283                                 for (size_t n = 0; n < sz; ++n)
284                                 {
285                                         if ((modules[n]) && (EventHandlers[i][x] == modules[n]) && (x <= swap_pos) && (source >= swap_pos))
286                                         {
287                                                 swap = true;
288                                                 swap_pos = x;
289                                         }
290                                 }
291                         }
292                 }
293                 break;
294         }
295
296         /* Do we need to swap? */
297         if (swap && (swap_pos != source))
298         {
299                 // We are going to change positions; we'll need to run again to verify all requirements
300                 if (prioritizationState == PRIO_STATE_LAST)
301                         prioritizationState = PRIO_STATE_AGAIN;
302                 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
303                 int incrmnt = 1;
304
305                 if (source > swap_pos)
306                         incrmnt = -1;
307
308                 for (unsigned int j = source; j != swap_pos; j += incrmnt)
309                 {
310                         if (( j + incrmnt > EventHandlers[i].size() - 1) || (j + incrmnt < 0))
311                                 continue;
312
313                         std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
314                 }
315         }
316
317         return true;
318 }
319
320 std::string& ModuleManager::LastError()
321 {
322         return LastModuleError;
323 }
324
325 bool ModuleManager::Load(const char* filename)
326 {
327         /* Do we have a glob pattern in the filename?
328          * The user wants to load multiple modules which
329          * match the pattern.
330          */
331         if (strchr(filename,'*') || (strchr(filename,'?')))
332         {
333                 int n_match = 0;
334                 DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
335                 if (library)
336                 {
337                         /* Try and locate and load all modules matching the pattern */
338                         dirent* entry = NULL;
339                         while (0 != (entry = readdir(library)))
340                         {
341                                 if (InspIRCd::Match(entry->d_name, filename, ascii_case_insensitive_map))
342                                 {
343                                         if (!this->Load(entry->d_name))
344                                                 n_match++;
345                                 }
346                         }
347                         closedir(library);
348                 }
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.
352                  */
353                 return (n_match > 0 ? false : true);
354         }
355
356         char modfile[MAXBUF];
357         snprintf(modfile,MAXBUF,"%s/%s",ServerInstance->Config->ModPath.c_str(),filename);
358         std::string filename_str = filename;
359
360         if (!ServerConfig::FileExists(modfile))
361         {
362                 LastModuleError = "Module file could not be found: " + filename_str;
363                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
364                 return false;
365         }
366
367         if (Modules.find(filename_str) != Modules.end())
368         {
369                 LastModuleError = "Module " + filename_str + " is already loaded, cannot load a module twice!";
370                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
371                 return false;
372         }
373
374         Module* newmod = NULL;
375         DLLManager* newhandle = new DLLManager(modfile);
376
377         try
378         {
379                 newmod = newhandle->callInit();
380
381                 if (newmod)
382                 {
383                         newmod->ModuleSourceFile = filename_str;
384                         newmod->ModuleDLLManager = newhandle;
385                         Version v = newmod->GetVersion();
386
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]"));
389
390                         Modules[filename_str] = newmod;
391                 }
392                 else
393                 {
394                         LastModuleError = "Unable to load " + filename_str + ": " + newhandle->LastError();
395                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
396                         delete newhandle;
397                         return false;
398                 }
399         }
400         catch (CoreException& modexcept)
401         {
402                 // failure in module constructor
403                 delete newmod;
404                 delete newhandle;
405                 LastModuleError = "Unable to load " + filename_str + ": " + modexcept.GetReason();
406                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
407                 return false;
408         }
409
410         this->ModCount++;
411         FOREACH_MOD(I_OnLoadModule,OnLoadModule(newmod, filename_str));
412
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
415          * of others
416          */
417         for(int tries = 0; tries < 20; tries++)
418         {
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();
422
423                 if (prioritizationState == PRIO_STATE_LAST)
424                         break;
425                 if (tries == 19)
426                         ServerInstance->Logs->Log("MODULE", DEFAULT, "Hook priority dependency loop detected while loading " + filename_str);
427         }
428
429         ServerInstance->BuildISupport();
430         return true;
431 }
432
433 bool ModuleManager::Unload(const char* filename)
434 {
435         std::string filename_str(filename);
436         std::map<std::string, Module*>::iterator modfind = Modules.find(filename);
437
438         if (modfind != Modules.end())
439         {
440                 if (modfind->second->GetVersion().Flags & VF_STATIC)
441                 {
442                         LastModuleError = "Module " + filename_str + " not unloadable (marked static)";
443                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
444                         return false;
445                 }
446                 std::pair<int,std::string> intercount = GetInterfaceInstanceCount(modfind->second);
447                 if (intercount.first > 0)
448                 {
449                         LastModuleError = "Failed to unload module " + filename_str + ", being used by " + ConvToStr(intercount.first) + " other(s) via interface '" + intercount.second + "'";
450                         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
451                         return false;
452                 }
453
454                 std::vector<ExtensionItem*> items;
455                 ServerInstance->Extensions.BeginUnregister(modfind->second, items);
456                 /* Give the module a chance to tidy out all its metadata */
457                 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
458                 {
459                         modfind->second->OnCleanup(TYPE_CHANNEL,c->second);
460                         c->second->doUnhookExtensions(items);
461                         const UserMembList* users = c->second->GetUsers();
462                         for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
463                                 mi->second->doUnhookExtensions(items);
464                 }
465                 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
466                 {
467                         modfind->second->OnCleanup(TYPE_USER,u->second);
468                         u->second->doUnhookExtensions(items);
469                 }
470
471                 /* Tidy up any dangling resolvers */
472                 ServerInstance->Res->CleanResolvers(modfind->second);
473
474                 FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(modfind->second, modfind->first));
475
476                 this->DetachAll(modfind->second);
477
478                 ServerInstance->GlobalCulls.AddItem(modfind->second);
479                 Modules.erase(modfind);
480
481                 ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",filename);
482                 this->ModCount--;
483                 ServerInstance->BuildISupport();
484                 return true;
485         }
486
487         LastModuleError = "Module " + filename_str + " is not loaded, cannot unload it!";
488         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
489         return false;
490 }
491
492 /* We must load the modules AFTER initializing the socket engine, now */
493 void ModuleManager::LoadAll()
494 {
495         char configToken[MAXBUF];
496         ModCount = 0;
497
498         printf("\nLoading core commands");
499         fflush(stdout);
500
501         DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
502         if (library)
503         {
504                 dirent* entry = NULL;
505                 while (0 != (entry = readdir(library)))
506                 {
507                         if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
508                         {
509                                 printf(".");
510                                 fflush(stdout);
511
512                                 if (!Load(entry->d_name))
513                                 {
514                                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
515                                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
516                                         ServerInstance->Exit(EXIT_STATUS_MODULE);
517                                 }
518                         }
519                 }
520                 closedir(library);
521                 printf("\n");
522         }
523
524         for(int count = 0; count < ServerInstance->Config->ConfValueEnum("module"); count++)
525         {
526                 ServerInstance->Config->ConfValue("module", "name", count, configToken, MAXBUF);
527                 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
528
529                 if (!this->Load(configToken))
530                 {
531                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
532                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
533                         ServerInstance->Exit(EXIT_STATUS_MODULE);
534                 }
535         }
536 }
537
538 bool ModuleManager::PublishFeature(const std::string &FeatureName, Module* Mod)
539 {
540         if (Features.find(FeatureName) == Features.end())
541         {
542                 Features[FeatureName] = Mod;
543                 return true;
544         }
545         return false;
546 }
547
548 bool ModuleManager::UnpublishFeature(const std::string &FeatureName)
549 {
550         featurelist::iterator iter = Features.find(FeatureName);
551
552         if (iter == Features.end())
553                 return false;
554
555         Features.erase(iter);
556         return true;
557 }
558
559 Module* ModuleManager::FindFeature(const std::string &FeatureName)
560 {
561         featurelist::iterator iter = Features.find(FeatureName);
562
563         if (iter == Features.end())
564                 return NULL;
565
566         return iter->second;
567 }
568
569 bool ModuleManager::PublishInterface(const std::string &InterfaceName, Module* Mod)
570 {
571         interfacelist::iterator iter = Interfaces.find(InterfaceName);
572
573         if (iter == Interfaces.end())
574         {
575                 modulelist ml;
576                 ml.push_back(Mod);
577                 Interfaces[InterfaceName] = std::make_pair(0, ml);
578         }
579         else
580         {
581                 iter->second.second.push_back(Mod);
582         }
583         return true;
584 }
585
586 bool ModuleManager::UnpublishInterface(const std::string &InterfaceName, Module* Mod)
587 {
588         interfacelist::iterator iter = Interfaces.find(InterfaceName);
589
590         if (iter == Interfaces.end())
591                 return false;
592
593         for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
594         {
595                 if (*x == Mod)
596                 {
597                         iter->second.second.erase(x);
598                         if (iter->second.second.empty())
599                                 Interfaces.erase(InterfaceName);
600                         return true;
601                 }
602         }
603         return false;
604 }
605
606 modulelist* ModuleManager::FindInterface(const std::string &InterfaceName)
607 {
608         interfacelist::iterator iter = Interfaces.find(InterfaceName);
609         if (iter == Interfaces.end())
610                 return NULL;
611         else
612                 return &(iter->second.second);
613 }
614
615 bool ModuleManager::ModuleHasInterface(Module* mod, const std::string& InterfaceName)
616 {
617         interfacelist::iterator iter = Interfaces.find(InterfaceName);
618         if (iter == Interfaces.end())
619                 return false;
620         else
621         {
622                 modulelist& ml = iter->second.second;
623                 modulelist::iterator mi = std::find(ml.begin(), ml.end(), mod);
624                 return (mi != ml.end());
625         }
626 }
627
628 void ModuleManager::UseInterface(const std::string &InterfaceName)
629 {
630         interfacelist::iterator iter = Interfaces.find(InterfaceName);
631         if (iter != Interfaces.end())
632                 iter->second.first++;
633
634 }
635
636 void ModuleManager::DoneWithInterface(const std::string &InterfaceName)
637 {
638         interfacelist::iterator iter = Interfaces.find(InterfaceName);
639         if (iter != Interfaces.end())
640                 iter->second.first--;
641 }
642
643 std::pair<int,std::string> ModuleManager::GetInterfaceInstanceCount(Module* m)
644 {
645         for (interfacelist::iterator iter = Interfaces.begin(); iter != Interfaces.end(); iter++)
646         {
647                 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
648                 {
649                         if (*x == m)
650                         {
651                                 return std::make_pair(iter->second.first, iter->first);
652                         }
653                 }
654         }
655         return std::make_pair(0, "");
656 }
657
658 const std::string& ModuleManager::GetModuleName(Module* m)
659 {
660         static std::string nothing;
661
662         for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
663         {
664                 if (n->second == m)
665                         return n->first;
666         }
667
668         return nothing;
669 }
670
671 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user)
672 {
673         return this->Parser->CallHandler(commandname, parameters, user);
674 }
675
676 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, User* user)
677 {
678         return this->Parser->IsValidCommand(commandname, pcnt, user);
679 }
680
681 void InspIRCd::AddCommand(Command *f)
682 {
683         if (!this->Parser->AddCommand(f))
684         {
685                 throw ModuleException("Command "+std::string(f->command)+" already exists.");
686         }
687 }
688
689 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
690 {
691         this->Modes->Process(parameters, user);
692 }
693
694 void InspIRCd::DumpText(User* user, const std::string &text)
695 {
696         if (IS_LOCAL(user))
697         {
698                 user->Write(text);
699         }
700         else
701         {
702                 PI->PushToClient(user, text);
703         }
704 }
705
706 void InspIRCd::DumpText(User* user, const char *text, ...)
707 {
708         va_list argsPtr;
709         char line[MAXBUF];
710
711         va_start(argsPtr, text);
712         vsnprintf(line, MAXBUF, text, argsPtr);
713         va_end(argsPtr);
714
715         DumpText(user, std::string(line));
716 }
717
718 void InspIRCd::DumpText(User* user, const std::string &LinePrefix, std::stringstream &TextStream)
719 {
720         char line[MAXBUF];
721         int start_pos = LinePrefix.length();
722         int pos = start_pos;
723         memcpy(line, LinePrefix.data(), pos);
724         std::string Word;
725         while (TextStream >> Word)
726         {
727                 int len = Word.length();
728                 if (pos + len + 12 > MAXBUF)
729                 {
730                         line[pos] = '\0';
731                         DumpText(user, std::string(line));
732                         pos = start_pos;
733                 }
734                 line[pos] = ' ';
735                 memcpy(line + pos + 1, Word.data(), len);
736                 pos += len + 1;
737         }
738         line[pos] = '\0';
739         DumpText(user, std::string(line));
740 }
741
742 bool InspIRCd::AddResolver(Resolver* r, bool cached)
743 {
744         if (!cached)
745                 return this->Res->AddResolverClass(r);
746         else
747         {
748                 r->TriggerCachedResult();
749                 delete r;
750                 return true;
751         }
752 }
753
754 Module* ModuleManager::Find(const std::string &name)
755 {
756         std::map<std::string, Module*>::iterator modfind = Modules.find(name);
757
758         if (modfind == Modules.end())
759                 return NULL;
760         else
761                 return modfind->second;
762 }
763
764 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
765 {
766         std::vector<std::string> retval;
767         for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
768                 if (!filter || (x->second->GetVersion().Flags & filter))
769                         retval.push_back(x->first);
770         return retval;
771 }
772
773 ConfigReader::ConfigReader()
774 {
775         this->error = 0;
776 }
777
778
779 ConfigReader::~ConfigReader()
780 {
781 }
782
783
784 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
785 {
786         /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
787         std::string result;
788
789         if (!ServerInstance->Config->ConfValue(tag, name, default_value, index, result, allow_linefeeds))
790         {
791                 this->error = CONF_VALUE_NOT_FOUND;
792         }
793         return result;
794 }
795
796 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
797 {
798         return ReadValue(tag, name, "", index, allow_linefeeds);
799 }
800
801 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
802 {
803         return ServerInstance->Config->ConfValueBool(tag, name, default_value, index);
804 }
805
806 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
807 {
808         return ReadFlag(tag, name, "", index);
809 }
810
811
812 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
813 {
814         int result;
815
816         if(!ServerInstance->Config->ConfValueInteger(tag, name, default_value, index, result))
817         {
818                 this->error = CONF_VALUE_NOT_FOUND;
819                 return 0;
820         }
821
822         if ((need_positive) && (result < 0))
823         {
824                 this->error = CONF_INT_NEGATIVE;
825                 return 0;
826         }
827
828         return result;
829 }
830
831 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
832 {
833         return ReadInteger(tag, name, "", index, need_positive);
834 }
835
836 long ConfigReader::GetError()
837 {
838         long olderr = this->error;
839         this->error = 0;
840         return olderr;
841 }
842
843 int ConfigReader::Enumerate(const std::string &tag)
844 {
845         return ServerInstance->Config->ConfValueEnum(tag);
846 }
847
848 int ConfigReader::EnumerateValues(const std::string &tag, int index)
849 {
850         return ServerInstance->Config->ConfVarEnum(tag, index);
851 }
852
853 FileReader::FileReader(const std::string &filename)
854 {
855         LoadFile(filename);
856 }
857
858 FileReader::FileReader()
859 {
860 }
861
862 std::string FileReader::Contents()
863 {
864         std::string x;
865         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
866         {
867                 x.append(*a);
868                 x.append("\r\n");
869         }
870         return x;
871 }
872
873 unsigned long FileReader::ContentSize()
874 {
875         return this->contentsize;
876 }
877
878 void FileReader::CalcSize()
879 {
880         unsigned long n = 0;
881         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
882                 n += (a->length() + 2);
883         this->contentsize = n;
884 }
885
886 void FileReader::LoadFile(const std::string &filename)
887 {
888         file_cache c;
889         c.clear();
890         if (ServerInstance->Config->ReadFile(c,filename.c_str()))
891         {
892                 this->fc = c;
893                 this->CalcSize();
894         }
895 }
896
897
898 FileReader::~FileReader()
899 {
900 }
901
902 bool FileReader::Exists()
903 {
904         return (!(fc.size() == 0));
905 }
906
907 std::string FileReader::GetLine(int x)
908 {
909         if ((x<0) || ((unsigned)x>fc.size()))
910                 return "";
911         return fc[x];
912 }
913
914 int FileReader::FileSize()
915 {
916         return fc.size();
917 }