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