]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Adding hook type checking to event calls to speed them up
[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(I_OnEvent,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 void            Module::Implements(bool &Implements[255]) { for (int j = 0; j < 255; j++) Implements[j] = false; };
300
301 /* server is a wrapper class that provides methods to all of the C-style
302  * exports in the core
303  */
304
305 Server::Server()
306 {
307 }
308
309 Server::~Server()
310 {
311 }
312
313 void Server::AddSocket(InspSocket* sock)
314 {
315         module_sockets.push_back(sock);
316 }
317
318 void Server::RehashServer()
319 {
320         WriteOpers("*** Rehashing config file");
321         Config->Read(false,NULL);
322 }
323
324 ServerConfig* Server::GetConfig()
325 {
326         return Config;
327 }
328
329 std::string Server::GetVersion()
330 {
331         return ServerInstance->GetVersionString();
332 }
333
334 void Server::DelSocket(InspSocket* sock)
335 {
336         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
337         {
338                 if (*a == sock)
339                 {
340                         module_sockets.erase(a);
341                         return;
342                 }
343         }
344 }
345
346 void Server::SendOpers(std::string s)
347 {
348         WriteOpers("%s",s.c_str());
349 }
350
351 bool Server::MatchText(std::string sliteral, std::string spattern)
352 {
353         char literal[MAXBUF],pattern[MAXBUF];
354         strlcpy(literal,sliteral.c_str(),MAXBUF);
355         strlcpy(pattern,spattern.c_str(),MAXBUF);
356         return match(literal,pattern);
357 }
358
359 void Server::SendToModeMask(std::string modes, int flags, std::string text)
360 {
361         WriteMode(modes.c_str(),flags,"%s",text.c_str());
362 }
363
364 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
365 {
366         return add_channel(user,cname.c_str(),key.c_str(),false);
367 }
368
369 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
370 {
371         return del_channel(user,cname.c_str(),reason.c_str(),false);
372 }
373
374 chanuserlist Server::GetUsers(chanrec* chan)
375 {
376         chanuserlist userl;
377         userl.clear();
378         std::vector<char*> *list = chan->GetUsers();
379         for (std::vector<char*>::iterator i = list->begin(); i != list->end(); i++)
380         {
381                 char* o = *i;
382                 userl.push_back((userrec*)o);
383         }
384         return userl;
385 }
386 void Server::ChangeUserNick(userrec* user, std::string nickname)
387 {
388         force_nickchange(user,nickname.c_str());
389 }
390
391 void Server::QuitUser(userrec* user, std::string reason)
392 {
393         kill_link(user,reason.c_str());
394 }
395
396 bool Server::IsUlined(std::string server)
397 {
398         return is_uline(server.c_str());
399 }
400
401 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
402 {
403         ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
404 }
405
406 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
407 {
408         return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
409 }
410
411 void Server::Log(int level, std::string s)
412 {
413         log(level,"%s",s.c_str());
414 }
415
416 void Server::AddCommand(command_t *f)
417 {
418         ServerInstance->Parser->CreateCommand(f);
419 }
420
421 void Server::SendMode(char **parameters, int pcnt, userrec *user)
422 {
423         ServerInstance->ModeGrok->ServerMode(parameters,pcnt,user);
424 }
425
426 void Server::Send(int Socket, std::string s)
427 {
428         Write(Socket,"%s",s.c_str());
429 }
430
431 void Server::SendServ(int Socket, std::string s)
432 {
433         WriteServ(Socket,"%s",s.c_str());
434 }
435
436 void Server::SendFrom(int Socket, userrec* User, std::string s)
437 {
438         WriteFrom(Socket,User,"%s",s.c_str());
439 }
440
441 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
442 {
443         if (!Source)
444         {
445                 // if source is NULL, then the message originates from the local server
446                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
447         }
448         else
449         {
450                 // otherwise it comes from the user specified
451                 WriteTo(Source,Dest,"%s",s.c_str());
452         }
453 }
454
455 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
456 {
457         WriteChannelWithServ((char*)ServName.c_str(), Channel, "%s", text.c_str());
458 }
459
460 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
461 {
462         if (IncludeSender)
463         {
464                 WriteChannel(Channel,User,"%s",s.c_str());
465         }
466         else
467         {
468                 ChanExceptSender(Channel,User,"%s",s.c_str());
469         }
470 }
471
472 bool Server::CommonChannels(userrec* u1, userrec* u2)
473 {
474         return (common_channels(u1,u2) != 0);
475 }
476
477 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
478 {
479         if (IncludeSender)
480         {
481                 WriteCommon(User,"%s",text.c_str());
482         }
483         else
484         {
485                 WriteCommonExcept(User,"%s",text.c_str());
486         }
487 }
488
489 void Server::SendWallops(userrec* User, std::string text)
490 {
491         WriteWallOps(User,false,"%s",text.c_str());
492 }
493
494 void Server::ChangeHost(userrec* user, std::string host)
495 {
496         ChangeDisplayedHost(user,host.c_str());
497 }
498
499 void Server::ChangeGECOS(userrec* user, std::string gecos)
500 {
501         ChangeName(user,gecos.c_str());
502 }
503
504 bool Server::IsNick(std::string nick)
505 {
506         return (isnick(nick.c_str()) != 0);
507 }
508
509 userrec* Server::FindNick(std::string nick)
510 {
511         return Find(nick);
512 }
513
514 userrec* Server::FindDescriptor(int socket)
515 {
516         return (socket < 65536 ? fd_ref_table[socket] : NULL);
517 }
518
519 chanrec* Server::FindChannel(std::string channel)
520 {
521         return FindChan(channel.c_str());
522 }
523
524 std::string Server::ChanMode(userrec* User, chanrec* Chan)
525 {
526         return cmode(User,Chan);
527 }
528
529 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
530 {
531         return has_channel(User,Chan);
532 }
533
534 std::string Server::GetServerName()
535 {
536         return Config->ServerName;
537 }
538
539 std::string Server::GetNetworkName()
540 {
541         return Config->Network;
542 }
543
544 std::string Server::GetServerDescription()
545 {
546         return Config->ServerDesc;
547 }
548
549 Admin Server::GetAdmin()
550 {
551         return Admin(Config->AdminName,Config->AdminEmail,Config->AdminNick);
552 }
553
554
555
556 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
557 {
558         if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
559         {
560                 if (type == MT_SERVER)
561                 {
562                         log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
563                         return false;
564                 }
565                 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
566                 {
567                         log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
568                         return false;
569                 }
570                 if ((params_when_on>1) || (params_when_off>1))
571                 {
572                         log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
573                         return false;
574                 }
575                 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
576         }
577         else
578         {
579                 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
580         }
581         return false;
582 }
583
584 bool Server::AddExtendedListMode(char modechar)
585 {
586         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
587         if (res)
588                 ModeMakeList(modechar);
589         return res;
590 }
591
592 int Server::CountUsers(chanrec* c)
593 {
594         return usercount(c);
595 }
596
597
598 bool Server::UserToPseudo(userrec* user,std::string message)
599 {
600         unsigned int old_fd = user->fd;
601         user->fd = FD_MAGIC_NUMBER;
602         user->ClearBuffer();
603         Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
604         ServerInstance->SE->DelFd(old_fd);
605         shutdown(old_fd,2);
606         close(old_fd);
607         return true;
608 }
609
610 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
611 {
612         zombie->fd = alive->fd;
613         alive->fd = FD_MAGIC_NUMBER;
614         alive->ClearBuffer();
615         Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
616         kill_link(alive,message.c_str());
617         fd_ref_table[zombie->fd] = zombie;
618         for (unsigned int i = 0; i < zombie->chans.size(); i++)
619         {
620                 if (zombie->chans[i].channel != NULL)
621                 {
622                         if (zombie->chans[i].channel->name)
623                         {
624                                 chanrec* Ptr = zombie->chans[i].channel;
625                                 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
626                                 if (Ptr->topicset)
627                                 {
628                                         WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
629                                         WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
630                                 }
631                                 userlist(zombie,Ptr);
632                                 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
633
634                         }
635                 }
636         }
637         return true;
638 }
639
640 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
641 {
642         add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
643 }
644
645 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
646 {
647         add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
648 }
649
650 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
651 {
652         add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
653 }
654
655 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
656 {
657         add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
658 }
659
660 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
661 {
662         add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
663 }
664
665 bool Server::DelGLine(std::string hostmask)
666 {
667         return del_gline(hostmask.c_str());
668 }
669
670 bool Server::DelQLine(std::string nickname)
671 {
672         return del_qline(nickname.c_str());
673 }
674
675 bool Server::DelZLine(std::string ipaddr)
676 {
677         return del_zline(ipaddr.c_str());
678 }
679
680 bool Server::DelKLine(std::string hostmask)
681 {
682         return del_kline(hostmask.c_str());
683 }
684
685 bool Server::DelELine(std::string hostmask)
686 {
687         return del_eline(hostmask.c_str());
688 }
689
690 long Server::CalcDuration(std::string delta)
691 {
692         return duration(delta.c_str());
693 }
694
695 bool Server::IsValidMask(std::string mask)
696 {
697         const char* dest = mask.c_str();
698         if (strchr(dest,'!')==0)
699                 return false;
700         if (strchr(dest,'@')==0)
701                 return false;
702         for (unsigned int i = 0; i < strlen(dest); i++)
703                 if (dest[i] < 32)
704                         return false;
705         for (unsigned int i = 0; i < strlen(dest); i++)
706                 if (dest[i] > 126)
707                         return false;
708         unsigned int c = 0;
709         for (unsigned int i = 0; i < strlen(dest); i++)
710                 if (dest[i] == '!')
711                         c++;
712         if (c>1)
713                 return false;
714         c = 0;
715         for (unsigned int i = 0; i < strlen(dest); i++)
716                 if (dest[i] == '@')
717                         c++;
718         if (c>1)
719                 return false;
720
721         return true;
722 }
723
724 Module* Server::FindModule(std::string name)
725 {
726         for (int i = 0; i <= MODCOUNT; i++)
727         {
728                 if (Config->module_names[i] == name)
729                 {
730                         return modules[i];
731                 }
732         }
733         return NULL;
734 }
735
736 ConfigReader::ConfigReader()
737 {
738         Config->ClearStack();
739         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
740         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
741         this->readerror = Config->LoadConf(CONFIG_FILE,this->cache,this->errorlog);
742         if (!this->readerror)
743                 this->error = CONF_FILE_NOT_FOUND;
744 }
745
746
747 ConfigReader::~ConfigReader()
748 {
749         if (this->cache)
750                 delete this->cache;
751         if (this->errorlog)
752                 delete this->errorlog;
753 }
754
755
756 ConfigReader::ConfigReader(std::string filename)
757 {
758         Config->ClearStack();
759         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
760         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
761         this->readerror = Config->LoadConf(filename.c_str(),this->cache,this->errorlog);
762         if (!this->readerror)
763                 this->error = CONF_FILE_NOT_FOUND;
764 };
765
766 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
767 {
768         char val[MAXBUF];
769         char t[MAXBUF];
770         char n[MAXBUF];
771         strlcpy(t,tag.c_str(),MAXBUF);
772         strlcpy(n,name.c_str(),MAXBUF);
773         int res = Config->ReadConf(cache,t,n,index,val);
774         if (!res)
775         {
776                 this->error = CONF_VALUE_NOT_FOUND;
777                 return "";
778         }
779         return val;
780 }
781
782 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
783 {
784         char val[MAXBUF];
785         char t[MAXBUF];
786         char n[MAXBUF];
787         strlcpy(t,tag.c_str(),MAXBUF);
788         strlcpy(n,name.c_str(),MAXBUF);
789         int res = Config->ReadConf(cache,t,n,index,val);
790         if (!res)
791         {
792                 this->error = CONF_VALUE_NOT_FOUND;
793                 return false;
794         }
795         std::string s = val;
796         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
797 }
798
799 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
800 {
801         char val[MAXBUF];
802         char t[MAXBUF];
803         char n[MAXBUF];
804         strlcpy(t,tag.c_str(),MAXBUF);
805         strlcpy(n,name.c_str(),MAXBUF);
806         int res = Config->ReadConf(cache,t,n,index,val);
807         if (!res)
808         {
809                 this->error = CONF_VALUE_NOT_FOUND;
810                 return 0;
811         }
812         for (unsigned int i = 0; i < strlen(val); i++)
813         {
814                 if (!isdigit(val[i]))
815                 {
816                         this->error = CONF_NOT_A_NUMBER;
817                         return 0;
818                 }
819         }
820         if ((needs_unsigned) && (atoi(val)<0))
821         {
822                 this->error = CONF_NOT_UNSIGNED;
823                 return 0;
824         }
825         return atoi(val);
826 }
827
828 long ConfigReader::GetError()
829 {
830         long olderr = this->error;
831         this->error = 0;
832         return olderr;
833 }
834
835 void ConfigReader::DumpErrors(bool bail, userrec* user)
836 {
837         if (bail)
838         {
839                 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
840                 exit(0);
841         }
842         else
843         {
844                 char dataline[1024];
845                 if (user)
846                 {
847                         WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
848                         while (!errorlog->eof())
849                         {
850                                 errorlog->getline(dataline,1024);
851                                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
852                         }
853                 }
854                 else
855                 {
856                         WriteOpers("There were errors in the configuration file:",user->nick);
857                         while (!errorlog->eof())
858                         {
859                                 errorlog->getline(dataline,1024);
860                                 WriteOpers(dataline);
861                         }
862                 }
863                 return;
864         }
865 }
866
867
868 int ConfigReader::Enumerate(std::string tag)
869 {
870         return Config->EnumConf(cache,tag.c_str());
871 }
872
873 int ConfigReader::EnumerateValues(std::string tag, int index)
874 {
875         return Config->EnumValues(cache, tag.c_str(), index);
876 }
877
878 bool ConfigReader::Verify()
879 {
880         return this->readerror;
881 }
882
883
884 FileReader::FileReader(std::string filename)
885 {
886         file_cache c;
887         readfile(c,filename.c_str());
888         this->fc = c;
889 }
890
891 FileReader::FileReader()
892 {
893 }
894
895 void FileReader::LoadFile(std::string filename)
896 {
897         file_cache c;
898         readfile(c,filename.c_str());
899         this->fc = c;
900 }
901
902
903 FileReader::~FileReader()
904 {
905 }
906
907 bool FileReader::Exists()
908 {
909         if (fc.size() == 0)
910         {
911                 return(false);
912         }
913         else
914         {
915                 return(true);
916         }
917 }
918
919 std::string FileReader::GetLine(int x)
920 {
921         if ((x<0) || ((unsigned)x>fc.size()))
922                 return "";
923         return fc[x];
924 }
925
926 int FileReader::FileSize()
927 {
928         return fc.size();
929 }
930
931
932 std::vector<Module*> modules(255);
933 std::vector<ircd_module*> factory(255);
934
935 int MODCOUNT  = -1;
936
937