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