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