]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Fixed false example info in config.
[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::OnUnloadModule(Module* mod,std::string name) { };
334 void            Module::OnBackgroundTimer(time_t curtime) { };
335 void            Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
336 int             Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user) { return 0; };
337 bool            Module::OnCheckReady(userrec* user) { return true; };
338 void            Module::OnUserRegister(userrec* user) { };
339 int             Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
340 void            Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
341 int             Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
342 int             Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
343 int             Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
344 int             Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
345 int             Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
346 void            Module::OnStats(char symbol) { };
347 int             Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
348 int             Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
349 int             Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
350 int             Module::OnMeshToken(char token,string_list params,serverrec* source,serverrec* reply, std::string tcp_host,std::string ipaddr,int port) { return 0; };
351 void            Module::OnEvent(Event* event) { return; };
352 char*           Module::OnRequest(Request* request) { return NULL; };
353 int             Module::OnOperCompare(std::string password, std::string input) { return 0; };
354 void            Module::OnGlobalOper(userrec* user) { };
355 void            Module::OnGlobalConnect(userrec* user) { };
356 int             Module::OnAddBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
357 int             Module::OnDelBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
358 void            Module::OnRawSocketAccept(int fd, std::string ip, int localport) { };
359 int             Module::OnRawSocketWrite(int fd, char* buffer, int count) { return 0; };
360 void            Module::OnRawSocketClose(int fd) { };
361 int             Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
362
363 // server is a wrapper class that provides methods to all of the C-style
364 // exports in the core
365 //
366
367 Server::Server()
368 {
369 }
370
371 Server::~Server()
372 {
373 }
374
375 void Server::SendOpers(std::string s)
376 {
377         WriteOpers("%s",s.c_str());
378 }
379
380 bool Server::MatchText(std::string sliteral, std::string spattern)
381 {
382         char literal[MAXBUF],pattern[MAXBUF];
383         strlcpy(literal,sliteral.c_str(),MAXBUF);
384         strlcpy(pattern,spattern.c_str(),MAXBUF);
385         return match(literal,pattern);
386 }
387
388 void Server::SendToModeMask(std::string modes, int flags, std::string text)
389 {
390         WriteMode(modes.c_str(),flags,"%s",text.c_str());
391 }
392
393 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
394 {
395         return add_channel(user,cname.c_str(),key.c_str(),false);
396 }
397
398 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
399 {
400         return del_channel(user,cname.c_str(),reason.c_str(),false);
401 }
402
403 chanuserlist Server::GetUsers(chanrec* chan)
404 {
405         chanuserlist userl;
406         userl.clear();
407         std::vector<char*> *list = chan->GetUsers();
408         for (std::vector<char*>::iterator i = list->begin(); i != list->end(); i++)
409         {
410                 char* o = *i;
411                 userl.push_back((userrec*)o);
412         }
413         return userl;
414 }
415 void Server::ChangeUserNick(userrec* user, std::string nickname)
416 {
417         force_nickchange(user,nickname.c_str());
418 }
419
420 void Server::QuitUser(userrec* user, std::string reason)
421 {
422         kill_link(user,reason.c_str());
423 }
424
425 bool Server::IsUlined(std::string server)
426 {
427         return is_uline(server.c_str());
428 }
429
430 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
431 {
432         call_handler(commandname.c_str(),parameters,pcnt,user);
433 }
434
435 void Server::Log(int level, std::string s)
436 {
437         log(level,"%s",s.c_str());
438 }
439
440 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams, char* source)
441 {
442         createcommand(cmd,f,flags,minparams,source);
443 }
444
445 void Server::SendMode(char **parameters, int pcnt, userrec *user)
446 {
447         server_mode(parameters,pcnt,user);
448 }
449
450 void Server::Send(int Socket, std::string s)
451 {
452         Write(Socket,"%s",s.c_str());
453 }
454
455 void Server::SendServ(int Socket, std::string s)
456 {
457         WriteServ(Socket,"%s",s.c_str());
458 }
459
460 void Server::SendFrom(int Socket, userrec* User, std::string s)
461 {
462         WriteFrom(Socket,User,"%s",s.c_str());
463 }
464
465 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
466 {
467         if (!Source)
468         {
469                 // if source is NULL, then the message originates from the local server
470                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
471         }
472         else
473         {
474                 // otherwise it comes from the user specified
475                 WriteTo(Source,Dest,"%s",s.c_str());
476         }
477 }
478
479 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
480 {
481         WriteChannelWithServ((char*)ServName.c_str(), Channel, "%s", text.c_str());
482 }
483
484 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
485 {
486         if (IncludeSender)
487         {
488                 WriteChannel(Channel,User,"%s",s.c_str());
489         }
490         else
491         {
492                 ChanExceptSender(Channel,User,"%s",s.c_str());
493         }
494 }
495
496 bool Server::CommonChannels(userrec* u1, userrec* u2)
497 {
498         return (common_channels(u1,u2) != 0);
499 }
500
501 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
502 {
503         if (IncludeSender)
504         {
505                 WriteCommon(User,"%s",text.c_str());
506         }
507         else
508         {
509                 WriteCommonExcept(User,"%s",text.c_str());
510         }
511 }
512
513 void Server::SendWallops(userrec* User, std::string text)
514 {
515         WriteWallOps(User,false,"%s",text.c_str());
516 }
517
518 void Server::ChangeHost(userrec* user, std::string host)
519 {
520         ChangeDisplayedHost(user,host.c_str());
521 }
522
523 void Server::ChangeGECOS(userrec* user, std::string gecos)
524 {
525         ChangeName(user,gecos.c_str());
526 }
527
528 bool Server::IsNick(std::string nick)
529 {
530         return (isnick(nick.c_str()) != 0);
531 }
532
533 userrec* Server::FindNick(std::string nick)
534 {
535         return Find(nick);
536 }
537
538 userrec* Server::FindDescriptor(int socket)
539 {
540         return (socket < 65536 ? fd_ref_table[socket] : NULL);
541 }
542
543 chanrec* Server::FindChannel(std::string channel)
544 {
545         return FindChan(channel.c_str());
546 }
547
548 std::string Server::ChanMode(userrec* User, chanrec* Chan)
549 {
550         return cmode(User,Chan);
551 }
552
553 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
554 {
555         return has_channel(User,Chan);
556 }
557
558 std::string Server::GetServerName()
559 {
560         return getservername();
561 }
562
563 std::string Server::GetNetworkName()
564 {
565         return getnetworkname();
566 }
567
568 std::string Server::GetServerDescription()
569 {
570         return getserverdesc();
571 }
572
573 Admin Server::GetAdmin()
574 {
575         return Admin(getadminname(),getadminemail(),getadminnick());
576 }
577
578
579
580 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
581 {
582         if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
583         {
584                 if (type == MT_SERVER)
585                 {
586                         log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
587                         return false;
588                 }
589                 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
590                 {
591                         log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
592                         return false;
593                 }
594                 if ((params_when_on>1) || (params_when_off>1))
595                 {
596                         log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
597                         return false;
598                 }
599                 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
600         }
601         else
602         {
603                 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
604         }
605         return false;
606 }
607
608 bool Server::AddExtendedListMode(char modechar)
609 {
610         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
611         if (res)
612                 ModeMakeList(modechar);
613         return res;
614 }
615
616 int Server::CountUsers(chanrec* c)
617 {
618         return usercount(c);
619 }
620
621
622 bool Server::UserToPseudo(userrec* user,std::string message)
623 {
624         unsigned int old_fd = user->fd;
625         user->fd = FD_MAGIC_NUMBER;
626         user->ClearBuffer();
627         Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
628 #ifdef USE_KQUEUE
629         struct kevent ke;
630         EV_SET(&ke, old_fd, EVFILT_READ, EV_DELETE, 0, 0, NULL);
631         int i = kevent(kq, &ke, 1, 0, 0, NULL);
632         if (i == -1)
633         {
634                 log(DEBUG,"kqueue: Failed to remove user from queue!");
635         }
636 #endif
637 #ifdef USE_EPOLL
638         struct epoll_event ev;
639         ev.events = EPOLLIN | EPOLLET;
640         ev.data.fd = old_fd;
641         int i = epoll_ctl(ep, EPOLL_CTL_DEL, old_fd, &ev);
642         if (i < 0)
643         {
644                 log(DEBUG,"epoll: List deletion failure!");
645         }
646 #endif
647
648         shutdown(old_fd,2);
649         close(old_fd);
650         return true;
651 }
652
653 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
654 {
655         zombie->fd = alive->fd;
656         alive->fd = FD_MAGIC_NUMBER;
657         alive->ClearBuffer();
658         Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
659         kill_link(alive,message.c_str());
660         fd_ref_table[zombie->fd] = zombie;
661         for (int i = 0; i != MAXCHANS; i++)
662         {
663                 if (zombie->chans[i].channel != NULL)
664                 {
665                         if (zombie->chans[i].channel->name)
666                         {
667                                 chanrec* Ptr = zombie->chans[i].channel;
668                                 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
669                                 if (Ptr->topicset)
670                                 {
671                                         WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
672                                         WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
673                                 }
674                                 userlist(zombie,Ptr);
675                                 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
676
677                         }
678                 }
679         }
680         return true;
681 }
682
683 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
684 {
685         add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
686 }
687
688 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
689 {
690         add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
691 }
692
693 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
694 {
695         add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
696 }
697
698 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
699 {
700         add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
701 }
702
703 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
704 {
705         add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
706 }
707
708 bool Server::DelGLine(std::string hostmask)
709 {
710         return del_gline(hostmask.c_str());
711 }
712
713 bool Server::DelQLine(std::string nickname)
714 {
715         return del_qline(nickname.c_str());
716 }
717
718 bool Server::DelZLine(std::string ipaddr)
719 {
720         return del_zline(ipaddr.c_str());
721 }
722
723 bool Server::DelKLine(std::string hostmask)
724 {
725         return del_kline(hostmask.c_str());
726 }
727
728 bool Server::DelELine(std::string hostmask)
729 {
730         return del_eline(hostmask.c_str());
731 }
732
733 long Server::CalcDuration(std::string delta)
734 {
735         return duration(delta.c_str());
736 }
737
738 bool Server::IsValidMask(std::string mask)
739 {
740         const char* dest = mask.c_str();
741         if (strchr(dest,'!')==0)
742                 return false;
743         if (strchr(dest,'@')==0)
744                 return false;
745         for (unsigned int i = 0; i < strlen(dest); i++)
746                 if (dest[i] < 32)
747                         return false;
748         for (unsigned int i = 0; i < strlen(dest); i++)
749                 if (dest[i] > 126)
750                         return false;
751         unsigned int c = 0;
752         for (unsigned int i = 0; i < strlen(dest); i++)
753                 if (dest[i] == '!')
754                         c++;
755         if (c>1)
756                 return false;
757         c = 0;
758         for (unsigned int i = 0; i < strlen(dest); i++)
759                 if (dest[i] == '@')
760                         c++;
761         if (c>1)
762                 return false;
763
764         return true;
765 }
766
767 void Server::MeshSendAll(std::string text)
768 {
769         NetSendToAll((char*)text.c_str());
770 }
771
772 void Server::MeshSendCommon(userrec* user, std::string text)
773 {
774         if (user)
775                 NetSendToCommon(user,(char*)text.c_str());
776 }
777
778 void Server::MeshSendAllAlive(std::string text)
779 {
780         NetSendToAllAlive((char*)text.c_str());
781 }
782
783 void Server::MeshSendUnicast(std::string destination, std::string text)
784 {
785         NetSendToOne((char*)destination.c_str(),(char*)text.c_str());
786 }
787
788 void Server::MeshSendAllExcept(std::string target, std::string text)
789 {
790         NetSendToAllExcept(target.c_str(),(char*)text.c_str());
791 }
792
793 bool Server::MeshCheckChan(chanrec *c,std::string servername)
794 {
795         if (c)
796         {
797                 return ChanAnyOnThisServer(c,(char*)servername.c_str());
798         }
799         else return false;
800 }
801
802 bool Server::MeshCheckCommon(userrec* u,std::string servername)
803 {
804         if (u)
805         {
806                 return CommonOnThisServer(u,(char*)servername.c_str());
807         }
808         else return false;
809 }
810
811 Module* Server::FindModule(std::string name)
812 {
813         for (int i = 0; i <= MODCOUNT; i++)
814         {
815                 if (module_names[i] == name)
816                 {
817                         return modules[i];
818                 }
819         }
820         return NULL;
821 }
822
823 ConfigReader::ConfigReader()
824 {
825         include_stack.clear();
826         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
827         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
828         this->readerror = LoadConf(CONFIG_FILE,this->cache,this->errorlog);
829         if (!this->readerror)
830                 this->error = CONF_FILE_NOT_FOUND;
831 }
832
833
834 ConfigReader::~ConfigReader()
835 {
836         if (this->cache)
837                 delete this->cache;
838         if (this->errorlog)
839                 delete this->errorlog;
840 }
841
842
843 ConfigReader::ConfigReader(std::string filename)
844 {
845         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
846         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
847         this->readerror = LoadConf(filename.c_str(),this->cache,this->errorlog);
848         if (!this->readerror)
849                 this->error = CONF_FILE_NOT_FOUND;
850 };
851
852 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
853 {
854         char val[MAXBUF];
855         char t[MAXBUF];
856         char n[MAXBUF];
857         strlcpy(t,tag.c_str(),MAXBUF);
858         strlcpy(n,name.c_str(),MAXBUF);
859         int res = ReadConf(cache,t,n,index,val);
860         if (!res)
861         {
862                 this->error = CONF_VALUE_NOT_FOUND;
863                 return "";
864         }
865         return val;
866 }
867
868 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
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 false;
880         }
881         std::string s = val;
882         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
883 }
884
885 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
886 {
887         char val[MAXBUF];
888         char t[MAXBUF];
889         char n[MAXBUF];
890         strlcpy(t,tag.c_str(),MAXBUF);
891         strlcpy(n,name.c_str(),MAXBUF);
892         int res = ReadConf(cache,t,n,index,val);
893         if (!res)
894         {
895                 this->error = CONF_VALUE_NOT_FOUND;
896                 return 0;
897         }
898         for (unsigned int i = 0; i < strlen(val); i++)
899         {
900                 if (!isdigit(val[i]))
901                 {
902                         this->error = CONF_NOT_A_NUMBER;
903                         return 0;
904                 }
905         }
906         if ((needs_unsigned) && (atoi(val)<0))
907         {
908                 this->error = CONF_NOT_UNSIGNED;
909                 return 0;
910         }
911         return atoi(val);
912 }
913
914 long ConfigReader::GetError()
915 {
916         long olderr = this->error;
917         this->error = 0;
918         return olderr;
919 }
920
921 void ConfigReader::DumpErrors(bool bail, userrec* user)
922 {
923         if (bail)
924         {
925                 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
926                 exit(0);
927         }
928         else
929         {
930                 char dataline[1024];
931                 if (user)
932                 {
933                         WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
934                         while (!errorlog->eof())
935                         {
936                                 errorlog->getline(dataline,1024);
937                                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
938                         }
939                 }
940                 else
941                 {
942                         WriteOpers("There were errors in the configuration file:",user->nick);
943                         while (!errorlog->eof())
944                         {
945                                 errorlog->getline(dataline,1024);
946                                 WriteOpers(dataline);
947                         }
948                 }
949                 return;
950         }
951 }
952
953
954 int ConfigReader::Enumerate(std::string tag)
955 {
956         return EnumConf(cache,tag.c_str());
957 }
958
959 int ConfigReader::EnumerateValues(std::string tag, int index)
960 {
961         return EnumValues(cache, tag.c_str(), index);
962 }
963
964 bool ConfigReader::Verify()
965 {
966         return this->readerror;
967 }
968
969
970 FileReader::FileReader(std::string filename)
971 {
972         file_cache c;
973         readfile(c,filename.c_str());
974         this->fc = c;
975 }
976
977 FileReader::FileReader()
978 {
979 }
980
981 void FileReader::LoadFile(std::string filename)
982 {
983         file_cache c;
984         readfile(c,filename.c_str());
985         this->fc = c;
986 }
987
988
989 FileReader::~FileReader()
990 {
991 }
992
993 bool FileReader::Exists()
994 {
995         if (fc.size() == 0)
996         {
997                 return(false);
998         }
999         else
1000         {
1001                 return(true);
1002         }
1003 }
1004
1005 std::string FileReader::GetLine(int x)
1006 {
1007         if ((x<0) || ((unsigned)x>fc.size()))
1008                 return "";
1009         return fc[x];
1010 }
1011
1012 int FileReader::FileSize()
1013 {
1014         return fc.size();
1015 }
1016
1017
1018 std::vector<Module*> modules(255);
1019 std::vector<ircd_module*> factory(255);
1020
1021 int MODCOUNT  = -1;
1022
1023