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