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