]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
7df7579bf84172afbce39afc41206bc0d9000e36
[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 "dns.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
41 void dynamic_reference_base::reset_all()
42 {
43         if (!dynrefs)
44                 return;
45         for(unsigned int i = 0; i < dynrefs->size(); i++)
46                 (*dynrefs)[i]->ClearCache();
47 }
48
49 // Version is a simple class for holding a modules version number
50 Version::Version(const std::string &desc, int flags) : description(desc), Flags(flags)
51 {
52 }
53
54 Version::Version(const std::string &desc, int flags, const std::string& linkdata)
55 : description(desc), Flags(flags), link_data(linkdata)
56 {
57 }
58
59 Request::Request(Module* src, Module* dst, const char* idstr)
60 : id(idstr), source(src), dest(dst)
61 {
62 }
63
64 void Request::Send()
65 {
66         if (dest)
67                 dest->OnRequest(*this);
68 }
69
70 Event::Event(Module* src, const std::string &eventid) : source(src), id(eventid) { }
71
72 void Event::Send()
73 {
74         FOREACH_MOD(I_OnEvent,OnEvent(*this));
75 }
76
77 // These declarations define the behavours of the base class Module (which does nothing at all)
78
79 Module::Module() { }
80 CullResult Module::cull()
81 {
82         return classbase::cull();
83 }
84 Module::~Module()
85 {
86 }
87
88 ModResult       Module::OnSendSnotice(char &snomask, std::string &type, const std::string &message) { return MOD_RES_PASSTHRU; }
89 void            Module::OnUserConnect(LocalUser*) { }
90 void            Module::OnUserQuit(User*, const std::string&, const std::string&) { }
91 void            Module::OnUserDisconnect(LocalUser*) { }
92 void            Module::OnUserJoin(Membership*, bool, bool, CUList&) { }
93 void            Module::OnPostJoin(Membership*) { }
94 void            Module::OnUserPart(Membership*, std::string&, CUList&) { }
95 void            Module::OnPreRehash(User*, const std::string&) { }
96 void            Module::OnModuleRehash(User*, const std::string&) { }
97 void            Module::OnRehash(User*) { }
98 ModResult       Module::OnUserPreJoin(User*, Channel*, const char*, std::string&, const std::string&) { return MOD_RES_PASSTHRU; }
99 void            Module::OnMode(User*, void*, int, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
100 void            Module::OnOper(User*, const std::string&) { }
101 void            Module::OnPostOper(User*, const std::string&, const std::string &) { }
102 void            Module::OnPostDeoper(User*) { }
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::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 ModResult       Module::OnAddBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
140 ModResult       Module::OnDelBan(User*, Channel*, const std::string &) { return MOD_RES_PASSTHRU; }
141 void            Module::OnStreamSocketAccept(StreamSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { }
142 int             Module::OnStreamSocketWrite(StreamSocket*, std::string&) { return -1; }
143 void            Module::OnStreamSocketClose(StreamSocket*) { }
144 void            Module::OnStreamSocketConnect(StreamSocket*) { }
145 int             Module::OnStreamSocketRead(StreamSocket*, std::string&) { return -1; }
146 void            Module::OnUserMessage(User*, void*, int, const std::string&, char, const CUList&) { }
147 void            Module::OnUserNotice(User*, void*, int, const std::string&, char, const CUList&) { }
148 void            Module::OnRemoteKill(User*, User*, const std::string&, const std::string&) { }
149 void            Module::OnUserInvite(User*, User*, Channel*, time_t) { }
150 void            Module::OnPostTopicChange(User*, Channel*, const std::string&) { }
151 void            Module::OnGetServerDescription(const std::string&, std::string&) { }
152 void            Module::OnSyncUser(User*, Module*, void*) { }
153 void            Module::OnSyncChannel(Channel*, Module*, void*) { }
154 void            Module::OnSyncNetwork(Module*, void*) { }
155 void            Module::ProtoSendMode(void*, TargetTypeFlags, void*, const std::vector<std::string>&, const std::vector<TranslateType>&) { }
156 void            Module::OnDecodeMetaData(Extensible*, const std::string&, const std::string&) { }
157 void            Module::ProtoSendMetaData(void*, Extensible*, const std::string&, const std::string&) { }
158 void            Module::OnWallops(User*, const std::string&) { }
159 void            Module::OnChangeHost(User*, const std::string&) { }
160 void            Module::OnChangeName(User*, const std::string&) { }
161 void            Module::OnChangeIdent(User*, const std::string&) { }
162 void            Module::OnAddLine(User*, XLine*) { }
163 void            Module::OnDelLine(User*, XLine*) { }
164 void            Module::OnExpireLine(XLine*) { }
165 void            Module::OnCleanup(int, void*) { }
166 ModResult       Module::OnChannelPreDelete(Channel*) { return MOD_RES_PASSTHRU; }
167 void            Module::OnChannelDelete(Channel*) { }
168 ModResult       Module::OnSetAway(User*, const std::string &) { return MOD_RES_PASSTHRU; }
169 ModResult       Module::OnWhoisLine(User*, User*, int&, std::string&) { return MOD_RES_PASSTHRU; }
170 void            Module::OnBuildNeighborList(User*, UserChanList&, std::map<User*,bool>&) { }
171 void            Module::OnGarbageCollect() { }
172 ModResult       Module::OnSetConnectClass(LocalUser* user, ConnectClass* myclass) { return MOD_RES_PASSTHRU; }
173 void            Module::OnText(User*, void*, int, const std::string&, char, CUList&) { }
174 void            Module::OnRunTestSuite() { }
175 void            Module::OnNamesListItem(User*, Membership*, std::string&, std::string&) { }
176 ModResult       Module::OnNumeric(User*, unsigned int, const std::string&) { return MOD_RES_PASSTHRU; }
177 void            Module::OnHookIO(StreamSocket*, ListenSocket*) { }
178 ModResult   Module::OnAcceptConnection(int, ListenSocket*, irc::sockets::sockaddrs*, irc::sockets::sockaddrs*) { return MOD_RES_PASSTHRU; }
179 void            Module::OnSendWhoLine(User*, const std::vector<std::string>&, User*, std::string&) { }
180 void            Module::OnSetUserIP(LocalUser*) { }
181
182 ModuleManager::ModuleManager() : ModCount(0)
183 {
184 }
185
186 ModuleManager::~ModuleManager()
187 {
188 }
189
190 bool ModuleManager::Attach(Implementation i, Module* mod)
191 {
192         if (std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod) != EventHandlers[i].end())
193                 return false;
194
195         EventHandlers[i].push_back(mod);
196         return true;
197 }
198
199 bool ModuleManager::Detach(Implementation i, Module* mod)
200 {
201         EventHandlerIter x = std::find(EventHandlers[i].begin(), EventHandlers[i].end(), mod);
202
203         if (x == EventHandlers[i].end())
204                 return false;
205
206         EventHandlers[i].erase(x);
207         return true;
208 }
209
210 void ModuleManager::Attach(Implementation* i, Module* mod, size_t sz)
211 {
212         for (size_t n = 0; n < sz; ++n)
213                 Attach(i[n], mod);
214 }
215
216 void ModuleManager::DetachAll(Module* mod)
217 {
218         for (size_t n = I_BEGIN + 1; n != I_END; ++n)
219                 Detach((Implementation)n, mod);
220 }
221
222 bool ModuleManager::SetPriority(Module* mod, Priority s)
223 {
224         for (size_t n = I_BEGIN + 1; n != I_END; ++n)
225                 SetPriority(mod, (Implementation)n, s);
226
227         return true;
228 }
229
230 bool ModuleManager::SetPriority(Module* mod, Implementation i, Priority s, Module* which)
231 {
232         /** To change the priority of a module, we first find its position in the vector,
233          * then we find the position of the other modules in the vector that this module
234          * wants to be before/after. We pick off either the first or last of these depending
235          * on which they want, and we make sure our module is *at least* before or after
236          * the first or last of this subset, depending again on the type of priority.
237          */
238         size_t my_pos = 0;
239
240         /* Locate our module. This is O(n) but it only occurs on module load so we're
241          * not too bothered about it
242          */
243         for (size_t x = 0; x != EventHandlers[i].size(); ++x)
244         {
245                 if (EventHandlers[i][x] == mod)
246                 {
247                         my_pos = x;
248                         goto found_src;
249                 }
250         }
251
252         /* Eh? this module doesnt exist, probably trying to set priority on an event
253          * theyre not attached to.
254          */
255         return false;
256
257 found_src:
258         size_t swap_pos = my_pos;
259         switch (s)
260         {
261                 case PRIORITY_FIRST:
262                         if (prioritizationState != PRIO_STATE_FIRST)
263                                 return true;
264                         else
265                                 swap_pos = 0;
266                         break;
267                 case PRIORITY_LAST:
268                         if (prioritizationState != PRIO_STATE_FIRST)
269                                 return true;
270                         else
271                                 swap_pos = EventHandlers[i].size() - 1;
272                         break;
273                 case PRIORITY_AFTER:
274                 {
275                         /* Find the latest possible position, only searching AFTER our position */
276                         for (size_t x = EventHandlers[i].size() - 1; x > my_pos; --x)
277                         {
278                                 if (EventHandlers[i][x] == which)
279                                 {
280                                         swap_pos = x;
281                                         goto swap_now;
282                                 }
283                         }
284                         // didn't find it - either not loaded or we're already after
285                         return true;
286                 }
287                 /* Place this module before a set of other modules */
288                 case PRIORITY_BEFORE:
289                 {
290                         for (size_t x = 0; x < my_pos; ++x)
291                         {
292                                 if (EventHandlers[i][x] == which)
293                                 {
294                                         swap_pos = x;
295                                         goto swap_now;
296                                 }
297                         }
298                         // didn't find it - either not loaded or we're already before
299                         return true;
300                 }
301         }
302
303 swap_now:
304         /* Do we need to swap? */
305         if (swap_pos != my_pos)
306         {
307                 // We are going to change positions; we'll need to run again to verify all requirements
308                 if (prioritizationState == PRIO_STATE_LAST)
309                         prioritizationState = PRIO_STATE_AGAIN;
310                 /* Suggestion from Phoenix, "shuffle" the modules to better retain call order */
311                 int incrmnt = 1;
312
313                 if (my_pos > swap_pos)
314                         incrmnt = -1;
315
316                 for (unsigned int j = my_pos; j != swap_pos; j += incrmnt)
317                 {
318                         if ((j + incrmnt > EventHandlers[i].size() - 1) || ((incrmnt == -1) && (j == 0)))
319                                 continue;
320
321                         std::swap(EventHandlers[i][j], EventHandlers[i][j+incrmnt]);
322                 }
323         }
324
325         return true;
326 }
327
328 bool ModuleManager::CanUnload(Module* mod)
329 {
330         std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
331
332         if ((modfind == Modules.end()) || (modfind->second != mod) || (mod->dying))
333         {
334                 LastModuleError = "Module " + mod->ModuleSourceFile + " is not loaded, cannot unload it!";
335                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
336                 return false;
337         }
338         if (mod->GetVersion().Flags & VF_STATIC)
339         {
340                 LastModuleError = "Module " + mod->ModuleSourceFile + " not unloadable (marked static)";
341                 ServerInstance->Logs->Log("MODULE", DEFAULT, LastModuleError);
342                 return false;
343         }
344
345         mod->dying = true;
346         return true;
347 }
348
349 void ModuleManager::DoSafeUnload(Module* mod)
350 {
351         std::map<std::string, Module*>::iterator modfind = Modules.find(mod->ModuleSourceFile);
352
353         std::vector<reference<ExtensionItem> > items;
354         ServerInstance->Extensions.BeginUnregister(modfind->second, items);
355         /* Give the module a chance to tidy out all its metadata */
356         for (chan_hash::iterator c = ServerInstance->chanlist->begin(); c != ServerInstance->chanlist->end(); )
357         {
358                 Channel* chan = c->second;
359                 ++c;
360                 mod->OnCleanup(TYPE_CHANNEL, chan);
361                 chan->doUnhookExtensions(items);
362                 const UserMembList* users = chan->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(); )
367         {
368                 User* user = u->second;
369                 // The module may quit the user (e.g. SSL mod unloading) and that will remove it from the container
370                 ++u;
371                 mod->OnCleanup(TYPE_USER, user);
372                 user->doUnhookExtensions(items);
373         }
374         for(char m='A'; m <= 'z'; m++)
375         {
376                 ModeHandler* mh;
377                 mh = ServerInstance->Modes->FindMode(m, MODETYPE_USER);
378                 if (mh && mh->creator == mod)
379                         ServerInstance->Modes->DelMode(mh);
380                 mh = ServerInstance->Modes->FindMode(m, MODETYPE_CHANNEL);
381                 if (mh && mh->creator == mod)
382                         ServerInstance->Modes->DelMode(mh);
383         }
384         for(std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.begin(); i != DataProviders.end(); )
385         {
386                 std::multimap<std::string, ServiceProvider*>::iterator curr = i++;
387                 if (curr->second->creator == mod)
388                         DataProviders.erase(curr);
389         }
390
391         dynamic_reference_base::reset_all();
392
393         /* Tidy up any dangling resolvers */
394         ServerInstance->Res->CleanResolvers(mod);
395
396         FOREACH_MOD(I_OnUnloadModule,OnUnloadModule(mod));
397
398         DetachAll(mod);
399
400         Modules.erase(modfind);
401         ServerInstance->GlobalCulls.AddItem(mod);
402
403         ServerInstance->Logs->Log("MODULE", DEFAULT,"Module %s unloaded",mod->ModuleSourceFile.c_str());
404         this->ModCount--;
405         ServerInstance->BuildISupport();
406 }
407
408 void ModuleManager::UnloadAll()
409 {
410         /* We do this more than once, so that any service providers get a
411          * chance to be unhooked by the modules using them, but then get
412          * a chance to be removed themsleves.
413          *
414          * Note: this deliberately does NOT delete the DLLManager objects
415          */
416         for (int tries = 0; tries < 4; tries++)
417         {
418                 std::map<std::string, Module*>::iterator i = Modules.begin();
419                 while (i != Modules.end())
420                 {
421                         std::map<std::string, Module*>::iterator me = i++;
422                         if (CanUnload(me->second))
423                         {
424                                 DoSafeUnload(me->second);
425                         }
426                 }
427                 ServerInstance->GlobalCulls.Apply();
428         }
429 }
430
431 std::string& ModuleManager::LastError()
432 {
433         return LastModuleError;
434 }
435
436 CmdResult InspIRCd::CallCommandHandler(const std::string &commandname, const std::vector<std::string>& parameters, User* user)
437 {
438         return this->Parser->CallHandler(commandname, parameters, user);
439 }
440
441 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, User* user)
442 {
443         return this->Parser->IsValidCommand(commandname, pcnt, user);
444 }
445
446 void ModuleManager::AddService(ServiceProvider& item)
447 {
448         switch (item.service)
449         {
450                 case SERVICE_COMMAND:
451                         if (!ServerInstance->Parser->AddCommand(static_cast<Command*>(&item)))
452                                 throw ModuleException("Command "+std::string(item.name)+" already exists.");
453                         return;
454                 case SERVICE_MODE:
455                         if (!ServerInstance->Modes->AddMode(static_cast<ModeHandler*>(&item)))
456                                 throw ModuleException("Mode "+std::string(item.name)+" already exists.");
457                         return;
458                 case SERVICE_METADATA:
459                         if (!ServerInstance->Extensions.Register(static_cast<ExtensionItem*>(&item)))
460                                 throw ModuleException("Extension " + std::string(item.name) + " already exists.");
461                         return;
462                 case SERVICE_DATA:
463                 case SERVICE_IOHOOK:
464                 {
465                         DataProviders.insert(std::make_pair(item.name, &item));
466                         std::string::size_type slash = item.name.find('/');
467                         if (slash != std::string::npos)
468                         {
469                                 DataProviders.insert(std::make_pair(item.name.substr(0, slash), &item));
470                                 DataProviders.insert(std::make_pair(item.name.substr(slash + 1), &item));
471                         }
472                         return;
473                 }
474                 default:
475                         throw ModuleException("Cannot add unknown service type");
476         }
477 }
478
479 void ModuleManager::DelService(ServiceProvider& item)
480 {
481         switch (item.service)
482         {
483                 case SERVICE_MODE:
484                         if (!ServerInstance->Modes->DelMode(static_cast<ModeHandler*>(&item)))
485                                 throw ModuleException("Mode "+std::string(item.name)+" does not exist.");
486                         return;
487                 case SERVICE_DATA:
488                 case SERVICE_IOHOOK:
489                 {
490                         for(std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.begin(); i != DataProviders.end(); )
491                         {
492                                 std::multimap<std::string, ServiceProvider*>::iterator curr = i++;
493                                 if (curr->second == &item)
494                                         DataProviders.erase(curr);
495                         }
496                         dynamic_reference_base::reset_all();
497                         return;
498                 }
499                 default:
500                         throw ModuleException("Cannot delete unknown service type");
501         }
502 }
503
504 ServiceProvider* ModuleManager::FindService(ServiceType type, const std::string& name)
505 {
506         switch (type)
507         {
508                 case SERVICE_DATA:
509                 case SERVICE_IOHOOK:
510                 {
511                         std::multimap<std::string, ServiceProvider*>::iterator i = DataProviders.find(name);
512                         if (i != DataProviders.end() && i->second->service == type)
513                                 return i->second;
514                         return NULL;
515                 }
516                 // TODO implement finding of the other types
517                 default:
518                         throw ModuleException("Cannot find unknown service type");
519         }
520 }
521
522 dynamic_reference_base::dynamic_reference_base(Module* Creator, const std::string& Name)
523         : name(Name), value(NULL), creator(Creator)
524 {
525         if (!dynrefs)
526                 dynrefs = new std::vector<dynamic_reference_base*>;
527         dynrefs->push_back(this);
528 }
529
530 dynamic_reference_base::~dynamic_reference_base()
531 {
532         for(unsigned int i = 0; i < dynrefs->size(); i++)
533         {
534                 if (dynrefs->at(i) == this)
535                 {
536                         unsigned int last = dynrefs->size() - 1;
537                         if (i != last)
538                                 dynrefs->at(i) = dynrefs->at(last);
539                         dynrefs->erase(dynrefs->begin() + last);
540                         if (dynrefs->empty())
541                         {
542                                 delete dynrefs;
543                                 dynrefs = NULL;
544                         }
545                         return;
546                 }
547         }
548 }
549
550 void dynamic_reference_base::SetProvider(const std::string& newname)
551 {
552         name = newname;
553         ClearCache();
554 }
555
556 void dynamic_reference_base::lookup()
557 {
558         if (!*this)
559                 throw ModuleException("Dynamic reference to '" + name + "' failed to resolve");
560 }
561
562 dynamic_reference_base::operator bool()
563 {
564         if (!value)
565         {
566                 std::multimap<std::string, ServiceProvider*>::iterator i = ServerInstance->Modules->DataProviders.find(name);
567                 if (i != ServerInstance->Modules->DataProviders.end())
568                         value = static_cast<DataProvider*>(i->second);
569         }
570         return (value != NULL);
571 }
572
573 void InspIRCd::SendMode(const std::vector<std::string>& parameters, User *user)
574 {
575         this->Modes->Process(parameters, user);
576 }
577
578
579 void InspIRCd::SendGlobalMode(const std::vector<std::string>& parameters, User *user)
580 {
581         Modes->Process(parameters, user);
582         if (!Modes->GetLastParse().empty())
583                 this->PI->SendMode(parameters[0], Modes->GetLastParseParams(), Modes->GetLastParseTranslate());
584 }
585
586 bool InspIRCd::AddResolver(Resolver* r, bool cached)
587 {
588         if (!cached)
589                 return this->Res->AddResolverClass(r);
590         else
591         {
592                 r->TriggerCachedResult();
593                 delete r;
594                 return true;
595         }
596 }
597
598 Module* ModuleManager::Find(const std::string &name)
599 {
600         std::map<std::string, Module*>::iterator modfind = Modules.find(name);
601
602         if (modfind == Modules.end())
603                 return NULL;
604         else
605                 return modfind->second;
606 }
607
608 const std::vector<std::string> ModuleManager::GetAllModuleNames(int filter)
609 {
610         std::vector<std::string> retval;
611         for (std::map<std::string, Module*>::iterator x = Modules.begin(); x != Modules.end(); ++x)
612                 if (!filter || (x->second->GetVersion().Flags & filter))
613                         retval.push_back(x->first);
614         return retval;
615 }
616
617 ConfigReader::ConfigReader()
618 {
619         this->error = 0;
620         ServerInstance->Logs->Log("MODULE", DEBUG, "ConfigReader is deprecated in 2.0; "
621                 "use ServerInstance->Config->ConfValue(\"key\") or ->ConfTags(\"key\") instead");
622 }
623
624
625 ConfigReader::~ConfigReader()
626 {
627 }
628
629 static ConfigTag* SlowGetTag(const std::string &tag, int index)
630 {
631         ConfigTagList tags = ServerInstance->Config->ConfTags(tag);
632         while (tags.first != tags.second)
633         {
634                 if (!index)
635                         return tags.first->second;
636                 tags.first++;
637                 index--;
638         }
639         return NULL;
640 }
641
642 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
643 {
644         std::string result = default_value;
645         ConfigTag* conftag = SlowGetTag(tag, index);
646         if (!conftag || !conftag->readString(name, result, allow_linefeeds))
647         {
648                 this->error = CONF_VALUE_NOT_FOUND;
649         }
650         return result;
651 }
652
653 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
654 {
655         return ReadValue(tag, name, "", index, allow_linefeeds);
656 }
657
658 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
659 {
660         bool def = (default_value == "yes");
661         ConfigTag* conftag = SlowGetTag(tag, index);
662         return conftag ? conftag->getBool(name, def) : def;
663 }
664
665 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
666 {
667         return ReadFlag(tag, name, "", index);
668 }
669
670
671 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool need_positive)
672 {
673         int v = atoi(default_value.c_str());
674         ConfigTag* conftag = SlowGetTag(tag, index);
675         int result = conftag ? conftag->getInt(name, v) : v;
676
677         if ((need_positive) && (result < 0))
678         {
679                 this->error = CONF_INT_NEGATIVE;
680                 return 0;
681         }
682
683         return result;
684 }
685
686 int ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool need_positive)
687 {
688         return ReadInteger(tag, name, "", index, need_positive);
689 }
690
691 long ConfigReader::GetError()
692 {
693         long olderr = this->error;
694         this->error = 0;
695         return olderr;
696 }
697
698 int ConfigReader::Enumerate(const std::string &tag)
699 {
700         ServerInstance->Logs->Log("MODULE", DEBUG, "Module is using ConfigReader::Enumerate on %s; this is slow!",
701                 tag.c_str());
702         int i=0;
703         while (SlowGetTag(tag, i)) i++;
704         return i;
705 }
706
707 FileReader::FileReader(const std::string &filename)
708 {
709         LoadFile(filename);
710 }
711
712 FileReader::FileReader()
713 {
714 }
715
716 std::string FileReader::Contents()
717 {
718         std::string x;
719         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
720         {
721                 x.append(*a);
722                 x.append("\r\n");
723         }
724         return x;
725 }
726
727 unsigned long FileReader::ContentSize()
728 {
729         return this->contentsize;
730 }
731
732 void FileReader::CalcSize()
733 {
734         unsigned long n = 0;
735         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
736                 n += (a->length() + 2);
737         this->contentsize = n;
738 }
739
740 void FileReader::LoadFile(const std::string &filename)
741 {
742         std::map<std::string, file_cache>::iterator file = ServerInstance->Config->Files.find(filename);
743         if (file != ServerInstance->Config->Files.end())
744         {
745                 this->fc = file->second;
746         }
747         else
748         {
749                 fc.clear();
750                 FILE* f = fopen(filename.c_str(), "r");
751                 if (!f)
752                         return;
753                 char linebuf[MAXBUF*10];
754                 while (fgets(linebuf, sizeof(linebuf), f))
755                 {
756                         int len = strlen(linebuf);
757                         if (len)
758                                 fc.push_back(std::string(linebuf, len - 1));
759                 }
760                 fclose(f);
761         }
762         CalcSize();
763 }
764
765
766 FileReader::~FileReader()
767 {
768 }
769
770 bool FileReader::Exists()
771 {
772         return (!(fc.size() == 0));
773 }
774
775 std::string FileReader::GetLine(int x)
776 {
777         if ((x<0) || ((unsigned)x>=fc.size()))
778                 return "";
779         return fc[x];
780 }
781
782 int FileReader::FileSize()
783 {
784         return fc.size();
785 }