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