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