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