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