]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
736042c33923d40fc617d78e52236c8c439d7c55
[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<std::string> include_stack;
59 extern std::vector<InspSocket*> module_sockets;
60
61 extern time_t TIME;
62 extern int WHOWAS_STALE;
63 extern int WHOWAS_MAX;
64 extern time_t startup_time;
65 extern std::vector<std::string> module_names;
66 extern int boundPortCount;
67 extern int portCount;
68 extern int ports[MAXSOCKS];
69
70 class Server;
71
72 extern std::stringstream config_f;
73 extern userrec* fd_ref_table[65536];
74
75 extern user_hash clientlist;
76 extern chan_hash chanlist;
77 extern whowas_hash whowas;
78 extern command_table cmdlist;
79 extern file_cache MOTD;
80 extern file_cache RULES;
81 extern address_cache IP;                                     
82 ExtModeList EMode;
83
84 // returns true if an extended mode character is in use
85 bool ModeDefined(char modechar, int type)
86 {
87         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
88         {
89                 if ((i->modechar == modechar) && (i->type == type))
90                 {
91                         return true;
92                 }
93         }
94         return false;
95 }
96
97 bool ModeIsListMode(char modechar, int type)
98 {
99         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
100         {
101                 if ((i->modechar == modechar) && (i->type == type) && (i->list == true))
102                 {
103                         return true;
104                 }
105         }
106         return false;
107 }
108
109 bool ModeDefinedOper(char modechar, int type)
110 {
111         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
112         {
113                 if ((i->modechar == modechar) && (i->type == type) && (i->needsoper == true))
114                 {
115                         return true;
116                 }
117         }
118         return false;
119 }
120
121 // returns number of parameters for a custom mode when it is switched on
122 int ModeDefinedOn(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_on;
129                 }
130         }
131         return 0;
132 }
133
134 // returns number of parameters for a custom mode when it is switched on
135 int ModeDefinedOff(char modechar, int type)
136 {
137         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
138         {
139                 if ((i->modechar == modechar) && (i->type == type))
140                 {
141                         return i->params_when_off;
142                 }
143         }
144         return 0;
145 }
146
147 // returns true if an extended mode character is in use
148 bool DoAddExtendedMode(char modechar, int type, bool requires_oper, int params_on, int params_off)
149 {
150         if (ModeDefined(modechar,type)) {
151                 return false;
152         }
153         EMode.push_back(ExtMode(modechar,type,requires_oper,params_on,params_off));
154         return true;
155 }
156
157 // turns a mode into a listmode
158 void ModeMakeList(char modechar)
159 {
160         for (ExtModeListIter i = EMode.begin(); i < EMode.end(); i++)
161         {
162                 if ((i->modechar == modechar) && (i->type == MT_CHANNEL))
163                 {
164                         i->list = true;
165                         return;
166                 }
167         }
168         return;
169 }
170
171 // version is a simple class for holding a modules version number
172
173 Version::Version(int major, int minor, int revision, int build, int flags) : Major(major), Minor(minor), Revision(revision), Build(build), Flags(flags) { };
174
175 // admin is a simple class for holding a server's administrative info
176
177 Admin::Admin(std::string name, std::string email, std::string nick) : Name(name), Email(email), Nick(nick) { };
178
179 Request::Request(char* anydata, Module* src, Module* dst) : data(anydata), source(src), dest(dst) { };
180
181 char* Request::GetData()
182 {
183         return this->data;
184 }
185
186 Module* Request::GetSource()
187 {
188         return this->source;
189 }
190
191 Module* Request::GetDest()
192 {
193         return this->dest;
194 }
195
196 char* Request::Send()
197 {
198         if (this->dest)
199         {
200                 return dest->OnRequest(this);
201         }
202         else
203         {
204                 return NULL;
205         }
206 }
207
208 Event::Event(char* anydata, Module* src, std::string eventid) : data(anydata), source(src), id(eventid) { };
209
210 char* Event::GetData()
211 {
212         return this->data;
213 }
214
215 Module* Event::GetSource()
216 {
217         return this->source;
218 }
219
220 char* Event::Send()
221 {
222         FOREACH_MOD OnEvent(this);
223         return NULL;
224 }
225
226 std::string Event::GetEventID()
227 {
228         return this->id;
229 }
230
231
232 // These declarations define the behavours of the base class Module (which does nothing at all)
233
234                 Module::Module(Server* Me) { }
235                 Module::~Module() { }
236 void            Module::OnUserConnect(userrec* user) { }
237 void            Module::OnUserQuit(userrec* user, std::string message) { }
238 void            Module::OnUserDisconnect(userrec* user) { }
239 void            Module::OnUserJoin(userrec* user, chanrec* channel) { }
240 void            Module::OnUserPart(userrec* user, chanrec* channel) { }
241 void            Module::OnRehash(std::string parameter) { }
242 void            Module::OnServerRaw(std::string &raw, bool inbound, userrec* user) { }
243 int             Module::OnUserPreJoin(userrec* user, chanrec* chan, const char* cname) { return 0; }
244 int             Module::OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params) { return false; }
245 void            Module::OnMode(userrec* user, void* dest, int target_type, std::string text) { };
246 Version         Module::GetVersion() { return Version(1,0,0,0,VF_VENDOR); }
247 void            Module::OnOper(userrec* user, std::string opertype) { };
248 void            Module::OnInfo(userrec* user) { };
249 void            Module::OnWhois(userrec* source, userrec* dest) { };
250 int             Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
251 int             Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
252 int             Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text) { return 0; };
253 int             Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
254 void            Module::OnUserPostNick(userrec* user, std::string oldnick) { };
255 int             Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
256 void            Module::On005Numeric(std::string &output) { };
257 int             Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
258 void            Module::OnLoadModule(Module* mod,std::string name) { };
259 void            Module::OnUnloadModule(Module* mod,std::string name) { };
260 void            Module::OnBackgroundTimer(time_t curtime) { };
261 void            Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
262 int             Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user) { return 0; };
263 bool            Module::OnCheckReady(userrec* user) { return true; };
264 void            Module::OnUserRegister(userrec* user) { };
265 int             Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
266 void            Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
267 int             Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
268 int             Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
269 int             Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
270 int             Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
271 int             Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
272 void            Module::OnStats(char symbol) { };
273 int             Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
274 int             Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
275 int             Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
276 void            Module::OnEvent(Event* event) { return; };
277 char*           Module::OnRequest(Request* request) { return NULL; };
278 int             Module::OnOperCompare(std::string password, std::string input) { return 0; };
279 void            Module::OnGlobalOper(userrec* user) { };
280 void            Module::OnGlobalConnect(userrec* user) { };
281 int             Module::OnAddBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
282 int             Module::OnDelBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
283 void            Module::OnRawSocketAccept(int fd, std::string ip, int localport) { };
284 int             Module::OnRawSocketWrite(int fd, char* buffer, int count) { return 0; };
285 void            Module::OnRawSocketClose(int fd) { };
286 int             Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
287 void            Module::OnUserMessage(userrec* user, void* dest, int target_type, std::string text) { };
288 void            Module::OnUserNotice(userrec* user, void* dest, int target_type, std::string text) { };
289 void            Module::OnRemoteKill(userrec* source, userrec* dest, std::string reason) { };
290 void            Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
291 void            Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { };
292 void            Module::OnGetServerDescription(std::string servername,std::string &description) { };
293 void            Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
294 void            Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
295 void            Module::ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline) { };
296 void            Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, std::string extname) { };
297 void            Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, std::string extname) { };
298 void            Module::OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata) { };
299 void            Module::ProtoSendMetaData(void* opaque, int target_type, void* target, std::string extname, std::string extdata) { };
300 void            Module::OnWallops(userrec* user, std::string text) { };
301 void            Module::OnChangeHost(userrec* user, std::string newhost) { };
302 void            Module::OnChangeName(userrec* user, std::string gecos) { };
303 void            Module::OnAddGLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
304 void            Module::OnAddZLine(long duration, userrec* source, std::string reason, std::string ipmask) { };
305 void            Module::OnAddKLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
306 void            Module::OnAddQLine(long duration, userrec* source, std::string reason, std::string nickmask) { };
307 void            Module::OnAddELine(long duration, userrec* source, std::string reason, std::string hostmask) { };
308 void            Module::OnDelGLine(userrec* source, std::string hostmask) { };
309 void            Module::OnDelZLine(userrec* source, std::string ipmask) { };
310 void            Module::OnDelKLine(userrec* source, std::string hostmask) { };
311 void            Module::OnDelQLine(userrec* source, std::string nickmask) { };
312 void            Module::OnDelELine(userrec* source, std::string hostmask) { };
313 void            Module::OnCleanup(int target_type, void* item) { };
314
315 /* server is a wrapper class that provides methods to all of the C-style
316  * exports in the core
317  */
318
319 Server::Server()
320 {
321 }
322
323 Server::~Server()
324 {
325 }
326
327 void Server::AddSocket(InspSocket* sock)
328 {
329         module_sockets.push_back(sock);
330 }
331
332 void Server::RehashServer()
333 {
334         WriteOpers("*** Rehashing config file");
335         ReadConfig(false,NULL);
336 }
337
338 void Server::DelSocket(InspSocket* sock)
339 {
340         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
341         {
342                 if (*a == sock)
343                 {
344                         module_sockets.erase(a);
345                         return;
346                 }
347         }
348 }
349
350 void Server::SendOpers(std::string s)
351 {
352         WriteOpers("%s",s.c_str());
353 }
354
355 bool Server::MatchText(std::string sliteral, std::string spattern)
356 {
357         char literal[MAXBUF],pattern[MAXBUF];
358         strlcpy(literal,sliteral.c_str(),MAXBUF);
359         strlcpy(pattern,spattern.c_str(),MAXBUF);
360         return match(literal,pattern);
361 }
362
363 void Server::SendToModeMask(std::string modes, int flags, std::string text)
364 {
365         WriteMode(modes.c_str(),flags,"%s",text.c_str());
366 }
367
368 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
369 {
370         return add_channel(user,cname.c_str(),key.c_str(),false);
371 }
372
373 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
374 {
375         return del_channel(user,cname.c_str(),reason.c_str(),false);
376 }
377
378 chanuserlist Server::GetUsers(chanrec* chan)
379 {
380         chanuserlist userl;
381         userl.clear();
382         std::vector<char*> *list = chan->GetUsers();
383         for (std::vector<char*>::iterator i = list->begin(); i != list->end(); i++)
384         {
385                 char* o = *i;
386                 userl.push_back((userrec*)o);
387         }
388         return userl;
389 }
390 void Server::ChangeUserNick(userrec* user, std::string nickname)
391 {
392         force_nickchange(user,nickname.c_str());
393 }
394
395 void Server::QuitUser(userrec* user, std::string reason)
396 {
397         kill_link(user,reason.c_str());
398 }
399
400 bool Server::IsUlined(std::string server)
401 {
402         return is_uline(server.c_str());
403 }
404
405 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
406 {
407         call_handler(commandname.c_str(),parameters,pcnt,user);
408 }
409
410 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
411 {
412         return is_valid_cmd(commandname.c_str(), pcnt, user);
413 }
414
415 void Server::Log(int level, std::string s)
416 {
417         log(level,"%s",s.c_str());
418 }
419
420 void Server::AddCommand(char* cmd, handlerfunc f, char flags, int minparams, char* source)
421 {
422         createcommand(cmd,f,flags,minparams,source);
423 }
424
425 void Server::SendMode(char **parameters, int pcnt, userrec *user)
426 {
427         server_mode(parameters,pcnt,user);
428 }
429
430 void Server::Send(int Socket, std::string s)
431 {
432         Write(Socket,"%s",s.c_str());
433 }
434
435 void Server::SendServ(int Socket, std::string s)
436 {
437         WriteServ(Socket,"%s",s.c_str());
438 }
439
440 void Server::SendFrom(int Socket, userrec* User, std::string s)
441 {
442         WriteFrom(Socket,User,"%s",s.c_str());
443 }
444
445 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
446 {
447         if (!Source)
448         {
449                 // if source is NULL, then the message originates from the local server
450                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
451         }
452         else
453         {
454                 // otherwise it comes from the user specified
455                 WriteTo(Source,Dest,"%s",s.c_str());
456         }
457 }
458
459 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
460 {
461         WriteChannelWithServ((char*)ServName.c_str(), Channel, "%s", text.c_str());
462 }
463
464 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
465 {
466         if (IncludeSender)
467         {
468                 WriteChannel(Channel,User,"%s",s.c_str());
469         }
470         else
471         {
472                 ChanExceptSender(Channel,User,"%s",s.c_str());
473         }
474 }
475
476 bool Server::CommonChannels(userrec* u1, userrec* u2)
477 {
478         return (common_channels(u1,u2) != 0);
479 }
480
481 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
482 {
483         if (IncludeSender)
484         {
485                 WriteCommon(User,"%s",text.c_str());
486         }
487         else
488         {
489                 WriteCommonExcept(User,"%s",text.c_str());
490         }
491 }
492
493 void Server::SendWallops(userrec* User, std::string text)
494 {
495         WriteWallOps(User,false,"%s",text.c_str());
496 }
497
498 void Server::ChangeHost(userrec* user, std::string host)
499 {
500         ChangeDisplayedHost(user,host.c_str());
501 }
502
503 void Server::ChangeGECOS(userrec* user, std::string gecos)
504 {
505         ChangeName(user,gecos.c_str());
506 }
507
508 bool Server::IsNick(std::string nick)
509 {
510         return (isnick(nick.c_str()) != 0);
511 }
512
513 userrec* Server::FindNick(std::string nick)
514 {
515         return Find(nick);
516 }
517
518 userrec* Server::FindDescriptor(int socket)
519 {
520         return (socket < 65536 ? fd_ref_table[socket] : NULL);
521 }
522
523 chanrec* Server::FindChannel(std::string channel)
524 {
525         return FindChan(channel.c_str());
526 }
527
528 std::string Server::ChanMode(userrec* User, chanrec* Chan)
529 {
530         return cmode(User,Chan);
531 }
532
533 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
534 {
535         return has_channel(User,Chan);
536 }
537
538 std::string Server::GetServerName()
539 {
540         return getservername();
541 }
542
543 std::string Server::GetNetworkName()
544 {
545         return getnetworkname();
546 }
547
548 std::string Server::GetServerDescription()
549 {
550         return getserverdesc();
551 }
552
553 Admin Server::GetAdmin()
554 {
555         return Admin(getadminname(),getadminemail(),getadminnick());
556 }
557
558
559
560 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
561 {
562         if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
563         {
564                 if (type == MT_SERVER)
565                 {
566                         log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
567                         return false;
568                 }
569                 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
570                 {
571                         log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
572                         return false;
573                 }
574                 if ((params_when_on>1) || (params_when_off>1))
575                 {
576                         log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
577                         return false;
578                 }
579                 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
580         }
581         else
582         {
583                 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
584         }
585         return false;
586 }
587
588 bool Server::AddExtendedListMode(char modechar)
589 {
590         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
591         if (res)
592                 ModeMakeList(modechar);
593         return res;
594 }
595
596 int Server::CountUsers(chanrec* c)
597 {
598         return usercount(c);
599 }
600
601
602 bool Server::UserToPseudo(userrec* user,std::string message)
603 {
604         unsigned int old_fd = user->fd;
605         user->fd = FD_MAGIC_NUMBER;
606         user->ClearBuffer();
607         Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
608         SE->DelFd(old_fd);
609         shutdown(old_fd,2);
610         close(old_fd);
611         return true;
612 }
613
614 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
615 {
616         zombie->fd = alive->fd;
617         alive->fd = FD_MAGIC_NUMBER;
618         alive->ClearBuffer();
619         Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
620         kill_link(alive,message.c_str());
621         fd_ref_table[zombie->fd] = zombie;
622         for (unsigned int i = 0; i < zombie->chans.size(); i++)
623         {
624                 if (zombie->chans[i].channel != NULL)
625                 {
626                         if (zombie->chans[i].channel->name)
627                         {
628                                 chanrec* Ptr = zombie->chans[i].channel;
629                                 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
630                                 if (Ptr->topicset)
631                                 {
632                                         WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
633                                         WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
634                                 }
635                                 userlist(zombie,Ptr);
636                                 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
637
638                         }
639                 }
640         }
641         return true;
642 }
643
644 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
645 {
646         add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
647 }
648
649 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
650 {
651         add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
652 }
653
654 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
655 {
656         add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
657 }
658
659 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
660 {
661         add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
662 }
663
664 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
665 {
666         add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
667 }
668
669 bool Server::DelGLine(std::string hostmask)
670 {
671         return del_gline(hostmask.c_str());
672 }
673
674 bool Server::DelQLine(std::string nickname)
675 {
676         return del_qline(nickname.c_str());
677 }
678
679 bool Server::DelZLine(std::string ipaddr)
680 {
681         return del_zline(ipaddr.c_str());
682 }
683
684 bool Server::DelKLine(std::string hostmask)
685 {
686         return del_kline(hostmask.c_str());
687 }
688
689 bool Server::DelELine(std::string hostmask)
690 {
691         return del_eline(hostmask.c_str());
692 }
693
694 long Server::CalcDuration(std::string delta)
695 {
696         return duration(delta.c_str());
697 }
698
699 bool Server::IsValidMask(std::string mask)
700 {
701         const char* dest = mask.c_str();
702         if (strchr(dest,'!')==0)
703                 return false;
704         if (strchr(dest,'@')==0)
705                 return false;
706         for (unsigned int i = 0; i < strlen(dest); i++)
707                 if (dest[i] < 32)
708                         return false;
709         for (unsigned int i = 0; i < strlen(dest); i++)
710                 if (dest[i] > 126)
711                         return false;
712         unsigned int c = 0;
713         for (unsigned int i = 0; i < strlen(dest); i++)
714                 if (dest[i] == '!')
715                         c++;
716         if (c>1)
717                 return false;
718         c = 0;
719         for (unsigned int i = 0; i < strlen(dest); i++)
720                 if (dest[i] == '@')
721                         c++;
722         if (c>1)
723                 return false;
724
725         return true;
726 }
727
728 Module* Server::FindModule(std::string name)
729 {
730         for (int i = 0; i <= MODCOUNT; i++)
731         {
732                 if (module_names[i] == name)
733                 {
734                         return modules[i];
735                 }
736         }
737         return NULL;
738 }
739
740 ConfigReader::ConfigReader()
741 {
742         include_stack.clear();
743         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
744         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
745         this->readerror = LoadConf(CONFIG_FILE,this->cache,this->errorlog);
746         if (!this->readerror)
747                 this->error = CONF_FILE_NOT_FOUND;
748 }
749
750
751 ConfigReader::~ConfigReader()
752 {
753         if (this->cache)
754                 delete this->cache;
755         if (this->errorlog)
756                 delete this->errorlog;
757 }
758
759
760 ConfigReader::ConfigReader(std::string filename)
761 {
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 = 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 = 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 = 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 = 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 EnumConf(cache,tag.c_str());
874 }
875
876 int ConfigReader::EnumerateValues(std::string tag, int index)
877 {
878         return 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