]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Add support for NAMESX
[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
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::OnPostConnect(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 int             Module::OnUserList(userrec* user, chanrec* Ptr) { return 0; };
213
214 /* server is a wrapper class that provides methods to all of the C-style
215  * exports in the core
216  */
217
218 void InspIRCd::AddSocket(InspSocket* sock)
219 {
220         this->module_sockets.push_back(sock);
221 }
222
223 void InspIRCd::RemoveSocket(InspSocket* sock)
224 {
225         for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
226         {
227                 InspSocket* s = (InspSocket*)*a;
228                 if (s == sock)
229                         s->MarkAsClosed();
230         }
231 }
232
233 long InspIRCd::PriorityAfter(const std::string &modulename)
234 {
235         for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
236         {
237                 if (this->Config->module_names[j] == modulename)
238                 {
239                         return ((j << 8) | PRIORITY_AFTER);
240                 }
241         }
242         return PRIORITY_DONTCARE;
243 }
244
245 long InspIRCd::PriorityBefore(const std::string &modulename)
246 {
247         for (unsigned int j = 0; j < this->Config->module_names.size(); j++)
248         {
249                 if (this->Config->module_names[j] == modulename)
250                 {
251                         return ((j << 8) | PRIORITY_BEFORE);
252                 }
253         }
254         return PRIORITY_DONTCARE;
255 }
256
257 bool InspIRCd::PublishFeature(const std::string &FeatureName, Module* Mod)
258 {
259         if (Features.find(FeatureName) == Features.end())
260         {
261                 Features[FeatureName] = Mod;
262                 return true;
263         }
264         return false;
265 }
266
267 bool InspIRCd::UnpublishFeature(const std::string &FeatureName)
268 {
269         featurelist::iterator iter = Features.find(FeatureName);
270         
271         if (iter == Features.end())
272                 return false;
273
274         Features.erase(iter);
275         return true;
276 }
277
278 Module* InspIRCd::FindFeature(const std::string &FeatureName)
279 {
280         featurelist::iterator iter = Features.find(FeatureName);
281
282         if (iter == Features.end())
283                 return NULL;
284
285         return iter->second;
286 }
287
288 const std::string& InspIRCd::GetModuleName(Module* m)
289 {
290         static std::string nothing = ""; /* Prevent compiler warning */
291         for (int i = 0; i <= this->GetModuleCount(); i++)
292         {
293                 if (this->modules[i] == m)
294                 {
295                         return this->Config->module_names[i];
296                 }
297         }
298         return nothing; /* As above */
299 }
300
301 void InspIRCd::RehashServer()
302 {
303         this->WriteOpers("*** Rehashing config file");
304         this->Config->Read(false,NULL);
305 }
306
307 void InspIRCd::DelSocket(InspSocket* sock)
308 {
309         for (std::vector<InspSocket*>::iterator a = this->module_sockets.begin(); a < this->module_sockets.end(); a++)
310         {
311                 if (*a == sock)
312                 {
313                         this->module_sockets.erase(a);
314                         return;
315                 }
316         }
317 }
318
319 /* This is ugly, yes, but hash_map's arent designed to be
320  * addressed in this manner, and this is a bit of a kludge.
321  * Luckily its a specialist function and rarely used by
322  * many modules (in fact, it was specially created to make
323  * m_safelist possible, initially).
324  */
325
326 chanrec* InspIRCd::GetChannelIndex(long index)
327 {
328         int target = 0;
329         for (chan_hash::iterator n = this->chanlist.begin(); n != this->chanlist.end(); n++, target++)
330         {
331                 if (index == target)
332                         return n->second;
333         }
334         return NULL;
335 }
336
337 bool InspIRCd::MatchText(const std::string &sliteral, const std::string &spattern)
338 {
339         return match(sliteral.c_str(),spattern.c_str());
340 }
341
342 bool InspIRCd::CallCommandHandler(const std::string &commandname, const char** parameters, int pcnt, userrec* user)
343 {
344         return this->Parser->CallHandler(commandname,parameters,pcnt,user);
345 }
346
347 bool InspIRCd::IsValidModuleCommand(const std::string &commandname, int pcnt, userrec* user)
348 {
349         return this->Parser->IsValidCommand(commandname, pcnt, user);
350 }
351
352 void InspIRCd::AddCommand(command_t *f)
353 {
354         if (!this->Parser->CreateCommand(f))
355         {
356                 ModuleException err("Command "+std::string(f->command)+" already exists.");
357                 throw (err);
358         }
359 }
360
361 void InspIRCd::SendMode(const char** parameters, int pcnt, userrec *user)
362 {
363         this->Modes->Process(parameters,pcnt,user,true);
364 }
365
366 void InspIRCd::DumpText(userrec* User, const std::string &LinePrefix, stringstream &TextStream)
367 {
368         std::string CompleteLine = LinePrefix;
369         std::string Word = "";
370         while (TextStream >> Word)
371         {
372                 if (CompleteLine.length() + Word.length() + 3 > 500)
373                 {
374                         User->WriteServ(CompleteLine);
375                         CompleteLine = LinePrefix;
376                 }
377                 CompleteLine = CompleteLine + Word + " ";
378         }
379         User->WriteServ(CompleteLine);
380 }
381
382 userrec* InspIRCd::FindDescriptor(int socket)
383 {
384         return reinterpret_cast<userrec*>(this->SE->GetRef(socket));
385 }
386
387 bool InspIRCd::AddMode(ModeHandler* mh, const unsigned char mode)
388 {
389         return this->Modes->AddMode(mh,mode);
390 }
391
392 bool InspIRCd::AddModeWatcher(ModeWatcher* mw)
393 {
394         return this->Modes->AddModeWatcher(mw);
395 }
396
397 bool InspIRCd::DelModeWatcher(ModeWatcher* mw)
398 {
399         return this->Modes->DelModeWatcher(mw);
400 }
401
402 bool InspIRCd::AddResolver(Resolver* r)
403 {
404         return this->Res->AddResolverClass(r);
405 }
406
407 bool InspIRCd::UserToPseudo(userrec* user, const std::string &message)
408 {
409         unsigned int old_fd = user->GetFd();
410         user->Write("ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
411         user->FlushWriteBuf();
412         user->ClearBuffer();
413         user->SetFd(FD_MAGIC_NUMBER);
414
415         if (find(local_users.begin(),local_users.end(),user) != local_users.end())
416         {
417                 local_users.erase(find(local_users.begin(),local_users.end(),user));
418         }
419
420         this->SE->DelFd(user);
421         shutdown(old_fd,2);
422         close(old_fd);
423         return true;
424 }
425
426 bool InspIRCd::PseudoToUser(userrec* alive, userrec* zombie, const std::string &message)
427 {
428         zombie->SetFd(alive->GetFd());
429         FOREACH_MOD_I(this,I_OnUserQuit,OnUserQuit(alive,message));
430         alive->SetFd(FD_MAGIC_NUMBER);
431         alive->FlushWriteBuf();
432         alive->ClearBuffer();
433         // save these for later
434         std::string oldnick = alive->nick;
435         std::string oldhost = alive->host;
436         std::string oldident = alive->ident;
437         userrec::QuitUser(this,alive,message.c_str());
438         if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
439         {
440                 local_users.erase(find(local_users.begin(),local_users.end(),alive));
441         }
442         // Fix by brain - cant write the user until their fd table entry is updated
443         zombie->Write(":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
444         for (std::vector<ucrec*>::const_iterator i = zombie->chans.begin(); i != zombie->chans.end(); i++)
445         {
446                 if (((ucrec*)(*i))->channel != NULL)
447                 {
448                                 chanrec* Ptr = ((ucrec*)(*i))->channel;
449                                 zombie->WriteFrom(zombie,"JOIN %s",Ptr->name);
450                                 if (Ptr->topicset)
451                                 {
452                                         zombie->WriteServ("332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
453                                         zombie->WriteServ("333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
454                                 }
455                                 Ptr->UserList(zombie);
456                                 zombie->WriteServ("366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
457                 }
458         }
459         if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->GetFd() != FD_MAGIC_NUMBER))
460                 local_users.push_back(zombie);
461         return true;
462 }
463
464 void InspIRCd::AddGLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
465 {
466         XLines->add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
467         XLines->apply_lines(APPLY_GLINES);
468 }
469
470 void InspIRCd::AddQLine(long duration, const std::string &source, const std::string &reason, const std::string &nickname)
471 {
472         XLines->add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
473         XLines->apply_lines(APPLY_QLINES);
474 }
475
476 void InspIRCd::AddZLine(long duration, const std::string &source, const std::string &reason, const std::string &ipaddr)
477 {
478         XLines->add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
479         XLines->apply_lines(APPLY_ZLINES);
480 }
481
482 void InspIRCd::AddKLine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
483 {
484         XLines->add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
485         XLines->apply_lines(APPLY_KLINES);
486 }
487
488 void InspIRCd::AddELine(long duration, const std::string &source, const std::string &reason, const std::string &hostmask)
489 {
490         XLines->add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
491 }
492
493 bool InspIRCd::DelGLine(const std::string &hostmask)
494 {
495         return XLines->del_gline(hostmask.c_str());
496 }
497
498 bool InspIRCd::DelQLine(const std::string &nickname)
499 {
500         return XLines->del_qline(nickname.c_str());
501 }
502
503 bool InspIRCd::DelZLine(const std::string &ipaddr)
504 {
505         return XLines->del_zline(ipaddr.c_str());
506 }
507
508 bool InspIRCd::DelKLine(const std::string &hostmask)
509 {
510         return XLines->del_kline(hostmask.c_str());
511 }
512
513 bool InspIRCd::DelELine(const std::string &hostmask)
514 {
515         return XLines->del_eline(hostmask.c_str());
516 }
517
518 /*
519  * XXX why on *earth* is this in modules.cpp...? I think
520  * perhaps we need a server.cpp for InspIRCd:: stuff where possible. -- w00t
521  */
522 bool InspIRCd::IsValidMask(const std::string &mask)
523 {
524         char* dest = (char*)mask.c_str();
525         if (strchr(dest,'!')==0)
526                 return false;
527         if (strchr(dest,'@')==0)
528                 return false;
529         for (char* i = dest; *i; i++)
530                 if (*i < 32)
531                         return false;
532         for (char* i = dest; *i; i++)
533                 if (*i > 126)
534                         return false;
535         unsigned int c = 0;
536         for (char* i = dest; *i; i++)
537                 if (*i == '!')
538                         c++;
539         if (c>1)
540                 return false;
541         c = 0;
542         for (char* i = dest; *i; i++)
543                 if (*i == '@')
544                         c++;
545         if (c>1)
546                 return false;
547
548         return true;
549 }
550
551 Module* InspIRCd::FindModule(const std::string &name)
552 {
553         for (int i = 0; i <= this->GetModuleCount(); i++)
554         {
555                 if (this->Config->module_names[i] == name)
556                 {
557                         return this->modules[i];
558                 }
559         }
560         return NULL;
561 }
562
563 ConfigReader::ConfigReader(InspIRCd* Instance) : ServerInstance(Instance)
564 {
565         /* Is there any reason to load the entire config file again here?
566          * it's needed if they specify another config file, but using the
567          * default one we can just use the global config data - pre-parsed!
568          */
569         this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
570         
571         this->data = &ServerInstance->Config->config_data;
572         this->privatehash = false;
573 }
574
575
576 ConfigReader::~ConfigReader()
577 {
578         if (this->errorlog)
579                 DELETE(this->errorlog);
580         if(this->privatehash)
581                 DELETE(this->data);
582 }
583
584
585 ConfigReader::ConfigReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
586 {
587         ServerInstance->Config->ClearStack();
588
589         this->data = new ConfigDataHash;
590         this->privatehash = true;
591         this->errorlog = new std::ostringstream(std::stringstream::in | std::stringstream::out);
592         this->readerror = ServerInstance->Config->LoadConf(*this->data, filename, *this->errorlog);
593         if (!this->readerror)
594                 this->error = CONF_FILE_NOT_FOUND;
595 };
596
597 std::string ConfigReader::ReadValue(const std::string &tag, const std::string &name, int index)
598 {
599         /* Don't need to strlcpy() tag and name anymore, ReadConf() takes const char* */ 
600         std::string result;
601         
602         if (!ServerInstance->Config->ConfValue(*this->data, tag, name, index, result))
603         {
604                 this->error = CONF_VALUE_NOT_FOUND;
605                 return "";
606         }
607         
608         return result;
609 }
610
611 bool ConfigReader::ReadFlag(const std::string &tag, const std::string &name, int index)
612 {
613         return ServerInstance->Config->ConfValueBool(*this->data, tag, name, index);
614 }
615
616 long ConfigReader::ReadInteger(const std::string &tag, const std::string &name, int index, bool needs_unsigned)
617 {
618         int result;
619         
620         if(!ServerInstance->Config->ConfValueInteger(*this->data, tag, name, index, result))
621         {
622                 this->error = CONF_VALUE_NOT_FOUND;
623                 return 0;
624         }
625         
626         if ((needs_unsigned) && (result < 0))
627         {
628                 this->error = CONF_NOT_UNSIGNED;
629                 return 0;
630         }
631         
632         return result;
633 }
634
635 long ConfigReader::GetError()
636 {
637         long olderr = this->error;
638         this->error = 0;
639         return olderr;
640 }
641
642 void ConfigReader::DumpErrors(bool bail, userrec* user)
643 {
644         /* XXX - Duplicated code */
645         
646         if (bail)
647         {
648                 printf("There were errors in your configuration:\n%s", this->errorlog->str().c_str());
649                 InspIRCd::Exit(ERROR);
650         }
651         else
652         {
653                 std::string errors = this->errorlog->str();
654                 std::string::size_type start;
655                 unsigned int prefixlen;
656                 
657                 start = 0;
658                 /* ":ServerInstance->Config->ServerName NOTICE user->nick :" */
659                 prefixlen = strlen(ServerInstance->Config->ServerName) + strlen(user->nick) + 11;
660         
661                 if (user)
662                 {
663                         user->WriteServ("NOTICE %s :There were errors in the configuration file:",user->nick);
664                         
665                         while(start < errors.length())
666                         {
667                                 user->WriteServ("NOTICE %s :%s",user->nick, errors.substr(start, 510 - prefixlen).c_str());
668                                 start += 510 - prefixlen;
669                         }
670                 }
671                 else
672                 {
673                         ServerInstance->WriteOpers("There were errors in the configuration file:");
674                         
675                         while(start < errors.length())
676                         {
677                                 ServerInstance->WriteOpers(errors.substr(start, 360).c_str());
678                                 start += 360;
679                         }
680                 }
681
682                 return;
683         }
684 }
685
686
687 int ConfigReader::Enumerate(const std::string &tag)
688 {
689         return ServerInstance->Config->ConfValueEnum(*this->data, tag);
690 }
691
692 int ConfigReader::EnumerateValues(const std::string &tag, int index)
693 {
694         return ServerInstance->Config->ConfVarEnum(*this->data, tag, index);
695 }
696
697 bool ConfigReader::Verify()
698 {
699         return this->readerror;
700 }
701
702
703 FileReader::FileReader(InspIRCd* Instance, const std::string &filename) : ServerInstance(Instance)
704 {
705         file_cache c;
706         ServerInstance->Config->ReadFile(c,filename.c_str());
707         this->fc = c;
708         this->CalcSize();
709 }
710
711 FileReader::FileReader(InspIRCd* Instance) : ServerInstance(Instance)
712 {
713 }
714
715 std::string FileReader::Contents()
716 {
717         std::string x = "";
718         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
719         {
720                 x.append(*a);
721                 x.append("\r\n");
722         }
723         return x;
724 }
725
726 unsigned long FileReader::ContentSize()
727 {
728         return this->contentsize;
729 }
730
731 void FileReader::CalcSize()
732 {
733         unsigned long n = 0;
734         for (file_cache::iterator a = this->fc.begin(); a != this->fc.end(); a++)
735                 n += (a->length() + 2);
736         this->contentsize = n;
737 }
738
739 void FileReader::LoadFile(const std::string &filename)
740 {
741         file_cache c;
742         ServerInstance->Config->ReadFile(c,filename.c_str());
743         this->fc = c;
744         this->CalcSize();
745 }
746
747
748 FileReader::~FileReader()
749 {
750 }
751
752 bool FileReader::Exists()
753 {
754         return (!(fc.size() == 0));
755 }
756
757 std::string FileReader::GetLine(int x)
758 {
759         if ((x<0) || ((unsigned)x>fc.size()))
760                 return "";
761         return fc[x];
762 }
763
764 int FileReader::FileSize()
765 {
766         return fc.size();
767 }
768
769