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