]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Review and optimize
[user/henk/code/inspircd.git] / src / modules.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2004 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 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <sys/errno.h>
24 #include <time.h>
25 #include <string>
26 #ifdef GCC3
27 #include <ext/hash_map>
28 #else
29 #include <hash_map>
30 #endif
31 #include <map>
32 #include <sstream>
33 #include <vector>
34 #include <deque>
35 #include "users.h"
36 #include "ctables.h"
37 #include "globals.h"
38 #include "modules.h"
39 #include "dynamic.h"
40 #include "wildcard.h"
41 #include "message.h"
42 #include "mode.h"
43 #include "xline.h"
44 #include "commands.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
47 #include "hashcomp.h"
48 #include "socket.h"
49 #include "socketengine.h"
50 #include "typedefs.h"
51 #include "modules.h"
52 #include "command_parse.h"
53
54 extern ServerConfig *Config;
55 extern InspIRCd* ServerInstance;
56 extern int MODCOUNT;
57 extern std::vector<Module*> modules;
58 extern std::vector<ircd_module*> factory;
59 extern std::vector<InspSocket*> module_sockets;
60 extern time_t TIME;
61 class Server;
62 extern userrec* fd_ref_table[65536];
63
64 extern user_hash clientlist;
65 extern chan_hash chanlist;
66 extern command_table cmdlist;
67 ExtModeList EMode;
68
69 // returns true if an extended mode character is in use
70 bool ModeDefined(char modechar, int type)
71 {
72         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
73         {
74                 if ((i->modechar == modechar) && (i->type == type))
75                 {
76                         return true;
77                 }
78         }
79         return false;
80 }
81
82 bool ModeIsListMode(char modechar, int type)
83 {
84         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
85         {
86                 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
87                 {
88                         return true;
89                 }
90         }
91         return false;
92 }
93
94 bool ModeDefinedOper(char modechar, int type)
95 {
96         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
97         {
98                 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
99                 {
100                         return true;
101                 }
102         }
103         return false;
104 }
105
106 // returns number of parameters for a custom mode when it is switched on
107 int ModeDefinedOn(char modechar, int type)
108 {
109         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
110         {
111                 if ((i->modechar == modechar) && (i->type == type))
112                 {
113                         return i->params_when_on;
114                 }
115         }
116         return 0;
117 }
118
119 // returns number of parameters for a custom mode when it is switched on
120 int ModeDefinedOff(char modechar, int type)
121 {
122         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
123         {
124                 if ((i->modechar == modechar) && (i->type == type))
125                 {
126                         return i->params_when_off;
127                 }
128         }
129         return 0;
130 }
131
132 // returns true if an extended mode character is in use
133 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
134 {
135         if (ModeDefined(modechar,type)) {
136                 return false;
137         }
138         EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
139         return true;
140 }
141
142 // turns a mode into a listmode
143 void ModeMakeList(char modechar)
144 {
145         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
146         {
147                 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
148                 {
149                         i->list = true;
150                         return;
151                 }
152         }
153         return;
154 }
155
156 // version is a simple class for holding a modules version number
157
158 Version::Version(int major, int minor, int revision, int build, int flags) : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags) { };
159
160 // admin is a simple class for holding a server's administrative info
161
162 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
163
164 Request::Request(char* anydata, Module* src, Module* dst) : data(anydata), source(src), dest(dst) { };
165
166 char* Request::GetData()
167 {
168         return this->data;
169 }
170
171 Module* Request::GetSource()
172 {
173         return this->source;
174 }
175
176 Module* Request::GetDest()
177 {
178         return this->dest;
179 }
180
181 char* Request::Send()
182 {
183         if (this->dest)
184         {
185                 return dest->OnRequest(this);
186         }
187         else
188         {
189                 return NULL;
190         }
191 }
192
193 Event::Event(char* anydata, Module* src, std::string eventid) : data(anydata), source(src), id(eventid) { };
194
195 char* Event::GetData()
196 {
197         return this->data;
198 }
199
200 Module* Event::GetSource()
201 {
202         return this->source;
203 }
204
205 char* Event::Send()
206 {
207         FOREACH_MOD OnEvent(this);
208         return NULL;
209 }
210
211 std::string Event::GetEventID()
212 {
213         return this->id;
214 }
215
216
217 // These declarations define the behavours of the base class Module (which does nothing at all)
218
219                 Module::Module(Server* Me) { }
220                 Module::~Module() { }
221 void            Module::OnUserConnect(userrec* user) { }
222 void            Module::OnUserQuit(userrec* user, std::string message) { }
223 void            Module::OnUserDisconnect(userrec* user) { }
224 void            Module::OnUserJoin(userrec* user, chanrec* channel) { }
225 void            Module::OnUserPart(userrec* user, chanrec* channel) { }
226 void            Module::OnRehash(std::string parameter) { }
227 void            Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
228 int             Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
229 int             Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params) { return false; }
230 void            Module::OnMode(userrec* user, void* dest, int target_type, std::string text) { };
231 Version         Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
232 void            Module::OnOper(userrec* user, std::string opertype) { };
233 void            Module::OnInfo(userrec* user) { };
234 void            Module::OnWhois(userrec* source, userrec* dest) { };
235 int             Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
236 int             Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
237 int             Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
238 int             Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
239 void            Module::OnUserPostNick(userrec* user, std::string oldnick) { };
240 int             Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
241 void            Module::On005Numeric(std::string &output) { };
242 int             Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
243 void            Module::OnLoadModule(Module* mod,std::string name) { };
244 void            Module::OnUnloadModule(Module* mod,std::string name) { };
245 void            Module::OnBackgroundTimer(time_t curtime) { };
246 void            Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
247 int             Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated) { return 0; };
248 bool            Module::OnCheckReady(userrec* user) { return true; };
249 void            Module::OnUserRegister(userrec* user) { };
250 int             Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
251 void            Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
252 int             Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
253 int             Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
254 int             Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
255 int             Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
256 int             Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
257 void            Module::OnStats(char symbol) { };
258 int             Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
259 int             Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
260 int             Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
261 void            Module::OnEvent(Event* event) { return; };
262 char*           Module::OnRequest(Request* request) { return NULL; };
263 int             Module::OnOperCompare(std::string password, std::string input) { return 0; };
264 void            Module::OnGlobalOper(userrec* user) { };
265 void            Module::OnGlobalConnect(userrec* user) { };
266 int             Module::OnAddBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
267 int             Module::OnDelBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
268 void            Module::OnRawSocketAccept(int fd, std::string ip, int localport) { };
269 int             Module::OnRawSocketWrite(int fd, char* buffer, int count) { return 0; };
270 void            Module::OnRawSocketClose(int fd) { };
271 int             Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
272 void            Module::OnUserMessage(userrec* user, void* dest, int target_type, std::string text) { };
273 void            Module::OnUserNotice(userrec* user, void* dest, int target_type, std::string text) { };
274 void            Module::OnRemoteKill(userrec* source, userrec* dest, std::string reason) { };
275 void            Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
276 void            Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { };
277 void            Module::OnGetServerDescription(std::string servername,std::string &description) { };
278 void            Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
279 void            Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
280 void            Module::ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline) { };
281 void            Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, std::string extname) { };
282 void            Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, std::string extname) { };
283 void            Module::OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata) { };
284 void            Module::ProtoSendMetaData(void* opaque, int target_type, void* target, std::string extname, std::string extdata) { };
285 void            Module::OnWallops(userrec* user, std::string text) { };
286 void            Module::OnChangeHost(userrec* user, std::string newhost) { };
287 void            Module::OnChangeName(userrec* user, std::string gecos) { };
288 void            Module::OnAddGLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
289 void            Module::OnAddZLine(long duration, userrec* source, std::string reason, std::string ipmask) { };
290 void            Module::OnAddKLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
291 void            Module::OnAddQLine(long duration, userrec* source, std::string reason, std::string nickmask) { };
292 void            Module::OnAddELine(long duration, userrec* source, std::string reason, std::string hostmask) { };
293 void            Module::OnDelGLine(userrec* source, std::string hostmask) { };
294 void            Module::OnDelZLine(userrec* source, std::string ipmask) { };
295 void            Module::OnDelKLine(userrec* source, std::string hostmask) { };
296 void            Module::OnDelQLine(userrec* source, std::string nickmask) { };
297 void            Module::OnDelELine(userrec* source, std::string hostmask) { };
298 void            Module::OnCleanup(int target_type, void* item) { };
299
300 /* server is a wrapper class that provides methods to all of the C-style
301  * exports in the core
302  */
303
304 Server::Server()
305 {
306 }
307
308 Server::~Server()
309 {
310 }
311
312 void Server::AddSocket(InspSocket* sock)
313 {
314         module_sockets.push_back(sock);
315 }
316
317 void Server::RehashServer()
318 {
319         WriteOpers("*** Rehashing config file");
320         Config->Read(false,NULL);
321 }
322
323 ServerConfig* Server::GetConfig()
324 {
325         return Config;
326 }
327
328 std::string Server::GetVersion()
329 {
330         return ServerInstance->GetVersionString();
331 }
332
333 void Server::DelSocket(InspSocket* sock)
334 {
335         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
336         {
337                 if (*a == sock)
338                 {
339                         module_sockets.erase(a);
340                         return;
341                 }
342         }
343 }
344
345 void Server::SendOpers(std::string s)
346 {
347         WriteOpers("%s",s.c_str());
348 }
349
350 bool Server::MatchText(std::string sliteral, std::string spattern)
351 {
352         char literal[MAXBUF],pattern[MAXBUF];
353         strlcpy(literal,sliteral.c_str(),MAXBUF);
354         strlcpy(pattern,spattern.c_str(),MAXBUF);
355         return match(literal,pattern);
356 }
357
358 void Server::SendToModeMask(std::string modes, int flags, std::string text)
359 {
360         WriteMode(modes.c_str(),flags,"%s",text.c_str());
361 }
362
363 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
364 {
365         return add_channel(user,cname.c_str(),key.c_str(),false);
366 }
367
368 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
369 {
370         return del_channel(user,cname.c_str(),reason.c_str(),false);
371 }
372
373 chanuserlist Server::GetUsers(chanrec* chan)
374 {
375         chanuserlist userl;
376         userl.clear();
377         std::vector<char*> *list = chan->GetUsers();
378         for (std::vector<char*>::iterator i = list->begin(); i != list->end(); i++)
379         {
380                 char* o = *i;
381                 userl.push_back((userrec*)o);
382         }
383         return userl;
384 }
385 void Server::ChangeUserNick(userrec* user, std::string nickname)
386 {
387         force_nickchange(user,nickname.c_str());
388 }
389
390 void Server::QuitUser(userrec* user, std::string reason)
391 {
392         kill_link(user,reason.c_str());
393 }
394
395 bool Server::IsUlined(std::string server)
396 {
397         return is_uline(server.c_str());
398 }
399
400 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
401 {
402         ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
403 }
404
405 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
406 {
407         return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
408 }
409
410 void Server::Log(int level, std::string s)
411 {
412         log(level,"%s",s.c_str());
413 }
414
415 void Server::AddCommand(command_t *f)
416 {
417         ServerInstance->Parser->CreateCommand(f);
418 }
419
420 void Server::SendMode(char **parameters, int pcnt, userrec *user)
421 {
422         ServerInstance->ModeGrok->ServerMode(parameters,pcnt,user);
423 }
424
425 void Server::Send(int Socket, std::string s)
426 {
427         Write(Socket,"%s",s.c_str());
428 }
429
430 void Server::SendServ(int Socket, std::string s)
431 {
432         WriteServ(Socket,"%s",s.c_str());
433 }
434
435 void Server::SendFrom(int Socket, userrec* User, std::string s)
436 {
437         WriteFrom(Socket,User,"%s",s.c_str());
438 }
439
440 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
441 {
442         if (!Source)
443         {
444                 // if source is NULL, then the message originates from the local server
445                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
446         }
447         else
448         {
449                 // otherwise it comes from the user specified
450                 WriteTo(Source,Dest,"%s",s.c_str());
451         }
452 }
453
454 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
455 {
456         WriteChannelWithServ((char*)ServName.c_str(), Channel, "%s", text.c_str());
457 }
458
459 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
460 {
461         if (IncludeSender)
462         {
463                 WriteChannel(Channel,User,"%s",s.c_str());
464         }
465         else
466         {
467                 ChanExceptSender(Channel,User,"%s",s.c_str());
468         }
469 }
470
471 bool Server::CommonChannels(userrec* u1, userrec* u2)
472 {
473         return (common_channels(u1,u2) != 0);
474 }
475
476 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
477 {
478         if (IncludeSender)
479         {
480                 WriteCommon(User,"%s",text.c_str());
481         }
482         else
483         {
484                 WriteCommonExcept(User,"%s",text.c_str());
485         }
486 }
487
488 void Server::SendWallops(userrec* User, std::string text)
489 {
490         WriteWallOps(User,false,"%s",text.c_str());
491 }
492
493 void Server::ChangeHost(userrec* user, std::string host)
494 {
495         ChangeDisplayedHost(user,host.c_str());
496 }
497
498 void Server::ChangeGECOS(userrec* user, std::string gecos)
499 {
500         ChangeName(user,gecos.c_str());
501 }
502
503 bool Server::IsNick(std::string nick)
504 {
505         return (isnick(nick.c_str()) != 0);
506 }
507
508 userrec* Server::FindNick(std::string nick)
509 {
510         return Find(nick);
511 }
512
513 userrec* Server::FindDescriptor(int socket)
514 {
515         return (socket < 65536 ? fd_ref_table[socket] : NULL);
516 }
517
518 chanrec* Server::FindChannel(std::string channel)
519 {
520         return FindChan(channel.c_str());
521 }
522
523 std::string Server::ChanMode(userrec* User, chanrec* Chan)
524 {
525         return cmode(User,Chan);
526 }
527
528 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
529 {
530         return has_channel(User,Chan);
531 }
532
533 std::string Server::GetServerName()
534 {
535         return Config->ServerName;
536 }
537
538 std::string Server::GetNetworkName()
539 {
540         return Config->Network;
541 }
542
543 std::string Server::GetServerDescription()
544 {
545         return Config->ServerDesc;
546 }
547
548 Admin Server::GetAdmin()
549 {
550         return Admin(Config->AdminName,Config->AdminEmail,Config->AdminNick);
551 }
552
553
554
555 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
556 {
557         if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
558         {
559                 if (type == MT_SERVER)
560                 {
561                         log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
562                         return false;
563                 }
564                 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
565                 {
566                         log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
567                         return false;
568                 }
569                 if ((params_when_on>1) || (params_when_off>1))
570                 {
571                         log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
572                         return false;
573                 }
574                 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
575         }
576         else
577         {
578                 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
579         }
580         return false;
581 }
582
583 bool Server::AddExtendedListMode(char modechar)
584 {
585         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
586         if (res)
587                 ModeMakeList(modechar);
588         return res;
589 }
590
591 int Server::CountUsers(chanrec* c)
592 {
593         return usercount(c);
594 }
595
596
597 bool Server::UserToPseudo(userrec* user,std::string message)
598 {
599         unsigned int old_fd = user->fd;
600         user->fd = FD_MAGIC_NUMBER;
601         user->ClearBuffer();
602         Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
603         ServerInstance->SE->DelFd(old_fd);
604         shutdown(old_fd,2);
605         close(old_fd);
606         return true;
607 }
608
609 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
610 {
611         zombie->fd = alive->fd;
612         alive->fd = FD_MAGIC_NUMBER;
613         alive->ClearBuffer();
614         Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
615         kill_link(alive,message.c_str());
616         fd_ref_table[zombie->fd] = zombie;
617         for (unsigned int i = 0; i < zombie->chans.size(); i++)
618         {
619                 if (zombie->chans[i].channel != NULL)
620                 {
621                         if (zombie->chans[i].channel->name)
622                         {
623                                 chanrec* Ptr = zombie->chans[i].channel;
624                                 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
625                                 if (Ptr->topicset)
626                                 {
627                                         WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
628                                         WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
629                                 }
630                                 userlist(zombie,Ptr);
631                                 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
632
633                         }
634                 }
635         }
636         return true;
637 }
638
639 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
640 {
641         add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
642 }
643
644 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
645 {
646         add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
647 }
648
649 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
650 {
651         add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
652 }
653
654 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
655 {
656         add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
657 }
658
659 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
660 {
661         add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
662 }
663
664 bool Server::DelGLine(std::string hostmask)
665 {
666         return del_gline(hostmask.c_str());
667 }
668
669 bool Server::DelQLine(std::string nickname)
670 {
671         return del_qline(nickname.c_str());
672 }
673
674 bool Server::DelZLine(std::string ipaddr)
675 {
676         return del_zline(ipaddr.c_str());
677 }
678
679 bool Server::DelKLine(std::string hostmask)
680 {
681         return del_kline(hostmask.c_str());
682 }
683
684 bool Server::DelELine(std::string hostmask)
685 {
686         return del_eline(hostmask.c_str());
687 }
688
689 long Server::CalcDuration(std::string delta)
690 {
691         return duration(delta.c_str());
692 }
693
694 bool Server::IsValidMask(std::string mask)
695 {
696         const char* dest = mask.c_str();
697         if (strchr(dest,'!')==0)
698                 return false;
699         if (strchr(dest,'@')==0)
700                 return false;
701         for (unsigned int i = 0; i < strlen(dest); i++)
702                 if (dest[i] < 32)
703                         return false;
704         for (unsigned int i = 0; i < strlen(dest); i++)
705                 if (dest[i] > 126)
706                         return false;
707         unsigned int c = 0;
708         for (unsigned int i = 0; i < strlen(dest); i++)
709                 if (dest[i] == '!')
710                         c++;
711         if (c>1)
712                 return false;
713         c = 0;
714         for (unsigned int i = 0; i < strlen(dest); i++)
715                 if (dest[i] == '@')
716                         c++;
717         if (c>1)
718                 return false;
719
720         return true;
721 }
722
723 Module* Server::FindModule(std::string name)
724 {
725         for (int i = 0; i <= MODCOUNT; i++)
726         {
727                 if (Config->module_names[i] == name)
728                 {
729                         return modules[i];
730                 }
731         }
732         return NULL;
733 }
734
735 ConfigReader::ConfigReader()
736 {
737         Config->ClearStack();
738         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
739         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
740         this->readerror = Config->LoadConf(CONFIG_FILE,this->cache,this->errorlog);
741         if (!this->readerror)
742                 this->error = CONF_FILE_NOT_FOUND;
743 }
744
745
746 ConfigReader::~ConfigReader()
747 {
748         if (this->cache)
749                 delete this->cache;
750         if (this->errorlog)
751                 delete this->errorlog;
752 }
753
754
755 ConfigReader::ConfigReader(std::string filename)
756 {
757         Config->ClearStack();
758         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
759         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
760         this->readerror = Config->LoadConf(filename.c_str(),this->cache,this->errorlog);
761         if (!this->readerror)
762                 this->error = CONF_FILE_NOT_FOUND;
763 };
764
765 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
766 {
767         char val[MAXBUF];
768         char t[MAXBUF];
769         char n[MAXBUF];
770         strlcpy(t,tag.c_str(),MAXBUF);
771         strlcpy(n,name.c_str(),MAXBUF);
772         int res = Config->ReadConf(cache,t,n,index,val);
773         if (!res)
774         {
775                 this->error = CONF_VALUE_NOT_FOUND;
776                 return "";
777         }
778         return val;
779 }
780
781 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
782 {
783         char val[MAXBUF];
784         char t[MAXBUF];
785         char n[MAXBUF];
786         strlcpy(t,tag.c_str(),MAXBUF);
787         strlcpy(n,name.c_str(),MAXBUF);
788         int res = Config->ReadConf(cache,t,n,index,val);
789         if (!res)
790         {
791                 this->error = CONF_VALUE_NOT_FOUND;
792                 return false;
793         }
794         std::string s = val;
795         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
796 }
797
798 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
799 {
800         char val[MAXBUF];
801         char t[MAXBUF];
802         char n[MAXBUF];
803         strlcpy(t,tag.c_str(),MAXBUF);
804         strlcpy(n,name.c_str(),MAXBUF);
805         int res = Config->ReadConf(cache,t,n,index,val);
806         if (!res)
807         {
808                 this->error = CONF_VALUE_NOT_FOUND;
809                 return 0;
810         }
811         for (unsigned int i = 0; i < strlen(val); i++)
812         {
813                 if (!isdigit(val[i]))
814                 {
815                         this->error = CONF_NOT_A_NUMBER;
816                         return 0;
817                 }
818         }
819         if ((needs_unsigned) && (atoi(val)<0))
820         {
821                 this->error = CONF_NOT_UNSIGNED;
822                 return 0;
823         }
824         return atoi(val);
825 }
826
827 long ConfigReader::GetError()
828 {
829         long olderr = this->error;
830         this->error = 0;
831         return olderr;
832 }
833
834 void ConfigReader::DumpErrors(bool bail, userrec* user)
835 {
836         if (bail)
837         {
838                 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
839                 exit(0);
840         }
841         else
842         {
843                 char dataline[1024];
844                 if (user)
845                 {
846                         WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
847                         while (!errorlog->eof())
848                         {
849                                 errorlog->getline(dataline,1024);
850                                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
851                         }
852                 }
853                 else
854                 {
855                         WriteOpers("There were errors in the configuration file:",user->nick);
856                         while (!errorlog->eof())
857                         {
858                                 errorlog->getline(dataline,1024);
859                                 WriteOpers(dataline);
860                         }
861                 }
862                 return;
863         }
864 }
865
866
867 int ConfigReader::Enumerate(std::string tag)
868 {
869         return Config->EnumConf(cache,tag.c_str());
870 }
871
872 int ConfigReader::EnumerateValues(std::string tag, int index)
873 {
874         return Config->EnumValues(cache, tag.c_str(), index);
875 }
876
877 bool ConfigReader::Verify()
878 {
879         return this->readerror;
880 }
881
882
883 FileReader::FileReader(std::string filename)
884 {
885         file_cache c;
886         readfile(c,filename.c_str());
887         this->fc = c;
888 }
889
890 FileReader::FileReader()
891 {
892 }
893
894 void FileReader::LoadFile(std::string filename)
895 {
896         file_cache c;
897         readfile(c,filename.c_str());
898         this->fc = c;
899 }
900
901
902 FileReader::~FileReader()
903 {
904 }
905
906 bool FileReader::Exists()
907 {
908         if (fc.size() == 0)
909         {
910                 return(false);
911         }
912         else
913         {
914                 return(true);
915         }
916 }
917
918 std::string FileReader::GetLine(int x)
919 {
920         if ((x<0) || ((unsigned)x>fc.size()))
921                 return "";
922         return fc[x];
923 }
924
925 int FileReader::FileSize()
926 {
927         return fc.size();
928 }
929
930
931 std::vector<Module*> modules(255);
932 std::vector<ircd_module*> factory(255);
933
934 int MODCOUNT  = -1;
935
936