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