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