]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
390efe5bf012cfd6dd066075aae6546a2e7cad29
[user/henk/code/inspircd.git] / src / modules.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "inspircd_config.h"
20 #include "inspircd.h"
21 #include "inspircd_io.h"
22 #include <unistd.h>
23 #include <sys/errno.h>
24 #include <time.h>
25 #include <string>
26 #ifdef GCC3
27 #include <ext/hash_map>
28 #else
29 #include <hash_map>
30 #endif
31 #include <map>
32 #include <sstream>
33 #include <vector>
34 #include <deque>
35 #include "users.h"
36 #include "ctables.h"
37 #include "globals.h"
38 #include "modules.h"
39 #include "dynamic.h"
40 #include "wildcard.h"
41 #include "message.h"
42 #include "mode.h"
43 #include "xline.h"
44 #include "commands.h"
45 #include "inspstring.h"
46 #include "helperfuncs.h"
47 #include "hashcomp.h"
48 #include "socket.h"
49 #include "socketengine.h"
50 #include "typedefs.h"
51 #include "modules.h"
52 #include "command_parse.h"
53
54 extern ServerConfig *Config;
55 extern InspIRCd* ServerInstance;
56 extern int MODCOUNT;
57 extern std::vector<Module*> modules;
58 extern std::vector<ircd_module*> factory;
59 extern std::vector<InspSocket*> module_sockets;
60 extern std::vector<userrec*> local_users;
61 extern time_t TIME;
62 class Server;
63 extern userrec* fd_ref_table[MAX_DESCRIPTORS];
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(I_OnEvent,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, std::string partmessage) { }
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, bool validated) { 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 int             Module::OnStats(char symbol, userrec* user) { return 0; };
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 void            Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
301 void            Module::OnChannelDelete(chanrec* chan) { };
302 Priority        Module::Prioritize() { return PRIORITY_DONTCARE; }
303
304 /* server is a wrapper class that provides methods to all of the C-style
305  * exports in the core
306  */
307
308 Server::Server()
309 {
310 }
311
312 Server::~Server()
313 {
314 }
315
316 void Server::AddSocket(InspSocket* sock)
317 {
318         module_sockets.push_back(sock);
319 }
320
321 void Server::RemoveSocket(InspSocket* sock)
322 {
323         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
324         {
325                 InspSocket* s = (InspSocket*)*a;
326                 if (s == sock)
327                 {
328                         log(DEBUG,"Forcibly removed socket");
329                         ServerInstance->SE->DelFd(s->GetFd());
330                         s->Close();
331                         module_sockets.erase(a);
332                         delete s;
333                         return;
334                 }
335         }
336 }
337
338 void Server::RehashServer()
339 {
340         WriteOpers("*** Rehashing config file");
341         Config->Read(false,NULL);
342 }
343
344 ServerConfig* Server::GetConfig()
345 {
346         return Config;
347 }
348
349 std::string Server::GetVersion()
350 {
351         return ServerInstance->GetVersionString();
352 }
353
354 void Server::DelSocket(InspSocket* sock)
355 {
356         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
357         {
358                 if (*a == sock)
359                 {
360                         module_sockets.erase(a);
361                         return;
362                 }
363         }
364 }
365
366 void Server::SendOpers(std::string s)
367 {
368         WriteOpers("%s",s.c_str());
369 }
370
371 bool Server::MatchText(std::string sliteral, std::string spattern)
372 {
373         char literal[MAXBUF],pattern[MAXBUF];
374         strlcpy(literal,sliteral.c_str(),MAXBUF);
375         strlcpy(pattern,spattern.c_str(),MAXBUF);
376         return match(literal,pattern);
377 }
378
379 void Server::SendToModeMask(std::string modes, int flags, std::string text)
380 {
381         WriteMode(modes.c_str(),flags,"%s",text.c_str());
382 }
383
384 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
385 {
386         return add_channel(user,cname.c_str(),key.c_str(),false);
387 }
388
389 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
390 {
391         return del_channel(user,cname.c_str(),reason.c_str(),false);
392 }
393
394 chanuserlist Server::GetUsers(chanrec* chan)
395 {
396         chanuserlist userl;
397         userl.clear();
398         std::map<char*,char*> *list = chan->GetUsers();
399         for (std::map<char*,char*>::iterator i = list->begin(); i != list->end(); i++)
400         {
401                 char* o = i->second;
402                 userl.push_back((userrec*)o);
403         }
404         return userl;
405 }
406 void Server::ChangeUserNick(userrec* user, std::string nickname)
407 {
408         force_nickchange(user,nickname.c_str());
409 }
410
411 void Server::QuitUser(userrec* user, std::string reason)
412 {
413         kill_link(user,reason.c_str());
414 }
415
416 bool Server::IsUlined(std::string server)
417 {
418         return is_uline(server.c_str());
419 }
420
421 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
422 {
423         ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
424 }
425
426 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
427 {
428         return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
429 }
430
431 void Server::Log(int level, std::string s)
432 {
433         log(level,"%s",s.c_str());
434 }
435
436 void Server::AddCommand(command_t *f)
437 {
438         ServerInstance->Parser->CreateCommand(f);
439 }
440
441 void Server::SendMode(char **parameters, int pcnt, userrec *user)
442 {
443         ServerInstance->ModeGrok->ServerMode(parameters,pcnt,user);
444 }
445
446 void Server::Send(int Socket, std::string s)
447 {
448         Write_NoFormat(Socket,s.c_str());
449 }
450
451 void Server::SendServ(int Socket, std::string s)
452 {
453         WriteServ_NoFormat(Socket,s.c_str());
454 }
455
456 void Server::SendFrom(int Socket, userrec* User, std::string s)
457 {
458         WriteFrom_NoFormat(Socket,User,s.c_str());
459 }
460
461 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
462 {
463         if (!Source)
464         {
465                 // if source is NULL, then the message originates from the local server
466                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
467         }
468         else
469         {
470                 // otherwise it comes from the user specified
471                 WriteTo_NoFormat(Source,Dest,s.c_str());
472         }
473 }
474
475 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
476 {
477         WriteChannelWithServ_NoFormat((char*)ServName.c_str(), Channel, text.c_str());
478 }
479
480 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
481 {
482         if (IncludeSender)
483         {
484                 WriteChannel_NoFormat(Channel,User,s.c_str());
485         }
486         else
487         {
488                 ChanExceptSender_NoFormat(Channel,User,s.c_str());
489         }
490 }
491
492 bool Server::CommonChannels(userrec* u1, userrec* u2)
493 {
494         return (common_channels(u1,u2) != 0);
495 }
496
497 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
498 {
499         if (IncludeSender)
500         {
501                 WriteCommon_NoFormat(User,text.c_str());
502         }
503         else
504         {
505                 WriteCommonExcept_NoFormat(User,text.c_str());
506         }
507 }
508
509 void Server::SendWallops(userrec* User, std::string text)
510 {
511         WriteWallOps(User,false,"%s",text.c_str());
512 }
513
514 void Server::ChangeHost(userrec* user, std::string host)
515 {
516         ChangeDisplayedHost(user,host.c_str());
517 }
518
519 void Server::ChangeGECOS(userrec* user, std::string gecos)
520 {
521         ChangeName(user,gecos.c_str());
522 }
523
524 bool Server::IsNick(std::string nick)
525 {
526         return (isnick(nick.c_str()) != 0);
527 }
528
529 userrec* Server::FindNick(std::string nick)
530 {
531         return Find(nick);
532 }
533
534 userrec* Server::FindDescriptor(int socket)
535 {
536         return (socket < 65536 ? fd_ref_table[socket] : NULL);
537 }
538
539 chanrec* Server::FindChannel(std::string channel)
540 {
541         return FindChan(channel.c_str());
542 }
543
544 std::string Server::ChanMode(userrec* User, chanrec* Chan)
545 {
546         return cmode(User,Chan);
547 }
548
549 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
550 {
551         return has_channel(User,Chan);
552 }
553
554 std::string Server::GetServerName()
555 {
556         return Config->ServerName;
557 }
558
559 std::string Server::GetNetworkName()
560 {
561         return Config->Network;
562 }
563
564 std::string Server::GetServerDescription()
565 {
566         return Config->ServerDesc;
567 }
568
569 Admin Server::GetAdmin()
570 {
571         return Admin(Config->AdminName,Config->AdminEmail,Config->AdminNick);
572 }
573
574
575
576 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
577 {
578         if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
579         {
580                 if (type == MT_SERVER)
581                 {
582                         log(DEBUG,"*** API ERROR *** Modes of type MT_SERVER are reserved for future expansion");
583                         return false;
584                 }
585                 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
586                 {
587                         log(DEBUG,"*** API ERROR *** Parameters on MT_CLIENT modes are not supported");
588                         return false;
589                 }
590                 if ((params_when_on>1) || (params_when_off>1))
591                 {
592                         log(DEBUG,"*** API ERROR *** More than one parameter for an MT_CHANNEL mode is not yet supported");
593                         return false;
594                 }
595                 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
596         }
597         else
598         {
599                 log(DEBUG,"*** API ERROR *** Muppet modechar detected.");
600         }
601         return false;
602 }
603
604 bool Server::AddExtendedListMode(char modechar)
605 {
606         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
607         if (res)
608                 ModeMakeList(modechar);
609         return res;
610 }
611
612 int Server::CountUsers(chanrec* c)
613 {
614         return usercount(c);
615 }
616
617
618 bool Server::UserToPseudo(userrec* user,std::string message)
619 {
620         unsigned int old_fd = user->fd;
621         Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
622         user->FlushWriteBuf();
623         user->ClearBuffer();
624         user->fd = FD_MAGIC_NUMBER;
625
626         if (find(local_users.begin(),local_users.end(),user) != local_users.end())
627         {
628                 local_users.erase(find(local_users.begin(),local_users.end(),user));
629                 log(DEBUG,"Delete local user");
630         }
631
632         ServerInstance->SE->DelFd(old_fd);
633         shutdown(old_fd,2);
634         close(old_fd);
635         return true;
636 }
637
638 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
639 {
640         log(DEBUG,"PseudoToUser");
641         zombie->fd = alive->fd;
642         FOREACH_MOD(I_OnUserQuit,OnUserQuit(alive,message));
643         alive->fd = FD_MAGIC_NUMBER;
644         alive->FlushWriteBuf();
645         alive->ClearBuffer();
646         Write(zombie->fd,":%s!%s@%s NICK %s",alive->nick,alive->ident,alive->host,zombie->nick);
647         kill_link(alive,message.c_str());
648
649         if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
650         {
651                 local_users.erase(find(local_users.begin(),local_users.end(),alive));
652                 log(DEBUG,"Delete local user");
653         }
654
655         fd_ref_table[zombie->fd] = zombie;
656         for (unsigned int i = 0; i < zombie->chans.size(); i++)
657         {
658                 if (zombie->chans[i].channel != NULL)
659                 {
660                         if (zombie->chans[i].channel->name)
661                         {
662                                 chanrec* Ptr = zombie->chans[i].channel;
663                                 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
664                                 if (Ptr->topicset)
665                                 {
666                                         WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
667                                         WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
668                                 }
669                                 userlist(zombie,Ptr);
670                                 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
671
672                         }
673                 }
674         }
675         if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
676                 local_users.push_back(zombie);
677
678         return true;
679 }
680
681 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
682 {
683         add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
684 }
685
686 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
687 {
688         add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
689 }
690
691 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
692 {
693         add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
694 }
695
696 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
697 {
698         add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
699 }
700
701 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
702 {
703         add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
704 }
705
706 bool Server::DelGLine(std::string hostmask)
707 {
708         return del_gline(hostmask.c_str());
709 }
710
711 bool Server::DelQLine(std::string nickname)
712 {
713         return del_qline(nickname.c_str());
714 }
715
716 bool Server::DelZLine(std::string ipaddr)
717 {
718         return del_zline(ipaddr.c_str());
719 }
720
721 bool Server::DelKLine(std::string hostmask)
722 {
723         return del_kline(hostmask.c_str());
724 }
725
726 bool Server::DelELine(std::string hostmask)
727 {
728         return del_eline(hostmask.c_str());
729 }
730
731 long Server::CalcDuration(std::string delta)
732 {
733         return duration(delta.c_str());
734 }
735
736 bool Server::IsValidMask(std::string mask)
737 {
738         const char* dest = mask.c_str();
739         if (strchr(dest,'!')==0)
740                 return false;
741         if (strchr(dest,'@')==0)
742                 return false;
743         for (unsigned int i = 0; i < strlen(dest); i++)
744                 if (dest[i] < 32)
745                         return false;
746         for (unsigned int i = 0; i < strlen(dest); i++)
747                 if (dest[i] > 126)
748                         return false;
749         unsigned int c = 0;
750         for (unsigned int i = 0; i < strlen(dest); i++)
751                 if (dest[i] == '!')
752                         c++;
753         if (c>1)
754                 return false;
755         c = 0;
756         for (unsigned int i = 0; i < strlen(dest); i++)
757                 if (dest[i] == '@')
758                         c++;
759         if (c>1)
760                 return false;
761
762         return true;
763 }
764
765 Module* Server::FindModule(std::string name)
766 {
767         for (int i = 0; i <= MODCOUNT; i++)
768         {
769                 if (Config->module_names[i] == name)
770                 {
771                         return modules[i];
772                 }
773         }
774         return NULL;
775 }
776
777 ConfigReader::ConfigReader()
778 {
779         Config->ClearStack();
780         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
781         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
782         this->readerror = Config->LoadConf(CONFIG_FILE,this->cache,this->errorlog);
783         if (!this->readerror)
784                 this->error = CONF_FILE_NOT_FOUND;
785 }
786
787
788 ConfigReader::~ConfigReader()
789 {
790         if (this->cache)
791                 delete this->cache;
792         if (this->errorlog)
793                 delete this->errorlog;
794 }
795
796
797 ConfigReader::ConfigReader(std::string filename)
798 {
799         Config->ClearStack();
800         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
801         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
802         this->readerror = Config->LoadConf(filename.c_str(),this->cache,this->errorlog);
803         if (!this->readerror)
804                 this->error = CONF_FILE_NOT_FOUND;
805 };
806
807 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
808 {
809         char val[MAXBUF];
810         char t[MAXBUF];
811         char n[MAXBUF];
812         strlcpy(t,tag.c_str(),MAXBUF);
813         strlcpy(n,name.c_str(),MAXBUF);
814         int res = Config->ReadConf(cache,t,n,index,val);
815         if (!res)
816         {
817                 this->error = CONF_VALUE_NOT_FOUND;
818                 return "";
819         }
820         return val;
821 }
822
823 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
824 {
825         char val[MAXBUF];
826         char t[MAXBUF];
827         char n[MAXBUF];
828         strlcpy(t,tag.c_str(),MAXBUF);
829         strlcpy(n,name.c_str(),MAXBUF);
830         int res = Config->ReadConf(cache,t,n,index,val);
831         if (!res)
832         {
833                 this->error = CONF_VALUE_NOT_FOUND;
834                 return false;
835         }
836         std::string s = val;
837         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
838 }
839
840 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
841 {
842         char val[MAXBUF];
843         char t[MAXBUF];
844         char n[MAXBUF];
845         strlcpy(t,tag.c_str(),MAXBUF);
846         strlcpy(n,name.c_str(),MAXBUF);
847         int res = Config->ReadConf(cache,t,n,index,val);
848         if (!res)
849         {
850                 this->error = CONF_VALUE_NOT_FOUND;
851                 return 0;
852         }
853         for (unsigned int i = 0; i < strlen(val); i++)
854         {
855                 if (!isdigit(val[i]))
856                 {
857                         this->error = CONF_NOT_A_NUMBER;
858                         return 0;
859                 }
860         }
861         if ((needs_unsigned) && (atoi(val)<0))
862         {
863                 this->error = CONF_NOT_UNSIGNED;
864                 return 0;
865         }
866         return atoi(val);
867 }
868
869 long ConfigReader::GetError()
870 {
871         long olderr = this->error;
872         this->error = 0;
873         return olderr;
874 }
875
876 void ConfigReader::DumpErrors(bool bail, userrec* user)
877 {
878         if (bail)
879         {
880                 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
881                 exit(0);
882         }
883         else
884         {
885                 char dataline[1024];
886                 if (user)
887                 {
888                         WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
889                         while (!errorlog->eof())
890                         {
891                                 errorlog->getline(dataline,1024);
892                                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
893                         }
894                 }
895                 else
896                 {
897                         WriteOpers("There were errors in the configuration file:",user->nick);
898                         while (!errorlog->eof())
899                         {
900                                 errorlog->getline(dataline,1024);
901                                 WriteOpers(dataline);
902                         }
903                 }
904                 return;
905         }
906 }
907
908
909 int ConfigReader::Enumerate(std::string tag)
910 {
911         return Config->EnumConf(cache,tag.c_str());
912 }
913
914 int ConfigReader::EnumerateValues(std::string tag, int index)
915 {
916         return Config->EnumValues(cache, tag.c_str(), index);
917 }
918
919 bool ConfigReader::Verify()
920 {
921         return this->readerror;
922 }
923
924
925 FileReader::FileReader(std::string filename)
926 {
927         file_cache c;
928         readfile(c,filename.c_str());
929         this->fc = c;
930 }
931
932 FileReader::FileReader()
933 {
934 }
935
936 void FileReader::LoadFile(std::string filename)
937 {
938         file_cache c;
939         readfile(c,filename.c_str());
940         this->fc = c;
941 }
942
943
944 FileReader::~FileReader()
945 {
946 }
947
948 bool FileReader::Exists()
949 {
950         if (fc.size() == 0)
951         {
952                 return(false);
953         }
954         else
955         {
956                 return(true);
957         }
958 }
959
960 std::string FileReader::GetLine(int x)
961 {
962         if ((x<0) || ((unsigned)x>fc.size()))
963                 return "";
964         return fc[x];
965 }
966
967 int FileReader::FileSize()
968 {
969         return fc.size();
970 }
971
972
973 std::vector<Module*> modules(255);
974 std::vector<ircd_module*> factory(255);
975
976 int MODCOUNT  = -1;
977
978