]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
871bb94995cc696ac9e316a4bf4c134aa058f1e3
[user/henk/code/inspircd.git] / src / modules.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007, 2009 Dennis Friis <peavey@inspircd.org>
6  *   Copyright (C) 2003-2008 Craig Edwards <craigedwards@brainbox.cc>
7  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
8  *   Copyright (C) 2006-2007 Robin Burchell <robin+git@viroteck.net>
9  *   Copyright (C) 2006-2007 Oliver Lupton <oliverlupton@gmail.com>
10  *   Copyright (C) 2007 Pippijn van Steenhoven <pip88nl@gmail.com>
11  *   Copyright (C) 2003 randomdan <???@???>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include <fstream>
28 #include "inspircd.h"
29 #include "xline.h"
30 #include "socket.h"
31 #include "socketengine.h"
32 #include "command_parse.h"
33 #include "exitcodes.h"
34
35 #ifndef _WIN32
36         #include <dirent.h>
37 #endif
38
39 static std::vector<dynamic_reference_base*>* dynrefs = NULL;
40 static bool dynref_init_complete = false;
41
42 void dynamic_reference_base::reset_all()
43 {
44         dynref_init_complete = true;
45         if (!dynrefs)
46                 return;
47         for(unsigned int i = 0; i < dynrefs->size(); i++)
48                 (*dynrefs)[i]->resolve();
49 }
50
51 // Version is a simple class for holding a modules version number
52 Version::Version(const std::string &desc, int flags) : description(desc), Flags(flags)
53 {
54 }
55
56 Version::Version(const std::string &desc, int flags, const std::string& linkdata)
57 : description(desc), Flags(flags), link_data(linkdata)
58 {
59 }
60
61 Request::Request(Module* src, Module* dst, const char* idstr)
62 : id(idstr), source(src), dest(dst)
63 {
64 }
65
66 void Request::Send()
67 {
68         if (dest)
69                 dest->OnRequest(*this);
70 }
71
72 Event::Event(Module* src, const std::string &eventid) : source(src), id(eventid) { }
73
74 void Event::Send()
75 {
76         FOREACH_MOD(I_OnEvent,OnEvent(*this));
77 }
78
79 // These declarations define the behavours of the base class Module (which does nothing at all)
80
81 Module::Module() { }
82 CullResult Module::cull()
83 {
84         return classbase::cull();
85 }
86 Module::~Module()
87 {
88 }
89
90 ModResult       Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
91 void            Module::OnUserConnect(LocalUser*) { }
92 void            Module::OnUserQuit(User*, const std::string&, const std::string&) { }
93 void            Module::OnUserDisconnect(LocalUser*) { }
94 void            Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
95 void            Module::OnPostJoin(Membership*) { }
96 void            Module::OnUserPart(Membership*, std::string&, CUList&) { }
97 void            Module::OnPreRehash(User*, const std::string&) { }
98 void            Module::OnModuleRehash(User*, const std::string&) { }
99 void            Module::OnRehash(User*) { }
100 ModResult       Module::OnUserPreJoin(LocalUser*, Channel*, const std::string&, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
101 void            Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
102 void            Module::OnOper(User*, const std::string&) { }
103 void            Module::OnPostOper(User*, const std::string&, const std::string &) { }
104 void            Module::OnInfo(User*) { }
105 void            Module::OnWhois(User*, User*) { }
106 ModResult       Module::OnUserPreInvite(User*, User*, Channel*, time_t) { return MOD_RES_PASSTHRU; }
107 ModResult       Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&, MessageType) { return MOD_RES_PASSTHRU; }
108 ModResult       Module::OnUserPreNick(User*, const std::string&) { return MOD_RES_PASSTHRU; }
109 void            Module::OnUserPostNick(User*, const std::string&) { }
110 ModResult       Module::OnPreMode(User*, User*, Channel*, const std::vector<std::string>&) { return MOD_RES_PASSTHRU; }
111 void            Module::On005Numeric(std::map<std::string, std::string>&) { }
112 ModResult       Module::OnKill(User*, User*, const std::string&) { return MOD_RES_PASSTHRU; }
113 void            Module::OnLoadModule(Module*) { }
114 void            Module::OnUnloadModule(Module*) { }
115 void            Module::OnBackgroundTimer(time_t) { }
116 ModResult       Module::OnPreCommand(std::string&, std::vector<std::string>&, LocalUser*, bool, const std::string&) { return MOD_RES_PASSTHRU; }
117 void            Module::OnPostCommand(const std::string&, const std::vector<std::string>&, LocalUser*, CmdResult, const std::string&) { }
118 void            Module::OnUserInit(LocalUser*) { }
119 ModResult       Module::OnCheckReady(LocalUser*) { return MOD_RES_PASSTHRU; }
120 ModResult       Module::OnUserRegister(LocalUser*) { return MOD_RES_PASSTHRU; }
121 ModResult       Module::OnUserPreKick(User*, Membership*, const std::string&) { return MOD_RES_PASSTHRU; }
122 void            Module::OnUserKick(User*, Membership*, const std::string&, CUList&) { }
123 ModResult       Module::OnRawMode(User*, Channel*, const char, const std::string &, bool, int) { return MOD_RES_PASSTHRU; }
124 ModResult       Module::OnCheckInvite(User*, Channel*) { return MOD_RES_PASSTHRU; }
125 ModResult       Module::OnCheckKey(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
126 ModResult       Module::OnCheckLimit(User*, Channel*) { return MOD_RES_PASSTHRU; }
127 ModResult       Module::OnCheckChannelBan(User*, Channel*) { return MOD_RES_PASSTHRU; }
128 ModResult       Module::OnCheckBan(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
129 ModResult       Module::OnExtBanCheck(User*, Channel*, char) { return MOD_RES_PASSTHRU; }
130 ModResult       Module::OnStats(char, User*, string_list&) { return MOD_RES_PASSTHRU; }
131 ModResult       Module::OnChangeLocalUserHost(LocalUser*, const std::string&) { return MOD_RES_PASSTHRU; }
132 ModResult       Module::OnChangeLocalUserGECOS(LocalUser*, const std::string&) { return MOD_RES_PASSTHRU; }
133 ModResult       Module::OnPreTopicChange(User*, Channel*, const std::string&) { return MOD_RES_PASSTHRU; }
134 void            Module::OnEvent(Event&) { }
135 void            Module::OnRequest(Request&) { }
136 ModResult       Module::OnPassCompare(Extensible* ex, const std::string &password, const std::string &input, const std::string& hashtype) { return MOD_RES_PASSTHRU; }
137 void            Module::OnGlobalOper(User*) { }
138 void            Module::OnPostConnect(User*) { }
139 void            Module::OnStreamSocketAccept(StreamSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { }
140 int             Module::OnStreamSocketWrite(StreamSocket*, std::string&) { return -1; }
141 void            Module::OnStreamSocketClose(StreamSocket*) { }
142 void            Module::OnStreamSocketConnect(StreamSocket*) { }
143 int             Module::OnStreamSocketRead(StreamSocket*, std::string&) { return -1; }
144 void            Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&, MessageType) { }
145 void            Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
146 void            Module::OnUserInvite(User*, User*, Channel*, time_t) { }
147 void            Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
148 void            Module::OnGetServerDescription(const std::string&, std::string&) { }
149 void            Module::OnSyncUser(User*, Module*, void*) { }
150 void            Module::OnSyncChannel(Channel*, Module*, void*) { }
151 void            Module::OnSyncNetwork(Module*, void*) { }
152 void            Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
153 void            Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
154 void            Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
155 void            Module::OnWallops(User*, const std::string&) { }
156 void            Module::OnChangeHost(User*, const std::string&) { }
157 void            Module::OnChangeName(User*, const std::string&) { }
158 void            Module::OnChangeIdent(User*, const std::string&) { }
159 void            Module::OnAddLine(User*, XLine*) { }
160 void            Module::OnDelLine(User*, XLine*) { }
161 void            Module::OnExpireLine(XLine*) { }
162 void            Module::OnCleanup(int, void*) { }
163 ModResult       Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
164 void            Module::OnChannelDelete(Channel*) { }
165 ModResult       Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
166 ModResult       Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
167 void            Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
168 void            Module::OnGarbageCollect() { }
169 ModResult       Module::OnSetConnectClass(LocalUser* user, ConnectClass* myclass) { return MOD_RES_PASSTHRU; }
170 void            Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
171 void            Module::OnRunTestSuite() { }
172 void            Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
173 ModResult       Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
174 void            Module::OnHookIO(StreamSocket*, ListenSocket*) { }
175 ModResult   Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { return MOD_RES_PASSTHRU; }
176 void            Module::OnSendWhoLine(User*, const std::vector<std::string>&, User*, std::string&) { }
177 void            Module::OnSetUserIP(LocalUser*) { }
178
179 ModuleManager::ModuleManager() : ModCount(0)
180 {
181 }
182
183 ModuleManager::~ModuleManager()
184 {
185 }
186
187 bool ModuleManager::Attach(Implementation i, Module* mod)
188 {
189         if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
190                 return false;
191
192         EventHandlers[i].push_back(mod);
193         return true;
194 }
195
196 bool ModuleManager::Detach(Implementation i, Module* mod)
197 {
198         EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
199
200         if (x == EventHandlers[i].end())
201                 return false;
202
203         EventHandlers[i].erase(x);
204         return true;
205 }
206
207 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
208 {
209         for (size_t n = 0; n < sz; ++n)
210                 Attach(i[n], mod);
211 }
212
213 void ModuleManager::DetachAll(Module* mod)
214 {
215         for (size_t n = I_BEGIN + 1; n != I_END; ++n)
216                 Detach((Implementation)n, mod);
217 }
218
219 bool ModuleManager::SetPriority(Module* mod, Priority s)
220 {
221         for (size_t n = I_BEGIN + 1; n != I_END; ++n)
222                 SetPriority(mod, (Implementation)n, s);
223
224         return true;
225 }
226
227 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module* which)
228 {
229         /** To change the priority of a module, we first find its position in the vector,
230          * then we find the position of the other modules in the vector that this module
231          * wants to be before/after. We pick off either the first or last of these depending
232          * on which they want, and we make sure our module is *at least* before or after
233          * the first or last of this subset, depending again on the type of priority.
234          */
235         size_t my_pos = 0;
236
237         /* Locate our module. This is O(n) but it only occurs on module load so we're
238          * not too bothered about it
239          */
240         for (size_t x = 0; x != EventHandlers[i].size(); ++x)
241         {
242                 if (EventHandlers[i][x] == mod)
243                 {
244                         my_pos = x;
245                         goto found_src;
246                 }
247         }
248
249         /* Eh? this module doesnt exist, probably trying to set priority on an event
250          * theyre not attached to.
251          */
252         return false;
253
254 found_src:
255         size_t swap_pos = my_pos;
256         switch (s)
257         {
258                 case PRIORITY_FIRST:
259                         if (prioritizationState != PRIO_STATE_FIRST)
260                                 return true;
261                         else
262                                 swap_pos = 0;
263                         break;
264                 case PRIORITY_LAST:
265                         if (prioritizationState != PRIO_STATE_FIRST)
266                                 return true;
267                         else
268                                 swap_pos = EventHandlers[i].size() - 1;
269                         break;
270                 case PRIORITY_AFTER:
271                 {
272                         /* Find the latest possible position, only searching AFTER our position */
273                         for (size_t x = EventHandlers[i].size() - 1; x > my_pos; --x)
274                         {
275                                 if (EventHandlers[i][x] == which)
276                                 {
277                                         swap_pos = x;
278                                         goto swap_now;
279                                 }
280                         }
281                         // didn't find it - either not loaded or we're already after
282                         return true;
283                 }
284                 /* Place this module before a set of other modules */
285                 case PRIORITY_BEFORE:
286                 {
287                         for (size_t x = 0; x < my_pos; ++x)
288                         {
289                                 if (EventHandlers[i][x] == which)
290                                 {
291                                         swap_pos = x;
292                                         goto swap_now;
293                                 }
294                         }
295                         // didn't find it - either not loaded or we're already before
296                         return true;
297                 }
298         }
299
300 swap_now:
301         /* Do we need to swap? */
302         if (swap_pos != my_pos)
303         {
304                 // We are going to change positions; we'll need to run again to verify all requirements
305                 if (prioritizationState == PRIO_STATE_LAST)
306                         prioritizationState = PRIO_STATE_AGAIN;
307                 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
308                 int incrmnt = 1;
309
310                 if (my_pos > swap_pos)
311                         incrmnt = -1;
312
313                 for (unsigned int j = my_pos; j != swap_pos; j += incrmnt)
314                 {
315                         if ((j + incrmnt > EventHandlers[i].size() - 1) || ((incrmnt == -1) && (j == 0)))
316                                 continue;
317
318                         std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
319                 }
320         }
321
322         return true;
323 }
324
325 bool ModuleManager::CanUnload(Module* mod)
326 {
327         std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
328
329         if ((modfind == Modules.end()) || (modfind->second != mod) || (mod->dying))
330         {
331                 LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
332                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
333                 return false;
334         }
335         if (mod->GetVersion().Flags & VF_STATIC)
336         {
337                 LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
338                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
339                 return false;
340         }
341
342         mod->dying = true;
343         return true;
344 }
345
346 void ModuleManager::DoSafeUnload(Module* mod)
347 {
348         // First, notify all modules that a module is about to be unloaded, so in case
349         // they pass execution to the soon to be unloaded module, it will happen now,
350         // i.e. before we unregister the services of the module being unloaded
351         FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
352
353         std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
354
355         std::vector<reference<ExtensionItem> > items;
356         ServerInstance->Extensions.BeginUnregister(modfind->second, items);
357         /* Give the module a chance to tidy out all its metadata */
358         for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
359         {
360                 mod->OnCleanup(TYPE_CHANNEL,c->second);
361                 c->second->doUnhookExtensions(items);
362                 const UserMembList* users = c->second->GetUsers();
363                 for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
364                         mi->second->doUnhookExtensions(items);
365         }
366         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
367         {
368                 mod->OnCleanup(TYPE_USER,u->second);
369                 u->second->doUnhookExtensions(items);
370         }
371         for(char m='A'; m <= 'z'; m++)
372         {
373                 ModeHandler* mh;
374                 mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
375                 if (mh && mh->creator == mod)
376                         this->DelService(*mh);
377                 mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
378                 if (mh && mh->creator == mod)
379                         this->DelService(*mh);
380         }
381         for(std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.begin(); i != DataProviders.end(); )
382         {
383                 std::multimap<std::string, ServiceProvider*>::iterator curr = i++;
384                 if (curr->second->creator == mod)
385                         DataProviders.erase(curr);
386         }
387
388         dynamic_reference_base::reset_all();
389
390         DetachAll(mod);
391
392         Modules.erase(modfind);
393         ServerInstance->GlobalCulls.AddItem(mod);
394
395         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, "Module %s unloaded",mod->ModuleSourceFile.c_str());
396         this->ModCount--;
397         ServerInstance->ISupport.Build();
398 }
399
400 void ModuleManager::UnloadAll()
401 {
402         /* We do this more than once, so that any service providers get a
403          * chance to be unhooked by the modules using them, but then get
404          * a chance to be removed themsleves.
405          *
406          * Note: this deliberately does NOT delete the DLLManager objects
407          */
408         for (int tries = 0; tries < 4; tries++)
409         {
410                 std::map<std::string, Module*>::iterator i = Modules.begin();
411                 while (i != Modules.end())
412                 {
413                         std::map<std::string, Module*>::iterator me = i++;
414                         if (CanUnload(me->second))
415                         {
416                                 DoSafeUnload(me->second);
417                         }
418                 }
419                 ServerInstance->GlobalCulls.Apply();
420         }
421 }
422
423 std::string& ModuleManager::LastError()
424 {
425         return LastModuleError;
426 }
427
428 void ModuleManager::AddService(ServiceProvider& item)
429 {
430         switch (item.service)
431         {
432                 case SERVICE_COMMAND:
433                         if (!ServerInstance->Parser->AddCommand(static_cast<Command*>(&item)))
434                                 throw ModuleException("Command "+std::string(item.name)+" already exists.");
435                         return;
436                 case SERVICE_MODE:
437                         if (!ServerInstance->Modes->AddMode(static_cast<ModeHandler*>(&item)))
438                                 throw ModuleException("Mode "+std::string(item.name)+" already exists.");
439                         DataProviders.insert(std::make_pair("mode/" + item.name, &item));
440                         dynamic_reference_base::reset_all();
441                         return;
442                 case SERVICE_METADATA:
443                         if (!ServerInstance->Extensions.Register(static_cast<ExtensionItem*>(&item)))
444                                 throw ModuleException("Extension " + std::string(item.name) + " already exists.");
445                         return;
446                 case SERVICE_DATA:
447                 case SERVICE_IOHOOK:
448                 {
449                         if (item.name.substr(0, 5) == "mode/")
450                                 throw ModuleException("The \"mode/\" service name prefix is reserved.");
451
452                         DataProviders.insert(std::make_pair(item.name, &item));
453                         std::string::size_type slash = item.name.find('/');
454                         if (slash != std::string::npos)
455                         {
456                                 DataProviders.insert(std::make_pair(item.name.substr(0, slash), &item));
457                                 DataProviders.insert(std::make_pair(item.name.substr(slash + 1), &item));
458                         }
459                         dynamic_reference_base::reset_all();
460                         return;
461                 }
462                 default:
463                         throw ModuleException("Cannot add unknown service type");
464         }
465 }
466
467 void ModuleManager::DelService(ServiceProvider& item)
468 {
469         switch (item.service)
470         {
471                 case SERVICE_MODE:
472                         if (!ServerInstance->Modes->DelMode(static_cast<ModeHandler*>(&item)))
473                                 throw ModuleException("Mode "+std::string(item.name)+" does not exist.");
474                         // Fall through
475                 case SERVICE_DATA:
476                 case SERVICE_IOHOOK:
477                 {
478                         for(std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.begin(); i != DataProviders.end(); )
479                         {
480                                 std::multimap<std::string, ServiceProvider*>::iterator curr = i++;
481                                 if (curr->second == &item)
482                                         DataProviders.erase(curr);
483                         }
484                         dynamic_reference_base::reset_all();
485                         return;
486                 }
487                 default:
488                         throw ModuleException("Cannot delete unknown service type");
489         }
490 }
491
492 ServiceProvider* ModuleManager::FindService(ServiceType type, const std::string& name)
493 {
494         switch (type)
495         {
496                 case SERVICE_DATA:
497                 case SERVICE_IOHOOK:
498                 {
499                         std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.find(name);
500                         if (i != DataProviders.end() && i->second->service == type)
501                                 return i->second;
502                         return NULL;
503                 }
504                 // TODO implement finding of the other types
505                 default:
506                         throw ModuleException("Cannot find unknown service type");
507         }
508 }
509
510 dynamic_reference_base::dynamic_reference_base(Module* Creator, const std::string& Name)
511         : name(Name), value(NULL), creator(Creator)
512 {
513         if (!dynrefs)
514                 dynrefs = new std::vector<dynamic_reference_base*>;
515         dynrefs->push_back(this);
516         if (dynref_init_complete)
517                 resolve();
518 }
519
520 dynamic_reference_base::~dynamic_reference_base()
521 {
522         for(unsigned int i = 0; i < dynrefs->size(); i++)
523         {
524                 if (dynrefs->at(i) == this)
525                 {
526                         unsigned int last = dynrefs->size() - 1;
527                         if (i != last)
528                                 dynrefs->at(i) = dynrefs->at(last);
529                         dynrefs->erase(dynrefs->begin() + last);
530                         if (dynrefs->empty())
531                         {
532                                 delete dynrefs;
533                                 dynrefs = NULL;
534                         }
535                         return;
536                 }
537         }
538 }
539
540 void dynamic_reference_base::SetProvider(const std::string& newname)
541 {
542         name = newname;
543         resolve();
544 }
545
546 void dynamic_reference_base::resolve()
547 {
548         std::multimap<std::string, ServiceProvider*>::iterator i = ServerInstance->Modules->DataProviders.find(name);
549         if (i != ServerInstance->Modules->DataProviders.end())
550                 value = static_cast<DataProvider*>(i->second);
551         else
552                 value = NULL;
553 }
554
555 void InspIRCd::SendGlobalMode(const std::vector<std::string>& parameters, User *user)
556 {
557         Modes->Process(parameters, user);
558         if (!Modes->GetLastParse().empty())
559                 this->PI->SendMode(parameters[0], Modes->GetLastParseParams(), Modes->GetLastParseTranslate());
560 }
561
562 Module* ModuleManager::Find(const std::string &name)
563 {
564         std::map<std::string, Module*>::iterator modfind = Modules.find(name);
565
566         if (modfind == Modules.end())
567                 return NULL;
568         else
569                 return modfind->second;
570 }
571
572 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
573 {
574         std::vector<std::string> retval;
575         for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
576                 if (!filter || (x->second->GetVersion().Flags & filter))
577                         retval.push_back(x->first);
578         return retval;
579 }
580
581 FileReader::FileReader(const std::string& filename)
582 {
583         Load(filename);
584 }
585
586 void FileReader::Load(const std::string& filename)
587 {
588         // If the file is stored in the file cache then we used that version instead.
589         ConfigFileCache::iterator it = ServerInstance->Config->Files.find(filename);
590         if (it != ServerInstance->Config->Files.end())
591         {
592                 this->lines = it->second;
593         }
594         else
595         {
596                 lines.clear();
597
598                 std::ifstream stream(filename.c_str());
599                 if (!stream.is_open())
600                         throw CoreException(filename + " does not exist or is not readable!");
601
602                 std::string line;
603                 while (std::getline(stream, line))
604                 {
605                         lines.push_back(line);
606                         totalSize += line.size() + 2;
607                 }
608
609                 stream.close();
610         }
611 }
612
613 std::string FileReader::GetString()
614 {
615         std::string buffer;
616         for (file_cache::iterator it = this->lines.begin(); it != this->lines.end(); ++it)
617         {
618                 buffer.append(*it);
619                 buffer.append("\r\n");
620         }
621         return buffer;
622 }