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