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