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