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