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