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