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