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