]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
DLLFactory--
[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 = Extensible::BeginUnregister(modfind->second);
455                 /* Give the module a chance to tidy out all its metadata */
456                 for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
457                 {
458                         modfind->second->OnCleanup(TYPE_CHANNEL,c->second);
459                         c->second->doUnhookExtensions(items);
460                         const UserMembList* users = c->second->GetUsers();
461                         for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
462                                 mi->second->doUnhookExtensions(items);
463                 }
464                 for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
465                 {
466                         modfind->second->OnCleanup(TYPE_USER,u->second);
467                         u->second->doUnhookExtensions(items);
468                 }
469
470                 /* Tidy up any dangling resolvers */
471                 ServerInstance->Res->CleanResolvers(modfind->second);
472
473                 FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(modfind->second, modfind->first));
474
475                 this->DetachAll(modfind->second);
476
477                 ServerInstance->Parser->RemoveCommands(modfind->second);
478                 ServerInstance->Modes->RemoveModes(modfind->second);
479
480                 ServerInstance->GlobalCulls.AddItem(modfind->second);
481                 Modules.erase(modfind);
482
483                 ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",filename);
484                 this->ModCount--;
485                 ServerInstance->BuildISupport();
486                 return true;
487         }
488
489         LastModuleError = "Module " + filename_str + " is not loaded, cannot unload it!";
490         ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
491         return false;
492 }
493
494 /* We must load the modules AFTER initializing the socket engine, now */
495 void ModuleManager::LoadAll()
496 {
497         char configToken[MAXBUF];
498         ModCount = -1;
499
500         printf("\nLoading core commands");
501         fflush(stdout);
502
503         DIR* library = opendir(ServerInstance->Config->ModPath.c_str());
504         if (library)
505         {
506                 dirent* entry = NULL;
507                 while (0 != (entry = readdir(library)))
508                 {
509                         if (InspIRCd::Match(entry->d_name, "cmd_*.so", ascii_case_insensitive_map))
510                         {
511                                 printf(".");
512                                 fflush(stdout);
513
514                                 if (!Load(entry->d_name))
515                                 {
516                                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
517                                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
518                                         ServerInstance->Exit(EXIT_STATUS_MODULE);
519                                 }
520                         }
521                 }
522                 closedir(library);
523                 printf("\n");
524         }
525
526         for(int count = 0; count < ServerInstance->Config->ConfValueEnum("module"); count++)
527         {
528                 ServerInstance->Config->ConfValue("module", "name", count, configToken, MAXBUF);
529                 printf_c("[\033[1;32m*\033[0m] Loading module:\t\033[1;32m%s\033[0m\n",configToken);
530
531                 if (!this->Load(configToken))
532                 {
533                         ServerInstance->Logs->Log("MODULE", DEFAULT, this->LastError());
534                         printf_c("\n[\033[1;31m*\033[0m] %s\n\n", this->LastError().c_str());
535                         ServerInstance->Exit(EXIT_STATUS_MODULE);
536                 }
537         }
538 }
539
540 bool ModuleManager::PublishFeature(const std::string &FeatureName, Module* Mod)
541 {
542         if (Features.find(FeatureName) == Features.end())
543         {
544                 Features[FeatureName] = Mod;
545                 return true;
546         }
547         return false;
548 }
549
550 bool ModuleManager::UnpublishFeature(const std::string &FeatureName)
551 {
552         featurelist::iterator iter = Features.find(FeatureName);
553
554         if (iter == Features.end())
555                 return false;
556
557         Features.erase(iter);
558         return true;
559 }
560
561 Module* ModuleManager::FindFeature(const std::string &FeatureName)
562 {
563         featurelist::iterator iter = Features.find(FeatureName);
564
565         if (iter == Features.end())
566                 return NULL;
567
568         return iter->second;
569 }
570
571 bool ModuleManager::PublishInterface(const std::string &InterfaceName, Module* Mod)
572 {
573         interfacelist::iterator iter = Interfaces.find(InterfaceName);
574
575         if (iter == Interfaces.end())
576         {
577                 modulelist ml;
578                 ml.push_back(Mod);
579                 Interfaces[InterfaceName] = std::make_pair(0, ml);
580         }
581         else
582         {
583                 iter->second.second.push_back(Mod);
584         }
585         return true;
586 }
587
588 bool ModuleManager::UnpublishInterface(const std::string &InterfaceName, Module* Mod)
589 {
590         interfacelist::iterator iter = Interfaces.find(InterfaceName);
591
592         if (iter == Interfaces.end())
593                 return false;
594
595         for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
596         {
597                 if (*x == Mod)
598                 {
599                         iter->second.second.erase(x);
600                         if (iter->second.second.empty())
601                                 Interfaces.erase(InterfaceName);
602                         return true;
603                 }
604         }
605         return false;
606 }
607
608 modulelist* ModuleManager::FindInterface(const std::string &InterfaceName)
609 {
610         interfacelist::iterator iter = Interfaces.find(InterfaceName);
611         if (iter == Interfaces.end())
612                 return NULL;
613         else
614                 return &(iter->second.second);
615 }
616
617 bool ModuleManager::ModuleHasInterface(Module* mod, const std::string& InterfaceName)
618 {
619         interfacelist::iterator iter = Interfaces.find(InterfaceName);
620         if (iter == Interfaces.end())
621                 return false;
622         else
623         {
624                 modulelist& ml = iter->second.second;
625                 modulelist::iterator mi = std::find(ml.begin(), ml.end(), mod);
626                 return (mi != ml.end());
627         }
628 }
629
630 void ModuleManager::UseInterface(const std::string &InterfaceName)
631 {
632         interfacelist::iterator iter = Interfaces.find(InterfaceName);
633         if (iter != Interfaces.end())
634                 iter->second.first++;
635
636 }
637
638 void ModuleManager::DoneWithInterface(const std::string &InterfaceName)
639 {
640         interfacelist::iterator iter = Interfaces.find(InterfaceName);
641         if (iter != Interfaces.end())
642                 iter->second.first--;
643 }
644
645 std::pair<int,std::string> ModuleManager::GetInterfaceInstanceCount(Module* m)
646 {
647         for (interfacelist::iterator iter = Interfaces.begin(); iter != Interfaces.end(); iter++)
648         {
649                 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
650                 {
651                         if (*x == m)
652                         {
653                                 return std::make_pair(iter->second.first, iter->first);
654                         }
655                 }
656         }
657         return std::make_pair(0, "");
658 }
659
660 const std::string& ModuleManager::GetModuleName(Module* m)
661 {
662         static std::string nothing;
663
664         for (std::map<std::string, Module*>::iterator n = Modules.begin(); n != Modules.end(); ++n)
665         {
666                 if (n->second == m)
667                         return n->first;
668         }
669
670         return nothing;
671 }
672
673 /* This is ugly, yes, but hash_map's arent designed to be
674  * addressed in this manner, and this is a bit of a kludge.
675  * Luckily its a specialist function and rarely used by
676  * many modules (in fact, it was specially created to make
677  * m_safelist possible, initially).
678  */
679
680 Channel* InspIRCd::GetChannelIndex(long index)
681 {
682         int target = 0;
683         for (chan_hash::iterator n = this->chanlist->begin(); n != this->chanlist->end(); n++, target++)
684         {
685                 if (index == target)
686                         return n->second;
687         }
688         return NULL;
689 }
690
691 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user)
692 {
693         return this->Parser->CallHandler(commandname, parameters, user);
694 }
695
696 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, User* user)
697 {
698         return this->Parser->IsValidCommand(commandname, pcnt, user);
699 }
700
701 void InspIRCd::AddCommand(Command *f)
702 {
703         if (!this->Parser->CreateCommand(f))
704         {
705                 ModuleException err("Command "+std::string(f->command)+" already exists.");
706                 throw (err);
707         }
708 }
709
710 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
711 {
712         this->Modes->Process(parameters, user);
713 }
714
715 void InspIRCd::DumpText(User* user, const std::string &text)
716 {
717         if (IS_LOCAL(user))
718         {
719                 user->Write(text);
720         }
721         else
722         {
723                 PI->PushToClient(user, text);
724         }
725 }
726
727 void InspIRCd::DumpText(User* user, const char *text, ...)
728 {
729         va_list argsPtr;
730         char line[MAXBUF];
731
732         va_start(argsPtr, text);
733         vsnprintf(line, MAXBUF, text, argsPtr);
734         va_end(argsPtr);
735
736         DumpText(user, std::string(line));
737 }
738
739 void InspIRCd::DumpText(User* user, const std::string &LinePrefix, std::stringstream &TextStream)
740 {
741         char line[MAXBUF];
742         int start_pos = LinePrefix.length();
743         int pos = start_pos;
744         memcpy(line, LinePrefix.data(), pos);
745         std::string Word;
746         while (TextStream >> Word)
747         {
748                 int len = Word.length();
749                 if (pos + len + 12 > MAXBUF)
750                 {
751                         line[pos] = '\0';
752                         DumpText(user, std::string(line));
753                         pos = start_pos;
754                 }
755                 line[pos] = ' ';
756                 memcpy(line + pos + 1, Word.data(), len);
757                 pos += len + 1;
758         }
759         line[pos] = '\0';
760         DumpText(user, std::string(line));
761 }
762
763 bool InspIRCd::AddResolver(Resolver* r, bool cached)
764 {
765         if (!cached)
766                 return this->Res->AddResolverClass(r);
767         else
768         {
769                 r->TriggerCachedResult();
770                 delete r;
771                 return true;
772         }
773 }
774
775 Module* ModuleManager::Find(const std::string &name)
776 {
777         std::map<std::string, Module*>::iterator modfind = Modules.find(name);
778
779         if (modfind == Modules.end())
780                 return NULL;
781         else
782                 return modfind->second;
783 }
784
785 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
786 {
787         std::vector<std::string> retval;
788         for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
789                 if (!filter || (x->second->GetVersion().Flags & filter))
790                         retval.push_back(x->first);
791         return retval;
792 }
793
794 ConfigReader::ConfigReader()
795 {
796         this->error = 0;
797 }
798
799
800 ConfigReader::~ConfigReader()
801 {
802 }
803
804
805 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
806 {
807         /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */
808         std::string result;
809
810         if (!ServerInstance->Config->ConfValue(tag, name, default_value, index, result, allow_linefeeds))
811         {
812                 this->error = CONF_VALUE_NOT_FOUND;
813         }
814         return result;
815 }
816
817 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
818 {
819         return ReadValue(tag, name, "", index, allow_linefeeds);
820 }
821
822 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
823 {
824         return ServerInstance->Config->ConfValueBool(tag, name, default_value, index);
825 }
826
827 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
828 {
829         return ReadFlag(tag, name, "", index);
830 }
831
832
833 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
834 {
835         int result;
836
837         if(!ServerInstance->Config->ConfValueInteger(tag, name, default_value, index, result))
838         {
839                 this->error = CONF_VALUE_NOT_FOUND;
840                 return 0;
841         }
842
843         if ((need_positive) && (result < 0))
844         {
845                 this->error = CONF_INT_NEGATIVE;
846                 return 0;
847         }
848
849         return result;
850 }
851
852 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
853 {
854         return ReadInteger(tag, name, "", index, need_positive);
855 }
856
857 long ConfigReader::GetError()
858 {
859         long olderr = this->error;
860         this->error = 0;
861         return olderr;
862 }
863
864 int ConfigReader::Enumerate(const std::string &tag)
865 {
866         return ServerInstance->Config->ConfValueEnum(tag);
867 }
868
869 int ConfigReader::EnumerateValues(const std::string &tag, int index)
870 {
871         return ServerInstance->Config->ConfVarEnum(tag, index);
872 }
873
874 FileReader::FileReader(const std::string &filename)
875 {
876         LoadFile(filename);
877 }
878
879 FileReader::FileReader()
880 {
881 }
882
883 std::string FileReader::Contents()
884 {
885         std::string x;
886         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
887         {
888                 x.append(*a);
889                 x.append("\r\n");
890         }
891         return x;
892 }
893
894 unsigned long FileReader::ContentSize()
895 {
896         return this->contentsize;
897 }
898
899 void FileReader::CalcSize()
900 {
901         unsigned long n = 0;
902         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
903                 n += (a->length() + 2);
904         this->contentsize = n;
905 }
906
907 void FileReader::LoadFile(const std::string &filename)
908 {
909         file_cache c;
910         c.clear();
911         if (ServerInstance->Config->ReadFile(c,filename.c_str()))
912         {
913                 this->fc = c;
914                 this->CalcSize();
915         }
916 }
917
918
919 FileReader::~FileReader()
920 {
921 }
922
923 bool FileReader::Exists()
924 {
925         return (!(fc.size() == 0));
926 }
927
928 std::string FileReader::GetLine(int x)
929 {
930         if ((x<0) || ((unsigned)x>fc.size()))
931                 return "";
932         return fc[x];
933 }
934
935 int FileReader::FileSize()
936 {
937         return fc.size();
938 }