]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules.cpp
Server::AddExtendedMode and Server::AddCommand will now throw exceptions when adding...
[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::OnPostOper(userrec* user, std::string opertype) { };
235 void            Module::OnInfo(userrec* user) { };
236 void            Module::OnWhois(userrec* source, userrec* dest) { };
237 int             Module::OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel) { return 0; };
238 int             Module::OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
239 int             Module::OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text,char status) { return 0; };
240 int             Module::OnUserPreNick(userrec* user, std::string newnick) { return 0; };
241 void            Module::OnUserPostNick(userrec* user, std::string oldnick) { };
242 int             Module::OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type) { return ACR_DEFAULT; };
243 void            Module::On005Numeric(std::string &output) { };
244 int             Module::OnKill(userrec* source, userrec* dest, std::string reason) { return 0; };
245 void            Module::OnLoadModule(Module* mod,std::string name) { };
246 void            Module::OnUnloadModule(Module* mod,std::string name) { };
247 void            Module::OnBackgroundTimer(time_t curtime) { };
248 void            Module::OnSendList(userrec* user, chanrec* channel, char mode) { };
249 int             Module::OnPreCommand(std::string command, char **parameters, int pcnt, userrec *user, bool validated) { return 0; };
250 bool            Module::OnCheckReady(userrec* user) { return true; };
251 void            Module::OnUserRegister(userrec* user) { };
252 int             Module::OnUserPreKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { return 0; };
253 void            Module::OnUserKick(userrec* source, userrec* user, chanrec* chan, std::string reason) { };
254 int             Module::OnRawMode(userrec* user, chanrec* chan, char mode, std::string param, bool adding, int pcnt) { return 0; };
255 int             Module::OnCheckInvite(userrec* user, chanrec* chan) { return 0; };
256 int             Module::OnCheckKey(userrec* user, chanrec* chan, std::string keygiven) { return 0; };
257 int             Module::OnCheckLimit(userrec* user, chanrec* chan) { return 0; };
258 int             Module::OnCheckBan(userrec* user, chanrec* chan) { return 0; };
259 int             Module::OnStats(char symbol, userrec* user) { return 0; };
260 int             Module::OnChangeLocalUserHost(userrec* user, std::string newhost) { return 0; };
261 int             Module::OnChangeLocalUserGECOS(userrec* user, std::string newhost) { return 0; };
262 int             Module::OnLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { return 0; };
263 void            Module::OnEvent(Event* event) { return; };
264 char*           Module::OnRequest(Request* request) { return NULL; };
265 int             Module::OnOperCompare(std::string password, std::string input) { return 0; };
266 void            Module::OnGlobalOper(userrec* user) { };
267 void            Module::OnGlobalConnect(userrec* user) { };
268 int             Module::OnAddBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
269 int             Module::OnDelBan(userrec* source, chanrec* channel,std::string banmask) { return 0; };
270 void            Module::OnRawSocketAccept(int fd, std::string ip, int localport) { };
271 int             Module::OnRawSocketWrite(int fd, char* buffer, int count) { return 0; };
272 void            Module::OnRawSocketClose(int fd) { };
273 int             Module::OnRawSocketRead(int fd, char* buffer, unsigned int count, int &readresult) { return 0; };
274 void            Module::OnUserMessage(userrec* user, void* dest, int target_type, std::string text, char status) { };
275 void            Module::OnUserNotice(userrec* user, void* dest, int target_type, std::string text, char status) { };
276 void            Module::OnRemoteKill(userrec* source, userrec* dest, std::string reason) { };
277 void            Module::OnUserInvite(userrec* source,userrec* dest,chanrec* channel) { };
278 void            Module::OnPostLocalTopicChange(userrec* user, chanrec* chan, std::string topic) { };
279 void            Module::OnGetServerDescription(std::string servername,std::string &description) { };
280 void            Module::OnSyncUser(userrec* user, Module* proto, void* opaque) { };
281 void            Module::OnSyncChannel(chanrec* chan, Module* proto, void* opaque) { };
282 void            Module::ProtoSendMode(void* opaque, int target_type, void* target, std::string modeline) { };
283 void            Module::OnSyncChannelMetaData(chanrec* chan, Module* proto,void* opaque, std::string extname) { };
284 void            Module::OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, std::string extname) { };
285 void            Module::OnSyncOtherMetaData(Module* proto, void* opaque) { };
286 void            Module::OnDecodeMetaData(int target_type, void* target, std::string extname, std::string extdata) { };
287 void            Module::ProtoSendMetaData(void* opaque, int target_type, void* target, std::string extname, std::string extdata) { };
288 void            Module::OnWallops(userrec* user, std::string text) { };
289 void            Module::OnChangeHost(userrec* user, std::string newhost) { };
290 void            Module::OnChangeName(userrec* user, std::string gecos) { };
291 void            Module::OnAddGLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
292 void            Module::OnAddZLine(long duration, userrec* source, std::string reason, std::string ipmask) { };
293 void            Module::OnAddKLine(long duration, userrec* source, std::string reason, std::string hostmask) { };
294 void            Module::OnAddQLine(long duration, userrec* source, std::string reason, std::string nickmask) { };
295 void            Module::OnAddELine(long duration, userrec* source, std::string reason, std::string hostmask) { };
296 void            Module::OnDelGLine(userrec* source, std::string hostmask) { };
297 void            Module::OnDelZLine(userrec* source, std::string ipmask) { };
298 void            Module::OnDelKLine(userrec* source, std::string hostmask) { };
299 void            Module::OnDelQLine(userrec* source, std::string nickmask) { };
300 void            Module::OnDelELine(userrec* source, std::string hostmask) { };
301 void            Module::OnCleanup(int target_type, void* item) { };
302 void            Module::Implements(char* Implements) { for (int j = 0; j < 255; j++) Implements[j] = 0; };
303 void            Module::OnChannelDelete(chanrec* chan) { };
304 Priority        Module::Prioritize() { return PRIORITY_DONTCARE; }
305 void            Module::OnSetAway(userrec* user) { };
306 void            Module::OnCancelAway(userrec* user) { };
307
308 /* server is a wrapper class that provides methods to all of the C-style
309  * exports in the core
310  */
311
312 Server::Server()
313 {
314 }
315
316 Server::~Server()
317 {
318 }
319
320 void Server::AddSocket(InspSocket* sock)
321 {
322         module_sockets.push_back(sock);
323 }
324
325 void Server::RemoveSocket(InspSocket* sock)
326 {
327         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
328         {
329                 InspSocket* s = (InspSocket*)*a;
330                 if (s == sock)
331                 {
332                         log(DEBUG,"Forcibly removed socket");
333                         ServerInstance->SE->DelFd(s->GetFd());
334                         s->Close();
335                         module_sockets.erase(a);
336                         delete s;
337                         return;
338                 }
339         }
340 }
341
342 long Server::PriorityAfter(std::string modulename)
343 {
344         for (unsigned int j = 0; j < Config->module_names.size(); j++)
345         {
346                 if (Config->module_names[j] == modulename)
347                 {
348                         return ((j << 8) | PRIORITY_AFTER);
349                 }
350         }
351         return PRIORITY_DONTCARE;
352 }
353
354 long Server::PriorityBefore(std::string modulename)
355 {
356         for (unsigned int j = 0; j < Config->module_names.size(); j++)
357         {
358                 if (Config->module_names[j] == modulename)
359                 {
360                         return ((j << 8) | PRIORITY_BEFORE);
361                 }
362         }
363         return PRIORITY_DONTCARE;
364 }
365
366 void Server::RehashServer()
367 {
368         WriteOpers("*** Rehashing config file");
369         Config->Read(false,NULL);
370 }
371
372 ServerConfig* Server::GetConfig()
373 {
374         return Config;
375 }
376
377 std::string Server::GetVersion()
378 {
379         return ServerInstance->GetVersionString();
380 }
381
382 void Server::DelSocket(InspSocket* sock)
383 {
384         for (std::vector<InspSocket*>::iterator a = module_sockets.begin(); a < module_sockets.end(); a++)
385         {
386                 if (*a == sock)
387                 {
388                         module_sockets.erase(a);
389                         return;
390                 }
391         }
392 }
393
394 void Server::SendOpers(std::string s)
395 {
396         WriteOpers("%s",s.c_str());
397 }
398
399 bool Server::MatchText(std::string sliteral, std::string spattern)
400 {
401         char literal[MAXBUF],pattern[MAXBUF];
402         strlcpy(literal,sliteral.c_str(),MAXBUF);
403         strlcpy(pattern,spattern.c_str(),MAXBUF);
404         return match(literal,pattern);
405 }
406
407 void Server::SendToModeMask(std::string modes, int flags, std::string text)
408 {
409         WriteMode(modes.c_str(),flags,"%s",text.c_str());
410 }
411
412 chanrec* Server::JoinUserToChannel(userrec* user, std::string cname, std::string key)
413 {
414         return add_channel(user,cname.c_str(),key.c_str(),false);
415 }
416
417 chanrec* Server::PartUserFromChannel(userrec* user, std::string cname, std::string reason)
418 {
419         return del_channel(user,cname.c_str(),reason.c_str(),false);
420 }
421
422 chanuserlist Server::GetUsers(chanrec* chan)
423 {
424         chanuserlist userl;
425         userl.clear();
426         std::map<char*,char*> *list = chan->GetUsers();
427         for (std::map<char*,char*>::iterator i = list->begin(); i != list->end(); i++)
428         {
429                 char* o = i->second;
430                 userl.push_back((userrec*)o);
431         }
432         return userl;
433 }
434 void Server::ChangeUserNick(userrec* user, std::string nickname)
435 {
436         force_nickchange(user,nickname.c_str());
437 }
438
439 void Server::KickUser(userrec* source, userrec* target, chanrec* chan, std::string reason)
440 {
441         if (source)
442         {
443                 kick_channel(source,target,chan,(char*)reason.c_str());
444         }
445         else
446         {
447                 server_kick_channel(target,chan,(char*)reason.c_str(),true);
448         }
449 }
450
451 void Server::QuitUser(userrec* user, std::string reason)
452 {
453         kill_link(user,reason.c_str());
454 }
455
456 bool Server::IsUlined(std::string server)
457 {
458         return is_uline(server.c_str());
459 }
460
461 void Server::CallCommandHandler(std::string commandname, char** parameters, int pcnt, userrec* user)
462 {
463         ServerInstance->Parser->CallHandler(commandname,parameters,pcnt,user);
464 }
465
466 bool Server::IsValidModuleCommand(std::string commandname, int pcnt, userrec* user)
467 {
468         return ServerInstance->Parser->IsValidCommand(commandname, pcnt, user);
469 }
470
471 void Server::Log(int level, std::string s)
472 {
473         log(level,"%s",s.c_str());
474 }
475
476 void Server::AddCommand(command_t *f)
477 {
478         if (!ServerInstance->Parser->CreateCommand(f))
479         {
480                 ModuleException err("Command "+std::string(f->command)+" already exists.");
481                 throw (err);
482         }
483 }
484
485 void Server::SendMode(char **parameters, int pcnt, userrec *user)
486 {
487         ServerInstance->ModeGrok->ServerMode(parameters,pcnt,user);
488 }
489
490 void Server::Send(int Socket, std::string s)
491 {
492         Write_NoFormat(Socket,s.c_str());
493 }
494
495 void Server::SendServ(int Socket, std::string s)
496 {
497         WriteServ_NoFormat(Socket,s.c_str());
498 }
499
500 void Server::SendFrom(int Socket, userrec* User, std::string s)
501 {
502         WriteFrom_NoFormat(Socket,User,s.c_str());
503 }
504
505 void Server::SendTo(userrec* Source, userrec* Dest, std::string s)
506 {
507         if (!Source)
508         {
509                 // if source is NULL, then the message originates from the local server
510                 Write(Dest->fd,":%s %s",this->GetServerName().c_str(),s.c_str());
511         }
512         else
513         {
514                 // otherwise it comes from the user specified
515                 WriteTo_NoFormat(Source,Dest,s.c_str());
516         }
517 }
518
519 void Server::SendChannelServerNotice(std::string ServName, chanrec* Channel, std::string text)
520 {
521         WriteChannelWithServ_NoFormat((char*)ServName.c_str(), Channel, text.c_str());
522 }
523
524 void Server::SendChannel(userrec* User, chanrec* Channel, std::string s,bool IncludeSender)
525 {
526         if (IncludeSender)
527         {
528                 WriteChannel_NoFormat(Channel,User,s.c_str());
529         }
530         else
531         {
532                 ChanExceptSender_NoFormat(Channel,User,0,s.c_str());
533         }
534 }
535
536 bool Server::CommonChannels(userrec* u1, userrec* u2)
537 {
538         return (common_channels(u1,u2) != 0);
539 }
540
541 void Server::SendCommon(userrec* User, std::string text,bool IncludeSender)
542 {
543         if (IncludeSender)
544         {
545                 WriteCommon_NoFormat(User,text.c_str());
546         }
547         else
548         {
549                 WriteCommonExcept_NoFormat(User,text.c_str());
550         }
551 }
552
553 void Server::SendWallops(userrec* User, std::string text)
554 {
555         WriteWallOps(User,false,"%s",text.c_str());
556 }
557
558 void Server::ChangeHost(userrec* user, std::string host)
559 {
560         ChangeDisplayedHost(user,host.c_str());
561 }
562
563 void Server::ChangeGECOS(userrec* user, std::string gecos)
564 {
565         ChangeName(user,gecos.c_str());
566 }
567
568 bool Server::IsNick(std::string nick)
569 {
570         return (isnick(nick.c_str()) != 0);
571 }
572
573 userrec* Server::FindNick(std::string nick)
574 {
575         return Find(nick);
576 }
577
578 userrec* Server::FindDescriptor(int socket)
579 {
580         return (socket < 65536 ? fd_ref_table[socket] : NULL);
581 }
582
583 chanrec* Server::FindChannel(std::string channel)
584 {
585         return FindChan(channel.c_str());
586 }
587
588 std::string Server::ChanMode(userrec* User, chanrec* Chan)
589 {
590         return cmode(User,Chan);
591 }
592
593 bool Server::IsOnChannel(userrec* User, chanrec* Chan)
594 {
595         return has_channel(User,Chan);
596 }
597
598 std::string Server::GetServerName()
599 {
600         return Config->ServerName;
601 }
602
603 std::string Server::GetNetworkName()
604 {
605         return Config->Network;
606 }
607
608 std::string Server::GetServerDescription()
609 {
610         return Config->ServerDesc;
611 }
612
613 Admin Server::GetAdmin()
614 {
615         return Admin(Config->AdminName,Config->AdminEmail,Config->AdminNick);
616 }
617
618
619
620 bool Server::AddExtendedMode(char modechar, int type, bool requires_oper, int params_when_on, int params_when_off)
621 {
622         if (((modechar >= 'A') && (modechar <= 'Z')) || ((modechar >= 'a') && (modechar <= 'z')))
623         {
624                 if (type == MT_SERVER)
625                 {
626                         ModuleException e("Modes of type MT_SERVER are reserved for future expansion");
627                         throw(e);
628                         return false;
629                 }
630                 if (((params_when_on>0) || (params_when_off>0)) && (type == MT_CLIENT))
631                 {
632                         ModuleException e("Parameters on MT_CLIENT modes are not supported");
633                         throw(e);
634                         return false;
635                 }
636                 if ((params_when_on>1) || (params_when_off>1))
637                 {
638                         ModuleException e("More than one parameter for an MT_CHANNEL mode is not yet supported");
639                         throw(e);
640                         return false;
641                 }
642                 return DoAddExtendedMode(modechar,type,requires_oper,params_when_on,params_when_off);
643         }
644         else
645         {
646                 ModuleException e("Muppet modechar detected.");
647                 throw(e);
648         }
649         return false;
650 }
651
652 bool Server::AddExtendedListMode(char modechar)
653 {
654         bool res = DoAddExtendedMode(modechar,MT_CHANNEL,false,1,1);
655         if (res)
656                 ModeMakeList(modechar);
657         return res;
658 }
659
660 int Server::CountUsers(chanrec* c)
661 {
662         return usercount(c);
663 }
664
665
666 bool Server::UserToPseudo(userrec* user,std::string message)
667 {
668         unsigned int old_fd = user->fd;
669         Write(old_fd,"ERROR :Closing link (%s@%s) [%s]",user->ident,user->host,message.c_str());
670         user->FlushWriteBuf();
671         user->ClearBuffer();
672         user->fd = FD_MAGIC_NUMBER;
673
674         if (find(local_users.begin(),local_users.end(),user) != local_users.end())
675         {
676                 local_users.erase(find(local_users.begin(),local_users.end(),user));
677                 log(DEBUG,"Delete local user");
678         }
679
680         ServerInstance->SE->DelFd(old_fd);
681         shutdown(old_fd,2);
682         close(old_fd);
683         return true;
684 }
685
686 bool Server::PseudoToUser(userrec* alive,userrec* zombie,std::string message)
687 {
688         log(DEBUG,"PseudoToUser");
689         zombie->fd = alive->fd;
690         FOREACH_MOD(I_OnUserQuit,OnUserQuit(alive,message));
691         alive->fd = FD_MAGIC_NUMBER;
692         alive->FlushWriteBuf();
693         alive->ClearBuffer();
694         // save these for later
695         std::string oldnick = alive->nick;
696         std::string oldhost = alive->host;
697         std::string oldident = alive->ident;
698         kill_link(alive,message.c_str());
699         if (find(local_users.begin(),local_users.end(),alive) != local_users.end())
700         {
701                 local_users.erase(find(local_users.begin(),local_users.end(),alive));
702                 log(DEBUG,"Delete local user");
703         }
704         // Fix by brain - cant write the user until their fd table entry is updated
705         fd_ref_table[zombie->fd] = zombie;
706         Write(zombie->fd,":%s!%s@%s NICK %s",oldnick.c_str(),oldident.c_str(),oldhost.c_str(),zombie->nick);
707         for (unsigned int i = 0; i < zombie->chans.size(); i++)
708         {
709                 if (zombie->chans[i].channel != NULL)
710                 {
711                         if (zombie->chans[i].channel->name)
712                         {
713                                 chanrec* Ptr = zombie->chans[i].channel;
714                                 WriteFrom(zombie->fd,zombie,"JOIN %s",Ptr->name);
715                                 if (Ptr->topicset)
716                                 {
717                                         WriteServ(zombie->fd,"332 %s %s :%s", zombie->nick, Ptr->name, Ptr->topic);
718                                         WriteServ(zombie->fd,"333 %s %s %s %d", zombie->nick, Ptr->name, Ptr->setby, Ptr->topicset);
719                                 }
720                                 userlist(zombie,Ptr);
721                                 WriteServ(zombie->fd,"366 %s %s :End of /NAMES list.", zombie->nick, Ptr->name);
722
723                         }
724                 }
725         }
726         if ((find(local_users.begin(),local_users.end(),zombie) == local_users.end()) && (zombie->fd != FD_MAGIC_NUMBER))
727                 local_users.push_back(zombie);
728
729         return true;
730 }
731
732 void Server::AddGLine(long duration, std::string source, std::string reason, std::string hostmask)
733 {
734         add_gline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
735 }
736
737 void Server::AddQLine(long duration, std::string source, std::string reason, std::string nickname)
738 {
739         add_qline(duration, source.c_str(), reason.c_str(), nickname.c_str());
740 }
741
742 void Server::AddZLine(long duration, std::string source, std::string reason, std::string ipaddr)
743 {
744         add_zline(duration, source.c_str(), reason.c_str(), ipaddr.c_str());
745 }
746
747 void Server::AddKLine(long duration, std::string source, std::string reason, std::string hostmask)
748 {
749         add_kline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
750 }
751
752 void Server::AddELine(long duration, std::string source, std::string reason, std::string hostmask)
753 {
754         add_eline(duration, source.c_str(), reason.c_str(), hostmask.c_str());
755 }
756
757 bool Server::DelGLine(std::string hostmask)
758 {
759         return del_gline(hostmask.c_str());
760 }
761
762 bool Server::DelQLine(std::string nickname)
763 {
764         return del_qline(nickname.c_str());
765 }
766
767 bool Server::DelZLine(std::string ipaddr)
768 {
769         return del_zline(ipaddr.c_str());
770 }
771
772 bool Server::DelKLine(std::string hostmask)
773 {
774         return del_kline(hostmask.c_str());
775 }
776
777 bool Server::DelELine(std::string hostmask)
778 {
779         return del_eline(hostmask.c_str());
780 }
781
782 long Server::CalcDuration(std::string delta)
783 {
784         return duration(delta.c_str());
785 }
786
787 bool Server::IsValidMask(std::string mask)
788 {
789         char* dest = (char*)mask.c_str();
790         if (strchr(dest,'!')==0)
791                 return false;
792         if (strchr(dest,'@')==0)
793                 return false;
794         for (char* i = dest; *i; i++)
795                 if (*i < 32)
796                         return false;
797         for (char* i = dest; *i; i++)
798                 if (*i > 126)
799                         return false;
800         unsigned int c = 0;
801         for (char* i = dest; *i; i++)
802                 if (*i == '!')
803                         c++;
804         if (c>1)
805                 return false;
806         c = 0;
807         for (char* i = dest; *i; i++)
808                 if (*i == '@')
809                         c++;
810         if (c>1)
811                 return false;
812
813         return true;
814 }
815
816 Module* Server::FindModule(std::string name)
817 {
818         for (int i = 0; i <= MODCOUNT; i++)
819         {
820                 if (Config->module_names[i] == name)
821                 {
822                         return modules[i];
823                 }
824         }
825         return NULL;
826 }
827
828 ConfigReader::ConfigReader()
829 {
830         Config->ClearStack();
831         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
832         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
833         this->readerror = Config->LoadConf(CONFIG_FILE,this->cache,this->errorlog);
834         if (!this->readerror)
835                 this->error = CONF_FILE_NOT_FOUND;
836 }
837
838
839 ConfigReader::~ConfigReader()
840 {
841         if (this->cache)
842                 delete this->cache;
843         if (this->errorlog)
844                 delete this->errorlog;
845 }
846
847
848 ConfigReader::ConfigReader(std::string filename)
849 {
850         Config->ClearStack();
851         this->cache = new std::stringstream(std::stringstream::in | std::stringstream::out);
852         this->errorlog = new std::stringstream(std::stringstream::in | std::stringstream::out);
853         this->readerror = Config->LoadConf(filename.c_str(),this->cache,this->errorlog);
854         if (!this->readerror)
855                 this->error = CONF_FILE_NOT_FOUND;
856 };
857
858 std::string ConfigReader::ReadValue(std::string tag, std::string name, int index)
859 {
860         char val[MAXBUF];
861         char t[MAXBUF];
862         char n[MAXBUF];
863         strlcpy(t,tag.c_str(),MAXBUF);
864         strlcpy(n,name.c_str(),MAXBUF);
865         int res = Config->ReadConf(cache,t,n,index,val);
866         if (!res)
867         {
868                 this->error = CONF_VALUE_NOT_FOUND;
869                 return "";
870         }
871         return val;
872 }
873
874 bool ConfigReader::ReadFlag(std::string tag, std::string name, int index)
875 {
876         char val[MAXBUF];
877         char t[MAXBUF];
878         char n[MAXBUF];
879         strlcpy(t,tag.c_str(),MAXBUF);
880         strlcpy(n,name.c_str(),MAXBUF);
881         int res = Config->ReadConf(cache,t,n,index,val);
882         if (!res)
883         {
884                 this->error = CONF_VALUE_NOT_FOUND;
885                 return false;
886         }
887         std::string s = val;
888         return ((s == "yes") || (s == "YES") || (s == "true") || (s == "TRUE") || (s == "1"));
889 }
890
891 long ConfigReader::ReadInteger(std::string tag, std::string name, int index, bool needs_unsigned)
892 {
893         char val[MAXBUF];
894         char t[MAXBUF];
895         char n[MAXBUF];
896         strlcpy(t,tag.c_str(),MAXBUF);
897         strlcpy(n,name.c_str(),MAXBUF);
898         int res = Config->ReadConf(cache,t,n,index,val);
899         if (!res)
900         {
901                 this->error = CONF_VALUE_NOT_FOUND;
902                 return 0;
903         }
904         for (char* i = val; *i; i++)
905         {
906                 if (!isdigit(*i))
907                 {
908                         this->error = CONF_NOT_A_NUMBER;
909                         return 0;
910                 }
911         }
912         if ((needs_unsigned) && (atoi(val)<0))
913         {
914                 this->error = CONF_NOT_UNSIGNED;
915                 return 0;
916         }
917         return atoi(val);
918 }
919
920 long ConfigReader::GetError()
921 {
922         long olderr = this->error;
923         this->error = 0;
924         return olderr;
925 }
926
927 void ConfigReader::DumpErrors(bool bail, userrec* user)
928 {
929         if (bail)
930         {
931                 printf("There were errors in your configuration:\n%s",errorlog->str().c_str());
932                 exit(0);
933         }
934         else
935         {
936                 char dataline[1024];
937                 if (user)
938                 {
939                         WriteServ(user->fd,"NOTICE %s :There were errors in the configuration file:",user->nick);
940                         while (!errorlog->eof())
941                         {
942                                 errorlog->getline(dataline,1024);
943                                 WriteServ(user->fd,"NOTICE %s :%s",user->nick,dataline);
944                         }
945                 }
946                 else
947                 {
948                         WriteOpers("There were errors in the configuration file:",user->nick);
949                         while (!errorlog->eof())
950                         {
951                                 errorlog->getline(dataline,1024);
952                                 WriteOpers(dataline);
953                         }
954                 }
955                 return;
956         }
957 }
958
959
960 int ConfigReader::Enumerate(std::string tag)
961 {
962         return Config->EnumConf(cache,tag.c_str());
963 }
964
965 int ConfigReader::EnumerateValues(std::string tag, int index)
966 {
967         return Config->EnumValues(cache, tag.c_str(), index);
968 }
969
970 bool ConfigReader::Verify()
971 {
972         return this->readerror;
973 }
974
975
976 FileReader::FileReader(std::string filename)
977 {
978         file_cache c;
979         readfile(c,filename.c_str());
980         this->fc = c;
981 }
982
983 FileReader::FileReader()
984 {
985 }
986
987 void FileReader::LoadFile(std::string filename)
988 {
989         file_cache c;
990         readfile(c,filename.c_str());
991         this->fc = c;
992 }
993
994
995 FileReader::~FileReader()
996 {
997 }
998
999 bool FileReader::Exists()
1000 {
1001         if (fc.size() == 0)
1002         {
1003                 return(false);
1004         }
1005         else
1006         {
1007                 return(true);
1008         }
1009 }
1010
1011 std::string FileReader::GetLine(int x)
1012 {
1013         if ((x<0) || ((unsigned)x>fc.size()))
1014                 return "";
1015         return fc[x];
1016 }
1017
1018 int FileReader::FileSize()
1019 {
1020         return fc.size();
1021 }
1022
1023
1024 std::vector<Module*> modules(255);
1025 std::vector<ircd_module*> factory(255);
1026
1027 int MODCOUNT  = -1;
1028
1029