]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Merge insp20
[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 "inspircd.h"
28 #include "xline.h"
29 #include "socket.h"
30 #include "socketengine.h"
31 #include "command_parse.h"
32 #include "exitcodes.h"
33
34 #ifndef _WIN32
35         #include <dirent.h>
36 #endif
37
38 static std::vector<dynamic_reference_base*>* dynrefs = NULL;
39 static bool dynref_init_complete = false;
40
41 void dynamic_reference_base::reset_all()
42 {
43         dynref_init_complete = true;
44         if (!dynrefs)
45                 return;
46         for(unsigned int i = 0; i < dynrefs->size(); i++)
47                 (*dynrefs)[i]->resolve();
48 }
49
50 // Version is a simple class for holding a modules version number
51 Version::Version(const std::string &desc, int flags) : description(desc), Flags(flags)
52 {
53 }
54
55 Version::Version(const std::string &desc, int flags, const std::string& linkdata)
56 : description(desc), Flags(flags), link_data(linkdata)
57 {
58 }
59
60 Request::Request(Module* src, Module* dst, const char* idstr)
61 : id(idstr), source(src), dest(dst)
62 {
63 }
64
65 void Request::Send()
66 {
67         if (dest)
68                 dest->OnRequest(*this);
69 }
70
71 Event::Event(Module* src, const std::string &eventid) : source(src), id(eventid) { }
72
73 void Event::Send()
74 {
75         FOREACH_MOD(I_OnEvent,OnEvent(*this));
76 }
77
78 // These declarations define the behavours of the base class Module (which does nothing at all)
79
80 Module::Module() { }
81 CullResult Module::cull()
82 {
83         return classbase::cull();
84 }
85 Module::~Module()
86 {
87 }
88
89 ModResult       Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
90 void            Module::OnUserConnect(LocalUser*) { }
91 void            Module::OnUserQuit(User*, const std::string&, const std::string&) { }
92 void            Module::OnUserDisconnect(LocalUser*) { }
93 void            Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
94 void            Module::OnPostJoin(Membership*) { }
95 void            Module::OnUserPart(Membership*, std::string&, CUList&) { }
96 void            Module::OnPreRehash(User*, const std::string&) { }
97 void            Module::OnModuleRehash(User*, const std::string&) { }
98 void            Module::OnRehash(User*) { }
99 ModResult       Module::OnUserPreJoin(LocalUser*, Channel*, const std::string&, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
100 void            Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
101 void            Module::OnOper(User*, const std::string&) { }
102 void            Module::OnPostOper(User*, const std::string&, const std::string &) { }
103 void            Module::OnInfo(User*) { }
104 void            Module::OnWhois(User*, User*) { }
105 ModResult       Module::OnUserPreInvite(User*, User*, Channel*, time_t) { return MOD_RES_PASSTHRU; }
106 ModResult       Module::OnUserPreMessage(User*, void*, int, std::string&, char, CUList&) { return MOD_RES_PASSTHRU; }
107 ModResult       Module::OnUserPreNotice(User*, void*, int, std::string&, char, CUList&) { 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&) { }
145 void            Module::OnUserNotice(User*, void*, int, const std::string&, char, const CUList&) { }
146 void            Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
147 void            Module::OnUserInvite(User*, User*, Channel*, time_t) { }
148 void            Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
149 void            Module::OnGetServerDescription(const std::string&, std::string&) { }
150 void            Module::OnSyncUser(User*, Module*, void*) { }
151 void            Module::OnSyncChannel(Channel*, Module*, void*) { }
152 void            Module::OnSyncNetwork(Module*, void*) { }
153 void            Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
154 void            Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
155 void            Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
156 void            Module::OnWallops(User*, const std::string&) { }
157 void            Module::OnChangeHost(User*, const std::string&) { }
158 void            Module::OnChangeName(User*, const std::string&) { }
159 void            Module::OnChangeIdent(User*, const std::string&) { }
160 void            Module::OnAddLine(User*, XLine*) { }
161 void            Module::OnDelLine(User*, XLine*) { }
162 void            Module::OnExpireLine(XLine*) { }
163 void            Module::OnCleanup(int, void*) { }
164 ModResult       Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
165 void            Module::OnChannelDelete(Channel*) { }
166 ModResult       Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
167 ModResult       Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
168 void            Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
169 void            Module::OnGarbageCollect() { }
170 ModResult       Module::OnSetConnectClass(LocalUser* user, ConnectClass* myclass) { return MOD_RES_PASSTHRU; }
171 void            Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
172 void            Module::OnRunTestSuite() { }
173 void            Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
174 ModResult       Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
175 void            Module::OnHookIO(StreamSocket*, ListenSocket*) { }
176 ModResult   Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { return MOD_RES_PASSTHRU; }
177 void            Module::OnSendWhoLine(User*, const std::vector<std::string>&, User*, std::string&) { }
178 void            Module::OnSetUserIP(LocalUser*) { }
179
180 ModuleManager::ModuleManager() : ModCount(0)
181 {
182 }
183
184 ModuleManager::~ModuleManager()
185 {
186 }
187
188 bool ModuleManager::Attach(Implementation i, Module* mod)
189 {
190         if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
191                 return false;
192
193         EventHandlers[i].push_back(mod);
194         return true;
195 }
196
197 bool ModuleManager::Detach(Implementation i, Module* mod)
198 {
199         EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
200
201         if (x == EventHandlers[i].end())
202                 return false;
203
204         EventHandlers[i].erase(x);
205         return true;
206 }
207
208 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
209 {
210         for (size_t n = 0; n < sz; ++n)
211                 Attach(i[n], mod);
212 }
213
214 void ModuleManager::DetachAll(Module* mod)
215 {
216         for (size_t n = I_BEGIN + 1; n != I_END; ++n)
217                 Detach((Implementation)n, mod);
218 }
219
220 bool ModuleManager::SetPriority(Module* mod, Priority s)
221 {
222         for (size_t n = I_BEGIN + 1; n != I_END; ++n)
223                 SetPriority(mod, (Implementation)n, s);
224
225         return true;
226 }
227
228 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module* which)
229 {
230         /** To change the priority of a module, we first find its position in the vector,
231          * then we find the position of the other modules in the vector that this module
232          * wants to be before/after. We pick off either the first or last of these depending
233          * on which they want, and we make sure our module is *at least* before or after
234          * the first or last of this subset, depending again on the type of priority.
235          */
236         size_t my_pos = 0;
237
238         /* Locate our module. This is O(n) but it only occurs on module load so we're
239          * not too bothered about it
240          */
241         for (size_t x = 0; x != EventHandlers[i].size(); ++x)
242         {
243                 if (EventHandlers[i][x] == mod)
244                 {
245                         my_pos = x;
246                         goto found_src;
247                 }
248         }
249
250         /* Eh? this module doesnt exist, probably trying to set priority on an event
251          * theyre not attached to.
252          */
253         return false;
254
255 found_src:
256         size_t swap_pos = my_pos;
257         switch (s)
258         {
259                 case PRIORITY_FIRST:
260                         if (prioritizationState != PRIO_STATE_FIRST)
261                                 return true;
262                         else
263                                 swap_pos = 0;
264                         break;
265                 case PRIORITY_LAST:
266                         if (prioritizationState != PRIO_STATE_FIRST)
267                                 return true;
268                         else
269                                 swap_pos = EventHandlers[i].size() - 1;
270                         break;
271                 case PRIORITY_AFTER:
272                 {
273                         /* Find the latest possible position, only searching AFTER our position */
274                         for (size_t x = EventHandlers[i].size() - 1; x > my_pos; --x)
275                         {
276                                 if (EventHandlers[i][x] == which)
277                                 {
278                                         swap_pos = x;
279                                         goto swap_now;
280                                 }
281                         }
282                         // didn't find it - either not loaded or we're already after
283                         return true;
284                 }
285                 /* Place this module before a set of other modules */
286                 case PRIORITY_BEFORE:
287                 {
288                         for (size_t x = 0; x < my_pos; ++x)
289                         {
290                                 if (EventHandlers[i][x] == which)
291                                 {
292                                         swap_pos = x;
293                                         goto swap_now;
294                                 }
295                         }
296                         // didn't find it - either not loaded or we're already before
297                         return true;
298                 }
299         }
300
301 swap_now:
302         /* Do we need to swap? */
303         if (swap_pos != my_pos)
304         {
305                 // We are going to change positions; we'll need to run again to verify all requirements
306                 if (prioritizationState == PRIO_STATE_LAST)
307                         prioritizationState = PRIO_STATE_AGAIN;
308                 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
309                 int incrmnt = 1;
310
311                 if (my_pos > swap_pos)
312                         incrmnt = -1;
313
314                 for (unsigned int j = my_pos; j != swap_pos; j += incrmnt)
315                 {
316                         if ((j + incrmnt > EventHandlers[i].size() - 1) || ((incrmnt == -1) && (j == 0)))
317                                 continue;
318
319                         std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
320                 }
321         }
322
323         return true;
324 }
325
326 bool ModuleManager::CanUnload(Module* mod)
327 {
328         std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
329
330         if ((modfind == Modules.end()) || (modfind->second != mod) || (mod->dying))
331         {
332                 LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
333                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
334                 return false;
335         }
336         if (mod->GetVersion().Flags & VF_STATIC)
337         {
338                 LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
339                 ServerInstance->Logs->Log("MODULE", LOG_DEFAULT, LastModuleError);
340                 return false;
341         }
342
343         mod->dying = true;
344         return true;
345 }
346
347 void ModuleManager::DoSafeUnload(Module* mod)
348 {
349         // First, notify all modules that a module is about to be unloaded, so in case
350         // they pass execution to the soon to be unloaded module, it will happen now,
351         // i.e. before we unregister the services of the module being unloaded
352         FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
353
354         std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
355
356         std::vector<reference<ExtensionItem> > items;
357         ServerInstance->Extensions.BeginUnregister(modfind->second, items);
358         /* Give the module a chance to tidy out all its metadata */
359         for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); c++)
360         {
361                 mod->OnCleanup(TYPE_CHANNEL,c->second);
362                 c->second->doUnhookExtensions(items);
363                 const UserMembList* users = c->second->GetUsers();
364                 for(UserMembCIter mi = users->begin(); mi != users->end(); mi++)
365                         mi->second->doUnhookExtensions(items);
366         }
367         for (user_hash::iterator u = ServerInstance->Users->clientlist->begin(); u != ServerInstance->Users->clientlist->end(); u++)
368         {
369                 mod->OnCleanup(TYPE_USER,u->second);
370                 u->second->doUnhookExtensions(items);
371         }
372         for(char m='A'; m <= 'z'; m++)
373         {
374                 ModeHandler* mh;
375                 mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
376                 if (mh && mh->creator == mod)
377                         this->DelService(*mh);
378                 mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
379                 if (mh && mh->creator == mod)
380                         this->DelService(*mh);
381         }
382         for(std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.begin(); i != DataProviders.end(); )
383         {
384                 std::multimap<std::string, ServiceProvider*>::iterator curr = i++;
385                 if (curr->second->creator == mod)
386                         DataProviders.erase(curr);
387         }
388
389         dynamic_reference_base::reset_all();
390
391         DetachAll(mod);
392
393         Modules.erase(modfind);
394         ServerInstance->GlobalCulls.AddItem(mod);
395
396         ServerInstance->Logs->Log("MODULE", LOG_DEFAULT,"Module %s unloaded",mod->ModuleSourceFile.c_str());
397         this->ModCount--;
398         ServerInstance->ISupport.Build();
399 }
400
401 void ModuleManager::UnloadAll()
402 {
403         /* We do this more than once, so that any service providers get a
404          * chance to be unhooked by the modules using them, but then get
405          * a chance to be removed themsleves.
406          *
407          * Note: this deliberately does NOT delete the DLLManager objects
408          */
409         for (int tries = 0; tries < 4; tries++)
410         {
411                 std::map<std::string, Module*>::iterator i = Modules.begin();
412                 while (i != Modules.end())
413                 {
414                         std::map<std::string, Module*>::iterator me = i++;
415                         if (CanUnload(me->second))
416                         {
417                                 DoSafeUnload(me->second);
418                         }
419                 }
420                 ServerInstance->GlobalCulls.Apply();
421         }
422 }
423
424 std::string& ModuleManager::LastError()
425 {
426         return LastModuleError;
427 }
428
429 void ModuleManager::AddService(ServiceProvider& item)
430 {
431         switch (item.service)
432         {
433                 case SERVICE_COMMAND:
434                         if (!ServerInstance->Parser->AddCommand(static_cast<Command*>(&item)))
435                                 throw ModuleException("Command "+std::string(item.name)+" already exists.");
436                         return;
437                 case SERVICE_MODE:
438                         if (!ServerInstance->Modes->AddMode(static_cast<ModeHandler*>(&item)))
439                                 throw ModuleException("Mode "+std::string(item.name)+" already exists.");
440                         DataProviders.insert(std::make_pair("mode/" + item.name, &item));
441                         dynamic_reference_base::reset_all();
442                         return;
443                 case SERVICE_METADATA:
444                         if (!ServerInstance->Extensions.Register(static_cast<ExtensionItem*>(&item)))
445                                 throw ModuleException("Extension " + std::string(item.name) + " already exists.");
446                         return;
447                 case SERVICE_DATA:
448                 case SERVICE_IOHOOK:
449                 {
450                         if (item.name.substr(0, 5) == "mode/")
451                                 throw ModuleException("The \"mode/\" service name prefix is reserved.");
452
453                         DataProviders.insert(std::make_pair(item.name, &item));
454                         std::string::size_type slash = item.name.find('/');
455                         if (slash != std::string::npos)
456                         {
457                                 DataProviders.insert(std::make_pair(item.name.substr(0, slash), &item));
458                                 DataProviders.insert(std::make_pair(item.name.substr(slash + 1), &item));
459                         }
460                         dynamic_reference_base::reset_all();
461                         return;
462                 }
463                 default:
464                         throw ModuleException("Cannot add unknown service type");
465         }
466 }
467
468 void ModuleManager::DelService(ServiceProvider& item)
469 {
470         switch (item.service)
471         {
472                 case SERVICE_MODE:
473                         if (!ServerInstance->Modes->DelMode(static_cast<ModeHandler*>(&item)))
474                                 throw ModuleException("Mode "+std::string(item.name)+" does not exist.");
475                         // Fall through
476                 case SERVICE_DATA:
477                 case SERVICE_IOHOOK:
478                 {
479                         for(std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.begin(); i != DataProviders.end(); )
480                         {
481                                 std::multimap<std::string, ServiceProvider*>::iterator curr = i++;
482                                 if (curr->second == &item)
483                                         DataProviders.erase(curr);
484                         }
485                         dynamic_reference_base::reset_all();
486                         return;
487                 }
488                 default:
489                         throw ModuleException("Cannot delete unknown service type");
490         }
491 }
492
493 ServiceProvider* ModuleManager::FindService(ServiceType type, const std::string& name)
494 {
495         switch (type)
496         {
497                 case SERVICE_DATA:
498                 case SERVICE_IOHOOK:
499                 {
500                         std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.find(name);
501                         if (i != DataProviders.end() && i->second->service == type)
502                                 return i->second;
503                         return NULL;
504                 }
505                 // TODO implement finding of the other types
506                 default:
507                         throw ModuleException("Cannot find unknown service type");
508         }
509 }
510
511 dynamic_reference_base::dynamic_reference_base(Module* Creator, const std::string& Name)
512         : name(Name), value(NULL), creator(Creator)
513 {
514         if (!dynrefs)
515                 dynrefs = new std::vector<dynamic_reference_base*>;
516         dynrefs->push_back(this);
517         if (dynref_init_complete)
518                 resolve();
519 }
520
521 dynamic_reference_base::~dynamic_reference_base()
522 {
523         for(unsigned int i = 0; i < dynrefs->size(); i++)
524         {
525                 if (dynrefs->at(i) == this)
526                 {
527                         unsigned int last = dynrefs->size() - 1;
528                         if (i != last)
529                                 dynrefs->at(i) = dynrefs->at(last);
530                         dynrefs->erase(dynrefs->begin() + last);
531                         if (dynrefs->empty())
532                         {
533                                 delete dynrefs;
534                                 dynrefs = NULL;
535                         }
536                         return;
537                 }
538         }
539 }
540
541 void dynamic_reference_base::SetProvider(const std::string& newname)
542 {
543         name = newname;
544         resolve();
545 }
546
547 void dynamic_reference_base::resolve()
548 {
549         std::multimap<std::string, ServiceProvider*>::iterator i = ServerInstance->Modules->DataProviders.find(name);
550         if (i != ServerInstance->Modules->DataProviders.end())
551                 value = static_cast<DataProvider*>(i->second);
552         else
553                 value = NULL;
554 }
555
556 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
557 {
558         this->Modes->Process(parameters, user);
559 }
560
561
562 void InspIRCd::SendGlobalMode(const std::vector<std::string>& parameters, User *user)
563 {
564         Modes->Process(parameters, user);
565         if (!Modes->GetLastParse().empty())
566                 this->PI->SendMode(parameters[0], Modes->GetLastParseParams(), Modes->GetLastParseTranslate());
567 }
568
569 Module* ModuleManager::Find(const std::string &name)
570 {
571         std::map<std::string, Module*>::iterator modfind = Modules.find(name);
572
573         if (modfind == Modules.end())
574                 return NULL;
575         else
576                 return modfind->second;
577 }
578
579 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
580 {
581         std::vector<std::string> retval;
582         for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
583                 if (!filter || (x->second->GetVersion().Flags & filter))
584                         retval.push_back(x->first);
585         return retval;
586 }
587
588 FileReader::FileReader(const std::string &filename)
589 {
590         LoadFile(filename);
591 }
592
593 FileReader::FileReader()
594 {
595 }
596
597 std::string FileReader::Contents()
598 {
599         std::string x;
600         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
601         {
602                 x.append(*a);
603                 x.append("\r\n");
604         }
605         return x;
606 }
607
608 unsigned long FileReader::ContentSize()
609 {
610         return this->contentsize;
611 }
612
613 void FileReader::CalcSize()
614 {
615         unsigned long n = 0;
616         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
617                 n += (a->length() + 2);
618         this->contentsize = n;
619 }
620
621 void FileReader::LoadFile(const std::string &filename)
622 {
623         std::map<std::string, file_cache>::iterator file = ServerInstance->Config->Files.find(filename);
624         if (file != ServerInstance->Config->Files.end())
625         {
626                 this->fc = file->second;
627         }
628         else
629         {
630                 fc.clear();
631                 FILE* f = fopen(filename.c_str(), "r");
632                 if (!f)
633                         return;
634                 char linebuf[MAXBUF*10];
635                 while (fgets(linebuf, sizeof(linebuf), f))
636                 {
637                         int len = strlen(linebuf);
638                         if (len)
639                                 fc.push_back(std::string(linebuf, len - 1));
640                 }
641                 fclose(f);
642         }
643         CalcSize();
644 }
645
646
647 FileReader::~FileReader()
648 {
649 }
650
651 bool FileReader::Exists()
652 {
653         return (!(fc.size() == 0));
654 }
655
656 std::string FileReader::GetLine(int x)
657 {
658         if ((x<0) || ((unsigned)x>fc.size()))
659                 return "";
660         return fc[x];
661 }
662
663 int FileReader::FileSize()
664 {
665         return fc.size();
666 }