]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
f0474500877efe136081817dd316f8dd063fc759
[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                 log(DEBUG,"Delete local user");
423         }
424
425         this->SE->DelFd(old_fd);
426         shutdown(old_fd,2);
427         close(old_fd);
428         return true;
429 }
430
431 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
432 {
433         log(DEBUG,"PseudoToUser");
434         zombie->fd = alive->fd;
435         FOREACH_MOD_I(this,I_OnUserQuit,OnUserQuit(alive,message));
436         alive->fd = FD_MAGIC_NUMBER;
437         alive->FlushWriteBuf();
438         alive->ClearBuffer();
439         // save these for later
440         std::string oldnick = alive->nick;
441         std::string oldhost = alive->host;
442         std::string oldident = alive->ident;
443         userrec::QuitUser(this,alive,message.c_str());
444         if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
445         {
446                 local_users.erase(find(local_users.begin(),local_users.end(),alive));
447                 log(DEBUG,"Delete local user");
448         }
449         // Fix by brain - cant write the user until their fd table entry is updated
450         this->fd_ref_table[zombie->fd] = zombie;
451         zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
452         for (std::vector<ucrec*>::const_iterator i = zombie->chans.begin(); i != zombie->chans.end(); i++)
453         {
454                 if (((ucrec*)(*i))->channel != NULL)
455                 {
456                                 chanrec* Ptr = ((ucrec*)(*i))->channel;
457                                 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
458                                 if (Ptr->topicset)
459                                 {
460                                         zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
461                                         zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
462                                 }
463                                 Ptr->UserList(zombie);
464                                 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
465                 }
466         }
467         if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
468                 local_users.push_back(zombie);
469
470         return true;
471 }
472
473 void InspIRCd::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
474 {
475         add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
476         apply_lines(APPLY_GLINES);
477 }
478
479 void InspIRCd::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
480 {
481         add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
482         apply_lines(APPLY_QLINES);
483 }
484
485 void InspIRCd::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
486 {
487         add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
488         apply_lines(APPLY_ZLINES);
489 }
490
491 void InspIRCd::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
492 {
493         add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
494         apply_lines(APPLY_KLINES);
495 }
496
497 void InspIRCd::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
498 {
499         add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
500 }
501
502 bool InspIRCd::DelGLine(const std::string &hostmask)
503 {
504         return del_gline(hostmask.c_str());
505 }
506
507 bool InspIRCd::DelQLine(const std::string &nickname)
508 {
509         return del_qline(nickname.c_str());
510 }
511
512 bool InspIRCd::DelZLine(const std::string &ipaddr)
513 {
514         return del_zline(ipaddr.c_str());
515 }
516
517 bool InspIRCd::DelKLine(const std::string &hostmask)
518 {
519         return del_kline(hostmask.c_str());
520 }
521
522 bool InspIRCd::DelELine(const std::string &hostmask)
523 {
524         return del_eline(hostmask.c_str());
525 }
526
527 long InspIRCd::CalcDuration(const std::string &delta)
528 {
529         return duration(delta.c_str());
530 }
531
532 /*
533  * XXX why on *earth* is this in modules.cpp...? I think
534  * perhaps we need a server.cpp for InspIRCd:: stuff where possible. -- w00t
535  */
536 bool InspIRCd::IsValidMask(const std::string &mask)
537 {
538         char* dest = (char*)mask.c_str();
539         if (strchr(dest,'!')==0)
540                 return false;
541         if (strchr(dest,'@')==0)
542                 return false;
543         for (char* i = dest; *i; i++)
544                 if (*i < 32)
545                         return false;
546         for (char* i = dest; *i; i++)
547                 if (*i > 126)
548                         return false;
549         unsigned int c = 0;
550         for (char* i = dest; *i; i++)
551                 if (*i == '!')
552                         c++;
553         if (c>1)
554                 return false;
555         c = 0;
556         for (char* i = dest; *i; i++)
557                 if (*i == '@')
558                         c++;
559         if (c>1)
560                 return false;
561
562         return true;
563 }
564
565 Module* InspIRCd::FindModule(const std::string &name)
566 {
567         for (int i = 0; i <= this->GetModuleCount(); i++)
568         {
569                 if (this->Config->module_names[i] == name)
570                 {
571                         return this->modules[i];
572                 }
573         }
574         return NULL;
575 }
576
577 ConfigReader::ConfigReader(InspIRCd* Instance) : ServerInstance(Instance)
578 {
579         /* Is there any reason to load the entire config file again here?
580          * it's needed if they specify another config file, but using the
581          * default one we can just use the global config data - pre-parsed!
582          */
583         this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
584         
585         this->data = &ServerInstance->Config->config_data;
586         this->privatehash = false;
587 }
588
589
590 ConfigReader::~ConfigReader()
591 {
592         if (this->errorlog)
593                 DELETE(this->errorlog);
594         if(this->privatehash)
595                 DELETE(this->data);
596 }
597
598
599 ConfigReader::ConfigReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
600 {
601         ServerInstance->Config->ClearStack();
602
603         this->data = new ConfigDataHash;
604         this->privatehash = true;
605         this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
606         this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
607         if (!this->readerror)
608                 this->error = CONF_FILE_NOT_FOUND;
609 };
610
611 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index)
612 {
613         /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */ 
614         std::string result;
615         
616         if (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result))
617         {
618                 this->error = CONF_VALUE_NOT_FOUND;
619                 return "";
620         }
621         
622         return result;
623 }
624
625 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
626 {
627         return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index);
628 }
629
630 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
631 {
632         int result;
633         
634         if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, index, result))
635         {
636                 this->error = CONF_VALUE_NOT_FOUND;
637                 return 0;
638         }
639         
640         if ((needs_unsigned) && (result < 0))
641         {
642                 this->error = CONF_NOT_UNSIGNED;
643                 return 0;
644         }
645         
646         return result;
647 }
648
649 long ConfigReader::GetError()
650 {
651         long olderr = this->error;
652         this->error = 0;
653         return olderr;
654 }
655
656 void ConfigReader::DumpErrors(bool bail, userrec* user)
657 {
658         /* XXX - Duplicated code */
659         
660         if (bail)
661         {
662                 printf("There were errors in your configuration:\n%s", this->errorlog->str().c_str());
663                 InspIRCd::Exit(ERROR);
664         }
665         else
666         {
667                 std::string errors = this->errorlog->str();
668                 std::string::size_type start;
669                 unsigned int prefixlen;
670                 
671                 start = 0;
672                 /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
673                 prefixlen = strlen(ServerInstance->Config->ServerName) + strlen(user->nick) + 11;
674         
675                 if (user)
676                 {
677                         user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
678                         
679                         while(start < errors.length())
680                         {
681                                 user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
682                                 start += 510 - prefixlen;
683                         }
684                 }
685                 else
686                 {
687                         ServerInstance->WriteOpers("There were errors in the configuration file:");
688                         
689                         while(start < errors.length())
690                         {
691                                 ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
692                                 start += 360;
693                         }
694                 }
695
696                 return;
697         }
698 }
699
700
701 int ConfigReader::Enumerate(const std::string &tag)
702 {
703         return ServerInstance->Config->ConfValueEnum(*this->data, tag);
704 }
705
706 int ConfigReader::EnumerateValues(const std::string &tag, int index)
707 {
708         return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
709 }
710
711 bool ConfigReader::Verify()
712 {
713         return this->readerror;
714 }
715
716
717 FileReader::FileReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
718 {
719         file_cache c;
720         ServerInstance->Config->ReadFile(c,filename.c_str());
721         this->fc = c;
722         this->CalcSize();
723 }
724
725 FileReader::FileReader(InspIRCd* Instance) : ServerInstance(Instance)
726 {
727 }
728
729 std::string FileReader::Contents()
730 {
731         std::string x = "";
732         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
733         {
734                 x.append(*a);
735                 x.append("\r\n");
736         }
737         return x;
738 }
739
740 unsigned long FileReader::ContentSize()
741 {
742         return this->contentsize;
743 }
744
745 void FileReader::CalcSize()
746 {
747         unsigned long n = 0;
748         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
749                 n += (a->length() + 2);
750         this->contentsize = n;
751 }
752
753 void FileReader::LoadFile(const std::string &filename)
754 {
755         file_cache c;
756         ServerInstance->Config->ReadFile(c,filename.c_str());
757         this->fc = c;
758         this->CalcSize();
759 }
760
761
762 FileReader::~FileReader()
763 {
764 }
765
766 bool FileReader::Exists()
767 {
768         return (!(fc.size() == 0));
769 }
770
771 std::string FileReader::GetLine(int x)
772 {
773         if ((x<0) || ((unsigned)x>fc.size()))
774                 return "";
775         return fc[x];
776 }
777
778 int FileReader::FileSize()
779 {
780         return fc.size();
781 }
782
783