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