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