]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
When opening a file to read, check its not a directory with stat()
[user/henk/code/inspircd.git] / src / modules.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "configreader.h"
15 #include "users.h"
16 #include "modules.h"
17 #include "wildcard.h"
18 #include "mode.h"
19 #include "xline.h"
20 #include "socket.h"
21 #include "socketengine.h"
22 #include "command_parse.h"
23 #include "dns.h"
24 #include "inspircd.h"
25
26 // version is a simple class for holding a modules version number
27 Version::Version(int major, int minor, int revision, int build, int flags, int api_ver)
28 : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags), API(api_ver)
29 {
30 }
31
32 Request::Request(char* anydata, Module* src, Module* dst)
33 : data(anydata), source(src), dest(dst)
34 {
35         /* Ensure that because this module doesnt support ID strings, it doesnt break modules that do
36          * by passing them uninitialized pointers (could happen)
37          */
38         id = '\0';
39 }
40
41 Request::Request(Module* src, Module* dst, const char* idstr)
42 : id(idstr), source(src), dest(dst)
43 {
44 };
45
46 char* Request::GetData()
47 {
48         return this->data;
49 }
50
51 const char* Request::GetId()
52 {
53         return this->id;
54 }
55
56 Module* Request::GetSource()
57 {
58         return this->source;
59 }
60
61 Module* Request::GetDest()
62 {
63         return this->dest;
64 }
65
66 char* Request::Send()
67 {
68         if (this->dest)
69         {
70                 return dest->OnRequest(this);
71         }
72         else
73         {
74                 return NULL;
75         }
76 }
77
78 Event::Event(char* anydata, Module* src, const std::string &eventid) : data(anydata), source(src), id(eventid) { };
79
80 char* Event::GetData()
81 {
82         return (char*)this->data;
83 }
84
85 Module* Event::GetSource()
86 {
87         return this->source;
88 }
89
90 char* Event::Send(InspIRCd* ServerInstance)
91 {
92         FOREACH_MOD(I_OnEvent,OnEvent(this));
93         return NULL;
94 }
95
96 std::string Event::GetEventID()
97 {
98         return this->id;
99 }
100
101
102 // These declarations define the behavours of the base class Module (which does nothing at all)
103
104                 Module::Module(InspIRCd* Me) : ServerInstance(Me) { }
105                 Module::~Module() { }
106 void            Module::OnUserConnect(userrec* user) { }
107 void            Module::OnUserQuit(userrec* user, const std::string& message) { }
108 void            Module::OnUserDisconnect(userrec* user) { }
109 void            Module::OnUserJoin(userrec* user, chanrec* channel) { }
110 void            Module::OnPostJoin(userrec* user, chanrec* channel) { }
111 void            Module::OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage) { }
112 void            Module::OnRehash(userrec* user, const std::string &parameter) { }
113 void            Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
114 int             Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname, std::string &privs) { return 0; }
115 void            Module::OnMode(userrec* user, void* dest, int target_type, const std::string &text) { };
116 Version         Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR,-1); }
117 void            Module::OnOper(userrec* user, const std::string &opertype) { };
118 void            Module::OnPostOper(userrec* user, const std::string &opertype) { };
119 void            Module::OnInfo(userrec* user) { };
120 void            Module::OnWhois(userrec* source, userrec* dest) { };
121 int             Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
122 int             Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status, CUList &exempt_list) { return 0; };
123 int             Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status, CUList &exempt_list) { return 0; };
124 int             Module::OnUserPreNick(userrec* user, const std::string &newnick) { return 0; };
125 void            Module::OnUserPostNick(userrec* user, const std::string &oldnick) { };
126 int             Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
127 void            Module::On005Numeric(std::string &output) { };
128 int             Module::OnKill(userrec* source, userrec* dest, const std::string &reason) { return 0; };
129 void            Module::OnLoadModule(Module* mod,const std::string &name) { };
130 void            Module::OnUnloadModule(Module* mod,const std::string &name) { };
131 void            Module::OnBackgroundTimer(time_t curtime) { };
132 int             Module::OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line) { return 0; };
133 void            Module::OnPostCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, CmdResult result, const std::string &original_line) { };
134 bool            Module::OnCheckReady(userrec* user) { return true; };
135 int             Module::OnUserRegister(userrec* user) { return 0; };
136 int             Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { return 0; };
137 void            Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason) { };
138 int             Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
139 int             Module::OnCheckKey(userrec* user, chanrec* chan, const std::string &keygiven) { return 0; };
140 int             Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
141 int             Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
142 int             Module::OnStats(char symbol, userrec* user, string_list &results) { return 0; };
143 int             Module::OnChangeLocalUserHost(userrec* user, const std::string &newhost) { return 0; };
144 int             Module::OnChangeLocalUserGECOS(userrec* user, const std::string &newhost) { return 0; };
145 int             Module::OnLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { return 0; };
146 void            Module::OnEvent(Event* event) { return; };
147 char*           Module::OnRequest(Request* request) { return NULL; };
148 int             Module::OnOperCompare(const std::string &password, const std::string &input, int tagnumber) { return 0; };
149 void            Module::OnGlobalOper(userrec* user) { };
150 void            Module::OnPostConnect(userrec* user) { };
151 int             Module::OnAddBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
152 int             Module::OnDelBan(userrec* source, chanrec* channel,const std::string &banmask) { return 0; };
153 void            Module::OnRawSocketAccept(int fd, const std::string &ip, int localport) { };
154 int             Module::OnRawSocketWrite(int fd, const char* buffer, int count) { return 0; };
155 void            Module::OnRawSocketClose(int fd) { };
156 void            Module::OnRawSocketConnect(int fd) { };
157 int             Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
158 void            Module::OnUserMessage(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list) { };
159 void            Module::OnUserNotice(userrec* user, void* dest, int target_type, const std::string &text, char status, const CUList &exempt_list) { };
160 void            Module::OnRemoteKill(userrec* source, userrec* dest, const std::string &reason) { };
161 void            Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
162 void            Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, const std::string &topic) { };
163 void            Module::OnGetServerDescription(const std::string &servername,std::string &description) { };
164 void            Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
165 void            Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
166 void            Module::ProtoSendMode(void* opaque, int target_type, void* target, const std::string &modeline) { };
167 void            Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, const std::string &extname) { };
168 void            Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname) { };
169 void            Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
170 void            Module::OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata) { };
171 void            Module::ProtoSendMetaData(void* opaque, int target_type, void* target, const std::string &extname, const std::string &extdata) { };
172 void            Module::OnWallops(userrec* user, const std::string &text) { };
173 void            Module::OnChangeHost(userrec* user, const std::string &newhost) { };
174 void            Module::OnChangeName(userrec* user, const std::string &gecos) { };
175 void            Module::OnAddGLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
176 void            Module::OnAddZLine(long duration, userrec* source, const std::string &reason, const std::string &ipmask) { };
177 void            Module::OnAddKLine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
178 void            Module::OnAddQLine(long duration, userrec* source, const std::string &reason, const std::string &nickmask) { };
179 void            Module::OnAddELine(long duration, userrec* source, const std::string &reason, const std::string &hostmask) { };
180 void            Module::OnDelGLine(userrec* source, const std::string &hostmask) { };
181 void            Module::OnDelZLine(userrec* source, const std::string &ipmask) { };
182 void            Module::OnDelKLine(userrec* source, const std::string &hostmask) { };
183 void            Module::OnDelQLine(userrec* source, const std::string &nickmask) { };
184 void            Module::OnDelELine(userrec* source, const std::string &hostmask) { };
185 void            Module::OnCleanup(int target_type, void* item) { };
186 void            Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
187 void            Module::OnChannelDelete(chanrec* chan) { };
188 Priority        Module::Prioritize() { return PRIORITY_DONTCARE; }
189 void            Module::OnSetAway(userrec* user) { };
190 void            Module::OnCancelAway(userrec* user) { };
191 int             Module::OnUserList(userrec* user, chanrec* Ptr) { return 0; };
192 int             Module::OnWhoisLine(userrec* user, userrec* dest, int &numeric, std::string &text) { return 0; };
193 void            Module::OnBuildExemptList(MessageType message_type, chanrec* chan, userrec* sender, char status, CUList &exempt_list) { };
194 void            Module::OnGarbageCollect() { };
195
196 long InspIRCd::PriorityAfter(const std::string &modulename)
197 {
198         for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
199         {
200                 if (this->Config->module_names[j] == modulename)
201                 {
202                         return ((j << 8) | PRIORITY_AFTER);
203                 }
204         }
205         return PRIORITY_DONTCARE;
206 }
207
208 long InspIRCd::PriorityBefore(const std::string &modulename)
209 {
210         for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
211         {
212                 if (this->Config->module_names[j] == modulename)
213                 {
214                         return ((j << 8) | PRIORITY_BEFORE);
215                 }
216         }
217         return PRIORITY_DONTCARE;
218 }
219
220 bool InspIRCd::PublishFeature(const std::string &FeatureName, Module* Mod)
221 {
222         if (Features.find(FeatureName) == Features.end())
223         {
224                 Features[FeatureName] = Mod;
225                 return true;
226         }
227         return false;
228 }
229
230 bool InspIRCd::UnpublishFeature(const std::string &FeatureName)
231 {
232         featurelist::iterator iter = Features.find(FeatureName);
233         
234         if (iter == Features.end())
235                 return false;
236
237         Features.erase(iter);
238         return true;
239 }
240
241 Module* InspIRCd::FindFeature(const std::string &FeatureName)
242 {
243         featurelist::iterator iter = Features.find(FeatureName);
244
245         if (iter == Features.end())
246                 return NULL;
247
248         return iter->second;
249 }
250
251 bool InspIRCd::PublishInterface(const std::string &InterfaceName, Module* Mod)
252 {
253         interfacelist::iterator iter = Interfaces.find(InterfaceName);
254
255         if (iter == Interfaces.end())
256         {
257                 modulelist ml;
258                 ml.push_back(Mod);
259                 Interfaces[InterfaceName] = std::make_pair(0, ml);
260                 return true;
261         }
262         else
263         {
264                 iter->second.second.push_back(Mod);
265                 return true;
266         }
267         return false;
268 }
269
270 bool InspIRCd::UnpublishInterface(const std::string &InterfaceName, Module* Mod)
271 {
272         interfacelist::iterator iter = Interfaces.find(InterfaceName);
273
274         if (iter == Interfaces.end())
275                 return false;
276
277         for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
278         {
279                 if (*x == Mod)
280                 {
281                         iter->second.second.erase(x);
282                         if (iter->second.second.empty())
283                                 Interfaces.erase(InterfaceName);
284                         return true;
285                 }
286         }
287         return false;
288 }
289
290 modulelist* InspIRCd::FindInterface(const std::string &InterfaceName)
291 {
292         interfacelist::iterator iter = Interfaces.find(InterfaceName);
293         if (iter == Interfaces.end())
294                 return NULL;
295         else
296                 return &(iter->second.second);
297 }
298
299 void InspIRCd::UseInterface(const std::string &InterfaceName)
300 {
301         interfacelist::iterator iter = Interfaces.find(InterfaceName);
302         if (iter != Interfaces.end())
303                 iter->second.first++;
304
305 }
306
307 void InspIRCd::DoneWithInterface(const std::string &InterfaceName)
308 {
309         interfacelist::iterator iter = Interfaces.find(InterfaceName);
310         if (iter != Interfaces.end())
311                 iter->second.first--;
312 }
313
314 std::pair<int,std::string> InspIRCd::GetInterfaceInstanceCount(Module* m)
315 {
316         for (interfacelist::iterator iter = Interfaces.begin(); iter != Interfaces.end(); iter++)
317         {
318                 for (modulelist::iterator x = iter->second.second.begin(); x != iter->second.second.end(); x++)
319                 {
320                         if (*x == m)
321                         {
322                                 return std::make_pair(iter->second.first, iter->first);
323                         }
324                 }
325         }
326         return std::make_pair(0, "");
327 }
328
329 const std::string& InspIRCd::GetModuleName(Module* m)
330 {
331         static std::string nothing = ""; /* Prevent compiler warning */
332
333         if (!this->GetModuleCount())
334                 return nothing;
335
336         for (int i = 0; i <= this->GetModuleCount(); i++)
337         {
338                 if (this->modules[i] == m)
339                 {
340                         return this->Config->module_names[i];
341                 }
342         }
343         return nothing; /* As above */
344 }
345
346 void InspIRCd::RehashServer()
347 {
348         this->WriteOpers("*** Rehashing config file");
349         this->RehashUsersAndChans();
350         this->Config->Read(false,NULL);
351         this->ResetMaxBans();
352         this->Res->Rehash();
353 }
354
355 /* This is ugly, yes, but hash_map's arent designed to be
356  * addressed in this manner, and this is a bit of a kludge.
357  * Luckily its a specialist function and rarely used by
358  * many modules (in fact, it was specially created to make
359  * m_safelist possible, initially).
360  */
361
362 chanrec* InspIRCd::GetChannelIndex(long index)
363 {
364         int target = 0;
365         for (chan_hash::iterator n = this->chanlist->begin(); n != this->chanlist->end(); n++, target++)
366         {
367                 if (index == target)
368                         return n->second;
369         }
370         return NULL;
371 }
372
373 bool InspIRCd::MatchText(const std::string &sliteral, const std::string &spattern)
374 {
375         return match(sliteral.c_str(),spattern.c_str());
376 }
377
378 bool InspIRCd::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
379 {
380         return this->Parser->CallHandler(commandname,parameters,pcnt,user);
381 }
382
383 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
384 {
385         return this->Parser->IsValidCommand(commandname, pcnt, user);
386 }
387
388 void InspIRCd::AddCommand(command_t *f)
389 {
390         if (!this->Parser->CreateCommand(f))
391         {
392                 ModuleException err("Command "+std::string(f->command)+" already exists.");
393                 throw (err);
394         }
395 }
396
397 void InspIRCd::SendMode(const char** parameters, int pcnt, userrec *user)
398 {
399         this->Modes->Process(parameters,pcnt,user,true);
400 }
401
402 void InspIRCd::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
403 {
404         std::string CompleteLine = LinePrefix;
405         std::string Word = "";
406         while (TextStream >> Word)
407         {
408                 if (CompleteLine.length() + Word.length() + 3 > 500)
409                 {
410                         User->WriteServ(CompleteLine);
411                         CompleteLine = LinePrefix;
412                 }
413                 CompleteLine = CompleteLine + Word + " ";
414         }
415         User->WriteServ(CompleteLine);
416 }
417
418 userrec* InspIRCd::FindDescriptor(int socket)
419 {
420         return reinterpret_cast<userrec*>(this->SE->GetRef(socket));
421 }
422
423 bool InspIRCd::AddMode(ModeHandler* mh, const unsigned char mode)
424 {
425         return this->Modes->AddMode(mh,mode);
426 }
427
428 bool InspIRCd::AddModeWatcher(ModeWatcher* mw)
429 {
430         return this->Modes->AddModeWatcher(mw);
431 }
432
433 bool InspIRCd::DelModeWatcher(ModeWatcher* mw)
434 {
435         return this->Modes->DelModeWatcher(mw);
436 }
437
438 bool InspIRCd::AddResolver(Resolver* r, bool cached)
439 {
440         if (!cached)
441                 return this->Res->AddResolverClass(r);
442         else
443         {
444                 r->TriggerCachedResult();
445                 delete r;
446                 return true;
447         }
448 }
449
450 bool InspIRCd::UserToPseudo(userrec* user, const std::string &message)
451 {
452         unsigned int old_fd = user->GetFd();
453         user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
454         user->FlushWriteBuf();
455         user->ClearBuffer();
456         user->SetFd(FD_MAGIC_NUMBER);
457
458         if (find(local_users.begin(),local_users.end(),user) != local_users.end())
459         {
460                 local_users.erase(find(local_users.begin(),local_users.end(),user));
461         }
462
463         this->SE->DelFd(user);
464         shutdown(old_fd,2);
465         close(old_fd);
466         return true;
467 }
468
469 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
470 {
471         zombie->SetFd(alive->GetFd());
472         FOREACH_MOD_I(this,I_OnUserQuit,OnUserQuit(alive,message));
473         alive->SetFd(FD_MAGIC_NUMBER);
474         alive->FlushWriteBuf();
475         alive->ClearBuffer();
476         // save these for later
477         std::string oldnick = alive->nick;
478         std::string oldhost = alive->host;
479         std::string oldident = alive->ident;
480         userrec::QuitUser(this,alive,message.c_str());
481         if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
482         {
483                 local_users.erase(find(local_users.begin(),local_users.end(),alive));
484         }
485         // Fix by brain - cant write the user until their fd table entry is updated
486         zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
487         for (UCListIter i = zombie->chans.begin(); i != zombie->chans.end(); i++)
488         {
489                 chanrec* Ptr = i->first;
490                 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
491                 if (Ptr->topicset)
492                 {
493                         zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
494                         zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
495                 }
496                 Ptr->UserList(zombie);
497                 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
498         }
499         if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->GetFd() != FD_MAGIC_NUMBER))
500                 local_users.push_back(zombie);
501         return true;
502 }
503
504 void InspIRCd::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
505 {
506         XLines->add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
507         XLines->apply_lines(APPLY_GLINES);
508 }
509
510 void InspIRCd::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
511 {
512         XLines->add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
513         XLines->apply_lines(APPLY_QLINES);
514 }
515
516 void InspIRCd::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
517 {
518         XLines->add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
519         XLines->apply_lines(APPLY_ZLINES);
520 }
521
522 void InspIRCd::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
523 {
524         XLines->add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
525         XLines->apply_lines(APPLY_KLINES);
526 }
527
528 void InspIRCd::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
529 {
530         XLines->add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
531 }
532
533 bool InspIRCd::DelGLine(const std::string &hostmask)
534 {
535         return XLines->del_gline(hostmask.c_str());
536 }
537
538 bool InspIRCd::DelQLine(const std::string &nickname)
539 {
540         return XLines->del_qline(nickname.c_str());
541 }
542
543 bool InspIRCd::DelZLine(const std::string &ipaddr)
544 {
545         return XLines->del_zline(ipaddr.c_str());
546 }
547
548 bool InspIRCd::DelKLine(const std::string &hostmask)
549 {
550         return XLines->del_kline(hostmask.c_str());
551 }
552
553 bool InspIRCd::DelELine(const std::string &hostmask)
554 {
555         return XLines->del_eline(hostmask.c_str());
556 }
557
558 /*
559  * XXX why on *earth* is this in modules.cpp...? I think
560  * perhaps we need a server.cpp for InspIRCd:: stuff where possible. -- w00t
561  */
562 bool InspIRCd::IsValidMask(const std::string &mask)
563 {
564         char* dest = (char*)mask.c_str();
565         if (strchr(dest,'!')==0)
566                 return false;
567         if (strchr(dest,'@')==0)
568                 return false;
569         for (char* i = dest; *i; i++)
570                 if (*i < 32)
571                         return false;
572         for (char* i = dest; *i; i++)
573                 if (*i > 126)
574                         return false;
575         unsigned int c = 0;
576         for (char* i = dest; *i; i++)
577                 if (*i == '!')
578                         c++;
579         if (c>1)
580                 return false;
581         c = 0;
582         for (char* i = dest; *i; i++)
583                 if (*i == '@')
584                         c++;
585         if (c>1)
586                 return false;
587
588         return true;
589 }
590
591 Module* InspIRCd::FindModule(const std::string &name)
592 {
593         for (int i = 0; i <= this->GetModuleCount(); i++)
594         {
595                 if (this->Config->module_names[i] == name)
596                 {
597                         return this->modules[i];
598                 }
599         }
600         return NULL;
601 }
602
603 ConfigReader::ConfigReader(InspIRCd* Instance) : ServerInstance(Instance)
604 {
605         /* Is there any reason to load the entire config file again here?
606          * it's needed if they specify another config file, but using the
607          * default one we can just use the global config data - pre-parsed!
608          */
609         this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
610         
611         this->data = &ServerInstance->Config->config_data;
612         this->privatehash = false;
613 }
614
615
616 ConfigReader::~ConfigReader()
617 {
618         if (this->errorlog)
619                 DELETE(this->errorlog);
620         if(this->privatehash)
621                 DELETE(this->data);
622 }
623
624
625 ConfigReader::ConfigReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
626 {
627         ServerInstance->Config->ClearStack();
628
629         this->data = new ConfigDataHash;
630         this->privatehash = true;
631         this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
632         this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
633         if (!this->readerror)
634                 this->error = CONF_FILE_NOT_FOUND;
635 };
636
637
638 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool allow_linefeeds)
639 {
640         /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */ 
641         std::string result;
642         
643         if (!ServerInstance->Config->ConfValue(*this->data, tag, name, default_value, index, result, allow_linefeeds))
644         {
645                 this->error = CONF_VALUE_NOT_FOUND;
646         }
647         return result;
648 }
649
650 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index, bool allow_linefeeds)
651 {
652         return ReadValue(tag, name, "", index, allow_linefeeds);
653 }
654
655 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, const std::string &default_value, int index)
656 {
657         return ServerInstance->Config->ConfValueBool(*this->data, tag, name, default_value, index);
658 }
659
660 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
661 {
662         return ReadFlag(tag, name, "", index);
663 }
664
665
666 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, const std::string &default_value, int index, bool needs_unsigned)
667 {
668         int result;
669         
670         if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, default_value, index, result))
671         {
672                 this->error = CONF_VALUE_NOT_FOUND;
673                 return 0;
674         }
675         
676         if ((needs_unsigned) && (result < 0))
677         {
678                 this->error = CONF_NOT_UNSIGNED;
679                 return 0;
680         }
681         
682         return result;
683 }
684
685 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
686 {
687         return ReadInteger(tag, name, "", index, needs_unsigned);
688 }
689
690 long ConfigReader::GetError()
691 {
692         long olderr = this->error;
693         this->error = 0;
694         return olderr;
695 }
696
697 void ConfigReader::DumpErrors(bool bail, userrec* user)
698 {
699         ServerInstance->Config->ReportConfigError(this->errorlog->str(), bail, user);
700 }
701
702
703 int ConfigReader::Enumerate(const std::string &tag)
704 {
705         return ServerInstance->Config->ConfValueEnum(*this->data, tag);
706 }
707
708 int ConfigReader::EnumerateValues(const std::string &tag, int index)
709 {
710         return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
711 }
712
713 bool ConfigReader::Verify()
714 {
715         return this->readerror;
716 }
717
718
719 FileReader::FileReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
720 {
721         LoadFile(filename);
722 }
723
724 FileReader::FileReader(InspIRCd* Instance) : ServerInstance(Instance)
725 {
726 }
727
728 std::string FileReader::Contents()
729 {
730         std::string x = "";
731         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
732         {
733                 x.append(*a);
734                 x.append("\r\n");
735         }
736         return x;
737 }
738
739 unsigned long FileReader::ContentSize()
740 {
741         return this->contentsize;
742 }
743
744 void FileReader::CalcSize()
745 {
746         unsigned long n = 0;
747         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
748                 n += (a->length() + 2);
749         this->contentsize = n;
750 }
751
752 void FileReader::LoadFile(const std::string &filename)
753 {
754         file_cache c;
755         c.clear();
756         if (ServerInstance->Config->ReadFile(c,filename.c_str()))
757         {
758                 this->fc = c;
759                 this->CalcSize();
760         }
761 }
762
763
764 FileReader::~FileReader()
765 {
766 }
767
768 bool FileReader::Exists()
769 {
770         return (!(fc.size() == 0));
771 }
772
773 std::string FileReader::GetLine(int x)
774 {
775         if ((x<0) || ((unsigned)x>fc.size()))
776                 return "";
777         return fc[x];
778 }
779
780 int FileReader::FileSize()
781 {
782         return fc.size();
783 }
784
785